forked from tokiclover/mkinitramfs-ll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init
executable file
·422 lines (331 loc) · 10 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/bin/sh
#
# $Header: mkinitramfs-ll/init Exp $
# $Author: (c) 2011-2014 -tclover <[email protected]> Exp $
# $License: 2-clause/new/simplified BSD Exp $
# $Version: 0.14.4 2014/10/10 12:33:03 Exp $
#
# WARNING: USE AT YOUR OWN RISK! EVERYTHING IS GIVEN “AS IS”, SEE COPYING FILE
# NEED SOME HELP? WHERE'S THE README FILE? YOU ASK? WHO KNOWS.
#
# @VARIABLE: ECK
# @DESCRIPTION: Environment ChecK, check binaries/busybox applets
: ${ECK=true}
# @VAIABLE: init
# @DESCRIPTION: real init
: ${init=/sbin/init}
# @VARIABLE: CONFDIR
# @DESCRIPTION: path to default mkinitramfs-ll configuration dir
: ${CONFDIR=/etc/mkinitramfs-ll}
# @VARIABLE: LIBDIR
# @DESCRIPTION: path to default mkinitramfs-ll library dir
: ${LIBDIR=/lib/mkinitramfs-ll}
# @VARIABLE: sh
# @DESCRIPTION: shell for scripts and user scripts execution and rescue shell
: ${sh=/bin/sh}
# @VARIABLE: LOGFILE
# @DESCRIPTION: log file to log execution commands for debugging
: ${LOGFILE=$0.log}
# @VARIABLE: RSH
# @DESCRIPTION: Rescue SHell level to be dropped into, see LVL
# @VARIABLE: LVL
# @DESCRIPTION: auto setted variable, lvl(), to hold the running shell LeVeL
# @VARIABLE: RIL
# @DESCRIPTION: Real Init run-Level to be passed to real init
# @VARIABLE: MNTDIR
# @DESCRIPTION: mount point for removable token or device
: ${MNTDIR=/mnt/tok}
# @VARIABLE: NEWROOT
# @DESCRIPTION: new root directory to switch root
: ${NEWROOT=/newroot}
# @VARIABLE: SPLD
# @DESCRIPTION: hold SPLash Daemon running state
: ${SPLD=false}
export CONFDIR LIBDIR LOGFILE MNTDIR NEWROOT SPLD init sh
export HOME=/root PATH=/sbin:/bin:/usr/bin:/usr/sbin
export PS1='-(rsh:$(tty | cut -c6-):$PWD)-# ' PS2='-# '
source $LIBDIR/functions || $sh -a
# @FUNCTION: die
# @INTERNAL
# @USAGE: <msg>
# @DESCRIPTION: drop into a rescue shell after a command failure
die() {
local _ret=$?
[ -n "$@" ] && msg -e "[$_ret]: $@"
msg -i "Dropping into a rescueshell..."
$SPLD && spld_stop
debug rsh || debug $sh -aim
}
# @FUNCTION: lvl
# @INTERNAL
# @DESCRIPTION: make LeVeL and drop to rescue shell if $LVL = $RSH
lvl() {
msg -i ">>> Switching to init shell run level: $1"
export LVL=$1
for _f in $(ls $LIBDIR/${1}-* 2>/dev/null); do
dohook "$_f"
done
[ "$LVL" = "$RSH" ] && debug die
return 0
}
# @VARIABLE: SPLD
# @DEFAULT_SET
# @DESCRIPTION: splash deamon running variable
# @FUNCTION: spld
# @INTERNAL
# @DESCRIPTION: run splash deamon
spld() {
$ECK && for bin in fbcondecor_helper splash_util; do
debug -d bck $bin
done
local IFS="${IFS}," _opt _opts cachedir event mode theme util
event="$(grep -Hsi keyboard /sys/class/input/input*/name | \
sed -e 's#.*input\([0-9]*\)/name.*#event\1#')"
: ${cachedir:=/run/splash/cache}
: ${util=$(which splash_util)}
: ${BOOT_MSG:="Booting the system (\$progress%)... Press F2 for verbose mode."}
: ${SPLASH_FIFO=$cachedir/.splash}
export BOOT_MSG SPLASH_FIFO
for _opt in $splash; do
case "${_opt%:*}" in
(fad*) _opts="set effects $_opt" ;;
(silent|verbose) mode="$_opt" ;;
(tty) export CONSOLE=/dev/"${_opt/:/}";;
(theme) theme="${_opt#*:}" ;;
esac
done
[ -d "$cachedir" ] || mkdir -p $cachedir
debug mkfifo $SPLASH_FIFO
debug $util -d -ms -t $theme && SPLD=true
[ -n "$_opts" ] && debug spld_cmd $_opts
debug spld_cmd "set event dev /dev/input/$event"
debug spld_cmd "set autoverbose 0"
}
# @FUNCTION: resume
# @INTERNAL
# @USAGE: <[type:]dev[:signature]>
# @DESCRIPTON: resume after a hibernation, TuxOnIce may work,
# the intrnal kernal implementation should be fixed/finished before use
resume() {
local _asw _dev=$1 _img_sz=0 _hpr
msg -i "Resuming from hibernation..."
if [ -d /sys/power/tuxonice ]; then
if $SPLD; then
[ -n "$theme" ] &&
debug ln -sf "/etc/splash/$theme" /etc/splash/tuxonice
_hpr="$(which tuxoniceui_fbsplash)"
else
_hpr="$(which tuxoniceui_text)"
fi
$ECK && debug -d bck $_hpr
echo $_hpr >/sys/power/tuxonice/user_interface/program
debug _modprobe tuxonice
echo $_dev >/sys/power/tuxonice/resume
read _img </sys/power/tuxonice/image_exists
if [ "$_img_sz" == "1" ]; then
msg -e "toi: no recognizable signature at the pointed location"
fi
echo >/sys/power/tuxonice/do_resume
$SPLD && spld_stop
echo >/sys/power/tuxonice/image_exists
debug _rmmod tuxonice
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
echo ${_dev#*:} >/sys/power/resume
read _img_sz </sys/power/image_exists
if [ "$_img_sz" == "1" ]; then
msg -e "swsusp: no recognizable signature at the pointed location"
fi
$SPLD && spld_stop
echo 0 >/sys/power/image_size
debug _rmmod swsusp
else
msg -e "no hibernation support found"
fi
die "failed to resume from hibernation"
}
# -*- Initialisation -*-
debug -d mkdir -p dev/pts proc run sys $NEWROOT $MNTDIR
debug -d mount -t proc proc /proc
for arg in $(cat /proc/cmdline); do
case "$arg" in
([Rr][Ee][Ss][Cc][Uu][Ee]*|[Rr][Ss][Hh]*) export RSH=1;;
(ienv=*) export "$arg" ;;
(ro|rw) export mopt="$arg" ;;
(single) export RIL=2 ;;
(splash*|console*|CONSOLE*) export "$arg" ;;
(ihook=*) export HOOKS="${arg#*=}" ;;
(i[bdklmnrsz]*) export "$arg" ;;
esac
done
if [ -n "$irsh" ]; then
RSH="${irsh%:*}"
arg "RIL" "$irsh" "2" ":" "-s"
fi
DEBUG="${idebug%:*}"
DMESG="${idebug#*:}"
[ "$DMESG" = "$DEBUG" ] && DMESG=
case "$ienv" in
(0|[Dd][Ii][Ss][Aa][Bb][Ll][Ee]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]|[Nn][Oo])
ENV=false;;
esac
${ENV:-true} && [ -f $CONFDIR/env ] &&
while read _line; do
case "${_line%%=*}" in
(CONFDIR|LIBDIR|HOME|KEYMODE|LOGFILE|MNTDIR|NEWROOT|RIL|PATH|RSH|SPLD|sh)
:;;
([a-zA-Z]*) [ "${ienv/:${_line%%=*}}" = "$ienv" ] && export "$_line" ;;
esac
done < $CONFDIR/env
[ -n "$DEBUG" ] && set -${DEBUG/e/}
[ -n "$DMESG" ] && debug dmesg -n $DMESG
debug -d mount -t sysfs sysfs /sys
debug -d mount -t tmpfs -o mode=755 tmpfs /run
debug -d mkdir -p /run/log
debug umask 0077
debug mv -f $LOGFILE /run/log && export LOGFILE=/run/log/$LOGFILE
$ECK && debug ack
if grep -q devtmpfs /proc/filesystems; then
debug -d mount -t devtmpfs devtmpfs /dev
else
debug -d mount -t tmpfs -o mode=755 tmpfs /dev
fi
debug mkdir /dev/pts
debug mount -t devpts -o gid=5,mode=0620 devpts /dev/pts
echo >/dev/mdev.seq
debug -d mdev -s
echo /sbin/mdev >/proc/sys/kernel/hotplug
for _m in $(echo "$imod" | sed 's/:/ /g'); do
debug _modprobe $_m
done
[ -h /dev/fd ] || debug ln -fs /proc/self/fd /dev/fd
[ -h /dev/stderr ] || debug ln -fs /proc/self/fd/2 /dev/stderr
[ -h /dev/stdin ] || debug ln -fs /proc/self/fd/0 /dev/stdin
[ -h /dev/stdout ] || debug ln -fs /proc/self/fd/1 /dev/stdout
: ${CONSOLE:=${console:-/dev/console}}
[ "${CONSOLE/dev}" != "$CONSOLE" ] || CONSOLE="/dev/$CONSOLE"
[ -c $CONSOLE ] || CONFDIR=/dev/console
exec 0<$CONSOLE 1>$CONSOLE 2>&1
export CONSOLE
[ -f $CONFDIR/boot ] &&
for _m in $(cat $CONFDIR/boot); do
debug _modprobe $_m
done
[ -n "$splash" ] && debug spld
lvl 1
# -*- Font and keymap -*-
if [ -n "$ikmap" ]; then
font="${ikmap#*:}"
kmap="${ikmap%:*}"
[ "$font" = "$kmap" ] && font=
fi
[ -z "$font" ] && [ -f "$CONFDIR/font" ] && font=$(cat $CONFDIR/font)
[ -z "$kmap" ] && [ -f "$CONFDIR/kmap" ] && kmap=$(cat $CONFDIR/kmap)
FONTDIR=/usr/share/consolefonts
KMAPDIR=/usr/share/keymaps
for file in $FONTDIR/$font*; do
[ -f "$file" ] && debug loadfont < $file && break
done
for file in $KMAPDIR/$kmap*; do
[ -f "$file" ] && debug loadkmap < $file && break
done
unset FONTDIR KMAPDIR file font kmap
# -*- Logo and or boot message -*-
[ -f /etc/issue.logo ] && cat /etc/issue.logo
debug test -f /etc/msg && clear &&
while read _line; do
msg -i "$_line"
done </etc/msg
# -*- Hooks -*-
lvl 2
[ -n "$ibcache" ] && HOOKS="bcache $HOOKS"
[ -n "$izfs" ] && HOOKS="zfs $HOOKS"
[ -n "$izram" ] && HOOKS="zram $HOOKS"
export HOOKS="$(echo $HOOKS | sed -e 's/:/ /g')"
for hook in $HOOKS; do
if [ -e "$LIBDIR/$hook" ]; then
debug dohook "$LIBDIR/$hook" ||
error "hook: $hook failed to excecute hook"
else
error "hook: $hook not found"
fi
done
# -*- Swap -*-
# @VARIABLE: SWAP
# @DESCRIPTION: this variable should be set if swap is available
if [ -n "$iswap" ]; then
lvl 2s
[ -z "$SWAP" ] && debug -d bkd "$iswap" "SWAP" "2" "$ikswap"
fi
# -*- Resume -*-
# @VARIABLE: RESU
# @DESCRIPTION: if resume swap is avalable, this variable should be set
if [ -n "$iresume" ]; then
if [ -z "$RESU" ] && [ "$iresume" != "swap" ]; then
debug -d bkd "$iresume" "RESU" "3" "$ikresume"
fi
lvl 2r
debug resume "${RESU:-$SWAP}"
fi
# -*- Root -*-
lvl 3
# @VARIABLE: ROOT
# @DESCRIPTION: if rootfs is mounted, this variable should be set
dorootfs() {
local FS CHK ROOT
eval set -- $(echo "$iroot" | sed -e 's/:/ /g')
FS="$2" CHK="$3" ROOT="$1"
debug -d bkd "$iroot" "ROOT" "1" "$ikroot"
lvl 3f
if [ -n "$CHK" ]; then
case $CHK in
([NnFfOo]*|0) CHK=-e;;
([CcYyTt]*|1) CHK=-d;;
(*) CHK= ;;
esac
debug $CHK fsck ${FS:+-t$FS} $ROOT
fi
lvl 3m
if [ -n "$mopt" ]; then
imopt="${imopt:+$imopt,}$mopt"
unset mopt
fi
debug mount ${imopt:+-o$imopt} ${FS:+-t$FS} $ROOT $NEWROOT ||
die "Failed to mount rootfs"
}
[ -z "$ROOT" ] && dorootfs
[ -n "$isquashd" ] && lvl 3s && debug -d squashd
# -*- Clean up and switch -*-
lvl 4
[ -n "$LBK" ] &&
for _k in $(echo $LBK | sed '/:/ /g'); do
debug dmclose "$_k"
done
[ -n "$LBD" ] &&
for _d in $(echo $LBD | sed 's/:/ /g'); 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
[ -n "$imount" ] && lvl 4m && debug fmount
lvl 4u
echo >/proc/sys/kernel/hotplug
msg -i "Switching / ..."
$SPLD && debug spld_stop
sleep 1
debug -d umount -l /sys
debug -d umount -l /dev/pts
debug -d umount -l /dev
export LOGFILE=/$0.log
debug -d umount -l /run
debug -d umount -l /proc
lvl 4s
debug -d exec switch_root $NEWROOT ${init:-/sbin/init} $RIL
#
# vim:fenc=utf-8:ft=sh:ci:pi:sts=0:sw=4:ts=4:
#