tinyramfs/init
2020-01-28 18:13:42 +03:00

96 lines
2.0 KiB
Plaintext

#!/sbin/busybox sh
# debugging
set -x
# install busybox
/sbin/busybox --install -s
panic() { echo "bruh moment :(" && sh; }
# silence is golden
#echo 0 >/proc/sys/kernel/printk
# TODO parse /proc/cmdline
# check config
[ -f /config ] && . /config || panic
# mount pseudofs's
mount -t proc none /proc
mount -t sysfs none /sys
mount -t devtmpfs none /dev
# handle device managers
if [ "$use_mdevd" = 1 ]; then
# setup mdevd
mdevd &
# trigger uevents
mdevd-coldplug
# TODO investigate this
# avoid race condition
sleep 1.5
elif [ "$use_mdev" = 1 ]; then
# setup mdev
if [ -e /proc/sys/kernel/hotplug ]; then
echo /sbin/mdev >/proc/sys/kernel/hotplug
else
uevent mdev &
fi
# trigger mdev
mdev -s
# trigger uevent for usb devices
for u in /sys/bus/usb/devices/*; do
case ${u##*/} in
[0-9]*-[0-9]*)
echo add > "$u/uevent"
;;
esac
done
# load drivers
find /sys -name 'modalias' -type f -exec cat '{}' + | sort -u | xargs modprobe -ba
elif [ "$use_udev" = 1 ]; then
# setup udev
udevd --daemon
udevadm trigger --action=add --type=subsystems
udevadm trigger --action=add --type=devices
udevadm settle
else
panic
fi
# TODO handle situations when LUKS on LVM
# unlock cryptsetup container
[ "$use_luks" = 1 ] && {
luks_root="$(findfs $luks_root)"
# TODO improve mapper name ( crypttab or config option )
cryptsetup $luks_args luksOpen "$luks_root" luks_root || panic
}
# manually trigger LVM if udev disabled
[ "$use_lvm" = 1 ] && [ "$use_udev" = 0 ] && {
lvm vgchange --sysinit -a y
}
# merge mount flags
[ -n "$root_args" ] && mount_args="$root_args"
[ -n "$root_type" ] && mount_args="$mount_args -t $root_type"
# mount rootfs
mount $mount_args "$root" /mnt/root || panic
# clean up
[ "$use_mdevd" = 1 ] && killall mdevd
[ "$use_mdev" = 1 ] && { echo "" >/proc/sys/kernel/hotplug || killall uevent; }
[ "$use_udev" = 1 ] && udevadm control --exit
umount /dev /sys /proc
# boot system
echo SUCCESS
exec switch_root /mnt/root /sbin/init