redesign code style and fix some issues

This commit is contained in:
illiliti 2020-04-15 19:50:45 +03:00
parent 0e8ed8c2c9
commit 9645b8c3c4
3 changed files with 256 additions and 243 deletions

216
tinyramfs
View File

@ -1,19 +1,13 @@
#!/bin/sh -ef
#
# tiny initramfs
#
# false positive
# shellcheck disable=2154
msg()
{
case "$1" in
info)
printf "info >> %s\n" "$2"
;;
panic)
printf "panic >> %s\n" "$2" >&2
exit 1
;;
esac
}
# TODO add some colors ?
panic() { printf "panic >> %s\n" "$1" >&2; exit 1; }
info() { printf "info >> %s\n" "$1"; }
usage()
{
@ -32,8 +26,7 @@ EOF
parse_args()
{
while [ "$1" ]; do
case "$1" in
while [ "$1" ]; do case "$1" in
-o | --output)
_output="${2:?}"
shift 2
@ -71,30 +64,24 @@ parse_args()
usage
exit 1
;;
esac
done
esac; done
}
prepare_environment()
{
msg info "preparing environment"
info "preparing environment"
for _file in $_config /etc/tinyramfs/config ./config; do
[ -f "$_file" ] &&
{
. "$_file"
break
}
done || msg panic "failed to source config"
# false positive
# shellcheck disable=1090
[ -f "$_file" ] && { . "$_file"; break; }
done || panic "failed to source config"
for _dir in $_filesdir /usr/share/tinyramfs ./usr/share/tinyramfs; do
[ -d "$_dir" ] &&
{
filesdir="$_dir"
break
}
done || msg panic "failed to locate required files"
[ -d "$_dir" ] && { filesdir="$_dir"; break; }
done || panic "failed to locate required files"
# general variables
debug="${_debug:-${debug:-0}}"
force="${_force:-${force:-0}}"
moddir="${_moddir:-${moddir:-/lib/modules}}"
@ -102,8 +89,9 @@ prepare_environment()
output="${_output:-${output:-/tmp/initramfs-${kernel}}}"
mkdir -p "${workdir=${XDG_CACHE_HOME:-${TMPDIR:-/tmp}}/initramfs.$$}" ||
msg panic "failed to create working directory"
panic "failed to create working directory"
# helpers variables
workdirbin="${workdir}/usr/bin/"
workdirlib="${workdir}/usr/lib/"
modker="${moddir}/${kernel}"
@ -112,22 +100,17 @@ prepare_environment()
remove_workdir()
{
msg info "removing working directory"
info "removing working directory"
rm -rf "$workdir"
}
install_requirements()
{
msg info "installing requirements"
info "installing requirements"
# install user specified binaries if any
for _binary in $binaries; do
install_binary "$_binary"
done
# install required binaries
for _binary in \[ sh sleep mount printf setsid switch_root; do
# install user specified and required binaries
for _binary in $binaries \[ sh sleep mount printf setsid switch_root; do
install_binary "$_binary"
done
@ -136,31 +119,33 @@ install_requirements()
# copy config
printf "%s\n" \
monolith="$monolith" \
root="$root" \
root_type="$root_type" \
root_opts="$root_opts" \
devmgr="$devmgr" \
lvm="$lvm" \
lvm_opts="$lvm_opts" \
luks="$luks" \
luks_root="$luks_root" \
luks_opts="$luks_opts" \
> "${workdir}/etc/config"
monolith="$monolith" \
>> "${workdir}/etc/config"
}
create_structure()
{
msg info "creating directory structure"
info "creating directory structure"
for _dir in etc tmp dev sys proc root usr/lib usr/bin mnt/root; do
mkdir -p "${workdir}/${_dir}"
done
mkdir -p \
"${workdir}/etc" \
"${workdir}/dev" \
"${workdir}/sys" \
"${workdir}/tmp" \
"${workdir}/proc" \
"${workdir}/root" \
"${workdir}/usr/lib" \
"${workdir}/usr/bin" \
"${workdir}/mnt/root"
}
create_symlinks()
{
msg info "creating symlinks"
info "creating symlinks"
ln -s usr/lib "${workdir}/lib"
ln -s usr/lib "${workdir}/lib64"
@ -172,7 +157,24 @@ create_symlinks()
install_devmgr()
{
msg info "installing device manager"
info "installing device manager"
install_devmgr_helper()
{
for _binary in ln kill mkdir blkid "${filesdir}/device-helper"; do
install_binary "$_binary"
done
printf "%s\n" \
'SUBSYSTEM=block;.* 0:0 660 @device-helper' \
> "${workdir}/etc/mdev.conf"
# false positive
# shellcheck disable=2016
[ "$monolith" != 1 ] && printf "%s\n" \
'$MODALIAS=.* 0:0 660 @modprobe "$MODALIAS"' \
>> "${workdir}/etc/mdev.conf"
}
# TODO investigate booting without device manager
case "$devmgr" in
@ -182,8 +184,7 @@ install_devmgr()
done
# exclusively handle requirement
[ "$luks" = 1 ] || [ "$lvm" = 1 ] &&
install_binary dmsetup
[ "$luks" = 1 ] || [ "$lvm" = 1 ] && install_binary dmsetup
for _binary in /usr/lib/udev/ata_id /usr/lib/udev/scsi_id; do
install -Dm755 "$_binary" "${workdir}${_binary}"
@ -194,48 +195,47 @@ install_devmgr()
install -Dm644 "$_file" "${workdir}${_file}"
done; set -f
;;
mdev | mdevd)
[ "$devmgr" = mdev ] && install_binary mdev
[ "$devmgr" = mdevd ] &&
mdev)
install_binary mdev
install_devmgr_helper
;;
mdevd)
for _binary in mdevd mdevd-coldplug; do
install_binary "$_binary"
done
for _binary in ln kill mkdir blkid "${filesdir}/device-helper"; do
install_binary "$_binary"
done
printf "%s\n" \
'SUBSYSTEM=block;.* 0:0 660 @device-helper' \
> "${workdir}/etc/mdev.conf"
[ "$monolith" != 1 ] && printf "%s\n" \
'$MODALIAS=.* 0:0 660 @modprobe "$MODALIAS"' \
>> "${workdir}/etc/mdev.conf"
install_devmgr_helper
;;
esac
}
install_lvm()
{
msg info "installing LVM"
info "installing LVM"
for _binary in lvchange vgchange; do
install_binary "$_binary"
done
{ IFS=,; set -- $lvm_opts; IFS="$OLD_IFS"; }
for opt; do
case "$opt" in
config | config=1)
install -Dm644 /etc/lvm/lvm.conf "${workdir}/etc/lvm/lvm.conf"
return
;;
esac
done
# copy config
printf "%s\n" \
lvm="$lvm" \
lvm_opts="$lvm_opts" \
>> "${workdir}/etc/config"
mkdir -p "${workdir}/etc/lvm"
# word splitting is safe by design
# shellcheck disable=2086
{ IFS=,; set -- $lvm_opts; IFS="$OLD_IFS"; }
for opt; do case "$opt" in
config | config=1)
lvmconfig > "${workdir}/etc/lvm/lvm.conf"
return
;;
esac; done
printf "%s\n" \
'devices {' \
'write_cache_state = 0' \
@ -252,19 +252,26 @@ install_lvm()
install_luks()
{
msg info "installing LUKS"
info "installing LUKS"
install_binary cryptsetup
# copy config
printf "%s\n" \
luks="$luks" \
luks_root="$luks_root" \
luks_opts="$luks_opts" \
>> "${workdir}/etc/config"
# avoid libgcc_s.so.1 missing error
# see https://bugs.archlinux.org/task/56771
[ -e /usr/lib/libgcc_s.so.1 ] &&
install_library /usr/lib/libgcc_s.so.1
[ -e /usr/lib/libgcc_s.so.1 ] && install_library /usr/lib/libgcc_s.so.1
# word splitting is safe by design
# shellcheck disable=2086
{ IFS=,; set -- $luks_opts; IFS="$OLD_IFS"; }
for opt; do
case "${opt%%=*}" in
for opt; do case "${opt%%=*}" in
header)
install -m400 "${opt##*=}" "${workdir}/root/header"
luks_opts=$(printf "%s" "$luks_opts" | sed "s|${opt##*=}|/root/header|")
@ -273,8 +280,7 @@ install_luks()
install -m400 "${opt##*=}" "${workdir}/root/key"
luks_opts=$(printf "%s" "$luks_opts" | sed "s|${opt##*=}|/root/key|")
;;
esac
done
esac; done
}
install_module()
@ -303,7 +309,7 @@ install_module()
install_hostonly_modules()
{
msg info "installing hostonly modules"
info "installing hostonly modules"
# perform autodetection of modules via /sys
find /sys -name modalias -exec sort -u {} + |
@ -328,13 +334,12 @@ install_hostonly_modules()
if [ "$root_type" ]; then
install_module "$root_type"
else
while read -r _ _dir _type _ _ _ || [ "$root_type" ]; do
[ "$_dir" = / ] &&
{
while read -r _ _dir _type _ _ _ || [ "$_dir" ]; do
[ "$_dir" = / ] && {
install_module "${root_type=$_type}"
break
}
done < /proc/mounts || msg panic "failed to install root partition module"
done < /proc/mounts || panic "failed to install root partition module"
fi
# install user specified modules if any
@ -345,7 +350,7 @@ install_hostonly_modules()
install_all_modules()
{
msg info "installing all modules"
info "installing all modules"
find \
"${modker}/kernel/arch" \
@ -381,18 +386,19 @@ install_binary()
: no operation
;;
"")
msg panic "$1 doesn't exist"
panic "$1 doesn't exist"
;;
*)
# word splitting is safe by design
# shellcheck disable=2086
{ IFS=:; set -- $PATH; IFS="$OLD_IFS"; }
for _dir; do
[ -x "${_dir}/${binary}" ] &&
{
[ -x "${_dir}/${binary}" ] && {
binary="${_dir}/${binary}"
break
}
done || msg panic "couldn't find external $1 binary"
done || panic "couldn't find external $1 binary"
;;
esac
@ -444,26 +450,26 @@ install_library()
create_initramfs()
{
msg info "creating initramfs image"
info "creating initramfs image"
# TODO add uncompressed option
# check if image already exist
[ "$force" != 1 ] && [ -e "$output" ] &&
msg panic "initramfs image already exist"
panic "initramfs image already exist"
(
cd "$workdir"
find . | cpio -oH newc | ${compress:-gzip -9}
) > "$output" 2> /dev/null ||
msg panic "failed to generate initramfs image"
panic "failed to generate initramfs image"
}
# int main()
{
# check root
[ "$(id -u)" = 0 ] || msg panic "must be run as root"
[ "$(id -u)" = 0 ] || panic "must be run as root"
parse_args "$@"
prepare_environment
@ -474,8 +480,8 @@ create_initramfs()
trap '
ret="$?"
trap - EXIT INT
[ "$debug" = 1 ] || remove_workdir
[ "$ret" = 0 ] || msg panic "something went wrong"
[ "$debug" != 1 ] && remove_workdir
[ "$ret" != 0 ] && panic "something went wrong"
' EXIT INT
create_structure
@ -485,8 +491,8 @@ create_initramfs()
[ "$luks" = 1 ] && install_luks
# check monolithic kernel
[ "$monolith" != 1 ] && [ -d "$moddir" ] &&
{
[ "$monolith" != 1 ] && [ -d "$moddir" ] && {
# check hostonly mode
if [ "$hostonly" = 1 ]; then
install_hostonly_modules
@ -509,5 +515,5 @@ create_initramfs()
install_requirements
create_initramfs
msg info "done! check out - $output"
info "done! check out - $output"
}

View File

@ -17,8 +17,7 @@ create_symlink()
{
[ "$MDEV" ] || exit 1
for line in $(blkid "$MDEV"); do
case "${line%%=*}" in
for line in $(blkid "$MDEV"); do case "${line%%=*}" in
UUID)
dir="/dev/disk/by-uuid/"
create_symlink "${line##*=}"
@ -31,11 +30,9 @@ create_symlink()
dir="/dev/disk/by-partuuid/"
create_symlink "${line##*=}"
;;
esac
done
esac; done
[ -e "/sys/block/${MDEV}/dm/name" ] &&
{
[ -e "/sys/block/${MDEV}/dm/name" ] && {
mkdir -p /dev/mapper 2> /dev/null
read -r name < "/sys/block/${MDEV}/dm/name"
ln -s "/dev/${MDEV}" "/dev/mapper/${name}" 2> /dev/null

View File

@ -1,11 +1,18 @@
#!/bin/sh -ef
#
# tiny init
#
# word splitting is safe by design
# shellcheck disable=2068,2046,2086
#
# false positive
# shellcheck disable=2154,2163,1091
panic()
panic() { printf "panic >> %s\n" "$1" >&2; shell; }
info() { printf "info >> %s\n" "$1"; shell; }
shell()
{
printf "panic >> %s\n" "$1"
# see https://busybox.net/FAQ.html#job_control
setsid sh -c "exec sh <> /dev/${console:-console} 2>&1" || sh
}
@ -31,9 +38,8 @@ findfs()
# avoid race condition
while [ ! -e "$device" ]; do
value=$(( value + 1 ))
[ "$value" = 15 ] && panic "failed to lookup partition"
sleep 1
value=$(( value + 1 )); sleep 1
done
}
@ -68,12 +74,9 @@ prepare_environment()
parse_cmdline()
{
[ "$break" = cmdline ] && panic "break before parse cmdline"
read -r cmdline < /proc/cmdline
for line in $cmdline; do
case "$line" in
for line in $cmdline; do case "$line" in
debug | debug=1)
set -x
;;
@ -90,13 +93,12 @@ parse_cmdline()
*)
export "${line}=1"
;;
esac
done
esac; done
}
setup_devmgr()
{
[ "$break" = devmgr ] && panic "break before setup device manager"
[ "$break" = devmgr ] && info "break before setup device manager"
case "$devmgr" in
udev)
@ -108,8 +110,7 @@ setup_devmgr()
mdev)
mdev -df 2> /dev/null & mdev_pid="$!"
[ "$monolith" != 1 ] &&
{
[ "$monolith" != 1 ] && {
set -- $(find /sys -name modalias -type f -exec sort -u {} +)
modprobe -a "$@" 2> /dev/null
}
@ -123,12 +124,11 @@ setup_devmgr()
unlock_luks()
{
[ "$break" = luks ] && panic "break before unlock LUKS"
[ "$break" = luks ] && info "break before unlock LUKS"
{ IFS=,; set -- $luks_opts; IFS="$OLD_IFS"; }
for opt; do
case "$opt" in
for opt; do case "$opt" in
discard | discard=1)
luks_discard="--allow-discards"
;;
@ -141,23 +141,28 @@ unlock_luks()
key=*)
luks_key="-d ${opt##*=}"
;;
esac
done
esac; done
findfs "$luks_root"
set -- "--disable-locks" "$luks_key" "$luks_header" "$luks_discard" "$device" "${luks_name:-luks-${device##*/}}"
set -- \
"--disable-locks" \
"$luks_key" \
"$luks_header" \
"$luks_discard" \
"$device" \
"${luks_name:-luks-${device##*/}}"
cryptsetup open $@ || panic "failed to unlock LUKS"
}
trigger_lvm()
{
[ "$break" = lvm ] && panic "break before trigger LVM"
[ "$break" = lvm ] && info "break before trigger LVM"
{ IFS=,; set -- $lvm_opts; IFS="$OLD_IFS"; }
for opt; do
case "$opt" in
for opt; do case "$opt" in
discard | discard=1)
lvm_discard="--config=devices{issue_discards=1}"
;;
@ -173,8 +178,7 @@ trigger_lvm()
tag=*)
lvm_tag="@${opt##*=}"
;;
esac
done
esac; done
set -- "--sysinit" "-qq" "-ay" "$lvm_discard"
@ -191,17 +195,21 @@ trigger_lvm()
mount_root()
{
[ "$break" = root ] && panic "break before mount root"
[ "$break" = root ] && info "break before mount root"
findfs "$root"
set -- "${root_type:+-t $root_type}" "${rorw:-o ro}${root_opts:+,$root_opts}" "$device" "/mnt/root"
set -- \
"${root_type:+-t $root_type}" \
"${rorw:--o ro}${root_opts:+,$root_opts}" \
"$device" "/mnt/root"
mount $@ || panic "failed to mount root"
}
cleanup()
{
[ "$break" = cleanup ] && panic "break before cleanup"
[ "$break" = cleanup ] && info "break before cleanup"
case "$devmgr" in
udev) udevadm control -e ;;
@ -209,7 +217,7 @@ cleanup()
mdevd) kill "$mdevd_pid" ;;
esac
# temporary workaround until util-linux mount implements 'mount -o move'
# temporary workaround until util-linux release a new version
# see https://github.com/karelzak/util-linux/issues/997
for dir in dev sys proc; do
mount -o move "$dir" "/mnt/root/${dir}" || mount --move "$dir" "/mnt/root/${dir}"
@ -218,7 +226,7 @@ cleanup()
boot_system()
{
[ "$break" = boot ] && panic "break before boot system"
[ "$break" = boot ] && info "break before boot system"
set -- "/mnt/root" "${init:-/sbin/init}"
exec switch_root $@ 2> /dev/null || panic "failed to boot system"
@ -230,6 +238,8 @@ boot_system()
parse_cmdline
setup_devmgr
# trigger lvm twice to handle both LUKS on LVM and LVM on LUKS
[ "$lvm" = 1 ] && trigger_lvm
[ "$luks" = 1 ] && unlock_luks
[ "$lvm" = 1 ] && trigger_lvm