test-functions revision 0d6e798a784ef0ba6b95512e4453067b2f84a91a
## @brief Converts numeric logging level to the first letter of level name. # @param lvl Numeric logging level in range from 1 to 6. # @retval 1 if @a lvl is out of range. # @retval 0 if @a lvl is correct. # @result Echoes first letter of level name. ## @brief Internal helper function for _do_dlog() # @param lvl Numeric logging level. # @retval 0 It's always returned, even if logging failed. # @note This function is not supposed to be called manually. Please use # dtrace(), ddebug(), or others instead which wrap this one. # This function calls _do_dlog() either with parameter msg, or if # none is given, it will read standard input and will use every line as # dwarn "This is a warning" # echo "This is a warning" | dwarn ## @brief Logs message at TRACE level (6) # @retval 0 It's always returned, even if logging failed. [ -n "$debug" ] && set -x || : ## @brief Logs message at DEBUG level (5) # @retval 0 It's always returned, even if logging failed. # [ -n "$debug" ] && set -x || : ## @brief Logs message at INFO level (4) # @retval 0 It's always returned, even if logging failed. [ -n "$debug" ] && set -x || : ## @brief Logs message at WARN level (3) # @retval 0 It's always returned, even if logging failed. [ -n "$debug" ] && set -x || : ## @brief Logs message at ERROR level (2) # @retval 0 It's always returned, even if logging failed. # [ -n "$debug" ] && set -x || : ## @brief Logs message at FATAL level (1) # @retval 0 It's always returned, even if logging failed. [ -n "$debug" ] && set -x || : # Generic substring function. If $2 is in $1, return 0. strstr() { [ "${1#*$2*}" != "$1" ]; } # Prints the normalized path, where it removes any duplicated # convert_abs_rel <from> <to> # Prints the relative path, when creating a symlink to <to> from <from>. # corner case #1 - self looping link [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; } # corner case #2 - own dir link [[ "${1%/*}" == "$2" ]] && { echo "."; return; } # Install a directory, keeping symlinks as on the original system. # Example: if /lib points to /lib64 on the host, "inst_dir /lib/file" # will create ${initdir}/lib64, ${initdir}/lib64/file, # and a symlink ${initdir}/lib -> lib64. [[ -e ${initdir}/"$1" ]] && return 0 # already there while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do # iterate over parent directories [[ -e "${initdir}/$_file" ]] && continue # $1 = file to copy to ramdisk # $2 (optional) Name for the file on the ramdisk # Location of the image dir is assumed to be $initdir # We never overwrite the target if it exists. [[ -f "$1" ]] || return 1 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}" # install checksum files also if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac" # find symlinks linked to given library file # Function searches for symlinks by stripping version numbers appended to # library filename, checks if it points to the same target and finally # prints the list of symlinks to stdout. [[ ${fn} =~ .*\.so\..* ]] || return 1 until [[ ${fn##*.} == so ]]; do # Same as above, but specialized to handle dynamic libraries. # It handles making symlinks according to how the original library # install checksum files also if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac" # Create additional symlinks. See rev_symlinks description. # find a binary. If we were not passed the full path directly, # search in the usual places to find the binary. if [[ -z ${1##/*} ]]; then # Same as above, but specialized to install binary executables. # Install binary executable, and all shared library dependencies, if any. local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)' [[ $_line = 'not a dynamic executable' ]] && break dfatal "dracut cannot create an initrd." # same as above, except for shell scripts. # If your shell script does not start with shebang, it is not a shell script. # If debug is set, clean unprintable chars to prevent messing up the term # same as above, but specialized for symlinks # attempt to install any programs specified in a udev rule if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)" # udev rules always get installed in the same place, so # create a function to install them to make life simpler. if [ "${rule#/}" = "$rule" ]; then # general purpose installation function *) dfatal "inst only takes 1 or 2 or 3 arguments" # install any of listed files # If first argument is '-d' and second some destination path, first accessible # source is installed into this path, otherwise it will installed in the same # path as source. If none of listed files was installed, function return 1. # On first successful installation it returns with 0 status. # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in [[ $1 = '-d' ]] && to="$2" && shift 2 # dracut_install [-o ] <file> [<file> ... ] # Install <file> to the initramfs image # -o optionally install the <file> and don't fail, if it is not there dinfo "Skipping program $1 as it cannot be found and is" \ # Install a single kernel module along with any firmware it may require. # $1 = full path to kernel module to install # no need to go further if the module is already installed [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0 local _modname=${1##*/} _fwdir _found _fw dinfo "Possible missing firmware \"${_fw}\" for kernel module" \ dwarn "Possible missing firmware \"${_fw}\" for kernel module" \ # Do something with all the dependencies of a kernel module. # Note that kernel modules depend on themselves using the technique we use # $1 = function to call for each dependency we find # It will be passed the full path to the found kernel module # $2 = module to get dependencies for # rest of args = arguments to modprobe # _fderr specifies FD passed from surrounding scope # filter kernel modules to install certain modules that meet specific # $1 = search only in subdirectory of /kernel/$1 # $2 = function to call with module name to filter. # This function will be passed the full path to the module to test. # The behaviour of this function can vary depending on whether $hostonly is set. # If it is, we will only look at modules that are already in memory. # If it is not, we will look at all kernel modules # This function returns the full filenames of modules that match $1 # instmods [-c] <kernel module> [<kernel module> ... ] # instmods [-c] <kernel subsystem> # install kernel modules along with all their dependencies. # called [sub]functions inherit _fderr cat "${KERNEL_MODS}/modules.${_mod#=}" ) \ i2o_scsi) return ;; # Do not load this diagnostic-only module # if we are already installed, skip this module and go on [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return # If we are building a host-specific initramfs and this # module is not already loaded, move on to the next one. # We use '-d' option in modprobe only if modules prefix path # differs from default '/'. This allows us to use Dracut with # old version of modprobe which doesn't have '-d' option. # ok, load the module, all its dependencies, and any firmware if (($# == 0)); then # filenames from stdin while (($# > 0)); do # filenames as arguments # Capture all stderr from modprobe to _fderr. We could use {var}>... # redirections, but that would make dracut require bash4 at least. eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \ # inst_libdir_file [-n <pattern>] <file> [<file>...] # Install a <file> located on a lib directory to the initramfs image # -n <pattern> install non-matching files if [[ "$1" == "-n" ]]; then if [[ $UID != "0" ]]; then