| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# Default PATH differs between shells, and is not automatically exported |
| 4 |
# by klibc dash. Make it consistent. |
| 5 |
export PATH=/sbin:/usr/sbin:/bin:/usr/bin |
| 6 |
|
| 7 |
[ -d /dev ] || mkdir -m 0755 /dev |
| 8 |
[ -d /root ] || mkdir -m 0700 /root |
| 9 |
[ -d /sys ] || mkdir /sys |
| 10 |
[ -d /proc ] || mkdir /proc |
| 11 |
[ -d /tmp ] || mkdir /tmp |
| 12 |
mkdir -p /var/lock |
| 13 |
mount -t sysfs -o nodev,noexec,nosuid sysfs /sys |
| 14 |
mount -t proc -o nodev,noexec,nosuid proc /proc |
| 15 |
|
| 16 |
# shellcheck disable=SC2013 |
| 17 |
for x in $(cat /proc/cmdline); do |
| 18 |
case $x in |
| 19 |
initramfs.clear) |
| 20 |
clear |
| 21 |
;; |
| 22 |
quiet) |
| 23 |
quiet=y |
| 24 |
;; |
| 25 |
esac |
| 26 |
done |
| 27 |
|
| 28 |
if [ "$quiet" != "y" ]; then |
| 29 |
quiet=n |
| 30 |
echo "Loading, please wait..." |
| 31 |
fi |
| 32 |
export quiet |
| 33 |
|
| 34 |
# Note that this only becomes /dev on the real filesystem if udev's scripts |
| 35 |
# are used; which they will be, but it's worth pointing out |
| 36 |
mount -t devtmpfs -o nosuid,mode=0755 udev /dev |
| 37 |
|
| 38 |
# Prepare the /dev directory |
| 39 |
[ ! -h /dev/fd ] && ln -s /proc/self/fd /dev/fd |
| 40 |
[ ! -h /dev/stdin ] && ln -s /proc/self/fd/0 /dev/stdin |
| 41 |
[ ! -h /dev/stdout ] && ln -s /proc/self/fd/1 /dev/stdout |
| 42 |
[ ! -h /dev/stderr ] && ln -s /proc/self/fd/2 /dev/stderr |
| 43 |
|
| 44 |
mkdir /dev/pts |
| 45 |
mount -t devpts -o noexec,nosuid,gid=5,mode=0600 devpts /dev/pts || true |
| 46 |
|
| 47 |
# Export the dpkg architecture |
| 48 |
export DPKG_ARCH= |
| 49 |
. /conf/arch.conf |
| 50 |
|
| 51 |
# Set modprobe env |
| 52 |
export MODPROBE_OPTIONS="-qb" |
| 53 |
|
| 54 |
# Export relevant variables |
| 55 |
export ROOT= |
| 56 |
export ROOTDELAY= |
| 57 |
export ROOTFLAGS= |
| 58 |
export ROOTFSTYPE= |
| 59 |
export IP= |
| 60 |
export DEVICE= |
| 61 |
export BOOT= |
| 62 |
export BOOTIF= |
| 63 |
export UBIMTD= |
| 64 |
export break= |
| 65 |
export init=/sbin/init |
| 66 |
export readonly=y |
| 67 |
export rootmnt=/root |
| 68 |
export debug= |
| 69 |
export panic= |
| 70 |
export blacklist= |
| 71 |
export resume= |
| 72 |
export resume_offset= |
| 73 |
export noresume= |
| 74 |
export drop_caps= |
| 75 |
export fastboot=n |
| 76 |
export forcefsck=n |
| 77 |
export fsckfix= |
| 78 |
|
| 79 |
|
| 80 |
# Bring in the main config |
| 81 |
. /conf/initramfs.conf |
| 82 |
for conf in conf/conf.d/*; do |
| 83 |
[ -f "${conf}" ] && . "${conf}" |
| 84 |
done |
| 85 |
. /scripts/functions |
| 86 |
|
| 87 |
# Parse command line options |
| 88 |
# shellcheck disable=SC2013 |
| 89 |
for x in $(cat /proc/cmdline); do |
| 90 |
case $x in |
| 91 |
init=*) |
| 92 |
init=${x#init=} |
| 93 |
;; |
| 94 |
root=*) |
| 95 |
ROOT=${x#root=} |
| 96 |
if [ -z "${BOOT}" ] && [ "$ROOT" = "/dev/nfs" ]; then |
| 97 |
BOOT=nfs |
| 98 |
fi |
| 99 |
;; |
| 100 |
rootflags=*) |
| 101 |
ROOTFLAGS="-o ${x#rootflags=}" |
| 102 |
;; |
| 103 |
rootfstype=*) |
| 104 |
# Linux interprets 'rootfstype=*tmpfs*' to control the |
| 105 |
# initramfs filesystem; we should remove 'tmpfs' from |
| 106 |
# the list |
| 107 |
ROOTFSTYPE="$(list_filter_out "${x#rootfstype=}" tmpfs)" |
| 108 |
;; |
| 109 |
rootdelay=*) |
| 110 |
ROOTDELAY="${x#rootdelay=}" |
| 111 |
case ${ROOTDELAY} in |
| 112 |
*[![:digit:].]*) |
| 113 |
ROOTDELAY= |
| 114 |
;; |
| 115 |
esac |
| 116 |
;; |
| 117 |
nfsroot=*) |
| 118 |
# shellcheck disable=SC2034 |
| 119 |
NFSROOT="${x#nfsroot=}" |
| 120 |
;; |
| 121 |
initramfs.runsize=*) |
| 122 |
RUNSIZE="${x#initramfs.runsize=}" |
| 123 |
;; |
| 124 |
ip=*) |
| 125 |
IP="${x#ip=}" |
| 126 |
;; |
| 127 |
boot=*) |
| 128 |
BOOT=${x#boot=} |
| 129 |
;; |
| 130 |
ubi.mtd=*) |
| 131 |
UBIMTD=${x#ubi.mtd=} |
| 132 |
;; |
| 133 |
resume=*) |
| 134 |
RESUME="${x#resume=}" |
| 135 |
;; |
| 136 |
resume_offset=*) |
| 137 |
resume_offset="${x#resume_offset=}" |
| 138 |
;; |
| 139 |
noresume) |
| 140 |
noresume=y |
| 141 |
;; |
| 142 |
drop_capabilities=*) |
| 143 |
drop_caps="-d ${x#drop_capabilities=}" |
| 144 |
;; |
| 145 |
panic=*) |
| 146 |
panic="${x#panic=}" |
| 147 |
;; |
| 148 |
ro) |
| 149 |
readonly=y |
| 150 |
;; |
| 151 |
rw) |
| 152 |
readonly=n |
| 153 |
;; |
| 154 |
debug) |
| 155 |
debug=y |
| 156 |
quiet=n |
| 157 |
if [ -n "${netconsole}" ]; then |
| 158 |
log_output=/dev/kmsg |
| 159 |
else |
| 160 |
log_output=/run/initramfs/initramfs.debug |
| 161 |
fi |
| 162 |
set -x |
| 163 |
;; |
| 164 |
debug=*) |
| 165 |
debug=y |
| 166 |
quiet=n |
| 167 |
set -x |
| 168 |
;; |
| 169 |
break=*) |
| 170 |
break=${x#break=} |
| 171 |
;; |
| 172 |
break) |
| 173 |
break=premount |
| 174 |
;; |
| 175 |
blacklist=*) |
| 176 |
blacklist=${x#blacklist=} |
| 177 |
;; |
| 178 |
netconsole=*) |
| 179 |
netconsole=${x#netconsole=} |
| 180 |
[ "$debug" = "y" ] && log_output=/dev/kmsg |
| 181 |
;; |
| 182 |
BOOTIF=*) |
| 183 |
BOOTIF=${x#BOOTIF=} |
| 184 |
;; |
| 185 |
fastboot|fsck.mode=skip) |
| 186 |
fastboot=y |
| 187 |
;; |
| 188 |
forcefsck|fsck.mode=force) |
| 189 |
forcefsck=y |
| 190 |
;; |
| 191 |
fsckfix|fsck.repair=yes) |
| 192 |
fsckfix=y |
| 193 |
;; |
| 194 |
fsck.repair=no) |
| 195 |
fsckfix=n |
| 196 |
;; |
| 197 |
esac |
| 198 |
done |
| 199 |
|
| 200 |
# Default to BOOT=local if no boot script defined. |
| 201 |
if [ -z "${BOOT}" ]; then |
| 202 |
BOOT=local |
| 203 |
fi |
| 204 |
|
| 205 |
if [ -n "${noresume}" ] || [ "$RESUME" = none ]; then |
| 206 |
noresume=y |
| 207 |
else |
| 208 |
resume=${RESUME:-} |
| 209 |
fi |
| 210 |
|
| 211 |
mount -t tmpfs -o "nodev,noexec,nosuid,size=${RUNSIZE:-10%},mode=0755" tmpfs /run |
| 212 |
mkdir -m 0700 /run/initramfs |
| 213 |
|
| 214 |
if [ -n "$log_output" ]; then |
| 215 |
exec >"$log_output" 2>&1 |
| 216 |
unset log_output |
| 217 |
fi |
| 218 |
|
| 219 |
maybe_break top |
| 220 |
|
| 221 |
# Don't do log messages here to avoid confusing graphical boots |
| 222 |
run_scripts /scripts/init-top |
| 223 |
|
| 224 |
maybe_break modules |
| 225 |
[ "$quiet" != "y" ] && log_begin_msg "Loading essential drivers" |
| 226 |
[ -n "${netconsole}" ] && /sbin/modprobe netconsole netconsole="${netconsole}" |
| 227 |
load_modules |
| 228 |
[ "$quiet" != "y" ] && log_end_msg |
| 229 |
|
| 230 |
starttime="$(_uptime)" |
| 231 |
starttime=$((starttime + 1)) # round up |
| 232 |
export starttime |
| 233 |
|
| 234 |
if [ "$ROOTDELAY" ]; then |
| 235 |
sleep "$ROOTDELAY" |
| 236 |
fi |
| 237 |
|
| 238 |
maybe_break premount |
| 239 |
[ "$quiet" != "y" ] && log_begin_msg "Running /scripts/init-premount" |
| 240 |
run_scripts /scripts/init-premount |
| 241 |
[ "$quiet" != "y" ] && log_end_msg |
| 242 |
|
| 243 |
maybe_break mount |
| 244 |
log_begin_msg "Mounting root file system" |
| 245 |
# Always load local and nfs (since these might be needed for /etc or |
| 246 |
# /usr, irrespective of the boot script used to mount the rootfs). |
| 247 |
. /scripts/local |
| 248 |
. /scripts/nfs |
| 249 |
. "/scripts/${BOOT}" |
| 250 |
parse_numeric "${ROOT}" |
| 251 |
maybe_break mountroot |
| 252 |
mount_top |
| 253 |
mount_premount |
| 254 |
mountroot |
| 255 |
log_end_msg |
| 256 |
|
| 257 |
if read_fstab_entry /usr; then |
| 258 |
log_begin_msg "Mounting /usr file system" |
| 259 |
mountfs /usr |
| 260 |
log_end_msg |
| 261 |
fi |
| 262 |
|
| 263 |
# Mount cleanup |
| 264 |
mount_bottom |
| 265 |
nfs_bottom |
| 266 |
local_bottom |
| 267 |
|
| 268 |
case "$IP" in |
| 269 |
""|none|off) ;; # Do nothing |
| 270 |
*) |
| 271 |
configure_networking |
| 272 |
esac |
| 273 |
|
| 274 |
maybe_break bottom |
| 275 |
[ "$quiet" != "y" ] && log_begin_msg "Running /scripts/init-bottom" |
| 276 |
# We expect udev's init-bottom script to move /dev to ${rootmnt}/dev |
| 277 |
run_scripts /scripts/init-bottom |
| 278 |
[ "$quiet" != "y" ] && log_end_msg |
| 279 |
|
| 280 |
# Move /run to the root |
| 281 |
mount -n -o move /run ${rootmnt}/run |
| 282 |
|
| 283 |
validate_init() { |
| 284 |
run-init -n "${rootmnt}" "${1}" |
| 285 |
} |
| 286 |
|
| 287 |
# Check init is really there |
| 288 |
if ! validate_init "$init"; then |
| 289 |
echo "Target filesystem doesn't have requested ${init}." |
| 290 |
init= |
| 291 |
for inittest in /sbin/init /etc/init /bin/init /bin/sh; do |
| 292 |
if validate_init "${inittest}"; then |
| 293 |
init="$inittest" |
| 294 |
break |
| 295 |
fi |
| 296 |
done |
| 297 |
fi |
| 298 |
|
| 299 |
# No init on rootmount |
| 300 |
if ! validate_init "${init}" ; then |
| 301 |
panic "No init found. Try passing init= bootarg." |
| 302 |
fi |
| 303 |
|
| 304 |
maybe_break init |
| 305 |
|
| 306 |
# don't leak too much of env - some init(8) don't clear it |
| 307 |
# (keep init, rootmnt, drop_caps) |
| 308 |
unset debug |
| 309 |
unset MODPROBE_OPTIONS |
| 310 |
unset DPKG_ARCH |
| 311 |
unset ROOTFLAGS |
| 312 |
unset ROOTFSTYPE |
| 313 |
unset ROOTDELAY |
| 314 |
unset ROOT |
| 315 |
unset IP |
| 316 |
unset BOOT |
| 317 |
unset BOOTIF |
| 318 |
unset DEVICE |
| 319 |
unset UBIMTD |
| 320 |
unset blacklist |
| 321 |
unset break |
| 322 |
unset noresume |
| 323 |
unset panic |
| 324 |
unset quiet |
| 325 |
unset readonly |
| 326 |
unset resume |
| 327 |
unset resume_offset |
| 328 |
unset noresume |
| 329 |
unset fastboot |
| 330 |
unset forcefsck |
| 331 |
unset fsckfix |
| 332 |
unset starttime |
| 333 |
|
| 334 |
# Move virtual filesystems over to the real filesystem |
| 335 |
mount -n -o move /sys ${rootmnt}/sys |
| 336 |
mount -n -o move /proc ${rootmnt}/proc |
| 337 |
|
| 338 |
# Chain to real filesystem |
| 339 |
# shellcheck disable=SC2086,SC2094 |
| 340 |
exec run-init ${drop_caps} "${rootmnt}" "${init}" "$@" <"${rootmnt}/dev/console" >"${rootmnt}/dev/console" 2>&1 |
| 341 |
echo "Something went badly wrong in the initramfs." |
| 342 |
panic "Please file a bug on initramfs-tools." |