@@ -15,59 +15,95 @@ if [ ! -d "$ROOT" ]; then
1515 exit 1
1616fi
1717
18- mount -o remount,rw " $ROOT " || { echo " error remounting $ROOT rw" ; exit 1; }
19- test -d /boot && mountpoint -q /boot && mount -o remount,rw /boot || { echo " error remounting /boot rw" ; exit 1; }
18+ is_mounted () {
19+ if [ -z " $1 " ]; then
20+ echo " is_mounted <directory>"
21+ return 1
22+ fi
23+ if test -d " $1 " && mountpoint -q " $1 " ; then
24+ return 0
25+ else
26+ return 1
27+ fi
28+ }
29+
30+ not_mounted () {
31+ if [ -z " $1 " ]; then
32+ echo " not_mounted <directory>"
33+ return 1
34+ fi
35+ if test -d " $1 " && ! mountpoint -q " $1 " ; then
36+ return 0
37+ else
38+ return 1
39+ fi
40+ }
2041
2142test_mountpoints () {
22- if [ $# != 2 ]; then
43+ if [ -z " $1 " -o -z " $2 " ]; then
2344 echo " test_mountpoints <source_mountpoint> <destination_mountpoint>"
2445 return 1
2546 fi
2647
2748 SOURCE=$1
2849 DESTINATION=$2
2950
30- if [ -d " $SOURCE " -a -d " $DESTINATION " ] && mountpoint -q " $SOURCE " && ! mountpoint -q " $DESTINATION " ; then
51+ if is_mounted " $SOURCE " && not_mounted " $DESTINATION " ; then
3152 return 0
3253 else
3354 return 1
3455 fi
35-
3656}
3757
58+ mount -o remount,rw " $ROOT " || { echo " error remounting $ROOT rw" ; exit 1; }
59+ if is_mounted /boot; then
60+ mount -o remount,rw /boot || { echo " error remounting /boot rw" ; exit 1; }
61+ fi
62+
3863for DIR in boot run; do
39- test_mountpoints " /$DIR " " $ROOT /$DIR " && \
64+ if test_mountpoints " /$DIR " " $ROOT /$DIR " ; then
4065 mount --rbind " /$DIR " " $ROOT /$DIR "
66+ fi
4167done
4268
43- test_mountpoints /sys " $ROOT /sys" && \
69+ if test_mountpoints /sys " $ROOT /sys" ; then
4470 mount -t sysfs sysfs " $ROOT /sys"
45- test_mountpoints /proc " $ROOT /proc" && \
71+ fi
72+ if test_mountpoints /proc " $ROOT /proc" ; then
4673 mount -t proc proc " $ROOT /proc"
47- test_mountpoints /dev " $ROOT /dev" && \
74+ fi
75+ if test_mountpoints /dev " $ROOT /dev" ; then
4876 mount -t devtmpfs devtmpfs " $ROOT /dev"
77+ fi
4978
5079IMCHROOTED=" $ROOT " chroot " $ROOT "
5180
52- test -d " $ROOT /sys" && mountpoint -q " $ROOT /sys " && \
81+ if is_mounted " $ROOT /sys" ; then
5382 umount -f " $ROOT /sys"
54- test -d " $ROOT /proc" && mountpoint -q " $ROOT /proc" && \
83+ fi
84+ if is_mounted " $ROOT /proc" ; then
5585 umount -f " $ROOT /proc"
56- test -d " $ROOT /dev" && mountpoint -q " $ROOT /dev" && \
86+ fi
87+ if is_mounted " $ROOT /dev" ; then
5788 umount -f " $ROOT /dev"
89+ fi
5890
5991# with mount --rbind /run $ROOT/run, umounting $ROOT/run is problematic as it's busy and if you umount -lf it umounts directories in /run/.
6092# I think because / is an overlay of $ROOT. So we don't unmount $ROOT/run
6193for DIR in boot; do
62- test -d " $ROOT /$DIR " && mountpoint -q " $ROOT / $DIR " && \
94+ if is_mounted " $ROOT /$DIR " ; then
6395 umount -lf " $ROOT /$DIR "
96+ fi
6497done
6598
66- test -d /boot && mountpoint -q /boot && \
99+ if is_mounted /boot; then
67100 mount -o remount,ro /boot
101+ fi
102+
68103mount -o remount,ro " $ROOT " || {
69104 echo " Failed to remount $ROOT read-only, possibly because of an open file."
70105 echo " The best thing to do would be to reboot now"
71106 exit $?
72107}
108+
73109exit 0
0 commit comments