archboot/usr/bin/archboot-cpio.sh

91 lines
2.8 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
2023-08-11 17:19:18 +02:00
# SPDX-License-Identifier: GPL-3.0-or-later
#
# archboot-cpio.sh - modular tool for building initramfs images
2023-10-13 07:22:07 +02:00
# optimized for size and speed
2023-03-16 07:36:15 +01:00
# by Tobias Powalowski <tpowa@archlinux.org>
2023-07-24 20:28:58 +02:00
. /usr/lib/archboot/common.sh
. /usr/lib/archboot/cpio/cpio.sh
2023-10-12 20:30:36 +02:00
if [[ -z "${1}" ]]; then
2023-10-12 09:11:53 +02:00
_usage
fi
2023-10-12 08:44:48 +02:00
_root_check
2023-10-26 18:46:48 +02:00
_parameters "$@"
2023-09-04 09:33:55 +02:00
#shellcheck disable="SC1090"
2023-10-13 07:22:07 +02:00
. "${_CONFIG}" 2>"${_NO_LOG}" || _abort "Failed to read ${_CONFIG} configuration file"
2023-11-12 20:21:09 +01:00
echo "Using config: ${_CONFIG}"
2023-10-12 20:30:36 +02:00
if [[ -z "${_KERNEL}" ]]; then
echo "Trying to autodetect ${_RUNNING_ARCH} kernel..."
[[ "${_RUNNING_ARCH}" == "x86_64" || "${_RUNNING_ARCH}" == "riscv64" ]] && _KERNEL="/usr/lib/modules/*/vmlinuz"
[[ "${_RUNNING_ARCH}" == "aarch64" ]] && _KERNEL="/boot/Image.gz"
fi
2023-09-03 16:05:46 +02:00
# allow * in config
2023-09-04 11:48:33 +02:00
#shellcheck disable=SC2116,2086
2023-10-12 20:30:36 +02:00
_KERNEL="$(echo ${_KERNEL})"
if [[ ! -f "${_KERNEL}" ]]; then
2023-10-12 10:51:43 +02:00
_abort "kernel image does not exist!"
fi
2023-10-12 20:30:36 +02:00
_KERNELVERSION="$(_kver "${_KERNEL}")"
2023-10-28 22:14:59 +02:00
_MOD_DIR="/lib/modules/${_KERNELVERSION}"
[[ -d "${_MOD_DIR}" ]] || _abort "${_MOD_DIR} is not a valid kernel module directory!"
2024-06-18 17:51:06 +02:00
_ALL_MODS="$(fd -t f -u '.ko' "${_MOD_DIR}" 2>"${_NO_LOG}")"
2023-10-12 20:30:36 +02:00
_BUILD_DIR="$(_init_rootfs "${_KERNELVERSION}" "${_TARGET_DIR}")" || exit 1
2023-10-13 07:22:07 +02:00
_ROOTFS="${_TARGET_DIR:-${_BUILD_DIR}/root}"
2023-11-12 20:36:55 +01:00
if (( ${#_HOOKS[*]} == 0 )); then
_abort "No hooks found in config file!"
fi
echo "Using kernel: ${_KERNEL}"
echo "Detected kernel version: ${_KERNELVERSION}"
2023-10-13 09:45:30 +02:00
if [[ -n "${_GENERATE_IMAGE}" ]]; then
echo "Starting build: ${_GENERATE_IMAGE}"
elif [[ -n "${_TARGET_DIR}" ]]; then
echo "Starting build directory: ${_TARGET_DIR}"
else
echo "Starting dry run..."
fi
_HOOK_COUNT=1
2023-10-14 22:38:52 +02:00
_HOOKS_END_COUNT="$(echo "${_HOOKS[@]}" | wc -w)"
2023-10-13 09:45:30 +02:00
if [[ "${_HOOKS_END_COUNT}" -lt 10 ]]; then
_ADD_ZERO=""
else
2023-10-13 09:45:30 +02:00
_ADD_ZERO=1
fi
2023-10-13 09:45:30 +02:00
echo "Running ${_HOOKS_END_COUNT} hooks..."
for i in "${_HOOKS[@]}"; do
if [[ -n "${_ADD_ZERO}" && "${_HOOK_COUNT}" -lt 10 ]]; then
echo "0${_HOOK_COUNT}/${_HOOKS_END_COUNT}: ${i}"
else
echo "${_HOOK_COUNT}/${_HOOKS_END_COUNT}: ${i}"
fi
_run_hook "${i}"
_HOOK_COUNT="$((_HOOK_COUNT+1))"
done
2023-10-28 21:58:30 +02:00
if [[ -z "${_FILES}" ]]; then
echo "Skipping files..."
else
echo "Adding files..."
_install_files
fi
2023-10-18 16:34:21 +02:00
_install_libs
if [[ -z "${_MODS}" && -z "${_MOD_DEPS}" ]]; then
echo "Skipping kernel modules..."
else
2023-10-25 22:20:23 +02:00
echo "Adding kernel modules..."
_install_mods
fi
systemd-sysusers --root "${_ROOTFS}" &>"${_NO_LOG}"
2023-10-13 09:45:30 +02:00
ldconfig -r "${_ROOTFS}" &>"${_NO_LOG}" || exit 1
2023-10-12 10:51:43 +02:00
# remove /var/cache/ldconfig/aux-cache for reproducibility
rm -f -- "${_ROOTFS}/var/cache/ldconfig/aux-cache"
2023-10-12 09:06:10 +02:00
if [[ -n "${_GENERATE_IMAGE}" ]]; then
2024-07-21 13:44:47 +02:00
_create_cpio "${_ROOTFS}" "${_GENERATE_IMAGE}" || exit 1
2023-10-15 08:52:09 +02:00
_cleanup
2023-10-13 09:45:30 +02:00
elif [[ -n "${_TARGET_DIR}" ]]; then
2023-10-15 08:52:09 +02:00
_cleanup
2023-10-13 09:45:30 +02:00
echo "Build directory complete."
else
2023-10-15 08:52:09 +02:00
_cleanup
2023-10-13 07:22:07 +02:00
echo "Dry run complete."
fi