tinyramfs/tinyramfs
2020-09-10 22:53:39 +03:00

364 lines
9.1 KiB
Bash
Executable File

#!/bin/sh
#
# tiny initramfs
#
# false positive
# shellcheck disable=2154
print()
{
printf "%b %s\n" "${2:-"\033[1;37m>>\033[m"}" "$1"
}
panic()
{
print "${1:-unexpected error occurred}" \
"\033[1;31m!!\033[m" >&2; exit 1
}
usage()
{
cat << EOF
usage: ${0##*/} [option ...]
-o, --output <file> set initramfs output path
default is /boot/initramfs-$(uname -r)
-c, --config <file> set config file path
default is /etc/tinyramfs/config
-m, --modules <dir> set modules directory
default is /lib/modules
-s, --sources <dir> set sources directory
default is /usr/share/tinyramfs
-k, --kernel <ver> set kernel version
default is $(uname -r)
-H, --hooks <dir> set hooks directory
default is /etc/tinyramfs/hooks (user hooks)
and /usr/share/tinyramfs/hooks (system hooks)
-d, --debug enable debug mode
-f, --force overwrite initramfs image
EOF
}
prepare_environment()
{
while [ "$1" ]; do case "$1" in
-o | --output)
output="${2:?}"; shift 2
;;
-c | --config)
config="${2:?}"; shift 2
;;
-m | --modules)
moddir="${2:?}"; shift 2
;;
-s | --sources)
srcdir="${2:?}"; shift 2
;;
-k | --kernel)
kernel="${2:?}"; shift 2
;;
-H | --hooks)
hksdir="${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
print "preparing environment"
# false positive
# shellcheck disable=1090
. "${config:=/etc/tinyramfs/config}"
: "${kernel:=$(uname -r)}"
: "${moddir:=/lib/modules}"
: "${srcdir:=/usr/share/tinyramfs}"
: "${output:=/boot/tinyramfs-${kernel}}"
mkdir -p "${tmpdir:=${TMPDIR:-/tmp}/tinyramfs.$$}"
# false positive
# shellcheck disable=2015
[ "$debug" = 1 ] && set -x || trap 'rm -rf $tmpdir' EXIT INT
}
prepare_initramfs()
{
print "preparing initramfs"
# make directories
mkdir -p \
"${tmpdir}/dev" \
"${tmpdir}/sys" \
"${tmpdir}/tmp" \
"${tmpdir}/run" \
"${tmpdir}/var" \
"${tmpdir}/proc" \
"${tmpdir}/root" \
"${tmpdir}/usr/lib" \
"${tmpdir}/usr/bin" \
"${tmpdir}/mnt/root" \
"${tmpdir}/etc/tinyramfs"
# make symlinks
ln -s usr/lib "${tmpdir}/lib"
ln -s usr/bin "${tmpdir}/bin"
ln -s usr/bin "${tmpdir}/sbin"
ln -s ../run "${tmpdir}/var/run"
ln -s ../run/lock "${tmpdir}/var/lock"
ln -s bin "${tmpdir}/usr/sbin"
# copy required binaries
for _binary in \
\[ sh ln env kill mkdir sleep mount \
printf switch_root "${srcdir}/device-helper"
do
copy_binary "$_binary"
done
command -v blkid > /dev/null && copy_binary blkid
copy_file "${srcdir}/init" /init 755 0
copy_file "$config" /etc/tinyramfs/config 644 0
}
copy_file()
(
file="$1"; dest="$2"; mode="$3"; strip="$4"
# check if file already exist
[ -e "${tmpdir}${dest}" ] && return 0
mkdir -p "${tmpdir}${dest%/*}" || panic
# iterate throught symlinks and copy them
while [ -h "$file" ]; do
cp -P "$file" "${tmpdir}${dest%/*}/${file##*/}"
cd -P "${file%/*}"
symlink=$(ls -ld "$file")
symlink="${symlink##* -> }"
file="${PWD}/${symlink##*/}"
done
# handle case when file and dest have same basenames
[ -h "${tmpdir}${dest}" ] && dest="${dest%/*}/${file##*/}"
{
cp "$file" "${tmpdir}${dest}"
chmod "$mode" "${tmpdir}${dest}"
} || panic
# false positive
# shellcheck disable=2015
[ "$strip" = 1 ] && strip "${tmpdir}${dest}" > /dev/null 2>&1 || :
)
copy_binary()
{
binary=$(command -v "$1")
# check if binary exist and builtin
# false positive
# shellcheck disable=2086
case "$binary" in */*) ;;
"")
panic "$1 does not exist"
;;
*)
IFS=:; set -- $PATH; unset IFS
# assume that `command -v` returned builtin command.
# this behavior depends on shell implementation.
# to be independent we simply iterating over PATH
# to find external alternative ( e.g kill => /bin/kill )
for _dir; do
[ -x "${_dir}/${binary}" ] || ! continue
binary="${_dir}/${binary}"; break
done || panic "$1 does not exist"
;;
esac
copy_file "$binary" "/bin/${binary##*/}" 755 1
# copy binary dependencies if any
ldd "$binary" 2> /dev/null |
while read -r _library || [ "$_library" ]; do
_library="${_library#* => }"
_library="${_library% *}"
[ -e "$_library" ] || continue
copy_file "$_library" "/lib/${_library##*/}" 755 1
done
}
copy_module()
{
module="$1"
modprobe -S "$kernel" -D "$module" 2> /dev/null |
while read -r _ module || [ "$module" ]; do
# check if module contains full path(not builtin)
[ "${module##*/*}" ] && continue
copy_file "$module" "$module" 644 0
done
}
copy_hook()
{
hook="$1"
for _dir in "$hksdir" /etc/tinyramfs/hooks /usr/share/tinyramfs/hooks; do
[ -f "${_dir}/${hook}/${hook}" ] || ! continue
done || panic "could not find $hook hook"
print "running $hook hook"
# false positive
# shellcheck disable=1090
. "${_dir}/${hook}/${hook}"
for _type in init init.late; do
[ -f "${_dir}/${hook}/${hook}.${_type}" ] || continue
print "copying ${hook}.${_type}"
copy_file "${_dir}/${hook}/${hook}.${_type}" \
"/usr/share/tinyramfs/hooks/${hook}/${hook}.${_type}" 644 0
done
}
copy_modules()
{
# skip this function if kernel
# compiled with builtin modules
if [ "$monolith" = 1 ]; then
return 0
elif [ "$hostonly" = 1 ]; then
print "copying hostonly modules"
# perform autodetection of modules via /sys
# see https://wiki.archlinux.org/index.php/Modalias
find /sys/devices -name modalias -exec sort -u {} + |
while read -r _module || [ "$_module" ]; do
# skip unneeded modules and skip modules which
# depends on them as well
case $(modprobe -S "$kernel" -D "$_module") in
*wmi* | *gpu* | *net*) continue ;;
esac 2> /dev/null
copy_module "$_module"
done
# copy root filesystem module
if [ "$root_type" ]; then
copy_module "$root_type"
else
while read -r _ _dir _type _; do
[ "$_dir" = / ] || ! continue
copy_module "$_type"; break
done < /proc/mounts ||
panic "could not copy root fs module"
fi
else
print "copying all modules"
find \
"${moddir}/${kernel}/kernel/fs" \
"${moddir}/${kernel}/kernel/lib" \
"${moddir}/${kernel}/kernel/arch" \
"${moddir}/${kernel}/kernel/crypto" \
"${moddir}/${kernel}/kernel/drivers/md" \
"${moddir}/${kernel}/kernel/drivers/ata" \
"${moddir}/${kernel}/kernel/drivers/scsi" \
"${moddir}/${kernel}/kernel/drivers/block" \
"${moddir}/${kernel}/kernel/drivers/virtio" \
"${moddir}/${kernel}/kernel/drivers/usb/host" \
"${moddir}/${kernel}/kernel/drivers/usb/storage" \
-type f 2> /dev/null |
while read -r _module || [ "$_module" ]; do
# strip path and extension
_module="${_module##*/}"
_module="${_module%%.*}"
# skip unneeded modules and skip modules which
# depends on them as well
case $(modprobe -S "$kernel" -D "$_module") in
*wmi* | *gpu* | *net*) continue ;;
esac 2> /dev/null
copy_module "$_module"
done
fi
copy_binary modprobe
copy_file "${moddir}/${kernel}/modules.order" "/lib/modules/${kernel}/modules.order" 644 0
copy_file "${moddir}/${kernel}/modules.builtin" "/lib/modules/${kernel}/modules.builtin" 644 0
depmod -b "$tmpdir" "$kernel"
}
make_initramfs()
(
print "generating initramfs image"
# check if image already exist
[ "$force" != 1 ] && [ -e "$output" ] &&
panic "initramfs image already exist"
cd "$tmpdir"; find . |
cpio -oH newc 2> /dev/null |
${compress:-cat} > "$output" ||
panic "failed to generate initramfs image"
print "done! check out $output"
)
[ "$(id -u)" = 0 ] || panic "must be run as root"
# enable exit on error and disable globbing
set -ef
prepare_environment "$@"
prepare_initramfs
# copy and run hooks if any
for _hook in $hooks; do
copy_hook "$_hook"
done
copy_modules
make_initramfs