implement parse_args, refactor

This commit is contained in:
illiliti 2020-02-22 20:46:57 +03:00
parent 5122f01e10
commit a519d29e19
2 changed files with 141 additions and 81 deletions

199
generate
View File

@ -2,8 +2,8 @@
# #
# tiny initramfs generation tool # tiny initramfs generation tool
# print message
msg() { msg() {
# print message
case "$1" in case "$1" in
info) info)
printf "info >> %s\n" "$2" >&2 printf "info >> %s\n" "$2" >&2
@ -20,8 +20,42 @@ msg() {
esac esac
} }
usage() {
cat << EOF
usage: $0 [options]
-o, --output <file> output file. default is $(readlink -f $(dirname "$0"))/initramfs-$(uname -r)
-f, --files <dir> files directory. default is $(readlink -f $(dirname "$0"))
EOF
}
parse_args() {
while [ "$1" ]; do
case "$1" in
-o | --output)
initramfs="$2"
shift 2
;;
-f | --files)
filesdir="$2"
shift 2
;;
-h | --help)
usage
exit 0
;;
*)
printf "%s\n\n" "invalid option: '$1'"
usage
exit 1
;;
esac
done
}
create_wrkdir() { create_wrkdir() {
msg info "creating working directory" msg info "creating working directory"
if [ "$XDG_CACHE_HOME" ]; then if [ "$XDG_CACHE_HOME" ]; then
wrkdir="${XDG_CACHE_HOME}/initramfs.$$" wrkdir="${XDG_CACHE_HOME}/initramfs.$$"
elif [ "$TMPDIR" ]; then elif [ "$TMPDIR" ]; then
@ -35,16 +69,8 @@ create_wrkdir() {
remove_wrkdir() { remove_wrkdir() {
msg info "removing working directory" msg info "removing working directory"
rm -rf "$wrkdir"
}
# change current directory to script directory if user haven't do it rm -rf "$wrkdir"
check_currentdir() {
scriptdir=$(readlink -f $(dirname "$0"))
[ "$PWD" = "$scriptdir" ] || {
msg info "changing current directory to script directory"
cd "$scriptdir" || msg panic "failed to change directory"
}
} }
install_requirements() { install_requirements() {
@ -62,17 +88,20 @@ install_requirements() {
create_structure() { create_structure() {
msg info "creating directory structure" msg info "creating directory structure"
for dir in dev tmp var run etc usr/lib usr/bin mnt/root proc root sys; do for dir in dev tmp var run etc usr/lib usr/bin mnt/root proc root sys; do
mkdir -p "${wrkdir}/${dir}" mkdir -p "${wrkdir}/${dir}"
done done
} }
# some dynamically linked libraries and binaries compiled with hardcoded
# dependencies path. to make it worked we need create symlinks for them.
# also POSIX ln doesn't have --relative flag like in GNU ln. as workaround
# we change directory to wrkdir and make needed symlinks.
create_symlinks() { create_symlinks() {
# some dynamically linked libraries and binaries compiled with hardcoded
# dependencies path. to make it worked we need create symlinks for them.
# also POSIX ln doesn't have --relative flag like in GNU ln. as workaround
# we change directory to wrkdir and make needed symlinks.
msg info "creating symlinks" msg info "creating symlinks"
( cd "$wrkdir" && { ( cd "$wrkdir" && {
ln -s usr/lib lib ln -s usr/lib lib
ln -s usr/lib lib64 ln -s usr/lib lib64
@ -85,40 +114,25 @@ create_symlinks() {
} ) } )
} }
parse_fstab() {
msg info "parsing fstab"
while read -r fs dir type opts dump pass; do
# ignore comments
if [ ! "${fs##"#"*}" ]; then
continue
elif [ "$dir" = / ]; then
root="${root:-$fs}"
root_type="${root_type:-$type}"
root_opts="${root_opts:-$opts}"
fi
done < /etc/fstab
}
#parse_crypttab() {
# TODO parse crypttab
#}
install_mdev() { install_mdev() {
msg info "installing mdev" msg info "installing mdev"
install -m644 mdev.conf -t "${wrkdir}/etc" install -m644 mdev.conf -t "${wrkdir}/etc"
install -Dm755 storage-device -t "${wrkdir}/lib/mdev" install -Dm755 storage-device -t "${wrkdir}/lib/mdev"
} }
install_mdevd() { install_mdevd() {
msg info "installing mdevd" msg info "installing mdevd"
install_binary mdevd mdevd-coldplug install_binary mdevd mdevd-coldplug
install -m644 mdev.conf -t "${wrkdir}/etc" install -m644 mdev.conf -t "${wrkdir}/etc"
install -Dm755 storage-device -t "${wrkdir}/lib/mdev" install -Dm755 storage-device -t "${wrkdir}/lib/mdev"
} }
install_udev() { install_udev() {
msg info "installing udev" msg info "installing udev"
install_binary udevd udevadm dmsetup install_binary udevd udevadm dmsetup
# FIXME rewrite this piece of crap # FIXME rewrite this piece of crap
find /usr/lib/udev -type f | grep -v "rc_keymaps\|hwdb.d" | cpio -pd "$wrkdir" > /dev/null 2>&1 find /usr/lib/udev -type f | grep -v "rc_keymaps\|hwdb.d" | cpio -pd "$wrkdir" > /dev/null 2>&1
@ -126,6 +140,7 @@ install_udev() {
install_lvm() { install_lvm() {
msg info "installing LVM" msg info "installing LVM"
install_binary lvm install_binary lvm
lvm_drivers="dm-thin-pool dm-multipath dm-snapshot dm-cache dm-log dm-mirror" lvm_drivers="dm-thin-pool dm-multipath dm-snapshot dm-cache dm-log dm-mirror"
@ -152,6 +167,7 @@ EOF
install_luks() { install_luks() {
msg info "installing LUKS" msg info "installing LUKS"
install_binary cryptsetup install_binary cryptsetup
luks_drivers="aes dm-crypt sha256 sha512 wp512 ecb lrw xts twofish serpent" luks_drivers="aes dm-crypt sha256 sha512 wp512 ecb lrw xts twofish serpent"
@ -161,6 +177,7 @@ install_luks() {
mkdir "${wrkdir}/run/cryptsetup" mkdir "${wrkdir}/run/cryptsetup"
# TODO get rid of this workaround # TODO get rid of this workaround
# TODO fix static binary
# workaround for luks2 # workaround for luks2
install -s -m755 /usr/lib/libgcc_s.so.1 -t "${wrkdir}/usr/lib" || msg panic "failed to install LUKS libraries" install -s -m755 /usr/lib/libgcc_s.so.1 -t "${wrkdir}/usr/lib" || msg panic "failed to install LUKS libraries"
@ -179,15 +196,22 @@ install_luks() {
install_driver() { install_driver() {
# check monolithic kernel(builtin drivers) # check monolithic kernel(builtin drivers)
[ -d "${moddir}/${kernel}" ] || return 0 [ -d "${moddir}/${kernel}" ] || return
# we need splitting
# shellcheck disable=SC2068
for driver in $@; do for driver in $@; do
# strip path and extension # strip path and extension
driver="${driver##*/}" driver="${driver##*/}"
driver="${driver%%.*}" driver="${driver%%.*}"
# TODO busybox modprobe doesn't support -S option # TODO busybox modprobe doesn't support -S option
modprobe -S "$kernel" -D "$driver" 2> /dev/null | grep -v builtin | cut -d " " -f 2 | while read -r driver_dep; do modprobe -S "$kernel" -D "$driver" 2> /dev/null |
grep -v "builtin\|net" |
cut -d " " -f 2 |
while read -r driver_dep; do
install -Dm644 "$driver_dep" "${wrkdir}${driver_dep}" install -Dm644 "$driver_dep" "${wrkdir}${driver_dep}"
done done
done done
@ -195,6 +219,7 @@ install_driver() {
install_hostonly_drivers() { install_hostonly_drivers() {
msg info "installing hostonly drivers" msg info "installing hostonly drivers"
[ "$root_type" ] || msg panic "hostonly mode required root_type option to be configured" [ "$root_type" ] || msg panic "hostonly mode required root_type option to be configured"
# perform autodetection of drivers via /sys # perform autodetection of drivers via /sys
@ -211,6 +236,7 @@ install_hostonly_drivers() {
install_all_drivers() { install_all_drivers() {
msg info "installing all drivers" msg info "installing all drivers"
modker="${moddir}/${kernel}/kernel" modker="${moddir}/${kernel}/kernel"
install_driver \ install_driver \
@ -231,51 +257,74 @@ install_all_drivers() {
generate_depmod() { generate_depmod() {
msg info "running depmod" msg info "running depmod"
modker="${moddir}/${kernel}" modker="${moddir}/${kernel}"
cp "${modker}/modules.builtin" "${modker}/modules.order" "${wrkdir}/${modker}" cp "${modker}/modules.builtin" "${modker}/modules.order" "${wrkdir}/${modker}"
depmod -b "$wrkdir" "$kernel" depmod -b "$wrkdir" "$kernel"
} }
# TODO make strip optional
install_binary() { install_binary() {
# TODO make strip optional
# we need splitting
# shellcheck disable=SC2068
for binary in $@; do for binary in $@; do
msg info "installing binary $binary" msg info "installing binary $binary"
fullbin=$(command -v "$binary")
# check binary existence # check binary existence
command -v "$binary" > /dev/null 2>&1 || msg panic "$binary doesn't exists" [ "$fullbin" ] || msg panic "$binary doesn't exists"
# install and strip binary # install and strip binary
install -s -m755 "$(command -v $binary)" -t "${wrkdir}/usr/bin" install -s -m755 "$fullbin" -t "${wrkdir}/usr/bin"
# check statically linking # check static
ldd "$(command -v $binary)" > /dev/null 2>&1 || continue ldd "$fullbin" > /dev/null 2>&1 || continue
# install libraries # install libraries
install_library "$binary" install_library "$binary"
done done
} }
# TODO make strip optional
install_library() { install_library() {
ldd "$(command -v $1)" | sed -nre 's,.* (/.*lib.*/.*.so.*) .*,\1,p' -e 's,.*(/lib.*/ld.*.so.*) .*,\1,p' | while read -r library; do # TODO make strip optional
wrkdirlib="${wrkdir}/usr/lib/"
# extract paths to libraries from ldd output
ldd $(command -v "$1") |
sed -nr \
-e 's,.* (/.*lib.*/.*.so.*) .*,\1,p' \
-e 's,.*(/lib.*/ld.*.so.*) .*,\1,p' |
while read -r library; do
namelib="${library##*/}"
reallib=$(readlink "$library")
fulllib=$(readlink -f "$library")
# check symlink # check symlink
if [ -h "$library" ]; then if [ -h "$library" ]; then
# check lib already existence # check lib already existence
if [ ! -e "${wrkdir}/usr/lib/${library##*/}" ] && [ ! -e "${wrkdir}/$(readlink -f $library)" ]; then [ -e "${wrkdirlib}${namelib}" ] ||
[ -e "${wrkdir}${fulllib}" ] ||
{
# regular # regular
install -s -m755 "$(readlink -f $library)" -t "${wrkdir}/usr/lib" install -s -m755 "${fulllib}" -t "${wrkdirlib}"
# FIXME handle all symlinks # FIXME handle all symlinks
# symlink may link to symlink # symlink may link to symlink
[ -h "/usr/lib/$(readlink $library)" ] && cp -a "/usr/lib/$(readlink $library)" "${wrkdir}/usr/lib" [ -h "/usr/lib/${reallib}" ] &&
cp -a "/usr/lib/${reallib}" "${wrkdirlib}"
# symlink # symlink
cp -a "$library" "${wrkdir}/usr/lib" cp -a "$library" "${wrkdirlib}"
fi }
else else
if [ ! -e "${wrkdir}/usr/lib/${library##*/}" ]; then [ -e "${wrkdirlib}${namelib}" ] ||
install -s -m755 "$library" -t "${wrkdir}/usr/lib" install -s -m755 "$library" -t "${wrkdirlib}"
fi
fi fi
done done
} }
@ -283,8 +332,27 @@ install_library() {
install_files() { install_files() {
msg info "installing files" msg info "installing files"
# initialize config cat << EOF > "${wrkdir}/config"
sed -e "/^#/d" -e "/^$/d" ./config > "${wrkdir}/config" debug="$debug"
init="$init"
root="$root"
root_type="$root_type"
root_opts="$root_opts"
devmgr="$devmgr"
#drivers
lvm="$lvm"
lvm_name="$lvm_name"
lvm_group="$lvm_group"
#lvm_discard
lvm_args="$lvm_args"
luks="$luks"
luks_root="$luks_root"
luks_name="$luks_name"
#luks_header
#luks_keyfile
luks_discard="$luks_discard"
luks_args="$luks_args"
EOF
# needed for devmgr # needed for devmgr
cat << EOF > "${wrkdir}/etc/group" cat << EOF > "${wrkdir}/etc/group"
@ -311,10 +379,9 @@ nobody:x:99:99::/:/bin/false
EOF EOF
# install init script # install init script
install -m755 ./init -t "$wrkdir" install -m755 "${filesdir}/init" -t "$wrkdir"
} }
# create and compress cpio archive
create_initramfs() { create_initramfs() {
msg info "creating initramfs image" msg info "creating initramfs image"
{ {
@ -322,7 +389,7 @@ create_initramfs() {
cd "$wrkdir" cd "$wrkdir"
find . | cpio -oH newc | ${compress:-gzip -9} find . | cpio -oH newc | ${compress:-gzip -9}
) | tee "${scriptdir}/${initramfs:=initramfs-${kernel}}" ) | tee "${initramfs:-${filesdir}/initramfs-${kernel}}"
} > /dev/null 2>&1 || msg panic "failed to generate initramfs image" } > /dev/null 2>&1 || msg panic "failed to generate initramfs image"
} }
@ -330,29 +397,26 @@ create_initramfs() {
# check root # check root
[ "$(id -u)" = 0 ] || msg panic "must be run as root" [ "$(id -u)" = 0 ] || msg panic "must be run as root"
create_wrkdir parse_args "$@"
. "${filesdir:=$(readlink -f $(dirname "$0"))}/config" || msg panic "failed to source config"
# remove wrkdir on exit or unexpected error # remove wrkdir on exit or unexpected error
trap remove_wrkdir EXIT INT trap remove_wrkdir EXIT INT
check_currentdir [ "$debug" = 1 ] && {
. ./config || msg panic "failed to source config"
[ "$shell_debug" = 1 ] && {
# debug shell commands # debug shell commands
set -x set -x
# don't remove anything # don't remove anything
trap : EXIT INT trap - EXIT INT
} }
kernel="${kernel:-$(uname -r)}" kernel="${kernel:-$(uname -r)}"
moddir="/lib/modules" moddir="/lib/modules"
create_wrkdir
create_structure create_structure
create_symlinks create_symlinks
[ "$fstab" = 1 ] && [ -f /etc/fstab ] && parse_fstab
#parse_crypttab
install_requirements install_requirements
if [ "$hostonly" = 1 ]; then if [ "$hostonly" = 1 ]; then
@ -364,16 +428,15 @@ fi
generate_depmod generate_depmod
case "$devmgr" in case "$devmgr" in
udev) install_udev ;;
mdev) install_mdev ;; mdev) install_mdev ;;
mdevd) install_mdevd ;; mdevd) install_mdevd ;;
udev) install_udev ;;
*) msg panic "devmgr option broken" ;; *) msg panic "devmgr option broken" ;;
esac esac
[ "$luks" = 1 ] && [ -x "$(command -v cryptsetup)" ] && install_luks
[ "$lvm" = 1 ] && [ -x "$(command -v lvm)" ] && install_lvm [ "$lvm" = 1 ] && [ -x "$(command -v lvm)" ] && install_lvm
[ "$luks" = 1 ] && [ -x "$(command -v cryptsetup)" ] && install_luks
install_files install_files
create_initramfs create_initramfs
msg info "done! check out $initramfs" msg info "done! check out ${initramfs:-${filesdir}/initramfs-${kernel}}"

13
init
View File

@ -9,15 +9,14 @@ panic() {
} }
parse_cmdline() { parse_cmdline() {
# store cmdline in variable # turn output into list
read -r cmdline < /proc/cmdline || panic "failed to parse cmdline" set -- $(cat /proc/cmdline)
# turn variable into list
set -- $cmdline
# parse line by line # parse line by line
for line in "$@"; do for line in "$@"; do
# parse options # parse options
case "${line%%=*}" in case "${line%%=*}" in
debug) debug="${line##*=}" ;;
init) init="${line##*=}" ;; init) init="${line##*=}" ;;
root) root="${line##*=}" ;; root) root="${line##*=}" ;;
root.type) root_type="${line##*=}" ;; root.type) root_type="${line##*=}" ;;
@ -31,8 +30,6 @@ parse_cmdline() {
luks.name) luks_name="${line##*=}" ;; luks.name) luks_name="${line##*=}" ;;
luks.discard) luks_discard="${line##*=}" ;; luks.discard) luks_discard="${line##*=}" ;;
luks.args) luks_args="${line##*=}" ;; luks.args) luks_args="${line##*=}" ;;
shell.debug) shell_debug="${line##*=}" ;;
shell.break) shell_break="${line##*=}" ;;
# TODO implement # TODO implement
#lvm.discard) ;; #lvm.discard) ;;
#lvm.conf) ;; #lvm.conf) ;;
@ -147,7 +144,7 @@ mnt_pseudofs
parse_cmdline parse_cmdline
# debug mode # debug mode
[ "$shell_debug" = 1 ] && set -x [ "$debug" = 1 ] && set -x
case "$devmgr" in case "$devmgr" in
mdev) setup_mdev ;; mdev) setup_mdev ;;
@ -160,6 +157,6 @@ esac
[ "$luks" = 1 ] && [ -x "$(command -v cryptsetup)" ] && unlock_luks [ "$luks" = 1 ] && [ -x "$(command -v cryptsetup)" ] && unlock_luks
[ "$lvm" = 1 ] && [ -x "$(command -v lvm)" ] && trigger_lvm [ "$lvm" = 1 ] && [ -x "$(command -v lvm)" ] && trigger_lvm
mnt_rootfs mnt_rootfs
[ "$shell_break" = 1 ] && panic "dropping to shell" [ "$debug" = 1 ] && panic "dropping to shell"
cleanup cleanup
boot_system boot_system