blob: 9611c2744d285fc8095319fb1e5112c65861a17b (
plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
# This file contains common functions used in init and in hooks
msg () {
[ "${quiet}" != "y" ] && echo $@
}
err () {
echo "ERROR: $@"
}
poll_device() {
local device=$1 seconds=${2//[!0-9]}
[ -z "$seconds" ] && seconds=5
[ -b "$device" ] && return 0
if [ "$udevd_running" -eq 1 ]; then
msg "Waiting $seconds seconds for device $device ..." >&2
while [ ! -b "$device" -a "$seconds" -gt 0 ]; do
sleep 1
seconds=$(( $seconds - 1 ))
done
fi
[ -b "$device" ]
}
launch_interactive_shell() {
export PS1='[rootfs \W]\$ '
[ "$1" = "--exec" ] && exec sh -i
sh -i
}
major_minor_to_device() {
local dev
[ -e "/sys/dev/block/$1:$2" ] || return 1
if dev=$(readlink -f "/sys/dev/block/$1:$2" 2>/dev/null); then
echo "/dev/${dev##*/}"
return 0
fi
return 1
}
parse_cmdline() {
local w in_quotes lhs rhs
in_quotes=0
for w in $(cat /proc/cmdline); do
if [ "$in_quotes" = 0 ]; then
case "$w" in
# ignore everything after a # in the commandline
\#*) break ;;
# special cases
rw|ro) rwopt=$w ;;
forcefsck) FORCEFSCK=-f ;;
# abide by shell variable naming rules
[[:alpha:]_]*=*)
rhs=${w#*=}
lhs=${w%%=*}
lhs=${lhs//[-.]/_}
if [ '"' = "${rhs:0:1}" ]; then
if [ '"' = "${rhs:$((${#rhs}-1))}" ]; then
rhs="${rhs:1:$((${#rhs}-2))}"
else
rhs=${rhs:1}
in_quotes=1
continue
fi
fi
eval $lhs=\$rhs
;;
[[:alpha:]_]*)
lhs=${w//[-.]/_}
eval $lhs=y
;;
esac
else
if [ '"' = "${w:$((${#w}-1))}" ]; then
rhs="$rhs ${w%\"}"
in_quotes=0
eval $lhs=\$rhs
else
rhs="$rhs $w"
fi
fi
done
}
fsck_device() {
[ -x /sbin/fsck ] || return 255
if [ ! -b "$1" ]; then
err "device '$1' not found. Skipping fsck."
return 255
fi
msg ":: performing fsck on '$1'"
FSTAB_FILE=/dev/null fsck -Ta -C"$FSCK_FD" "$1" -- $FORCEFSCK
}
fsck_root() {
fsck_device "$root"
fsckret=$?
fsck_ret() {
[ -z "$fsckret" ] && return 1
[ "$fsckret" -eq "$1" ] && return 0
[ "$(( $fsckret & $1 ))" -eq "$1" ]
}
if [ "$fsckret" -ne 255 ]; then
if fsck_ret 0 || fsck_ret 1; then
echo "$fsckret" > /run/initramfs/root-fsck
elif fsck_ret 4; then
err "Bailing out. Run 'fsck $root' manually"
printf '%s\n' \
"********** FILESYSTEM CHECK FAILED **********"
"* *"
"* Please run fsck manually. After leaving *"
"* this maintenance shell, the system will *"
" reboot automatically. *"
"* *"
"*********************************************"
launch_interactive_shell
echo ":: Automatic reboot in progress"
sleep 2
reboot -f
elif fsck_ret 2; then
printf '%s\n' \
"************** REBOOT REQUIRED **************" \
"* *" \
"* automatically restarting in 10 seconds *" \
"* *" \
"*********************************************"
sleep 10
reboot -f
elif fsck_ret 8; then
err "fsck failed on '$root'"
elif fsck_ret 16; then
err "Failed to invoke fsck: usage or syntax error"
elif fsck_ret 32; then
echo ":: fsck cancelled on user request"
elif fsck_ret 128; then
err "fatal error invoking fsck"
fi
fi
}
resolve_device() {
local major minor dev device=$1
case $device in
# resolve tag name to block device
UUID=*|LABEL=*)
dev=$(blkid -lt "$device" -o device)
[ -n "$device" ] && device=$dev
;;
esac
case $device in
/dev/*)
if poll_device "$device" "$rootdelay"; then
echo "$device"
return 0
fi
# block device, e.g. (/dev/sda1) -> /sys/class/block/sda1/dev
if [ -e /sys/class/block/${device:5}/dev ]; then
IFS=':' read major minor < "/sys/class/block/${device:5}/dev"
fi
;;
[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]|[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])
# hex encoded major/minor, such as from LILO
major=$(( 0x0$device >> 8 ))
minor=$(( 0x0$device & 0xff ))
;;
0x[0-9a-fA-F][0-9a-fA-F]*)
major=$(( $device >> 8 ))
minor=$(( $device & 0xff ))
;;
esac
if [ -n "$major" -a -n "$minor" ]; then
device=$(major_minor_to_device "$major" "$minor" || echo '/dev/root')
if [ ! -b "$device" ]; then
msg "Creating device node with major $major and minor $minor." >&2
mknod "$device" b "$major" "$minor"
fi
echo "$device"
return 0
fi
return 1
}
default_mount_handler() {
if [ ! -b "$root" ]; then
err "Unable to find root device '$root'."
echo "You are being dropped to a recovery shell"
echo " Type 'exit' to try and continue booting"
launch_interactive_shell
msg "Trying to continue (this will most likely fail) ..."
fi
if ! mount ${fstype:+-t $fstype} -o ${rwopt:-ro}${rootflags:+,$rootflags} "$root" "$1"; then
echo "You are now being dropped into an emergency shell."
launch_interactive_shell
msg "Trying to continue (this will most likely fail) ..."
fi
}
# vim: set ft=sh ts=4 sw=4 et:
|