test-functions revision c7eda0133b6bf13a182337cbe8a61bf2faf9b32e
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt# ex: ts=8 sw=4 sts=4 et filetype=sh
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flyktif ! ROOTLIBDIR=$(pkg-config --variable=systemdutildir systemd); then
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt echo "WARNING! Cannot determine rootlibdir from pkg-config, assuming /usr/lib/systemd" >&2
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik FlyktBASICTOOLS="sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe"
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik FlyktDEBUGTOOLS="df free ls stty cat ps ln ip route dmesg dhclient mkdir cp ping dhclient strace less grep id tty touch du sort hostname"
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a kvm qemu-kvm 2>/dev/null | grep '^/' -m1)
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt # QEMU's own build system calls it qemu-system-x86_64
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-x86_64 2>/dev/null | grep '^/' -m1)
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-i386 2>/dev/null | grep '^/' -m1)
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu 2>/dev/null | grep '^/' -m1)
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen echo "Could not find a suitable QEMU binary" >&2
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt if [ -f /etc/machine-id ]; then
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [ -z "$INITRD" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/initrd" ] \
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt && INITRD="/boot/$MACHINE_ID/$KERNEL_VER/initrd"
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [ -z "$KERNEL_BIN" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/linux" ] \
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt && KERNEL_BIN="/boot/$MACHINE_ID/$KERNEL_VER/linux"
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt [ "$KERNEL_BIN" ] || KERNEL_BIN=/boot/vmlinuz-$KERNEL_VER
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt [ "$INITRD" ] || INITRD=/boot/initramfs-${KERNEL_VER}.img
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersenraid=noautodetect \
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersenconsole=ttyS0 \
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND" $TESTDIR/rootdisk.img ) || return 1
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt ../../systemd-nspawn --boot --directory=$TESTDIR/nspawn-root $ROOTLIBDIR/systemd $KERNEL_APPEND
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt type -P dmeventd >/dev/null && dracut_install dmeventd
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt inst_rules 10-dm.rules 13-dm-disk.rules 95-dm-notify.rules
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt (cd $TEST_BASE_DIR/..; set -x; make DESTDIR=$initdir install)
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt # we strip binaries since debug symbols increase binaries size a lot
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt for i in $initdir/{sbin,bin}/* $initdir/lib/systemd/*; do
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt # Create the blank file to use as a root filesystem
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt dd if=/dev/null of="$TESTDIR/rootdisk.img" bs=1M seek=300
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt LOOPDEV=$(losetup --show -P -f $TESTDIR/rootdisk.img)
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [[ -f $TESTDIR/nspawn-root/failed ]] && cp -a $TESTDIR/nspawn-root/failed $TESTDIR
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt cp -a $TESTDIR/nspawn-root/var/log/journal $TESTDIR
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | xargs strip --strip-unneeded | ddebug
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt ddebug "install any Execs from the service files"
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt export PKG_CONFIG_PATH=$TEST_BASE_DIR/../src/core/
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt systemdsystemunitdir=$(pkg-config --variable=systemdsystemunitdir systemd)
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt systemduserunitdir=$(pkg-config --variable=systemduserunitdir systemd)
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/{$systemdsystemunitdir,$systemduserunitdir}/*.service \
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt | while read i; do
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt if [[ -d $initdir/lib/modules/$KERNEL_VER ]] && \
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt # install plymouth, if found... else remove plymouth service files
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt rm -f $initdir/{usr/lib,etc}/systemd/system/plymouth* $initdir/{usr/lib,etc}/systemd/system/*/plymouth*
a9aff3615b430f86bd0a824214d95f634efaf894Patrik FlyktLABEL=systemd / ext3 rw 0 1
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt [[ $BASICTOOLS ]] && dracut_install $BASICTOOLS
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt # in Debian ldconfig is just a shell script wrapper around ldconfig.real
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt [[ $DEBUGTOOLS ]] && dracut_install $DEBUGTOOLS
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt NSS_LIBS=$(LD_DEBUG=files getent passwd 2>&1 >/dev/null |sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}')
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt | while read file; do
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt | while read file; do
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt [[ -f $i ]] || continue
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt /usr/lib/kbd/consolefonts/latarcyrheb-sun16*; do
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt [[ -f $i ]] || continue
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt [ -f ${_terminfodir}/l/linux ] && break
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt cp $TEST_BASE_DIR/testsuite.target $initdir/etc/systemd/system/
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt cp $TEST_BASE_DIR/end.service $initdir/etc/systemd/system/
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt mkdir -p $initdir/etc/systemd/system/testsuite.target.wants
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt ln -fs $TEST_BASE_DIR/testsuite.service $initdir/etc/systemd/system/testsuite.target.wants/testsuite.service
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt ln -fs $TEST_BASE_DIR/end.service $initdir/etc/systemd/system/testsuite.target.wants/end.service
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt ln -fs testsuite.target $initdir/etc/systemd/system/default.target
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt 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
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt [[ $_line = 'not a dynamic executable' ]] && break
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt if [[ $_line =~ $_so_regex ]]; then
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt [[ -e ${initdir}/$_file ]] && continue
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt if [[ $_line =~ not\ found ]]; then
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt dfatal "Missing a shared library required by $_bin."
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt dfatal "Run \"ldd $_bin\" to find out what it is."
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt TESTDIR=$(mktemp --tmpdir=/var/tmp -d -t systemd-test.XXXXXX)
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt## @brief Converts numeric logging level to the first letter of level name.
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt# @param lvl Numeric logging level in range from 1 to 6.
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt# @retval 1 if @a lvl is out of range.
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt# @retval 0 if @a lvl is correct.
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt# @result Echoes first letter of level name.
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt *) return 1;;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt## @brief Internal helper function for _do_dlog()
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt# @param lvl Numeric logging level.
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt# @param msg Message.
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt# @retval 0 It's always returned, even if logging failed.
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt# @note This function is not supposed to be called manually. Please use
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt# dtrace(), ddebug(), or others instead which wrap this one.
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt# This function calls _do_dlog() either with parameter msg, or if
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt# none is given, it will read standard input and will use every line as
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt# This enables:
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt# dwarn "This is a warning"
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt# echo "This is a warning" | dwarn
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt [ $1 -le $LOG_LEVEL ] || return 0
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt if [ $# -ge 1 ]; then
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt while read line; do
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt## @brief Logs message at TRACE level (6)
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt# @param msg Message.
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt# @retval 0 It's always returned, even if logging failed.
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt## @brief Logs message at DEBUG level (5)
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt# @param msg Message.
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt# @retval 0 It's always returned, even if logging failed.
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt# [ -n "$debug" ] && set -x || :
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt## @brief Logs message at INFO level (4)
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt# @param msg Message.
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt# @retval 0 It's always returned, even if logging failed.
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt## @brief Logs message at WARN level (3)
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# @param msg Message.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# @retval 0 It's always returned, even if logging failed.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt## @brief Logs message at ERROR level (2)
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# @param msg Message.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# @retval 0 It's always returned, even if logging failed.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# [ -n "$debug" ] && set -x || :
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt## @brief Logs message at FATAL level (1)
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# @param msg Message.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# @retval 0 It's always returned, even if logging failed.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# Generic substring function. If $2 is in $1, return 0.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# normalize_path <path>
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# Prints the normalized path, where it removes any duplicated
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# and trailing slashes.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# $ normalize_path ///test/test//
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# convert_abs_rel <from> <to>
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# Prints the relative path, when creating a symlink to <to> from <from>.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# $ convert_abs_rel /usr/bin/test /bin/test-2
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt local __current __absolute __abssize __cursize __newpath
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt while [[ ${__absolute[__level]} == ${__current[__level]} ]]
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt if (( __level > __abssize || __level > __cursize ))
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt for ((__i = __level; __i < __cursize-1; __i++))
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# Install a directory, keeping symlinks as on the original system.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# will create ${initdir}/lib64, ${initdir}/lib64/file,
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt# and a symlink ${initdir}/lib -> lib64.
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt [[ -e ${initdir}/"$1" ]] && return 0 # already there
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt if [[ -L $_file ]]; then
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt mkdir -m 0755 -p "${initdir}/$_file" || return 1
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt# $1 = file to copy to ramdisk
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt# $2 (optional) Name for the file on the ramdisk
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# Location of the image dir is assumed to be $initdir
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# We never overwrite the target if it exists.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt if ! [[ -d ${initdir}/$target ]]; then
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [[ -e ${initdir}/$target ]] && return 0
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [[ -L ${initdir}/$target ]] && return 0
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt cp --sparse=always -pfL "$_src" "${initdir}/$target"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# find symlinks linked to given library file
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# $1 = library file
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# Function searches for symlinks by stripping version numbers appended to
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# library filename, checks if it points to the same target and finally
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# prints the list of symlinks to stdout.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# rev_lib_symlinks libfoo.so.8.1
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt# (Only if libfoo.so.8 and libfoo.so exists on host system.)
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt [[ ! $1 ]] && return 0
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt local fn="$1" orig="$(readlink -f "$1")" links=''
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt [[ ${fn} =~ .*\.so\..* ]] || return 1
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# Same as above, but specialized to handle dynamic libraries.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# It handles making symlinks according to how the original library
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# is referenced.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [[ -e $initdir/$_dest ]] && return 0
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt if [[ -L $_src ]]; then
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt ln -sfn $(convert_abs_rel "${_dest}" "${_reallib}") "${initdir}/${_dest}"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt # Create additional symlinks. See rev_symlinks description.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# find a binary. If we were not passed the full path directly,
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# search in the usual places to find the binary.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# Same as above, but specialized to install binary executables.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# Install binary executable, and all shared library dependencies, if any.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [[ -e $initdir/$_target ]] && return 0
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [[ -L $_bin ]] && inst_symlink $_bin $_target && return 0
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt [[ $_line = 'not a dynamic executable' ]] && break
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt if [[ $_line =~ $_so_regex ]]; then
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt [[ -e ${initdir}/$_file ]] && continue
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt if [[ $_line =~ not\ found ]]; then
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt dfatal "Missing a shared library required by $_bin."
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt dfatal "Run \"ldd $_bin\" to find out what it is."
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# same as above, except for shell scripts.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# If your shell script does not start with shebang, it is not a shell script.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt # If debug is set, clean unprintable chars to prevent messing up the term
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]')
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt [[ $_line =~ $_shebang_regex ]] || return 1
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@"
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt# same as above, but specialized for symlinks
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt local _src=$1 _target=${2:-$1} _realsrc
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt [[ -L $1 ]] || return 1
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt [[ -L $initdir/$_target ]] && return 0
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt if ! [[ -e $initdir/$_realsrc ]]; then
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt if [[ -d $_realsrc ]]; then
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}"
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt ln -sfn $(convert_abs_rel "${_target}" "${_realsrc}") "$initdir/$_target"
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt# attempt to install any programs specified in a udev rule
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt if [ -x /lib/udev/$_prog ]; then
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt# udev rules always get installed in the same place, so
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt# create a function to install them to make life simpler.
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt for r in /lib/udev/rules.d /etc/udev/rules.d; do
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt if [[ -f $r/$_rule ]]; then
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt if [[ -f ${r}$_rule ]]; then
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt# general purpose installation function
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt# Same args as above.
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt 2) [[ ! $initdir && -d $2 ]] && export initdir=$2
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt [[ $initdir = $2 ]] && set $1;;
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt 3) [[ -z $initdir ]] && export initdir=$2
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt *) dfatal "inst only takes 1 or 2 or 3 arguments"
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt for _x in inst_symlink inst_script inst_binary inst_simple; do
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt# install any of listed files
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt# If first argument is '-d' and second some destination path, first accessible
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt# source is installed into this path, otherwise it will installed in the same
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt# path as source. If none of listed files was installed, function return 1.
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt# On first successful installation it returns with 0 status.
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt# inst_any -d /bin/foo /bin/bar /bin/baz
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt# Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt if [[ -e $f ]]; then
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt# dracut_install [-o ] <file> [<file> ... ]
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt# Install <file> to the initramfs image
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt# -o optionally install the <file> and don't fail, if it is not there
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt if [[ $_optional = yes ]]; then
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt dinfo "Skipping program $1 as it cannot be found and is" \
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt# Install a single kernel module along with any firmware it may require.
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt# $1 = full path to kernel module to install
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt # no need to go further if the module is already installed
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~ $omit_drivers ]]; then
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt inst_simple "$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt || return $?
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt for _fw in $(modinfo -k $KERNEL_VER -F firmware $1 2>/dev/null); do
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt if [[ $_found != yes ]]; then
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt if ! grep -qe "\<${_modname//-/_}\>" /proc/modules; then
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt# Do something with all the dependencies of a kernel module.
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt# Note that kernel modules depend on themselves using the technique we use
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt# $1 = function to call for each dependency we find
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt# It will be passed the full path to the found kernel module
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt# $2 = module to get dependencies for
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt# rest of args = arguments to modprobe
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt# _fderr specifies FD passed from surrounding scope
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt local _func=$1 _kmod=$2 _cmd _modpath _options _found=0
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt while read _cmd _modpath _options; do
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [[ $_cmd = insmod ]] || continue
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [[ $_found -eq 0 ]] && exit 1
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt# filter kernel modules to install certain modules that meet specific
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt# requirements.
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt# $1 = search only in subdirectory of /kernel/$1
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt# $2 = function to call with module name to filter.
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt# This function will be passed the full path to the module to test.
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt# The behavior of this function can vary depending on whether $hostonly is set.
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt# If it is, we will only look at modules that are already in memory.
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt# If it is not, we will look at all kernel modules
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt# This function returns the full filenames of modules that match $1
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt if ! [[ $hostonly ]]; then
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt _filtercmd='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen _filtercmd+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen _filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen *.ko.gz) gzip -dc "$_modname" > $initdir/$$.ko
if ! [[ $hostonly ]]; then
[[ $no_kernel = yes ]] && return
local _fderr=9
| instmods
| instmods
local _moddirname=${KERNEL_MODS%%/lib/modules/*}
--set-version $KERNEL_VER ${_moddirname} $_mpargs
return $_ret
while read _mod; do
set -o pipefail
| while read line; do [[ "$line" =~ $_filter_not_found ]] && echo $line || echo $line >&2 ;done | derror
_ret=$?
set +o pipefail
return $_ret
local _pattern=$1
case $1 in
--run)
ret=$?
exit $ret;;
--setup)
--clean)
--all)
ret=$?
exit $ret
ret=$?
exit $ret;;