improve code, drop setsid, move set -ef to main function
This commit is contained in:
parent
55008b4c98
commit
0eb35dca65
15
README.md
15
README.md
@ -20,7 +20,6 @@ Dependencies
|
|||||||
* `switch_root`
|
* `switch_root`
|
||||||
* `readlink`
|
* `readlink`
|
||||||
* `install`
|
* `install`
|
||||||
* `setsid`
|
|
||||||
* `mount`
|
* `mount`
|
||||||
* `blkid`
|
* `blkid`
|
||||||
* `cpio`
|
* `cpio`
|
||||||
@ -63,13 +62,13 @@ Usage
|
|||||||
|
|
||||||
```
|
```
|
||||||
usage: tinyramfs [option]
|
usage: tinyramfs [option]
|
||||||
-o, --output <file> set initramfs output path
|
-o, --output <file> set initramfs output path
|
||||||
-c, --config <file> set config file path
|
-c, --config <file> set config file path
|
||||||
-m, --moddir <dir> set modules directory
|
-m, --moddir <dir> set modules directory
|
||||||
-k, --kernel <ver> set kernel version
|
-k, --kernel <ver> set kernel version
|
||||||
-F, --files <dir> set files directory
|
-F, --files <dir> set files directory
|
||||||
-d, --debug enable debug mode
|
-d, --debug enable debug mode
|
||||||
-f, --force overwrite initramfs image
|
-f, --force overwrite initramfs image
|
||||||
```
|
```
|
||||||
|
|
||||||
Configuration
|
Configuration
|
||||||
|
@ -12,7 +12,7 @@ create_symlink()
|
|||||||
sym="${dir}/${sym}"
|
sym="${dir}/${sym}"
|
||||||
|
|
||||||
mkdir -p "$dir"
|
mkdir -p "$dir"
|
||||||
ln -s "/dev/${dev_name}" "$sym"
|
ln -s "../../${dev_name}" "$sym"
|
||||||
}
|
}
|
||||||
|
|
||||||
# int main()
|
# int main()
|
||||||
@ -21,7 +21,7 @@ create_symlink()
|
|||||||
|
|
||||||
exec > /dev/null 2>&1
|
exec > /dev/null 2>&1
|
||||||
|
|
||||||
# avoid race condition
|
# prevent race condition
|
||||||
while ! blkid "/dev/${dev_name}"; do sleep 1; done
|
while ! blkid "/dev/${dev_name}"; do sleep 1; done
|
||||||
|
|
||||||
for line in $(blkid "/dev/${dev_name}"); do case "${line%%=*}" in
|
for line in $(blkid "/dev/${dev_name}"); do case "${line%%=*}" in
|
||||||
@ -30,9 +30,8 @@ create_symlink()
|
|||||||
PARTUUID) create_symlink /dev/disk/by-partuuid "${line##*=}" ;;
|
PARTUUID) create_symlink /dev/disk/by-partuuid "${line##*=}" ;;
|
||||||
esac; done
|
esac; done
|
||||||
|
|
||||||
[ -e "/sys/block/${dev_name}/dm/name" ] && {
|
read -r dm_name < "/sys/block/${dev_name}/dm/name" && {
|
||||||
mkdir -p /dev/mapper
|
mkdir -p /dev/mapper
|
||||||
read -r dm_name < "/sys/block/${dev_name}/dm/name"
|
ln -sf "../${dev_name}" "/dev/mapper/${dm_name:?}"
|
||||||
ln -sf "/dev/${dev_name}" "/dev/mapper/${dm_name}"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
156
init
156
init
@ -16,15 +16,7 @@ print()
|
|||||||
panic()
|
panic()
|
||||||
{
|
{
|
||||||
print "${1:-unexpected error occurred}" \
|
print "${1:-unexpected error occurred}" \
|
||||||
"\033[1;31m!!\033[m" >&2
|
"\033[1;31m!!\033[m" >&2; sh
|
||||||
|
|
||||||
shell
|
|
||||||
}
|
|
||||||
|
|
||||||
shell()
|
|
||||||
{
|
|
||||||
# see https://busybox.net/FAQ.html#job_control
|
|
||||||
setsid sh -c "exec sh <> /dev/${console:-tty1} 2>&1" || sh
|
|
||||||
}
|
}
|
||||||
|
|
||||||
findfs()
|
findfs()
|
||||||
@ -32,25 +24,15 @@ findfs()
|
|||||||
count=0; device=
|
count=0; device=
|
||||||
|
|
||||||
case "${1%%=*}" in
|
case "${1%%=*}" in
|
||||||
/dev/*)
|
/dev/*) device="$1" ;;
|
||||||
device="$1"
|
UUID) device="/dev/disk/by-uuid/${1##*=}" ;;
|
||||||
;;
|
LABEL) device="/dev/disk/by-label/${1##*=}" ;;
|
||||||
UUID)
|
PARTUUID) device="/dev/disk/by-partuuid/${1##*=}" ;;
|
||||||
device="/dev/disk/by-uuid/${1##*=}"
|
|
||||||
;;
|
|
||||||
LABEL)
|
|
||||||
device="/dev/disk/by-label/${1##*=}"
|
|
||||||
;;
|
|
||||||
PARTUUID)
|
|
||||||
device="/dev/disk/by-partuuid/${1##*=}"
|
|
||||||
;;
|
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# avoid race condition
|
# prevent race condition
|
||||||
while [ ! -e "$device" ]; do
|
while [ ! -e "$device" ]; do sleep 1
|
||||||
sleep 1; : $(( count += 1 ))
|
[ "$(( count += 1 ))" = 30 ] && {
|
||||||
|
|
||||||
[ "$count" = 30 ] && {
|
|
||||||
panic "failed to lookup partition"
|
panic "failed to lookup partition"
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -62,17 +44,8 @@ prepare_environment()
|
|||||||
. /etc/tinyramfs/config
|
. /etc/tinyramfs/config
|
||||||
|
|
||||||
export \
|
export \
|
||||||
LANG=C \
|
PATH=/bin TERM=linux SHELL=/bin/sh \
|
||||||
PS1="# " \
|
LANG=C LC_ALL=C PS1="# " HOME=/root \
|
||||||
LC_ALL=C \
|
|
||||||
TERM=linux \
|
|
||||||
HOME=/root \
|
|
||||||
SHELL=/bin/sh \
|
|
||||||
OLD_IFS="$IFS" \
|
|
||||||
PATH=/bin:/sbin:/usr/bin:/usr/sbin
|
|
||||||
|
|
||||||
# fix for ubase mount
|
|
||||||
: > /etc/fstab
|
|
||||||
|
|
||||||
mount -t proc -o nosuid,noexec,nodev proc /proc
|
mount -t proc -o nosuid,noexec,nodev proc /proc
|
||||||
mount -t sysfs -o nosuid,noexec,nodev sys /sys
|
mount -t sysfs -o nosuid,noexec,nodev sys /sys
|
||||||
@ -91,89 +64,62 @@ prepare_environment()
|
|||||||
|
|
||||||
trap panic EXIT
|
trap panic EXIT
|
||||||
|
|
||||||
[ "$modules" ] || return 0
|
[ ! "$modules" ] || modprobe -a "$modules"
|
||||||
|
|
||||||
modprobe -a "$modules"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
parse_cmdline()
|
parse_cmdline()
|
||||||
{
|
{
|
||||||
read -r cmdline < /proc/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)
|
debug | debug=1) set -x ;;
|
||||||
set -x
|
rootfstype=*) root_type="$line" ;;
|
||||||
;;
|
rootflags=*) root_opts="$line" ;;
|
||||||
rootfstype=*)
|
ro | rw) rorw=" -o $line" ;;
|
||||||
root_type="$line"
|
*.*) : no operation ;;
|
||||||
;;
|
*=*) export "$line" ;;
|
||||||
rootflags=*)
|
*) export "${line}=1" ;;
|
||||||
root_opts="$line"
|
|
||||||
;;
|
|
||||||
ro | rw)
|
|
||||||
rorw="-o $line"
|
|
||||||
;;
|
|
||||||
*.*)
|
|
||||||
# TODO implement backward compatibilty with dracut, mkinitcpio, etc
|
|
||||||
: no operation
|
|
||||||
;;
|
|
||||||
*=*)
|
|
||||||
export "$line" || :
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
export "${line}=1" || :
|
|
||||||
;;
|
|
||||||
esac; done
|
esac; done
|
||||||
}
|
}
|
||||||
|
|
||||||
setup_devmgr()
|
setup_devmgr()
|
||||||
{
|
{
|
||||||
[ "$break" = devmgr ] && { print "break before setup_devmgr()"; shell; }
|
[ "$break" = devmgr ] && { print "break before setup_devmgr()"; sh; }
|
||||||
|
|
||||||
case "$devmgr" in
|
case "$devmgr" in
|
||||||
udev)
|
udev)
|
||||||
udevd -dN never
|
udevd -N never & devmgr_pid="$!"
|
||||||
udevadm trigger -c add -t subsystems
|
udevadm trigger -c add -t subsystems
|
||||||
udevadm trigger -c add -t devices
|
udevadm trigger -c add -t devices
|
||||||
udevadm settle
|
udevadm settle
|
||||||
;;
|
;;
|
||||||
mdev)
|
mdev)
|
||||||
mdev -df 2> /dev/null & mdev_pid="$!"
|
|
||||||
mdev -s
|
mdev -s
|
||||||
|
mdev -df & devmgr_pid="$!"
|
||||||
|
|
||||||
[ "$monolith" = 1 ] && return 0
|
[ "$monolith" = 1 ] && return 0
|
||||||
|
|
||||||
# TODO maybe replace sort using while-read loop
|
set -- $(find /sys -name modalias -exec sort -u {} +)
|
||||||
# while-read can cause slowdown, so why i'm unsure
|
modprobe -a "$@" || :
|
||||||
set -- $(find /sys -name modalias -type f -exec sort -u {} +)
|
|
||||||
modprobe -a "$@" 2> /dev/null || :
|
|
||||||
;;
|
;;
|
||||||
mdevd)
|
mdevd)
|
||||||
mdevd 2> /dev/null & mdevd_pid="$!"
|
mdevd & devmgr_pid="$!"
|
||||||
mdevd-coldplug
|
mdevd-coldplug
|
||||||
;;
|
;;
|
||||||
esac
|
esac 2> /dev/null
|
||||||
}
|
}
|
||||||
|
|
||||||
unlock_luks()
|
unlock_luks()
|
||||||
{
|
{
|
||||||
[ "$break" = luks ] && { print "break before unlock_luks()"; shell; }
|
[ "$break" = luks ] && { print "break before unlock_luks()"; sh; }
|
||||||
|
|
||||||
{ IFS=,; set -- $luks_opts; IFS="$OLD_IFS"; }
|
{ IFS=,; set -- $luks_opts; unset IFS; }
|
||||||
|
|
||||||
for opt; do case "$opt" in
|
for opt; do case "$opt" in
|
||||||
discard | discard=1)
|
discard | discard=1) luks_discard="--allow-discards" ;;
|
||||||
luks_discard="--allow-discards"
|
header=*) luks_header="--${opt}" ;;
|
||||||
;;
|
name=*) luks_name="${opt##*=}" ;;
|
||||||
header=*)
|
key=*) luks_key="-d ${opt##*=}" ;;
|
||||||
luks_header="--${opt}"
|
|
||||||
;;
|
|
||||||
name=*)
|
|
||||||
luks_name="${opt##*=}"
|
|
||||||
;;
|
|
||||||
key=*)
|
|
||||||
luks_key="-d ${opt##*=}"
|
|
||||||
;;
|
|
||||||
esac; done
|
esac; done
|
||||||
|
|
||||||
findfs "$luks_root"
|
findfs "$luks_root"
|
||||||
@ -188,26 +134,16 @@ unlock_luks()
|
|||||||
|
|
||||||
trigger_lvm()
|
trigger_lvm()
|
||||||
{
|
{
|
||||||
[ "$break" = lvm ] && { print "break before trigger_lvm()"; shell; }
|
[ "$break" = lvm ] && { print "break before trigger_lvm()"; sh; }
|
||||||
|
|
||||||
{ IFS=,; set -- $lvm_opts; IFS="$OLD_IFS"; }
|
{ IFS=,; set -- $lvm_opts; unset IFS; }
|
||||||
|
|
||||||
for opt; do case "$opt" in
|
for opt; do case "$opt" in
|
||||||
discard | discard=1)
|
discard | discard=1) lvm_discard="--config=devices{issue_discards=1}" ;;
|
||||||
lvm_discard="--config=devices{issue_discards=1}"
|
config=0) : > /etc/lvm/lvm.conf ;;
|
||||||
;;
|
group=*) lvm_group="${opt##*=}" ;;
|
||||||
config=0)
|
name=*) lvm_name="/${opt##*=}" ;;
|
||||||
: > /etc/lvm/lvm.conf
|
tag=*) lvm_tag="@${opt##*=}" ;;
|
||||||
;;
|
|
||||||
group=*)
|
|
||||||
lvm_group="${opt##*=}"
|
|
||||||
;;
|
|
||||||
name=*)
|
|
||||||
lvm_name="/${opt##*=}"
|
|
||||||
;;
|
|
||||||
tag=*)
|
|
||||||
lvm_tag="@${opt##*=}"
|
|
||||||
;;
|
|
||||||
esac; done
|
esac; done
|
||||||
|
|
||||||
set -- "--sysinit" "-qq" "-aay" "$lvm_discard"
|
set -- "--sysinit" "-qq" "-aay" "$lvm_discard"
|
||||||
@ -225,7 +161,7 @@ trigger_lvm()
|
|||||||
|
|
||||||
mount_root()
|
mount_root()
|
||||||
{
|
{
|
||||||
[ "$break" = root ] && { print "break before mount_root()"; shell; }
|
[ "$break" = root ] && { print "break before mount_root()"; sh; }
|
||||||
|
|
||||||
findfs "$root"
|
findfs "$root"
|
||||||
|
|
||||||
@ -239,17 +175,13 @@ mount_root()
|
|||||||
|
|
||||||
cleanup()
|
cleanup()
|
||||||
{
|
{
|
||||||
[ "$break" = cleanup ] && { print "break before cleanup()"; shell; }
|
[ "$break" = cleanup ] && { print "break before cleanup()"; sh; }
|
||||||
|
|
||||||
case "$devmgr" in
|
kill "$devmgr_pid"
|
||||||
udev) udevadm control -e ;;
|
|
||||||
mdev) kill "$mdev_pid" ;;
|
|
||||||
mdevd) kill "$mdevd_pid" ;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
# temporary workaround until util-linux release a new version
|
# temporary workaround until util-linux release a new version
|
||||||
# see https://github.com/karelzak/util-linux/issues/997
|
# see https://github.com/karelzak/util-linux/issues/997
|
||||||
for dir in /run /dev /sys /proc; do
|
for dir in run dev sys proc; do
|
||||||
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
|
||||||
@ -257,7 +189,7 @@ cleanup()
|
|||||||
|
|
||||||
boot_system()
|
boot_system()
|
||||||
{
|
{
|
||||||
[ "$break" = boot ] && { print "break before boot_system()"; shell; }
|
[ "$break" = boot ] && { print "break before boot_system()"; sh; }
|
||||||
|
|
||||||
set -- "/mnt/root" "${init:-/sbin/init}"
|
set -- "/mnt/root" "${init:-/sbin/init}"
|
||||||
exec switch_root $@ 2> /dev/null || panic "failed to boot system"
|
exec switch_root $@ 2> /dev/null || panic "failed to boot system"
|
||||||
|
37
tinyramfs
37
tinyramfs
@ -1,4 +1,4 @@
|
|||||||
#!/bin/sh -ef
|
#!/bin/sh
|
||||||
#
|
#
|
||||||
# tiny initramfs
|
# tiny initramfs
|
||||||
#
|
#
|
||||||
@ -13,9 +13,7 @@ print()
|
|||||||
panic()
|
panic()
|
||||||
{
|
{
|
||||||
print "${1:-unexpected error occurred}" \
|
print "${1:-unexpected error occurred}" \
|
||||||
"\033[1;31m!!\033[m" >&2
|
"\033[1;31m!!\033[m" >&2; exit 1
|
||||||
|
|
||||||
exit 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
usage()
|
usage()
|
||||||
@ -95,7 +93,6 @@ prepare_environment()
|
|||||||
workdirbin="${workdir}/usr/bin/"
|
workdirbin="${workdir}/usr/bin/"
|
||||||
workdirlib="${workdir}/usr/lib/"
|
workdirlib="${workdir}/usr/lib/"
|
||||||
modker="${moddir}/${kernel}"
|
modker="${moddir}/${kernel}"
|
||||||
OLD_IFS="$IFS"
|
|
||||||
|
|
||||||
[ "$debug" = 1 ] && set -x || trap trap_helper EXIT INT
|
[ "$debug" = 1 ] && set -x || trap trap_helper EXIT INT
|
||||||
}
|
}
|
||||||
@ -134,6 +131,9 @@ install_requirements()
|
|||||||
# install init
|
# install init
|
||||||
install -m755 "${filesdir}/init" "${workdir}/init"
|
install -m755 "${filesdir}/init" "${workdir}/init"
|
||||||
|
|
||||||
|
# fix ubase mount issue
|
||||||
|
: > "${workdir}/etc/fstab"
|
||||||
|
|
||||||
populate_config \
|
populate_config \
|
||||||
"root='$root'" \
|
"root='$root'" \
|
||||||
"devmgr='$devmgr'" \
|
"devmgr='$devmgr'" \
|
||||||
@ -180,7 +180,7 @@ install_devmgr()
|
|||||||
|
|
||||||
case "$devmgr" in
|
case "$devmgr" in
|
||||||
none)
|
none)
|
||||||
# TODO implement device-manager-less mode 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)
|
||||||
@ -194,6 +194,10 @@ install_devmgr()
|
|||||||
|
|
||||||
[ "$monolith" = 1 ] && return 0
|
[ "$monolith" = 1 ] && return 0
|
||||||
|
|
||||||
|
for _binary in find sort; do
|
||||||
|
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"
|
||||||
@ -253,7 +257,7 @@ install_lvm()
|
|||||||
|
|
||||||
# word splitting is safe by design
|
# word splitting is safe by design
|
||||||
# shellcheck disable=2086
|
# shellcheck disable=2086
|
||||||
{ IFS=,; set -- $lvm_opts; IFS="$OLD_IFS"; }
|
{ IFS=,; set -- $lvm_opts; unset IFS; }
|
||||||
|
|
||||||
for opt; do case "$opt" in
|
for opt; do case "$opt" in
|
||||||
config | config=1) embed_lvm_config=1 ;;
|
config | config=1) embed_lvm_config=1 ;;
|
||||||
@ -282,15 +286,12 @@ install_luks()
|
|||||||
|
|
||||||
# word splitting is safe by design
|
# word splitting is safe by design
|
||||||
# shellcheck disable=2086
|
# shellcheck disable=2086
|
||||||
{ IFS=,; set -- $luks_opts; IFS="$OLD_IFS"; }
|
{ IFS=,; set -- $luks_opts; unset IFS; }
|
||||||
|
|
||||||
for opt; do case "${opt%%=*}" in
|
for opt; do case "${opt%%=*}" in
|
||||||
key | header)
|
key | header)
|
||||||
install -m400 "${opt##*=}" \
|
install -m400 "${opt##*=}" "${workdir}/root/${opt%%=*}" || panic
|
||||||
"${workdir}/root/${opt%%=*}" || panic
|
luks_opts=$(printf "%s" "$luks_opts" | sed "s|${opt##*=}|/root/${opt%%=*}|")
|
||||||
|
|
||||||
luks_opts=$(printf "%s" "$luks_opts" |
|
|
||||||
sed "s|${opt##*=}|/root/${opt%%=*}|")
|
|
||||||
;;
|
;;
|
||||||
esac; done
|
esac; done
|
||||||
|
|
||||||
@ -406,11 +407,6 @@ install_modules()
|
|||||||
populate_config "modules='$modules'"
|
populate_config "modules='$modules'"
|
||||||
}
|
}
|
||||||
|
|
||||||
[ "$devmgr" = mdev ] &&
|
|
||||||
for _binary in find sort; do
|
|
||||||
install_binary "$_binary"
|
|
||||||
done
|
|
||||||
|
|
||||||
install_binary modprobe
|
install_binary modprobe
|
||||||
|
|
||||||
install -m644 \
|
install -m644 \
|
||||||
@ -436,7 +432,7 @@ install_binary()
|
|||||||
*)
|
*)
|
||||||
# word splitting is safe by design
|
# word splitting is safe by design
|
||||||
# shellcheck disable=2086
|
# shellcheck disable=2086
|
||||||
{ IFS=:; set -- $PATH; IFS="$OLD_IFS"; }
|
{ IFS=:; set -- $PATH; unset IFS; }
|
||||||
|
|
||||||
# try to discover external binary/script by checking PATH
|
# try to discover external binary/script by checking PATH
|
||||||
for _dir; do
|
for _dir; do
|
||||||
@ -519,6 +515,9 @@ create_initramfs()
|
|||||||
{
|
{
|
||||||
[ "$(id -u)" = 0 ] || panic "must be run as root"
|
[ "$(id -u)" = 0 ] || panic "must be run as root"
|
||||||
|
|
||||||
|
# enable exit on error and disable globbing
|
||||||
|
set -ef
|
||||||
|
|
||||||
parse_args "$@"
|
parse_args "$@"
|
||||||
prepare_environment
|
prepare_environment
|
||||||
create_structure
|
create_structure
|
||||||
|
Loading…
x
Reference in New Issue
Block a user