tinyramfs/tinyramfs

536 lines
13 KiB
Plaintext
Raw Normal View History

#!/bin/sh
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
2020-04-21 20:05:55 +05:30
print()
{
printf "%b %s\n" "${2:-"\033[1;37m>>\033[m"}" "$1"
2020-04-21 20:05:55 +05:30
}
panic()
{
print "${1:-unexpected error occurred}" \
"\033[1;31m!!\033[m" >&2; exit 1
2020-04-21 20:05:55 +05:30
}
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]
-o, --output <file> set initramfs output path
-c, --config <file> set config file path
-m, --moddir <dir> set modules directory
-k, --kernel <ver> set kernel version
-F, --files <dir> set files directory
-d, --debug enable debug mode
-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()
{
2020-04-21 20:05:55 +05:30
print "preparing environment"
2020-04-12 01:01:02 +05:30
2020-04-17 19:43:35 +05:30
# false positive
# shellcheck disable=1090
2020-05-26 19:14:20 +05:30
for _file in "$_config" /etc/tinyramfs/config; do
[ -f "$_file" ] && { . "$_file"; break; }
done || panic "failed to source config"
2020-03-08 06:48:07 +05:30
2020-05-26 19:14:20 +05:30
for _dir in "$_filesdir" /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
mkdir -p "${workdir=${XDG_CACHE_HOME:-${TMPDIR:-/tmp}}/initramfs.$$}"
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}"
2020-04-21 20:05:55 +05:30
2020-05-26 19:14:20 +05:30
# false positive
# shellcheck disable=2015
[ "$debug" = 1 ] && set -x || trap trap_helper EXIT INT
2020-02-11 04:18:51 +05:30
}
2020-04-21 20:05:55 +05:30
trap_helper()
2020-04-12 01:01:02 +05:30
{
[ "${ret=$?}" = 0 ] ||
print "unexpected error occurred" \
"\033[1;31m!!\033[m" >&2
2020-04-21 20:05:55 +05:30
print "removing working directory"; rm -rf "$workdir"
exit "$ret"
}
2020-04-21 20:05:55 +05:30
populate_config() {
printf "%s\n" "$@" >> "${workdir}/etc/tinyramfs/config"
2020-02-02 17:49:20 +05:30
}
2020-04-12 01:01:02 +05:30
install_requirements()
{
2020-04-21 20:05:55 +05:30
print "installing requirements"
# install required binaries
2020-05-26 19:14:20 +05:30
for _binary in \[ sh ln kill mkdir \
blkid sleep mount printf \
switch_root "${filesdir}/device-helper"
do
install_binary "$_binary"
done
# install user specified binaries
for _binary in $binaries; 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
# install init
2020-04-12 01:01:02 +05:30
install -m755 "${filesdir}/init" "${workdir}/init"
# fix ubase mount issue
: > "${workdir}/etc/fstab"
populate_config \
"root='$root'" \
"devmgr='$devmgr'" \
"monolith='$monolith'" \
"root_type='$root_type'" \
"root_opts='$root_opts'"
2020-01-30 19:23:17 +05:30
}
2020-04-12 01:01:02 +05:30
create_structure()
{
2020-04-21 20:05:55 +05:30
print "creating directory structure"
mkdir -p \
"${workdir}/dev" \
"${workdir}/sys" \
"${workdir}/tmp" \
"${workdir}/run" \
"${workdir}/var" \
"${workdir}/proc" \
"${workdir}/root" \
"${workdir}/usr/lib" \
"${workdir}/usr/bin" \
"${workdir}/mnt/root" \
"${workdir}/etc/tinyramfs"
2020-01-30 19:23:17 +05:30
}
2020-04-12 01:01:02 +05:30
create_symlinks()
{
2020-04-21 20:05:55 +05:30
print "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 ../run/lock "${workdir}/var/lock"
2020-02-28 20:08:03 +05:30
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()
{
2020-04-21 20:05:55 +05:30
print "installing device manager"
2020-05-26 19:14:20 +05:30
# false positive
# shellcheck disable=2016
case "$devmgr" in
none)
# TODO implement mode without device manager using deprecated
# /sys/kernel/uevent_helper or /proc/sys/kernel/hotplug
;;
mdev)
2020-05-26 19:14:20 +05:30
install_binary mdev
2020-01-19 02:31:21 +05:30
printf "%s\n" \
'SUBSYSTEM=block;.* 0:0 660 @device-helper' \
> "${workdir}/etc/mdev.conf"
2020-04-18 15:46:05 +05:30
[ "$monolith" = 1 ] && return 0
2020-02-22 23:16:57 +05:30
2020-05-26 19:14:20 +05:30
install_binary find
printf "%s\n" \
'$MODALIAS=.* 0:0 660 @modprobe "$MODALIAS"' \
>> "${workdir}/etc/mdev.conf"
;;
mdevd)
2020-05-26 19:14:20 +05:30
for _binary in mdevd mdevd-coldplug; do
install_binary "$_binary"
2020-04-12 01:01:02 +05:30
done
printf "%s\n" \
'SUBSYSTEM=block;.* 0:0 660 @device-helper' \
> "${workdir}/etc/mdev.conf"
[ "$monolith" = 1 ] && return 0
printf "%s\n" \
'$MODALIAS=.* 0:0 660 @modprobe "$MODALIAS"' \
>> "${workdir}/etc/mdev.conf"
;;
udev)
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
mkdir -p "${workdir}/usr/lib/udev/rules.d"
printf "%s\n" \
'SUBSYSTEMS=="block", ACTION=="add", RUN+="/bin/device-helper"' \
> "${workdir}/usr/lib/udev/rules.d/device-helper.rules"
[ "$monolith" = 1 ] && return 0
printf "%s\n" \
'ENV{MODALIAS}=="?*", ACTION=="add", RUN+="/bin/modprobe %E{MODALIAS}"' \
>> "${workdir}/usr/lib/udev/rules.d/device-helper.rules"
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()
{
2020-04-21 20:05:55 +05:30
print "installing LVM"
2020-02-22 23:16:57 +05:30
install_binary lvm
2020-02-27 22:25:23 +05:30
2020-04-16 01:33:07 +05:30
lvm_config="
devices {
write_cache_state = 0
}
backup {
backup = 0
archive = 0
}
global {
use_lvmetad = 0
}"
# word splitting is safe by design
# shellcheck disable=2086
{ IFS=,; set -- $lvm_opts; unset IFS; }
2020-02-27 22:25:23 +05:30
for opt; do case "$opt" in
config | config=1) embed_lvm_config=1 ;;
esac; done
mkdir -p "${workdir}/etc/lvm"
lvm config \
2020-04-16 01:33:07 +05:30
--config "$lvm_config" \
${embed_lvm_config+--mergedconfig} \
> "${workdir}/etc/lvm/lvm.conf"
2020-04-16 10:07:45 +05:30
populate_config "lvm='$lvm'" "lvm_opts='$lvm_opts'"
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()
{
2020-04-21 20:05:55 +05:30
print "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
# fix 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
{ IFS=,; set -- $luks_opts; unset IFS; }
2020-02-06 03:59:59 +05:30
for opt; do case "${opt%%=*}" in
key | header)
install -m400 "${opt##*=}" "${workdir}/root/${opt%%=*}" || panic
luks_opts=$(printf "%s" "$luks_opts" | sed "s|${opt##*=}|/root/${opt%%=*}|")
;;
esac; done
2020-04-16 10:07:45 +05:30
populate_config \
"luks='$luks'" \
"luks_root='$luks_root'" \
"luks_opts='$luks_opts'"
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
# skip unneeded stuff
for _exclude_module in wmi gpu net builtin $modules_exclude; do
2020-04-18 21:15:24 +05:30
case "$module" in *"$_exclude_module"*) continue 2 ;; esac
2020-04-12 01:01:02 +05:30
done
2020-02-27 23:47:05 +05:30
2020-04-18 21:15:24 +05:30
module="${module#insmod }"
2020-04-12 01:01:02 +05:30
# check if module already installed
[ -e "${workdir}${module}" ] && continue
install -Dm644 "$module" "${workdir}${module}" || panic
2020-05-26 19:14:20 +05:30
done
2020-02-12 00:17:36 +05:30
}
2020-04-12 01:01:02 +05:30
install_hostonly_modules()
{
2020-04-21 20:05:55 +05:30
print "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"
2020-05-26 19:14:20 +05:30
done
2020-04-12 01:01:02 +05:30
# install LVM modules
[ "$lvm" = 1 ] &&
for _module in dm-thin-pool dm-multipath \
dm-snapshot dm-cache dm-log dm-mirror
do
2020-04-12 01:01:02 +05:30
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
2020-04-12 01:01:02 +05:30
install_module "$_module"
2020-02-25 16:21:20 +05:30
done
2020-04-12 01:01:02 +05:30
# install root filesystem module
2020-04-12 01:01:02 +05:30
if [ "$root_type" ]; then
install_module "$root_type"
else
2020-05-26 19:14:20 +05:30
while read -r _ _dir _type _; do
[ "$_dir" = / ] || continue
install_module "${root_type=$_type}"; break
done < /proc/mounts || panic
2020-04-12 01:01:02 +05:30
fi
2020-02-06 21:51:00 +05:30
}
2020-04-12 01:01:02 +05:30
install_all_modules()
{
2020-04-21 20:05:55 +05:30
print "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/fs" \
"${modker}/kernel/lib" \
"${modker}/kernel/arch" \
"${modker}/kernel/crypto" \
2020-02-27 17:24:30 +05:30
"${modker}/kernel/drivers/md" \
"${modker}/kernel/drivers/ata" \
2020-02-27 17:24:30 +05:30
"${modker}/kernel/drivers/scsi" \
"${modker}/kernel/drivers/block" \
2020-02-27 17:24:30 +05:30
"${modker}/kernel/drivers/virtio" \
"${modker}/kernel/drivers/usb/host" \
"${modker}/kernel/drivers/usb/storage" \
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"
2020-05-26 19:14:20 +05:30
done
}
install_modules()
{
if [ "$hostonly" = 1 ]; then
install_hostonly_modules
else
install_all_modules
fi
# install user specified modules if any
[ "$modules" ] && {
for _module in $modules; do
install_module "$_module"
done
populate_config "modules='$modules'"
}
install_binary modprobe
install -m644 \
"${modker}/modules.builtin" \
"${modker}/modules.order" \
"${workdir}${modker}"
depmod -b "$workdir" "$kernel"
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
{ IFS=:; set -- $PATH; unset IFS; }
2020-04-12 01:01:02 +05:30
# try to discover external binary/script by checking PATH
2020-04-12 01:01:02 +05:30
for _dir; do
[ -x "${_dir}/${binary}" ] || ! continue
binary="${_dir}/${binary}"; break
done || panic "couldn't find external $1"
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
[ -e "${workdirbin}${binary##*/}" ] && return 0
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" || panic
2020-04-18 13:51:28 +05:30
readlink_binary=$(readlink "$binary")
binary="${binary%/*}/${readlink_binary##*/}"
2020-04-12 01:01:02 +05:30
done
2020-02-22 23:16:57 +05:30
install -m755 "$binary" "${workdirbin}${binary##*/}" || panic
strip "${workdirbin}${binary##*/}" > /dev/null 2>&1 || :
2020-01-30 19:23:17 +05:30
# skip static binaries/scripts
2020-04-12 01:01:02 +05:30
ldd "$binary" > /dev/null 2>&1 || return 0
2020-01-30 19:23:17 +05:30
# parse ldd output to find libraries paths
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
# skip unneeded stuff
2020-04-21 20:05:55 +05:30
[ "${_library##*vdso*}" ] || continue
2020-04-18 21:15:24 +05:30
2020-02-29 17:16:13 +05:30
_library="${_library#* => }"
_library="${_library% *}"
2020-02-28 23:27:03 +05:30
install_library "$_library"
2020-05-26 19:14:20 +05:30
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
[ -e "${workdirlib}${library##*/}" ] && return 0
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" || panic
2020-04-18 13:51:28 +05:30
readlink_library=$(readlink "$library")
library="${library%/*}/${readlink_library##*/}"
2020-04-12 01:01:02 +05:30
done
2020-01-05 23:31:39 +05:30
install -m755 "$library" "${workdirlib}${library##*/}" || panic
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()
(
2020-04-21 20:05:55 +05:30
print "creating initramfs image"
2020-02-24 13:39:37 +05:30
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
cd "$workdir"; find . |
2020-02-18 13:23:03 +05:30
if [ "$compress" = none ]; then
cpio -oH newc
else
cpio -oH newc | ${compress:-gzip -9}
fi \
> "$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()
{
[ "$(id -u)" = 0 ] || panic "must be run as root"
2020-04-12 01:01:02 +05:30
# enable exit on error and disable globbing
set -ef
2020-04-12 01:01:02 +05:30
parse_args "$@"
prepare_environment
create_structure
create_symlinks
[ "$lvm" = 1 ] && install_lvm
[ "$luks" = 1 ] && install_luks
[ "$monolith" = 1 ] || install_modules
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
2020-04-21 20:05:55 +05:30
print "done! check out $output"
2020-03-09 00:07:19 +05:30
}