1
1
#! /bin/bash
2
2
# vim: softtabstop=2 shiftwidth=2 expandtab
3
3
4
+ _mnt=()
5
+
6
+ cleanup () {
7
+ for _fs in " ${_mnt[@]} " ; do
8
+ umount " ${_fs} " || zerror " unable to unmount ${_fs} "
9
+ done
10
+
11
+ _mnt=()
12
+ trap - HUP INT QUIT ABRT EXIT
13
+ }
14
+
4
15
# shellcheck disable=SC1091
5
16
source /lib/profiling-lib.sh > /dev/null 2>&1
17
+ # shellcheck disable=SC1091
6
18
source /etc/zfsbootmenu.conf 2>&1 || exit 1
19
+ # shellcheck disable=SC1091
7
20
source /lib/kmsg-log-lib.sh > /dev/null 2>&1 || exit 1
21
+ # shellcheck disable=SC1091
8
22
source /lib/zfsbootmenu-core.sh > /dev/null 2>&1 || exit 1
9
23
10
24
selected=" ${1} "
@@ -13,45 +27,61 @@ zdebug "started with ${selected}"
13
27
14
28
[ -n " ${selected} " ] || exit 0
15
29
16
- if mountpoint=" $( allow_rw=yes mount_zfs " ${selected} " ) " ; then
17
- zdebug " mounted ${selected} to ${mountpoint} "
18
- mount -t proc proc " ${mountpoint} /proc"
19
- mount -t sysfs sys " ${mountpoint} /sys"
20
- mount -B /dev " ${mountpoint} /dev"
21
- mount -B /tmp " ${mountpoint} /tmp"
22
- mount -t devpts pts " ${mountpoint} /dev/pts"
23
-
24
- pool=" ${selected%%/* } "
25
-
26
- # Snapshots and read-only pools always produce read-only mounts
27
- if is_snapshot " ${selected} " || ! is_writable " ${pool} " ; then
28
- writemode=" $( colorize green " read-only" ) "
29
- else
30
- writemode=" $( colorize red " read/write" ) "
31
- fi
32
-
33
- _SHELL=
34
- if [ -x " ${mountpoint} /bin/bash" ] \
30
+ if ! mountpoint=" $( allow_rw=yes mount_zfs " ${selected} " ) " ; then
31
+ zerror " failed to mount ${selected} "
32
+ exit 1
33
+ fi
34
+
35
+ # Track submounts so we know how to clean up on exit
36
+ trap cleanup HUP INT QUIT ABRT EXIT
37
+ _mnt=( " ${mountpoint} " )
38
+
39
+ zdebug " mounted ${selected} to ${mountpoint} "
40
+
41
+ mount -B /tmp " ${mountpoint} /tmp" \
42
+ && _mnt=( " ${mountpoint} /tmp" " ${_mnt[@]} " )
43
+
44
+ mount -t proc proc " ${mountpoint} /proc" \
45
+ && _mnt=( " ${mountpoint} /proc" " ${_mnt[@]} " )
46
+
47
+ mount -t sysfs sys " ${mountpoint} /sys" \
48
+ && _mnt=( " ${mountpoint} /sys" " ${_mnt[@]} " )
49
+
50
+ mount -B /dev " ${mountpoint} /dev" \
51
+ && _mnt=( " ${mountpoint} /dev" " ${_mnt[@]} " )
52
+
53
+ # Not all /dev filesystems have /dev/pts
54
+ [ -d " ${mountpoint} /dev/pts" ] \
55
+ && mount -t devpts pts " ${mountpoint} /dev/pts" \
56
+ && _mnt=( " ${mountpoint} /dev/pts" " ${_mnt[@]} " )
57
+
58
+ pool=" ${selected%%/* } "
59
+
60
+ # Snapshots and read-only pools always produce read-only mounts
61
+ if is_snapshot " ${selected} " || ! is_writable " ${pool} " ; then
62
+ writemode=" $( colorize green " read-only" ) "
63
+ else
64
+ writemode=" $( colorize red " read/write" ) "
65
+ fi
66
+
67
+ _SHELL=
68
+ if [ -x " ${mountpoint} /bin/bash" ] \
35
69
&& chroot " ${mountpoint} " /bin/bash -c " exit 0" > /dev/null 2>&1 ; then
36
- _SHELL=" /bin/bash"
37
- chroot_extra=" --norc"
38
- elif [ -x " ${mountpoint} /bin/sh" ] \
70
+ _SHELL=" /bin/bash"
71
+ chroot_extra=" --norc"
72
+ elif [ -x " ${mountpoint} /bin/sh" ] \
39
73
&& chroot " ${mountpoint} " /bin/sh -c " exit 0" > /dev/null 2>&1 ; then
40
- _SHELL=" /bin/sh"
41
- else
42
- zerror " unable to test execute a shell in ${selected} "
43
- fi
44
-
45
- if [ -n " ${_SHELL} " ]; then
46
- echo -e " $( colorize orange " ${selected} " ) is mounted ${writemode} , /tmp is shared and read/write\n"
47
-
48
- # regardless of shell, set PS1
49
- if ! env " PS1=$( colorize orange " ${selected} " ) \w > " chroot " ${mountpoint} " " ${_SHELL} " " ${chroot_extra} " ; then
50
- zdebug " chroot ${selected} :${_SHELL} returned code $? "
51
- fi
52
- fi
53
-
54
- if ! umount -R " ${mountpoint} " ; then
55
- zerror " unable to unmount ${mountpoint} "
56
- fi
74
+ _SHELL=" /bin/sh"
75
+ fi
76
+
77
+ if [ -z " ${_SHELL} " ]; then
78
+ zerror " unable to test execute a shell in ${selected} "
79
+ exit 1
80
+ fi
81
+
82
+ echo -e " $( colorize orange " ${selected} " ) is mounted ${writemode} , /tmp is shared and read/write\n"
83
+
84
+ # regardless of shell, set PS1
85
+ if ! env " PS1=$( colorize orange " ${selected} " ) \w > " chroot " ${mountpoint} " " ${_SHELL} " " ${chroot_extra} " ; then
86
+ zdebug " chroot ${selected} :${_SHELL} returned code $? "
57
87
fi
0 commit comments