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-09 23:15:10 +05:30
|
|
|
printf "panic >> %s\n" "$1" && sh
|
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
|
2020-02-09 23:15:10 +05:30
|
|
|
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-01-30 20:10:13 +05:30
|
|
|
use_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-01-30 20:10:13 +05:30
|
|
|
printf /sbin/mdev >/proc/sys/kernel/hotplug
|
2020-01-28 20:43:42 +05:30
|
|
|
else
|
|
|
|
uevent mdev &
|
|
|
|
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
|
|
|
|
for u in /sys/bus/usb/devices/*; do
|
|
|
|
case ${u##*/} in
|
|
|
|
[0-9]*-[0-9]*)
|
2020-01-30 20:10:13 +05:30
|
|
|
printf add > "${u}/uevent"
|
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-01-30 20:10:13 +05:30
|
|
|
use_mdevd() {
|
2020-02-02 18:07:14 +05:30
|
|
|
# setup daemon
|
2020-01-30 20:10:13 +05:30
|
|
|
mdevd &
|
|
|
|
# trigger uevents
|
|
|
|
mdevd-coldplug
|
|
|
|
|
|
|
|
# TODO investigate this
|
|
|
|
# avoid race condition
|
|
|
|
sleep 1.5
|
|
|
|
}
|
|
|
|
|
2020-02-02 18:07:14 +05:30
|
|
|
# setup udev
|
2020-01-30 20:10:13 +05:30
|
|
|
use_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
|
|
|
|
2020-02-09 23:15:10 +05:30
|
|
|
# shell findfs
|
|
|
|
findfs_sh() {
|
|
|
|
case "${1%%=*}" in
|
|
|
|
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() {
|
|
|
|
# find device of luks root
|
2020-02-09 23:15:10 +05:30
|
|
|
luks_root="$(findfs_sh $luks_root)"
|
|
|
|
|
|
|
|
# TODO investigate this
|
|
|
|
# avoid race condition
|
|
|
|
[ "$devmgr" != "udev" ] && 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-01-30 20:10:13 +05:30
|
|
|
# unlock luks container
|
|
|
|
cryptsetup $luks_args luksOpen "$luks_root" luks_root || panic "failed to unlock luks container"
|
2020-01-25 16:57:02 +05:30
|
|
|
}
|
|
|
|
|
2020-02-02 18:07:14 +05:30
|
|
|
# manually trigger LVM if udev disabled
|
2020-01-30 20:10:13 +05:30
|
|
|
trigger_lvm() {
|
2020-02-09 23:15:10 +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() {
|
|
|
|
# merge mount flags
|
|
|
|
[ -n "$root_args" ] && mount_args="$root_args"
|
|
|
|
[ -n "$root_type" ] && mount_args="$mount_args -t $root_type"
|
2020-01-05 23:31:39 +05:30
|
|
|
|
2020-02-09 23:15:10 +05:30
|
|
|
# find root
|
|
|
|
root="$(findfs_sh $root)"
|
|
|
|
|
2020-01-30 20:10:13 +05:30
|
|
|
# mount rootfs
|
|
|
|
mount $mount_args "$root" /mnt/root || panic "failed to mount rootfs"
|
|
|
|
}
|
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() {
|
2020-02-09 23:15:10 +05:30
|
|
|
case "$devmgr" in
|
|
|
|
mdev)
|
|
|
|
# stop mdev
|
|
|
|
{ printf "" >/proc/sys/kernel/hotplug || killall uevent; } >/dev/null 2>&1
|
|
|
|
;;
|
|
|
|
mdevd)
|
|
|
|
# stop mdevd
|
|
|
|
killall mdevd
|
|
|
|
;;
|
|
|
|
udev)
|
|
|
|
# stop udev
|
|
|
|
udevadm control --exit
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# TODO re-do
|
2020-02-02 18:07:14 +05:30
|
|
|
# if debug mode off then restore kernel logging
|
|
|
|
[ "$debug" = 0 ] && printf 1 >/proc/sys/kernel/printk
|
2020-02-09 23:15:10 +05:30
|
|
|
|
|
|
|
# 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
|
|
|
|
. /config || panic "config doesn't exists"
|
2020-01-30 20:10:13 +05:30
|
|
|
|
2020-02-09 23:15:10 +05:30
|
|
|
# TODO re-do
|
2020-01-30 20:58:03 +05:30
|
|
|
if [ "$debug" = 1 ]; then
|
|
|
|
# debug shell commands
|
|
|
|
set -x
|
|
|
|
else
|
|
|
|
# silence is golden
|
|
|
|
printf 0 >/proc/sys/kernel/printk
|
|
|
|
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
|
|
|
|
mdev) use_mdev ;;
|
|
|
|
mdevd) use_mdevd ;;
|
|
|
|
udev) use_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
|
|
|
|
[ "$use_luks" = 1 ] && unlock_luks
|
2020-01-30 20:58:03 +05:30
|
|
|
[ "$use_lvm" = 1 ] && [ "$devmgr" != "udev" ] && trigger_lvm
|
2020-01-30 20:10:13 +05:30
|
|
|
mnt_rootfs
|
|
|
|
cleanup
|
|
|
|
boot_system
|