Skip to content

Commit af2a484

Browse files
committed
zfs-chroot: unmount everything under chroot mountpoint
Instead of manually tracking everything mounted under the chroot, check mounts and sort the list by the directory depth. Given directory depths of 10,9,8, assume that any directories at the same depth can be unmounted in any order.
1 parent 54bbde2 commit af2a484

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

zfsbootmenu/bin/zfs-chroot

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,35 +66,25 @@ notices+=( "$(colorize white "*" ) $( colorize orange "${selected}" ) is mounted
6666

6767
# Track submounts so we know how to clean up on exit
6868
trap cleanup HUP INT QUIT ABRT EXIT
69-
_mnt=( "${mountpoint}" )
7069

7170
zdebug "mounted ${selected} to ${mountpoint}"
7271

73-
mount -B /tmp "${mountpoint}/tmp" \
74-
&& _mnt=( "${mountpoint}/tmp" "${_mnt[@]}" )
75-
76-
mount -t proc proc "${mountpoint}/proc" \
77-
&& _mnt=( "${mountpoint}/proc" "${_mnt[@]}" )
78-
79-
mount -t sysfs sys "${mountpoint}/sys" \
80-
&& _mnt=( "${mountpoint}/sys" "${_mnt[@]}" )
81-
82-
mount -B /dev "${mountpoint}/dev" \
83-
&& _mnt=( "${mountpoint}/dev" "${_mnt[@]}" )
72+
mount -B /tmp "${mountpoint}/tmp"
73+
mount -t proc proc "${mountpoint}/proc"
74+
mount -t sysfs sys "${mountpoint}/sys"
75+
mount -B /dev "${mountpoint}/dev"
8476

8577

8678
if mount_efivarfs "${efivarmode}" ; then
8779
efivarfs="${mountpoint}/sys/firmware/efi/efivars"
88-
mount_efivarfs "${efivarmode}" "${efivarfs}" \
89-
&& _mnt=( "${efivarfs}" "${_mnt[@]}" )
80+
mount_efivarfs "${efivarmode}" "${efivarfs}"
9081

9182
notices+=( "\n$(colorize white "*" ) $( colorize orange "efivarfs" ) is mounted ${writemode}" )
9283
fi
9384

9485
# Not all /dev filesystems have /dev/pts
9586
[ -d "${mountpoint}/dev/pts" ] \
96-
&& mount -t devpts pts "${mountpoint}/dev/pts" \
97-
&& _mnt=( "${mountpoint}/dev/pts" "${_mnt[@]}" )
87+
&& mount -t devpts pts "${mountpoint}/dev/pts"
9888

9989
_SHELL=
10090
if [ -x "${mountpoint}/bin/bash" ] \
@@ -126,3 +116,20 @@ unset PROMPT_COMMAND
126116
if ! env "PS1=\[\033[0;33m\]${selected}\[\033[0m\] \w > " chroot ${mountpoint} ${_SHELL} ${chroot_extra} ; then
127117
zdebug "chroot ${selected}:${_SHELL} returned code $?"
128118
fi
119+
120+
mounts=()
121+
122+
mp_re="^${mountpoint}"
123+
124+
# shellcheck disable=SC2034
125+
while read -r skip mp skip skip ; do
126+
if [[ "${mp}" =~ ${mp_re} ]]; then
127+
lines="${mp//[!\/]/}"
128+
lines="${#lines}"
129+
mounts+=( "${lines},${mp}" )
130+
fi
131+
done < /proc/self/mounts
132+
133+
while IFS=$'\n' read -r filesystem; do
134+
_mnt+=( "${filesystem#*,}" )
135+
done <<<"$( printf '%s\n' "${mounts[@]}" | sort -n -k1 -r )"

0 commit comments

Comments
 (0)