formatting

This commit is contained in:
illiliti 2020-02-11 21:02:23 +03:00
parent 3608109b78
commit a0bd4caf46
2 changed files with 153 additions and 151 deletions

228
generate
View File

@ -5,18 +5,18 @@
# print message # print message
msg() { msg() {
case "$1" in case "$1" in
info) info)
printf "info >> %s\n" "$2" >&2 printf "info >> %s\n" "$2" >&2
;; ;;
warn) warn)
printf "warning >> %s\n" "$2" >&2 printf "warning >> %s\n" "$2" >&2
printf "do you want to continue? press enter or ctrl+c to exit\n" printf "do you want to continue? press enter or ctrl+c to exit\n"
read -r _ read -r _
;; ;;
panic) panic)
printf "panic >> %s\n" "$2" >&2 printf "panic >> %s\n" "$2" >&2
exit 1 exit 1
;; ;;
esac esac
} }
@ -47,8 +47,8 @@ remove_tmpdir() {
check_currentdir() { check_currentdir() {
script_dir=$(dirname $(readlink -f "$0")) script_dir=$(dirname $(readlink -f "$0"))
[ "$PWD" = "$script_dir" ] || { [ "$PWD" = "$script_dir" ] || {
msg info "changing directory to script dir" msg info "changing directory to script dir"
cd "$script_dir" || msg panic "failed to change directory" cd "$script_dir" || msg panic "failed to change directory"
} }
} }
@ -57,43 +57,43 @@ check_requirements() {
msg info "checking requirements" msg info "checking requirements"
# check busybox installed # check busybox installed
command -v busybox >/dev/null 2>&1 && { command -v busybox > /dev/null 2>&1 && {
# check busybox supports CONFIG_FEATURE_INSTALLER # check busybox supports CONFIG_FEATURE_INSTALLER
busybox --help | grep -q "\-\-install \[\-s\]" || msg panic "recompile busybox with CONFIG_FEATURE_INSTALLER" busybox --help | grep -q "\-\-install \[\-s\]" || msg panic "recompile busybox with CONFIG_FEATURE_INSTALLER"
# check requirements for init # check requirements for init
for busybox_dep in mdev uevent switch_root; do for busybox_dep in mdev uevent switch_root; do
busybox $busybox_dep --help >/dev/null 2>&1 || msg panic "recompile busybox with $busybox_dep" busybox $busybox_dep --help > /dev/null 2>&1 || msg panic "recompile busybox with $busybox_dep"
done done
} || msg panic "busybox doesn't installed" } || msg panic "busybox doesn't installed"
# check kmod modprobe installed # check kmod modprobe installed
command -v modprobe >/dev/null 2>&1 && { command -v modprobe > /dev/null 2>&1 && {
# busybox modprobe doesn't supported(yet) # busybox modprobe doesn't supported(yet)
modprobe --version 2>&1 | grep -q "kmod" || msg panic "kmod modprobe version doesn't installed" modprobe --version 2>&1 | grep -q "kmod" || msg panic "kmod modprobe version doesn't installed"
} || msg panic "modprobe doesn't installed" } || msg panic "modprobe doesn't installed"
# check util-linux tools # check util-linux tools
[ "$use_util_linux" = 1 ] && { [ "$use_util_linux" = 1 ] && {
# check mount installed # check mount installed
if command -v mount >/dev/null 2>&1; then if command -v mount > /dev/null 2>&1; then
mount --version 2>&1 | grep -q "util-linux" || { mount --version 2>&1 | grep -q "util-linux" || {
msg warning "util-linux mount version doesn't installed. PARTUUID and filesystem type autodetection support will be missing" msg warning "util-linux mount version doesn't installed. PARTUUID and filesystem type autodetection support will be missing"
use_util_linux=0 use_util_linux=0
} }
else else
msg panic "mount doesn't installed" msg panic "mount doesn't installed"
fi fi
# check blkid installed # check blkid installed
if command -v blkid >/dev/null 2>&1; then if command -v blkid > /dev/null 2>&1; then
blkid --version 2>&1 | grep -q "util-linux" || { blkid --version 2>&1 | grep -q "util-linux" || {
msg warning "util-linux blkid version doesn't installed. PARTUUID support will be missing" msg warning "util-linux blkid version doesn't installed. PARTUUID support will be missing"
use_util_linux=0 use_util_linux=0
} }
else else
msg panic "blkid doesn't installed" msg panic "blkid doesn't installed"
fi fi
} }
} }
@ -115,7 +115,7 @@ 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 "${tmpdir}/${dir}" mkdir -p "${tmpdir}/${dir}"
done done
} }
@ -126,14 +126,14 @@ create_structure() {
create_symlinks() { create_symlinks() {
msg info "creating symlinks" msg info "creating symlinks"
( cd "$tmpdir" && { ( cd "$tmpdir" && {
ln -s usr/lib lib ln -s usr/lib lib
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 "${tmpdir}/usr" cd "${tmpdir}/usr"
ln -s bin sbin ln -s bin sbin
ln -s lib lib64 ln -s lib lib64
} ) } )
} }
@ -166,7 +166,7 @@ install_udev() {
msg info "installing udev" msg info "installing udev"
install_binaries udevd udevadm dmsetup install_binaries 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 "$tmpdir" >/dev/null 2>&1 find /usr/lib/udev -type f | grep -v "rc_keymaps\|hwdb.d" | cpio -pd "$tmpdir" > /dev/null 2>&1
} }
# handle lvm # handle lvm
@ -176,18 +176,18 @@ install_lvm() {
# if hostonly mode enabled install only needed drivers # if hostonly mode enabled install only needed drivers
[ "$hostonly" = 1 ] && { [ "$hostonly" = 1 ] && {
for lvm_driver in dm-thin-pool dm-multipath dm-snapshot dm-cache dm-log dm-mirror; do for lvm_driver in dm-thin-pool dm-multipath dm-snapshot dm-cache dm-log dm-mirror; do
for lvm_driver_dep in $(modprobe -D "$lvm_driver" 2>/dev/null | grep -v builtin | cut -d " " -f 2); do for lvm_driver_dep in $(modprobe -D "$lvm_driver" 2> /dev/null | grep -v builtin | cut -d " " -f 2); do
install -Dm644 "$lvm_driver_dep" "${tmpdir}${lvm_driver_dep}" install -Dm644 "$lvm_driver_dep" "${tmpdir}${lvm_driver_dep}"
done done
done done
} }
if [ "$lvm_conf" = 1 ]; then if [ "$lvm_conf" = 1 ]; then
install -Dm644 /etc/lvm/*.conf -t "${tmpdir}/etc/lvm" || msg panic "failed to install LVM config" install -Dm644 /etc/lvm/*.conf -t "${tmpdir}/etc/lvm" || msg panic "failed to install LVM config"
else else
mkdir "${tmpdir}/etc/lvm" mkdir "${tmpdir}/etc/lvm"
cat <<EOF > "${tmpdir}/etc/lvm/lvm.conf" cat << EOF > "${tmpdir}/etc/lvm/lvm.conf"
devices { devices {
issue_discards = ${lvm_discard:-0} issue_discards = ${lvm_discard:-0}
} }
@ -206,11 +206,11 @@ install_luks() {
# if hostonly mode enabled install only needed drivers # if hostonly mode enabled install only needed drivers
[ "$hostonly" = 1 ] && { [ "$hostonly" = 1 ] && {
for luks_driver in aes dm-crypt sha256 sha512 wp512 ecb lrw xts twofish serpent; do for luks_driver in aes dm-crypt sha256 sha512 wp512 ecb lrw xts twofish serpent; do
for luks_driver_dep in $(modprobe -D "$luks_driver" 2>/dev/null | grep -v builtin | cut -d " " -f 2); do for luks_driver_dep in $(modprobe -D "$luks_driver" 2> /dev/null | grep -v builtin | cut -d " " -f 2); do
install -Dm644 "$luks_driver_dep" "${tmpdir}${luks_driver_dep}" install -Dm644 "$luks_driver_dep" "${tmpdir}${luks_driver_dep}"
done done
done done
} }
# avoid locking directory missing warning message # avoid locking directory missing warning message
@ -225,14 +225,14 @@ install_luks() {
# copy luks header # copy luks header
[ -f "$luks_header" ] && { [ -f "$luks_header" ] && {
install -m400 "$luks_header" "${tmpdir}/root/luks_header" || msg panic "failed to copy LUKS header" install -m400 "$luks_header" "${tmpdir}/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" "${tmpdir}/root/luks_keyfile" || msg panic "failed to copy LUKS keyfile" install -m400 "$luks_keyfile" "${tmpdir}/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"
} }
} }
@ -243,25 +243,25 @@ install_drivers() {
# perform autodetection of drivers via /sys # perform autodetection of drivers via /sys
find /sys/devices -name modalias -exec sort -u "{}" "+" | while read -r driver; do find /sys/devices -name modalias -exec sort -u "{}" "+" | while read -r driver; do
for driver_dep in $(modprobe -D "$driver" 2>/dev/null | grep -v builtin | cut -d " " -f 2); do for driver_dep in $(modprobe -D "$driver" 2> /dev/null | grep -v builtin | cut -d " " -f 2); do
install -Dm644 "$driver_dep" "${tmpdir}${driver_dep}" install -Dm644 "$driver_dep" "${tmpdir}${driver_dep}"
done done
done done
# TODO autodetect root fs driver # TODO autodetect root fs driver
# TODO separate root type option # TODO separate root type option
# install root fs driver # install root fs driver
for root_driver in $(modprobe -D "$root_type" 2>/dev/null | grep -v builtin | cut -d " " -f 2); do for root_driver in $(modprobe -D "$root_type" 2> /dev/null | grep -v builtin | cut -d " " -f 2); do
install -Dm644 "$root_driver" "${tmpdir}${root_driver}" install -Dm644 "$root_driver" "${tmpdir}${root_driver}"
done done
# install user specified drivers # install user specified drivers
[ -n "$drivers" ] && { [ -n "$drivers" ] && {
printf "%s\n" "$drivers" | while read -r custom_driver; do printf "%s\n" "$drivers" | while read -r custom_driver; do
for custom_driver_dep in $(modprobe -D "$custom_driver" 2>/dev/null | grep -v builtin | cut -d " " -f 2); do for custom_driver_dep in $(modprobe -D "$custom_driver" 2> /dev/null | grep -v builtin | cut -d " " -f 2); do
install -Dm644 "$custom_driver_dep" "${tmpdir}${custom_driver_dep}" install -Dm644 "$custom_driver_dep" "${tmpdir}${custom_driver_dep}"
done done
done done
} }
} }
@ -280,7 +280,7 @@ install_all_drivers() {
"${modker}/kernel/drivers/usb/storage" \ "${modker}/kernel/drivers/usb/storage" \
"${modker}/kernel/drivers/usb/host" \ "${modker}/kernel/drivers/usb/host" \
"${modker}/kernel/drivers/virtio" \ "${modker}/kernel/drivers/virtio" \
-type f | cpio -pd "$tmpdir" >/dev/null 2>&1 -type f | cpio -pd "$tmpdir" > /dev/null 2>&1
} }
# generate "modules" files # generate "modules" files
@ -297,18 +297,18 @@ generate_depmod() {
# handle binaries # handle binaries
install_binaries() { install_binaries() {
printf "%s\n" "$@" | while read -r binary; do printf "%s\n" "$@" | while read -r binary; do
msg info "installing binary $binary" msg info "installing binary $binary"
# check binary existence # check binary existence
command -v "$binary" >/dev/null 2>&1 || msg panic "$binary doesn't exists" command -v "$binary" > /dev/null 2>&1 || msg panic "$binary doesn't exists"
# install and strip binary # install and strip binary
install -s -m755 "$(command -v $binary)" -t "${tmpdir}/usr/bin" install -s -m755 "$(command -v $binary)" -t "${tmpdir}/usr/bin"
# check statically linking # check statically linking
ldd "$(command -v $binary)" >/dev/null 2>&1 || continue ldd "$(command -v $binary)" > /dev/null 2>&1 || continue
# install libraries # install libraries
install_libraries $binary install_libraries $binary
done done
} }
@ -316,26 +316,26 @@ install_binaries() {
# handle libraries # handle libraries
install_libraries() { install_libraries() {
for library in $(ldd "$(command -v $1)" | sed -nre 's,.* (/.*lib.*/.*.so.*) .*,\1,p' -e 's,.*(/lib.*/ld.*.so.*) .*,\1,p'); do for library in $(ldd "$(command -v $1)" | sed -nre 's,.* (/.*lib.*/.*.so.*) .*,\1,p' -e 's,.*(/lib.*/ld.*.so.*) .*,\1,p'); do
msg info "installing library $library" msg info "installing library $library"
# check symlink # check symlink
if [ -h "$library" ]; then if [ -h "$library" ]; then
# check lib already existence # check lib already existence
if [ ! -e "${tmpdir}/usr/lib/${library##*/}" ] && [ ! -e "${tmpdir}/$(readlink -f $library)" ]; then if [ ! -e "${tmpdir}/usr/lib/${library##*/}" ] && [ ! -e "${tmpdir}/$(readlink -f $library)" ]; then
# regular # regular
install -s -m755 "$(readlink -f $library)" -t "${tmpdir}/usr/lib" install -s -m755 "$(readlink -f $library)" -t "${tmpdir}/usr/lib"
# 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)" "${tmpdir}/usr/lib" [ -h "/usr/lib/$(readlink $library)" ] && cp -a "/usr/lib/$(readlink $library)" "${tmpdir}/usr/lib"
# symlink # symlink
cp -a "$library" "${tmpdir}/usr/lib" cp -a "$library" "${tmpdir}/usr/lib"
fi fi
else else
if [ ! -e "${tmpdir}/usr/lib/${library##*/}" ]; then if [ ! -e "${tmpdir}/usr/lib/${library##*/}" ]; then
install -s -m755 "$library" -t "${tmpdir}/usr/lib" install -s -m755 "$library" -t "${tmpdir}/usr/lib"
fi fi
fi fi
done done
} }
@ -344,7 +344,7 @@ install_files() {
msg info "installing files" msg info "installing files"
# FIXME eof broken # FIXME eof broken
# initialize config # initialize config
cat <<EOF > "${tmpdir}/config" cat << EOF > "${tmpdir}/config"
debug="$debug" debug="$debug"
root="$root" root="$root"
root_type="$root_type" root_type="$root_type"
@ -358,7 +358,7 @@ luks_args="$luks_args"
EOF EOF
# needed for devmgr # needed for devmgr
cat <<EOF > "${tmpdir}/etc/group" cat << EOF > "${tmpdir}/etc/group"
root:x:0: root:x:0:
tty:x:5: tty:x:5:
dialout:x:11: dialout:x:11:
@ -376,7 +376,7 @@ floppy:x:8:
EOF EOF
# needed for devmgr # needed for devmgr
cat <<EOF > "${tmpdir}/etc/passwd" cat << EOF > "${tmpdir}/etc/passwd"
root:x:0:0::/root:/bin/sh root:x:0:0::/root:/bin/sh
nobody:x:99:99::/:/bin/false nobody:x:99:99::/:/bin/false
EOF EOF
@ -390,12 +390,14 @@ EOF
create_initramfs() { create_initramfs() {
msg info "creating initramfs image" msg info "creating initramfs image"
{ {
( cd "$tmpdir" (
find . | cd "$tmpdir"
cpio -oH newc | find . \
gzip -9 ) | | cpio -oH newc \
tee "${script_dir}/initramfs-${kernel}.img.gz" | gzip -9
} >/dev/null 2>&1 || msg panic "failed to generate initramfs image" ) \
| tee "${script_dir}/initramfs-${kernel}.img.gz"
} > /dev/null 2>&1 || msg panic "failed to generate initramfs image"
} }
# check root # check root

76
init
View File

@ -22,9 +22,9 @@ mnt_pseudofs() {
use_mdev() { use_mdev() {
# setup hotplugger # setup hotplugger
if [ -e /proc/sys/kernel/hotplug ]; then if [ -e /proc/sys/kernel/hotplug ]; then
printf /sbin/mdev >/proc/sys/kernel/hotplug printf /sbin/mdev > /proc/sys/kernel/hotplug
else else
uevent mdev & uevent mdev &
fi fi
# trigger mdev # trigger mdev
@ -32,10 +32,10 @@ use_mdev() {
# trigger uevent for usb devices # trigger uevent for usb devices
for u in /sys/bus/usb/devices/*; do for u in /sys/bus/usb/devices/*; do
case ${u##*/} in case ${u##*/} in
[0-9]*-[0-9]*) [0-9]*-[0-9]*)
printf add > "${u}/uevent" printf add > "${u}/uevent"
;; ;;
esac esac
done done
@ -66,21 +66,21 @@ use_udev() {
# shell findfs # shell findfs
findfs_sh() { findfs_sh() {
case "${1%%=*}" in case "${1%%=*}" in
LABEL) LABEL)
printf "/dev/disk/by-label/%s\n" "${1##*=}" printf "/dev/disk/by-label/%s\n" "${1##*=}"
;; ;;
UUID) UUID)
printf "/dev/disk/by-uuid/%s\n" "${1##*=}" printf "/dev/disk/by-uuid/%s\n" "${1##*=}"
;; ;;
PARTUUID) PARTUUID)
printf "/dev/disk/by-partuuid/%s\n" "${1##*=}" printf "/dev/disk/by-partuuid/%s\n" "${1##*=}"
;; ;;
/dev/*) /dev/*)
printf "%s\n" "$1" printf "%s\n" "$1"
;; ;;
*) *)
panic "findfs option broken" panic "findfs option broken"
;; ;;
esac esac
} }
@ -100,7 +100,7 @@ unlock_luks() {
# manually trigger LVM if udev disabled # manually trigger LVM if udev disabled
trigger_lvm() { trigger_lvm() {
lvm vgchange --quiet --sysinit -a y >/dev/null lvm vgchange --quiet --sysinit -a y > /dev/null
} }
# mount rootfs to /mnt/root # mount rootfs to /mnt/root
@ -112,30 +112,30 @@ mnt_rootfs() {
# find root # find root
root="$(findfs_sh $root)" root="$(findfs_sh $root)"
# mount rootfs # mount rootfs
mount $mount_args "$root" /mnt/root || panic "failed to mount rootfs" mount $mount_args "$root" /mnt/root || panic "failed to mount rootfs"
} }
# kill and unmount # kill and unmount
cleanup() { cleanup() {
case "$devmgr" in case "$devmgr" in
mdev) mdev)
# stop mdev # stop mdev
{ printf "" >/proc/sys/kernel/hotplug || killall uevent; } >/dev/null 2>&1 { printf "" > /proc/sys/kernel/hotplug || killall uevent; } > /dev/null 2>&1
;; ;;
mdevd) mdevd)
# stop mdevd # stop mdevd
killall mdevd killall mdevd
;; ;;
udev) udev)
# stop udev # stop udev
udevadm control --exit udevadm control --exit
;; ;;
esac esac
# TODO re-do # TODO re-do
# if debug mode off then restore kernel logging # if debug mode off then restore kernel logging
[ "$debug" = 0 ] && printf 1 >/proc/sys/kernel/printk [ "$debug" = 0 ] && printf 1 > /proc/sys/kernel/printk
# unmount pseudofs's # unmount pseudofs's
umount /dev /sys /proc /tmp umount /dev /sys /proc /tmp
@ -154,11 +154,11 @@ boot_system() {
# TODO re-do # TODO re-do
if [ "$debug" = 1 ]; then if [ "$debug" = 1 ]; then
# debug shell commands # debug shell commands
set -x set -x
else else
# silence is golden # silence is golden
printf 0 >/proc/sys/kernel/printk printf 0 > /proc/sys/kernel/printk
fi fi
#parse_cmdline #parse_cmdline