tinyramfs/tinyramfs

491 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
msg() {
case "$1" in
2020-02-11 23:32:23 +05:30
info)
printf "info >> %s\n" "$2" >&2
;;
warn)
2020-03-01 16:08:15 +05:30
printf "warning >> %s\n" "$2" \
"are you sure you want to continue?" \
"press enter to continue or ctrl+c to exit" >&2
2020-02-11 23:32:23 +05:30
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() {
cat << EOF
2020-03-09 00:07:19 +05:30
usage: $0 [option...]
2020-03-08 00:54:59 +05:30
-o, --output <file> set initramfs image name
-c, --config <file> set config file path
-m, --moddir <dir> set drivers directory
-k, --kernel <ver> set kernel version
-F, --files <dir> set files directory
-d, --debug enable debug mode
2020-03-08 06:48:07 +05:30
-f, --force overwrite initramfs image
2020-02-22 23:16:57 +05:30
EOF
}
parse_args() {
while [ "$1" ]; do
case "$1" in
-o | --output)
2020-02-25 18:10:07 +05:30
_initramfs="${2:?}"
2020-02-22 23:16:57 +05:30
shift 2
;;
2020-02-25 18:36:58 +05:30
-c | --config)
_config="${2:?}"
shift 2
;;
-m | --moddir)
_moddir="${2:?}"
shift 2
;;
-k | --kernel)
_kernel="${2:?}"
shift 2
;;
2020-03-01 20:09:12 +05:30
-F | --files)
2020-02-25 18:10:07 +05:30
_filesdir="${2:?}"
2020-02-24 21:06:27 +05:30
shift 2
;;
2020-02-25 18:36:58 +05:30
-d | --debug)
2020-03-03 22:15:55 +05:30
_debug=1
shift 1
2020-02-22 23:16:57 +05:30
;;
2020-03-01 19:12:57 +05:30
-f | --force)
2020-03-03 22:15:55 +05:30
_force=1
shift 1
2020-03-01 19:12:57 +05:30
;;
2020-02-22 23:16:57 +05:30
-h | --help)
usage
exit 0
;;
*)
printf "%s\n\n" "invalid option: '$1'"
usage
exit 1
;;
esac
done
}
2020-03-08 06:48:07 +05:30
prepare_environment() {
2020-03-09 18:52:21 +05:30
for _file in $_config /etc/tinyramfs/config ./config; do
[ -f "$_file" ] && . "$_file" && break
done || msg panic "failed to source config"
2020-03-08 06:48:07 +05:30
2020-03-09 18:52:21 +05:30
for _dir in $_filesdir /usr/share/tinyramfs ./usr/share/tinyramfs; do
[ -d "$_dir" ] && filesdir="$_dir" && break
done || msg panic "failed to locate required files"
2020-02-24 13:39:37 +05:30
2020-02-25 18:36:58 +05:30
kernel="${_kernel:-${kernel:-$(uname -r)}}"
moddir="${_moddir:-${moddir:-/lib/modules}}"
debug="${_debug:-${debug:-0}}"
2020-03-01 19:12:57 +05:30
force="${_force:-${force:-0}}"
2020-03-08 06:48:07 +05:30
initramfs="${_initramfs:-${initramfs:-/tmp/initramfs-${kernel}}}"
2020-02-27 17:24:30 +05:30
modker="${moddir}/${kernel}"
2020-02-24 13:39:37 +05:30
}
2020-02-25 00:26:12 +05:30
create_workdir() {
2020-02-13 07:48:53 +05:30
msg info "creating working directory"
2020-02-22 23:16:57 +05:30
2020-02-25 01:34:24 +05:30
workdir="${XDG_CACHE_HOME:-${TMPDIR:-/tmp}}/initramfs.$$"
mkdir -p "$workdir" ||
msg panic "failed to create working directory"
2020-02-11 04:18:51 +05:30
}
2020-02-25 00:26:12 +05:30
remove_workdir() {
2020-02-13 08:03:18 +05:30
msg info "removing working directory"
2020-01-30 19:23:17 +05:30
2020-02-25 00:26:12 +05:30
rm -rf "$workdir"
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
[ "$binaries" ] &&
2020-02-29 17:16:13 +05:30
for _binary in $binaries; do
install_binary "$_binary"
2020-02-25 16:21:20 +05:30
done
# install util-linux binaries
[ "$util_linux" = 1 ] &&
2020-02-29 17:16:13 +05:30
for _binary in mount blkid; do
install_binary "$_binary"
2020-02-25 16:21:20 +05:30
done
2020-03-08 06:48:07 +05:30
# install required binaries
2020-02-29 17:16:13 +05:30
for _binary in busybox modprobe; do
install_binary "$_binary"
2020-02-25 16:21:20 +05:30
done
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-03-01 19:12:57 +05:30
for _dir in dev tmp var run etc usr/lib usr/bin mnt/root proc root sys; do
mkdir -p "${workdir}/${_dir}"
2020-01-30 19:23:17 +05:30
done
}
create_symlinks() {
2020-02-07 18:16:34 +05:30
msg info "creating symlinks"
2020-02-22 23:16:57 +05:30
2020-02-28 20:08:03 +05:30
ln -s usr/lib "${workdir}/lib"
ln -s usr/lib "${workdir}/lib64"
ln -s usr/bin "${workdir}/bin"
ln -s usr/bin "${workdir}/sbin"
ln -s ../run "${workdir}/var/run"
ln -s bin "${workdir}/usr/sbin"
ln -s lib "${workdir}/usr/lib64"
2020-01-30 19:23:17 +05:30
}
install_devmgr() {
2020-02-25 16:21:20 +05:30
msg info "installing device manager"
case "$devmgr" in
udev)
2020-02-29 17:16:13 +05:30
for _binary in udevd udevadm dmsetup; do
install_binary "$_binary"
2020-02-25 16:21:20 +05:30
done
2020-01-19 02:31:21 +05:30
find /usr/lib/udev \
! -path "*rc_keymaps*" \
! -path "*hwdb.d*" \
-type f |
2020-02-22 23:16:57 +05:30
2020-03-08 05:58:00 +05:30
cpio -pd "$workdir" > /dev/null 2>&1
;;
mdev)
2020-02-27 22:25:23 +05:30
install -m644 "${filesdir}/mdev.conf" "${workdir}/etc/mdev.conf"
install -Dm755 "${filesdir}/storage-device" "${workdir}/lib/mdev/storage-device"
;;
mdevd)
2020-02-29 17:16:13 +05:30
for _binary in mdevd mdevd-coldplug; do
install_binary "$_binary"
2020-02-25 16:21:20 +05:30
done
2020-02-27 22:25:23 +05:30
install -m644 "${filesdir}/mdev.conf" "${workdir}/etc/mdev.conf"
install -Dm755 "${filesdir}/storage-device" "${workdir}/lib/mdev/storage-device"
;;
esac
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
# install lvm config
2020-03-08 05:58:00 +05:30
if [ "$lvm_config" = 1 ]; then
mkdir -p "${workdir}/etc/lvm"
2020-02-27 22:25:23 +05:30
2020-03-08 05:58:00 +05:30
cp /etc/lvm/*.conf "${workdir}/etc/lvm"
2020-02-09 01:35:03 +05:30
else
mkdir -p "${workdir}/etc/lvm"
2020-02-27 22:25:23 +05:30
2020-02-25 00:26:12 +05:30
cat << EOF > "${workdir}/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-03-08 08:59:14 +05:30
# avoid libgcc_s.so.1 missing error
2020-02-23 16:27:48 +05:30
# see https://bugs.archlinux.org/task/56771
2020-02-25 16:21:20 +05:30
[ -e /usr/lib/libgcc_s.so.1 ] &&
install_library /usr/lib/libgcc_s.so.1
2020-01-25 16:57:02 +05:30
2020-02-06 03:59:59 +05:30
# copy luks header
[ -f "$luks_header" ] &&
2020-03-08 05:58:00 +05:30
install -m400 "$luks_header" "${workdir}/root/luks_header"
2020-02-06 03:59:59 +05:30
# copy luks keyfile
[ -f "$luks_keyfile" ] &&
2020-03-08 05:58:00 +05:30
install -m400 "$luks_keyfile" "${workdir}/root/luks_keyfile"
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-25 16:21:20 +05:30
driver="$1"
2020-02-18 13:23:03 +05:30
2020-02-25 16:21:20 +05:30
modprobe -S "$kernel" -D "$driver" 2> /dev/null |
2020-02-22 23:16:57 +05:30
2020-02-29 17:16:13 +05:30
while read -r driver; do
2020-02-26 01:15:21 +05:30
2020-02-28 23:27:03 +05:30
# strip unneeded stuff
2020-02-29 17:16:13 +05:30
driver="${driver##*builtin*}"
driver="${driver##*net*}"
driver="${driver#insmod }"
2020-02-25 16:21:20 +05:30
2020-03-01 20:09:12 +05:30
# exclude user specified drivers
[ "$drivers_exclude" ] &&
for _exclude_driver in $drivers_exclude; do
driver="${driver##*${_exclude_driver}*}"
done
2020-02-27 23:47:05 +05:30
# check empty
2020-02-29 17:16:13 +05:30
[ "$driver" ] || continue
2020-02-27 23:47:05 +05:30
2020-02-29 17:16:13 +05:30
# check if driver already installed
[ -e "${workdir}${driver}" ] ||
install -Dm644 "$driver" "${workdir}${driver}"
2020-02-06 21:51:00 +05:30
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-12 00:17:36 +05:30
# perform autodetection of drivers via /sys
2020-02-25 16:21:20 +05:30
find /sys -name modalias -exec sort -u {} + |
2020-02-29 17:16:13 +05:30
while read -r _driver; do
install_driver "$_driver"
2020-02-25 16:21:20 +05:30
done
2020-02-05 19:28:58 +05:30
2020-02-06 21:51:00 +05:30
# install user specified drivers
[ "$drivers" ] &&
2020-02-29 17:16:13 +05:30
for _driver in $drivers; do
install_driver "$_driver"
2020-02-25 16:21:20 +05:30
done
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-25 16:21:20 +05:30
find \
2020-02-27 17:24:30 +05:30
"${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" \
2020-02-25 16:21:20 +05:30
-type f 2> /dev/null |
2020-02-29 17:16:13 +05:30
while read -r _driver; do
2020-02-27 22:25:23 +05:30
# strip path and extension
2020-02-29 17:16:13 +05:30
_driver="${_driver##*/}"
_driver="${_driver%%.*}"
2020-02-27 22:25:23 +05:30
2020-02-29 17:16:13 +05:30
install_driver "$_driver"
2020-02-27 22:25:23 +05:30
done
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-27 22:25:23 +05:30
msg info "generating drivers list"
2020-02-22 23:16:57 +05:30
cp "${modker}/modules.builtin" \
2020-02-25 16:21:20 +05:30
"${modker}/modules.order" \
"${workdir}${modker}"
2020-02-25 00:26:12 +05:30
depmod -b "$workdir" "$kernel"
2020-01-30 19:23:17 +05:30
}
2020-02-14 22:42:45 +05:30
install_binary() {
2020-02-29 17:16:13 +05:30
binary=$(command -v "$1")
2020-03-09 20:26:14 +05:30
realbin="$(dirname "$binary")/$(readlink "$binary")"
fullbin=$(readlink -f "$binary")
workdirbin="${workdir}/usr/bin/"
2020-02-25 16:21:20 +05:30
# check if binary exists
2020-02-29 17:16:13 +05:30
[ "$binary" ] || msg panic "$binary doesn't exists"
2020-02-22 23:16:57 +05:30
2020-02-25 16:21:20 +05:30
# check if binary already installed
2020-03-09 20:26:14 +05:30
[ -e "${workdirbin}${fullbin##*/}" ] &&
return
# install symlinks if any
[ -h "$binary" ] && {
# symlink may link to symlink
[ -h "$realbin" ] &&
cp -a "$realbin" "$workdirbin"
cp -a "$binary" "$workdirbin"
}
2020-02-22 23:16:57 +05:30
2020-02-25 16:21:20 +05:30
# install and strip binary
2020-03-09 20:26:14 +05:30
install -s -m755 "$fullbin" "${workdirbin}${fullbin##*/}"
2020-01-30 19:23:17 +05:30
2020-02-25 16:21:20 +05:30
# check static
2020-02-29 17:16:13 +05:30
ldd "$binary" > /dev/null 2>&1 || return
2020-01-30 19:23:17 +05:30
2020-02-25 16:21:20 +05:30
# exract paths to libraries
2020-02-29 17:16:13 +05:30
ldd "$binary" |
2020-01-30 19:23:17 +05:30
2020-02-29 17:16:13 +05:30
while read -r _library; do
2020-02-28 23:27:03 +05:30
# strip unneeded stuff
2020-02-29 17:16:13 +05:30
_library="${_library##*vdso*}"
_library="${_library#* => }"
_library="${_library% *}"
2020-02-28 23:27:03 +05:30
# check empty
2020-02-29 17:16:13 +05:30
[ "$_library" ] || continue
2020-02-28 23:27:03 +05:30
2020-02-29 17:16:13 +05:30
install_library "$_library"
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-25 16:21:20 +05:30
library="$1"
2020-03-09 20:26:14 +05:30
reallib="$(dirname "$library")/$(readlink "$library")"
2020-02-25 16:21:20 +05:30
fulllib=$(readlink -f "$library")
2020-02-25 00:26:12 +05:30
workdirlib="${workdir}/usr/lib/"
2020-02-22 23:16:57 +05:30
2020-02-27 22:25:23 +05:30
# check if library already installed
[ -e "${workdirlib}${fulllib##*/}" ] &&
return
# install symlinks if any
[ -h "$library" ] && {
# symlink may link to symlink
[ -h "$reallib" ] &&
cp -a "$reallib" "$workdirlib"
cp -a "$library" "$workdirlib"
}
# install library
install -s -m755 "$fulllib" "${workdirlib}${fulllib##*/}"
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-03-08 06:48:07 +05:30
cat > "${workdir}/config" << EOF
2020-02-22 23:16:57 +05:30
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-27 22:25:23 +05:30
install -m644 "${filesdir}/passwd" "${workdir}/etc/passwd"
install -m644 "${filesdir}/group" "${workdir}/etc/group"
install -m755 "${filesdir}/init" "${workdir}/init"
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
2020-03-01 16:08:15 +05:30
# check if image already exist
2020-03-03 22:15:55 +05:30
[ "$force" = 0 ] && [ -e "$initramfs" ] &&
2020-03-01 16:08:15 +05:30
msg warn "looks like you already have initramfs image"
2020-02-18 13:23:03 +05:30
2020-03-01 16:08:15 +05:30
(
cd "$workdir"
find . | cpio -oH newc | ${compress:-gzip -9}
2020-02-18 13:23:03 +05:30
2020-03-01 16:08:15 +05:30
) > "$initramfs" 2> /dev/null ||
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-03-08 06:48:07 +05:30
prepare_environment
2020-02-22 23:16:57 +05:30
2020-03-01 18:55:52 +05:30
# remove workdir on signals
# we are doing unset EXIT signal to avoid endless loop
2020-03-08 06:48:07 +05:30
# because afterwards we execute exit command.
2020-03-01 18:55:52 +05:30
# also some shells (dash,mksh,etc) doesn't exit on INT
# signal. as workaround we manually execute exit command.
# tested bash,dash,mksh,busybox sh
# TODO fix zsh, ref https://www.zsh.org/mla/users/2015/msg00436.html
trap "remove_workdir && trap - EXIT && exit" EXIT INT TERM HUP
2020-02-24 19:02:54 +05:30
2020-02-22 23:16:57 +05:30
[ "$debug" = 1 ] && {
2020-03-08 06:48:07 +05:30
2020-01-30 21:04:33 +05:30
# debug shell commands
2020-01-30 20:58:03 +05:30
set -x
2020-03-08 06:48:07 +05:30
2020-01-30 21:04:33 +05:30
# don't remove anything
2020-03-01 18:55:52 +05:30
trap - EXIT INT TERM HUP
2020-01-30 20:58:03 +05:30
}
2020-02-25 00:26:12 +05:30
create_workdir
2020-01-30 19:23:17 +05:30
create_structure
create_symlinks
2020-02-28 21:30:58 +05:30
[ "$lvm" = 1 ] && install_lvm
[ "$luks" = 1 ] && install_luks
2020-02-28 21:30:58 +05:30
2020-03-09 00:07:19 +05:30
# check monotihic kernel
[ "$monolith" != 1 ] && [ -d "$moddir" ] && {
2020-02-28 21:30:58 +05:30
# check hostonly mode
if [ "$hostonly" = 1 ]; then
install_hostonly_drivers
2020-03-09 00:07:19 +05:30
# install lvm drivers
[ "$lvm" = 1 ] &&
for _driver in dm-thin-pool dm-multipath dm-snapshot dm-cache dm-log dm-mirror; do
install_driver "$_driver"
done
# install luks drivers
[ "$luks" = 1 ] &&
for _driver in aes dm-crypt sha256 sha512 wp512 ecb lrw xts twofish serpent; do
install_driver "$_driver"
done
2020-02-28 21:30:58 +05:30
else
install_all_drivers
fi
generate_depmod
2020-03-09 00:07:19 +05:30
}
2020-02-28 21:30:58 +05:30
2020-03-08 00:54:59 +05:30
install_devmgr
2020-03-03 23:02:53 +05:30
install_files
2020-03-08 00:54:59 +05:30
install_requirements
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"