This commit is contained in:
illiliti 2020-05-26 16:44:20 +03:00
parent 91e1969ab2
commit aa4812ad04
2 changed files with 35 additions and 38 deletions

41
init
View File

@ -32,11 +32,11 @@ findfs()
# prevent race condition # prevent race condition
while [ ! -e "$device" ]; do sleep 1 while [ ! -e "$device" ]; do sleep 1
[ "$(( count += 1 ))" = 30 ] && { [ "$(( count += 1 ))" != 30 ] || {
panic "failed to lookup partition" panic "failed to lookup partition"
break break
} }
done || : done
} }
prepare_environment() prepare_environment()
@ -73,10 +73,10 @@ parse_cmdline()
for line in $cmdline; do case "$line" in for line in $cmdline; do case "$line" in
debug | debug=1) set -x ;; debug | debug=1) set -x ;;
rootfstype=*) root_type="$line" ;; rootfstype=*) root_type="${line##*=}" ;;
rootflags=*) root_opts="$line" ;; rootflags=*) root_opts="${line##*=}" ;;
ro | rw) rorw=" -o $line" ;; ro | rw) rorw="-o $line" ;;
*.*) : no operation ;; --) init_args="${cmdline##*--}"; break ;;
*=*) export "$line" ;; *=*) export "$line" ;;
*) export "${line}=1" ;; *) export "${line}=1" ;;
esac; done esac; done
@ -99,8 +99,11 @@ setup_devmgr()
[ "$monolith" = 1 ] && return 0 [ "$monolith" = 1 ] && return 0
set -- $(find /sys -name modalias -exec sort -u {} +) find /sys/devices -name uevent |
modprobe -a "$@" || :
while read -r uevent; do
printf add > "$uevent"
done
;; ;;
mdevd) mdevd)
mdevd & devmgr_pid="$!" mdevd & devmgr_pid="$!"
@ -125,9 +128,8 @@ unlock_luks()
findfs "$luks_root" findfs "$luks_root"
set -- \ set -- \
"$luks_key" "$luks_header" \ "$luks_key" "$luks_header" "$luks_discard" \
"$luks_discard" "$device" \ "$device" "${luks_name:-luks-${device##*/}}"
"${luks_name:-luks-${device##*/}}"
cryptsetup open $@ || panic "failed to unlock LUKS" cryptsetup open $@ || panic "failed to unlock LUKS"
} }
@ -166,16 +168,15 @@ mount_root()
findfs "$root" findfs "$root"
set -- \ set -- \
"${root_type:+-t $root_type}" \
"${rorw:--o ro}${root_opts:+,$root_opts}" \ "${rorw:--o ro}${root_opts:+,$root_opts}" \
"$device" "/mnt/root" "${root_type:+-t $root_type}" "$device" "/mnt/root"
mount $@ || panic "failed to mount 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" kill "$devmgr_pid"
@ -185,14 +186,11 @@ cleanup()
mount -o move "$dir" "/mnt/root/${dir}" || mount -o move "$dir" "/mnt/root/${dir}" ||
mount --move "$dir" "/mnt/root/${dir}" mount --move "$dir" "/mnt/root/${dir}"
done done
}
boot_system() set -- \
{ "/mnt/root" "${init:-/sbin/init}" "$init_args"
[ "$break" = boot ] && { print "break before boot_system()"; sh; }
set -- "/mnt/root" "${init:-/sbin/init}" exec switch_root $@ || panic "failed to boot system"
exec switch_root $@ 2> /dev/null || panic "failed to boot system"
} }
# int main() # int main()
@ -207,6 +205,5 @@ boot_system()
[ "$lvm" = 1 ] && trigger_lvm [ "$lvm" = 1 ] && trigger_lvm
mount_root mount_root
cleanup
boot_system boot_system
} }

View File

