test-functions revision 0d6e798a784ef0ba6b95512e4453067b2f84a91a
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/A## @brief Converts numeric logging level to the first letter of level name. 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## @brief Internal helper function for _do_dlog() 1653N/A# @param lvl Numeric logging level. 1653N/A# @retval 0 It's always returned, even if logging failed. 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# 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# echo "This is a warning" | dwarn 1653N/A## @brief Logs message at TRACE level (6) 1653N/A# @retval 0 It's always returned, even if logging failed. 1653N/A## @brief Logs message at DEBUG level (5) 1653N/A# @retval 0 It's always returned, even if logging failed. 1653N/A# [ -n "$debug" ] && set -x || : 1653N/A## @brief Logs message at INFO level (4) 1653N/A# @retval 0 It's always returned, even if logging failed. 1653N/A## @brief Logs message at WARN level (3) 1653N/A# @retval 0 It's always returned, even if logging failed. 1653N/A## @brief Logs message at ERROR level (2) 1653N/A# @retval 0 It's always returned, even if logging failed. 1653N/A# [ -n "$debug" ] && set -x || : 1653N/A## @brief Logs message at FATAL level (1) 1653N/A# @retval 0 It's always returned, even if logging failed. 1653N/A# Generic substring function. If $2 is in $1, return 0. 1653N/A# Prints the normalized path, where it removes any duplicated 1653N/A# convert_abs_rel <from> <to> 1653N/A# Prints the relative path, when creating a symlink to <to> from <from>. 1653N/A # corner case #1 - self looping link 1653N/A [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; } 1653N/A # corner case #2 - own dir link 1653N/A [[ "${1%/*}" == "$2" ]] && { echo "."; return; } 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" 5858N/A# and a symlink ${initdir}/lib -> lib64. 1653N/A while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do 1653N/A # iterate over parent directories 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/A # install checksum files also 3853N/A if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then 3853N/A inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac" 5858N/A# find symlinks linked to given 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# Same as above, but specialized to handle dynamic libraries. 1653N/A# It handles making symlinks according to how the original library 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 # Create additional symlinks. See rev_symlinks description. 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/A# Same as above, but specialized to install binary executables. 1653N/A# Install binary executable, and all shared library dependencies, if any. 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/A # If debug is set, clean unprintable chars to prevent messing up the term 1653N/A# same as above, but specialized for symlinks 3853N/A# attempt to install any programs specified in a udev rule 1653N/A #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)" 1653N/A# udev rules always get installed in the same place, so 1653N/A# create a function to install them to make life simpler. 5858N/A# general purpose installation function 1653N/A# install any of listed files 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. 3853N/A [[ $1 = '-d' ]] && to="$2" && shift 2 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/A dinfo "Skipping program $1 as it cannot be found and is" \ 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/A # no need to go further if the module is already installed 1653N/A dinfo "Possible missing firmware \"${_fw}\" for kernel module" \ 1653N/A dwarn "Possible missing firmware \"${_fw}\" for kernel module" \ 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/A# filter kernel modules to install certain modules that meet specific 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/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 # called [sub]functions inherit _fderr 1653N/A cat "${KERNEL_MODS}/modules.${_mod#=}" ) \ 1653N/A # if we are already installed, skip this module and go on 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 # 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. 3853N/A # ok, load the module, all its dependencies, and any firmware 1653N/A if (($# == 0)); then # filenames from stdin 1653N/A while (($# > 0)); do # filenames as arguments 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# 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/A if [[ "$1" == "-n" ]]; then