tinyramfs/generate

467 lines
11 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-02-06 22:05:49 +05:30
msg() {
2020-02-22 23:16:57 +05:30
# print message
2020-02-06 22:05:49 +05:30
case "$1" in
2020-02-11 23:32:23 +05:30
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 22:05:49 +05:30
esac
2020-01-30 19:23:17 +05:30
}
2020-01-05 23:31:39 +05:30
2020-02-22 23:16:57 +05:30
usage() {
2020-02-24 13:39:37 +05:30
# TODO more options
2020-02-22 23:16:57 +05:30
cat << EOF
usage: $0 [options]
-o, --output <file> output file. default is $(readlink -f $(dirname "$0"))/initramfs-$(uname -r)
2020-02-24 13:39:37 +05:30
-f, --files <dir> files directory. default is $(readlink -f $(dirname "$0"))
2020-02-22 23:16:57 +05:30
EOF
}
parse_args() {
while [ "$1" ]; do
case "$1" in
-o | --output)
initramfs="$2"
shift 2
;;
-f | --files)
filesdir="$2"
shift 2
;;
-h | --help)
usage
exit 0
;;
*)
printf "%s\n\n" "invalid option: '$1'"
usage
exit 1
;;
esac
done
}
2020-02-24 13:39:37 +05:30
parse_conf() {
while read -r line; do
# ignore comments
if [ ! "${line##\#*}" ]; then
continue
# check if variable already exists via 'variable indirection' method
# if no exists then 'source' variable
# see https://stackoverflow.com/q/36235612
elif [ ! "$(eval printf "%s" "\"\$${line%%=*}\"")" ]; then
eval "$line"
fi
done < "${filesdir:-$(readlink -f $(dirname "$0"))}/config" || msg panic "failed to parse config"
}
2020-02-14 22:42:45 +05:30
create_wrkdir() {
2020-02-13 07:48:53 +05:30
msg info "creating working directory"
2020-02-22 23:16:57 +05:30
2020-02-19 12:17:48 +05:30
if [ "$XDG_CACHE_HOME" ]; then
2020-02-14 22:42:45 +05:30
wrkdir="${XDG_CACHE_HOME}/initramfs.$$"
2020-02-19 12:17:48 +05:30
elif [ "$TMPDIR" ]; then
2020-02-14 22:42:45 +05:30
wrkdir="${TMPDIR}/initramfs.$$"
2020-02-11 04:18:51 +05:30
else
2020-02-14 22:42:45 +05:30
wrkdir="/tmp/initramfs.$$"
2020-02-11 04:18:51 +05:30
fi
2020-02-13 06:06:13 +05:30
2020-02-14 22:42:45 +05:30
mkdir "$wrkdir" || msg panic "failed to create working directory"
2020-02-11 04:18:51 +05:30
}
2020-02-14 22:42:45 +05:30
remove_wrkdir() {
2020-02-13 08:03:18 +05:30
msg info "removing working directory"
2020-01-30 19:23:17 +05:30
2020-02-22 23:16:57 +05:30
rm -rf "$wrkdir"
2020-02-02 17:49:20 +05:30
}
2020-02-09 06:09:08 +05:30
install_requirements() {
msg info "installing requirements"
2020-02-18 13:23:03 +05:30
# install user specified binaries
2020-02-22 23:16:57 +05:30
[ "$binaries" ] && install_binary "$binaries"
# install util-linux binaries
2020-02-14 22:42:45 +05:30
[ "$util_linux" = 1 ] && install_binary mount blkid
# install mandatory binaries
2020-02-14 22:42:45 +05:30
install_binary busybox modprobe
2020-01-30 19:23:17 +05:30
}
create_structure() {
2020-02-07 18:16:34 +05:30
msg info "creating directory structure"
2020-02-22 23:16:57 +05:30
2020-02-09 06:09:08 +05:30
for dir in dev tmp var run etc usr/lib usr/bin mnt/root proc root sys; do
2020-02-14 22:42:45 +05:30
mkdir -p "${wrkdir}/${dir}"
2020-01-30 19:23:17 +05:30
done
}
create_symlinks() {
2020-02-22 23:16:57 +05:30
# 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 wrkdir and make needed symlinks.
2020-02-07 18:16:34 +05:30
msg info "creating symlinks"
2020-02-22 23:16:57 +05:30
2020-02-24 13:39:37 +05:30
# TODO remove grouping
2020-02-14 22:42:45 +05:30
( cd "$wrkdir" && {
2020-02-11 23:32:23 +05:30
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 22:42:45 +05:30
cd "${wrkdir}/usr"
2020-02-11 23:32:23 +05:30
ln -s bin sbin
ln -s lib lib64
2020-01-30 19:23:17 +05:30
} )
}
2020-02-02 18:40:43 +05:30
install_mdev() {
2020-02-07 18:16:34 +05:30
msg info "installing mdev"
2020-02-22 23:16:57 +05:30
2020-02-22 23:28:34 +05:30
install -m644 "${filesdir}/mdev.conf" -t "${wrkdir}/etc"
install -Dm755 "${filesdir}/storage-device" -t "${wrkdir}/lib/mdev"
2020-01-30 19:23:17 +05:30
}
2020-01-19 02:31:21 +05:30
2020-02-02 18:40:43 +05:30
install_mdevd() {
2020-02-07 18:16:34 +05:30
msg info "installing mdevd"
2020-02-22 23:16:57 +05:30
2020-02-14 22:42:45 +05:30
install_binary mdevd mdevd-coldplug
2020-02-22 23:16:57 +05:30
2020-02-22 23:28:34 +05:30
install -m644 "${filesdir}/mdev.conf" -t "${wrkdir}/etc"
install -Dm755 "${filesdir}/storage-device" -t "${wrkdir}/lib/mdev"
2020-01-30 19:23:17 +05:30
}
2020-01-25 16:57:02 +05:30
2020-02-02 18:40:43 +05:30
install_udev() {
2020-02-07 18:16:34 +05:30
msg info "installing udev"
2020-02-22 23:16:57 +05:30
2020-02-14 22:42:45 +05:30
install_binary udevd udevadm dmsetup
2020-01-30 19:23:17 +05:30
# FIXME rewrite this piece of crap
2020-02-14 22:42:45 +05:30
find /usr/lib/udev -type f | grep -v "rc_keymaps\|hwdb.d" | cpio -pd "$wrkdir" > /dev/null 2>&1
2020-01-30 19:23:17 +05:30
}
2020-01-25 16:57:02 +05:30
2020-02-02 18:40:43 +05:30
install_lvm() {
2020-02-07 18:16:34 +05:30
msg info "installing LVM"
2020-02-22 23:16:57 +05:30
2020-02-14 22:42:45 +05:30
install_binary lvm
2020-02-06 21:51:00 +05:30
2020-02-18 13:23:03 +05:30
lvm_drivers="dm-thin-pool dm-multipath dm-snapshot dm-cache dm-log dm-mirror"
[ "$hostonly" = 1 ] && install_driver "$lvm_drivers"
2020-02-06 21:51:00 +05:30
2020-02-18 13:23:03 +05:30
# install lvm config
2020-02-09 01:35:03 +05:30
if [ "$lvm_conf" = 1 ]; then
2020-02-14 22:42:45 +05:30
install -Dm644 /etc/lvm/*.conf -t "${wrkdir}/etc/lvm" || msg panic "failed to install LVM config"
2020-02-09 01:35:03 +05:30
else
2020-02-14 22:42:45 +05:30
mkdir "${wrkdir}/etc/lvm"
cat << EOF > "${wrkdir}/etc/lvm/lvm.conf"
devices {
2020-02-18 13:23:03 +05:30
# block discard support
2020-02-09 01:35:03 +05:30
issue_discards = ${lvm_discard:-0}
}
global {
2020-02-18 13:23:03 +05:30
# disable lvmetad
2020-02-09 01:35:03 +05:30
use_lvmetad = 0
}
EOF
fi
2020-01-30 19:23:17 +05:30
}
2020-01-19 02:31:21 +05:30
2020-02-02 18:40:43 +05:30
install_luks() {
2020-02-07 18:16:34 +05:30
msg info "installing LUKS"
2020-02-22 23:16:57 +05:30
2020-02-14 22:42:45 +05:30
install_binary cryptsetup
2020-01-25 16:57:02 +05:30
2020-02-18 13:23:03 +05:30
luks_drivers="aes dm-crypt sha256 sha512 wp512 ecb lrw xts twofish serpent"
[ "$hostonly" = 1 ] && install_driver "$luks_drivers"
2020-02-06 21:51:00 +05:30
2020-02-23 16:27:48 +05:30
# avoid "locking directory missing" warning message and libgcc_s.so.1 missing error
# see https://bugs.archlinux.org/task/56771
2020-02-14 22:42:45 +05:30
mkdir "${wrkdir}/run/cryptsetup"
2020-01-25 16:57:02 +05:30
2020-02-23 16:27:48 +05:30
[ -e /usr/lib/libgcc_s.so.1 ] && {
install -s -m755 /usr/lib/libgcc_s.so.1 -t "${wrkdir}/usr/lib" ||
msg panic "failed to install LUKS libraries"
}
2020-01-25 16:57:02 +05:30
2020-02-06 03:59:59 +05:30
# copy luks header
[ -f "$luks_header" ] && {
2020-02-14 22:42:45 +05:30
install -m400 "$luks_header" "${wrkdir}/root/luks_header" || msg panic "failed to copy LUKS header"
2020-02-11 23:32:23 +05:30
luks_args="--header=/root/luks_header $luks_args"
2020-02-06 03:59:59 +05:30
}
# copy luks keyfile
[ -f "$luks_keyfile" ] && {
2020-02-14 22:42:45 +05:30
install -m400 "$luks_keyfile" "${wrkdir}/root/luks_keyfile" || msg panic "failed to copy LUKS keyfile"
2020-02-11 23:32:23 +05:30
luks_args="--key-file=/root/luks_keyfile $luks_args"
2020-02-06 03:59:59 +05:30
}
2020-01-30 19:23:17 +05:30
}
2020-01-19 02:31:21 +05:30
2020-02-14 22:42:45 +05:30
install_driver() {
2020-02-16 07:02:13 +05:30
# check monolithic kernel(builtin drivers)
2020-02-22 23:16:57 +05:30
[ -d "${moddir}/${kernel}" ] || return
2020-02-16 07:02:13 +05:30
2020-02-22 23:16:57 +05:30
# we need splitting
# shellcheck disable=SC2068
2020-02-19 18:09:43 +05:30
for driver in $@; do
2020-02-22 23:16:57 +05:30
2020-02-12 06:48:02 +05:30
# strip path and extension
driver="${driver##*/}"
driver="${driver%%.*}"
2020-02-18 13:23:03 +05:30
2020-02-15 07:47:34 +05:30
# TODO busybox modprobe doesn't support -S option
2020-02-22 23:16:57 +05:30
modprobe -S "$kernel" -D "$driver" 2> /dev/null |
grep -v "builtin\|net" |
cut -d " " -f 2 |
while read -r driver_dep; do
2020-02-14 22:42:45 +05:30
install -Dm644 "$driver_dep" "${wrkdir}${driver_dep}"
2020-02-06 21:51:00 +05:30
done
done
2020-02-12 00:17:36 +05:30
}
install_hostonly_drivers() {
msg info "installing hostonly drivers"
2020-02-22 23:16:57 +05:30
2020-02-19 12:17:48 +05:30
[ "$root_type" ] || msg panic "hostonly mode required root_type option to be configured"
2020-02-12 00:17:36 +05:30
# perform autodetection of drivers via /sys
2020-02-18 13:23:03 +05:30
install_driver "$(find /sys -name modalias -exec sort -u "{}" "+")"
2020-02-05 19:28:58 +05:30
2020-02-06 21:51:00 +05:30
# TODO autodetect root fs driver
# TODO separate root type option
# install root fs driver
2020-02-14 22:42:45 +05:30
install_driver "$root_type"
2020-02-06 21:51:00 +05:30
# install user specified drivers
2020-02-19 12:17:48 +05:30
[ "$drivers" ] && install_driver "$drivers"
2020-02-06 21:51:00 +05:30
}
install_all_drivers() {
2020-02-07 18:16:34 +05:30
msg info "installing all drivers"
2020-02-22 23:16:57 +05:30
2020-02-12 06:48:02 +05:30
modker="${moddir}/${kernel}/kernel"
2020-02-14 22:42:45 +05:30
install_driver \
2020-02-18 13:23:03 +05:30
"$(find \
2020-02-12 06:48:02 +05:30
"${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 13:23:03 +05:30
-type f 2> /dev/null)"
2020-02-06 21:51:00 +05:30
}
2020-01-30 19:23:17 +05:30
2020-02-06 21:51:00 +05:30
generate_depmod() {
2020-02-13 07:48:53 +05:30
msg info "running depmod"
2020-02-22 23:16:57 +05:30
2020-02-13 07:48:53 +05:30
modker="${moddir}/${kernel}"
2020-02-24 13:39:37 +05:30
cp "${modker}/modules.builtin" "${modker}/modules.order" "${wrkdir}${modker}"
2020-02-14 22:42:45 +05:30
depmod -b "$wrkdir" "$kernel"
2020-01-30 19:23:17 +05:30
}
2020-02-14 22:42:45 +05:30
install_binary() {
2020-02-22 23:16:57 +05:30
# TODO make strip optional
# we need splitting
# shellcheck disable=SC2068
2020-02-19 18:09:43 +05:30
for binary in $@; do
2020-02-11 23:32:23 +05:30
msg info "installing binary $binary"
2020-02-22 23:16:57 +05:30
fullbin=$(command -v "$binary")
2020-02-24 13:39:37 +05:30
# check if binary exists
2020-02-22 23:16:57 +05:30
[ "$fullbin" ] || msg panic "$binary doesn't exists"
2020-01-30 19:23:17 +05:30
2020-02-11 23:32:23 +05:30
# install and strip binary
2020-02-22 23:16:57 +05:30
install -s -m755 "$fullbin" -t "${wrkdir}/usr/bin"
2020-01-30 19:23:17 +05:30
2020-02-22 23:16:57 +05:30
# check static
ldd "$fullbin" > /dev/null 2>&1 || continue
2020-01-30 19:23:17 +05:30
2020-02-11 23:32:23 +05:30
# install libraries
2020-02-18 13:23:03 +05:30
install_library "$binary"
2020-01-30 19:23:17 +05:30
done
}
2020-01-05 23:31:39 +05:30
2020-02-14 22:42:45 +05:30
install_library() {
2020-02-22 23:16:57 +05:30
# TODO make strip optional
wrkdirlib="${wrkdir}/usr/lib/"
# extract paths to libraries from ldd output
ldd $(command -v "$1") |
sed -nr \
-e 's,.* (/.*lib.*/.*.so.*) .*,\1,p' \
-e 's,.*(/lib.*/ld.*.so.*) .*,\1,p' |
while read -r library; do
namelib="${library##*/}"
reallib=$(readlink "$library")
fulllib=$(readlink -f "$library")
2020-02-11 23:32:23 +05:30
# check symlink
if [ -h "$library" ]; then
2020-02-22 23:16:57 +05:30
2020-02-24 13:39:37 +05:30
# check if library already exists
[ -e "${wrkdirlib}${fulllib##*/}" ] ||
[ -e "${wrkdirlib}${namelib}" ] ||
2020-02-22 23:16:57 +05:30
{
2020-02-11 23:32:23 +05:30
# regular
2020-02-22 23:16:57 +05:30
install -s -m755 "${fulllib}" -t "${wrkdirlib}"
2020-02-11 23:32:23 +05:30
# FIXME handle all symlinks
# symlink may link to symlink
2020-02-22 23:16:57 +05:30
[ -h "/usr/lib/${reallib}" ] &&
cp -a "/usr/lib/${reallib}" "${wrkdirlib}"
2020-02-11 23:32:23 +05:30
# symlink
2020-02-22 23:16:57 +05:30
cp -a "$library" "${wrkdirlib}"
}
2020-02-11 23:32:23 +05:30
else
2020-02-22 23:16:57 +05:30
[ -e "${wrkdirlib}${namelib}" ] ||
install -s -m755 "$library" -t "${wrkdirlib}"
2020-02-11 23:32:23 +05:30
fi
2020-01-07 03:09:15 +05:30
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-02-07 18:16:34 +05:30
msg info "installing files"
2020-02-21 14:27:07 +05:30
2020-02-22 23:16:57 +05:30
cat << EOF > "${wrkdir}/config"
debug="$debug"
init="$init"
root="$root"
root_type="$root_type"
root_opts="$root_opts"
devmgr="$devmgr"
#drivers
lvm="$lvm"
lvm_name="$lvm_name"
lvm_group="$lvm_group"
#lvm_discard
lvm_args="$lvm_args"
luks="$luks"
luks_root="$luks_root"
luks_name="$luks_name"
#luks_header
#luks_keyfile
luks_discard="$luks_discard"
luks_args="$luks_args"
EOF
2020-01-05 23:31:39 +05:30
2020-02-06 21:51:00 +05:30
# needed for devmgr
2020-02-14 22:42:45 +05:30
cat << EOF > "${wrkdir}/etc/group"
2020-01-30 19:23:17 +05:30
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 21:51:00 +05:30
# needed for devmgr
2020-02-14 22:42:45 +05:30
cat << EOF > "${wrkdir}/etc/passwd"
2020-01-30 19:23:17 +05:30
root:x:0:0::/root:/bin/sh
nobody:x:99:99::/:/bin/false
EOF
# install init script
2020-02-22 23:16:57 +05:30
install -m755 "${filesdir}/init" -t "$wrkdir"
2020-01-30 19:23:17 +05:30
}
create_initramfs() {
2020-02-07 18:16:34 +05:30
msg info "creating initramfs image"
2020-02-24 13:39:37 +05:30
# TODO add uncompressed option
# TODO remove grouping
2020-01-30 19:23:17 +05:30
{
2020-02-11 23:32:23 +05:30
(
2020-02-14 22:42:45 +05:30
cd "$wrkdir"
2020-02-18 13:23:03 +05:30
find . | cpio -oH newc | ${compress:-gzip -9}
2020-02-24 13:39:37 +05:30
) | tee "$initramfs"
2020-02-18 13:23:03 +05:30
2020-02-11 23:32:23 +05:30
} > /dev/null 2>&1 || msg panic "failed to generate initramfs image"
2020-01-30 19:23:17 +05:30
}
2020-02-02 17:49:20 +05:30
# check root
2020-02-06 22:05:49 +05:30
[ "$(id -u)" = 0 ] || msg panic "must be run as root"
2020-01-30 19:23:17 +05:30
2020-02-22 23:16:57 +05:30
parse_args "$@"
2020-02-24 13:39:37 +05:30
parse_conf
2020-02-22 23:16:57 +05:30
2020-02-24 13:39:37 +05:30
: "${kernel:=$(uname -r)}"
: "${moddir:=/lib/modules}"
: "${filesdir:=$(readlink -f $(dirname "$0"))}"
: "${initramfs:=${filesdir}/initramfs-${kernel}}"
2020-02-05 19:05:17 +05:30
2020-02-22 23:16:57 +05:30
[ "$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-02-22 23:16:57 +05:30
trap - EXIT INT
2020-01-30 20:58:03 +05:30
}
2020-02-24 13:39:37 +05:30
# remove wrkdir on exit or unexpected error
trap remove_wrkdir EXIT INT
2020-02-02 17:49:20 +05:30
2020-02-22 23:16:57 +05:30
create_wrkdir
2020-01-30 19:23:17 +05:30
create_structure
create_symlinks
2020-02-09 06:09:08 +05:30
install_requirements
2020-02-06 21:51:00 +05:30
if [ "$hostonly" = 1 ]; then
2020-02-12 00:17:36 +05:30
install_hostonly_drivers
2020-02-06 21:51:00 +05:30
else
install_all_drivers
fi
generate_depmod
2020-01-30 19:23:17 +05:30
case "$devmgr" in
2020-02-22 23:16:57 +05:30
udev) install_udev ;;
2020-02-19 13:23:43 +05:30
mdev) install_mdev ;;
2020-02-02 18:40:43 +05:30
mdevd) install_mdevd ;;
2020-02-19 13:23:43 +05:30
*) msg panic "devmgr option broken" ;;
2020-01-30 19:23:17 +05:30
esac
2020-02-22 23:16:57 +05:30
[ "$lvm" = 1 ] && [ -x "$(command -v lvm)" ] && install_lvm
2020-02-21 14:27:07 +05:30
[ "$luks" = 1 ] && [ -x "$(command -v cryptsetup)" ] && install_luks
2020-02-06 19:45:29 +05:30
install_files
2020-01-30 19:23:17 +05:30
create_initramfs
2020-01-05 23:31:39 +05:30
2020-02-24 13:39:37 +05:30
msg info "done! check out $initramfs"