init: code cleanup
This commit is contained in:
parent
654a787074
commit
1d8b2ca1e9
70
init
70
init
@ -1,8 +1,8 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
#
|
#
|
||||||
# tiny init
|
# Tiny init
|
||||||
#
|
#
|
||||||
# false positive
|
# https://www.shellcheck.net/wiki/SC2154
|
||||||
# shellcheck disable=2154
|
# shellcheck disable=2154
|
||||||
|
|
||||||
print()
|
print()
|
||||||
@ -13,8 +13,10 @@ print()
|
|||||||
panic()
|
panic()
|
||||||
{
|
{
|
||||||
print "${1:-unexpected error occurred}" \
|
print "${1:-unexpected error occurred}" \
|
||||||
"\033[1;31m!!\033[m" >&2; sh
|
"\033[1;31m!!\033[m"
|
||||||
}
|
|
||||||
|
sh
|
||||||
|
} >&2
|
||||||
|
|
||||||
resolve_device()
|
resolve_device()
|
||||||
{
|
{
|
||||||
@ -28,12 +30,16 @@ resolve_device()
|
|||||||
*) return 0 ;;
|
*) return 0 ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# prevent race condition
|
# Race condition may occur if device manager is not yet initialized device.
|
||||||
while [ ! -b "$device" ]; do sleep 1
|
# To fix this, we simply waiting until device is available. If device
|
||||||
[ "$((count += 1))" = "${rootdelay:=30}" ] && {
|
# didn't appear in specified time, we panic.
|
||||||
|
while [ ! -b "$device" ]; do
|
||||||
|
if [ "$((count += 1))" = "${rootdelay:=30}" ]; then
|
||||||
panic "failed to lookup partition"
|
panic "failed to lookup partition"
|
||||||
break
|
break
|
||||||
}
|
else
|
||||||
|
sleep 1
|
||||||
|
fi
|
||||||
done || :
|
done || :
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,8 +47,9 @@ run_hook()
|
|||||||
{
|
{
|
||||||
type="$1"
|
type="$1"
|
||||||
|
|
||||||
# run hooks if any
|
# Run hooks if any exist.
|
||||||
# false positive
|
#
|
||||||
|
# https://www.shellcheck.net/wiki/SC1090
|
||||||
# shellcheck disable=1090
|
# shellcheck disable=1090
|
||||||
for hook in $hooks; do
|
for hook in $hooks; do
|
||||||
[ -f "/usr/share/tinyramfs/hooks/${hook}/${hook}.${type}" ] || continue
|
[ -f "/usr/share/tinyramfs/hooks/${hook}/${hook}.${type}" ] || continue
|
||||||
@ -52,7 +59,7 @@ run_hook()
|
|||||||
|
|
||||||
prepare_environment()
|
prepare_environment()
|
||||||
{
|
{
|
||||||
# false positive
|
# https://www.shellcheck.net/wiki/SC1091
|
||||||
# shellcheck disable=1091
|
# shellcheck disable=1091
|
||||||
. /etc/tinyramfs/config
|
. /etc/tinyramfs/config
|
||||||
|
|
||||||
@ -92,13 +99,11 @@ mount_root()
|
|||||||
|
|
||||||
resolve_device "$root"
|
resolve_device "$root"
|
||||||
|
|
||||||
set -- \
|
# https://www.shellcheck.net/wiki/SC2086
|
||||||
"${rorw:--o ro}${root_opts:+,$root_opts}" \
|
# shellcheck disable=2086
|
||||||
"${root_type:+-t $root_type}" "$device" "/mnt/root"
|
mount \
|
||||||
|
${rorw:--o ro}${root_opts:+,$root_opts} ${root_type:+-t $root_type} \
|
||||||
# word splitting is safe by design
|
-- "$device" /mnt/root || panic "failed to mount root"
|
||||||
# shellcheck disable=2068
|
|
||||||
mount $@ || panic "failed to mount root"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
boot_system()
|
boot_system()
|
||||||
@ -109,19 +114,26 @@ boot_system()
|
|||||||
mount -o move "$dir" "/mnt/root/${dir}"
|
mount -o move "$dir" "/mnt/root/${dir}"
|
||||||
done
|
done
|
||||||
|
|
||||||
set -- "/mnt/root" "${init:-/sbin/init}" "$init_args"
|
# POSIX 'exec' has no '-c' flag to execute command with empty environment.
|
||||||
|
# Using 'env -i' instead to prevent leaking exported variables.
|
||||||
# POSIX exec has no -c flag to execute command with empty environment
|
#
|
||||||
# use 'env -i' to prevent leaking exported variables
|
# Some implementations of 'switch_root' doesn't conform to POSIX utility
|
||||||
# word splitting is safe by design
|
# guidelines and doesn't support '--'. This means that we can't guarantee
|
||||||
# shellcheck disable=2068
|
# safety of init_args.
|
||||||
exec env -i TERM=linux PATH=/bin:/sbin:/usr/bin:/usr/sbin \
|
# shellcheck disable=2086
|
||||||
switch_root $@ || panic "failed to boot system"
|
exec \
|
||||||
|
env -i TERM=linux PATH=/bin:/sbin:/usr/bin:/usr/sbin \
|
||||||
|
switch_root /mnt/root "${init-/sbin/init}" $init_args ||
|
||||||
|
panic "failed to boot system"
|
||||||
}
|
}
|
||||||
|
|
||||||
# enable exit on error and disable globbing
|
# Exit if command fails and disable globbing.
|
||||||
# trap EXIT signal
|
set -ef
|
||||||
set -ef; trap panic EXIT
|
|
||||||
|
# Run emergency shell if init unexpectedly exiting due to error.
|
||||||
|
trap panic EXIT
|
||||||
|
|
||||||
|
# TODO display fancy colored status info
|
||||||
|
|
||||||
prepare_environment
|
prepare_environment
|
||||||
parse_cmdline
|
parse_cmdline
|
||||||
|
Loading…
Reference in New Issue
Block a user