Improve how we unmount dirs
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Sun, 23 Jun 2019 14:23:56 +0000 (16:23 +0200)
committerJonatan Schlag <jonatan.schlag@ipfire.org>
Sun, 23 Jun 2019 14:23:56 +0000 (16:23 +0200)
Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
functions.sh

index 095e3dd..f666c30 100644 (file)
@@ -20,8 +20,6 @@ FALSE=1
 
 IMAGE_RELEASE=${FALSE}
 
-# maybe mounted dirs
-MOUNTED_DIRS="/proc" "/sys" "dev/pts" "dev/shm" "dev" "run" "tmp" ""
 
 
 # Functions
@@ -53,26 +51,55 @@ cmd() {
 }
 
 
-cleanup_stage_1() {
+is_mounted() {
+       local mounted_dir=${1}
+
+       if [ ! -d ${mounted_dir} ]; then
+               log ERROR "Is not a directory ${mounted_dir}"
+               return ${FALSE}
+       else
+               mountpoint ${mounted_dir}
+       fi
+}
 
+unmount_dirs() {
        local mounted_dir
-       for mounted_dir in  ${MOUNTED_DIRS}
-       do
+       local path
+
+       local return_value=${EXIT_OK}
 
-               if is_mounted "${IMAGE_MOUNT_DIR}${mounted_dir}"; then
-                       umount  "${IMAGE_MOUNT_DIR}${mounted_dir}"
+       for mounted_dir in "/proc" "/sys" "/dev/pts" "/dev/shm" "/dev" "/run" "/tmp" ""
+       do
+               path="${IMAGE_MOUNT_DIR}${mounted_dir}"
+
+               if is_mounted "${path}"; then
+                       log DEBUG "Unmounting ${path}"
+                       umount  "${path}"
+                       # Check once again
+                       if is_mounted "${path}"; then
+                               return_value=${EXIT_ERROR}
+                       fi
                else
-                       log DEBUG "${IMAGE_MOUNT_DIR}${mounted_dir} is not  mounted"
+                       log DEBUG "${path} is not  mounted"
                fi
        done
 
+}
+
+
+
+
+cleanup_stage_1() {
 
-       # Remove partition from the kernel table.
-       partx -d ${outlo}p1
+       if unmount_dirs; then
 
-       # Remove loopback device
-       losetup -d ${outlo}
+               # Remove partition from the kernel table.
+               partx -d ${outlo}p1
 
+               # Remove loopback device
+               log DEBUG " Remove loopback device"
+               losetup -d ${outlo}
+       fi
 
 
        # Cleanup Config file for the local repo
@@ -82,35 +109,14 @@ cleanup_stage_1() {
 
 }
 
-is_mounted() {
-       local mounted_dir=${1}
-
-       if [ ! -d ${mounted_dir} ]; then
-               log ERROR "Is not a directory ${mounted_dir}"
-               return ${FALSE}
-       else
-               mountpoint ${mounted_dir}
-       fi
-}
 
 cleanup_stage_2() {
-       #Check that nothing is mounted in working dir
-       local mounted_dir
-       for mounted_dir in ${MOUNTED_DIRS}
-       do
-
-               if is_mounted "${IMAGE_MOUNT_DIR}${mounted_dir}"; then
-                       log ERROR "${IMAGE_MOUNT_DIR}${mounted_dir} is still mounted"
-                       return ${EXIT_ERROR}
-               else
-                       log DEBUG "${IMAGE_MOUNT_DIR}${mounted_dir} is not  mounted"
-
+       if unmount_dirs; then
+               # Drop working dir
+               if [ -d "${WORKING_DIR}" ]; then
+                       #rm -dfR "${WORKING_DIR}"
+                       echo ""
                fi
-       done
-
-       # Drop working dir
-       if [ -d "${WORKING_DIR}" ]; then
-               rm -dfR "${WORKING_DIR}"
        fi
 }