Files
airboot-linux-stage2/init.sh
T
2026-10-31 12:57:26 +08:00

188 lines
4.4 KiB
Bash

#!/bin/sh
# Global arguments (collected from kernel command line)
early_mount=
root=
live_copytoram=
rootfstype="auto"
rootflags=
init="/bin/init /sbin/init /init /bin/sh"
# Global state
services=
# Helper functions
log_info() {
printf '[\033[1mInfo\033[0m] %s\n' "$1"
}
log_error() {
printf '[\033[31mError\033[0m] %s\n' "$1"
}
start_service() {
"$@" >>"/tmp/$1.log" 2>&1 &
local pid=$!
services="$services $pid"
log_info "Started initramfs service $@ (pid $pid)."
}
kill_services() {
for kill_pid in $services; do
kill -SIGTERM $kill_pid
done
}
# Error handling
emergency() {
echo
echo "===== EMERGENCY SHELL ====="
echo "The operating system failed to boot. An emergency shell is now launched,"
echo "you can inspect, and do changes here. Once the shell exits, we will try"
echo "to boot the system again."
sh
umount -q /proc
umount -q /sys
umount -q /dev
umount -q /run
kill_services
exec /init
}
# Child steps
setup_live() {
mount --mkdir -t "${rootfstype}" -o "${rootflags}" "$1" /liveroot || emergency
if [ "${live_copytoram}" -eq "1" ]; then
log_info "Copying LiveCD root filesystem to system RAM..."
mount --mkdir -t tmpfs none /newroot || emergency
cp -a /liveroot/. /newroot/ || emergency
else
log_error "Live system must enable copytoram to boot currently."
emergency
fi
}
# Boot steps
prepare_env() {
# Set PATH to /bin
export PATH=/bin
# Mount necessary kernel filesystems.
mount --mkdir -t proc none /proc || emergency
mount --mkdir -t sysfs none /sys || emergency
mount --mkdir -t devtmpfs none /dev || emergency
mount --mkdir -t tmpfs none /run || emergency
mkdir -p /tmp || emergency
# Some kernels may require `/sbin` or `/usr/sbin` for modprobe helpers.
ln -sf /bin /sbin
mkdir -p /usr
ln -sf /bin /usr/bin
ln -sf /bin /usr/sbin
# Activate console output by emitting a newline
echo
}
start_lxdeviced() {
LXDEVICED_HAS_DEVTMPFS=1 start_service lxdeviced
until device wait-for-idle >>/tmp/device-start-loop.log 2>&1; do
sleep 0.05
done
device trigger --action=add
}
parse_kcmdline() {
local kcmdline=$(cat /proc/cmdline)
for entry in $kcmdline; do
case $entry in
early_mount=*)
early_mount="${early_mount} ${entry#early_mount=}"
;;
root=*)
root="${entry#root=}"
;;
copytoram)
live_copytoram="1"
;;
rootfstype=*)
rootfstype="${entry#rootfstype=}"
;;
rootflags=*)
rootflags="${rootflags},${entry#rootflags=}"
;;
ro)
rootflags="${rootflags},ro"
;;
init=*)
init="${entry#init=}"
;;
*=*)
export "${entry}"
;;
*)
;;
esac
done
}
do_early_mount() {
device wait-for-idle
for early_mount_spec in ${early_mount}; do
IFS=':' read -r early_mount_src early_mount_dst <<< "$early_mount_spec"
log_info "Mounting ${early_mount_src} to ${early_mount_dst}..."
mount --mkdir "${early_mount_src}" "${early_mount_dst}" || emergency
done
}
do_fsck() {
return
}
enter_newroot() {
device wait-for-idle
case "${root}" in
live:*)
liveroot="${root#live:}"
setup_live "${liveroot}"
;;
"")
log_error "Cannot find a root directory to be mounted."
emergency
;;
*)
log_info "Mounting new root filesystem ${root}(type ${rootfstype}, flags ${rootflags})..."
mount --mkdir -t "${rootfstype}" -o "${rootflags}" "${root}" /newroot || emergency
;;
esac
for init_candidate in $init; do
if [ -L "/newroot/${init_candidate}" ] || [ -e "/newroot/${init_candidate}" ]; then
log_info "Found init at ${init_candidate}, passing system control..."
kill_services
exec env -i switch_root /newroot "${init_candidate}"
fi
done
log_error "Error: No init program is available."
emergency
}
# Main logic
main() {
trap emergency EXIT
prepare_env
start_lxdeviced
parse_kcmdline
do_early_mount
do_fsck
enter_newroot
}
main