tinyramfs: simplification and cleanup

This commit is contained in:
illiliti 2021-05-10 16:00:51 +03:00
parent bd16dcbd77
commit 0b96f116dd
3 changed files with 51 additions and 48 deletions

View File

@ -6,7 +6,7 @@ Tinyramfs - tiny initramfs written in POSIX shell
# SYNOPSIS # SYNOPSIS
*tinyramfs* [option ...] *tinyramfs* [option]...
# DESCRIPTION # DESCRIPTION
@ -15,20 +15,11 @@ with focus on portability.
# OPTIONS # OPTIONS
*-m*, *--modules* <directory> *-o*, *--output* <file>
Specify directory where modules stored. Default is */lib/modules*.
*-s*, *--sources* <directory>
Specify directory where tinyramfs stored files such as *device-helper*
and *init*. Default is */usr/share/tinyramfs*.
*-o*, *--output* <output>
Specify output location where initramfs image will be stored. Specify output location where initramfs image will be stored.
*-c*, *--config* <config> *-c*, *--config* <file>
Specify config location. Default is */etc/tinyramfs/config*. Specify config location. Default is */etc/tinyramfs/config*.
@ -37,11 +28,24 @@ with focus on portability.
Specify kernel version. Useful for bootstraping future system with modular Specify kernel version. Useful for bootstraping future system with modular
kernel. Default is current kernel version. kernel. Default is current kernel version.
*-m*, *--modules* <directory>
Specify directory where modules stored. Default is */lib/modules*.
This option has no effect if *monolith* or *hostonly* were enabled in config.
*-H*, *--hooks* <directory> *-H*, *--hooks* <directory>
Specify additional hooks location. Default is */usr/share/tinyramfs/hooks* Specify additional hooks location. Default is */usr/share/tinyramfs/hooks*
and */etc/tinyramfs/hooks*. and */etc/tinyramfs/hooks*.
*-D*, *--helper* <file>
Specify location to device helper. Default is */usr/share/tinyramfs/device-helper*.
*-I*, *--init* <file>
Specify location to init script. Default is */usr/share/tinyramfs/init*.
*-d*, *--debug* *-d*, *--debug*
Enable debug mode. Useful for reporting bugs in build stage. Enable debug mode. Useful for reporting bugs in build stage.

View File

@ -80,8 +80,9 @@ interact with build and init system.
- B: tmpdir - full path of tinyramfs working directory (initramfs rootfs in future) - B: tmpdir - full path of tinyramfs working directory (initramfs rootfs in future)
- B: kernel - kernel version - B: kernel - kernel version
- B: moddir - modules directory - B: moddir - modules directory
- B: init - path to init script
- B: helper - path to device-helper script
- B: config - config location - B: config - config location
- B: srcdir - directory of tinyramfs system files
- B: output - output path - B: output - output path
- BI: see tinyramfs.config(5) - BI: see tinyramfs.config(5)
``` ```

View File

