get rid of install -t and improve formatting

This commit is contained in:
illiliti 2020-02-25 00:19:29 +03:00
parent affdc030ae
commit 347d0d2d01
2 changed files with 89 additions and 41 deletions

View File

@ -13,7 +13,6 @@ parse_cmdline() {
# turn output into list # turn output into list
set -- $(cat /proc/cmdline) set -- $(cat /proc/cmdline)
# parse line by line
for line in "$@"; do for line in "$@"; do
# parse options # parse options
@ -67,7 +66,8 @@ setup_mdev() {
done done
# load drivers # load drivers
find /sys -name modalias -type f -exec sort -u "{}" "+" | xargs modprobe -qba find /sys -name modalias -type f -exec sort -u "{}" "+" |
xargs modprobe -qba
} }
setup_mdevd() { setup_mdevd() {
@ -99,15 +99,19 @@ findfs_sh() {
sleep 0.5 sleep 0.5
[ "$increment" ] || increment=0 [ "$increment" ] || increment=0
increment=$(( increment + 1 )) increment=$(( increment + 1 ))
[ "$increment" = 10 ] && panic "failed to lookup partition" [ "$increment" = 10 ] &&
panic "failed to lookup partition"
done done
printf "%s\n" "$device" printf "%s\n" "$device"
} }
unlock_luks() { unlock_luks() {
[ "$luks_discard" = 1 ] && luks_args="--allow-discards $luks_args" [ "$luks_discard" = 1 ] &&
cryptsetup $luks_args luksOpen $(findfs_sh "$luks_root") ${luks_name:-luks_root} || panic "failed to unlock luks container" luks_args="--allow-discards $luks_args"
cryptsetup $luks_args luksOpen $(findfs_sh "$luks_root") ${luks_name:-luks_root} ||
panic "failed to unlock luks container"
} }
trigger_lvm() { trigger_lvm() {
@ -121,7 +125,8 @@ trigger_lvm() {
} }
mnt_rootfs() { mnt_rootfs() {
mount ${root_type:+-t $root_type} ${root_opts:+-o $root_opts} $(findfs_sh "$root") /mnt/root || panic "failed to mount rootfs" mount ${root_type:+-t $root_type} ${root_opts:+-o $root_opts} $(findfs_sh "$root") /mnt/root ||
panic "failed to mount rootfs"
} }
cleanup() { cleanup() {
@ -136,12 +141,14 @@ cleanup() {
} }
boot_system() { boot_system() {
exec switch_root /mnt/root ${init:-/sbin/init} || panic "failed to boot system" exec switch_root /mnt/root ${init:-/sbin/init} ||
panic "failed to boot system"
} }
/sbin/busybox --install -s /sbin/busybox --install -s
. /config || panic "failed to source config" . /config ||
panic "failed to source config"
mnt_pseudofs mnt_pseudofs
parse_cmdline parse_cmdline
@ -157,8 +164,14 @@ case "$devmgr" in
esac esac
# TODO handle situations when LUKS on LVM # TODO handle situations when LUKS on LVM
[ "$luks" = 1 ] && command -v cryptsetup 2>&1 > /dev/null && unlock_luks [ "$luks" = 1 ] &&
[ "$lvm" = 1 ] && command -v lvm 2>&1 > /dev/null && trigger_lvm command -v cryptsetup > /dev/null 2>&1 &&
unlock_luks
[ "$lvm" = 1 ] &&
command -v lvm > /dev/null 2>&1 &&
trigger_lvm
mnt_rootfs mnt_rootfs
[ "$debug" = 1 ] && panic "dropping to shell" [ "$debug" = 1 ] && panic "dropping to shell"
cleanup cleanup

View File

@ -81,7 +81,8 @@ create_workdir() {
msg info "creating working directory" msg info "creating working directory"
workdir="${XDG_CACHE_HOME:-${TMPDIR:-/tmp}}/initramfs.$$" workdir="${XDG_CACHE_HOME:-${TMPDIR:-/tmp}}/initramfs.$$"
mkdir "$workdir" || msg panic "failed to create working directory" mkdir -p "$workdir" ||
msg panic "failed to create working directory"
} }
remove_workdir() { remove_workdir() {
@ -94,10 +95,12 @@ install_requirements() {
msg info "installing requirements" msg info "installing requirements"
# install user specified binaries # install user specified binaries
[ "$binaries" ] && install_binary "$binaries" [ "$binaries" ] &&
install_binary "$binaries"
# install util-linux binaries # install util-linux binaries
[ "$util_linux" = 1 ] && install_binary mount blkid [ "$util_linux" = 1 ] &&
install_binary mount blkid
# install mandatory binaries # install mandatory binaries
install_binary busybox modprobe install_binary busybox modprobe
@ -179,13 +182,16 @@ install_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"
[ "$hostonly" = 1 ] && install_driver "$lvm_drivers" [ "$hostonly" = 1 ] &&
install_driver "$lvm_drivers"
# install lvm config # install lvm config
if [ "$lvm_conf" = 1 ]; then if [ "$lvm_conf" = 1 ]; then
install -Dm644 /etc/lvm/*.conf -t "${workdir}/etc/lvm" || msg panic "failed to install LVM config" mkdir -p "${workdir}/etc/lvm"
cp /etc/lvm/*.conf "${workdir}/etc/lvm" ||
msg panic "failed to install LVM config"
else else
mkdir "${workdir}/etc/lvm" mkdir -p "${workdir}/etc/lvm"
cat << EOF > "${workdir}/etc/lvm/lvm.conf" cat << EOF > "${workdir}/etc/lvm/lvm.conf"
devices { devices {
# block discard support # block discard support
@ -206,26 +212,35 @@ install_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"
[ "$hostonly" = 1 ] && install_driver "$luks_drivers" [ "$hostonly" = 1 ] &&
install_driver "$luks_drivers"
# avoid "locking directory missing" warning message and libgcc_s.so.1 missing error # avoid "locking directory missing" warning
# message and libgcc_s.so.1 missing error
# see https://bugs.archlinux.org/task/56771 # see https://bugs.archlinux.org/task/56771
mkdir "${workdir}/run/cryptsetup" mkdir -p "${workdir}/run/cryptsetup"
[ -e /usr/lib/libgcc_s.so.1 ] && { [ -e /usr/lib/libgcc_s.so.1 ] && {
install -s -m755 /usr/lib/libgcc_s.so.1 -t "${workdir}/usr/lib" || install -s -m755 /usr/lib/libgcc_s.so.1 \
"${workdir}/usr/lib/libgcc_s.so.1" ||
msg panic "failed to install LUKS libraries" msg panic "failed to install LUKS libraries"
} }
# copy luks header # copy luks header
[ -f "$luks_header" ] && { [ -f "$luks_header" ] && {
install -m400 "$luks_header" "${workdir}/root/luks_header" || msg panic "failed to copy LUKS header" install -m400 "$luks_header" \
"${workdir}/root/luks_header" ||
msg panic "failed to copy LUKS header"
luks_args="--header=/root/luks_header $luks_args" luks_args="--header=/root/luks_header $luks_args"
} }
# copy luks keyfile # copy luks keyfile
[ -f "$luks_keyfile" ] && { [ -f "$luks_keyfile" ] && {
install -m400 "$luks_keyfile" "${workdir}/root/luks_keyfile" || msg panic "failed to copy LUKS keyfile" install -m400 "$luks_keyfile" \
"${workdir}/root/luks_keyfile" ||
msg panic "failed to copy LUKS keyfile"
luks_args="--key-file=/root/luks_keyfile $luks_args" luks_args="--key-file=/root/luks_keyfile $luks_args"
} }
} }
@ -248,7 +263,8 @@ install_driver() {
cut -d " " -f 2 | cut -d " " -f 2 |
while read -r driver_dep; do while read -r driver_dep; do
install -Dm644 "$driver_dep" "${workdir}${driver_dep}" install -Dm644 "$driver_dep" \
"${workdir}${driver_dep}"
done done
done done
} }
@ -256,7 +272,8 @@ 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
install_driver "$(find /sys -name modalias -exec sort -u "{}" "+")" install_driver "$(find /sys -name modalias -exec sort -u "{}" "+")"
@ -267,7 +284,8 @@ install_hostonly_drivers() {
install_driver "$root_type" install_driver "$root_type"
# install user specified drivers # install user specified drivers
[ "$drivers" ] && install_driver "$drivers" [ "$drivers" ] &&
install_driver "$drivers"
} }
install_all_drivers() { install_all_drivers() {
@ -296,13 +314,18 @@ generate_depmod() {
modker="${moddir}/${kernel}" modker="${moddir}/${kernel}"
cp "${modker}/modules.builtin" "${modker}/modules.order" "${workdir}${modker}" cp "${modker}/modules.builtin" \
"${modker}/modules.order" \
"${workdir}${modker}"
depmod -b "$workdir" "$kernel" depmod -b "$workdir" "$kernel"
} }
install_binary() { install_binary() {
# TODO make strip optional # TODO make strip optional
workdirbin="${workdir}/usr/bin/"
# we need splitting # we need splitting
# shellcheck disable=SC2068 # shellcheck disable=SC2068
for binary in $@; do for binary in $@; do
@ -311,13 +334,16 @@ install_binary() {
fullbin=$(command -v "$binary") fullbin=$(command -v "$binary")
# check if binary exists # check if binary exists
[ "$fullbin" ] || msg panic "$binary doesn't exists" [ "$fullbin" ] ||
msg panic "$binary doesn't exists"
# install and strip binary # install and strip binary
install -s -m755 "$fullbin" -t "${workdir}/usr/bin" install -s -m755 "$fullbin" \
"${workdirbin}${fullbin##*/}"
# check static # check static
ldd "$fullbin" > /dev/null 2>&1 || continue ldd "$fullbin" > /dev/null 2>&1 ||
continue
# install libraries # install libraries
install_library "$binary" install_library "$binary"
@ -348,19 +374,22 @@ install_library() {
[ -e "${workdirlib}${namelib}" ] || [ -e "${workdirlib}${namelib}" ] ||
{ {
# regular # regular
install -s -m755 "${fulllib}" -t "${workdirlib}" install -s -m755 "${fulllib}" \
"${workdirlib}${fulllib##*/}"
# FIXME handle all symlinks # FIXME handle all symlinks
# symlink may link to symlink # symlink may link to symlink
[ -h "/usr/lib/${reallib}" ] && [ -h "/usr/lib/${reallib}" ] &&
cp -a "/usr/lib/${reallib}" "${workdirlib}" cp -a "/usr/lib/${reallib}" \
"${workdirlib}"
# symlink # symlink
cp -a "$library" "${workdirlib}" cp -a "$library" "${workdirlib}"
} }
else else
[ -e "${workdirlib}${namelib}" ] || [ -e "${workdirlib}${namelib}" ] ||
install -s -m755 "$library" -t "${workdirlib}" install -s -m755 "$library" \
"${workdirlib}${namelib}"
fi fi
done done
} }
@ -390,9 +419,14 @@ luks_discard="$luks_discard"
luks_args="$luks_args" luks_args="$luks_args"
EOF EOF
install -m644 "${filesdir}/passwd" -t "$workdir" install -m644 "${filesdir}/passwd" \
install -m644 "${filesdir}/group" -t "$workdir" "$workdir/etc/passwd"
install -m755 "${filesdir}/init" -t "$workdir"
install -m644 "${filesdir}/group" \
"$workdir/etc/group"
install -m755 "${filesdir}/init" \
"$workdir/init"
} }
create_initramfs() { create_initramfs() {
@ -408,7 +442,8 @@ create_initramfs() {
) | tee "$initramfs" ) | tee "$initramfs"
} > /dev/null 2>&1 || msg panic "failed to generate initramfs image" } > /dev/null 2>&1 ||
msg panic "failed to generate initramfs image"
} }
# check root # check root