@ -72,11 +72,11 @@ prepare_environment()
# false positive # false positive
# shellcheck disable=1090 # shellcheck disable=1090
for _file in $_config /etc/tinyramfs/config; do for _file in "$_config" /etc/tinyramfs/config; do
[ -f "$_file" ] && { . "$_file"; break; } [ -f "$_file" ] && { . "$_file"; break; }
done || panic "failed to source config" 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; } [ -d "$_dir" ] && { filesdir="$_dir"; break; }
done || panic "failed to locate required files" done || panic "failed to locate required files"
@ -94,6 +94,8 @@ prepare_environment()
workdirlib="${workdir}/usr/lib/" workdirlib="${workdir}/usr/lib/"
modker="${moddir}/${kernel}" modker="${moddir}/${kernel}"
# false positive
# shellcheck disable=2015
[ "$debug" = 1 ] && set -x || trap trap_helper EXIT INT [ "$debug" = 1 ] && set -x || trap trap_helper EXIT INT
} }
@ -116,8 +118,8 @@ install_requirements()
print "installing requirements" print "installing requirements"
# install required binaries # install required binaries
for _binary in \[ sh ln mkdir blkid \ for _binary in \[ sh ln kill mkdir \
sleep mount printf setsid \ blkid sleep mount printf \
switch_root "${filesdir}/device-helper" switch_root "${filesdir}/device-helper"
do do
install_binary "$_binary" install_binary "$_binary"
@ -178,15 +180,15 @@ install_devmgr()
{ {
print "installing device manager" print "installing device manager"
# false positive
# shellcheck disable=2016
case "$devmgr" in case "$devmgr" in
none) none)
# TODO implement mode without device manager using deprecated # TODO implement mode without device manager using deprecated
# /sys/kernel/uevent_helper or /proc/sys/kernel/hotplug # /sys/kernel/uevent_helper or /proc/sys/kernel/hotplug
;; ;;
mdev) mdev)
for _binary in kill mdev; do install_binary mdev
install_binary "$_binary"
done
printf "%s\n" \ printf "%s\n" \
'SUBSYSTEM=block;.* 0:0 660 @device-helper' \ 'SUBSYSTEM=block;.* 0:0 660 @device-helper' \
@ -194,16 +196,14 @@ install_devmgr()
[ "$monolith" = 1 ] && return 0 [ "$monolith" = 1 ] && return 0
for _binary in find sort; do install_binary find
install_binary "$_binary"
done
printf "%s\n" \ printf "%s\n" \
'$MODALIAS=.* 0:0 660 @modprobe "$MODALIAS"' \ '$MODALIAS=.* 0:0 660 @modprobe "$MODALIAS"' \
>> "${workdir}/etc/mdev.conf" >> "${workdir}/etc/mdev.conf"
;; ;;
mdevd) mdevd)
for _binary in kill mdevd mdevd-coldplug; do for _binary in mdevd mdevd-coldplug; do
install_binary "$_binary" install_binary "$_binary"
done done
@ -320,7 +320,7 @@ install_module()
[ -e "${workdir}${module}" ] && continue [ -e "${workdir}${module}" ] && continue
install -Dm644 "$module" "${workdir}${module}" || panic install -Dm644 "$module" "${workdir}${module}" || panic
done || : done
} }
install_hostonly_modules() install_hostonly_modules()
@ -332,7 +332,7 @@ install_hostonly_modules()
while read -r _module || [ "$_module" ]; do while read -r _module || [ "$_module" ]; do
install_module "$_module" install_module "$_module"
done || : done
# install LVM modules # install LVM modules
[ "$lvm" = 1 ] && [ "$lvm" = 1 ] &&
@ -354,7 +354,7 @@ install_hostonly_modules()
if [ "$root_type" ]; then if [ "$root_type" ]; then
install_module "$root_type" install_module "$root_type"
else else
while read -r _ _dir _type _ || [ "$_dir" ]; do while read -r _ _dir _type _; do
[ "$_dir" = / ] || continue [ "$_dir" = / ] || continue
install_module "${root_type=$_type}"; break install_module "${root_type=$_type}"; break
@ -387,7 +387,7 @@ install_all_modules()
_module="${_module%%.*}" _module="${_module%%.*}"
install_module "$_module" install_module "$_module"
done || : done
} }
install_modules() install_modules()
@ -471,7 +471,7 @@ install_binary()
_library="${_library% *}" _library="${_library% *}"
install_library "$_library" install_library "$_library"
done || : done
} }
install_library() install_library()