From 1287f2996b6c7a95b4b67e4ad22029ca000d7da4 Mon Sep 17 00:00:00 2001 From: illiliti Date: Tue, 2 Jun 2020 14:26:42 +0300 Subject: [PATCH] cleanup --- device-helper | 4 +--- init | 30 ++++++++++++++---------------- tinyramfs | 37 ++++++++++++++++--------------------- 3 files changed, 31 insertions(+), 40 deletions(-) diff --git a/device-helper b/device-helper index b0ea005..a4e1381 100755 --- a/device-helper +++ b/device-helper @@ -19,8 +19,6 @@ create_symlink() { [ -b "/dev/${dev_name=${DEVPATH##*/}}" ] || exit 1 - exec > /dev/null 2>&1 - # prevent race condition while ! blkid "/dev/${dev_name}"; do sleep 1; done @@ -34,4 +32,4 @@ create_symlink() mkdir -p /dev/mapper ln -sf "../${dev_name}" "/dev/mapper/${dm_name:?}" } -} +} > /dev/null 2>&1 diff --git a/init b/init index 9597718..2bf4292 100755 --- a/init +++ b/init @@ -19,7 +19,7 @@ panic() "\033[1;31m!!\033[m" >&2; sh } -findfs() +resolve_device() { count=0; device= @@ -31,7 +31,7 @@ findfs() esac # prevent race condition - while [ ! -e "$device" ]; do sleep 1 + while [ ! -b "$device" ]; do sleep 1 [ "$(( count += 1 ))" != 30 ] || { panic "failed to lookup partition" break @@ -52,10 +52,7 @@ prepare_environment() mount -t tmpfs -o nosuid,nodev,mode=0755 run /run mount -t devtmpfs -o nosuid,noexec,mode=0755 dev /dev - mkdir -p \ - /run/cryptsetup \ - /run/lock \ - /run/lvm + mkdir -p /run/cryptsetup /run/lock /run/lvm ln -s /proc/self/fd /dev/fd ln -s fd/0 /dev/stdin @@ -76,7 +73,7 @@ parse_cmdline() rootfstype=*) root_type="${line##*=}" ;; rootflags=*) root_opts="${line##*=}" ;; ro | rw) rorw="-o $line" ;; - --) init_args="${cmdline##*--}"; break ;; + --*) init_args="${cmdline##*--}"; break ;; *=*) command export "$line" ;; *) command export "${line}=1" ;; esac 2> /dev/null || continue; done @@ -97,8 +94,6 @@ setup_devmgr() mdev -s mdev -df & devmgr_pid="$!" - [ "$monolith" = 1 ] && return 0 - find /sys/devices -name uevent | while read -r uevent; do @@ -125,11 +120,11 @@ unlock_luks() key=*) luks_key="-d ${opt##*=}" ;; esac; done - findfs "$luks_root" + resolve_device "$luks_root" set -- \ "$luks_key" "$luks_header" "$luks_discard" \ - "$device" "${luks_name:-luks-${device##*/}}" + "$device" "${luks_name:-crypt-${device##*/}}" cryptsetup open $@ || panic "failed to unlock LUKS" } @@ -165,7 +160,7 @@ mount_root() { [ "$break" = root ] && { print "break before mount_root()"; sh; } - findfs "$root" + resolve_device "$root" set -- \ "${rorw:--o ro}${root_opts:+,$root_opts}" \ @@ -185,12 +180,15 @@ boot_system() for dir in run dev sys proc; do mount -o move "$dir" "/mnt/root/${dir}" || mount --move "$dir" "/mnt/root/${dir}" - done + done 2> /dev/null - set -- \ - "/mnt/root" "${init:-/sbin/init}" "$init_args" + set -- "/mnt/root" "${init:-/sbin/init}" "$init_args" - exec switch_root $@ || panic "failed to boot system" + # use 'env -i' to prevent leaking exported variables + exec env -i \ + TERM=linux \ + PATH=/bin:/sbin:/usr/bin:/usr/sbin \ + switch_root $@ || panic "failed to boot system" } # int main() diff --git a/tinyramfs b/tinyramfs index ed96b7a..1663c0e 100755 --- a/tinyramfs +++ b/tinyramfs @@ -109,7 +109,8 @@ trap_helper() exit "$ret" } -populate_config() { +populate_config() +{ printf "%s\n" "$@" >> "${workdir}/etc/tinyramfs/config" } @@ -118,7 +119,7 @@ install_requirements() print "installing requirements" # install required binaries - for _binary in \[ sh ln kill mkdir \ + for _binary in \[ sh ln kill mkdir env \ blkid sleep mount printf \ switch_root "${filesdir}/device-helper" do @@ -139,7 +140,6 @@ install_requirements() populate_config \ "root='$root'" \ "devmgr='$devmgr'" \ - "monolith='$monolith'" \ "root_type='$root_type'" \ "root_opts='$root_opts'" } @@ -188,7 +188,9 @@ install_devmgr() # /sys/kernel/uevent_helper or /proc/sys/kernel/hotplug ;; mdev) - install_binary mdev + for _binary in mdev find; do + install_binary "$_binary" + done printf "%s\n" \ 'SUBSYSTEM=block;.* 0:0 660 @device-helper' \ @@ -196,8 +198,6 @@ install_devmgr() [ "$monolith" = 1 ] && return 0 - install_binary find - printf "%s\n" \ '$MODALIAS=.* 0:0 660 @modprobe "$MODALIAS"' \ >> "${workdir}/etc/mdev.conf" @@ -402,9 +402,7 @@ install_modules() [ "$modules" ] && { for _module in $modules; do install_module "$_module" - done - - populate_config "modules='$modules'" + done; populate_config "modules='$modules'" } install_binary modprobe @@ -422,10 +420,7 @@ install_binary() binary=$(command -v "$1") # check if binary exist and builtin - case "$binary" in - */*) - : no operation - ;; + case "$binary" in */*) ;; "") panic "$1 doesn't exist" ;; @@ -434,7 +429,10 @@ install_binary() # shellcheck disable=2086 { IFS=:; set -- $PATH; unset IFS; } - # try to discover external binary/script by checking PATH + # assume that `command -v` returned builtin command. + # this behavior depends on shell implementation. + # to be independented we simply iterating over PATH + # to find external alternative ( e.g kill => /bin/kill ) for _dir; do [ -x "${_dir}/${binary}" ] || ! continue @@ -456,11 +454,8 @@ install_binary() install -m755 "$binary" "${workdirbin}${binary##*/}" || panic strip "${workdirbin}${binary##*/}" > /dev/null 2>&1 || : - # skip static binaries/scripts - ldd "$binary" > /dev/null 2>&1 || return 0 - - # parse ldd output to find libraries paths - ldd "$binary" | + # install binary dependencies if any + ldd "$binary" 2> /dev/null | while read -r _library || [ "$_library" ]; do @@ -509,6 +504,8 @@ create_initramfs() fi \ > "$output" 2> /dev/null || panic "failed to generate initramfs image" + + print "done! check out $output" ) # int main() @@ -530,6 +527,4 @@ create_initramfs() install_devmgr install_requirements create_initramfs - - print "done! check out $output" }