tinyramfs/init

160 lines
3.2 KiB
Plaintext
Raw Normal View History

2020-01-27 19:09:58 +05:30
#!/sbin/busybox sh
2020-01-30 20:10:13 +05:30
#
# tiny init script
2020-01-05 23:31:39 +05:30
2020-01-30 20:10:13 +05:30
panic() {
2020-02-13 07:48:53 +05:30
printf "panic >> %s\n" "$1"
sh -l
2020-01-30 20:10:13 +05:30
}
2020-01-28 20:43:42 +05:30
2020-01-30 20:10:13 +05:30
# parse_cmdline() {
2020-01-28 20:43:42 +05:30
# TODO parse /proc/cmdline
2020-01-30 20:10:13 +05:30
#}
2020-01-05 23:31:39 +05:30
2020-02-02 18:07:14 +05:30
# mount pseudofs's
2020-01-30 20:10:13 +05:30
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 20:10:13 +05:30
}
2020-01-05 23:31:39 +05:30
2020-02-02 18:07:14 +05:30
# setup mdev
2020-02-14 22:42:45 +05:30
setup_mdev() {
2020-02-02 18:07:14 +05:30
# setup hotplugger
2020-01-28 20:43:42 +05:30
if [ -e /proc/sys/kernel/hotplug ]; then
2020-02-11 23:32:23 +05:30
printf /sbin/mdev > /proc/sys/kernel/hotplug
2020-01-28 20:43:42 +05:30
else
2020-02-11 23:32:23 +05:30
uevent mdev &
2020-01-28 20:43:42 +05:30
fi
2020-01-19 02:31:21 +05:30
2020-01-28 20:43:42 +05:30
# trigger mdev
mdev -s
# trigger uevent for usb devices
2020-02-13 07:48:53 +05:30
for device in /sys/bus/usb/devices/*; do
case ${device##*/} in
2020-02-11 23:32:23 +05:30
[0-9]*-[0-9]*)
2020-02-13 07:48:53 +05:30
printf add > "${device}/uevent"
2020-02-11 23:32:23 +05:30
;;
2020-01-28 20:43:42 +05:30
esac
done
# load drivers
2020-02-09 18:54:54 +05:30
find /sys -name "modalias" -type f -exec sort -u "{}" "+" | xargs modprobe -qba
2020-01-30 20:10:13 +05:30
}
2020-02-02 18:07:14 +05:30
# setup mdevd
2020-02-14 22:42:45 +05:30
setup_mdevd() {
2020-02-02 18:07:14 +05:30
# setup daemon
2020-01-30 20:10:13 +05:30
mdevd &
# trigger uevents
mdevd-coldplug
}
2020-02-02 18:07:14 +05:30
# setup udev
2020-02-14 22:42:45 +05:30
setup_udev() {
2020-01-28 20:43:42 +05:30
udevd --daemon
udevadm trigger --action=add --type=subsystems
udevadm trigger --action=add --type=devices
udevadm settle
2020-01-30 20:10:13 +05:30
}
2020-01-05 23:31:39 +05:30
# shell findfs
findfs_sh() {
case "${1%%=*}" in
2020-02-11 23:32:23 +05:30
LABEL)
printf "/dev/disk/by-label/%s\n" "${1##*=}"
;;
UUID)
printf "/dev/disk/by-uuid/%s\n" "${1##*=}"
;;
PARTUUID)
printf "/dev/disk/by-partuuid/%s\n" "${1##*=}"
;;
/dev/*)
printf "%s\n" "$1"
;;
*)
panic "findfs option broken"
;;
esac
}
2020-02-02 18:07:14 +05:30
# unlock LUKS container
2020-01-30 20:10:13 +05:30
unlock_luks() {
# TODO investigate this
# avoid race condition
2020-02-14 01:46:36 +05:30
sleep 1.5
2020-01-30 20:10:13 +05:30
2020-01-25 16:57:02 +05:30
# TODO improve mapper name ( crypttab or config option )
2020-02-13 07:48:53 +05:30
cryptsetup $luks_args luksOpen $(findfs_sh "$luks_root") luks_root || panic "failed to unlock luks container"
2020-01-25 16:57:02 +05:30
}
2020-02-14 01:46:36 +05:30
# trigger LVM
2020-01-30 20:10:13 +05:30
trigger_lvm() {
2020-02-11 23:32:23 +05:30
lvm vgchange --quiet --sysinit -a y > /dev/null
2020-01-28 20:43:42 +05:30
}
2020-01-05 23:31:39 +05:30
2020-02-02 18:07:14 +05:30
# mount rootfs to /mnt/root
2020-01-30 20:10:13 +05:30
mnt_rootfs() {
2020-02-14 01:46:36 +05:30
# TODO investigate this
# avoid race condition
sleep 1.5
2020-02-13 07:48:53 +05:30
mount $root_type $root_args $(findfs_sh "$root") /mnt/root || panic "failed to mount rootfs"
2020-01-30 20:10:13 +05:30
}
2020-01-05 23:31:39 +05:30
2020-02-02 18:07:14 +05:30
# kill and unmount
2020-01-30 20:10:13 +05:30
cleanup() {
case "$devmgr" in
2020-02-13 07:48:53 +05:30
mdev) { printf "" > /proc/sys/kernel/hotplug || killall uevent; } > /dev/null 2>&1 ;;
mdevd) killall mdevd ;;
udev) udevadm control --exit ;;
esac
# TODO re-do
2020-02-02 18:07:14 +05:30
# if debug mode off then restore kernel logging
2020-02-11 23:32:23 +05:30
[ "$debug" = 0 ] && printf 1 > /proc/sys/kernel/printk
# unmount pseudofs's
umount /dev /sys /proc /tmp
2020-01-30 20:10:13 +05:30
}
2020-01-05 23:31:39 +05:30
2020-02-02 18:07:14 +05:30
# exec /mnt/root/sbin/init
2020-01-30 20:10:13 +05:30
boot_system() {
exec switch_root /mnt/root /sbin/init || panic "failed to boot system"
}
# install busybox
/sbin/busybox --install -s
2020-02-02 18:07:14 +05:30
# source config
2020-02-13 07:48:53 +05:30
. /config || panic "failed to source config"
2020-01-30 20:10:13 +05:30
# TODO re-do
2020-01-30 20:58:03 +05:30
if [ "$debug" = 1 ]; then
2020-02-11 23:32:23 +05:30
# debug shell commands
2020-01-30 20:58:03 +05:30
set -x
else
# silence is golden
2020-02-11 23:32:23 +05:30
printf 0 > /proc/sys/kernel/printk
2020-01-30 20:58:03 +05:30
fi
2020-01-30 20:10:13 +05:30
#parse_cmdline
mnt_pseudofs
2020-01-30 20:58:03 +05:30
2020-01-30 20:10:13 +05:30
case "$devmgr" in
2020-02-14 22:42:45 +05:30
mdev) setup_mdev ;;
mdevd) setup_mdevd ;;
udev) setup_udev ;;
2020-01-30 20:58:03 +05:30
*) panic "devmgr option broken" ;;
2020-01-30 20:10:13 +05:30
esac
2020-01-30 20:58:03 +05:30
2020-01-30 20:10:13 +05:30
# TODO handle situations when LUKS on LVM
2020-02-14 22:42:45 +05:30
[ "$luks" = 1 ] && unlock_luks
[ "$lvm" = 1 ] && trigger_lvm
2020-01-30 20:10:13 +05:30
mnt_rootfs
cleanup
boot_system