redesign code style and fix some issues
This commit is contained in:
parent
0e8ed8c2c9
commit
9645b8c3c4
216
tinyramfs
216
tinyramfs
@ -1,19 +1,13 @@
|
|||||||
#!/bin/sh -ef
|
#!/bin/sh -ef
|
||||||
#
|
#
|
||||||
# tiny initramfs
|
# tiny initramfs
|
||||||
|
#
|
||||||
|
# false positive
|
||||||
|
# shellcheck disable=2154
|
||||||
|
|
||||||
msg()
|
# TODO add some colors ?
|
||||||
{
|
panic() { printf "panic >> %s\n" "$1" >&2; exit 1; }
|
||||||
case "$1" in
|
info() { printf "info >> %s\n" "$1"; }
|
||||||
info)
|
|
||||||
printf "info >> %s\n" "$2"
|
|
||||||
;;
|
|
||||||
panic)
|
|
||||||
printf "panic >> %s\n" "$2" >&2
|
|
||||||
exit 1
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
}
|
|
||||||
|
|
||||||
usage()
|
usage()
|
||||||
{
|
{
|
||||||
@ -32,8 +26,7 @@ EOF
|
|||||||
|
|
||||||
parse_args()
|
parse_args()
|
||||||
{
|
{
|
||||||
while [ "$1" ]; do
|
while [ "$1" ]; do case "$1" in
|
||||||
case "$1" in
|
|
||||||
-o | --output)
|
-o | --output)
|
||||||
_output="${2:?}"
|
_output="${2:?}"
|
||||||
shift 2
|
shift 2
|
||||||
@ -71,30 +64,24 @@ parse_args()
|
|||||||
usage
|
usage
|
||||||
exit 1
|
exit 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac; done
|
||||||
done
|
|
||||||
}
|
}
|
||||||
|
|
||||||
prepare_environment()
|
prepare_environment()
|
||||||
{
|
{
|
||||||
msg info "preparing environment"
|
info "preparing environment"
|
||||||
|
|
||||||
for _file in $_config /etc/tinyramfs/config ./config; do
|
for _file in $_config /etc/tinyramfs/config ./config; do
|
||||||
[ -f "$_file" ] &&
|
# false positive
|
||||||
{
|
# shellcheck disable=1090
|
||||||
. "$_file"
|
[ -f "$_file" ] && { . "$_file"; break; }
|
||||||
break
|
done || panic "failed to source config"
|
||||||
}
|
|
||||||
done || msg panic "failed to source config"
|
|
||||||
|
|
||||||
for _dir in $_filesdir /usr/share/tinyramfs ./usr/share/tinyramfs; do
|
for _dir in $_filesdir /usr/share/tinyramfs ./usr/share/tinyramfs; do
|
||||||
[ -d "$_dir" ] &&
|
[ -d "$_dir" ] && { filesdir="$_dir"; break; }
|
||||||
{
|
done || panic "failed to locate required files"
|
||||||
filesdir="$_dir"
|
|
||||||
break
|
|
||||||
}
|
|
||||||
done || msg panic "failed to locate required files"
|
|
||||||
|
|
||||||
|
# general variables
|
||||||
debug="${_debug:-${debug:-0}}"
|
debug="${_debug:-${debug:-0}}"
|
||||||
force="${_force:-${force:-0}}"
|
force="${_force:-${force:-0}}"
|
||||||
moddir="${_moddir:-${moddir:-/lib/modules}}"
|
moddir="${_moddir:-${moddir:-/lib/modules}}"
|
||||||
@ -102,8 +89,9 @@ prepare_environment()
|
|||||||
output="${_output:-${output:-/tmp/initramfs-${kernel}}}"
|
output="${_output:-${output:-/tmp/initramfs-${kernel}}}"
|
||||||
|
|
||||||
mkdir -p "${workdir=${XDG_CACHE_HOME:-${TMPDIR:-/tmp}}/initramfs.$$}" ||
|
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/"
|
workdirbin="${workdir}/usr/bin/"
|
||||||
workdirlib="${workdir}/usr/lib/"
|
workdirlib="${workdir}/usr/lib/"
|
||||||
modker="${moddir}/${kernel}"
|
modker="${moddir}/${kernel}"
|
||||||
@ -112,22 +100,17 @@ prepare_environment()
|
|||||||
|
|
||||||
remove_workdir()
|
remove_workdir()
|
||||||
{
|
{
|
||||||
msg info "removing working directory"
|
info "removing working directory"
|
||||||
|
|
||||||
rm -rf "$workdir"
|
rm -rf "$workdir"
|
||||||
}
|
}
|
||||||
|
|
||||||
install_requirements()
|
install_requirements()
|
||||||
{
|
{
|
||||||
msg info "installing requirements"
|
info "installing requirements"
|
||||||
|
|
||||||
# install user specified binaries if any
|
# install user specified and required binaries
|
||||||
for _binary in $binaries; do
|
for _binary in $binaries \[ sh sleep mount printf setsid switch_root; do
|
||||||
install_binary "$_binary"
|
|
||||||
done
|
|
||||||
|
|
||||||
# install required binaries
|
|
||||||
for _binary in \[ sh sleep mount printf setsid switch_root; do
|
|
||||||
install_binary "$_binary"
|
install_binary "$_binary"
|
||||||
done
|
done
|
||||||
|
|
||||||
@ -136,31 +119,33 @@ install_requirements()
|
|||||||
|
|
||||||
# copy config
|
# copy config
|
||||||
printf "%s\n" \
|
printf "%s\n" \
|
||||||
monolith="$monolith" \
|
|
||||||
root="$root" \
|
root="$root" \
|
||||||
root_type="$root_type" \
|
root_type="$root_type" \
|
||||||
root_opts="$root_opts" \
|
root_opts="$root_opts" \
|
||||||
devmgr="$devmgr" \
|
devmgr="$devmgr" \
|
||||||
lvm="$lvm" \
|
monolith="$monolith" \
|
||||||
lvm_opts="$lvm_opts" \
|
>> "${workdir}/etc/config"
|
||||||
luks="$luks" \
|
|
||||||
luks_root="$luks_root" \
|
|
||||||
luks_opts="$luks_opts" \
|
|
||||||
> "${workdir}/etc/config"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
create_structure()
|
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 \
|
||||||
mkdir -p "${workdir}/${_dir}"
|
"${workdir}/etc" \
|
||||||
done
|
"${workdir}/dev" \
|
||||||
|
"${workdir}/sys" \
|
||||||
|
"${workdir}/tmp" \
|
||||||
|
"${workdir}/proc" \
|
||||||
|
"${workdir}/root" \
|
||||||
|
"${workdir}/usr/lib" \
|
||||||
|
"${workdir}/usr/bin" \
|
||||||
|
"${workdir}/mnt/root"
|
||||||
}
|
}
|
||||||
|
|
||||||
create_symlinks()
|
create_symlinks()
|
||||||
{
|
{
|
||||||
msg info "creating symlinks"
|
info "creating symlinks"
|
||||||
|
|
||||||
ln -s usr/lib "${workdir}/lib"
|
ln -s usr/lib "${workdir}/lib"
|
||||||
ln -s usr/lib "${workdir}/lib64"
|
ln -s usr/lib "${workdir}/lib64"
|
||||||
@ -172,7 +157,24 @@ create_symlinks()
|
|||||||
|
|
||||||
install_devmgr()
|
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
|
# TODO investigate booting without device manager
|
||||||
case "$devmgr" in
|
case "$devmgr" in
|
||||||
@ -182,8 +184,7 @@ install_devmgr()
|
|||||||
done
|
done
|
||||||
|
|
||||||
# exclusively handle requirement
|
# exclusively handle requirement
|
||||||
[ "$luks" = 1 ] || [ "$lvm" = 1 ] &&
|
[ "$luks" = 1 ] || [ "$lvm" = 1 ] && install_binary dmsetup
|
||||||
install_binary dmsetup
|
|
||||||
|
|
||||||
for _binary in /usr/lib/udev/ata_id /usr/lib/udev/scsi_id; do
|
for _binary in /usr/lib/udev/ata_id /usr/lib/udev/scsi_id; do
|
||||||
install -Dm755 "$_binary" "${workdir}${_binary}"
|
install -Dm755 "$_binary" "${workdir}${_binary}"
|
||||||
@ -194,48 +195,47 @@ install_devmgr()
|
|||||||
install -Dm644 "$_file" "${workdir}${_file}"
|
install -Dm644 "$_file" "${workdir}${_file}"
|
||||||
done; set -f
|
done; set -f
|
||||||
;;
|
;;
|
||||||
mdev | mdevd)
|
mdev)
|
||||||
[ "$devmgr" = mdev ] && install_binary mdev
|
install_binary mdev
|
||||||
[ "$devmgr" = mdevd ] &&
|
install_devmgr_helper
|
||||||
|
;;
|
||||||
|
mdevd)
|
||||||
for _binary in mdevd mdevd-coldplug; do
|
for _binary in mdevd mdevd-coldplug; do
|
||||||
install_binary "$_binary"
|
install_binary "$_binary"
|
||||||
done
|
done
|
||||||
|
|
||||||
for _binary in ln kill mkdir blkid "${filesdir}/device-helper"; do
|
install_devmgr_helper
|
||||||
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"
|
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
install_lvm()
|
install_lvm()
|
||||||
{
|
{
|
||||||
msg info "installing LVM"
|
info "installing LVM"
|
||||||
|
|
||||||
for _binary in lvchange vgchange; do
|
for _binary in lvchange vgchange; do
|
||||||
install_binary "$_binary"
|
install_binary "$_binary"
|
||||||
done
|
done
|
||||||
|
|
||||||
{ IFS=,; set -- $lvm_opts; IFS="$OLD_IFS"; }
|
# copy config
|
||||||
|
printf "%s\n" \
|
||||||
for opt; do
|
lvm="$lvm" \
|
||||||
case "$opt" in
|
lvm_opts="$lvm_opts" \
|
||||||
config | config=1)
|
>> "${workdir}/etc/config"
|
||||||
install -Dm644 /etc/lvm/lvm.conf "${workdir}/etc/lvm/lvm.conf"
|
|
||||||
return
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
mkdir -p "${workdir}/etc/lvm"
|
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" \
|
printf "%s\n" \
|
||||||
'devices {' \
|
'devices {' \
|
||||||
'write_cache_state = 0' \
|
'write_cache_state = 0' \
|
||||||
@ -252,19 +252,26 @@ install_lvm()
|
|||||||
|
|
||||||
install_luks()
|
install_luks()
|
||||||
{
|
{
|
||||||
msg info "installing LUKS"
|
info "installing LUKS"
|
||||||
|
|
||||||
install_binary cryptsetup
|
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
|
# avoid libgcc_s.so.1 missing error
|
||||||
# see https://bugs.archlinux.org/task/56771
|
# see https://bugs.archlinux.org/task/56771
|
||||||
[ -e /usr/lib/libgcc_s.so.1 ] &&
|
[ -e /usr/lib/libgcc_s.so.1 ] && install_library /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"; }
|
{ IFS=,; set -- $luks_opts; IFS="$OLD_IFS"; }
|
||||||
|
|
||||||
for opt; do
|
for opt; do case "${opt%%=*}" in
|
||||||
case "${opt%%=*}" in
|
|
||||||
header)
|
header)
|
||||||
install -m400 "${opt##*=}" "${workdir}/root/header"
|
install -m400 "${opt##*=}" "${workdir}/root/header"
|
||||||
luks_opts=$(printf "%s" "$luks_opts" | sed "s|${opt##*=}|/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"
|
install -m400 "${opt##*=}" "${workdir}/root/key"
|
||||||
luks_opts=$(printf "%s" "$luks_opts" | sed "s|${opt##*=}|/root/key|")
|
luks_opts=$(printf "%s" "$luks_opts" | sed "s|${opt##*=}|/root/key|")
|
||||||
;;
|
;;
|
||||||
esac
|
esac; done
|
||||||
done
|
|
||||||
}
|
}
|
||||||
|
|
||||||
install_module()
|
install_module()
|
||||||
@ -303,7 +309,7 @@ install_module()
|
|||||||
|
|
||||||
install_hostonly_modules()
|
install_hostonly_modules()
|
||||||
{
|
{
|
||||||
msg info "installing hostonly modules"
|
info "installing hostonly modules"
|
||||||
|
|
||||||
# perform autodetection of modules via /sys
|
# perform autodetection of modules via /sys
|
||||||
find /sys -name modalias -exec sort -u {} + |
|
find /sys -name modalias -exec sort -u {} + |
|
||||||
@ -328,13 +334,12 @@ 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 _ _ _ || [ "$root_type" ]; do
|
while read -r _ _dir _type _ _ _ || [ "$_dir" ]; do
|
||||||
[ "$_dir" = / ] &&
|
[ "$_dir" = / ] && {
|
||||||
{
|
|
||||||
install_module "${root_type=$_type}"
|
install_module "${root_type=$_type}"
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
done < /proc/mounts || msg panic "failed to install root partition module"
|
done < /proc/mounts || panic "failed to install root partition module"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# install user specified modules if any
|
# install user specified modules if any
|
||||||
@ -345,7 +350,7 @@ install_hostonly_modules()
|
|||||||
|
|
||||||
install_all_modules()
|
install_all_modules()
|
||||||
{
|
{
|
||||||
msg info "installing all modules"
|
info "installing all modules"
|
||||||
|
|
||||||
find \
|
find \
|
||||||
"${modker}/kernel/arch" \
|
"${modker}/kernel/arch" \
|
||||||
@ -381,18 +386,19 @@ install_binary()
|
|||||||
: no operation
|
: 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"; }
|
{ IFS=:; set -- $PATH; IFS="$OLD_IFS"; }
|
||||||
|
|
||||||
for _dir; do
|
for _dir; do
|
||||||
[ -x "${_dir}/${binary}" ] &&
|
[ -x "${_dir}/${binary}" ] && {
|
||||||
{
|
|
||||||
binary="${_dir}/${binary}"
|
binary="${_dir}/${binary}"
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
done || msg panic "couldn't find external $1 binary"
|
done || panic "couldn't find external $1 binary"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
@ -444,26 +450,26 @@ install_library()
|
|||||||
|
|
||||||
create_initramfs()
|
create_initramfs()
|
||||||
{
|
{
|
||||||
msg info "creating initramfs image"
|
info "creating initramfs image"
|
||||||
|
|
||||||
# TODO add uncompressed option
|
# TODO add uncompressed option
|
||||||
|
|
||||||
# check if image already exist
|
# check if image already exist
|
||||||
[ "$force" != 1 ] && [ -e "$output" ] &&
|
[ "$force" != 1 ] && [ -e "$output" ] &&
|
||||||
msg panic "initramfs image already exist"
|
panic "initramfs image already exist"
|
||||||
|
|
||||||
(
|
(
|
||||||
cd "$workdir"
|
cd "$workdir"
|
||||||
find . | cpio -oH newc | ${compress:-gzip -9}
|
find . | cpio -oH newc | ${compress:-gzip -9}
|
||||||
|
|
||||||
) > "$output" 2> /dev/null ||
|
) > "$output" 2> /dev/null ||
|
||||||
msg panic "failed to generate initramfs image"
|
panic "failed to generate initramfs image"
|
||||||
}
|
}
|
||||||
|
|
||||||
# int main()
|
# int main()
|
||||||
{
|
{
|
||||||
# check root
|
# check root
|
||||||
[ "$(id -u)" = 0 ] || msg panic "must be run as root"
|
[ "$(id -u)" = 0 ] || panic "must be run as root"
|
||||||
|
|
||||||
parse_args "$@"
|
parse_args "$@"
|
||||||
prepare_environment
|
prepare_environment
|
||||||
@ -474,8 +480,8 @@ create_initramfs()
|
|||||||
trap '
|
trap '
|
||||||
ret="$?"
|
ret="$?"
|
||||||
trap - EXIT INT
|
trap - EXIT INT
|
||||||
[ "$debug" = 1 ] || remove_workdir
|
[ "$debug" != 1 ] && remove_workdir
|
||||||
[ "$ret" = 0 ] || msg panic "something went wrong"
|
[ "$ret" != 0 ] && panic "something went wrong"
|
||||||
' EXIT INT
|
' EXIT INT
|
||||||
|
|
||||||
create_structure
|
create_structure
|
||||||
@ -485,8 +491,8 @@ create_initramfs()
|
|||||||
[ "$luks" = 1 ] && install_luks
|
[ "$luks" = 1 ] && install_luks
|
||||||
|
|
||||||
# check monolithic kernel
|
# check monolithic kernel
|
||||||
[ "$monolith" != 1 ] && [ -d "$moddir" ] &&
|
[ "$monolith" != 1 ] && [ -d "$moddir" ] && {
|
||||||
{
|
|
||||||
# check hostonly mode
|
# check hostonly mode
|
||||||
if [ "$hostonly" = 1 ]; then
|
if [ "$hostonly" = 1 ]; then
|
||||||
install_hostonly_modules
|
install_hostonly_modules
|
||||||
@ -509,5 +515,5 @@ create_initramfs()
|
|||||||
install_requirements
|
install_requirements
|
||||||
create_initramfs
|
create_initramfs
|
||||||
|
|
||||||
msg info "done! check out - $output"
|
info "done! check out - $output"
|
||||||
}
|
}
|
||||||
|
@ -17,8 +17,7 @@ create_symlink()
|
|||||||
{
|
{
|
||||||
[ "$MDEV" ] || exit 1
|
[ "$MDEV" ] || exit 1
|
||||||
|
|
||||||
for line in $(blkid "$MDEV"); do
|
for line in $(blkid "$MDEV"); do case "${line%%=*}" in
|
||||||
case "${line%%=*}" in
|
|
||||||
UUID)
|
UUID)
|
||||||
dir="/dev/disk/by-uuid/"
|
dir="/dev/disk/by-uuid/"
|
||||||
create_symlink "${line##*=}"
|
create_symlink "${line##*=}"
|
||||||
@ -31,11 +30,9 @@ create_symlink()
|
|||||||
dir="/dev/disk/by-partuuid/"
|
dir="/dev/disk/by-partuuid/"
|
||||||
create_symlink "${line##*=}"
|
create_symlink "${line##*=}"
|
||||||
;;
|
;;
|
||||||
esac
|
esac; done
|
||||||
done
|
|
||||||
|
|
||||||
[ -e "/sys/block/${MDEV}/dm/name" ] &&
|
[ -e "/sys/block/${MDEV}/dm/name" ] && {
|
||||||
{
|
|
||||||
mkdir -p /dev/mapper 2> /dev/null
|
mkdir -p /dev/mapper 2> /dev/null
|
||||||
read -r name < "/sys/block/${MDEV}/dm/name"
|
read -r name < "/sys/block/${MDEV}/dm/name"
|
||||||
ln -s "/dev/${MDEV}" "/dev/mapper/${name}" 2> /dev/null
|
ln -s "/dev/${MDEV}" "/dev/mapper/${name}" 2> /dev/null
|
||||||
|
@ -1,11 +1,18 @@
|
|||||||
#!/bin/sh -ef
|
#!/bin/sh -ef
|
||||||
#
|
#
|
||||||
# tiny init
|
# 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
|
# see https://busybox.net/FAQ.html#job_control
|
||||||
setsid sh -c "exec sh <> /dev/${console:-console} 2>&1" || sh
|
setsid sh -c "exec sh <> /dev/${console:-console} 2>&1" || sh
|
||||||
}
|
}
|
||||||
@ -31,9 +38,8 @@ findfs()
|
|||||||
|
|
||||||
# avoid race condition
|
# avoid race condition
|
||||||
while [ ! -e "$device" ]; do
|
while [ ! -e "$device" ]; do
|
||||||
value=$(( value + 1 ))
|
|
||||||
[ "$value" = 15 ] && panic "failed to lookup partition"
|
[ "$value" = 15 ] && panic "failed to lookup partition"
|
||||||
sleep 1
|
value=$(( value + 1 )); sleep 1
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,12 +74,9 @@ prepare_environment()
|
|||||||
|
|
||||||
parse_cmdline()
|
parse_cmdline()
|
||||||
{
|
{
|
||||||
[ "$break" = cmdline ] && panic "break before parse cmdline"
|
|
||||||
|
|
||||||
read -r cmdline < /proc/cmdline
|
read -r cmdline < /proc/cmdline
|
||||||
|
|
||||||
for line in $cmdline; do
|
for line in $cmdline; do case "$line" in
|
||||||
case "$line" in
|
|
||||||
debug | debug=1)
|
debug | debug=1)
|
||||||
set -x
|
set -x
|
||||||
;;
|
;;
|
||||||
@ -90,13 +93,12 @@ parse_cmdline()
|
|||||||
*)
|
*)
|
||||||
export "${line}=1"
|
export "${line}=1"
|
||||||
;;
|
;;
|
||||||
esac
|
esac; done
|
||||||
done
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setup_devmgr()
|
setup_devmgr()
|
||||||
{
|
{
|
||||||
[ "$break" = devmgr ] && panic "break before setup device manager"
|
[ "$break" = devmgr ] && info "break before setup device manager"
|
||||||
|
|
||||||
case "$devmgr" in
|
case "$devmgr" in
|
||||||
udev)
|
udev)
|
||||||
@ -108,8 +110,7 @@ setup_devmgr()
|
|||||||
mdev)
|
mdev)
|
||||||
mdev -df 2> /dev/null & mdev_pid="$!"
|
mdev -df 2> /dev/null & mdev_pid="$!"
|
||||||
|
|
||||||
[ "$monolith" != 1 ] &&
|
[ "$monolith" != 1 ] && {
|
||||||
{
|
|
||||||
set -- $(find /sys -name modalias -type f -exec sort -u {} +)
|
set -- $(find /sys -name modalias -type f -exec sort -u {} +)
|
||||||
modprobe -a "$@" 2> /dev/null
|
modprobe -a "$@" 2> /dev/null
|
||||||
}
|
}
|
||||||
@ -123,12 +124,11 @@ setup_devmgr()
|
|||||||
|
|
||||||
unlock_luks()
|
unlock_luks()
|
||||||
{
|
{
|
||||||
[ "$break" = luks ] && panic "break before unlock LUKS"
|
[ "$break" = luks ] && info "break before unlock LUKS"
|
||||||
|
|
||||||
{ IFS=,; set -- $luks_opts; IFS="$OLD_IFS"; }
|
{ IFS=,; set -- $luks_opts; IFS="$OLD_IFS"; }
|
||||||
|
|
||||||
for opt; do
|
for opt; do case "$opt" in
|
||||||
case "$opt" in
|
|
||||||
discard | discard=1)
|
discard | discard=1)
|
||||||
luks_discard="--allow-discards"
|
luks_discard="--allow-discards"
|
||||||
;;
|
;;
|
||||||
@ -141,23 +141,28 @@ unlock_luks()
|
|||||||
key=*)
|
key=*)
|
||||||
luks_key="-d ${opt##*=}"
|
luks_key="-d ${opt##*=}"
|
||||||
;;
|
;;
|
||||||
esac
|
esac; done
|
||||||
done
|
|
||||||
|
|
||||||
findfs "$luks_root"
|
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"
|
cryptsetup open $@ || panic "failed to unlock LUKS"
|
||||||
}
|
}
|
||||||
|
|
||||||
trigger_lvm()
|
trigger_lvm()
|
||||||
{
|
{
|
||||||
[ "$break" = lvm ] && panic "break before trigger LVM"
|
[ "$break" = lvm ] && info "break before trigger LVM"
|
||||||
|
|
||||||
{ IFS=,; set -- $lvm_opts; IFS="$OLD_IFS"; }
|
{ IFS=,; set -- $lvm_opts; IFS="$OLD_IFS"; }
|
||||||
|
|
||||||
for opt; do
|
for opt; do case "$opt" in
|
||||||
case "$opt" in
|
|
||||||
discard | discard=1)
|
discard | discard=1)
|
||||||
lvm_discard="--config=devices{issue_discards=1}"
|
lvm_discard="--config=devices{issue_discards=1}"
|
||||||
;;
|
;;
|
||||||
@ -173,8 +178,7 @@ trigger_lvm()
|
|||||||
tag=*)
|
tag=*)
|
||||||
lvm_tag="@${opt##*=}"
|
lvm_tag="@${opt##*=}"
|
||||||
;;
|
;;
|
||||||
esac
|
esac; done
|
||||||
done
|
|
||||||
|
|
||||||
set -- "--sysinit" "-qq" "-ay" "$lvm_discard"
|
set -- "--sysinit" "-qq" "-ay" "$lvm_discard"
|
||||||
|
|
||||||
@ -191,17 +195,21 @@ trigger_lvm()
|
|||||||
|
|
||||||
mount_root()
|
mount_root()
|
||||||
{
|
{
|
||||||
[ "$break" = root ] && panic "break before mount root"
|
[ "$break" = root ] && info "break before mount root"
|
||||||
|
|
||||||
findfs "$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"
|
mount $@ || panic "failed to mount root"
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanup()
|
cleanup()
|
||||||
{
|
{
|
||||||
[ "$break" = cleanup ] && panic "break before cleanup"
|
[ "$break" = cleanup ] && info "break before cleanup"
|
||||||
|
|
||||||
case "$devmgr" in
|
case "$devmgr" in
|
||||||
udev) udevadm control -e ;;
|
udev) udevadm control -e ;;
|
||||||
@ -209,7 +217,7 @@ cleanup()
|
|||||||
mdevd) kill "$mdevd_pid" ;;
|
mdevd) kill "$mdevd_pid" ;;
|
||||||
esac
|
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
|
# see https://github.com/karelzak/util-linux/issues/997
|
||||||
for dir in dev sys proc; do
|
for dir in dev sys proc; do
|
||||||
mount -o move "$dir" "/mnt/root/${dir}" || mount --move "$dir" "/mnt/root/${dir}"
|
mount -o move "$dir" "/mnt/root/${dir}" || mount --move "$dir" "/mnt/root/${dir}"
|
||||||
@ -218,7 +226,7 @@ cleanup()
|
|||||||
|
|
||||||
boot_system()
|
boot_system()
|
||||||
{
|
{
|
||||||
[ "$break" = boot ] && panic "break before boot system"
|
[ "$break" = boot ] && info "break before boot system"
|
||||||
|
|
||||||
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"
|
||||||
@ -230,6 +238,8 @@ boot_system()
|
|||||||
parse_cmdline
|
parse_cmdline
|
||||||
setup_devmgr
|
setup_devmgr
|
||||||
|
|
||||||
|
# trigger lvm twice to handle both LUKS on LVM and LVM on LUKS
|
||||||
|
[ "$lvm" = 1 ] && trigger_lvm
|
||||||
[ "$luks" = 1 ] && unlock_luks
|
[ "$luks" = 1 ] && unlock_luks
|
||||||
[ "$lvm" = 1 ] && trigger_lvm
|
[ "$lvm" = 1 ] && trigger_lvm
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user