tinyramfs/generate

394 lines
10 KiB
Plaintext
Raw Normal View History

2020-01-05 21:50:12 +03:00
#!/bin/sh
2020-01-05 21:01:39 +03:00
#
# tiny initramfs generation tool
2020-02-06 19:35:49 +03:00
# print message
msg() {
case "$1" in
2020-02-11 21:02:23 +03:00
info)
printf "info >> %s\n" "$2" >&2
;;
warn)
printf "warning >> %s\n" "$2" >&2
printf "do you want to continue? press enter or ctrl+c to exit\n"
read -r _
;;
panic)
printf "panic >> %s\n" "$2" >&2
exit 1
;;
2020-02-06 19:35:49 +03:00
esac
2020-01-30 16:53:17 +03:00
}
2020-01-05 21:01:39 +03:00
2020-02-14 20:12:45 +03:00
create_wrkdir() {
2020-02-13 05:18:53 +03:00
msg info "creating working directory"
2020-02-19 09:47:48 +03:00
if [ "$XDG_CACHE_HOME" ]; then
2020-02-14 20:12:45 +03:00
wrkdir="${XDG_CACHE_HOME}/initramfs.$$"
2020-02-19 09:47:48 +03:00
elif [ "$TMPDIR" ]; then
2020-02-14 20:12:45 +03:00
wrkdir="${TMPDIR}/initramfs.$$"
2020-02-11 01:48:51 +03:00
else
2020-02-14 20:12:45 +03:00
wrkdir="/tmp/initramfs.$$"
2020-02-11 01:48:51 +03:00
fi
2020-02-13 03:36:13 +03:00
2020-02-14 20:12:45 +03:00
mkdir "$wrkdir" || msg panic "failed to create working directory"
2020-02-11 01:48:51 +03:00
}
2020-02-14 20:12:45 +03:00
remove_wrkdir() {
2020-02-13 05:33:18 +03:00
msg info "removing working directory"
2020-02-14 20:12:45 +03:00
rm -rf "$wrkdir"
2020-01-30 16:53:17 +03:00
}
2020-02-02 15:19:20 +03:00
# change current directory to script directory if user haven't do it
check_currentdir() {
2020-02-16 02:53:33 +03:00
scriptdir=$(readlink -f $(dirname "$0"))
2020-02-14 20:12:45 +03:00
[ "$PWD" = "$scriptdir" ] || {
2020-02-13 05:33:18 +03:00
msg info "changing current directory to script directory"
2020-02-14 20:12:45 +03:00
cd "$scriptdir" || msg panic "failed to change directory"
2020-02-02 15:19:20 +03:00
}
}
2020-02-09 03:39:08 +03:00
install_requirements() {
msg info "installing requirements"
2020-02-18 10:53:03 +03:00
# install user specified binaries
2020-02-19 09:47:48 +03:00
[ "$binaries" ] && install_binary "$binaries"
# install util-linux binaries
2020-02-14 20:12:45 +03:00
[ "$util_linux" = 1 ] && install_binary mount blkid
# install mandatory binaries
2020-02-14 20:12:45 +03:00
install_binary busybox modprobe
2020-01-30 16:53:17 +03:00
}
create_structure() {
2020-02-07 15:46:34 +03:00
msg info "creating directory structure"
2020-02-09 03:39:08 +03:00
for dir in dev tmp var run etc usr/lib usr/bin mnt/root proc root sys; do
2020-02-14 20:12:45 +03:00
mkdir -p "${wrkdir}/${dir}"
2020-01-30 16:53:17 +03:00
done
}
2020-02-02 15:19:20 +03:00
# 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
2020-02-14 20:12:45 +03:00
# we change directory to wrkdir and make needed symlinks.
2020-01-30 16:53:17 +03:00
create_symlinks() {
2020-02-07 15:46:34 +03:00
msg info "creating symlinks"
2020-02-14 20:12:45 +03:00
( cd "$wrkdir" && {
2020-02-11 21:02:23 +03:00
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
2020-02-14 20:12:45 +03:00
cd "${wrkdir}/usr"
2020-02-11 21:02:23 +03:00
ln -s bin sbin
ln -s lib lib64
2020-01-30 16:53:17 +03:00
} )
}
2020-02-19 19:25:45 +03:00
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_args="${root_args:-$opts}"
fi
done < /etc/fstab
}
2020-01-30 16:53:17 +03:00
#parse_crypttab() {
# TODO parse crypttab
#}
2020-01-05 21:01:39 +03:00
2020-02-02 16:10:43 +03:00
install_mdev() {
2020-02-07 15:46:34 +03:00
msg info "installing mdev"
2020-02-14 20:12:45 +03:00
install -m644 mdev.conf -t "${wrkdir}/etc"
install -Dm755 storage-device -t "${wrkdir}/lib/mdev"
2020-01-30 16:53:17 +03:00
}
2020-01-19 00:01:21 +03:00
2020-02-02 16:10:43 +03:00
install_mdevd() {
2020-02-07 15:46:34 +03:00
msg info "installing mdevd"
2020-02-14 20:12:45 +03:00
install_binary mdevd mdevd-coldplug
install -m644 mdev.conf -t "${wrkdir}/etc"
install -Dm755 storage-device -t "${wrkdir}/lib/mdev"
2020-01-30 16:53:17 +03:00
}
2020-01-25 14:27:02 +03:00
2020-02-02 16:10:43 +03:00
install_udev() {
2020-02-07 15:46:34 +03:00
msg info "installing udev"
2020-02-14 20:12:45 +03:00
install_binary udevd udevadm dmsetup
2020-01-30 16:53:17 +03:00
# FIXME rewrite this piece of crap
2020-02-14 20:12:45 +03:00
find /usr/lib/udev -type f | grep -v "rc_keymaps\|hwdb.d" | cpio -pd "$wrkdir" > /dev/null 2>&1
2020-01-30 16:53:17 +03:00
}
2020-01-25 14:27:02 +03:00
2020-02-02 16:10:43 +03:00
install_lvm() {
2020-02-07 15:46:34 +03:00
msg info "installing LVM"
2020-02-14 20:12:45 +03:00
install_binary lvm
2020-02-06 19:21:00 +03:00
2020-02-18 10:53:03 +03:00
lvm_drivers="dm-thin-pool dm-multipath dm-snapshot dm-cache dm-log dm-mirror"
[ "$hostonly" = 1 ] && install_driver "$lvm_drivers"
2020-02-06 19:21:00 +03:00
2020-02-18 10:53:03 +03:00
# install lvm config
2020-02-08 23:05:03 +03:00
if [ "$lvm_conf" = 1 ]; then
2020-02-14 20:12:45 +03:00
install -Dm644 /etc/lvm/*.conf -t "${wrkdir}/etc/lvm" || msg panic "failed to install LVM config"
2020-02-08 23:05:03 +03:00
else
2020-02-14 20:12:45 +03:00
mkdir "${wrkdir}/etc/lvm"
cat << EOF > "${wrkdir}/etc/lvm/lvm.conf"
devices {
2020-02-18 10:53:03 +03:00
# block discard support
2020-02-08 23:05:03 +03:00
issue_discards = ${lvm_discard:-0}
}
global {
2020-02-18 10:53:03 +03:00
# disable lvmetad
2020-02-08 23:05:03 +03:00
use_lvmetad = 0
}
EOF
fi
2020-01-30 16:53:17 +03:00
}
2020-01-19 00:01:21 +03:00
2020-02-02 16:10:43 +03:00
install_luks() {
2020-02-07 15:46:34 +03:00
msg info "installing LUKS"
2020-02-14 20:12:45 +03:00
install_binary cryptsetup
2020-01-25 14:27:02 +03:00
2020-02-18 10:53:03 +03:00
luks_drivers="aes dm-crypt sha256 sha512 wp512 ecb lrw xts twofish serpent"
[ "$hostonly" = 1 ] && install_driver "$luks_drivers"
2020-02-06 19:21:00 +03:00
2020-02-18 10:53:03 +03:00
# avoid "locking directory missing" warning message
2020-02-14 20:12:45 +03:00
mkdir "${wrkdir}/run/cryptsetup"
2020-01-25 14:27:02 +03:00
# TODO get rid of this workaround
# workaround for luks2
2020-02-14 20:12:45 +03:00
install -s -m755 /usr/lib/libgcc_s.so.1 -t "${wrkdir}/usr/lib" || msg panic "failed to install LUKS libraries"
2020-01-25 14:27:02 +03:00
2020-02-06 01:29:59 +03:00
# block discard support
2020-01-28 18:13:42 +03:00
[ "$luks_discard" = 1 ] && luks_args="--allow-discards $luks_args"
2020-01-25 14:27:02 +03:00
2020-02-06 01:29:59 +03:00
# copy luks header
[ -f "$luks_header" ] && {
2020-02-14 20:12:45 +03:00
install -m400 "$luks_header" "${wrkdir}/root/luks_header" || msg panic "failed to copy LUKS header"
2020-02-11 21:02:23 +03:00
luks_args="--header=/root/luks_header $luks_args"
2020-02-06 01:29:59 +03:00
}
# copy luks keyfile
[ -f "$luks_keyfile" ] && {
2020-02-14 20:12:45 +03:00
install -m400 "$luks_keyfile" "${wrkdir}/root/luks_keyfile" || msg panic "failed to copy LUKS keyfile"
2020-02-11 21:02:23 +03:00
luks_args="--key-file=/root/luks_keyfile $luks_args"
2020-02-06 01:29:59 +03:00
}
2020-01-30 16:53:17 +03:00
}
2020-01-19 00:01:21 +03:00
2020-02-14 20:12:45 +03:00
install_driver() {
2020-02-16 04:32:13 +03:00
# check monolithic kernel(builtin drivers)
[ -d "${moddir}/${kernel}" ] || return 0
2020-02-19 15:39:43 +03:00
for driver in $@; do
2020-02-12 04:18:02 +03:00
# strip path and extension
driver="${driver##*/}"
driver="${driver%%.*}"
2020-02-18 10:53:03 +03:00
2020-02-15 05:17:34 +03:00
# TODO busybox modprobe doesn't support -S option
2020-02-18 10:53:03 +03:00
modprobe -S "$kernel" -D "$driver" 2> /dev/null | grep -v builtin | cut -d " " -f 2 | while read -r driver_dep; do
2020-02-14 20:12:45 +03:00
install -Dm644 "$driver_dep" "${wrkdir}${driver_dep}"
2020-02-06 19:21:00 +03:00
done
done
2020-02-11 21:47:36 +03:00
}
install_hostonly_drivers() {
msg info "installing hostonly drivers"
2020-02-19 09:47:48 +03:00
[ "$root_type" ] || msg panic "hostonly mode required root_type option to be configured"
2020-02-11 21:47:36 +03:00
# perform autodetection of drivers via /sys
2020-02-18 10:53:03 +03:00
install_driver "$(find /sys -name modalias -exec sort -u "{}" "+")"
2020-02-05 16:58:58 +03:00
2020-02-06 19:21:00 +03:00
# TODO autodetect root fs driver
# TODO separate root type option
# install root fs driver
2020-02-14 20:12:45 +03:00
install_driver "$root_type"
2020-02-06 19:21:00 +03:00
# install user specified drivers
2020-02-19 09:47:48 +03:00
[ "$drivers" ] && install_driver "$drivers"
2020-02-06 19:21:00 +03:00
}
install_all_drivers() {
2020-02-07 15:46:34 +03:00
msg info "installing all drivers"
2020-02-12 04:18:02 +03:00
modker="${moddir}/${kernel}/kernel"
2020-02-14 20:12:45 +03:00
install_driver \
2020-02-18 10:53:03 +03:00
"$(find \
2020-02-12 04:18:02 +03:00
"${modker}/arch" \
"${modker}/crypto" \
"${modker}/fs" \
"${modker}/lib" \
"${modker}/drivers/block" \
"${modker}/drivers/ata" \
"${modker}/drivers/md" \
"${modker}/drivers/scsi" \
"${modker}/drivers/usb/storage" \
"${modker}/drivers/usb/host" \
"${modker}/drivers/virtio" \
2020-02-18 10:53:03 +03:00
-type f 2> /dev/null)"
2020-02-06 19:21:00 +03:00
}
2020-01-30 16:53:17 +03:00
2020-02-06 19:21:00 +03:00
generate_depmod() {
2020-02-13 05:18:53 +03:00
msg info "running depmod"
modker="${moddir}/${kernel}"
2020-02-18 09:11:56 +03:00
cp "${modker}/modules.builtin" "${modker}/modules.order" "${wrkdir}/${modker}"
2020-02-14 20:12:45 +03:00
depmod -b "$wrkdir" "$kernel"
2020-01-30 16:53:17 +03:00
}
# TODO make strip optional
2020-02-14 20:12:45 +03:00
install_binary() {
2020-02-19 15:39:43 +03:00
for binary in $@; do
2020-02-11 21:02:23 +03:00
msg info "installing binary $binary"
# check binary existence
command -v "$binary" > /dev/null 2>&1 || msg panic "$binary doesn't exists"
2020-01-30 16:53:17 +03:00
2020-02-11 21:02:23 +03:00
# install and strip binary
2020-02-14 20:12:45 +03:00
install -s -m755 "$(command -v $binary)" -t "${wrkdir}/usr/bin"
2020-01-30 16:53:17 +03:00
# check statically linking
2020-02-11 21:02:23 +03:00
ldd "$(command -v $binary)" > /dev/null 2>&1 || continue
2020-01-30 16:53:17 +03:00
2020-02-11 21:02:23 +03:00
# install libraries
2020-02-18 10:53:03 +03:00
install_library "$binary"
2020-01-30 16:53:17 +03:00
done
}
2020-01-05 21:01:39 +03:00
2020-01-13 05:22:45 +03:00
# TODO make strip optional
2020-02-14 20:12:45 +03:00
install_library() {
2020-02-18 10:53:03 +03:00
ldd "$(command -v $1)" | sed -nre 's,.* (/.*lib.*/.*.so.*) .*,\1,p' -e 's,.*(/lib.*/ld.*.so.*) .*,\1,p' | while read -r library; do
2020-02-11 21:02:23 +03:00
# check symlink
if [ -h "$library" ]; then
# check lib already existence
2020-02-15 06:08:42 +03:00
if [ ! -e "${wrkdir}/usr/lib/${library##*/}" ] && [ ! -e "${wrkdir}/$(readlink -f $library)" ]; then
2020-02-11 21:02:23 +03:00
# regular
2020-02-14 20:12:45 +03:00
install -s -m755 "$(readlink -f $library)" -t "${wrkdir}/usr/lib"
2020-02-11 21:02:23 +03:00
# FIXME handle all symlinks
# symlink may link to symlink
2020-02-14 20:12:45 +03:00
[ -h "/usr/lib/$(readlink $library)" ] && cp -a "/usr/lib/$(readlink $library)" "${wrkdir}/usr/lib"
2020-02-11 21:02:23 +03:00
# symlink
2020-02-14 20:12:45 +03:00
cp -a "$library" "${wrkdir}/usr/lib"
2020-02-11 21:02:23 +03:00
fi
else
2020-02-14 20:12:45 +03:00
if [ ! -e "${wrkdir}/usr/lib/${library##*/}" ]; then
install -s -m755 "$library" -t "${wrkdir}/usr/lib"
2020-02-11 21:02:23 +03:00
fi
fi
2020-01-07 00:39:15 +03:00
done
2020-01-30 16:53:17 +03:00
}
2020-01-05 21:01:39 +03:00
2020-01-30 16:53:17 +03:00
install_files() {
2020-02-07 15:46:34 +03:00
msg info "installing files"
2020-02-06 19:21:00 +03:00
# FIXME eof broken
# initialize config
2020-02-14 20:12:45 +03:00
cat << EOF > "${wrkdir}/config"
2020-01-30 17:47:23 +03:00
debug="$debug"
2020-01-05 21:01:39 +03:00
root="$root"
2020-01-25 14:27:02 +03:00
root_type="$root_type"
root_args="$root_args"
2020-01-30 16:53:17 +03:00
devmgr="$devmgr"
2020-01-25 14:27:02 +03:00
#drivers="$drivers"
2020-02-14 20:12:45 +03:00
lvm="$lvm"
luks="$luks"
2020-01-25 14:27:02 +03:00
luks_root="$luks_root"
luks_args="$luks_args"
2020-01-05 21:01:39 +03:00
EOF
2020-02-06 19:21:00 +03:00
# needed for devmgr
2020-02-14 20:12:45 +03:00
cat << EOF > "${wrkdir}/etc/group"
2020-01-30 16:53:17 +03:00
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
2020-02-06 19:21:00 +03:00
# needed for devmgr
2020-02-14 20:12:45 +03:00
cat << EOF > "${wrkdir}/etc/passwd"
2020-01-30 16:53:17 +03:00
root:x:0:0::/root:/bin/sh
nobody:x:99:99::/:/bin/false
EOF
# install init script
2020-02-14 20:12:45 +03:00
install -m755 ./init -t "$wrkdir"
2020-01-30 16:53:17 +03:00
}
2020-01-30 18:34:33 +03:00
# create and compress cpio archive
2020-01-30 16:53:17 +03:00
create_initramfs() {
2020-02-07 15:46:34 +03:00
msg info "creating initramfs image"
2020-01-30 16:53:17 +03:00
{
2020-02-11 21:02:23 +03:00
(
2020-02-14 20:12:45 +03:00
cd "$wrkdir"
2020-02-18 10:53:03 +03:00
find . | cpio -oH newc | ${compress:-gzip -9}
) | tee "${scriptdir}/${initramfs:=initramfs-${kernel}}"
2020-02-11 21:02:23 +03:00
} > /dev/null 2>&1 || msg panic "failed to generate initramfs image"
2020-01-30 16:53:17 +03:00
}
2020-02-02 15:19:20 +03:00
# check root
2020-02-06 19:35:49 +03:00
[ "$(id -u)" = 0 ] || msg panic "must be run as root"
2020-01-30 16:53:17 +03:00
2020-02-14 20:12:45 +03:00
create_wrkdir
2020-02-13 03:36:13 +03:00
2020-02-14 20:12:45 +03:00
# remove wrkdir on exit or unexpected error
trap remove_wrkdir EXIT INT
2020-02-05 16:35:17 +03:00
2020-02-02 15:19:20 +03:00
check_currentdir
2020-01-30 16:53:17 +03:00
2020-02-13 05:18:53 +03:00
. ./config || msg panic "failed to source config"
2020-01-30 18:28:03 +03:00
[ "$debug" = 1 ] && {
2020-01-30 18:34:33 +03:00
# debug shell commands
2020-01-30 18:28:03 +03:00
set -x
2020-01-30 18:34:33 +03:00
# don't remove anything
2020-01-30 18:28:03 +03:00
trap : EXIT INT
}
2020-02-11 23:30:20 +03:00
kernel="${kernel:-$(uname -r)}"
2020-02-11 01:48:51 +03:00
moddir="/lib/modules"
2020-02-02 15:19:20 +03:00
2020-01-30 16:53:17 +03:00
create_structure
create_symlinks
2020-02-19 19:25:45 +03:00
[ "$fstab" = 1 ] && parse_fstab
2020-01-30 16:53:17 +03:00
#parse_crypttab
2020-02-09 03:39:08 +03:00
install_requirements
2020-02-06 19:21:00 +03:00
if [ "$hostonly" = 1 ]; then
2020-02-11 21:47:36 +03:00
install_hostonly_drivers
2020-02-06 19:21:00 +03:00
else
install_all_drivers
fi
generate_depmod
2020-01-30 16:53:17 +03:00
case "$devmgr" in
2020-02-19 10:53:43 +03:00
mdev) install_mdev ;;
2020-02-02 16:10:43 +03:00
mdevd) install_mdevd ;;
2020-02-19 10:53:43 +03:00
udev) install_udev ;;
*) msg panic "devmgr option broken" ;;
2020-01-30 16:53:17 +03:00
esac
2020-02-14 20:12:45 +03:00
[ "$luks" = 1 ] && install_luks
[ "$lvm" = 1 ] && install_lvm
2020-01-05 21:01:39 +03:00
2020-02-06 17:15:29 +03:00
install_files
2020-01-30 16:53:17 +03:00
create_initramfs
2020-01-05 21:01:39 +03:00
2020-02-18 10:53:03 +03:00
msg info "done! check out $initramfs"