From 74a84259c5a896ca3716f23a41e59af11ddb05d4 Mon Sep 17 00:00:00 2001 From: Zorro Lang Date: Mon, 13 Feb 2017 19:55:42 -0500 Subject: [PATCH] common/rc: new functions for multi-level mount/umount operations When I try to write cases about mount shared subtrees test, I find I always need to do many mount operations, then then umount those mount point one by one. To make the code clear, I use a stack to save mounted points sequentially, then I write 3 common functions to operate this stack. 1. The global stack named MOUNTED_POINT_STACK 2. _get_mount() accepts mount parameters like _mount() does, but the mountpoint parameter must be the last one. It will run the mount operation and push the mountpoint name into stack. 3. _put_mount() doesn't need any parameter. It will pop the newest mountpoint name from the stack, and umount it. 4. _clear_mount_stack() doesn't need any parameter either. It will umount all mountpoints in the stack sequentially, and set MOUNTED_POINT_STACK="" Generally, the _clear_mount_stack() function also can be used as _init_mount_stack() at the beginning of a case. Because it will prepare an empty stack. [eguan: add comments and fix code style] Signed-off-by: Zorro Lang Signed-off-by: Theodore Ts'o Reviewed-by: Eryu Guan Signed-off-by: Eryu Guan --- common/rc | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/common/rc b/common/rc index 76515e2b..f5bc0323 100644 --- a/common/rc +++ b/common/rc @@ -160,6 +160,41 @@ _mount() $MOUNT_PROG `_mount_ops_filter $*` } +# Call _mount to do mount operation but also save mountpoint to +# MOUNTED_POINT_STACK. Note that the mount point must be the last parameter +_get_mount() +{ + local mnt_point=${!#} + + _mount $* + if [ $? -eq 0 ]; then + MOUNTED_POINT_STACK="$mnt_point $MOUNTED_POINT_STACK" + else + return 1 + fi +} + +# Unmount the last mounted mountpoint in MOUNTED_POINT_STACK +# and return it to caller +_put_mount() +{ + local last_mnt=`echo $MOUNTED_POINT_STACK | awk '{print $1}'` + + if [ -n "$last_mnt" ]; then + $UMOUNT_PROG $last_mnt + fi + MOUNTED_POINT_STACK=`echo $MOUNTED_POINT_STACK | cut -d\ -f2-` +} + +# Unmount all mountpoints in MOUNTED_POINT_STACK and clear the stack +_clear_mount_stack() +{ + if [ -n "$MOUNTED_POINT_STACK" ]; then + $UMOUNT_PROG $MOUNTED_POINT_STACK + fi + MOUNTED_POINT_STACK="" +} + _scratch_options() { type=$1 -- 2.39.5