summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@oper.io>2023-03-05 12:12:51 -0700
committerAaron Ball <nullspoon@oper.io>2023-03-05 12:13:28 -0700
commit6ead95901ccb3d9f53e5536a0b080b09961a33a7 (patch)
tree7a277239eff7a8da3402eeed5510f19c77d19d9c
parentdfcf646257566df65a07722c30564286935f394a (diff)
downloadmkinitramfs-master.tar.gz
mkinitramfs-master.tar.xz
Refactor mkinitramfsHEADmaster
This does many little things. First, this changes the `function <name>` syntax to `<name>()`. This also moves referencse from `echo` to `printf`. It also switches out the bash `[[ ]]` conditional blocks to more ubiquitous `[ ]`. This also simplifies the `get_first_path` function (which will later be replaced as it is pretty bad). Finally, this rewrites the `check_crypto_support` function. Rather than copy in all of the kernel modules, then removing unneeded ones, it has become easier to only copy what is needed, which further reduces the initramfs size.
-rwxr-xr-xmkinitramfs130
1 files changed, 65 insertions, 65 deletions
diff --git a/mkinitramfs b/mkinitramfs
index d38111d..2799838 100755
--- a/mkinitramfs
+++ b/mkinitramfs
@@ -31,18 +31,17 @@ declare -a fqbins
#
# @param bins All function arguments are the names of binaries to locate.
#
-function resolve_bins {
+resolve_bins() {
local args=${@}
+ local path=''
for i in ${args[@]}; do
- local path=$(type -p ${i} 2>/dev/null)
-
- if [[ $? -gt 0 ]]; then
- echo "Could not find binary ${i}. Is it installed?"
+ if ! path="$(type -p ${i} 2>/dev/null)"; then
+ printf 'Could not find binary %s. Is it installed?\n' "${i}"
exit 1
fi
- echo "Located ${i} at ${path}"
+ printf 'Located %s at %s\n' "${i}" "${path}"
fqbins+=("${path}")
done
}
@@ -51,34 +50,29 @@ function resolve_bins {
#
# Gets first path locatable in a string
#
-function get_first_path {
- local str=${1}
-
+get_first_path() {
+ local str="${1:-}"
local out=''
- local cursor=''
+ # Find the first occurrence of `/`
for ((i=0; i<${#str}; i++)); do
- if [[ ${str:$i:1} == '/' ]]; then
- cursor=${i}
- break
- fi
+ [ "${str:$i:1}" = '/' ] && break
done
# Exit if no path found
- [[ ${cursor} == '' ]] && return
+ [ "${i}" -eq "${#str}" ] && return 2
- while [[ ${str:$cursor:1} != ' ' ]] && [[ ${cursor} -lt ${#str} ]]; do
- out="${out}${str:$cursor:1}"
- cursor=$((${cursor} + 1))
+ # Find end of string by looking for space
+ while [ "${str:$i:1}" != ' ' ] && [ "${i}" -lt "${#str}" ]; do
+ out="${out}${str:$i:1}"
+ (( i += 1 ))
done
-
- echo "${out}"
+ printf '%s\n' "${out}"
}
-function get_deps {
- local bin=${1}
-
+get_deps() {
+ local bin="${1:-}"
for dep in $(ldd ${bin}); do
get_first_path "${dep}"
done
@@ -92,22 +86,22 @@ function get_deps {
# @param cache Path to the uncompressed source directory to be archived
# @param version Version of initrd archive
#
-function mkcpio {
- [[ -z ${1} ]] && echo "Initramfs cache path required." && exit 1
- [[ -z ${2} ]] && echo "Kernel version required." && exit 1
+mkcpio() {
+ [ -z "${1:-}" ] && printf 'Initramfs cache path required.\n' && exit 1
+ [ -z "${2:-}" ] && printf 'Kernel version required.\n' && exit 1
- local cache=${1}
- local version=${2}
+ local cache="${1}"
+ local version="${2}"
- cd ${cache}
+ cd "${cache}"
fspath="/boot/initrd-${version}"
# Notify user of initrd overwriting
- [[ -f ${fspath} ]] && echo -e "\n\n${fspath} exists. Overwriting.\n"
+ [ -f "${fspath}" ] && printf "\n\n%s exists. Overwriting.\n" "${fspath}"
# Create the initrd
- echo "Building initrd to ${fspath}"
- find . -print0 | cpio --null -o --format=newc | xz -C crc32 -9 -c > ${fspath}
+ printf 'Building initrd to %s\n' "${fspath}"
+ find . -print0 | cpio --null -o --format=newc | xz -C crc32 -9 -c > "${fspath}"
}
@@ -117,18 +111,16 @@ function mkcpio {
#
# @param cache Path to the cache directory. Will be created if not exists
#
-function cache_dir_setup {
- [[ -z ${1} ]] && echo "Please specify a cache dir." && exit 1
- local cache=${1}
+cache_dir_setup() {
+ [ -z "${1}" ] && printf 'Please specify a cache dir.\n' && exit 1
+ local cache="${1:-}"
# Clean the cache so we start fresh
- rm -rf ${cache} && mkdir ${cache}
-
- local dirs=(bin dev etc lib lib32 lib64 mnt/root proc root run sbin sys usr)
+ rm -r "${cache:?}" && mkdir "${cache}"
# Create the temporary directory structure
- for i in ${dirs[*]}; do
- mkdir -p ${cache}/${i}
+ for i in bin dev etc lib lib32 lib64 mnt/root proc root run sbin sys usr/share; do
+ mkdir -p "${cache}/${i}"
done
# Copy in terminals database for `l` (linux - stock tty)
@@ -143,23 +135,31 @@ function cache_dir_setup {
#
# @param version Version of kernel modules to check
#
-function check_crypto_support {
- [[ -z ${1} ]] && echo "Kernel version required." && exit 1
- local version=${1}
+check_crypto_support() {
+ [ -z "${1:-}" ] && printf 'Kernel version required.\n' && exit 1
+ local version="${1}"
local buf=''
-
- builtinpath=/lib/modules/${version}/modules.builtin
-
- mkdir -p ${cache}/lib/modules/
- cp -vr "/lib/modules/${version}/" "${cache}/lib/modules/${version}"
-
- # Strip out kernel modules not required for bootstrapping
- for i in virt sound drivers/{bluetooth,gpu,hid,media,mmc,net,video,virtio}; do
- buf="${cache}/lib/modules/${version}/kernel/${i}"
- if [ -d "${buf}" ]; then
- printf 'Stripping %s from initramfs\n' "${buf}"
- rm -r "${buf:?}"
- fi
+ local -a copy=(
+ arch/x86/crypto
+ crypto
+ drivers/input/{keyboard,serio,*.ko}
+ drivers/{crypto,usb,platform,md}
+ lib
+ fs/{btrfs,xfs,ext4}
+ )
+
+ mkdir -p "${cache}/lib/modules/${version}"
+ cp -v /lib/modules/${version}/modules*.bin "${cache}/lib/modules/${version}/"
+
+ # Copy in all the rewuired kernel modules
+ for i in ${copy[@]}; do
+ buf=(/lib/modules/${version}/kernel/${i})
+ # For globbing in the array
+ for ent in ${buf[@]}; do
+ printf 'Copying in %s\n' "${ent}"
+ mkdir -p "$(dirname ${cache}/${ent})"
+ cp -r "${ent}" "${cache}/${ent}"
+ done
done
}
@@ -170,21 +170,21 @@ function check_crypto_support {
#
# @param version Version to check for installation status
#
-function check_kernel_version {
- [[ -z ${1} ]] && echo "Kernel version required." && exit 1
- local version=${1}
+check_kernel_version() {
+ [ -z ${1} ] && printf 'Kernel version required.\n' && exit 1
+ local version="${1}"
+ local modulespath="/lib/modules/${version}"
- modulespath=/lib/modules/${version}
- if [[ ! -d ${modulespath} ]]; then
- echo "Error: Could not find ${modulespath}."
- echo "Has kernel version ${version} been comiled?"
+ if [ ! -d ${modulespath} ]; then
+ printf 'Error: Could not find %s.\n' "${modulespath}"
+ printf 'Has kernel version %s been comiled?' "${version}"
exit 1
fi
}
-function main {
- [[ -z ${1} ]] && echo "Please specify a kernel version" && exit 1
+main() {
+ [ -z "${1:-}" ] && printf 'Please specify a kernel version\n' && exit 1
local version=${1}

Generated by cgit