tinyramfs/usr/share/tinyramfs/init

158 lines
4.0 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"
2020-02-24 13:49:38 +05:30
2020-02-21 14:27:07 +05:30
# TODO fix job control
2020-03-08 00:54:59 +05:30
sh
2020-01-30 20:10:13 +05:30
}
2020-01-28 20:43:42 +05:30
2020-02-21 14:27:07 +05:30
parse_cmdline() {
2020-02-27 23:51:34 +05:30
read -r cmdline < /proc/cmdline
set -f && set +f -- $cmdline
2020-02-21 14:27:07 +05:30
for line in "$@"; do
2020-02-26 01:13:25 +05:30
value="${line##*=}"
2020-02-24 13:49:38 +05:30
2020-02-21 14:27:07 +05:30
case "${line%%=*}" in
2020-02-26 01:13:25 +05:30
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 14:27:07 +05:30
# TODO implement
2020-03-08 00:54:59 +05:30
#lvm.discard) ;;
#lvm.config) ;;
#luks.header) ;;
#luks.keyfile) ;;
2020-02-21 14:27:07 +05:30
esac
done
}
2020-01-05 23:31:39 +05:30
2020-02-26 01:13:25 +05:30
mount_pseudofs() {
mount -t proc none /proc
mount -t sysfs none /sys
2020-01-30 20:10:13 +05:30
mount -t devtmpfs none /dev
}
2020-01-05 23:31:39 +05:30
2020-02-26 01:13:25 +05:30
setup_devmgr() {
case "$devmgr" in
udev)
2020-03-08 04:44:59 +05:30
udevd -d -N never
2020-02-26 01:13:25 +05:30
udevadm trigger -c add -t subsystems
udevadm trigger -c add -t devices
udevadm settle
;;
mdev)
2020-03-08 00:54:59 +05:30
mdev -df &
2020-02-26 01:13:25 +05:30
find /sys -name modalias -type f -exec sort -u {} + |
2020-03-08 00:54:59 +05:30
xargs modprobe -ba
2020-02-26 01:13:25 +05:30
;;
mdevd)
mdevd &
mdevd-coldplug
;;
esac
2020-01-30 20:10:13 +05:30
}
2020-01-05 23:31:39 +05:30
findfs_sh() {
2020-02-26 01:13:25 +05:30
value="${1##*=}"
case "${1%%=*}" in
2020-02-26 01:13:25 +05:30
LABEL) device="/dev/disk/by-label/${value}" ;;
UUID) device="/dev/disk/by-uuid/${value}" ;;
PARTUUID) device="/dev/disk/by-partuuid/${value}" ;;
/dev/*) device="$1" ;;
esac
2020-02-16 01:03:13 +05:30
# avoid race condition
2020-02-19 15:18:46 +05:30
while [ ! -e "$device" ]; do
sleep 0.5
[ "$increment" ] || increment=0
increment=$(( increment + 1 ))
2020-02-27 23:51:34 +05:30
[ "$increment" = 10 ] && panic "failed to lookup partition"
2020-02-19 15:18:46 +05:30
done
2020-02-16 01:03:13 +05:30
printf "%s\n" "$device"
}
2020-01-30 20:10:13 +05:30
unlock_luks() {
[ "$luks_discard" = 1 ] && luks_args="--allow-discards $luks_args"
[ -f /root/luks_header ] && luks_args="--header=/root/luks_header $luks_args"
[ -f /root/luks_keyfile ] && luks_args="--key-file=/root/luks_keyfile $luks_args"
2020-02-27 23:51:34 +05:30
cryptsetup $luks_args \
luksOpen \
$(findfs_sh "$luks_root") \
${luks_name:-luks_root} ||
panic "failed to unlock luks container"
2020-01-25 16:57:02 +05:30
}
2020-01-30 20:10:13 +05:30
trigger_lvm() {
2020-02-21 14:27:07 +05:30
if [ "$lvm_group" ] && [ "$lvm_name" ]; then
2020-02-26 01:13:25 +05:30
lvm lvchange $lvm_args --sysinit -q -a y "${lvm_group}/${lvm_name}" > /dev/null
2020-02-21 14:27:07 +05:30
elif [ "$lvm_group" ]; then
2020-02-26 01:13:25 +05:30
lvm vgchange $lvm_args --sysinit -q -a y "$lvm_group" > /dev/null
2020-02-21 14:27:07 +05:30
else
2020-02-26 01:13:25 +05:30
lvm vgchange $lvm_args --sysinit -q -a y > /dev/null
2020-02-21 14:27:07 +05:30
fi
2020-01-28 20:43:42 +05:30
}
2020-01-05 23:31:39 +05:30
2020-02-26 01:13:25 +05:30
mount_rootfs() {
2020-02-27 23:51:34 +05:30
mount ${root_type:+-t $root_type} \
${root_opts:+-o $root_opts} \
$(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-01-30 20:10:13 +05:30
cleanup() {
case "$devmgr" in
2020-02-26 01:13:25 +05:30
udev) udevadm control -e ;;
2020-03-08 00:54:59 +05:30
mdev) killall mdev ;;
2020-02-26 01:13:25 +05:30
mdevd) killall mdevd ;;
esac
2020-02-26 01:13:25 +05:30
umount /dev /sys /proc
2020-01-30 20:10:13 +05:30
}
2020-01-05 23:31:39 +05:30
2020-01-30 20:10:13 +05:30
boot_system() {
exec switch_root \
/mnt/root \
${init:-/sbin/init} ||
panic "failed to boot system"
2020-01-30 20:10:13 +05:30
}
/sbin/busybox --install -s
2020-02-27 23:51:34 +05:30
. /config || panic "failed to source config"
2020-01-30 20:10:13 +05:30
2020-02-26 01:13:25 +05:30
mount_pseudofs
2020-02-21 14:27:07 +05:30
parse_cmdline
2020-02-22 23:16:57 +05:30
[ "$debug" = 1 ] && set -x
2020-02-26 01:13:25 +05:30
setup_devmgr
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-27 23:51:34 +05:30
[ "$luks" = 1 ] &&
command -v cryptsetup > /dev/null 2>&1 && unlock_luks
2020-02-26 01:13:25 +05:30
2020-02-27 23:51:34 +05:30
[ "$lvm" = 1 ] &&
command -v lvm > /dev/null 2>&1 && trigger_lvm
2020-02-26 01:13:25 +05:30
mount_rootfs
2020-02-27 23:51:34 +05:30
[ "$debug" = 1 ] && panic "dropping to shell"
2020-01-30 20:10:13 +05:30
cleanup
boot_system