@ -22,14 +22,15 @@ usage()
{ {
cat << EOF cat << EOF
usage: ${0##*/} [option]... usage: ${0##*/} [option]...
-o, --output <file> set initramfs output path -o, --output <file> set path to initramfs image
-c, --config <file> set config file path -c, --config <file> set path to config
-m, --modules <dir> set modules directory -k, --kernel <ver> set kernel version
-s, --sources <dir> set sources directory -m, --modules <dir> set path to modules
-k, --kernel <ver> set kernel version -H, --hooks <dir> set directory to hooks
-H, --hooks <dir> set hooks directory -D, --helper <file> set path to device helper
-d, --debug enable debug mode -I, --init <file> set path to init script
-f, --force overwrite initramfs image -d, --debug enable debug mode
-f, --force overwrite initramfs image
EOF EOF
} }
@ -42,18 +43,21 @@ parse_arguments()
-c | --config) -c | --config)
config="${2:?}"; shift 2 config="${2:?}"; shift 2
;; ;;
-m | --modules)
moddir="${2:?}"; shift 2
;;
-s | --sources)
srcdir="${2:?}"; shift 2
;;
-k | --kernel) -k | --kernel)
kernel="${2:?}"; shift 2 kernel="${2:?}"; shift 2
;; ;;
-m | --modules)
moddir="${2:?}"; shift 2
;;
-H | --hooks) -H | --hooks)
hksdir="${2:?}"; shift 2 hksdir="${2:?}"; shift 2
;; ;;
-D | --helper)
helper="${2:?}"; shift 2
;;
-I | --init)
init="${2:?}"; shift 2
;;
-d | --debug) -d | --debug)
debug=1; shift 1 debug=1; shift 1
;; ;;
@ -76,8 +80,9 @@ parse_arguments()
: "${kernel:=$(uname -r)}" : "${kernel:=$(uname -r)}"
: "${moddir:=/lib/modules}" : "${moddir:=/lib/modules}"
: "${srcdir:=/usr/share/tinyramfs}" : "${init:=/usr/share/tinyramfs/init}"
: "${output:=/boot/tinyramfs-${kernel}}" : "${helper:=/usr/share/tinyramfs/device-helper}"
: "${output:=${TMPDIR:-/tmp}/tinyramfs-${kernel}}"
mkdir -p "${tmpdir:=${TMPDIR:-/tmp}/tinyramfs.$$}" mkdir -p "${tmpdir:=${TMPDIR:-/tmp}/tinyramfs.$$}"
@ -113,15 +118,15 @@ prepare_initramfs()
for _binary in \ for _binary in \
\[ sh ln env kill mkdir sleep mount \ \[ sh ln env kill mkdir sleep mount \
printf switch_root "${srcdir}/device-helper" printf switch_root "$helper"
do do
copy_binary "$_binary" copy_binary "$_binary"
done done
command -v blkid > /dev/null && copy_binary blkid command -v blkid > /dev/null && copy_binary blkid
copy_file "${srcdir}/init" /init 755 0 copy_file "$init" /init 755 0
copy_file "$config" /etc/tinyramfs/config 644 0 copy_file "$config" /etc/tinyramfs/config 644 0
} }
copy_file() copy_file()
@ -184,6 +189,9 @@ copy_binary()
copy_file "$binary" "/bin/${binary##*/}" 755 1 copy_file "$binary" "/bin/${binary##*/}" 755 1
# Skip copying binary dependencies if ldd not available.
command -v ldd || return 0
# Copy binary dependencies if any exist. # Copy binary dependencies if any exist.
ldd "$binary" 2> /dev/null | ldd "$binary" 2> /dev/null |
@ -289,25 +297,17 @@ copy_modules()
-type f 2> /dev/null | -type f 2> /dev/null |
while read -r _module || [ "$_module" ]; do while read -r _module || [ "$_module" ]; do
copy_file "$_module" "/lib/modules/${_module#$moddir}" 644 0
# 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 done
fi fi
copy_binary modprobe copy_binary modprobe
copy_file "${moddir}/${kernel}/modules.order" "/lib/modules/${kernel}/modules.order" 644 0 copy_file "${moddir}/${kernel}/modules.order" \
copy_file "${moddir}/${kernel}/modules.builtin" "/lib/modules/${kernel}/modules.builtin" 644 0 "/lib/modules/${kernel}/modules.order" 644 0
copy_file "${moddir}/${kernel}/modules.builtin" \
"/lib/modules/${kernel}/modules.builtin" 644 0
depmod -b "$tmpdir" "$kernel" depmod -b "$tmpdir" "$kernel"
} }
@ -327,8 +327,6 @@ make_initramfs()
print "done! check out $output" print "done! check out $output"
) )
[ "$(id -u)" = 0 ] || panic "must be run as root"
# Exit if command fails and disable globbing. # Exit if command fails and disable globbing.
set -ef set -ef