-
Notifications
You must be signed in to change notification settings - Fork 0
/
init
executable file
·208 lines (171 loc) · 5.67 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
#!/bin/sh
# init - tiny init for initial ramdisk environment
# See COPYING and COPYRIGHT files for corresponding information.
# Intentional: "var is referenced but not assigned".
# https://www.shellcheck.net/wiki/SC2154
# shellcheck disable=SC2154
# Intentional: allow "local"s declaration.
# https://www.shellcheck.net/wiki/SC3043
# shellcheck disable=SC3043
export PATH=/bin TERM=linux SHELL=/bin/sh LANG=C LC_ALL=C PS1="# " \
HOME=/root
######################################################################
# Notification helpers. #
######################################################################
# Print info message to stdout.
# arg $1: message
info() {
echo "=======> $1"
}
# Print error message to stderr and terminate the program.
# arg $1: message
error() {
echo "=======> ERROR: $1" 1>&2
exit 1
}
# Print error message to stderr and fallback to the sh(1).
# arg $1: message
panic() {
echo "=======> ERROR: ${1:-Unexpected error occurred}" 1>&2
sh
}
######################################################################
# Init helpers. #
######################################################################
# Set ''device'' variable to full path of resolved UUID, PARTUUID,
# LABEL, or /dev/*.
# arg $1: device
# arg $2: seconds to wait until device is available (optional)
resolve_device() {
local _count="${2:-30}"
device="$1"
case "${device%%=*}" in
UUID) device="/dev/disk/by-uuid/${device#*=}" ;;
LABEL) device="/dev/disk/by-label/${device#*=}" ;;
PARTUUID) device="/dev/disk/by-partuuid/${device#*=}" ;;
/dev/*) ;;
*) return 0 ;;
esac
# Race condition may occur if device manager is not yet
# initialized device. To fix this, we simply waiting until
# device is available. If device didn't appear in specified
# time, we panic.
while : ; do
if [ -b "$device" ]; then
return 0
elif [ "$((_count -= 1))" = 0 ]; then
break
else
sleep 1
fi
done
panic "Failed to lookup partition: $device"
}
# Run hooks (by type) if any exist.
# arg $1: hook type (init or init.late)
run_hook() {
local _hook
local _hooktype="$1"
# Intentional: "can't follow non-constant source".
# https://www.shellcheck.net/wiki/SC1090
# shellcheck disable=SC1090
for _hook in $hooks; do
if [ -f "/usr/share/mkinitramfs/hooks/$_hook/$_hook.$_hooktype" ]; then
. "/usr/share/mkinitramfs/hooks/$_hook/$_hook.$_hooktype"
fi
done
}
# Mount base filesystems, like /proc, /sys, /run and /dev.
mount_basefs() {
mount -t proc -o nosuid,noexec,nodev proc /proc
mount -t sysfs -o nosuid,noexec,nodev sys /sys
mount -t tmpfs -o nosuid,nodev,mode=0755 run /run
mount -t devtmpfs -o nosuid,noexec,mode=0755 dev /dev
# HACK! Some scripts needs this.
ln -s /proc/self/fd /dev/fd
ln -s fd/0 /dev/stdin
ln -s fd/1 /dev/stdout
ln -s fd/2 /dev/stderr
}
# Read kernel command-line arguments.
parse_cmdline() {
local _cmdline
local _param
read -r _cmdline < /proc/cmdline
# https://kernel.org/doc/html/latest/admin-guide/kernel-parameters.html
# ... parameters with '=' go into init's environment ...
for _param in $_cmdline; do
case "$_param" in
rootfstype=*) root_type="${_param#*=}" ;;
rootflags=*) root_opts="${_param#*=}" ;;
rootdelay=*) root_wait="${_param#*=}" ;;
rootwait) root_wait=-1 ;;
debug=1) set -x ;;
ro|rw) rorw="$_param" ;;
init=*) init="${_param#*=}" ;;
--*) init_args="${cmdline##*--}" ; break ;;
*=*) command export "$_param" ;;
*) command export "$_param=1" ;;
esac 2>/dev/null || :
done
}
# Mount root filesystem.
mount_root() {
if [ "$break" = root ]; then
info "break before mount_root()"
sh
fi
resolve_device "$root" "${root_wait:-$rootdelay}"
# Intentional word splitting.
# https://www.shellcheck.net/wiki/SC2086
# shellcheck disable=SC2086
if ! mount -o "${rorw:-ro}${root_opts:+,$root_opts}" \
${root_type:+-t $root_type} -- "$device" /mnt/root; then
panic "Failed to mount rootfs: $device"
fi
}
# Switch to mounted root filesystem as the root of the mount tree and
# start init process.
boot_system() {
local _dir
if [ "$break" = boot ]; then
info "break before boot_system()"
sh
fi
# XXX Isn't switch_root(8) already doing this?
for _dir in run dev sys proc; do
mount -o move "/$_dir" "/mnt/root/$_dir" || :
done
# POSIX 'exec' has no '-c' flag to execute command with empty
# environment. Using 'env -i' instead to prevent leaking
# exported variables.
#
# Some implementations of 'switch_root' doesn't conform to
# POSIX utility guidelines and doesn't support '--'. This
# means that safety of init_args isn't guaranteed.
# Intentional word splitting.
# https://www.shellcheck.net/wiki/SC2086
# shellcheck disable=SC2086
exec env -i TERM=linux PATH=/bin:/sbin:/usr/bin:/usr/sbin \
switch_root /mnt/root "${init-/sbin/init}" $init_args ||
panic "Failed to boot system"
}
######################################################################
# -e: Exit if command return status greater than 0.
# -f: Disable globbing *?[].
set -ef
# Run emergency shell if init unexpectedly exiting due to error.
trap panic EXIT
# Read configuration.
# https://www.shellcheck.net/wiki/SC1091
# shellcheck disable=SC1091
. /etc/mkinitramfs/config
# Run init helpers.
mount_basefs
parse_cmdline
run_hook init
mount_root
run_hook init.late
boot_system
# vim: cc=72 tw=70
# End of file.