-
Notifications
You must be signed in to change notification settings - Fork 9
/
init
executable file
·390 lines (344 loc) · 8.78 KB
/
init
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/bin/sh
#
# $Header: mkinitramfs-ll/init Exp $
# $Author: (c) 2011-19 tokiclover <[email protected]> Exp $
# $License: 2-clause/new/simplified BSD Exp $
# $Version: 0.22.2 2019/01/12 12:33:03 Exp $
#
# WARNING: USE AT YOUR OWN RISK! EVERYTHING IS GIVEN “AS IS”, SEE COPYING FILE
#
CHECK_ENV=true
init=/sbin/init
confdir=/etc/mkinitramfs-ll
libdir=/lib/mkinitramfs-ll
logfile=$0.log
mntdir=/mnt/tok
newroot=/newroot
splashd=false
NULL=/dev/null
export SHELL=/bin/sh
export HOME=/root
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
export TERM=linux
exec 3>$logfile
logfd=3
PS1='-($(tty | cut -c6-):$PWD)-# '
PS2='-# '
# @FUNCTION: Handle fatal error
# @ARG: <msg>
die() {
local _ret=$?
[ -n "$@" ] && msg -e "[$_ret]: $@"
echo -e "$color_fg_red>>>$color_rst Dropping to a Rescue Shell"
$splashd && splashd_stop
debug rescue_shell || debug $SHELL -lim
}
. $libdir/functions || $SHELL -lim
eval_colors
name=init
# @FUNCTION: Shell Run Level helper
# @ARG: <level>
level() {
echo -e "$color_fg_grn>>>$color_rst Switching to init shell run level $color_fg_blu$1$color_rst"
level=$1
local _f
for _f in $(ls $libdir/${1}-* 2>$NULL); do
dohook "$_f"
done
case "$level" in
("$rescue_shell") debug die;;
esac
return 0
}
# @FUNCTION: Splash daemon helper
splashd() {
$CHECK_ENV && debug -d bck fbcondecor_helper splash_util
local IFS="${IFS}," _opt _opts cachedir event mode theme
event="$(grep -Hsi keyboard /sys/class/input/input*/name | \
sed -e 's#.*input\([0-9]*\)/name.*#event\1#')"
: ${cachedir:=/run/splash/cache}
: ${BOOT_MSG:="Booting the system (\$progress%)... Press F2 for verbose mode."}
: ${splash_fifo=$cachedir/.splash}
for _opt in $splash; do
case "${_opt%:*}" in
(fad*) _opts="set effects $_opt";;
(silent|verbose) mode="$_opt" ;;
(tty) CONSOLE=/dev/"${_opt/:/}" ;;
(theme) theme="${_opt#*:}" ;;
esac
done
[ -d "$cachedir" ] || mkdir -p $cachedir
debug mkfifo $splash_fifo
debug splash_util -d -ms -t $theme && splashd=true
[ -n "$_opts" ] && debug splashd_cmd $_opts
debug splashd_cmd "set ${mode:-verbose}"
debug splashd_cmd "set event dev /dev/input/$event"
debug splashd_cmd "set autoverbose 0"
}
# @FUNCTION: Resume helper (support SwSusp/TuxOnIce)
# @ARG: <[type:]dev[:signature]>
resume() {
local _asw _dev=$1 _img_sz _hpr
grep -q $mntdir /proc/mounts && debug -d umount -l $mntdir
if [ -d /sys/power/tuxonice ]; then
if $splashd; then
[ -n "$theme" ] &&
debug ln -sf "/etc/splash/$theme" /etc/splash/tuxonice
_hpr=tuxoniceui_fbsplash
else
_hpr=tuxoniceui_text
fi
$CHECK_ENV && debug -d check_bin $_hpr && _hpr="$(type -p $_hpr)"
debug MODPROBE tuxonice
echo $_hpr >/sys/power/tuxonice/user_interface/program
echo $_dev >/sys/power/tuxonice/resume
read _img </sys/power/tuxonice/image_exists
if [ "$_img_sz" = "1" ]; then
echo >/sys/power/tuxonice/image_exists
else
echo >/sys/power/tuxonice/do_resume
fi
elif [ -f /sys/power/resume ]; then
debug MODPROBE swsusp
echo platform >/sys/power/disk # or 'shutdown' if ACPI does not work
echo disk >/sys/power/state
local _node=${_dev#*:}
[ -h "$_node" ] && _node=$(readlink $_node)
read _node </sys/class/block/${_node#*/}/dev
echo $_node >/sys/power/resume
read _img_sz </sys/power/image_exists
if [ "$_img_sz" != "0" ]; then
echo $_node >/sys/power/resume
else
echo 0 >/sys/power/image_size
fi
fi
}
# -*- Initialisation -*-
debug umask 0077
debug mkdir -p dev/pts proc run sys $newroot $mntdir
debug mount -t proc proc /proc
debug mount -t sysfs sysfs /sys
if grep -q devtmpfs /proc/filesystems; then
debug mount -t devtmpfs devtmpfs /dev
else
debug mount -t tmpfs -o exec,mode=755 tmpfs /dev
fi
echo >/dev/mdev.seq
[ -x /sbin/mdev ] && mdev=/sbin/mdev || mdev="/bin/busybox mdev"
debug -d $mdev -s
echo $mdev >/proc/sys/kernel/hotplug
debug mount -t tmpfs -o mode=755,size=1% tmpfs /run
CMDLINE="$(cat /proc/cmdline)"
for arg in $CMDLINE; do
case "$arg" in
([Rr][Ee][Ss][Cc][Uu][Ee]*|[Rr][Ss][Hh]*) rescue_shell=1;;
(ro|rw) rootflags="${rootflags:+$rootflags,}$arg";;
([Cbcdefhiklmnrsvz]*=*) eval "$arg" ;;
esac
done
: ${rootfstype:=$rootfs}
if [ -n "$level" ]; then
rescue_shell="${level%:*}"
fi
case "$env" in
(""|*:*) ENV=true;;
(*)
yesno "${env:-YES}"
case "$?" in
(0) ENV=true ;;
(*) ENV=false;;
esac
;;
esac
$ENV && [ -f $confdir/env ] &&
while read _line; do
case "$_line" in
(confdir=*|libdir=*|HOME=*|keymode=*|logfile=*|mntdir=*|newroot=*) ;;
(PATH=*|rescue_shell=*|NULL=*|splashd=*|SHELL=*|name=*) ;;
([A-Za-z]*=*)
case "$env" in
(*:${_line%%=*}:*) ;;
(*) eval "$_line" ;;
esac;;
esac
done < $confdir/env
$CHECK_ENV && debug check_applet
CUT "dmesg" "$debug" ":" "2" "-s"
debug="${debug%:*}"
case "$debug" in
(*l*)
keeplog=true debug="${debug/l/}"
exec 2>&3
;;
esac
[ -n "$debug" ] && set -${debug/e/}
[ -n "$dmesg" ] && debug dmesg -n $dmesg
dmesg=
[ -f $confdir/module-boot ] &&
for _m in $(cat $confdir/module-boot); do
debug MODPROBE $_m
done
for _m in $(echo "$module" | sed 's/[:,]/ /g'); do
debug MODPROBE $_m
done
[ -h /dev/fd ] || debug ln -fns /proc/self/fd /dev/fd
[ -h /dev/stderr ] || debug ln -fns fd/2 /dev/stderr
[ -h /dev/stdin ] || debug ln -fns fd/0 /dev/stdin
[ -h /dev/stdout ] || debug ln -fns fd/1 /dev/stdout
: ${CONSOLE:=${console:-/dev/console}}
[ "${CONSOLE/dev}" != "$CONSOLE" ] || CONSOLE="/dev/$CONSOLE"
[ -c $CONSOLE ] || CONSOLE=/dev/console
exec 0<$CONSOLE 1>$CONSOLE 2>&1
eval_colors
set >/environ
if [ -n "$video" ]; then
debug dohook "$libdir/fb-video"
fi
if [ -n "$splash" ]; then
begin "Starting Splash"
splashd
end "$?"
fi
# -*- Font and keymap -*-
if [ -n "$keymap" ]; then
CUT "font" "$keymap" "2" ":" "-s"
kmap="${keymap%:*}"
fi
[ -z "$font" ] && [ -f "$confdir/font" ] && font=$(cat $confdir/font)
[ -z "$kmap" ] && [ -f "$confdir/kmap" ] && kmap=$(cat $confdir/kmap)
[ -n "$font" ] &&
for file in /usr/share/consolefonts/$font*; do
[ -f "$file" ] && debug loadfont < $file && break
done
[ -n "$kmap" ] &&
for file in /usr/share/keymaps/$kmap*; do
[ -f "$file" ] && debug loadkmap < $file && break
done
unset file font kmap
# -*- Logo and or boot message -*-
level 1
[ -f /etc/issue.logo ] && cat /etc/issue.logo
if debug test -f /etc/issue; then
clear
while read _line; do
msg -i "$_line"
done </etc/issue
fi
# -*- Hooks -*-
level 2
for _h in bcache btrfs zfs zram; do
eval [ \"x\$$_h\" != x ] && hook="$hook $_h"
done
hook="$(echo $hook | sed -e 's/[:,]/ /g')"
for _h in $hook; do
if [ -e "$libdir/$_h" ]; then
debug dohook "$libdir/$_h"
else
error "$_h not found"
fi
done
_h=
# -*- Swap/Resume -*-
# SWAP should be set if available
if [ -n "$swap" ]; then
level 2s
if [ -z "$SWAP" ]; then
begin "Enabling swap device"
get_dev "$swap" "SWAP" "2"
end "$?"
fi
fi
# RESU should be set if available
if [ -n "$resume" ]; then
level 2r
if [ -z "$RESU" ] && [ "$resume" != "swap" ]; then
begin "Starting Resume"
get_dev "$resume" "RESU" "3"
end "$?"
fi
debug resume "${RESU:-$SWAP}"
fi
# -*- Root -*-
level 3
# ROOT should be set if available
mount_rootfs() {
local fstype="${rootfstype%:*}" check
CUT "check" "$rootfstype" ":" "2" "-s"
debug -d get_dev "$root" "ROOT" "1"
level 3f
if [ -n "$check" ]; then
yesno $check && check=-d || check=-e
debug $check fsck ${fstype:+-t} $fstype $ROOT
fi
level 3m
debug mount ${rootflags:+-o} $rootflags ${fstype:+-t} $fstype $ROOT $newroot ||
die "Failed to mount rootfs"
}
if [ -z "$ROOT" ]; then
begin "Mounting RootFS"
mount_rootfs
end "$?"
fi
if [ -n "$squashd" ]; then
level 3s
begin "Mounting Squashed FileSystems"
squashd "$squashd"
end "$?"
fi
# -*- Clean up and switch -*-
level 4
for _k in $loopback_key; do
debug dmclose "$_k"
done
for _d in $loopback_dev; do
debug losetup -d $_d
done
[ "$keymode" = "gpg" ] && debug RMMOD gpg
if grep -q $mntdir /proc/mounts; then
debug -d umount -l $mntdir
debug RMMOD remdev
fi
if [ -n "$fsmount" -o -n "$fstab" ]; then
level 4m
begin "Mounting FileSystems"
fsmount $fsmount $fstab
end "$?"
fi
level 4u
#
# FIXME: is this necessary?
#
$splashd && debug splashd_stop
if [ -n "$keeplog" ]; then
cp $logfile $newroot
cp $logfile /run
fi
echo >/proc/sys/kernel/hotplug
for _dir in /run /sys /dev /proc; do
if [ "${SYSFS/${_dir#/}}" != "$SYSFS" ]; then
begin "Moving $_dir to $newroot"
mkdir -p $newroot$_dir
mount -o move $_dir $newroot$_dir >$NULL 2>&1
else
begin "Unmounting $_dir"
umount -l $_dir >$NULL >&1
fi
end "$?"
done
level 4s
set >/environ
echo -e "$color_fg_grn>>>$color_rst Switching Root"
exec 3>&-
while read _env; do
case "$_env" in
init=*) : ;;
[Cbcdefhiklmnrsz]*=*) unset "${_env%%=*}" ;;
esac
done </environ
exec switch_root /newroot ${init:-/sbin/init}
echo -e "$color_fg_red>>>$color_rst Failed to switch root!"
exec >${SHELL:-/bin/sh} -lim
#
# vim:fenc=utf-8:ft=sh:ci:pi:sts=0:sw=4:ts=4:
#