171 lines
4.3 KiB
Plaintext
Raw Normal View History

2020-01-27 16:39:58 +03:00
#!/sbin/busybox sh
2020-01-30 17:40:13 +03:00
#
# tiny init script
2020-01-05 21:01:39 +03:00
2020-01-30 17:40:13 +03:00
panic() {
2020-02-13 05:18:53 +03:00
printf "panic >> %s\n" "$1"
2020-02-24 11:19:38 +03:00
2020-02-21 11:57:07 +03:00
# TODO fix job control
2020-02-13 05:18:53 +03:00
sh -l
2020-01-30 17:40:13 +03:00
}
2020-01-28 18:13:42 +03:00
2020-02-21 11:57:07 +03:00
parse_cmdline() {
2020-02-29 14:46:13 +03:00
2020-02-27 21:21:34 +03:00
# store output in variable
read -r cmdline < /proc/cmdline
2020-02-22 20:46:57 +03:00
# turn output into list
2020-02-27 21:21:34 +03:00
set -f && set +f -- $cmdline
2020-02-21 11:57:07 +03:00
for line in "$@"; do
2020-02-25 22:43:25 +03:00
value="${line##*=}"
2020-02-24 11:19:38 +03:00
2020-02-21 11:57:07 +03:00
case "${line%%=*}" in
2020-02-25 22:43:25 +03:00
debug) debug="$value" ;;
init) init="$value" ;;
root) root="$value" ;;
root.type) root_type="$value" ;;
root.opts) root_opts="$value" ;;
lvm) lvm="$value" ;;
lvm.name) lvm_name="$value" ;;
lvm.group) lvm_group="$value" ;;
lvm.args) lvm_args="$value" ;;
luks) luks="$value" ;;
luks.root) luks_root="$value" ;;
luks.name) luks_name="$value" ;;
luks.discard) luks_discard="$value" ;;
luks.args) luks_args="$value" ;;
2020-02-21 11:57:07 +03:00
# TODO implement
#lvm.discard) ;;
#lvm.conf) ;;
#luks_header) ;;
#luks_keyfile) ;;
esac
done
}
2020-01-05 21:01:39 +03:00
2020-02-25 22:43:25 +03:00
mount_pseudofs() {
mount -t proc none /proc
mount -t sysfs none /sys
2020-01-30 17:40:13 +03:00
mount -t devtmpfs none /dev
}
2020-01-05 21:01:39 +03:00
2020-02-25 22:43:25 +03:00
setup_devmgr() {
case "$devmgr" in
udev)
udevd -d
udevadm trigger -c add -t subsystems
udevadm trigger -c add -t devices
udevadm settle
;;
mdev)
uevent mdev &
mdev -s
for device in /sys/bus/usb/devices/*; do
case "${device##*/}" in [0-9]*-[0-9]*)
printf add > "${device}/uevent" ;;
esac
done
find /sys -name modalias -type f -exec sort -u {} + |
xargs modprobe -b > /dev/null 2>&1
;;
mdevd)
mdevd &
mdevd-coldplug
;;
*)
panic "devmgr option broken"
;;
esac
2020-01-30 17:40:13 +03:00
}
2020-01-05 21:01:39 +03:00
findfs_sh() {
2020-02-25 22:43:25 +03:00
value="${1##*=}"
case "${1%%=*}" in
2020-02-25 22:43:25 +03:00
LABEL) device="/dev/disk/by-label/${value}" ;;
UUID) device="/dev/disk/by-uuid/${value}" ;;
PARTUUID) device="/dev/disk/by-partuuid/${value}" ;;
/dev/*) device="$1" ;;
*) panic "findfs option broken" ;;
esac
2020-02-15 22:33:13 +03:00
# avoid race condition
2020-02-19 12:48:46 +03:00
while [ ! -e "$device" ]; do
sleep 0.5
[ "$increment" ] || increment=0
increment=$(( increment + 1 ))
2020-02-27 21:21:34 +03:00
[ "$increment" = 10 ] && panic "failed to lookup partition"
2020-02-19 12:48:46 +03:00
done
2020-02-15 22:33:13 +03:00
printf "%s\n" "$device"
}
2020-01-30 17:40:13 +03:00
unlock_luks() {
2020-02-27 21:21:34 +03:00
[ "$luks_discard" = 1 ] && luks_args="--allow-discards $luks_args"
2020-02-27 21:21:34 +03:00
cryptsetup $luks_args \
luksOpen \
$(findfs_sh "$luks_root") \
${luks_name:-luks_root} ||
panic "failed to unlock luks container"
2020-01-25 14:27:02 +03:00
}
2020-01-30 17:40:13 +03:00
trigger_lvm() {
2020-02-21 11:57:07 +03:00
if [ "$lvm_group" ] && [ "$lvm_name" ]; then
2020-02-25 22:43:25 +03:00
lvm lvchange $lvm_args --sysinit -q -a y "${lvm_group}/${lvm_name}" > /dev/null
2020-02-21 11:57:07 +03:00
elif [ "$lvm_group" ]; then
2020-02-25 22:43:25 +03:00
lvm vgchange $lvm_args --sysinit -q -a y "$lvm_group" > /dev/null
2020-02-21 11:57:07 +03:00
else
2020-02-25 22:43:25 +03:00
lvm vgchange $lvm_args --sysinit -q -a y > /dev/null
2020-02-21 11:57:07 +03:00
fi
2020-01-28 18:13:42 +03:00
}
2020-01-05 21:01:39 +03:00
2020-02-25 22:43:25 +03:00
mount_rootfs() {
2020-02-27 21:21:34 +03:00
mount ${root_type:+-t $root_type} \
${root_opts:+-o $root_opts} \
$(findfs_sh "$root") \
/mnt/root ||
panic "failed to mount rootfs"
2020-01-30 17:40:13 +03:00
}
2020-01-05 21:01:39 +03:00
2020-01-30 17:40:13 +03:00
cleanup() {
case "$devmgr" in
2020-02-25 22:43:25 +03:00
udev) udevadm control -e ;;
mdev) killall uevent ;;
mdevd) killall mdevd ;;
esac
# unmount pseudofs's
2020-02-25 22:43:25 +03:00
umount /dev /sys /proc
2020-01-30 17:40:13 +03:00
}
2020-01-05 21:01:39 +03:00
2020-01-30 17:40:13 +03:00
boot_system() {
2020-02-27 21:21:34 +03:00
exec switch_root /mnt/root \
2020-02-29 14:46:13 +03:00
${init:-/sbin/init} ||
panic "failed to boot system"
2020-01-30 17:40:13 +03:00
}
/sbin/busybox --install -s
2020-02-27 21:21:34 +03:00
. /config || panic "failed to source config"
2020-01-30 17:40:13 +03:00
2020-02-25 22:43:25 +03:00
mount_pseudofs
2020-02-21 11:57:07 +03:00
parse_cmdline
2020-02-22 20:46:57 +03:00
[ "$debug" = 1 ] && set -x
2020-02-25 22:43:25 +03:00
setup_devmgr
2020-01-30 18:28:03 +03:00
2020-01-30 17:40:13 +03:00
# TODO handle situations when LUKS on LVM
2020-02-27 21:21:34 +03:00
[ "$luks" = 1 ] &&
command -v cryptsetup > /dev/null 2>&1 && unlock_luks
2020-02-25 22:43:25 +03:00
2020-02-27 21:21:34 +03:00
[ "$lvm" = 1 ] &&
command -v lvm > /dev/null 2>&1 && trigger_lvm
2020-02-25 22:43:25 +03:00
mount_rootfs
2020-02-27 21:21:34 +03:00
[ "$debug" = 1 ] && panic "dropping to shell"
2020-01-30 17:40:13 +03:00
cleanup
boot_system