2023-03-15 22:22:25 +01:00
|
|
|
#!/usr/bin/env bash
|
2023-08-11 17:19:18 +02:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2023-03-15 22:22:25 +01:00
|
|
|
#
|
2023-10-11 19:28:56 +02:00
|
|
|
# archboot-cpio.sh - modular tool for building initramfs images
|
2023-10-12 20:30:36 +02:00
|
|
|
# focused and optimized for size and speed
|
2023-03-16 07:36:15 +01:00
|
|
|
# by Tobias Powalowski <tpowa@archlinux.org>
|
2023-03-15 22:22:25 +01:00
|
|
|
|
|
|
|
shopt -s extglob
|
|
|
|
|
2023-07-24 20:28:58 +02:00
|
|
|
. /usr/lib/archboot/common.sh
|
2023-10-12 09:38:24 +02:00
|
|
|
. /usr/lib/archboot/cpio/cpio.sh
|
2023-03-15 22:22:25 +01:00
|
|
|
|
2023-10-12 20:30:36 +02:00
|
|
|
if [[ -z "${1}" ]]; then
|
2023-10-12 09:11:53 +02:00
|
|
|
_usage
|
2023-04-23 21:31:41 +02:00
|
|
|
fi
|
2023-10-12 08:44:48 +02:00
|
|
|
_root_check
|
2023-07-24 20:28:58 +02:00
|
|
|
|
2023-03-15 22:22:25 +01:00
|
|
|
while :; do
|
2023-10-12 09:38:24 +02:00
|
|
|
case "${1}" in
|
|
|
|
-c) shift
|
2023-10-12 20:30:36 +02:00
|
|
|
_CONFIG="${1}"
|
2023-03-15 22:22:25 +01:00
|
|
|
;;
|
2023-10-12 09:38:24 +02:00
|
|
|
-k) shift
|
2023-10-12 20:30:36 +02:00
|
|
|
_KERNEL="${1}"
|
2023-03-15 22:22:25 +01:00
|
|
|
;;
|
2023-10-12 09:38:24 +02:00
|
|
|
-d) shift
|
2023-10-12 20:30:36 +02:00
|
|
|
_TARGET_DIR="${1}"
|
2023-03-15 22:22:25 +01:00
|
|
|
;;
|
2023-10-12 09:38:24 +02:00
|
|
|
-g) shift
|
2023-10-12 10:51:43 +02:00
|
|
|
[[ -d "${1}" ]] && _abort "Invalid image path -- ${1} is a directory!"
|
2023-10-12 20:30:36 +02:00
|
|
|
if ! _GENERATE_IMAGE="$(readlink -f "${1}")" || [[ ! -e "${_GENERATE_IMAGE%/*}" ]]; then
|
2023-10-12 10:51:43 +02:00
|
|
|
_abort "Unable to write to path!" "${1}"
|
2023-03-15 22:22:25 +01:00
|
|
|
fi
|
|
|
|
;;
|
2023-10-12 09:38:24 +02:00
|
|
|
-h) _usage
|
2023-10-12 09:11:53 +02:00
|
|
|
exit 0
|
2023-03-15 22:22:25 +01:00
|
|
|
;;
|
2023-10-12 09:38:24 +02:00
|
|
|
*) _usage ;;
|
2023-03-15 22:22:25 +01:00
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
2023-10-12 08:49:48 +02:00
|
|
|
|
2023-09-04 09:33:55 +02:00
|
|
|
#shellcheck disable="SC1090"
|
2023-10-12 20:30:36 +02:00
|
|
|
! . "${_CONFIG}" 2>"${_NO_LOG}" && _abort "Failed to read ${_CONFIG} configuration file"
|
|
|
|
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"
|
2023-03-15 22:22:25 +01:00
|
|
|
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})"
|
|
|
|
echo "Using kernel: ${_KERNEL}"
|
|
|
|
if [[ ! -f "${_KERNEL}" ]]; then
|
2023-10-12 10:51:43 +02:00
|
|
|
_abort "kernel image does not exist!"
|
2023-09-03 14:22:59 +02:00
|
|
|
fi
|
2023-10-12 20:30:36 +02:00
|
|
|
_KERNELVERSION="$(_kver "${_KERNEL}")"
|
|
|
|
_MODULE_DIR="/lib/modules/${_KERNELVERSION}"
|
|
|
|
[[ -d "${_MODULE_DIR}" ]] || _abort "${_MODULE_DIR} is not a valid kernel module directory!"
|
|
|
|
_BUILD_DIR="$(_init_rootfs "${_KERNELVERSION}" "${_TARGET_DIR}")" || exit 1
|
|
|
|
_ROOTFS="${_TARGET_DIR}:-${_BUILD_DIR}/root}"
|
|
|
|
if (( ${#_HOOKS[*]} == 0 )); then
|
|
|
|
_abort "No hooks found in config file!"
|
2023-03-15 22:22:25 +01:00
|
|
|
fi
|
2023-10-12 20:30:36 +02:00
|
|
|
if [[ -n "${_GENERATE_IMAGE}" || -n "${_TARGET_DIR}" ]]; then
|
|
|
|
echo "Starting build: ${_KERNELVERSION}"
|
2023-03-15 22:22:25 +01:00
|
|
|
else
|
2023-10-12 20:30:36 +02:00
|
|
|
echo "Starting dry run: ${_KERNELVERSION}"
|
2023-03-15 22:22:25 +01:00
|
|
|
fi
|
2023-10-12 20:30:36 +02:00
|
|
|
_builtin_modules
|
|
|
|
_map _run_hook "${_HOOKS[@]}"
|
|
|
|
_install_modules "${!_MOD_PATH[@]}"
|
2023-03-15 22:22:25 +01:00
|
|
|
# this is simply a nice-to-have -- it doesn't matter if it fails.
|
2023-10-12 10:51:43 +02:00
|
|
|
ldconfig -r "${_ROOTFS}" &>"${_NO_LOG}"
|
|
|
|
# remove /var/cache/ldconfig/aux-cache for reproducibility
|
|
|
|
rm -f -- "${_ROOTFS}/var/cache/ldconfig/aux-cache"
|
2023-04-15 21:18:18 +02:00
|
|
|
# Set umask to create initramfs images as 600
|
2023-03-15 22:22:25 +01:00
|
|
|
umask 077
|
2023-10-12 09:06:10 +02:00
|
|
|
if [[ -n "${_GENERATE_IMAGE}" ]]; then
|
2023-10-12 20:30:36 +02:00
|
|
|
_create_cpio "${_GENERATE_IMAGE}" "${_COMP}" || exit 1
|
2023-10-12 09:10:33 +02:00
|
|
|
elif [[ -n "${_TARGET_DIR}" ]]; then
|
2023-03-15 22:22:25 +01:00
|
|
|
msg "Build complete."
|
|
|
|
else
|
|
|
|
msg "Dry run complete, use -g IMAGE to generate a real image"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# vim: set ft=sh ts=4 sw=4 et:
|