From aa4812ad04e71a6bc46f9653cec074ab30f3c4f5 Mon Sep 17 00:00:00 2001 From: illiliti Date: Tue, 26 May 2020 16:44:20 +0300 Subject: [PATCH] cleanup --- init | 41 +++++++++++++++++++---------------------- tinyramfs | 32 ++++++++++++++++---------------- 2 files changed, 35 insertions(+), 38 deletions(-) diff --git a/init b/init index 74518dc..7f10bd6 100755 --- a/init +++ b/init @@ -32,11 +32,11 @@ findfs() # prevent race condition while [ ! -e "$device" ]; do sleep 1 - [ "$(( count += 1 ))" = 30 ] && { + [ "$(( count += 1 ))" != 30 ] || { panic "failed to lookup partition" break } - done || : + done } prepare_environment() @@ -73,10 +73,10 @@ parse_cmdline() for line in $cmdline; do case "$line" in debug | debug=1) set -x ;; - rootfstype=*) root_type="$line" ;; - rootflags=*) root_opts="$line" ;; - ro | rw) rorw=" -o $line" ;; - *.*) : no operation ;; + rootfstype=*) root_type="${line##*=}" ;; + rootflags=*) root_opts="${line##*=}" ;; + ro | rw) rorw="-o $line" ;; + --) init_args="${cmdline##*--}"; break ;; *=*) export "$line" ;; *) export "${line}=1" ;; esac; done @@ -99,8 +99,11 @@ setup_devmgr() [ "$monolith" = 1 ] && return 0 - set -- $(find /sys -name modalias -exec sort -u {} +) - modprobe -a "$@" || : + find /sys/devices -name uevent | + + while read -r uevent; do + printf add > "$uevent" + done ;; mdevd) mdevd & devmgr_pid="$!" @@ -125,9 +128,8 @@ unlock_luks() findfs "$luks_root" set -- \ - "$luks_key" "$luks_header" \ - "$luks_discard" "$device" \ - "${luks_name:-luks-${device##*/}}" + "$luks_key" "$luks_header" "$luks_discard" \ + "$device" "${luks_name:-luks-${device##*/}}" cryptsetup open $@ || panic "failed to unlock LUKS" } @@ -166,16 +168,15 @@ mount_root() findfs "$root" set -- \ - "${root_type:+-t $root_type}" \ "${rorw:--o ro}${root_opts:+,$root_opts}" \ - "$device" "/mnt/root" + "${root_type:+-t $root_type}" "$device" "/mnt/root" mount $@ || panic "failed to mount root" } -cleanup() +boot_system() { - [ "$break" = cleanup ] && { print "break before cleanup()"; sh; } + [ "$break" = boot ] && { print "break before boot_system()"; sh; } kill "$devmgr_pid" @@ -185,14 +186,11 @@ cleanup() mount -o move "$dir" "/mnt/root/${dir}" || mount --move "$dir" "/mnt/root/${dir}" done -} -boot_system() -{ - [ "$break" = boot ] && { print "break before boot_system()"; sh; } + set -- \ + "/mnt/root" "${init:-/sbin/init}" "$init_args" - set -- "/mnt/root" "${init:-/sbin/init}" - exec switch_root $@ 2> /dev/null || panic "failed to boot system" + exec switch_root $@ || panic "failed to boot system" } # int main() @@ -207,6 +205,5 @@ boot_system() [ "$lvm" = 1 ] && trigger_lvm mount_root - cleanup boot_system } diff --git a/tinyramfs b/tinyramfs index caa5e60..ed96b7a 100755 --- a/tinyramfs +++ b/tinyramfs @@ -72,11 +72,11 @@ prepare_environment() # false positive # shellcheck disable=1090 - for _file in $_config /etc/tinyramfs/config; do + for _file in "$_config" /etc/tinyramfs/config; do [ -f "$_file" ] && { . "$_file"; break; } done || panic "failed to source config" - for _dir in $_filesdir /usr/share/tinyramfs; do + for _dir in "$_filesdir" /usr/share/tinyramfs; do [ -d "$_dir" ] && { filesdir="$_dir"; break; } done || panic "failed to locate required files" @@ -94,6 +94,8 @@ prepare_environment() workdirlib="${workdir}/usr/lib/" modker="${moddir}/${kernel}" + # false positive + # shellcheck disable=2015 [ "$debug" = 1 ] && set -x || trap trap_helper EXIT INT } @@ -116,8 +118,8 @@ install_requirements() print "installing requirements" # install required binaries - for _binary in \[ sh ln mkdir blkid \ - sleep mount printf setsid \ + for _binary in \[ sh ln kill mkdir \ + blkid sleep mount printf \ switch_root "${filesdir}/device-helper" do install_binary "$_binary" @@ -178,15 +180,15 @@ install_devmgr() { print "installing device manager" + # false positive + # shellcheck disable=2016 case "$devmgr" in none) # TODO implement mode without device manager using deprecated # /sys/kernel/uevent_helper or /proc/sys/kernel/hotplug ;; mdev) - for _binary in kill mdev; do - install_binary "$_binary" - done + install_binary mdev printf "%s\n" \ 'SUBSYSTEM=block;.* 0:0 660 @device-helper' \ @@ -194,16 +196,14 @@ install_devmgr() [ "$monolith" = 1 ] && return 0 - for _binary in find sort; do - install_binary "$_binary" - done + install_binary find printf "%s\n" \ '$MODALIAS=.* 0:0 660 @modprobe "$MODALIAS"' \ >> "${workdir}/etc/mdev.conf" ;; mdevd) - for _binary in kill mdevd mdevd-coldplug; do + for _binary in mdevd mdevd-coldplug; do install_binary "$_binary" done @@ -320,7 +320,7 @@ install_module() [ -e "${workdir}${module}" ] && continue install -Dm644 "$module" "${workdir}${module}" || panic - done || : + done } install_hostonly_modules() @@ -332,7 +332,7 @@ install_hostonly_modules() while read -r _module || [ "$_module" ]; do install_module "$_module" - done || : + done # install LVM modules [ "$lvm" = 1 ] && @@ -354,7 +354,7 @@ install_hostonly_modules() if [ "$root_type" ]; then install_module "$root_type" else - while read -r _ _dir _type _ || [ "$_dir" ]; do + while read -r _ _dir _type _; do [ "$_dir" = / ] || continue install_module "${root_type=$_type}"; break @@ -387,7 +387,7 @@ install_all_modules() _module="${_module%%.*}" install_module "$_module" - done || : + done } install_modules() @@ -471,7 +471,7 @@ install_binary() _library="${_library% *}" install_library "$_library" - done || : + done } install_library()