test-functions revision 0d6e798a784ef0ba6b95512e4453067b2f84a91a
1653N/A#!/bin/bash
1653N/A# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
1653N/A# ex: ts=8 sw=4 sts=4 et filetype=sh
1653N/APATH=/sbin:/bin:/usr/sbin:/usr/bin
1653N/Aexport PATH
1653N/A
1653N/AKERNEL_VER=${KERNEL_VER-$(uname -r)}
1653N/AKERNEL_MODS="/lib/modules/$KERNEL_VER/"
1653N/A
1653N/Asetup_basic_dirs() {
1653N/A for d in usr/bin usr/sbin bin etc lib "$libdir" sbin tmp usr var var/log dev proc sys sysroot root run run/lock run/initramfs; do
1653N/A if [ -L "/$d" ]; then
1653N/A inst_symlink "/$d"
1653N/A else
1653N/A inst_dir "/$d"
1653N/A fi
1653N/A done
1653N/A
1653N/A ln -sfn /run "$initdir/var/run"
1653N/A ln -sfn /run/lock "$initdir/var/lock"
1653N/A}
1653N/A
1653N/Ainst_libs() {
1653N/A local _bin=$1
3215N/A local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
5858N/A local _file _line
1653N/A
1653N/A LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
1653N/A [[ $_line = 'not a dynamic executable' ]] && break
1653N/A
1653N/A if [[ $_line =~ $_so_regex ]]; then
1653N/A _file=${BASH_REMATCH[1]}
1653N/A [[ -e ${initdir}/$_file ]] && continue
1653N/A inst_library "$_file"
1653N/A continue
1653N/A fi
1653N/A
1653N/A if [[ $_line =~ not\ found ]]; then
1653N/A dfatal "Missing a shared library required by $_bin."
1653N/A dfatal "Run \"ldd $_bin\" to find out what it is."
1653N/A dfatal "$_line"
1653N/A dfatal "dracut cannot create an initrd."
1653N/A exit 1
1653N/A fi
1653N/A done
1653N/A}
1653N/A
1653N/Aimport_testdir() {
1653N/A STATEFILE=".testdir"
1653N/A [[ -e $STATEFILE ]] && . $STATEFILE
1653N/A if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
1653N/A TESTDIR=$(mktemp --tmpdir=/var/tmp -d -t systemd-test.XXXXXX)
1653N/A echo "TESTDIR=\"$TESTDIR\"" > $STATEFILE
1653N/A export TESTDIR
2342N/A fi
1653N/A}
1653N/A
1653N/A## @brief Converts numeric logging level to the first letter of level name.
1653N/A#
1653N/A# @param lvl Numeric logging level in range from 1 to 6.
1653N/A# @retval 1 if @a lvl is out of range.
1653N/A# @retval 0 if @a lvl is correct.
1653N/A# @result Echoes first letter of level name.
1653N/A_lvl2char() {
1653N/A case "$1" in
1653N/A 1) echo F;;
1653N/A 2) echo E;;
1653N/A 3) echo W;;
1653N/A 4) echo I;;
1653N/A 5) echo D;;
1653N/A 6) echo T;;
1653N/A *) return 1;;
1653N/A esac
1653N/A}
1653N/A
1653N/A## @brief Internal helper function for _do_dlog()
1653N/A#
1653N/A# @param lvl Numeric logging level.
1653N/A# @param msg Message.
1653N/A# @retval 0 It's always returned, even if logging failed.
1653N/A#
1653N/A# @note This function is not supposed to be called manually. Please use
1653N/A# dtrace(), ddebug(), or others instead which wrap this one.
1653N/A#
1653N/A# This function calls _do_dlog() either with parameter msg, or if
1653N/A# none is given, it will read standard input and will use every line as
1653N/A# a message.
1653N/A#
1653N/A# This enables:
1653N/A# dwarn "This is a warning"
1653N/A# echo "This is a warning" | dwarn
1653N/ALOG_LEVEL=4
1653N/A
1653N/Adlog() {
1653N/A [ -z "$LOG_LEVEL" ] && return 0
1653N/A [ $1 -le $LOG_LEVEL ] || return 0
1653N/A local lvl="$1"; shift
1653N/A local lvlc=$(_lvl2char "$lvl") || return 0
1653N/A
1653N/A if [ $# -ge 1 ]; then
1653N/A echo "$lvlc: $*"
1653N/A else
1653N/A while read line; do
1653N/A echo "$lvlc: " "$line"
1653N/A done
1653N/A fi
1653N/A}
1653N/A
1653N/A## @brief Logs message at TRACE level (6)
1653N/A#
1653N/A# @param msg Message.
1653N/A# @retval 0 It's always returned, even if logging failed.
1653N/Adtrace() {
1653N/A set +x
1874N/A dlog 6 "$@"
1874N/A [ -n "$debug" ] && set -x || :
1874N/A}
1653N/A
1653N/A## @brief Logs message at DEBUG level (5)
1653N/A#
1653N/A# @param msg Message.
1653N/A# @retval 0 It's always returned, even if logging failed.
1653N/Addebug() {
1653N/A# set +x
1653N/A dlog 5 "$@"
1653N/A# [ -n "$debug" ] && set -x || :
1653N/A}
1653N/A
1653N/A## @brief Logs message at INFO level (4)
1653N/A#
1653N/A# @param msg Message.
1653N/A# @retval 0 It's always returned, even if logging failed.
1653N/Adinfo() {
1653N/A set +x
1653N/A dlog 4 "$@"
1653N/A [ -n "$debug" ] && set -x || :
1653N/A}
1653N/A
1653N/A## @brief Logs message at WARN level (3)
1653N/A#
1653N/A# @param msg Message.
1653N/A# @retval 0 It's always returned, even if logging failed.
1653N/Adwarn() {
1653N/A set +x
1653N/A dlog 3 "$@"
1653N/A [ -n "$debug" ] && set -x || :
1653N/A}
1653N/A
1653N/A## @brief Logs message at ERROR level (2)
1653N/A#
1653N/A# @param msg Message.
1653N/A# @retval 0 It's always returned, even if logging failed.
1653N/Aderror() {
1874N/A# set +x
1874N/A dlog 2 "$@"
1653N/A# [ -n "$debug" ] && set -x || :
1653N/A}
1653N/A
1653N/A## @brief Logs message at FATAL level (1)
1653N/A#
1653N/A# @param msg Message.
1653N/A# @retval 0 It's always returned, even if logging failed.
1653N/Adfatal() {
1653N/A set +x
1653N/A dlog 1 "$@"
1653N/A [ -n "$debug" ] && set -x || :
1653N/A}
1653N/A
1653N/A
1653N/A# Generic substring function. If $2 is in $1, return 0.
1653N/Astrstr() { [ "${1#*$2*}" != "$1" ]; }
1653N/A
1653N/A# normalize_path <path>
1653N/A# Prints the normalized path, where it removes any duplicated
1653N/A# and trailing slashes.
1653N/A# Example:
1653N/A# $ normalize_path ///test/test//
1653N/A# /test/test
1653N/Anormalize_path() {
1653N/A shopt -q -s extglob
1653N/A set -- "${1//+(\/)//}"
1653N/A shopt -q -u extglob
1653N/A echo "${1%/}"
1653N/A}
1653N/A
1653N/A# convert_abs_rel <from> <to>
1653N/A# Prints the relative path, when creating a symlink to <to> from <from>.
1653N/A# Example:
1653N/A# $ convert_abs_rel /usr/bin/test /bin/test-2
1653N/A# ../../bin/test-2
1653N/A# $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
1653N/Aconvert_abs_rel() {
1653N/A local __current __absolute __abssize __cursize __newpath
1653N/A local -i __i __level
1653N/A
1653N/A set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
1653N/A
1653N/A # corner case #1 - self looping link
1653N/A [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
1653N/A
1653N/A # corner case #2 - own dir link
1653N/A [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
1653N/A
1653N/A IFS="/" __current=($1)
1653N/A IFS="/" __absolute=($2)
1653N/A
1653N/A __abssize=${#__absolute[@]}
1653N/A __cursize=${#__current[@]}
1653N/A
1653N/A while [[ ${__absolute[__level]} == ${__current[__level]} ]]
1653N/A do
1653N/A (( __level++ ))
1653N/A if (( __level > __abssize || __level > __cursize ))
1653N/A then
1653N/A break
1653N/A fi
1653N/A done
1653N/A
1653N/A for ((__i = __level; __i < __cursize-1; __i++))
1653N/A do
1653N/A if ((__i > __level))
1653N/A then
1653N/A __newpath=$__newpath"/"
1653N/A fi
1653N/A __newpath=$__newpath".."
1653N/A done
1874N/A
1874N/A for ((__i = __level; __i < __abssize; __i++))
1653N/A do
1653N/A if [[ -n $__newpath ]]
1653N/A then
1653N/A __newpath=$__newpath"/"
1653N/A fi
1653N/A __newpath=$__newpath${__absolute[__i]}
1653N/A done
1653N/A
1653N/A echo "$__newpath"
1653N/A}
1653N/A
1653N/A
5858N/A# Install a directory, keeping symlinks as on the original system.
5858N/A# Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
1653N/A# will create ${initdir}/lib64, ${initdir}/lib64/file,
5858N/A# and a symlink ${initdir}/lib -> lib64.
1653N/Ainst_dir() {
1653N/A [[ -e ${initdir}/"$1" ]] && return 0 # already there
5858N/A
5858N/A local _dir="$1" _part="${1%/*}" _file
1653N/A while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
1653N/A _dir="$_part $_dir"
1653N/A _part=${_part%/*}
1653N/A done
1653N/A
1653N/A # iterate over parent directories
1653N/A for _file in $_dir; do
1653N/A [[ -e "${initdir}/$_file" ]] && continue
1653N/A if [[ -L $_file ]]; then
1653N/A inst_symlink "$_file"
1653N/A else
1653N/A # create directory
1653N/A mkdir -m 0755 -p "${initdir}/$_file" || return 1
1653N/A [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
1653N/A chmod u+w "${initdir}/$_file"
1653N/A fi
1653N/A done
1653N/A}
1653N/A
1653N/A# $1 = file to copy to ramdisk
5858N/A# $2 (optional) Name for the file on the ramdisk
1653N/A# Location of the image dir is assumed to be $initdir
1653N/A# We never overwrite the target if it exists.
1653N/Ainst_simple() {
1653N/A [[ -f "$1" ]] || return 1
1653N/A strstr "$1" "/" || return 1
1653N/A
1653N/A local _src=$1 target="${2:-$1}"
1653N/A if ! [[ -d ${initdir}/$target ]]; then
1653N/A [[ -e ${initdir}/$target ]] && return 0
1653N/A [[ -L ${initdir}/$target ]] && return 0
1653N/A [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
1653N/A fi
1653N/A # install checksum files also
3853N/A if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
3853N/A inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
1653N/A fi
1653N/A ddebug "Installing $_src"
1653N/A cp --sparse=always -pfL "$_src" "${initdir}/$target"
1653N/A}
1653N/A
5858N/A# find symlinks linked to given library file
1653N/A# $1 = library file
1653N/A# Function searches for symlinks by stripping version numbers appended to
1653N/A# library filename, checks if it points to the same target and finally
1653N/A# prints the list of symlinks to stdout.
1653N/A#
1653N/A# Example:
1653N/A# rev_lib_symlinks libfoo.so.8.1
1653N/A# output: libfoo.so.8 libfoo.so
1653N/A# (Only if libfoo.so.8 and libfoo.so exists on host system.)
1653N/Arev_lib_symlinks() {
1653N/A [[ ! $1 ]] && return 0
1653N/A
1653N/A local fn="$1" orig="$(readlink -f "$1")" links=''
1653N/A
3853N/A [[ ${fn} =~ .*\.so\..* ]] || return 1
3853N/A
1653N/A until [[ ${fn##*.} == so ]]; do
1653N/A fn="${fn%.*}"
1653N/A [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
1653N/A done
1653N/A
5858N/A echo "${links}"
1653N/A}
1653N/A
1653N/A# Same as above, but specialized to handle dynamic libraries.
1653N/A# It handles making symlinks according to how the original library
1653N/A# is referenced.
1653N/Ainst_library() {
1653N/A local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
1653N/A strstr "$1" "/" || return 1
1653N/A [[ -e $initdir/$_dest ]] && return 0
2342N/A if [[ -L $_src ]]; then
1653N/A # install checksum files also
1653N/A if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
1653N/A inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
1653N/A fi
1653N/A _reallib=$(readlink -f "$_src")
1653N/A inst_simple "$_reallib" "$_reallib"
1653N/A inst_dir "${_dest%/*}"
1653N/A [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
1653N/A ln -sfn $(convert_abs_rel "${_dest}" "${_reallib}") "${initdir}/${_dest}"
1653N/A else
1653N/A inst_simple "$_src" "$_dest"
1653N/A fi
1653N/A
1653N/A # Create additional symlinks. See rev_symlinks description.
1653N/A for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
1653N/A [[ ! -e $initdir/$_symlink ]] && {
1653N/A ddebug "Creating extra symlink: $_symlink"
1653N/A inst_symlink $_symlink
1653N/A }
1653N/A done
1653N/A}
3853N/A
3853N/A# find a binary. If we were not passed the full path directly,
1653N/A# search in the usual places to find the binary.
1653N/Afind_binary() {
1653N/A if [[ -z ${1##/*} ]]; then
1653N/A if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then
1653N/A echo $1
5858N/A return 0
1653N/A fi
1653N/A fi
1653N/A
1653N/A type -P $1
1653N/A}
1653N/A
1653N/A# Same as above, but specialized to install binary executables.
1653N/A# Install binary executable, and all shared library dependencies, if any.
1653N/Ainst_binary() {
2342N/A local _bin _target
1653N/A _bin=$(find_binary "$1") || return 1
1653N/A _target=${2:-$_bin}
1653N/A [[ -e $initdir/$_target ]] && return 0
1653N/A [[ -L $_bin ]] && inst_symlink $_bin $_target && return 0
1653N/A local _file _line
1653N/A local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
1653N/A # I love bash!
1653N/A LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
1653N/A [[ $_line = 'not a dynamic executable' ]] && break
1653N/A
1653N/A if [[ $_line =~ $_so_regex ]]; then
1653N/A _file=${BASH_REMATCH[1]}
1653N/A [[ -e ${initdir}/$_file ]] && continue
1653N/A inst_library "$_file"
1653N/A continue
1653N/A fi
1653N/A
1653N/A if [[ $_line =~ not\ found ]]; then
1653N/A dfatal "Missing a shared library required by $_bin."
1653N/A dfatal "Run \"ldd $_bin\" to find out what it is."
1653N/A dfatal "$_line"
3853N/A dfatal "dracut cannot create an initrd."
3853N/A exit 1
1653N/A fi
1653N/A done
1653N/A inst_simple "$_bin" "$_target"
1653N/A}
1653N/A
5858N/A# same as above, except for shell scripts.
1653N/A# If your shell script does not start with shebang, it is not a shell script.
1653N/Ainst_script() {
1653N/A local _bin
1653N/A _bin=$(find_binary "$1") || return 1
1653N/A shift
1653N/A local _line _shebang_regex
1653N/A read -r -n 80 _line <"$_bin"
1653N/A # If debug is set, clean unprintable chars to prevent messing up the term
1653N/A [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]')
1653N/A _shebang_regex='(#! *)(/[^ ]+).*'
2342N/A [[ $_line =~ $_shebang_regex ]] || return 1
1653N/A inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@"
1653N/A}
1653N/A
1653N/A# same as above, but specialized for symlinks
1653N/Ainst_symlink() {
1653N/A local _src=$1 _target=${2:-$1} _realsrc
1653N/A strstr "$1" "/" || return 1
1653N/A [[ -L $1 ]] || return 1
1653N/A [[ -L $initdir/$_target ]] && return 0
1653N/A _realsrc=$(readlink -f "$_src")
1653N/A if ! [[ -e $initdir/$_realsrc ]]; then
1653N/A if [[ -d $_realsrc ]]; then
1653N/A inst_dir "$_realsrc"
1653N/A else
1653N/A inst "$_realsrc"
1653N/A fi
1653N/A fi
1653N/A [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}"
1653N/A [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
1653N/A ln -sfn $(convert_abs_rel "${_target}" "${_realsrc}") "$initdir/$_target"
1653N/A}
3853N/A
3853N/A# attempt to install any programs specified in a udev rule
1653N/Ainst_rule_programs() {
1653N/A local _prog _bin
1653N/A
1653N/A if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
1653N/A for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
1653N/A if [ -x /lib/udev/$_prog ]; then
5858N/A _bin=/lib/udev/$_prog
1653N/A else
1653N/A _bin=$(find_binary "$_prog") || {
1653N/A dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
1653N/A continue;
1653N/A }
1653N/A fi
1653N/A
1653N/A #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
1653N/A dracut_install "$_bin"
1653N/A done
1653N/A fi
1653N/A}
1653N/A
1653N/A# udev rules always get installed in the same place, so
1653N/A# create a function to install them to make life simpler.
1653N/Ainst_rules() {
1653N/A local _target=/etc/udev/rules.d _rule _found
1653N/A
1653N/A inst_dir "/lib/udev/rules.d"
1653N/A inst_dir "$_target"
1653N/A for _rule in "$@"; do
1653N/A if [ "${rule#/}" = "$rule" ]; then
1653N/A for r in /lib/udev/rules.d /etc/udev/rules.d; do
1653N/A if [[ -f $r/$_rule ]]; then
1653N/A _found="$r/$_rule"
1653N/A inst_simple "$_found"
1653N/A inst_rule_programs "$_found"
1653N/A fi
1653N/A done
1653N/A fi
1653N/A for r in '' ./ $dracutbasedir/rules.d/; do
1653N/A if [[ -f ${r}$_rule ]]; then
1653N/A _found="${r}$_rule"
1653N/A inst_simple "$_found" "$_target/${_found##*/}"
3853N/A inst_rule_programs "$_found"
3853N/A fi
1653N/A done
1653N/A [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
1653N/A done
1653N/A}
1653N/A
5858N/A# general purpose installation function
1653N/A# Same args as above.
1653N/Ainst() {
1653N/A local _x
1653N/A
1653N/A case $# in
1653N/A 1) ;;
1653N/A 2) [[ ! $initdir && -d $2 ]] && export initdir=$2
1653N/A [[ $initdir = $2 ]] && set $1;;
1653N/A 3) [[ -z $initdir ]] && export initdir=$2
1653N/A set $1 $3;;
1653N/A *) dfatal "inst only takes 1 or 2 or 3 arguments"
1653N/A exit 1;;
1653N/A esac
1653N/A for _x in inst_symlink inst_script inst_binary inst_simple; do
1653N/A $_x "$@" && return 0
1653N/A done
1653N/A return 1
1653N/A}
1653N/A
1653N/A# install any of listed files
1653N/A#
1653N/A# If first argument is '-d' and second some destination path, first accessible
1653N/A# source is installed into this path, otherwise it will installed in the same
1653N/A# path as source. If none of listed files was installed, function return 1.
1653N/A# On first successful installation it returns with 0 status.
1653N/A#
1653N/A# Example:
1653N/A#
1653N/A# inst_any -d /bin/foo /bin/bar /bin/baz
1653N/A#
1653N/A# Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
1653N/A# initramfs.
1653N/Ainst_any() {
1653N/A local to f
3853N/A
3853N/A [[ $1 = '-d' ]] && to="$2" && shift 2
1653N/A
1653N/A for f in "$@"; do
1653N/A if [[ -e $f ]]; then
1653N/A [[ $to ]] && inst "$f" "$to" && return 0
1653N/A inst "$f" && return 0
1653N/A fi
5858N/A done
1653N/A
1653N/A return 1
1653N/A}
1653N/A
1653N/A# dracut_install [-o ] <file> [<file> ... ]
1653N/A# Install <file> to the initramfs image
1653N/A# -o optionally install the <file> and don't fail, if it is not there
1653N/Adracut_install() {
1653N/A local _optional=no
1653N/A if [[ $1 = '-o' ]]; then
1653N/A _optional=yes
1653N/A shift
1653N/A fi
1653N/A while (($# > 0)); do
1653N/A if ! inst "$1" ; then
1653N/A if [[ $_optional = yes ]]; then
1653N/A dinfo "Skipping program $1 as it cannot be found and is" \
1653N/A "flagged to be optional"
1653N/A else
1653N/A dfatal "Failed to install $1"
1653N/A exit 1
1653N/A fi
1653N/A fi
1653N/A shift
1653N/A done
1653N/A}
1653N/A
1653N/A# Install a single kernel module along with any firmware it may require.
1653N/A# $1 = full path to kernel module to install
1653N/Ainstall_kmod_with_fw() {
1653N/A # no need to go further if the module is already installed
1653N/A
1653N/A [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
1653N/A && return 0
3853N/A
3853N/A [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
1653N/A
1653N/A if [[ $omit_drivers ]]; then
1653N/A local _kmod=${1##*/}
1653N/A _kmod=${_kmod%.ko}
1653N/A _kmod=${_kmod/-/_}
1653N/A if [[ "$_kmod" =~ $omit_drivers ]]; then
5858N/A dinfo "Omitting driver $_kmod"
1653N/A return 1
1653N/A fi
1653N/A if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~ $omit_drivers ]]; then
1653N/A dinfo "Omitting driver $_kmod"
1653N/A return 1
1653N/A fi
1653N/A fi
1653N/A
1653N/A [ -d "$initdir/.kernelmodseen" ] && \
1653N/A > "$initdir/.kernelmodseen/${1##*/}"
1653N/A
1653N/A inst_simple "$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
1653N/A || return $?
1653N/A
1653N/A local _modname=${1##*/} _fwdir _found _fw
1653N/A _modname=${_modname%.ko*}
1653N/A for _fw in $(modinfo -k $KERNEL_VER -F firmware $1 2>/dev/null); do
1653N/A _found=''
1653N/A for _fwdir in $fw_dir; do
1653N/A if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1653N/A inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1653N/A _found=yes
1653N/A fi
1653N/A done
1653N/A if [[ $_found != yes ]]; then
1653N/A if ! grep -qe "\<${_modname//-/_}\>" /proc/modules; then
1653N/A dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
1653N/A "\"${_modname}.ko\""
1653N/A else
1653N/A dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
1653N/A "\"${_modname}.ko\""
1653N/A fi
1653N/A fi
1653N/A done
3853N/A return 0
3853N/A}
1653N/A
1653N/A# Do something with all the dependencies of a kernel module.
1653N/A# Note that kernel modules depend on themselves using the technique we use
1653N/A# $1 = function to call for each dependency we find
1653N/A# It will be passed the full path to the found kernel module
1653N/A# $2 = module to get dependencies for
5858N/A# rest of args = arguments to modprobe
1653N/A# _fderr specifies FD passed from surrounding scope
1653N/Afor_each_kmod_dep() {
1653N/A local _func=$1 _kmod=$2 _cmd _modpath _options _found=0
1653N/A shift 2
1653N/A modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
1653N/A while read _cmd _modpath _options; do
1653N/A [[ $_cmd = insmod ]] || continue
1653N/A $_func ${_modpath} || exit $?
1653N/A _found=1
1653N/A done
1653N/A [[ $_found -eq 0 ]] && exit 1
1653N/A exit 0
1653N/A )
1653N/A}
1653N/A
1653N/A# filter kernel modules to install certain modules that meet specific
1653N/A# requirements.
1653N/A# $1 = search only in subdirectory of /kernel/$1
1653N/A# $2 = function to call with module name to filter.
1653N/A# This function will be passed the full path to the module to test.
1653N/A# The behaviour of this function can vary depending on whether $hostonly is set.
1653N/A# If it is, we will only look at modules that are already in memory.
1653N/A# If it is not, we will look at all kernel modules
1653N/A# This function returns the full filenames of modules that match $1
1653N/Afilter_kernel_modules_by_path () (
1653N/A local _modname _filtercmd
1653N/A if ! [[ $hostonly ]]; then
1653N/A _filtercmd='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
1653N/A _filtercmd+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
1653N/A _filtercmd+=' -o -name "*.ko.xz"'
1653N/A _filtercmd+=' 2>/dev/null'
1653N/A else
1653N/A _filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
1653N/A _filtercmd+='-k $KERNEL_VER 2>/dev/null'
3853N/A fi
3853N/A for _modname in $(eval $_filtercmd); do
1653N/A case $_modname in
1653N/A *.ko) "$2" "$_modname" && echo "$_modname";;
1653N/A *.ko.gz) gzip -dc "$_modname" > $initdir/$$.ko
1653N/A $2 $initdir/$$.ko && echo "$_modname"
1653N/A rm -f $initdir/$$.ko
1653N/A ;;
5858N/A *.ko.xz) xz -dc "$_modname" > $initdir/$$.ko
1653N/A $2 $initdir/$$.ko && echo "$_modname"
1653N/A rm -f $initdir/$$.ko
1653N/A ;;
1653N/A esac
1653N/A done
1653N/A)
1653N/Afind_kernel_modules_by_path () (
1653N/A if ! [[ $hostonly ]]; then
1653N/A find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
1653N/A -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev/null
1653N/A else
1653N/A cut -d " " -f 1 </proc/modules \
1653N/A | xargs modinfo -F filename -k $KERNEL_VER 2>/dev/null
1653N/A fi
1653N/A)
1653N/A
1653N/Afilter_kernel_modules () {
1653N/A filter_kernel_modules_by_path drivers "$1"
1653N/A}
1653N/A
1653N/Afind_kernel_modules () {
1653N/A find_kernel_modules_by_path drivers
1653N/A}
1653N/A
1653N/A# instmods [-c] <kernel module> [<kernel module> ... ]
1653N/A# instmods [-c] <kernel subsystem>
1653N/A# install kernel modules along with all their dependencies.
1653N/A# <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1653N/Ainstmods() {
1653N/A [[ $no_kernel = yes ]] && return
1653N/A # called [sub]functions inherit _fderr
1653N/A local _fderr=9
1653N/A local _check=no
1653N/A if [[ $1 = '-c' ]]; then
3853N/A _check=yes
3853N/A shift
1653N/A fi
1653N/A
1653N/A function inst1mod() {
1653N/A local _ret=0 _mod="$1"
1653N/A case $_mod in
5858N/A =*)
1653N/A if [ -f $KERNEL_MODS/modules.${_mod#=} ]; then
1653N/A ( [[ "$_mpargs" ]] && echo $_mpargs
1653N/A cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
1653N/A | instmods
1653N/A else
1653N/A ( [[ "$_mpargs" ]] && echo $_mpargs
1653N/A find "$KERNEL_MODS" -path "*/${_mod#=}/*" -printf '%f\n' ) \
1653N/A | instmods
1653N/A fi
1653N/A ;;
1653N/A --*) _mpargs+=" $_mod" ;;
1653N/A i2o_scsi) return ;; # Do not load this diagnostic-only module
1653N/A *)
1653N/A _mod=${_mod##*/}
1653N/A # if we are already installed, skip this module and go on
1653N/A # to the next one.
1653N/A [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
1653N/A
1653N/A if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then
1653N/A dinfo "Omitting driver ${_mod##$KERNEL_MODS}"
1653N/A return
1653N/A fi
1653N/A # If we are building a host-specific initramfs and this
1653N/A # module is not already loaded, move on to the next one.
1653N/A [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc/modules \
1653N/A && ! echo $add_drivers | grep -qe "\<${_mod}\>" \
1653N/A && return
1653N/A
1653N/A # We use '-d' option in modprobe only if modules prefix path
1653N/A # differs from default '/'. This allows us to use Dracut with
1653N/A # old version of modprobe which doesn't have '-d' option.
1653N/A local _moddirname=${KERNEL_MODS%%/lib/modules/*}
1653N/A [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1653N/A
3853N/A # ok, load the module, all its dependencies, and any firmware
3853N/A # it may require
1653N/A for_each_kmod_dep install_kmod_with_fw $_mod \
1653N/A --set-version $KERNEL_VER ${_moddirname} $_mpargs
1653N/A ((_ret+=$?))
1653N/A ;;
1653N/A esac
1653N/A return $_ret
5858N/A }
1653N/A
1653N/A function instmods_1() {
1653N/A local _mod _mpargs
1653N/A if (($# == 0)); then # filenames from stdin
1653N/A while read _mod; do
1653N/A inst1mod "${_mod%.ko*}" || {
1653N/A if [ "$_check" = "yes" ]; then
1653N/A dfatal "Failed to install $_mod"
1653N/A return 1
1653N/A fi
1653N/A }
1653N/A done
1653N/A fi
1653N/A while (($# > 0)); do # filenames as arguments
1653N/A inst1mod ${1%.ko*} || {
1653N/A if [ "$_check" = "yes" ]; then
1653N/A dfatal "Failed to install $1"
1653N/A return 1
1653N/A fi
1653N/A }
1653N/A shift
1653N/A done
1653N/A return 0
1653N/A }
1653N/A
1653N/A local _ret _filter_not_found='FATAL: Module .* not found.'
1653N/A set -o pipefail
1653N/A # Capture all stderr from modprobe to _fderr. We could use {var}>...
1653N/A # redirections, but that would make dracut require bash4 at least.
1653N/A eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1653N/A | while read line; do [[ "$line" =~ $_filter_not_found ]] && echo $line || echo $line >&2 ;done | derror
1653N/A _ret=$?
1653N/A set +o pipefail
1653N/A return $_ret
3853N/A}
3853N/A
1653N/A# inst_libdir_file [-n <pattern>] <file> [<file>...]
1653N/A# Install a <file> located on a lib directory to the initramfs image
1653N/A# -n <pattern> install non-matching files
1653N/Ainst_libdir_file() {
1653N/A if [[ "$1" == "-n" ]]; then
1653N/A local _pattern=$1
5858N/A shift 2
1653N/A for _dir in $libdirs; do
1653N/A for _i in "$@"; do
1653N/A for _f in "$_dir"/$_i; do
1653N/A [[ "$_i" =~ $_pattern ]] || continue
1653N/A [[ -e "$_i" ]] && dracut_install "$_i"
1653N/A done
1653N/A done
1653N/A done
1653N/A else
1653N/A for _dir in $libdirs; do
1653N/A for _i in "$@"; do
1653N/A for _f in "$_dir"/$_i; do
1653N/A [[ -e "$_f" ]] && dracut_install "$_f"
1653N/A done
1653N/A done
1653N/A done
1653N/A fi
1653N/A}
1653N/A
1653N/Acheck_qemu() {
1653N/A command -v qemu-kvm &>/dev/null && [[ -c /dev/kvm ]]
1653N/A}
1653N/A
1653N/Acheck_nspawn() {
1653N/A [[ -d /sys/fs/cgroup/systemd ]]
1653N/A}
1653N/A
1653N/A
1653N/Ado_test() {
1653N/A if [[ $UID != "0" ]]; then
1653N/A echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
1653N/A exit 0
1653N/A fi
1653N/A
3853N/A# Detect lib paths
3853N/A [[ $libdir ]] || for libdir in /lib64 /lib; do
1653N/A [[ -d $libdir ]] && libdirs+=" $libdir" && break
1653N/A done
1653N/A
1653N/A [[ $usrlibdir ]] || for usrlibdir in /usr/lib64 /usr/lib; do
1653N/A [[ -d $usrlibdir ]] && libdirs+=" $usrlibdir" && break
5858N/A done
1653N/A
1653N/A import_testdir
1653N/A
while (($# > 0)); do
case $1 in
--run)
echo "TEST RUN: $TEST_DESCRIPTION"
test_run
ret=$?
if [ $ret -eq 0 ]; then
echo "TEST RUN: $TEST_DESCRIPTION [OK]"
else
echo "TEST RUN: $TEST_DESCRIPTION [FAILED]"
fi
exit $ret;;
--setup)
echo "TEST SETUP: $TEST_DESCRIPTION"
test_setup
exit $?;;
--clean)
echo "TEST CLEANUP: $TEST_DESCRIPTION"
test_cleanup
rm -fr "$TESTDIR"
rm -f .testdir
exit $?;;
--all)
echo -n "TEST: $TEST_DESCRIPTION ";
(
test_setup && test_run
ret=$?
test_cleanup
rm -fr "$TESTDIR"
rm -f .testdir
exit $ret
) </dev/null >test.log 2>&1
ret=$?
if [ $ret -eq 0 ]; then
rm test.log
echo "[OK]"
else
echo "[FAILED]"
echo "see $(pwd)/test.log"
fi
exit $ret;;
*) break ;;
esac
shift
done
}