]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
rbdmap: unmap RBDMAPFILE images unless called with unmap-all
authorDavid Disseldorp <ddiss@suse.de>
Fri, 10 Feb 2017 16:50:12 +0000 (17:50 +0100)
committerNathan Cutler <ncutler@suse.com>
Wed, 22 Mar 2017 10:54:39 +0000 (11:54 +0100)
When called with a "map" parameter, the rbdmap script iterates the list
of images present in RBDMAPFILE (/etc/ceph/rbdmap), and maps each entry.
When called with "unmap", rbdmap currently iterates *all* mapped RBD
images and unmaps each one, regardless of whether it's listed in the
RBDMAPFILE or not.

This commit adds functionality such that only RBD images listed in the
configuration file are unmapped. This behaviour is the new default for
"rbdmap unmap". A new "unmap-all" parameter is added to offer the old
unmap-all-rbd-images behaviour, which is used by the systemd service.

Fixes: http://tracker.ceph.com/issues/18884
Signed-off-by: David Disseldorp <ddiss@suse.de>
(cherry picked from commit e58413abf408cbe254232e563f3e30d2dc0d707c)

src/rbdmap
systemd/rbdmap.service

index ec58261c4a7218d8798355217dc53b6cef165781..25de4648398d62aef955c92c3d60a88b26aff6d3 100755 (executable)
@@ -1,15 +1,6 @@
 #!/bin/bash
 
 do_map() {
-
-        # default to reasonable value if RBDMAPFILE not set in environment
-        printenv RBDMAPFILE >/dev/null || local RBDMAPFILE=/etc/ceph/rbdmap
-
-       if [ ! -f "$RBDMAPFILE" ]; then
-               logger -p "daemon.warning" -t rbdmap "No $RBDMAPFILE found."
-               exit 0
-       fi
-
        # Read /etc/rbdtab to create non-existant mapping
        RET=0
        while read DEV PARAMS; do
@@ -65,7 +56,33 @@ do_map() {
 
 }
 
-do_unmap() {
+unmount_unmap() {
+       local rbd_dev=$1
+       local mnt=$(findmnt --mtab --source ${rbd_dev} --noheadings \
+                                                       | awk '{print $1'})
+
+       logger -p "daemon.debug" -t rbdmap "Unmapping '${rbd_dev}'"
+       if [ -n "${mnt}" ]; then
+           logger -p "daemon.debug" -t rbdmap "Unmounting '${mnt}'"
+           umount "${mnt}" >>/dev/null 2>&1
+       fi
+       if mountpoint -q "${mnt}"; then
+           ## Un-mounting failed.
+           logger -p "daemon.warning" -t rbdmap "Failed to unmount '${mnt}'"
+           return 1
+       fi
+       ## Un-mapping.
+       rbd unmap $rbd_dev >>/dev/null 2>&1
+       if [ $? -ne 0 ]; then
+           logger -p "daemon.warning" -t rbdmap "Failed to unmap '${mnt}'"
+           return 1
+       fi
+       logger -p "daemon.debug" -t rbdmap "Unmapped '${rbd_dev}'"
+
+       return 0
+}
+
+do_unmap_all() {
        RET=0
        ## Unmount and unmap all rbd devices
        if ls /dev/rbd[0-9]* >/dev/null 2>&1; then
@@ -81,31 +98,57 @@ do_unmap() {
                            fi
                        done
 
-                       logger -p "daemon.debug" -t rbdmap "Unmapping '${DEV}'"
-                       MNT=$(findmnt --mtab --source ${DEV} --noheadings | awk '{print $1'})
-                       if [ -n "${MNT}" ]; then
-                           logger -p "daemon.debug" -t rbdmap "Unmounting '${MNT}'"
-                           umount "${MNT}" >>/dev/null 2>&1
-                       fi
-                       if mountpoint -q "${MNT}"; then
-                           ## Un-mounting failed.
-                           logger -p "daemon.warning" -t rbdmap "Failed to unmount '${MNT}'"
-                           RET=$((${RET}+1))
-                           continue
-                       fi
-                       ## Un-mapping.
-                       rbd unmap $DEV >>/dev/null 2>&1
-                       if [ $? -ne 0 ]; then
-                           logger -p "daemon.warning" -t rbdmap "Failed to unmap '${MNT}'"
-                           RET=$((${RET}+$?))
-                           continue
-                       fi
-                       logger -p "daemon.debug" -t rbdmap "Unmapped '${DEV}'"
+                       unmount_unmap "$DEV" || RET=$((${RET}+$?))
+
                done
        fi
        exit ${RET}
 }
 
+do_unmap() {
+       RET=0
+       ## skip if nothing is mapped
+       ls /dev/rbd[0-9]* >/dev/null 2>&1 || exit ${RET}
+
+       # Read /etc/rbdtab to create non-existant mapping
+       while read DEV PARAMS; do
+               case "$DEV" in
+                 ""|\#*)
+                       continue
+                       ;;
+                 */*)
+                       ;;
+                 *)
+                       DEV=rbd/$DEV
+                       ;;
+               esac
+
+               MAP_RV="$(readlink -f /dev/rbd/$DEV)"
+               if [ ! -b $MAP_RV ]; then
+                       logger -p "daemon.debug" -t rbdmap "$DEV not mapped, skipping unmap"
+                       continue
+               fi
+
+               ## pre-unmapping
+               if [ -x "/etc/ceph/rbd.d/${DEV}" ]; then
+                       logger -p "daemon.debug" -t rbdmap "Running pre-unmap hook '/etc/ceph/rbd.d/${DEV}'"
+                       /etc/ceph/rbd.d/${DEV} unmap "/dev/rbd/${DEV}"
+               fi
+
+               unmount_unmap "$MAP_RV" || RET=$((${RET}+$?))
+
+       done < $RBDMAPFILE
+       exit ${RET}
+}
+
+# default to reasonable value if RBDMAPFILE not set in environment
+RBDMAPFILE="${RBDMAPFILE:-/etc/ceph/rbdmap}"
+
+if [ ! -f "$RBDMAPFILE" ]; then
+       logger -p "daemon.warning" -t rbdmap "No $RBDMAPFILE found."
+       exit 0
+fi
+
 case "$1" in
   map)
        do_map
@@ -114,6 +157,11 @@ case "$1" in
   unmap)
        do_unmap
        ;;
+
+  unmap-all)
+       do_unmap_all
+       ;;
+
   *)
-       echo "Usage: rbdmap map | unmap"
+       echo "Usage: rbdmap map | unmap | unmap-all"
 esac
index 70501eaca7d861d2318b6f4b7fd1751970e89853..15e64abb4b0b44ae7436a162219ff879abee8c55 100644 (file)
@@ -11,7 +11,7 @@ Type=oneshot
 RemainAfterExit=yes
 ExecStart=/usr/bin/rbdmap map
 ExecReload=/usr/bin/rbdmap map
-ExecStop=/usr/bin/rbdmap unmap
+ExecStop=/usr/bin/rbdmap unmap-all
 
 [Install]
 WantedBy=multi-user.target