mirror of
https://gitlab.archlinux.org/tpowa/archboot.git
synced 2024-09-20 03:50:37 +02:00
remove empty lines, remove not needed things
This commit is contained in:
parent
3a6a3bc2c6
commit
3c41961280
3 changed files with 78 additions and 305 deletions
|
@ -8,21 +8,16 @@
|
|||
|
||||
shopt -s extglob
|
||||
|
||||
### globals within mkinitcpio, but not intended to be used by hooks
|
||||
|
||||
# needed files/directories
|
||||
_f_functions=/usr/lib/archboot/cpio.sh
|
||||
_f_config=
|
||||
_d_hooks=/lib/initcpio/hooks
|
||||
_d_install=/lib/initcpio/install
|
||||
|
||||
# options and runtime data
|
||||
_optgenimg=''
|
||||
_opttargetdir=''
|
||||
_optosrelease=''
|
||||
_optquiet=1 _optcolor=1
|
||||
declare -A _addedmodules _modpaths
|
||||
|
||||
# Sanitize environment further
|
||||
# GREP_OPTIONS="--color=always" will break everything
|
||||
# CDPATH can affect cd and pushd
|
||||
|
@ -34,11 +29,11 @@ usage() {
|
|||
usage: ${0##*/} [options]
|
||||
|
||||
Options:
|
||||
-c, --config <config> Use config file
|
||||
-g, --generate <path> Generate cpio image and write to specified path
|
||||
-h, --help Display this message and exit
|
||||
-k, --kernel <kernel> Use specified kernel
|
||||
-d, --generatedir <dir> Write generated image into <dir>
|
||||
-c <config> Use config file
|
||||
-g <path> Generate cpio image and write to specified path
|
||||
-h Display this message and exit
|
||||
-k <kernel> Use specified kernel
|
||||
-d <dir> Write generated image into <dir>
|
||||
EOF
|
||||
}
|
||||
|
||||
|
@ -54,27 +49,21 @@ cleanup() {
|
|||
|
||||
resolve_kernver() {
|
||||
local kernel="$1"
|
||||
|
||||
if [[ "${kernel:0:1}" != / ]]; then
|
||||
echo "$kernel"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [[ ! -e "$kernel" ]]; then
|
||||
error "specified kernel image does not exist: '%s'" "$kernel"
|
||||
return 1
|
||||
fi
|
||||
|
||||
kver "$kernel" && return
|
||||
|
||||
error "invalid kernel specified: '%s'" "$1"
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
build_image() {
|
||||
local out="$1" compressout="$1" compress="$2" errmsg pipestatus
|
||||
|
||||
case "$compress" in
|
||||
cat)
|
||||
msg "Creating uncompressed initcpio image: '%s'" "$out"
|
||||
|
@ -93,53 +82,41 @@ build_image() {
|
|||
COMPRESSION_OPTIONS=('-T0' "${COMPRESSION_OPTIONS[@]}")
|
||||
;;
|
||||
esac
|
||||
|
||||
if [[ -f "$out" ]]; then
|
||||
local curr_size space_left_on_device
|
||||
|
||||
curr_size="$(stat --format="%s" "$out")"
|
||||
space_left_on_device="$(($(stat -f --format="%a*%S" "$out")))"
|
||||
|
||||
# check if there is enough space on the device to write the image to a tempfile, fallback otherwise
|
||||
# this assumes that the new image is not more than 1¼ times the size of the old one
|
||||
(( $((curr_size + (curr_size/4))) < space_left_on_device )) && compressout="$out".tmp
|
||||
fi
|
||||
|
||||
pushd "$BUILDROOT" >/dev/null || return
|
||||
|
||||
# Reproducibility: set all timestamps to 0
|
||||
find . -mindepth 1 -execdir touch -hcd "@0" "{}" +
|
||||
|
||||
# If this pipeline changes, |pipeprogs| below needs to be updated as well.
|
||||
find . -mindepth 1 -printf '%P\0' |
|
||||
sort -z |
|
||||
LANG=C bsdtar --null -cnf - -T - |
|
||||
LANG=C bsdtar --null -cf - --format=newc @- |
|
||||
$compress "${COMPRESSION_OPTIONS[@]}" > "$compressout"
|
||||
|
||||
pipestatus=("${PIPESTATUS[@]}")
|
||||
pipeprogs=('find' 'sort' 'bsdtar (step 1)' 'bsdtar (step 2)' "$compress")
|
||||
|
||||
popd >/dev/null || return
|
||||
|
||||
for (( i = 0; i < ${#pipestatus[*]}; ++i )); do
|
||||
if (( pipestatus[i] )); then
|
||||
errmsg="${pipeprogs[i]} reported an error"
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
if (( _builderrors )); then
|
||||
warning "errors were encountered during the build. The image may not be complete."
|
||||
fi
|
||||
|
||||
if [[ -n "$errmsg" ]]; then
|
||||
error "Image generation FAILED: '%s'" "$errmsg"
|
||||
return 1
|
||||
elif (( _builderrors == 0 )); then
|
||||
msg "Image generation successful"
|
||||
fi
|
||||
|
||||
# sync and rename as we only wrote to a tempfile so far to ensure consistency
|
||||
if [[ "$compressout" != "$out" ]]; then
|
||||
sync -d -- "$compressout"
|
||||
|
@ -150,11 +127,9 @@ build_image() {
|
|||
preload_builtin_modules() {
|
||||
local modname field value
|
||||
local -a path
|
||||
|
||||
# Prime the _addedmodules list with the builtins for this kernel. We prefer
|
||||
# the modinfo file if it exists, but this requires a recent enough kernel
|
||||
# and kmod>=27.
|
||||
|
||||
if [[ -r $_d_kmoduledir/modules.builtin.modinfo ]]; then
|
||||
while IFS=.= read -rd '' modname field value; do
|
||||
_addedmodules[${modname//-/_}]=2
|
||||
|
@ -164,7 +139,6 @@ preload_builtin_modules() {
|
|||
;;
|
||||
esac
|
||||
done <"$_d_kmoduledir/modules.builtin.modinfo"
|
||||
|
||||
elif [[ -r "$_d_kmoduledir/modules.builtin" ]]; then
|
||||
while IFS=/ read -ra path; do
|
||||
modname="${path[-1]%.ko}"
|
||||
|
@ -175,22 +149,14 @@ preload_builtin_modules() {
|
|||
|
||||
# shellcheck source=functions
|
||||
. "$_f_functions"
|
||||
|
||||
_opt_short='c:d:g:hk:'
|
||||
_opt_long=('config:' 'generate:' 'help'
|
||||
'kernel:'
|
||||
'generatedir:'
|
||||
'osrelease:')
|
||||
|
||||
parseopts "$_opt_short" "${_opt_long[@]}" -- "$@" || exit 1
|
||||
parseopts "$_opt_short" -- "$@" || exit 1
|
||||
set -- "${OPTRET[@]}"
|
||||
unset _opt_short _opt_long OPTRET
|
||||
|
||||
unset _opt_short OPTRET
|
||||
if [[ -z "$1" ]]; then
|
||||
usage
|
||||
cleanup 0
|
||||
fi
|
||||
|
||||
while :; do
|
||||
case "$1" in
|
||||
-c|--config)
|
||||
|
@ -223,35 +189,27 @@ while :; do
|
|||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
if [[ -t 1 ]] && (( _optcolor )); then
|
||||
try_enable_color
|
||||
fi
|
||||
|
||||
# insist that /proc and /dev be mounted (important for chroots)
|
||||
# NOTE: avoid using mountpoint for this -- look for the paths that we actually
|
||||
# use in mkinitcpio. Avoids issues like FS#26344.
|
||||
[[ -e /proc/self/mountinfo ]] || die "/proc must be mounted!"
|
||||
[[ -e /dev/fd ]] || die "/dev must be mounted!"
|
||||
|
||||
if [[ "$KERNELVERSION" != 'none' ]]; then
|
||||
KERNELVERSION="$(resolve_kernver "$KERNELVERSION")" || exit 1
|
||||
_d_kmoduledir="/lib/modules/$KERNELVERSION"
|
||||
[[ -d "$_d_kmoduledir" ]] || die "'$_d_kmoduledir' is not a valid kernel module directory"
|
||||
fi
|
||||
|
||||
_d_workdir="$(initialize_buildroot "$KERNELVERSION" "$_opttargetdir")" || exit 1
|
||||
BUILDROOT="${_opttargetdir:-$_d_workdir/root}"
|
||||
|
||||
# shellcheck source=mkinitcpio.conf
|
||||
! . "$_f_config" 2>/dev/null && die "Failed to read configuration '%s'" "$_f_config"
|
||||
|
||||
_hooks=("${HOOKS[@]}")
|
||||
|
||||
if (( ${#_hooks[*]} == 0 )); then
|
||||
die "Invalid config: No hooks found"
|
||||
fi
|
||||
|
||||
if [[ -n "$_optgenimg" ]]; then
|
||||
# check for permissions. if the image doesn't already exist,
|
||||
# then check the directory
|
||||
|
@ -259,39 +217,30 @@ if [[ -n "$_optgenimg" ]]; then
|
|||
( ! -d ${_optgenimg%/*} || ! -w ${_optgenimg%/*} ) ]]; then
|
||||
die "Unable to write to '%s'" "$_optgenimg"
|
||||
fi
|
||||
|
||||
msg "Starting build: '%s'" "$KERNELVERSION"
|
||||
elif [[ -n "$_opttargetdir" ]]; then
|
||||
msg "Starting build: '%s'" "$KERNELVERSION"
|
||||
else
|
||||
msg "Starting dry run: '%s'" "$KERNELVERSION"
|
||||
fi
|
||||
|
||||
# set functrace and trap to catch errors in add_* functions
|
||||
declare -i _builderrors=0
|
||||
|
||||
preload_builtin_modules
|
||||
|
||||
map run_build_hook "${_hooks[@]}" || (( ++_builderrors ))
|
||||
|
||||
install_modules "${!_modpaths[@]}"
|
||||
|
||||
# this is simply a nice-to-have -- it doesn't matter if it fails.
|
||||
ldconfig -r "$BUILDROOT" &>/dev/null
|
||||
# remove /var/cache/ldconfig/aux-cache for reproducability
|
||||
rm -f -- "$BUILDROOT/var/cache/ldconfig/aux-cache"
|
||||
|
||||
# Set umask to create initramfs images as 600
|
||||
umask 077
|
||||
|
||||
if [[ -n "$_optgenimg" ]]; then
|
||||
build_image "$_optgenimg" || exit 1
|
||||
build_image "$_optgenimg" "${COMPRESSION}" || exit 1
|
||||
elif [[ -n "$_opttargetdir" ]]; then
|
||||
msg "Build complete."
|
||||
else
|
||||
msg "Dry run complete, use -g IMAGE to generate a real image"
|
||||
fi
|
||||
|
||||
exit $(( !!_builderrors ))
|
||||
|
||||
# vim: set ft=sh ts=4 sw=4 et:
|
||||
|
|
|
@ -2,49 +2,14 @@
|
|||
# SPDX-License-Identifier: GPL-2.0-only
|
||||
# simplified, stripped down, optimized for size and speed
|
||||
# Arch Linux mkinitcpio - modular tool for building an initramfs images
|
||||
# created by Tobias Powalowski <tpowa@archlinux.org>
|
||||
# by Tobias Powalowski <tpowa@archlinux.org>
|
||||
|
||||
# no long options support in archboot
|
||||
parseopts() {
|
||||
local opt='' optarg='' i='' shortopts="$1"
|
||||
local -a longopts=() unused_argv=()
|
||||
|
||||
local -a unused_argv=()
|
||||
shift
|
||||
while [[ -n "$1" && "$1" != '--' ]]; do
|
||||
longopts+=("$1")
|
||||
shift
|
||||
done
|
||||
shift
|
||||
|
||||
longoptmatch() {
|
||||
local o longmatch=()
|
||||
for o in "${longopts[@]}"; do
|
||||
if [[ "${o%:}" == "$1" ]]; then
|
||||
longmatch=("$o")
|
||||
break
|
||||
fi
|
||||
[[ "${o%:}" == "$1"* ]] && longmatch+=("$o")
|
||||
done
|
||||
|
||||
case "${#longmatch[*]}" in
|
||||
1)
|
||||
# success, override with opt and return arg req (0 == none, 1 == required)
|
||||
opt="${longmatch%:}"
|
||||
if [[ "${longmatch[*]}" == *: ]]; then
|
||||
return 1
|
||||
else
|
||||
return 0
|
||||
fi ;;
|
||||
0)
|
||||
# fail, no match found
|
||||
return 255 ;;
|
||||
*)
|
||||
# fail, ambiguous match
|
||||
printf "%s: option '%s' is ambiguous; possibilities:%s\n" "${0##*/}" \
|
||||
"--$1" "$(printf " '%s'" "${longmatch[@]%:}")"
|
||||
return 254 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
while (( $# )); do
|
||||
case "$1" in
|
||||
--) # explicit end of options
|
||||
|
@ -54,14 +19,12 @@ parseopts() {
|
|||
-[!-]*) # short option
|
||||
for (( i = 1; i < ${#1}; i++ )); do
|
||||
opt=${1:i:1}
|
||||
|
||||
# option doesn't exist
|
||||
if [[ $shortopts != *$opt* ]]; then
|
||||
printf "%s: invalid option -- '%s'\n" "${0##*/}" "$opt"
|
||||
OPTRET=(--)
|
||||
return 1
|
||||
fi
|
||||
|
||||
OPTRET+=("-$opt")
|
||||
# option requires optarg
|
||||
if [[ "$shortopts" == *"${opt}:"* ]]; then
|
||||
|
@ -83,169 +46,37 @@ parseopts() {
|
|||
fi
|
||||
done
|
||||
;;
|
||||
--?*=*|--?*) # long option
|
||||
IFS='=' read -r opt optarg <<< "${1#--}"
|
||||
longoptmatch "$opt"
|
||||
case $? in
|
||||
0)
|
||||
if [[ -n "$optarg" ]]; then
|
||||
printf "%s: option '--%s' doesn't allow an argument\n" "${0##*/}" "$opt"
|
||||
OPTRET=(--)
|
||||
return 1
|
||||
else
|
||||
OPTRET+=("--$opt")
|
||||
fi
|
||||
;;
|
||||
1)
|
||||
# --longopt=optarg
|
||||
if [[ -n "$optarg" ]]; then
|
||||
OPTRET+=("--$opt" "$optarg")
|
||||
# --longopt optarg
|
||||
elif [[ -n "$2" ]]; then
|
||||
OPTRET+=("--$opt" "$2" )
|
||||
shift
|
||||
else
|
||||
printf "%s: option '--%s' requires an argument\n" "${0##*/}" "$opt"
|
||||
OPTRET=(--)
|
||||
return 1
|
||||
fi
|
||||
;;
|
||||
254)
|
||||
# ambiguous option -- error was reported for us by longoptmatch()
|
||||
OPTRET=(--)
|
||||
return 1
|
||||
;;
|
||||
255)
|
||||
# parse failure
|
||||
printf "%s: unrecognized option '%s'\n" "${0##*/}" "--$opt"
|
||||
OPTRET=(--)
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
;;
|
||||
*) # non-option arg encountered, add it as a parameter
|
||||
unused_argv+=("$1")
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
# add end-of-opt terminator and any leftover positional parameters
|
||||
OPTRET+=('--' "${unused_argv[@]}" "$@")
|
||||
unset longoptmatch
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
kver_x86() {
|
||||
local kver
|
||||
local -i offset
|
||||
# scrape the version out of the kernel image. locate the offset
|
||||
# to the version string by reading 2 bytes out of image at at
|
||||
# address 0x20E. this leads us to a string of, at most, 128 bytes.
|
||||
# read the first word from this string as the kernel version.
|
||||
offset="$(od -An -j0x20E -dN2 "$1")" || return
|
||||
|
||||
read -r kver _ < \
|
||||
<(dd if="$1" bs=1 count=127 skip=$((offset + 0x200)) 2>/dev/null)
|
||||
|
||||
printf '%s' "$kver"
|
||||
}
|
||||
|
||||
detect_compression() {
|
||||
local bytes
|
||||
|
||||
bytes="$(od -An -t x1 -N6 "$1" | tr -dc '[:alnum:]')"
|
||||
case "$bytes" in
|
||||
'fd377a585a00')
|
||||
echo 'xz'
|
||||
return
|
||||
;;
|
||||
esac
|
||||
|
||||
bytes="$(od -An -t x1 -N4 "$1" | tr -dc '[:alnum:]')"
|
||||
if [[ "$bytes" == '894c5a4f' ]]; then
|
||||
echo 'lzop'
|
||||
return
|
||||
fi
|
||||
|
||||
bytes="$(od -An -t x2 -N2 "$1" | tr -dc '[:alnum:]')"
|
||||
if [[ "$bytes" == '8b1f' ]]; then
|
||||
echo 'gzip'
|
||||
return
|
||||
fi
|
||||
|
||||
bytes="$(od -An -t x4 -N4 "$1" | tr -dc '[:alnum:]')"
|
||||
case "$bytes" in
|
||||
'184d2204')
|
||||
error 'Newer lz4 stream format detected! This may not boot!'
|
||||
echo 'lz4'
|
||||
return
|
||||
;;
|
||||
'184c2102')
|
||||
echo 'lz4 -l'
|
||||
return
|
||||
;;
|
||||
'fd2fb528')
|
||||
echo 'zstd'
|
||||
return
|
||||
;;
|
||||
esac
|
||||
|
||||
bytes="$(od -An -c -N3 "$1" | tr -dc '[:alnum:]')"
|
||||
if [[ "$bytes" == 'BZh' ]]; then
|
||||
echo 'bzip2'
|
||||
return
|
||||
fi
|
||||
|
||||
# lzma detection sucks and there's really no good way to
|
||||
# do it without reading large portions of the stream. this
|
||||
# check is good enough for GNU tar, apparently, so it's good
|
||||
# enough for me.
|
||||
bytes="$(od -An -t x1 -N3 "$1" | tr -dc '[:alnum:]')"
|
||||
if [[ "$bytes" == '5d0000' ]]; then
|
||||
echo 'lzma'
|
||||
return
|
||||
fi
|
||||
|
||||
# out of ideas, assuming uncompressed
|
||||
}
|
||||
|
||||
kver_generic() {
|
||||
# For unknown architectures, we can try to grep the uncompressed or gzipped
|
||||
# image for the boot banner.
|
||||
# This should work at least for ARM when run on /boot/Image, or RISC-V on
|
||||
# gzipped /boot/vmlinuz-linuz. On other architectures it may be worth trying
|
||||
# rather than bailing, and inform the user if none was found.
|
||||
|
||||
# Loosely grep for `linux_banner`:
|
||||
# https://elixir.bootlin.com/linux/v5.7.2/source/init/version.c#L46
|
||||
local kver='' reader='cat'
|
||||
|
||||
[[ "$(detect_compression "$1")" == 'gzip' ]] && reader='zcat'
|
||||
|
||||
read -r _ _ kver _ < <($reader "$1" | grep -m1 -aoE 'Linux version .(\.[-[:alnum:]+]+)+')
|
||||
|
||||
printf '%s' "$kver"
|
||||
}
|
||||
|
||||
kver() {
|
||||
# this is intentionally very loose. only ensure that we're
|
||||
# dealing with some sort of string that starts with something
|
||||
# resembling dotted decimal notation. remember that there's no
|
||||
# requirement for CONFIG_LOCALVERSION to be set.
|
||||
local kver re='^[[:digit:]]+(\.[[:digit:]]+)+'
|
||||
|
||||
local arch
|
||||
local arch bytes reader
|
||||
arch="$(uname -m)"
|
||||
if [[ $arch == @(i?86|x86_64) ]]; then
|
||||
kver="$(kver_x86 "$1")"
|
||||
local -i offset
|
||||
offset="$(od -An -j0x20E -dN2 "$1")" || return
|
||||
read -r kver _ < \
|
||||
<(dd if="$1" bs=1 count=127 skip=$((offset + 0x200)) 2>/dev/null)
|
||||
else
|
||||
kver="$(kver_generic "$1")"
|
||||
reader='cat'
|
||||
bytes="$(od -An -t x2 -N2 "$1" | tr -dc '[:alnum:]')"
|
||||
[[ "$bytes" == '8b1f' ]] && reader='zcat'
|
||||
read -r _ _ kver _ < <($reader "$1" | grep -m1 -aoE 'Linux version .(\.[-[:alnum:]+]+)+')
|
||||
fi
|
||||
|
||||
[[ "$kver" =~ $re ]] || return 1
|
||||
|
||||
printf '%s' "$kver"
|
||||
}
|
||||
|
||||
|
@ -294,16 +125,47 @@ modprobe() {
|
|||
command modprobe -d "$_optmoduleroot" -S "$KERNELVERSION" "$@"
|
||||
}
|
||||
|
||||
all_modules() {
|
||||
# Add modules to the initcpio, filtered by grep.
|
||||
# $@: filter arguments to grep
|
||||
# -f FILTER: ERE to filter found modules
|
||||
local -i count=0
|
||||
local mod='' OPTIND='' OPTARG='' modfilter=()
|
||||
while getopts ':f:' flag; do
|
||||
[[ "$flag" = "f" ]] && modfilter+=("$OPTARG")
|
||||
done
|
||||
shift $(( OPTIND - 1 ))
|
||||
# _d_kmoduledir is assigned in mkinitcpio
|
||||
# shellcheck disable=SC2154
|
||||
while read -r -d '' mod; do
|
||||
(( ++count ))
|
||||
for f in "${modfilter[@]}"; do
|
||||
[[ "$mod" =~ $f ]] && continue 2
|
||||
done
|
||||
mod="${mod##*/}"
|
||||
mod="${mod%.ko*}"
|
||||
printf '%s\n' "${mod//-/_}"
|
||||
done < <(find "$_d_kmoduledir" -name '*.ko*' -print0 2>/dev/null | grep -EZz "$@")
|
||||
(( count ))
|
||||
}
|
||||
|
||||
add_all_modules() {
|
||||
# Add modules to the initcpio.
|
||||
# $@: arguments to all_modules
|
||||
local mod
|
||||
local -a mods
|
||||
mapfile -t mods < <(all_modules "$@")
|
||||
map add_module "${mods[@]}"
|
||||
return $(( !${#mods[*]} ))
|
||||
}
|
||||
|
||||
add_firmware() {
|
||||
# add a firmware file to the image.
|
||||
# $1: firmware path fragment
|
||||
|
||||
local fw fwpath
|
||||
local -a fwfile
|
||||
local -i r=1
|
||||
|
||||
fwpath=/lib/firmware
|
||||
|
||||
for fw; do
|
||||
# shellcheck disable=SC2154
|
||||
if ! compgen -G "${BUILDROOT}${fwpath}/${fw}?(.*)" &>/dev/null; then
|
||||
|
@ -320,18 +182,13 @@ add_module() {
|
|||
# Add a kernel module to the initcpio image. Dependencies will be
|
||||
# discovered and added.
|
||||
# $1: module name
|
||||
|
||||
local target='' module='' softdeps=() deps=() field='' value='' firmware=()
|
||||
|
||||
if [[ "$1" == *\? ]]; then
|
||||
set -- "${1%?}"
|
||||
fi
|
||||
|
||||
target="${1%.ko*}" target="${target//-/_}"
|
||||
|
||||
# skip expensive stuff if this module has already been added
|
||||
(( _addedmodules["$target"] == 1 )) && return
|
||||
|
||||
while IFS=':= ' read -r -d '' field value; do
|
||||
case "$field" in
|
||||
filename)
|
||||
|
@ -361,7 +218,6 @@ add_module() {
|
|||
;;
|
||||
esac
|
||||
done < <(modinfo -b "$_optmoduleroot" -k "$KERNELVERSION" -0 "$target" 2>/dev/null)
|
||||
|
||||
if (( ${#firmware[*]} )); then
|
||||
add_firmware "${firmware[@]}"
|
||||
fi
|
||||
|
@ -371,13 +227,9 @@ add_checked_modules() {
|
|||
# Add modules to the initcpio, filtered by the list of autodetected
|
||||
# modules.
|
||||
# $@: arguments to all_modules
|
||||
|
||||
local mods
|
||||
|
||||
mapfile -t mods < <(all_modules "$@")
|
||||
|
||||
map add_module "${mods[@]}"
|
||||
|
||||
return $(( !${#mods[*]} ))
|
||||
}
|
||||
|
||||
|
@ -395,13 +247,11 @@ add_dir() {
|
|||
# $1: pathname on initcpio
|
||||
# $2: mode (optional)
|
||||
local mode="${2:-755}"
|
||||
|
||||
# shellcheck disable=SC2153
|
||||
if [[ -d "${BUILDROOT}${1}" ]]; then
|
||||
# ignore dir already exists
|
||||
return 0
|
||||
fi
|
||||
|
||||
command mkdir -p -m "${mode}" "${BUILDROOT}${1}" || return 1
|
||||
}
|
||||
|
||||
|
@ -410,9 +260,7 @@ add_symlink() {
|
|||
# to ensure that the target of the symlink exists.
|
||||
# $1: pathname of symlink on image
|
||||
# $2: absolute path to target of symlink (optional, can be read from $1)
|
||||
|
||||
local name="$1" target="${2:-$1}" linkobject
|
||||
|
||||
# find out the link target
|
||||
if [[ "$name" == "$target" ]]; then
|
||||
linkobject="$(find "$target" -prune -printf '%l')"
|
||||
|
@ -426,21 +274,23 @@ add_symlink() {
|
|||
elif [[ -L "$target" ]]; then
|
||||
target="$(realpath -eq -- "$target")"
|
||||
fi
|
||||
|
||||
add_dir "${name%/*}"
|
||||
ln -sfn "$target" "${BUILDROOT}${name}"
|
||||
}
|
||||
|
||||
# no runscript support in archboot
|
||||
add_runscript() {
|
||||
:
|
||||
}
|
||||
|
||||
add_file() {
|
||||
# Add a plain file to the initcpio image. No parsing is performed and only
|
||||
# the singular file is added.
|
||||
# $1: path to file
|
||||
# $2: destination on initcpio (optional, defaults to same as source)
|
||||
# $3: mode
|
||||
|
||||
# determine source and destination
|
||||
local src="$1" dest="${2:-$1}" mode="$3" srcrealpath
|
||||
|
||||
if [[ ! -e "${BUILDROOT}${dest}" ]]; then
|
||||
if [[ "$src" != "$dest" ]]; then
|
||||
command tar --hard-dereference --transform="s|$src|$dest|" -C / -cpf - ."$src" | tar -C "${BUILDROOT}" -xpf - || return 1
|
||||
|
@ -463,19 +313,14 @@ add_binary() {
|
|||
# be discovered and added.
|
||||
# $1: path to binary
|
||||
# $2: destination on initcpio (optional, defaults to same as source)
|
||||
|
||||
local line='' regex='' binary='' dest='' mode='' sodep=''
|
||||
|
||||
if [[ "${1:0:1}" != '/' ]]; then
|
||||
binary="$(type -P "$1")"
|
||||
else
|
||||
binary="$1"
|
||||
fi
|
||||
|
||||
dest="${2:-$binary}"
|
||||
|
||||
add_file "$binary" "$dest"
|
||||
|
||||
# non-binaries
|
||||
if ! lddout="$(ldd "$binary" 2>/dev/null)"; then
|
||||
return 0
|
||||
|
@ -486,33 +331,43 @@ add_binary() {
|
|||
if [[ "$line" =~ $regex ]]; then
|
||||
sodep="${BASH_REMATCH[2]}"
|
||||
fi
|
||||
|
||||
if [[ -f "$sodep" && ! -e "${BUILDROOT}${sodep}" ]]; then
|
||||
add_file "$sodep" "$sodep"
|
||||
fi
|
||||
done <<< "$lddout"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# no detection of helpers in archboot
|
||||
add_udev_rule() {
|
||||
# Add an udev rules file to the initcpio image. Dependencies on binaries
|
||||
# will be discovered and added.
|
||||
# $1: path to rules file (or name of rules file)
|
||||
local rules="$1"
|
||||
if [[ "${rules:0:1}" != '/' ]]; then
|
||||
rules="$(PATH='/usr/lib/udev/rules.d' type -P "$rules")"
|
||||
fi
|
||||
if [[ -z "$rules" ]]; then
|
||||
# complain about not found rules
|
||||
return 1
|
||||
fi
|
||||
add_file "$rules" /usr/lib/udev/rules.d/"${rules##*/}"
|
||||
}
|
||||
|
||||
initialize_buildroot() {
|
||||
# creates a temporary directory for the buildroot and initialize it with a
|
||||
# basic set of necessary directories and symlinks
|
||||
|
||||
local workdir='' kernver="$1" arch buildroot osreleasefile
|
||||
arch="$(uname -m)"
|
||||
|
||||
if ! workdir="$(mktemp -d --tmpdir mkinitcpio.XXXXXX)"; then
|
||||
error 'Failed to create temporary working directory in %s' "${TMPDIR:-/tmp}"
|
||||
return 1
|
||||
fi
|
||||
buildroot="${2:-$workdir/root}"
|
||||
|
||||
if [[ ! -w "${2:-$workdir}" ]]; then
|
||||
error 'Unable to write to build root: %s' "$buildroot"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# base directory structure
|
||||
install -dm755 "$buildroot"/{new_root,proc,sys,dev,run,tmp,var,etc,usr/{local{,/bin,/sbin,/lib},lib,bin}}
|
||||
ln -s "usr/lib" "$buildroot/lib"
|
||||
|
@ -520,25 +375,17 @@ initialize_buildroot() {
|
|||
ln -s "usr/bin" "$buildroot/bin"
|
||||
ln -s "usr/bin" "$buildroot/sbin"
|
||||
ln -s "/run" "$buildroot/var/run"
|
||||
|
||||
case "$arch" in
|
||||
x86_64)
|
||||
ln -s "lib" "$buildroot/usr/lib64"
|
||||
ln -s "usr/lib" "$buildroot/lib64"
|
||||
;;
|
||||
esac
|
||||
|
||||
# mkinitcpio version stamp
|
||||
# shellcheck disable=SC2154
|
||||
printf '%s' "$version" >"$buildroot/VERSION"
|
||||
|
||||
# kernel module dir
|
||||
[[ "$kernver" != 'none' ]] && install -dm755 "$buildroot/usr/lib/modules/$kernver/kernel"
|
||||
|
||||
# mount tables
|
||||
ln -s ../proc/self/mounts "$buildroot/etc/mtab"
|
||||
: >"$buildroot/etc/fstab"
|
||||
|
||||
# add os-release and initrd-release for systemd
|
||||
if [[ -e /etc/os-release ]]; then
|
||||
if [[ -L /etc/os-release ]]; then
|
||||
|
@ -553,10 +400,8 @@ initialize_buildroot() {
|
|||
else
|
||||
: >"$buildroot/etc/initrd-release"
|
||||
fi
|
||||
|
||||
# add a blank ld.so.conf to keep ldconfig happy
|
||||
: >"$buildroot/etc/ld.so.conf"
|
||||
|
||||
printf '%s' "$workdir"
|
||||
}
|
||||
|
||||
|
@ -564,7 +409,6 @@ run_build_hook() {
|
|||
local hook="$1" script='' resolved=''
|
||||
# shellcheck disable=SC2034
|
||||
local MODULES=() BINARIES=() FILES=() SCRIPT=''
|
||||
|
||||
# find script in install dirs
|
||||
# _d_install is assigned in mkinitcpio
|
||||
# shellcheck disable=SC2154
|
||||
|
@ -572,14 +416,6 @@ run_build_hook() {
|
|||
error "Hook '$hook' cannot be found"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# check for deprecation
|
||||
if resolved="$(readlink -e "$script")" && [[ "${script##*/}" != "${resolved##*/}" ]]; then
|
||||
warning "Hook '%s' is deprecated. Replace it with '%s' in your config" \
|
||||
"${script##*/}" "${resolved##*/}"
|
||||
script="$resolved"
|
||||
fi
|
||||
|
||||
# source
|
||||
unset -f build
|
||||
# shellcheck disable=SC1090
|
||||
|
@ -587,20 +423,13 @@ run_build_hook() {
|
|||
error 'Failed to read %s' "$script"
|
||||
return 1
|
||||
fi
|
||||
|
||||
if ! declare -f build >/dev/null; then
|
||||
error "Hook '%s' has no build function" "${script}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# run
|
||||
if (( _optquiet )); then
|
||||
msg2 "Running build hook: [%s]" "${script##*/}"
|
||||
else
|
||||
msg2 "Running build hook: [%s]" "$script"
|
||||
fi
|
||||
msg2 "Running build hook: [%s]" "${script##*/}"
|
||||
build
|
||||
|
||||
# if we made it this far, return successfully. Hooks can
|
||||
# do their own error catching if it's severe enough, and
|
||||
# we already capture errors from the add_* functions.
|
||||
|
@ -609,12 +438,10 @@ run_build_hook() {
|
|||
|
||||
try_enable_color() {
|
||||
local colors
|
||||
|
||||
if ! colors="$(tput colors 2>/dev/null)"; then
|
||||
warning "Failed to enable color. Check your TERM environment variable"
|
||||
return
|
||||
fi
|
||||
|
||||
if (( colors > 0 )) && tput setaf 0 &>/dev/null; then
|
||||
_color_none="$(tput sgr0)"
|
||||
_color_bold="$(tput bold)"
|
||||
|
@ -626,13 +453,10 @@ try_enable_color() {
|
|||
}
|
||||
|
||||
install_modules() {
|
||||
|
||||
command tar --hard-dereference -C / -cpf - "$@" | tar -C "${BUILDROOT}" -xpf -
|
||||
|
||||
msg "Generating module dependencies"
|
||||
map add_file "$_d_kmoduledir"/modules.{builtin,builtin.modinfo,order}
|
||||
depmod -b "$BUILDROOT" "$KERNELVERSION"
|
||||
|
||||
# remove all non-binary module.* files (except devname for on-demand module loading)
|
||||
rm "${BUILDROOT}${_d_kmoduledir}"/modules.!(*.bin|devname|softdep)
|
||||
}
|
||||
|
|
|
@ -23,8 +23,8 @@ build ()
|
|||
nilfs-resize nilfs-tune xfs_growfs xfs_quota \
|
||||
xfs_spaceman compile_et mk_cmds fsck.xfs xfs_admin xfs_bmap xfs_freeze \
|
||||
xfs_info xfs_metadump xfs_mkfile xfs_ncheck gpart
|
||||
map add_file "/etc/nilfs_cleanerd.conf" "/etc/nvme/discovery.conf"
|
||||
map add_full_dir /etc/cifs-utils /etc/request-key.d
|
||||
add_file "/etc/nilfs_cleanerd.conf"
|
||||
map add_full_dir /etc/cifs-utils /etc/request-key.d /etc/nvme
|
||||
# fix licenses
|
||||
add_file "/usr/share/licenses/e2fsprogs/MIT-LICENSE"
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue