From 01b30b66336264972f51afe5ba65967f0e7bc034 Mon Sep 17 00:00:00 2001 From: illiliti Date: Fri, 21 Feb 2020 11:57:07 +0300 Subject: [PATCH] implement parse_cmdline --- config | 25 ++++++++++++++++---- generate | 28 ++++++---------------- init | 71 ++++++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 81 insertions(+), 43 deletions(-) diff --git a/config b/config index 60d677b..7f57e21 100644 --- a/config +++ b/config @@ -3,7 +3,13 @@ # # debug mode -#debug=1 +shell_debug=1 + +# drop into shell after mnt_rootfs +#shell_break=0 + +# custom init +#init="" # custom output name #initramfs="" @@ -24,8 +30,8 @@ root="UUID=07729c48-25d8-4096-acaf-ce5322915680" # change this if you using busybox util-linux root_type="ext4" -# root mount options -#root_args="" +# root options +#root_opts="" # disable this if you want to get rid of util-linux # NOTE busybox util-linux implemetation doesn't have @@ -47,12 +53,21 @@ hostonly=1 # LVM support lvm=1 +# LVM logical volume name +#lvm_name="" + +# LVM volume group name +#lvm_group="" + # LVM include config #lvm_conf=1 # LVM issue_discards lvm_discard=1 +# LVM parameters +#lvm_args="" + # LUKS support luks=1 @@ -62,7 +77,7 @@ luks=1 # LUKS encrypted root ( device,partuuid,uuid,label ) luks_root="PARTUUID=b04395be-f467-458b-8630-9a429b487600" -# luks mapper name ( /dev/mapper/) +# luks mapper name ( /dev/mapper/ ) #luks_name="" # LUKS detached header @@ -74,5 +89,5 @@ luks_root="PARTUUID=b04395be-f467-458b-8630-9a429b487600" # LUKS allow_discards luks_discard=1 -# LUKS unlock options +# LUKS parameters #luks_args="" diff --git a/generate b/generate index 1f8b718..d0c0df1 100755 --- a/generate +++ b/generate @@ -95,7 +95,7 @@ parse_fstab() { elif [ "$dir" = / ]; then root="${root:-$fs}" root_type="${root_type:-$type}" - root_args="${root_args:-$opts}" + root_opts="${root_opts:-$opts}" fi done < /etc/fstab } @@ -164,9 +164,6 @@ install_luks() { # workaround for luks2 install -s -m755 /usr/lib/libgcc_s.so.1 -t "${wrkdir}/usr/lib" || msg panic "failed to install LUKS libraries" - # block discard support - [ "$luks_discard" = 1 ] && luks_args="--allow-discards $luks_args" - # copy luks header [ -f "$luks_header" ] && { install -m400 "$luks_header" "${wrkdir}/root/luks_header" || msg panic "failed to copy LUKS header" @@ -285,20 +282,9 @@ install_library() { install_files() { msg info "installing files" - # FIXME eof broken + # initialize config - cat << EOF > "${wrkdir}/config" -debug="$debug" -root="$root" -root_type="$root_type" -root_args="$root_args" -devmgr="$devmgr" -#drivers="$drivers" -lvm="$lvm" -luks="$luks" -luks_root="$luks_root" -luks_args="$luks_args" -EOF + sed -e "/^#/d" -e "/^$/d" ./config > "${wrkdir}/config" # needed for devmgr cat << EOF > "${wrkdir}/etc/group" @@ -353,7 +339,7 @@ check_currentdir . ./config || msg panic "failed to source config" -[ "$debug" = 1 ] && { +[ "$shell_debug" = 1 ] && { # debug shell commands set -x # don't remove anything @@ -365,7 +351,7 @@ moddir="/lib/modules" create_structure create_symlinks -[ "$fstab" = 1 ] && parse_fstab +[ "$fstab" = 1 ] && [ -f /etc/fstab ] && parse_fstab #parse_crypttab install_requirements @@ -384,8 +370,8 @@ case "$devmgr" in *) msg panic "devmgr option broken" ;; esac -[ "$luks" = 1 ] && install_luks -[ "$lvm" = 1 ] && install_lvm +[ "$luks" = 1 ] && [ -x "$(command -v cryptsetup)" ] && install_luks +[ "$lvm" = 1 ] && [ -x "$(command -v lvm)" ] && install_lvm install_files create_initramfs diff --git a/init b/init index be81f03..35ebb14 100644 --- a/init +++ b/init @@ -4,12 +4,43 @@ panic() { printf "panic >> %s\n" "$1" + # TODO fix job control sh -l } -# parse_cmdline() { -# TODO parse /proc/cmdline -#} +parse_cmdline() { + # store cmdline in variable + read -r cmdline < /proc/cmdline || panic "failed to parse cmdline" + # turn variable into list + set -- $cmdline + + # parse line by line + for line in "$@"; do + # parse options + case "${line%%=*}" in + 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##*=}" ;; + shell.debug) shell_debug="${line##*=}" ;; + shell.break) shell_break="${line##*=}" ;; + # TODO implement + #lvm.discard) ;; + #lvm.conf) ;; + #luks_header) ;; + #luks_keyfile) ;; + esac + done +} mnt_pseudofs() { mount -t proc none /proc @@ -31,10 +62,8 @@ setup_mdev() { # trigger uevent for usb devices for device in /sys/bus/usb/devices/*; do - case ${device##*/} in - [0-9]*-[0-9]*) - printf add > "${device}/uevent" - ;; + case "${device##*/}" in + [0-9]*-[0-9]*) printf add > "${device}/uevent" ;; esac done @@ -70,22 +99,29 @@ findfs_sh() { sleep 0.5 [ "$increment" ] || increment=0 increment=$(( increment + 1 )) - [ "$increment" = 10 ] && panic "failed to lookup rootfs" + [ "$increment" = 10 ] && panic "failed to lookup partition" done printf "%s\n" "$device" } unlock_luks() { + [ "$luks_discard" = 1 ] && luks_args="--allow-discards $luks_args" cryptsetup $luks_args luksOpen $(findfs_sh "$luks_root") ${luks_name:-luks_root} || panic "failed to unlock luks container" } trigger_lvm() { - lvm vgchange --quiet --sysinit -a y > /dev/null + 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 } mnt_rootfs() { - mount ${root_type:+-t $root_type} ${root_args:+-o $root_args} $(findfs_sh "$root") /mnt/root || panic "failed to mount rootfs" + mount ${root_type:+-t $root_type} ${root_opts:+-o $root_opts} $(findfs_sh "$root") /mnt/root || panic "failed to mount rootfs" } cleanup() { @@ -100,18 +136,18 @@ cleanup() { } boot_system() { - exec switch_root /mnt/root /sbin/init || panic "failed to boot system" + exec switch_root /mnt/root ${init:-/sbin/init} || panic "failed to boot system" } /sbin/busybox --install -s . /config || panic "failed to source config" -# debug shell commands -[ "$debug" = 1 ] && set -x - -#parse_cmdline mnt_pseudofs +parse_cmdline + +# debug mode +[ "$shell_debug" = 1 ] && set -x case "$devmgr" in mdev) setup_mdev ;; @@ -121,8 +157,9 @@ case "$devmgr" in esac # TODO handle situations when LUKS on LVM -[ "$luks" = 1 ] && unlock_luks -[ "$lvm" = 1 ] && trigger_lvm +[ "$luks" = 1 ] && [ -x "$(command -v cryptsetup)" ] && unlock_luks +[ "$lvm" = 1 ] && [ -x "$(command -v lvm)" ] && trigger_lvm mnt_rootfs +[ "$shell_break" = 1 ] && panic "dropping to shell" cleanup boot_system