-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathrootwork
executable file
·113 lines (96 loc) · 2.38 KB
/
rootwork
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
if [ `id -u` -gt 0 ]; then
exec sudo "$0" "$@"
fi
if [ -z "$1" ]; then
ROOT="/overlay/lower"
else
ROOT="$1"
fi
if [ ! -d "$ROOT" ]; then
echo "$ROOT directory not found"
exit 1
fi
is_mounted () {
if [ -z "$1" ]; then
echo "is_mounted <directory>"
return 1
fi
if test -d "$1" && mountpoint -q "$1"; then
return 0
else
return 1
fi
}
not_mounted () {
if [ -z "$1" ]; then
echo "not_mounted <directory>"
return 1
fi
if test -d "$1" && ! mountpoint -q "$1"; then
return 0
else
return 1
fi
}
test_mountpoints () {
if [ -z "$1" -o -z "$2" ]; then
echo "test_mountpoints <source_mountpoint> <destination_mountpoint>"
return 1
fi
SOURCE=$1
DESTINATION=$2
if is_mounted "$SOURCE" && not_mounted "$DESTINATION"; then
return 0
else
return 1
fi
}
mount -o remount,rw "$ROOT" || { echo "error remounting $ROOT rw"; exit 1; }
for DIR in boot boot/firmware; do
if is_mounted "/$DIR"; then
mount -o remount,rw "/$DIR" || { echo "error remounting \"/$DIR\" rw"; exit 1; }
fi
done
for DIR in boot boot/firmware run; do
if test_mountpoints "/$DIR" "$ROOT/$DIR"; then
mount --rbind "/$DIR" "$ROOT/$DIR"
fi
done
if test_mountpoints /sys "$ROOT/sys"; then
mount -t sysfs sysfs "$ROOT/sys"
fi
if test_mountpoints /proc "$ROOT/proc"; then
mount -t proc proc "$ROOT/proc"
fi
if test_mountpoints /dev "$ROOT/dev"; then
mount -t devtmpfs devtmpfs "$ROOT/dev"
fi
IMCHROOTED="$ROOT" chroot "$ROOT"
if is_mounted "$ROOT/sys"; then
umount -f "$ROOT/sys"
fi
if is_mounted "$ROOT/proc"; then
umount -f "$ROOT/proc"
fi
if is_mounted "$ROOT/dev"; then
umount -f "$ROOT/dev"
fi
# 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/.
# I think because / is an overlay of $ROOT. So we don't unmount $ROOT/run
for DIR in boot/firmware boot; do
if is_mounted "$ROOT/$DIR"; then
umount -lf "$ROOT/$DIR"
fi
done
for DIR in boot/firmware boot; do
if is_mounted "/$DIR"; then
mount -o remount,ro "/$DIR"
fi
done
mount -o remount,ro "$ROOT" || {
echo "Failed to remount $ROOT read-only, possibly because of an open file."
echo "The best thing to do would be to reboot now"
exit $?
}
exit 0