tinyramfs/generate

440 lines
12 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
# print message
msg() {
case "$1" in
info)
2020-02-07 18:16:34 +05:30
printf "info >> %s\n" "$2" >&2
2020-02-06 22:05:49 +05:30
;;
2020-02-09 06:09:08 +05:30
warn)
printf "warning >> %s\n" "$2" >&2
printf "do you want to continue? press enter or ctrl+c to exit\n"
read -r _
;;
2020-02-06 22:05:49 +05:30
panic)
2020-02-07 18:16:34 +05:30
printf "panic >> %s\n" "$2" >&2
2020-02-06 22:05:49 +05:30
exit 1
;;
esac
2020-01-30 19:23:17 +05:30
}
2020-01-05 23:31:39 +05:30
2020-02-02 17:49:20 +05:30
# remove tmpdir
2020-01-30 19:23:17 +05:30
remove_tmpdir() {
2020-02-07 18:16:34 +05:30
msg info "removing work dir"
2020-01-30 19:23:17 +05:30
rm -rf "$tmpdir"
}
2020-02-02 17:49:20 +05:30
# change current directory to script directory if user haven't do it
check_currentdir() {
2020-02-09 06:09:08 +05:30
script_dir=$(dirname $(readlink -f "$0"))
2020-02-02 17:49:20 +05:30
[ "$PWD" = "$script_dir" ] || {
2020-02-09 01:35:03 +05:30
msg info "changing directory to script dir"
2020-02-06 22:05:49 +05:30
cd "$script_dir" || msg panic "failed to change directory"
2020-02-02 17:49:20 +05:30
}
}
# check needed files
2020-01-30 19:23:17 +05:30
check_requirements() {
2020-02-07 18:16:34 +05:30
msg info "checking requirements"
2020-01-30 19:23:17 +05:30
2020-02-09 06:09:08 +05:30
# check busybox installed
command -v busybox >/dev/null 2>&1 && {
# check busybox supports CONFIG_FEATURE_INSTALLER
busybox --help | grep -q "\-\-install \[\-s\]" || msg panic "recompile busybox with CONFIG_FEATURE_INSTALLER"
# check requirements for init
for busybox_dep in mdev uevent switch_root; do
busybox $busybox_dep --help >/dev/null 2>&1 || msg panic "recompile busybox with $busybox_dep"
done
} || msg panic "busybox doesn't installed"
# check kmod modprobe installed
command -v modprobe >/dev/null 2>&1 && {
# busybox modprobe doesn't supported(yet)
modprobe --version 2>&1 | grep -q "kmod" || msg panic "kmod modprobe version doesn't installed"
} || msg panic "modprobe doesn't installed"
# check util-linux tools
[ "$use_util_linux" = 1 ] && {
# check mount installed
if command -v mount >/dev/null 2>&1; then
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"
use_util_linux=0
}
else
msg panic "mount doesn't installed"
fi
# check blkid installed
if command -v blkid >/dev/null 2>&1; then
blkid --version 2>&1 | grep -q "util-linux" || {
msg warning "util-linux blkid version doesn't installed. PARTUUID support will be missing"
use_util_linux=0
}
else
msg panic "blkid doesn't installed"
fi
}
2020-02-09 06:09:08 +05:30
}
# install requirements
2020-02-09 06:09:08 +05:30
install_requirements() {
msg info "installing requirements"
# install user specified binaries
[ -n "$binaries" ] && install_binaries $binaries
# install util-linux binaries
[ "$use_util_linux" = 1 ] && install_binaries mount blkid
# install mandatory binaries
install_binaries busybox modprobe
2020-01-30 19:23:17 +05:30
}
2020-02-02 17:49:20 +05:30
# create FHS directory structure
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-09 06:09:08 +05:30
for dir in dev tmp var run etc usr/lib usr/bin mnt/root proc root sys; do
mkdir -p "${tmpdir}/${dir}"
2020-01-30 19:23:17 +05:30
done
}
2020-02-02 17:49:20 +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 tmpdir and make needed symlinks.
2020-01-30 19:23:17 +05:30
create_symlinks() {
2020-02-07 18:16:34 +05:30
msg info "creating symlinks"
2020-01-30 19:23:17 +05:30
( 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
2020-02-02 18:30:23 +05:30
cd "${tmpdir}/usr"
2020-01-30 19:23:17 +05:30
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-02-02 17:49:20 +05:30
# install mdev
2020-02-02 18:40:43 +05:30
install_mdev() {
2020-02-07 18:16:34 +05:30
msg info "installing mdev"
2020-02-02 18:30:23 +05:30
install -m644 mdev.conf -t "${tmpdir}/etc"
install -Dm755 storage-device -t "${tmpdir}/lib/mdev"
2020-01-30 19:23:17 +05:30
}
2020-01-19 02:31:21 +05:30
2020-02-02 17:49:20 +05:30
# install mdevd
2020-02-02 18:40:43 +05:30
install_mdevd() {
2020-02-07 18:16:34 +05:30
msg info "installing mdevd"
2020-01-30 19:23:17 +05:30
install_binaries mdevd mdevd-coldplug
2020-02-02 18:30:23 +05:30
install -m644 mdev.conf -t "${tmpdir}/etc"
install -Dm755 storage-device -t "${tmpdir}/lib/mdev"
2020-01-30 19:23:17 +05:30
}
2020-01-25 16:57:02 +05:30
2020-02-02 17:49:20 +05:30
# install udev
2020-02-02 18:40:43 +05:30
install_udev() {
2020-02-07 18:16:34 +05:30
msg info "installing udev"
2020-01-30 19:23:17 +05:30
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-02-02 18:40:43 +05:30
install_lvm() {
2020-02-07 18:16:34 +05:30
msg info "installing LVM"
2020-01-30 19:23:17 +05:30
install_binaries lvm
2020-02-06 21:51:00 +05:30
# if hostonly mode enabled install only needed drivers
[ "$hostonly" = 1 ] && {
for lvm_driver in dm-thin-pool dm-multipath dm-snapshot dm-cache dm-log dm-mirror; do
2020-02-07 00:47:33 +05:30
for lvm_driver_dep in $(modprobe -D "$lvm_driver" 2>/dev/null | grep -v builtin | cut -d " " -f 2); do
2020-02-07 18:16:34 +05:30
install -Dm644 "$lvm_driver_dep" "${tmpdir}${lvm_driver_dep}"
2020-02-06 21:51:00 +05:30
done
done
}
2020-02-09 01:35:03 +05:30
if [ "$lvm_conf" = 1 ]; then
install -Dm644 /etc/lvm/*.conf -t "${tmpdir}/etc/lvm" || msg panic "failed to install LVM config"
else
mkdir "${tmpdir}/etc/lvm"
cat <<EOF > "${tmpdir}/etc/lvm/lvm.conf"
devices {
2020-02-09 01:35:03 +05:30
issue_discards = ${lvm_discard:-0}
}
global {
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-01-25 16:57:02 +05:30
# handle luks
2020-02-02 18:40:43 +05:30
install_luks() {
2020-02-07 18:16:34 +05:30
msg info "installing LUKS"
2020-01-30 19:23:17 +05:30
install_binaries cryptsetup
2020-01-25 16:57:02 +05:30
2020-02-06 21:51:00 +05:30
# if hostonly mode enabled install only needed drivers
[ "$hostonly" = 1 ] && {
for luks_driver in aes dm-crypt sha256 sha512 wp512 ecb lrw xts twofish serpent; do
2020-02-07 00:47:33 +05:30
for luks_driver_dep in $(modprobe -D "$luks_driver" 2>/dev/null | grep -v builtin | cut -d " " -f 2); do
2020-02-07 18:16:34 +05:30
install -Dm644 "$luks_driver_dep" "${tmpdir}${luks_driver_dep}"
2020-02-06 21:51:00 +05:30
done
done
}
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-02-07 18:16:34 +05:30
install -s -m755 /usr/lib/libgcc_s.so.1 -t "${tmpdir}/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
# block discard support
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
2020-02-06 03:59:59 +05:30
# copy luks header
[ -f "$luks_header" ] && {
2020-02-07 18:16:34 +05:30
install -m400 "$luks_header" "${tmpdir}/root/luks_header" || msg panic "failed to copy LUKS header"
2020-02-06 03:59:59 +05:30
luks_args="--header=/root/luks_header $luks_args"
}
# copy luks keyfile
[ -f "$luks_keyfile" ] && {
2020-02-07 18:16:34 +05:30
install -m400 "$luks_keyfile" "${tmpdir}/root/luks_keyfile" || msg panic "failed to copy LUKS keyfile"
2020-02-06 03:59:59 +05:30
luks_args="--key-file=/root/luks_keyfile $luks_args"
}
2020-01-30 19:23:17 +05:30
}
2020-01-19 02:31:21 +05:30
2020-01-05 23:31:39 +05:30
# install drivers
2020-01-30 19:23:17 +05:30
install_drivers() {
2020-02-07 18:16:34 +05:30
msg info "installing hostonly drivers"
2020-02-06 22:05:49 +05:30
[ -n "$root_type" ] || msg panic "hostonly mode required root_type option to be configured"
2020-02-05 19:28:58 +05:30
2020-02-06 21:51:00 +05:30
# perform autodetection of drivers via /sys
2020-02-09 06:09:08 +05:30
find /sys/devices -name modalias -exec sort -u "{}" "+" | while read -r driver; do
2020-02-07 00:47:33 +05:30
for driver_dep in $(modprobe -D "$driver" 2>/dev/null | grep -v builtin | cut -d " " -f 2); do
2020-02-07 18:16:34 +05:30
install -Dm644 "$driver_dep" "${tmpdir}${driver_dep}"
2020-02-06 21:51:00 +05:30
done
done
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-07 00:47:33 +05:30
for root_driver in $(modprobe -D "$root_type" 2>/dev/null | grep -v builtin | cut -d " " -f 2); do
2020-02-07 18:16:34 +05:30
install -Dm644 "$root_driver" "${tmpdir}${root_driver}"
2020-02-06 21:51:00 +05:30
done
# install user specified drivers
[ -n "$drivers" ] && {
2020-02-09 06:09:08 +05:30
printf "%s\n" "$drivers" | while read -r custom_driver; do
2020-02-07 00:47:33 +05:30
for custom_driver_dep in $(modprobe -D "$custom_driver" 2>/dev/null | grep -v builtin | cut -d " " -f 2); do
2020-02-07 18:16:34 +05:30
install -Dm644 "$custom_driver_dep" "${tmpdir}${custom_driver_dep}"
2020-02-06 03:32:46 +05:30
done
2020-02-06 21:51:00 +05:30
done
}
}
# find and install all drivers
install_all_drivers() {
2020-02-07 18:16:34 +05:30
msg info "installing all drivers"
2020-02-06 21:51:00 +05:30
find \
"${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" \
"${modker}/kernel/drivers/virtio" \
-type f | cpio -pd "$tmpdir" >/dev/null 2>&1
}
2020-01-30 19:23:17 +05:30
2020-02-06 21:51:00 +05:30
# generate "modules" files
generate_depmod() {
2020-02-07 18:16:34 +05:30
msg info "generating dependendies list of drivers"
2020-01-30 19:23:17 +05:30
# 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() {
2020-02-09 06:09:08 +05:30
printf "%s\n" "$@" | while read -r binary; do
msg info "installing binary $binary"
2020-01-30 19:23:17 +05:30
# check binary existence
2020-02-09 06:09:08 +05:30
command -v "$binary" >/dev/null 2>&1 || msg panic "$binary doesn't exists"
2020-01-30 19:23:17 +05:30
# install and strip binary
2020-02-09 06:09:08 +05:30
install -s -m755 "$(command -v $binary)" -t "${tmpdir}/usr/bin"
2020-01-30 19:23:17 +05:30
# check statically linking
2020-02-09 06:09:08 +05:30
ldd "$(command -v $binary)" >/dev/null 2>&1 || continue
2020-01-30 19:23:17 +05:30
# install libraries
2020-02-09 06:09:08 +05:30
install_libraries $binary
2020-01-30 19:23:17 +05:30
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() {
2020-02-09 06:09:08 +05:30
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"
2020-01-12 02:42:24 +05:30
# check symlink
2020-02-09 06:09:08 +05:30
if [ -h "$library" ]; then
2020-01-13 07:52:45 +05:30
# check lib already existence
2020-02-09 06:09:08 +05:30
if [ ! -e "${tmpdir}/usr/lib/${library##*/}" ] && [ ! -e "${tmpdir}/$(readlink -f $library)" ]; then
2020-01-12 02:42:24 +05:30
# regular
2020-02-09 06:09:08 +05:30
install -s -m755 "$(readlink -f $library)" -t "${tmpdir}/usr/lib"
2020-01-30 19:23:17 +05:30
# FIXME handle all symlinks
2020-01-28 20:43:42 +05:30
# symlink may link to symlink
2020-02-09 06:09:08 +05:30
[ -h "/usr/lib/$(readlink $library)" ] && cp -a "/usr/lib/$(readlink $library)" "${tmpdir}/usr/lib"
2020-01-30 19:23:17 +05:30
2020-01-12 02:42:24 +05:30
# symlink
2020-02-09 06:09:08 +05:30
cp -a "$library" "${tmpdir}/usr/lib"
2020-01-12 02:42:24 +05:30
fi
2020-01-07 03:09:15 +05:30
else
2020-02-09 06:09:08 +05:30
if [ ! -e "${tmpdir}/usr/lib/${library##*/}" ]; then
install -s -m755 "$library" -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-02-07 18:16:34 +05:30
# install important files used by init
2020-01-30 19:23:17 +05:30
install_files() {
2020-02-07 18:16:34 +05:30
msg info "installing files"
2020-02-06 21:51:00 +05:30
# FIXME eof broken
# initialize config
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_args="$luks_args"
2020-01-05 23:31:39 +05:30
EOF
2020-02-06 21:51:00 +05:30
# needed for devmgr
cat <<EOF > "${tmpdir}/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
cat <<EOF > "${tmpdir}/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
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() {
2020-02-07 18:16:34 +05:30
msg info "creating initramfs image"
# TODO rewrite this ugly mess | dash doesn't working here
2020-01-30 19:23:17 +05:30
{
( cd "$tmpdir" && {
find . | cpio -oH newc | gzip -9
2020-02-02 18:30:23 +05:30
} ) > "${script_dir}/initramfs-${kernel}.img.gz"
2020-01-30 19:23:17 +05:30
} >/dev/null 2>&1
2020-02-06 22:05:49 +05:30
[ "$?" = 0 ] || 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-05 19:05:17 +05:30
# remove tmpdir on exit or unexpected error
trap remove_tmpdir EXIT INT
2020-02-02 17:49:20 +05:30
check_currentdir
2020-01-30 19:23:17 +05:30
2020-02-02 17:49:20 +05:30
# source config
2020-02-06 22:05:49 +05:30
. ./config || msg panic "./config doesn't exists"
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-02-02 17:49:20 +05:30
# variables
2020-02-06 22:05:49 +05:30
tmpdir="$(mktemp -d /tmp/initramfs.XXXXXXXX)" || msg panic "failed to create working directory"
2020-02-02 17:49:20 +05:30
kernel="$(uname -r)"
moddir="/lib/modules/"
2020-02-06 21:51:00 +05:30
modker="${moddir}${kernel}"
2020-02-02 17:49:20 +05:30
check_requirements
2020-01-30 19:23:17 +05:30
create_structure
create_symlinks
#parse_fstab
#parse_crypttab
2020-02-09 06:09:08 +05:30
install_requirements
2020-02-06 21:51:00 +05:30
if [ "$hostonly" = 1 ]; then
install_drivers
else
install_all_drivers
fi
generate_depmod
2020-01-30 19:23:17 +05:30
2020-02-02 17:49:20 +05:30
# handle device manager
2020-01-30 19:23:17 +05:30
case "$devmgr" in
2020-02-02 18:40:43 +05:30
mdev) install_mdev ;;
mdevd) install_mdevd ;;
udev) install_udev ;;
2020-02-06 22:05:49 +05:30
*) msg panic "devmgr option broken" ;;
2020-01-30 19:23:17 +05:30
esac
2020-02-02 18:40:43 +05:30
[ "$use_luks" = 1 ] && install_luks
[ "$use_lvm" = 1 ] && install_lvm
2020-01-05 23:31:39 +05:30
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-06 22:05:49 +05:30
msg info "done! check out initramfs-${kernel}.img.gz"