166 lines
4.2 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-22 20:46:57 +03:00
# turn output into list
set -- $(cat /proc/cmdline)
2020-02-21 11:57:07 +03:00
# parse line by line
for line in "$@"; do
2020-02-24 11:19:38 +03:00
2020-02-21 11:57:07 +03:00
# parse options
case "${line%%=*}" in
2020-02-22 20:46:57 +03:00
debug) debug="${line##*=}" ;;
2020-02-21 11:57:07 +03:00
init) init="${line##*=}" ;;
root) root="${line##*=}" ;;
root.type) root_type="${line##*=}" ;;
root.opts) root_opts="${line##*=}" ;;
lvm) lvm="${line##*=}" ;;
lvm.name) lvm_name="${line##*=}" ;;
lvm.group) lvm_group="${line##*=}" ;;
lvm.args) lvm_args="${line##*=}" ;;
luks) luks="${line##*=}" ;;
luks.root) luks_root="${line##*=}" ;;
luks.name) luks_name="${line##*=}" ;;
luks.discard) luks_discard="${line##*=}" ;;
luks.args) luks_args="${line##*=}" ;;
# TODO implement
#lvm.discard) ;;
#lvm.conf) ;;
#luks_header) ;;
#luks_keyfile) ;;
esac
done
}
2020-01-05 21:01:39 +03:00
2020-01-30 17:40:13 +03:00
mnt_pseudofs() {
mount -t proc none /proc
mount -t sysfs none /sys
mount -t devtmpfs none /dev
mount -t tmpfs none /tmp
2020-01-30 17:40:13 +03:00
}
2020-01-05 21:01:39 +03:00
2020-02-14 20:12:45 +03:00
setup_mdev() {
2020-02-02 15:37:14 +03:00
# setup hotplugger
2020-01-28 18:13:42 +03:00
if [ -e /proc/sys/kernel/hotplug ]; then
2020-02-11 21:02:23 +03:00
printf /sbin/mdev > /proc/sys/kernel/hotplug
2020-01-28 18:13:42 +03:00
else
2020-02-11 21:02:23 +03:00
uevent mdev &
2020-01-28 18:13:42 +03:00
fi
2020-01-19 00:01:21 +03:00
2020-01-28 18:13:42 +03:00
# trigger mdev
mdev -s
# trigger uevent for usb devices
2020-02-13 05:18:53 +03:00
for device in /sys/bus/usb/devices/*; do
2020-02-21 11:57:07 +03:00
case "${device##*/}" in
[0-9]*-[0-9]*) printf add > "${device}/uevent" ;;
2020-01-28 18:13:42 +03:00
esac
done
# load drivers
2020-02-18 10:53:03 +03:00
find /sys -name modalias -type f -exec sort -u "{}" "+" | xargs modprobe -qba
2020-01-30 17:40:13 +03:00
}
2020-02-14 20:12:45 +03:00
setup_mdevd() {
2020-02-02 15:37:14 +03:00
# setup daemon
2020-01-30 17:40:13 +03:00
mdevd &
2020-02-24 11:19:38 +03:00
2020-01-30 17:40:13 +03:00
# trigger uevents
mdevd-coldplug
}
2020-02-14 20:12:45 +03:00
setup_udev() {
2020-01-28 18:13:42 +03:00
udevd --daemon
udevadm trigger --action=add --type=subsystems
udevadm trigger --action=add --type=devices
udevadm settle
2020-01-30 17:40:13 +03:00
}
2020-01-05 21:01:39 +03:00
findfs_sh() {
case "${1%%=*}" in
2020-02-19 10:53:43 +03:00
LABEL) device="/dev/disk/by-label/${1##*=}" ;;
UUID) device="/dev/disk/by-uuid/${1##*=}" ;;
PARTUUID) device="/dev/disk/by-partuuid/${1##*=}" ;;
/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-21 11:57:07 +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-21 11:57:07 +03:00
[ "$luks_discard" = 1 ] && luks_args="--allow-discards $luks_args"
2020-02-19 10:12:37 +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
lvm lvchange $lvm_args --quiet --sysinit -a y "${lvm_group}/${lvm_name}" > /dev/null
elif [ "$lvm_group" ]; then
lvm vgchange $lvm_args --quiet --sysinit -a y "$lvm_group" > /dev/null
else
lvm vgchange $lvm_args --quiet --sysinit -a y > /dev/null
fi
2020-01-28 18:13:42 +03:00
}
2020-01-05 21:01:39 +03:00
2020-01-30 17:40:13 +03:00
mnt_rootfs() {
2020-02-21 11:57:07 +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-19 10:53:43 +03:00
mdev) { printf "" > /proc/sys/kernel/hotplug || killall uevent; } > /dev/null 2>&1 ;;
2020-02-13 05:18:53 +03:00
mdevd) killall mdevd ;;
2020-02-19 10:53:43 +03:00
udev) udevadm control --exit ;;
esac
# unmount pseudofs's
umount /dev /sys /proc /tmp
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-21 11:57:07 +03:00
exec switch_root /mnt/root ${init:-/sbin/init} || panic "failed to boot system"
2020-01-30 17:40:13 +03:00
}
/sbin/busybox --install -s
2020-02-13 05:18:53 +03:00
. /config || panic "failed to source config"
2020-01-30 17:40:13 +03:00
mnt_pseudofs
2020-02-21 11:57:07 +03:00
parse_cmdline
# debug mode
2020-02-22 20:46:57 +03:00
[ "$debug" = 1 ] && set -x
2020-01-30 18:28:03 +03:00
2020-01-30 17:40:13 +03:00
case "$devmgr" in
2020-02-19 10:53:43 +03:00
mdev) setup_mdev ;;
2020-02-14 20:12:45 +03:00
mdevd) setup_mdevd ;;
2020-02-19 10:53:43 +03:00
udev) setup_udev ;;
*) panic "devmgr option broken" ;;
2020-01-30 17:40:13 +03:00
esac
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-22 20:46:57 +03:00
[ "$luks" = 1 ] && [ -x "$(command -v cryptsetup)" ] && unlock_luks
[ "$lvm" = 1 ] && [ -x "$(command -v lvm)" ] && trigger_lvm
2020-01-30 17:40:13 +03:00
mnt_rootfs
2020-02-22 20:46:57 +03:00
[ "$debug" = 1 ] && panic "dropping to shell"
2020-01-30 17:40:13 +03:00
cleanup
boot_system