diff options
author | Justin Boffemmyer | 2018-02-21 11:23:11 +0900 |
---|---|---|
committer | Justin Boffemmyer | 2018-02-21 11:23:11 +0900 |
commit | 0e1939ba51da8d792a437a7965a188a62bc4c0f3 (patch) | |
tree | 5bb17ec43cc887260702aed114e4f7b86070cc4d | |
parent | 77068c26f27de7dffd960c250f18a5f440cf101c (diff) |
cauldron: lib.chroot: refactor done + cleanup
Refactor the shared code in cauldron_chroot_done and cauldron_chroot_cleanup
into a separate, new function cauldron_chroot_close. The usage of
cauldron_chroot_close is potentially dangerous (whereas cleanup is guaranteed
dangerous), so it should be used carefully.
Additionally, rather than hardcoding the mountpoints or relying on a recursive
umount, which may not be available on the host system, get the list of mount
points to umount from the list of what's actually mounted in that chroot, but
sorted in reverse order (to guarantee that nested subdirs are unmounted before
their parents).
-rw-r--r-- | cauldron/lib/lib.chroot | 76 |
1 files changed, 53 insertions, 23 deletions
diff --git a/cauldron/lib/lib.chroot b/cauldron/lib/lib.chroot index d499c92..ad9698f 100644 --- a/cauldron/lib/lib.chroot +++ b/cauldron/lib/lib.chroot @@ -106,6 +106,55 @@ function cauldron_chroot_init() { } #------------------------------------------------------------------------------- +## @param chroot index (optional) +## @param target chroot paths (optional) +## +## Unmounts required mount points in the chroot and unsets the related CHROOT +## variables. If no target chroot is not provided, it will close ALL known +## chroot paths. +## +#------------------------------------------------------------------------------- +function cauldron_chroot_close() { + local index="$1" + local chroot= + local target= + + shift 1 + chroot=( "$@" ) + [[ -z "$chroot" ]] && chroot=( "${LIBCHROOT_PATHS[@]}" ) + + for target in "${chroot[@]}" + do + local mounts=( $(grep -o "$target[^[:space:]]*" | sort -r) ) + [[ -n "$mounts" ]] || mounts=( sys run dev/pts dev proc ) + cauldron_verbose "closing chroot mounts: ${mounts[@]}" + + # unmount the chroot mount points + for mnt in "${mounts[@]}" + do + if grep -q "$mnt" "$target/proc/mounts" + then + "${CAULDRON_CMD_UNMOUNT[@]}" "$target/$mnt" || + return $ERR_CHROOT_DONE + fi + done + done + + # clear the chroot definitions + if [[ -n "$index" ]] + then + unset LIBCHROOT_USERS[index] + unset LIBCHROOT_PATHS[index] + else + unset LIBCHROOT_USERS + unset LIBCHROOT_PATHS + fi + unset CAULDRON_CHROOT + + return $ERR_OK +} + +#------------------------------------------------------------------------------- ## @param target chroot dir ## ## Cleans up the chroot environment after it is no longer needed. @@ -131,14 +180,8 @@ function cauldron_chroot_done() { then LIBCHROOT_USERS[index]=$((LIBCHROOT_USERS[index]-1)) else - # unmount the chroot mount points - "${CAULDRON_CMD_UNMOUNT_RECURSIVE[@]}" "$target"/* || - return $ERR_CHROOT_DONE - - # clear the chroot definitions - unset CAULDRON_CHROOT - unset LIBCHROOT_USERS[index] - unset LIBCHROOT_PATHS[index] + cauldron_chroot_close "$index" "$target" + liberror_check || return $? fi fi done @@ -153,21 +196,8 @@ function cauldron_chroot_done() { ## #------------------------------------------------------------------------------- function cauldron_chroot_cleanup() { - local target= - - for target in "${LIBCHROOT_PATHS[@]}" - do - # unmount the chroot mount points - "${CAULDRON_CMD_UNMOUNT_RECURSIVE[@]}" "$target"/* || - return $ERR_CHROOT_DONE - done - - # clear the chroot definitions - unset LIBCHROOT_USERS - unset LIBCHROOT_PATHS - unset CAULDRON_CHROOT - - return $ERR_OK + cauldron_chroot_close + liberror_check || return $? } #------------------------------------------------------------------------------- |