From 5b305ffa7b787f864878d53856f3207f1b3025b4 Mon Sep 17 00:00:00 2001 From: Yolanda Robla Date: Fri, 12 May 2017 18:41:37 +0200 Subject: [PATCH] Only unmount directories that are mounted To avoid failures with double unmount, skip unmounting the mountpoints that are managed by block device. Change-Id: I228779eb9bf544a27a53e5017c87573023fd375a --- diskimage_builder/lib/common-functions | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/diskimage_builder/lib/common-functions b/diskimage_builder/lib/common-functions index 7faee8f2..546907db 100644 --- a/diskimage_builder/lib/common-functions +++ b/diskimage_builder/lib/common-functions @@ -343,6 +343,8 @@ function unmount_dir { local dir="$1" local real_dir local mnts + local split_mounts + local found_mountpoint if [ ! -d $dir ]; then echo "*** $dir is not a directory" @@ -353,12 +355,30 @@ function unmount_dir { # /proc/mounts is the real path real_dir=$(readlink -e $dir) + # populate the exported mountpoints + IFS='|' read -ra split_mounts <<< "$DIB_MOUNTPOINTS" + + # note the "/" on real_dir ... we are just looking for things # mounted *underneath* this directory. mnts=$(awk '{print $2}' < /proc/mounts | grep "^$real_dir/" | sort -r) for m in $mnts; do - echo "Unmount $m" - sudo umount -fl $m || true + # check if suffix is in array + found_mountpoint=false + for mountpoint in "${split_mounts[@]}"; do + if [[ "$mountpoint" != "/" ]]; then + if [[ "$m" == *$mountpoint ]]; then + echo "Mountpoint $m managed by block device; skipping" + found_mountpoint=true + break + fi + fi + done + if [ $found_mountpoint == false ]; then + # unmount the directory as it is not managed by block device + echo "Unmount $m" + sudo umount -fl $m || true + fi done }