tinyramfs/tinyramfs

520 lines
12 KiB
Plaintext
Raw Normal View History

2020-04-12 01:01:02 +05:30
#!/bin/sh -ef
2020-01-05 23:31:39 +05:30
#
2020-04-12 01:01:02 +05:30
# tiny initramfs
#
# false positive
# shellcheck disable=2154
2020-01-05 23:31:39 +05:30
# TODO add some colors ?
panic() { printf "panic >> %s\n" "$1" >&2; exit 1; }
info() { printf "info >> %s\n" "$1"; }
2020-01-05 23:31:39 +05:30
2020-04-12 01:01:02 +05:30
usage()
{
2020-02-22 23:16:57 +05:30
cat << EOF
2020-04-12 01:01:02 +05:30
usage: $0 [option]
2020-04-13 00:47:01 +05:30
-o, --output <file> set initramfs output path
2020-03-08 00:54:59 +05:30
-c, --config <file> set config file path
2020-04-12 01:01:02 +05:30
-m, --moddir <dir> set modules directory
2020-03-08 00:54:59 +05:30
-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
}
2020-04-12 01:01:02 +05:30
parse_args()
{
while [ "$1" ]; do case "$1" in
-o | --output)
_output="${2:?}"
shift 2
;;
-c | --config)
_config="${2:?}"
shift 2
;;
-m | --moddir)
_moddir="${2:?}"
shift 2
;;
-k | --kernel)
_kernel="${2:?}"
shift 2
;;
-F | --files)
_filesdir="${2:?}"
shift 2
;;
-d | --debug)
_debug=1
shift 1
;;
-f | --force)
_force=1
shift 1
;;
-h | --help)
usage
exit 0
;;
*)
printf "invalid option: %s\n\n" "$1"
usage
exit 1
;;
esac; done
2020-02-22 23:16:57 +05:30
}
2020-04-12 01:01:02 +05:30
prepare_environment()
{
info "preparing environment"
2020-04-12 01:01:02 +05:30
2020-03-09 18:52:21 +05:30
for _file in $_config /etc/tinyramfs/config ./config; do
# false positive
# shellcheck disable=1090
[ -f "$_file" ] && { . "$_file"; break; }
done || 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 || panic "failed to locate required files"
2020-02-24 13:39:37 +05:30
# general variables
2020-02-25 18:36:58 +05:30
debug="${_debug:-${debug:-0}}"
2020-03-01 19:12:57 +05:30
force="${_force:-${force:-0}}"
2020-04-12 01:01:02 +05:30
moddir="${_moddir:-${moddir:-/lib/modules}}"
kernel="${_kernel:-${kernel:-$(uname -r)}}"
output="${_output:-${output:-/tmp/initramfs-${kernel}}}"
2020-02-22 23:16:57 +05:30
2020-04-12 01:01:02 +05:30
mkdir -p "${workdir=${XDG_CACHE_HOME:-${TMPDIR:-/tmp}}/initramfs.$$}" ||
panic "failed to create working directory"
2020-04-12 01:01:02 +05:30
# helpers variables
2020-04-12 01:01:02 +05:30
workdirbin="${workdir}/usr/bin/"
workdirlib="${workdir}/usr/lib/"
modker="${moddir}/${kernel}"
OLD_IFS="$IFS"
2020-02-11 04:18:51 +05:30
}
2020-04-12 01:01:02 +05:30
remove_workdir()
{
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-04-12 01:01:02 +05:30
install_requirements()
{
info "installing requirements"
# install user specified and required binaries
for _binary in $binaries \[ sh sleep mount printf setsid switch_root; do
2020-02-29 17:16:13 +05:30
install_binary "$_binary"
2020-02-25 16:21:20 +05:30
done
2020-04-12 01:01:02 +05:30
# copy init
install -m755 "${filesdir}/init" "${workdir}/init"
# copy config
printf "%s\n" \
root="$root" \
root_type="$root_type" \
root_opts="$root_opts" \
devmgr="$devmgr" \
monolith="$monolith" \
>> "${workdir}/etc/config"
2020-01-30 19:23:17 +05:30
}
2020-04-12 01:01:02 +05:30
create_structure()
{
info "creating directory structure"
mkdir -p \
"${workdir}/etc" \
"${workdir}/dev" \
"${workdir}/sys" \
"${workdir}/tmp" \
"${workdir}/proc" \
"${workdir}/root" \
"${workdir}/usr/lib" \
"${workdir}/usr/bin" \
"${workdir}/mnt/root"
2020-01-30 19:23:17 +05:30
}
2020-04-12 01:01:02 +05:30
create_symlinks()
{
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 bin "${workdir}/usr/sbin"
ln -s lib "${workdir}/usr/lib64"
2020-01-30 19:23:17 +05:30
}
2020-04-12 01:01:02 +05:30
install_devmgr()
{
info "installing device manager"
install_devmgr_helper()
{
for _binary in ln kill mkdir blkid "${filesdir}/device-helper"; do
install_binary "$_binary"
done
printf "%s\n" \
'SUBSYSTEM=block;.* 0:0 660 @device-helper' \
> "${workdir}/etc/mdev.conf"
# false positive
# shellcheck disable=2016
[ "$monolith" != 1 ] && printf "%s\n" \
'$MODALIAS=.* 0:0 660 @modprobe "$MODALIAS"' \
>> "${workdir}/etc/mdev.conf"
}
2020-02-25 16:21:20 +05:30
2020-04-12 01:01:02 +05:30
# TODO investigate booting without device manager
case "$devmgr" in
udev)
2020-04-12 01:01:02 +05:30
for _binary in udevd udevadm; do
2020-02-29 17:16:13 +05:30
install_binary "$_binary"
2020-02-25 16:21:20 +05:30
done
2020-01-19 02:31:21 +05:30
2020-04-12 01:01:02 +05:30
# exclusively handle requirement
[ "$luks" = 1 ] || [ "$lvm" = 1 ] && install_binary dmsetup
2020-02-22 23:16:57 +05:30
2020-04-12 01:01:02 +05:30
for _binary in /usr/lib/udev/ata_id /usr/lib/udev/scsi_id; do
install -Dm755 "$_binary" "${workdir}${_binary}"
done
2020-04-13 03:23:43 +05:30
# TODO we really need all rules?
2020-04-12 01:01:02 +05:30
set +f; for _file in /usr/lib/udev/rules.d/*; do
install -Dm644 "$_file" "${workdir}${_file}"
done; set -f
;;
mdev)
install_binary mdev
install_devmgr_helper
;;
mdevd)
for _binary in mdevd mdevd-coldplug; do
2020-02-29 17:16:13 +05:30
install_binary "$_binary"
2020-02-25 16:21:20 +05:30
done
install_devmgr_helper
2020-04-12 01:01:02 +05:30
;;
esac
2020-01-30 19:23:17 +05:30
}
2020-01-25 16:57:02 +05:30
2020-04-12 01:01:02 +05:30
install_lvm()
{
info "installing LVM"
2020-02-22 23:16:57 +05:30
2020-04-12 01:01:02 +05:30
for _binary in lvchange vgchange; do
install_binary "$_binary"
done
2020-02-27 22:25:23 +05:30
# copy config
printf "%s\n" \
lvm="$lvm" \
lvm_opts="$lvm_opts" \
>> "${workdir}/etc/config"
mkdir -p "${workdir}/etc/lvm"
# word splitting is safe by design
# shellcheck disable=2086
2020-04-12 01:01:02 +05:30
{ IFS=,; set -- $lvm_opts; IFS="$OLD_IFS"; }
2020-02-27 22:25:23 +05:30
for opt; do case "$opt" in
config | config=1)
lvmconfig > "${workdir}/etc/lvm/lvm.conf"
return
;;
esac; done
2020-04-12 01:01:02 +05:30
printf "%s\n" \
'devices {' \
'write_cache_state = 0' \
'}' \
'backup {' \
'backup = 0' \
'archive = 0' \
'}' \
'global {' \
'use_lvmetad = 0' \
'}' \
> "${workdir}/etc/lvm/lvm.conf"
2020-01-30 19:23:17 +05:30
}
2020-01-19 02:31:21 +05:30
2020-04-12 01:01:02 +05:30
install_luks()
{
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
# copy config
printf "%s\n" \
luks="$luks" \
luks_root="$luks_root" \
luks_opts="$luks_opts" \
>> "${workdir}/etc/config"
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
[ -e /usr/lib/libgcc_s.so.1 ] && install_library /usr/lib/libgcc_s.so.1
2020-01-25 16:57:02 +05:30
# word splitting is safe by design
# shellcheck disable=2086
2020-04-12 01:01:02 +05:30
{ IFS=,; set -- $luks_opts; IFS="$OLD_IFS"; }
2020-02-06 03:59:59 +05:30
for opt; do case "${opt%%=*}" in
header)
install -m400 "${opt##*=}" "${workdir}/root/header"
luks_opts=$(printf "%s" "$luks_opts" | sed "s|${opt##*=}|/root/header|")
;;
key)
install -m400 "${opt##*=}" "${workdir}/root/key"
luks_opts=$(printf "%s" "$luks_opts" | sed "s|${opt##*=}|/root/key|")
;;
esac; done
2020-01-30 19:23:17 +05:30
}
2020-01-19 02:31:21 +05:30
2020-04-12 01:01:02 +05:30
install_module()
{
module="$1"
2020-02-18 13:23:03 +05:30
2020-04-12 01:01:02 +05:30
modprobe -S "$kernel" -D "$module" 2> /dev/null |
2020-02-22 23:16:57 +05:30
2020-04-12 01:01:02 +05:30
while read -r module || [ "$module" ]; do
2020-02-26 01:15:21 +05:30
2020-02-28 23:27:03 +05:30
# strip unneeded stuff
2020-04-12 01:01:02 +05:30
module="${module##*builtin*}"
module="${module##*net*}"
module="${module#insmod }"
2020-03-01 20:09:12 +05:30
2020-04-12 01:01:02 +05:30
# exclude user specified modules if any
for _exclude_module in $modules_exclude; do
module="${module##*${_exclude_module}*}"
done
2020-02-27 23:47:05 +05:30
2020-04-12 01:01:02 +05:30
# check if module already installed
[ -e "$module" ] && [ ! -e "${workdir}${module}" ] &&
install -Dm644 "$module" "${workdir}${module}"
done ||:
2020-02-12 00:17:36 +05:30
}
2020-04-12 01:01:02 +05:30
install_hostonly_modules()
{
info "installing hostonly modules"
2020-02-22 23:16:57 +05:30
2020-04-12 01:01:02 +05:30
# perform autodetection of modules via /sys
2020-02-25 16:21:20 +05:30
find /sys -name modalias -exec sort -u {} + |
2020-04-12 01:01:02 +05:30
while read -r _module || [ "$_module" ]; do
install_module "$_module"
done ||:
# install LVM modules
[ "$lvm" = 1 ] &&
for _module in dm-thin-pool dm-multipath dm-snapshot dm-cache dm-log dm-mirror; do
install_module "$_module"
done
2020-02-05 19:28:58 +05:30
2020-04-12 01:01:02 +05:30
# install LUKS modules
[ "$luks" = 1 ] &&
for _module in aes dm-crypt sha256 sha512 wp512 ecb lrw xts twofish serpent; do
install_module "$_module"
2020-02-25 16:21:20 +05:30
done
2020-04-12 01:01:02 +05:30
# install root partition module
if [ "$root_type" ]; then
install_module "$root_type"
else
while read -r _ _dir _type _ _ _ || [ "$_dir" ]; do
[ "$_dir" = / ] && {
2020-04-13 00:47:01 +05:30
install_module "${root_type=$_type}"
2020-04-12 01:01:02 +05:30
break
}
done < /proc/mounts || panic "failed to install root partition module"
2020-04-12 01:01:02 +05:30
fi
# install user specified modules if any
for _module in $modules; do
install_module "$_module"
done
2020-02-06 21:51:00 +05:30
}
2020-04-12 01:01:02 +05:30
install_all_modules()
{
info "installing all modules"
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-04-12 01:01:02 +05:30
while read -r _module || [ "$_module" ]; do
2020-02-27 22:25:23 +05:30
# strip path and extension
2020-04-12 01:01:02 +05:30
_module="${_module##*/}"
_module="${_module%%.*}"
2020-04-12 01:01:02 +05:30
install_module "$_module"
done ||:
2020-01-30 19:23:17 +05:30
}
2020-04-12 01:01:02 +05:30
install_binary()
{
2020-02-29 17:16:13 +05:30
binary=$(command -v "$1")
2020-04-12 01:01:02 +05:30
# check if binary exist and builtin
case "$binary" in
*/*)
: no operation
;;
"")
panic "$1 doesn't exist"
2020-04-12 01:01:02 +05:30
;;
*)
# word splitting is safe by design
# shellcheck disable=2086
2020-04-12 01:01:02 +05:30
{ IFS=:; set -- $PATH; IFS="$OLD_IFS"; }
for _dir; do
[ -x "${_dir}/${binary}" ] && {
2020-04-12 01:01:02 +05:30
binary="${_dir}/${binary}"
break
}
done || panic "couldn't find external $1 binary"
2020-04-12 01:01:02 +05:30
;;
esac
2020-02-22 23:16:57 +05:30
2020-02-25 16:21:20 +05:30
# check if binary already installed
2020-04-12 01:01:02 +05:30
[ -e "${workdirbin}${binary##*/}" ] && return
2020-03-09 20:26:14 +05:30
2020-04-12 01:01:02 +05:30
# iterate throught symlinks and copy them
while [ -h "$binary" ]; do
cp -P "$binary" "$workdirbin"
binary="${binary%/*}/$(readlink "$binary")"
done
2020-02-22 23:16:57 +05:30
2020-04-12 01:01:02 +05:30
install -m755 "$binary" "${workdirbin}${binary##*/}"
strip "${workdirbin}${binary##*/}" > /dev/null 2>&1 ||:
2020-01-30 19:23:17 +05:30
2020-04-12 01:01:02 +05:30
# check if binary statically linked
ldd "$binary" > /dev/null 2>&1 || return 0
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-04-12 01:01:02 +05:30
while read -r _library || [ "$_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
2020-04-12 01:01:02 +05:30
[ -e "$_library" ] && install_library "$_library"
done ||:
2020-01-30 19:23:17 +05:30
}
2020-01-05 23:31:39 +05:30
2020-04-12 01:01:02 +05:30
install_library()
{
2020-02-25 16:21:20 +05:30
library="$1"
2020-02-22 23:16:57 +05:30
2020-02-27 22:25:23 +05:30
# check if library already installed
2020-04-12 01:01:02 +05:30
[ -e "${workdirlib}${library##*/}" ] && return
2020-02-27 22:25:23 +05:30
2020-04-12 01:01:02 +05:30
# iterate throught symlinks and copy them
while [ -h "$library" ]; do
cp -P "$library" "$workdirlib"
library="${library%/*}/$(readlink "$library")"
done
2020-01-05 23:31:39 +05:30
2020-04-12 01:01:02 +05:30
install -m755 "$library" "${workdirlib}${library##*/}"
strip "${workdirlib}${library##*/}" > /dev/null 2>&1 ||:
2020-01-30 19:23:17 +05:30
}
2020-04-12 01:01:02 +05:30
create_initramfs()
{
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-04-12 01:01:02 +05:30
[ "$force" != 1 ] && [ -e "$output" ] &&
panic "initramfs image already exist"
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-04-12 01:01:02 +05:30
) > "$output" 2> /dev/null ||
panic "failed to generate initramfs image"
2020-01-30 19:23:17 +05:30
}
2020-04-12 01:01:02 +05:30
# int main()
{
# check root
[ "$(id -u)" = 0 ] || panic "must be run as root"
2020-04-12 01:01:02 +05:30
parse_args "$@"
prepare_environment
[ "$debug" = 1 ] && set -x
# hacky, but compatible with all posix shells
trap '
ret="$?"
trap - EXIT INT
[ "$debug" != 1 ] && remove_workdir
[ "$ret" != 0 ] && panic "something went wrong"
2020-04-12 01:01:02 +05:30
' EXIT INT
create_structure
create_symlinks
[ "$lvm" = 1 ] && install_lvm
[ "$luks" = 1 ] && install_luks
# check monolithic kernel
[ "$monolith" != 1 ] && [ -d "$moddir" ] && {
2020-04-12 01:01:02 +05:30
# check hostonly mode
if [ "$hostonly" = 1 ]; then
install_hostonly_modules
else
install_all_modules
fi
for _binary in find sort modprobe; do
install_binary "$_binary"
done
2020-02-28 21:30:58 +05:30
2020-04-12 01:01:02 +05:30
cp "${modker}/modules.builtin" \
"${modker}/modules.order" \
"${workdir}${modker}"
2020-03-09 00:07:19 +05:30
2020-04-12 01:01:02 +05:30
depmod -b "$workdir" "$kernel"
}
2020-03-09 00:07:19 +05:30
2020-04-12 01:01:02 +05:30
install_devmgr
install_requirements
create_initramfs
2020-02-28 21:30:58 +05:30
info "done! check out - $output"
2020-03-09 00:07:19 +05:30
}