tinyramfs/generate

287 lines
6.3 KiB
Plaintext
Raw Normal View History

2020-01-06 00:20:12 +05:30
#!/bin/sh
2020-01-05 23:31:39 +05:30
#
# tiny initramfs generation tool
2020-01-30 19:23:17 +05:30
panic() {
printf "panic >> %s\n" "$@"
2020-01-09 21:16:50 +05:30
exit 1
2020-01-30 19:23:17 +05:30
}
2020-01-05 23:31:39 +05:30
2020-01-30 19:23:17 +05:30
info() {
printf "info >> %s\n" "$@"
}
2020-01-05 23:31:39 +05:30
2020-01-30 19:23:17 +05:30
remove_tmpdir() {
# remove tmpdir
rm -rf "$tmpdir"
}
check_requirements() {
# check needed files
for f in ./config ./init ./busybox; do
[ -e "$f" ] || panic "$f doesn't exists"
done
# TODO handle busybox requirements ( busybox --list | grep ash )
# source config
. ./config
}
create_structure() {
# create FHS directory structure
for d in dev tmp var run etc usr/lib usr/bin mnt/root proc root sys; do
mkdir -p "${tmpdir}/${d}"
done
}
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 tmpdir and make needed symlinks.
( cd "$tmpdir" && {
ln -s usr/lib lib
ln -s usr/lib lib64
ln -s usr/bin bin
ln -s usr/bin sbin
ln -s ../run var/run
cd "$tmpdir/usr"
ln -s bin sbin
ln -s lib lib64
} )
}
#parse_fstab() {
# TODO parse fstab
2020-01-05 23:31:39 +05:30
#while [ "$use_fstab" -eq 1 ] && read fs dir type opts; do thing; done < /etc/fstab
2020-01-30 19:23:17 +05:30
#}
#parse_crypttab() {
# TODO parse crypttab
#}
2020-01-05 23:31:39 +05:30
2020-01-28 20:43:42 +05:30
# handle device managers
2020-01-30 19:23:17 +05:30
use_mdev() {
2020-01-28 20:43:42 +05:30
# install mdev
2020-01-30 19:23:17 +05:30
install -m644 mdev.conf -t "$tmpdir/etc"
install -Dm755 storage-device -t "$tmpdir/lib/mdev"
}
2020-01-19 02:31:21 +05:30
2020-01-30 19:23:17 +05:30
use_mdevd() {
# install mdevd
install_binaries mdevd mdevd-coldplug
install -m644 mdev.conf -t "$tmpdir/etc"
install -Dm755 storage-device -t "$tmpdir/lib/mdev"
}
2020-01-25 16:57:02 +05:30
2020-01-30 19:23:17 +05:30
use_udev() {
# install udev
install_binaries udevd udevadm dmsetup
# 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
}
2020-01-25 16:57:02 +05:30
2020-01-28 20:43:42 +05:30
# handle lvm
2020-01-30 19:23:17 +05:30
use_lvm() {
install_binaries lvm
# FIXME this code doesn't working with udev
2020-01-28 20:43:42 +05:30
#mkdir "$tmpdir/etc/lvm"
# use_lvmetad = 0 - avoid lvmetad missing warning message
#cat <<EOF > "$tmpdir/etc/lvm/lvmlocal.conf"
#local {
# issue_discards = ${lvm_discard:-0}
# use_lvmetad = 0
#}
#EOF
2020-01-25 16:57:02 +05:30
# TODO implement use_lvmconf
2020-01-30 19:23:17 +05:30
}
2020-01-19 02:31:21 +05:30
2020-01-25 16:57:02 +05:30
# handle luks
2020-01-30 19:23:17 +05:30
use_luks() {
install_binaries cryptsetup
2020-01-25 16:57:02 +05:30
# avoid locking directory missing warning message
2020-01-30 19:23:17 +05:30
mkdir "${tmpdir}/run/cryptsetup"
2020-01-25 16:57:02 +05:30
# TODO get rid of this workaround
# workaround for luks2
2020-01-30 19:23:17 +05:30
install -s -m755 /usr/lib/libgcc_s.so.1 -t "${tmpdir}/usr/lib"
2020-01-25 16:57:02 +05:30
2020-01-28 20:43:42 +05:30
[ "$luks_discard" = 1 ] && luks_args="--allow-discards $luks_args"
2020-01-25 16:57:02 +05:30
# TODO detached header
# TODO keyfile
2020-01-30 19:23:17 +05:30
}
2020-01-19 02:31:21 +05:30
2020-01-30 19:23:17 +05:30
# TODO implement hostonly mode
2020-01-05 23:31:39 +05:30
# install drivers
2020-01-30 19:23:17 +05:30
install_drivers() {
modker="${moddir}${kernel}"
find \
"${modker}/kernel/drivers/virtio" \
"${modker}/kernel/arch" \
"${modker}/kernel/crypto" \
"${modker}/kernel/fs" \
"${modker}/kernel/lib" \
"${modker}/kernel/drivers/block" \
"${modker}/kernel/drivers/ata" \
"${modker}/kernel/drivers/md" \
"${modker}/kernel/drivers/scsi" \
"${modker}/kernel/drivers/usb/storage" \
"${modker}/kernel/drivers/usb/host" \
-type f | cpio -pd "$tmpdir" >/dev/null 2>&1
# install list of drivers
cp "${modker}/modules.softdep" "${modker}/modules.builtin" "${modker}/modules.order" "${tmpdir}/${modker}"
# generate dependencies list of drivers
depmod -b "$tmpdir" "$kernel"
}
# TODO make strip optional
# handle binaries
install_binaries() {
for b in $(printf "%s\n" "$@" | tr " " "\n"); do
# check binary existence
command -v "$b" >/dev/null 2>&1 || panic "$b doesn't exists"
# install and strip binary
install -s -m755 "$(command -v $b)" -t "${tmpdir}/usr/bin"
# check statically linking
ldd "$(command -v $b)" >/dev/null 2>&1 || continue
# install libraries
install_libraries $b
done
}
2020-01-05 23:31:39 +05:30
2020-01-13 07:52:45 +05:30
# TODO make strip optional
2020-01-30 19:23:17 +05:30
# handle libraries
install_libraries() {
for l in $(ldd "$(command -v $1)" | sed -nre 's,.* (/.*lib.*/.*.so.*) .*,\1,p' -e 's,.*(/lib.*/ld.*.so.*) .*,\1,p'); do
2020-01-12 02:42:24 +05:30
# check symlink
2020-01-07 03:09:15 +05:30
if [ -h "$l" ]; then
2020-01-13 07:52:45 +05:30
# check lib already existence
2020-01-30 19:23:17 +05:30
if [ ! -e "${tmpdir}/usr/lib/${l##*/}" ] && [ ! -e "${tmpdir}/$(readlink -f $l)" ]; then
2020-01-12 02:42:24 +05:30
# regular
2020-01-30 19:23:17 +05:30
install -s -m755 "$(readlink -f $l)" -t "${tmpdir}/usr/lib"
# FIXME handle all symlinks
2020-01-28 20:43:42 +05:30
# symlink may link to symlink
2020-01-30 19:23:17 +05:30
[ -h "/usr/lib/$(readlink $l)" ] && cp -a "/usr/lib/$(readlink $l)" "${tmpdir}/usr/lib"
2020-01-12 02:42:24 +05:30
# symlink
2020-01-30 19:23:17 +05:30
cp -a "$l" "${tmpdir}/usr/lib"
2020-01-12 02:42:24 +05:30
fi
2020-01-07 03:09:15 +05:30
else
2020-01-30 19:23:17 +05:30
if [ ! -e "${tmpdir}/usr/lib/${l##*/}" ]; then
install -s -m755 "$l" -t "${tmpdir}/usr/lib"
2020-01-12 02:42:24 +05:30
fi
2020-01-07 03:09:15 +05:30
fi
done
2020-01-30 19:23:17 +05:30
}
2020-01-05 23:31:39 +05:30
2020-01-30 19:23:17 +05:30
install_files() {
2020-01-30 20:58:03 +05:30
# FIXME eof broken
2020-01-07 03:09:15 +05:30
# initialize config
2020-01-30 19:23:17 +05:30
cat <<EOF > "${tmpdir}/config"
2020-01-30 20:17:23 +05:30
debug="$debug"
2020-01-05 23:31:39 +05:30
root="$root"
2020-01-25 16:57:02 +05:30
root_type="$root_type"
root_args="$root_args"
2020-01-30 19:23:17 +05:30
devmgr="$devmgr"
2020-01-25 16:57:02 +05:30
#drivers="$drivers"
use_lvm="$use_lvm"
use_luks="$use_luks"
luks_root="$luks_root"
luks_header="$luks_header"
luks_keyfile="$luks_keyfile"
luks_args="$luks_args"
2020-01-05 23:31:39 +05:30
EOF
2020-01-30 19:23:17 +05:30
# needed for devmgr
cat <<EOF > "${tmpdir}/etc/group"
root:x:0:
tty:x:5:
dialout:x:11:
uucp:x:14:
kmem:x:3:
input:x:25:
video:x:13:
audio:x:12:
lp:x:10:
disk:x:9:
cdrom:x:16:
tape:x:6:
kvm:x:24:
floppy:x:8:
EOF
# needed for devmgr
cat <<EOF > "${tmpdir}/etc/passwd"
root:x:0:0::/root:/bin/sh
nobody:x:99:99::/:/bin/false
EOF
# install init script
install -m755 ./init -t "$tmpdir"
}
# TODO add more compession tools
2020-01-30 21:04:33 +05:30
# create and compress cpio archive
2020-01-30 19:23:17 +05:30
create_initramfs() {
{
( cd "$tmpdir" && {
find . | cpio -oH newc | gzip -9
2020-01-30 20:17:23 +05:30
} ) > "./initramfs-${kernel}.img.gz"
2020-01-30 19:23:17 +05:30
} >/dev/null 2>&1
[ "$?" = 0 ] || panic "failed to generate initramfs image"
}
2020-01-30 20:58:03 +05:30
# TODO cd to script directory
2020-01-30 19:23:17 +05:30
[ "$(id -u)" = 0 ] || panic "must be run as root"
# remove tmpdir on exit or unexpected error
2020-01-30 20:17:23 +05:30
trap remove_tmpdir EXIT INT
2020-01-30 19:23:17 +05:30
# variables
tmpdir="$(mktemp -d /tmp/initramfs.XXXXXXXX)" || panic "failed to create working directory"
kernel="$(uname -r)"
moddir="/lib/modules/"
check_requirements
2020-01-30 20:58:03 +05:30
# handle debug mode
[ "$debug" = 1 ] && {
2020-01-30 21:04:33 +05:30
# debug shell commands
2020-01-30 20:58:03 +05:30
set -x
2020-01-30 21:04:33 +05:30
# don't remove anything
2020-01-30 20:58:03 +05:30
trap : EXIT INT
}
2020-01-30 19:23:17 +05:30
create_structure
create_symlinks
#parse_fstab
#parse_crypttab
install_binaries $binaries
install_drivers
install_files
case "$devmgr" in
mdev) use_mdev ;;
mdevd) use_mdevd ;;
udev) use_udev ;;
*) panic "devmgr option broken" ;;
esac
[ "$use_luks" = 1 ] && use_luks
[ "$use_lvm" = 1 ] && use_lvm
2020-01-05 23:31:39 +05:30
2020-01-30 19:23:17 +05:30
create_initramfs
2020-01-05 23:31:39 +05:30
2020-01-30 19:23:17 +05:30
info "done! check out initramfs-${kernel}.img.gz"