From 53c57d75c786f7b4f1aa267c93d2c5825e232767 Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Thu, 28 Jan 2016 13:14:57 +1100 Subject: [PATCH] Cleanup unmount_dir function Cleanup this function to work with a symlinked directory. Document it's behaviour more exactly, and add a simple unit-test for it (not run by default, due to doing things like mounting and unmounting system dirs on a live system, which doesn't seem safe for CI. But it is useful for developers ensuring sanity). Change-Id: I335316019ef948758392b03e91f9869102a472b9 --- lib/common-functions | 28 +++++++++++++++------ tests/dib_functions_test.sh | 50 +++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 7 deletions(-) create mode 100755 tests/dib_functions_test.sh diff --git a/lib/common-functions b/lib/common-functions index b8ded27f5..0f1b25658 100644 --- a/lib/common-functions +++ b/lib/common-functions @@ -281,12 +281,26 @@ function mount_proc_dev_sys () { sudo mount -t sysfs none $TMP_MOUNT_PATH/sys } -function unmount_dir () { - local pattern="$1" mnts="" - if [ -n "$pattern" ] && awk '{print $2}' < /proc/mounts | grep "^$pattern"; then - mnts=`awk '{print $2}' < /proc/mounts | grep "^$pattern" | sort -r` - fi - if [ -n "$mnts" ]; then - sudo umount -fl $mnts || true +# Recursively unmount directories under a given directory DIR +# usage: +# unmount_dir DIR +function unmount_dir { + local dir="$1" + local real_dir + local mnts + + if [ ! -d $dir ]; then + echo "*** $dir is not a directory" + return 1 fi + + # get rid of any symlink elements in the incoming path, because + # /proc/mounts is the real path + real_dir=$(readlink -e $dir) + + mnts=$(awk '{print $2}' < /proc/mounts | grep "^$real_dir" | sort -r) + for m in $mnts; do + echo "Unmount $m" + sudo umount -fl $m || true + done } diff --git a/tests/dib_functions_test.sh b/tests/dib_functions_test.sh new file mode 100755 index 000000000..a1a497367 --- /dev/null +++ b/tests/dib_functions_test.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +# unit testing for some of the common-functions +# +# This is fairly invasive and *may* leave behind mounts, etc, that +# need a human in the loop. Thus it's mostly useful for developers +# during testing, but not so great for CI + +source ../lib/common-functions + +# +# Directory mounting and unmounting +# + +# make mount points +TMP_DIR=$(mktemp -d) +cd $TMP_DIR +mkdir mnt +mkdir mnt/proc mnt/dev mnt/dev/pts mnt/sysfs mnt/sys + +# for extra complexity, simulate the path being behind a symlink +ln -s mnt mnt-symlink +TMP_MOUNT_PATH=$TMP_DIR/mnt-symlink + +# mount devices +mount_proc_dev_sys + +if [ $(grep "$TMP_DIR" /proc/mounts | wc -l) -ne 4 ]; then + echo "*** FAILED to mount all directories" + # we might be in an unclean state here, but something is broken... + # we don't want to delete mounted system directories + return 1 +else + echo "*** PASS : mounted all directories" +fi + +# umount devices +unmount_dir $TMP_MOUNT_PATH + +if [ $(grep "$TMP_DIR" /proc/mounts | wc -l) -ne 0 ]; then + echo "*** FAILED due to mounts being left behind" + return 1 +else + echo "*** PASS all directories unmounted" +fi + +# cleanup +rm -rf $TMP_DIR + +### TODO more tests here