improve code

This commit is contained in:
illiliti 2020-02-27 19:55:23 +03:00
parent a08c405a54
commit f39daed199

132
tinyramfs
View File

@ -143,7 +143,7 @@ create_symlinks() {
ln -s usr/lib lib64 ln -s usr/lib lib64
ln -s usr/bin bin ln -s usr/bin bin
ln -s usr/bin sbin ln -s usr/bin sbin
ln -s ../run var/run ln -s ../run var/run
cd usr cd usr
ln -s bin sbin ln -s bin sbin
ln -s lib lib64 ln -s lib lib64
@ -164,26 +164,19 @@ install_devmgr() {
! -path "*hwdb.d*" \ ! -path "*hwdb.d*" \
-type f | -type f |
cpio -pd "$workdir" > /dev/null 2>&1 || cpio -pd "$workdir" > /dev/null 2>&1 || msg panic "failed to install udev"
msg panic "failed to install udev"
;; ;;
mdev) mdev)
install -m644 "${filesdir}/mdev.conf" \ install -m644 "${filesdir}/mdev.conf" "${workdir}/etc/mdev.conf"
"${workdir}/etc/mdev.conf" install -Dm755 "${filesdir}/storage-device" "${workdir}/lib/mdev/storage-device"
install -Dm755 "${filesdir}/storage-device" \
"${workdir}/lib/mdev/storage-device"
;; ;;
mdevd) mdevd)
for mdevd_binary in mdevd mdevd-coldplug; do for mdevd_binary in mdevd mdevd-coldplug; do
install_binary "$mdevd_binary" install_binary "$mdevd_binary"
done done
install -m644 "${filesdir}/mdev.conf" \ install -m644 "${filesdir}/mdev.conf" "${workdir}/etc/mdev.conf"
"${workdir}/etc/mdev.conf" install -Dm755 "${filesdir}/storage-device" "${workdir}/lib/mdev/storage-device"
install -Dm755 "${filesdir}/storage-device" \
"${workdir}/lib/mdev/storage-device"
;; ;;
*) *)
msg panic "devmgr option broken" msg panic "devmgr option broken"
@ -204,10 +197,12 @@ install_lvm() {
# install lvm config # install lvm config
if [ "$lvm_conf" = 1 ]; then if [ "$lvm_conf" = 1 ]; then
mkdir -p "${workdir}/etc/lvm" mkdir -p "${workdir}/etc/lvm"
cp /etc/lvm/*.conf "${workdir}/etc/lvm" || cp /etc/lvm/*.conf "${workdir}/etc/lvm" ||
msg panic "failed to install LVM config" msg panic "failed to install LVM config"
else else
mkdir -p "${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
@ -242,18 +237,16 @@ install_luks() {
# copy luks header # copy luks header
[ -f "$luks_header" ] && { [ -f "$luks_header" ] && {
install -m400 "$luks_header" \ install -m400 "$luks_header" "${workdir}/root/luks_header" ||
"${workdir}/root/luks_header" || msg panic "failed to copy 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" \ install -m400 "$luks_keyfile" "${workdir}/root/luks_keyfile" ||
"${workdir}/root/luks_keyfile" || msg panic "failed to copy 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"
} }
@ -261,21 +254,19 @@ install_luks() {
install_driver() { install_driver() {
driver="$1" driver="$1"
driver="${driver##*/}"
driver="${driver%%.*}"
# TODO busybox modprobe doesn't support -S option
modprobe -S "$kernel" -D "$driver" 2> /dev/null | modprobe -S "$kernel" -D "$driver" 2> /dev/null |
sed "/builtin\|net/d;s/insmod //" |
while read -r full_driver; do while read -r full_driver; do
# check if driver already installed # strip uneeded stuff
[ -e "${workdir}${full_driver}" ] && full_driver="${full_driver##*builtin*}"
continue full_driver="${full_driver##*net*}"
full_driver="${full_driver#insmod }"
install -Dm644 "$full_driver" \ # check if already installed
"${workdir}${full_driver}" [ -e "${workdir}${full_driver}" ] ||
install -Dm644 "$full_driver" "${workdir}${full_driver}"
done done
} }
@ -292,8 +283,6 @@ install_hostonly_drivers() {
install_driver "$alias_driver" install_driver "$alias_driver"
done done
# TODO autodetect root fs driver
# TODO separate root type option
# install root fs driver # install root fs driver
install_driver "$root_type" install_driver "$root_type"
@ -321,13 +310,18 @@ install_all_drivers() {
"${modker}/kernel/drivers/virtio" \ "${modker}/kernel/drivers/virtio" \
-type f 2> /dev/null | -type f 2> /dev/null |
while read -r path_driver; do while read -r path_driver; do
install_driver "$path_driver"
done # strip path and extension
path_driver="${path_driver##*/}"
path_driver="${path_driver%%.*}"
install_driver "$path_driver"
done
} }
generate_depmod() { generate_depmod() {
msg info "running depmod" msg info "generating drivers list"
cp "${modker}/modules.builtin" \ cp "${modker}/modules.builtin" \
"${modker}/modules.order" \ "${modker}/modules.order" \
@ -337,33 +331,24 @@ generate_depmod() {
} }
install_binary() { install_binary() {
# TODO make strip optional
binary="$1" binary="$1"
fullbin=$(command -v "$binary") fullbin=$(command -v "$binary")
workdirbin="${workdir}/usr/bin/" workdirbin="${workdir}/usr/bin/"
# check if binary exists # check if binary exists
[ "$fullbin" ] || [ "$fullbin" ] || msg panic "$binary doesn't exists"
msg panic "$binary doesn't exists"
# check if binary already installed # check if binary already installed
[ -e "${workdirbin}${fullbin##*/}" ] && [ -e "${workdirbin}${fullbin##*/}" ] && return
return
# install and strip binary # install and strip binary
install -s -m755 "$fullbin" \ install -s -m755 "$fullbin" "${workdirbin}${fullbin##*/}"
"${workdirbin}${fullbin##*/}"
# check static # check static
ldd "$fullbin" > /dev/null 2>&1 || ldd "$fullbin" > /dev/null 2>&1 || return
return
# exract paths to libraries # exract paths to libraries
ldd "$fullbin" | ldd "$fullbin" | sed -nr 's,.* (/.*lib.*/.*.so.*) .*,\1,p' |
sed -nr \
-e 's,.* (/.*lib.*/.*.so.*) .*,\1,p' \
-e 's,.*(/lib.*/ld.*.so.*) .*,\1,p' |
while read -r library; do while read -r library; do
install_library "$library" install_library "$library"
@ -371,39 +356,29 @@ install_binary() {
} }
install_library() { install_library() {
# TODO make strip optional
library="$1" library="$1"
namelib="${library##*/}" namelib="${library##*/}"
reallib=$(readlink "$library") reallib="/usr/lib/$(readlink "$library")"
fulllib=$(readlink -f "$library") fulllib=$(readlink -f "$library")
workdirlib="${workdir}/usr/lib/" workdirlib="${workdir}/usr/lib/"
# check symlink # check if library already installed
if [ -h "$library" ]; then [ -e "${workdirlib}${fulllib##*/}" ] &&
[ -e "${workdirlib}${namelib}" ] &&
return
# check if library already exists # install symlinks if any
[ -e "${workdirlib}${fulllib##*/}" ] || [ -h "$library" ] && {
[ -e "${workdirlib}${namelib}" ] ||
{
# regular
install -s -m755 "${fulllib}" \
"${workdirlib}${fulllib##*/}"
# FIXME handle all symlinks # symlink may link to symlink
# symlink may link to symlink [ -h "$reallib" ] &&
[ -h "/usr/lib/${reallib}" ] && cp -a "$reallib" "$workdirlib"
cp -a "/usr/lib/${reallib}" \
"${workdirlib}"
# symlink cp -a "$library" "$workdirlib"
cp -a "$library" "${workdirlib}" }
}
else # install library
[ -e "${workdirlib}${namelib}" ] || install -s -m755 "$fulllib" "${workdirlib}${fulllib##*/}"
install -s -m755 "$library" \
"${workdirlib}${namelib}"
fi
} }
install_files() { install_files() {
@ -431,14 +406,9 @@ luks_discard="$luks_discard"
luks_args="$luks_args" luks_args="$luks_args"
EOF EOF
install -m644 "${filesdir}/passwd" \ install -m644 "${filesdir}/passwd" "${workdir}/etc/passwd"
"$workdir/etc/passwd" install -m644 "${filesdir}/group" "${workdir}/etc/group"
install -m755 "${filesdir}/init" "${workdir}/init"
install -m644 "${filesdir}/group" \
"$workdir/etc/group"
install -m755 "${filesdir}/init" \
"$workdir/init"
} }
create_initramfs() { create_initramfs() {