test-functions revision c7eda0133b6bf13a182337cbe8a61bf2faf9b32e
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt#!/bin/bash
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 FlyktPATH=/sbin:/bin:/usr/sbin:/usr/bin
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flyktexport PATH
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik FlyktKERNEL_VER=${KERNEL_VER-$(uname -r)}
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik FlyktKERNEL_MODS="/lib/modules/$KERNEL_VER/"
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
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 Flykt ROOTLIBDIR=/usr/lib/systemd
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flyktfi
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
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
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flyktfunction find_qemu_bin() {
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt # SUSE and Red Hat call the binary qemu-kvm
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt # Debian and Gentoo call it kvm
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a kvm qemu-kvm 2>/dev/null | grep '^/' -m1)
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [ "$ARCH" ] || ARCH=$(uname -m)
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt case $ARCH in
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt x86_64)
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)
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt ;;
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen i*86)
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt # new i386 version of QEMU
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-i386 2>/dev/null | grep '^/' -m1)
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt # i386 version of QEMU
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu 2>/dev/null | grep '^/' -m1)
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt ;;
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt esac
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt if [ ! -e "$QEMU_BIN" ]; then
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen echo "Could not find a suitable QEMU binary" >&2
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen return 1
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen fi
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt}
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flyktrun_qemu() {
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt if [ -f /etc/machine-id ]; then
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt read MACHINE_ID < /etc/machine-id
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 fi
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt [ "$KERNEL_BIN" ] || KERNEL_BIN=/boot/vmlinuz-$KERNEL_VER
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt [ "$INITRD" ] || INITRD=/boot/initramfs-${KERNEL_VER}.img
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt [ "$QEMU_SMP" ] || QEMU_SMP=1
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt find_qemu_bin || return 1
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt KERNEL_APPEND="root=/dev/sda1 \
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flyktsystemd.log_level=debug \
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersenraid=noautodetect \
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersenloglevel=2 \
a276e6d68606861b552140cbcc003f4af10626fcTom Gunderseninit=$ROOTLIBDIR/systemd \
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersenro \
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersenconsole=ttyS0 \
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersenselinux=0 \
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt$KERNEL_APPEND \
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt"
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt QEMU_OPTIONS="-machine accel=kvm:tcg \
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt-smp $QEMU_SMP \
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt-net none \
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt-m 512M \
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt-nographic \
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt-kernel $KERNEL_BIN \
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt"
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt if [ "$INITRD" ]; then
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt QEMU_OPTIONS="$QEMU_OPTIONS -initrd $INITRD"
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt fi
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt ( set -x
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND" $TESTDIR/rootdisk.img ) || return 1
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt}
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flyktrun_nspawn() {
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt set -x
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt ../../systemd-nspawn --boot --directory=$TESTDIR/nspawn-root $ROOTLIBDIR/systemd $KERNEL_APPEND
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt}
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flyktsetup_basic_environment() {
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt # create the basic filesystem layout
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt setup_basic_dirs
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt install_systemd
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt install_missing_libraries
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt install_config_files
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt create_rc_local
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt install_basic_tools
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt install_libnss
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt install_pam
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt install_dbus
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt install_fonts
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt install_keymaps
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt install_terminfo
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt install_execs
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt install_plymouth
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt install_debug_tools
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt install_ld_so_conf
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt strip_binaries
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt install_depmod_files
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt generate_module_dependencies
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt # softlink mtab
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt ln -fs /proc/self/mounts $initdir/etc/mtab
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt}
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flyktinstall_dmevent() {
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt instmods dm_crypt =crypto
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt type -P dmeventd >/dev/null && dracut_install dmeventd
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt inst_libdir_file "libdevmapper-event.so*"
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt inst_rules 10-dm.rules 13-dm-disk.rules 95-dm-notify.rules
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt}
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flyktinstall_systemd() {
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt # install compiled files
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt (cd $TEST_BASE_DIR/..; set -x; make DESTDIR=$initdir install)
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt # remove unneeded documentation
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt rm -fr $initdir/usr/share/{man,doc}
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt # we strip binaries since debug symbols increase binaries size a lot
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt # and it could fill the available space
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt strip_binaries
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt}
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flyktinstall_missing_libraries() {
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt # install possible missing libraries
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt for i in $initdir/{sbin,bin}/* $initdir/lib/systemd/*; do
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt inst_libs $i
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt done
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt}
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flyktcreate_empty_image() {
ea3b3a75abb3f8b853f7da454b9b8e258a120eeaPatrik Flykt rm -f "$TESTDIR/rootdisk.img"
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 [ -b "$LOOPDEV" ] || return 1
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt echo "LOOPDEV=$LOOPDEV" >> $STATEFILE
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt sfdisk "$LOOPDEV" <<EOF
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt,290M
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt,
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik FlyktEOF
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt mkfs.ext3 -L systemd "${LOOPDEV}p1"
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt}
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flyktcheck_result_nspawn() {
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt ret=1
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [[ -e $TESTDIR/nspawn-root/testok ]] && ret=0
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
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt ls -l $TESTDIR/journal/*/*.journal
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt test -s $TESTDIR/failed && ret=$(($ret+1))
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt return $ret
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt}
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flyktstrip_binaries() {
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt ddebug "Strip binaries"
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | xargs strip --strip-unneeded | ddebug
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt}
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flyktcreate_rc_local() {
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt mkdir -p $initdir/etc/rc.d
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt cat >$initdir/etc/rc.d/rc.local <<EOF
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt#!/bin/bash
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flyktexit 0
d1b0afe3653b4316a6361d204169620726d468a0Patrik FlyktEOF
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt chmod 0755 $initdir/etc/rc.d/rc.local
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt}
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flyktinstall_execs() {
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt ddebug "install any Execs from the service files"
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt (
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
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt i=${i##Exec*=}; i=${i##-}
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt inst $i
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt done
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt )
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt}
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flyktgenerate_module_dependencies() {
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt if [[ -d $initdir/lib/modules/$KERNEL_VER ]] && \
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt ! depmod -a -b "$initdir" $KERNEL_VER; then
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt dfatal "\"depmod -a $KERNEL_VER\" failed."
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt exit 1
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt fi
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt}
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flyktinstall_depmod_files() {
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt inst /lib/modules/$KERNEL_VER/modules.order
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt inst /lib/modules/$KERNEL_VER/modules.builtin
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt}
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flyktinstall_plymouth() {
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 # dracut_install plymouth plymouthd
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt # else
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt rm -f $initdir/{usr/lib,etc}/systemd/system/plymouth* $initdir/{usr/lib,etc}/systemd/system/*/plymouth*
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt # fi
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt}
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flyktinstall_ld_so_conf() {
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt cp -a /etc/ld.so.conf* $initdir/etc
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt ldconfig -r "$initdir"
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt}
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flyktinstall_config_files() {
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt inst /etc/sysconfig/init
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt inst /etc/passwd
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt inst /etc/shadow
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt inst /etc/group
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt inst /etc/shells
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt inst /etc/nsswitch.conf
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt inst /etc/pam.conf
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt inst /etc/securetty
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt inst /etc/os-release
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt inst /etc/localtime
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt # we want an empty environment
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt > $initdir/etc/environment
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt > $initdir/etc/machine-id
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt # set the hostname
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt echo systemd-testsuite > $initdir/etc/hostname
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt # fstab
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt cat >$initdir/etc/fstab <<EOF
a9aff3615b430f86bd0a824214d95f634efaf894Patrik FlyktLABEL=systemd / ext3 rw 0 1
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik FlyktEOF
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt}
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flyktinstall_basic_tools() {
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt [[ $BASICTOOLS ]] && dracut_install $BASICTOOLS
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt dracut_install -o sushell
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt # in Debian ldconfig is just a shell script wrapper around ldconfig.real
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt dracut_install -o ldconfig.real
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt}
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flyktinstall_debug_tools() {
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt [[ $DEBUGTOOLS ]] && dracut_install $DEBUGTOOLS
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt}
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flyktinstall_libnss() {
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt # install libnss_files for login
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt NSS_LIBS=$(LD_DEBUG=files getent passwd 2>&1 >/dev/null |sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}')
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt dracut_install $NSS_LIBS
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt}
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flyktinstall_dbus() {
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt inst $ROOTLIBDIR/system/dbus.socket
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt inst $ROOTLIBDIR/system/dbus.service
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt find \
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt /etc/dbus-1 /usr/share/dbus-1 -xtype f \
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt | while read file; do
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt inst $file
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt done
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt}
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flyktinstall_pam() {
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt find \
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt /etc/pam.d \
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt /etc/security \
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt /lib64/security \
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt /lib/security -xtype f \
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt | while read file; do
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt inst $file
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt done
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt}
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flyktinstall_keymaps() {
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt for i in \
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt /usr/lib/kbd/keymaps/include/* \
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt /usr/lib/kbd/keymaps/i386/include/* \
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt /usr/lib/kbd/keymaps/i386/qwerty/us.*; do
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt [[ -f $i ]] || continue
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt inst $i
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt done
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt}
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flyktinstall_fonts() {
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt for i in \
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt /usr/lib/kbd/consolefonts/eurlatgr* \
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt /usr/lib/kbd/consolefonts/latarcyrheb-sun16*; do
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt [[ -f $i ]] || continue
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt inst $i
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt done
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt}
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flyktinstall_terminfo() {
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt [ -f ${_terminfodir}/l/linux ] && break
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt done
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt dracut_install -o ${_terminfodir}/l/linux
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt}
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flyktsetup_testsuite() {
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
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
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt # make the testsuite the default target
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt ln -fs testsuite.target $initdir/etc/systemd/system/default.target
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt}
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flyktsetup_nspawn_root() {
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt rm -fr $TESTDIR/nspawn-root
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt ddebug "cp -ar $initdir $TESTDIR/nspawn-root"
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt cp -ar $initdir $TESTDIR/nspawn-root
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt # we don't mount in the nspawn root
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt rm -f $TESTDIR/nspawn-root/etc/fstab
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt}
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flyktsetup_basic_dirs() {
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt mkdir -p $initdir/run
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt mkdir -p $initdir/etc/systemd/system
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt mkdir -p $initdir/var/log/journal
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
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 if [ -L "/$d" ]; then
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt inst_symlink "/$d"
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt else
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt inst_dir "/$d"
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt fi
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt done
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt ln -sfn /run "$initdir/var/run"
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt ln -sfn /run/lock "$initdir/var/lock"
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt}
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flyktinst_libs() {
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt local _bin=$1
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt local _file _line
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt [[ $_line = 'not a dynamic executable' ]] && break
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt if [[ $_line =~ $_so_regex ]]; then
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt _file=${BASH_REMATCH[1]}
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt [[ -e ${initdir}/$_file ]] && continue
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt inst_library "$_file"
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt continue
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt fi
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
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 dfatal "$_line"
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt dfatal "dracut cannot create an initrd."
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt exit 1
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt fi
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt done
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt}
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flyktimport_testdir() {
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt STATEFILE=".testdir"
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt [[ -e $STATEFILE ]] && . $STATEFILE
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt TESTDIR=$(mktemp --tmpdir=/var/tmp -d -t systemd-test.XXXXXX)
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt echo "TESTDIR=\"$TESTDIR\"" > $STATEFILE
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt export TESTDIR
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt fi
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt}
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flyktimport_initdir() {
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt initdir=$TESTDIR/root
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt export initdir
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt}
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt## @brief Converts numeric logging level to the first letter of level name.
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt#
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_lvl2char() {
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt case "$1" in
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt 1) echo F;;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt 2) echo E;;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt 3) echo W;;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt 4) echo I;;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt 5) echo D;;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt 6) echo T;;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt *) return 1;;
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt esac
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt}
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt## @brief Internal helper function for _do_dlog()
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt#
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#
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#
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# a message.
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt#
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt# This enables:
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt# dwarn "This is a warning"
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt# echo "This is a warning" | dwarn
d1b0afe3653b4316a6361d204169620726d468a0Patrik FlyktLOG_LEVEL=4
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flyktdlog() {
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt [ -z "$LOG_LEVEL" ] && return 0
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt [ $1 -le $LOG_LEVEL ] || return 0
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt local lvl="$1"; shift
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt local lvlc=$(_lvl2char "$lvl") || return 0
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt if [ $# -ge 1 ]; then
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt echo "$lvlc: $*"
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt else
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt while read line; do
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt echo "$lvlc: " "$line"
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt done
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt fi
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt}
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt## @brief Logs message at TRACE level (6)
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt#
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt# @param msg Message.
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt# @retval 0 It's always returned, even if logging failed.
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flyktdtrace() {
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt set +x
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt dlog 6 "$@"
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt [ -n "$debug" ] && set -x || :
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt}
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt## @brief Logs message at DEBUG level (5)
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt#
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt# @param msg Message.
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt# @retval 0 It's always returned, even if logging failed.
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flyktddebug() {
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt# set +x
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt dlog 5 "$@"
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt# [ -n "$debug" ] && set -x || :
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt}
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt## @brief Logs message at INFO level (4)
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt#
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt# @param msg Message.
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt# @retval 0 It's always returned, even if logging failed.
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flyktdinfo() {
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt set +x
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt dlog 4 "$@"
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt [ -n "$debug" ] && set -x || :
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt}
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt## @brief Logs message at WARN level (3)
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt#
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# @param msg Message.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# @retval 0 It's always returned, even if logging failed.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flyktdwarn() {
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt set +x
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt dlog 3 "$@"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [ -n "$debug" ] && set -x || :
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt}
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt## @brief Logs message at ERROR level (2)
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt#
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# @param msg Message.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# @retval 0 It's always returned, even if logging failed.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flyktderror() {
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# set +x
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt dlog 2 "$@"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# [ -n "$debug" ] && set -x || :
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt}
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt## @brief Logs message at FATAL level (1)
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt#
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# @param msg Message.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# @retval 0 It's always returned, even if logging failed.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flyktdfatal() {
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt set +x
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt dlog 1 "$@"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [ -n "$debug" ] && set -x || :
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt}
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# Generic substring function. If $2 is in $1, return 0.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flyktstrstr() { [ "${1#*$2*}" != "$1" ]; }
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# normalize_path <path>
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# Prints the normalized path, where it removes any duplicated
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# and trailing slashes.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# Example:
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# $ normalize_path ///test/test//
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# /test/test
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flyktnormalize_path() {
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt shopt -q -s extglob
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt set -- "${1//+(\/)//}"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt shopt -q -u extglob
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt echo "${1%/}"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt}
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# convert_abs_rel <from> <to>
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# Prints the relative path, when creating a symlink to <to> from <from>.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# Example:
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# $ convert_abs_rel /usr/bin/test /bin/test-2
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# ../../bin/test-2
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flyktconvert_abs_rel() {
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt local __current __absolute __abssize __cursize __newpath
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt local -i __i __level
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt # corner case #1 - self looping link
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt # corner case #2 - own dir link
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt IFS="/" __current=($1)
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt IFS="/" __absolute=($2)
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt __abssize=${#__absolute[@]}
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt __cursize=${#__current[@]}
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt while [[ ${__absolute[__level]} == ${__current[__level]} ]]
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt do
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt (( __level++ ))
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt if (( __level > __abssize || __level > __cursize ))
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt then
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt break
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt fi
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt done
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt for ((__i = __level; __i < __cursize-1; __i++))
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt do
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt if ((__i > __level))
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt then
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt __newpath=$__newpath"/"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt fi
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt __newpath=$__newpath".."
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt done
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt for ((__i = __level; __i < __abssize; __i++))
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt do
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt if [[ -n $__newpath ]]
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt then
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt __newpath=$__newpath"/"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt fi
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt __newpath=$__newpath${__absolute[__i]}
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt done
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt echo "$__newpath"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt}
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
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 Flyktinst_dir() {
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt [[ -e ${initdir}/"$1" ]] && return 0 # already there
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt local _dir="$1" _part="${1%/*}" _file
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt _dir="$_part $_dir"
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt _part=${_part%/*}
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt done
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt # iterate over parent directories
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt for _file in $_dir; do
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt [[ -e "${initdir}/$_file" ]] && continue
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt if [[ -L $_file ]]; then
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt inst_symlink "$_file"
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt else
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt # create directory
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt mkdir -m 0755 -p "${initdir}/$_file" || return 1
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt chmod u+w "${initdir}/$_file"
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt fi
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt done
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt}
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
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 Flyktinst_simple() {
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [[ -f "$1" ]] || return 1
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt strstr "$1" "/" || return 1
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt local _src=$1 target="${2:-$1}"
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 fi
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt # install checksum files also
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt fi
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt ddebug "Installing $_src"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt cp --sparse=always -pfL "$_src" "${initdir}/$target"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt}
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
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#
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# Example:
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# rev_lib_symlinks libfoo.so.8.1
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# output: libfoo.so.8 libfoo.so
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt# (Only if libfoo.so.8 and libfoo.so exists on host system.)
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flyktrev_lib_symlinks() {
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt [[ ! $1 ]] && return 0
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt local fn="$1" orig="$(readlink -f "$1")" links=''
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt [[ ${fn} =~ .*\.so\..* ]] || return 1
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt until [[ ${fn##*.} == so ]]; do
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt fn="${fn%.*}"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt done
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt echo "${links}"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt}
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
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 Flyktinst_library() {
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt strstr "$1" "/" || return 1
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [[ -e $initdir/$_dest ]] && return 0
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt if [[ -L $_src ]]; then
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt # install checksum files also
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt fi
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt _reallib=$(readlink -f "$_src")
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt inst_simple "$_reallib" "$_reallib"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt inst_dir "${_dest%/*}"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt ln -sfn $(convert_abs_rel "${_dest}" "${_reallib}") "${initdir}/${_dest}"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt else
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt inst_simple "$_src" "$_dest"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt fi
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
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 [[ ! -e $initdir/$_symlink ]] && {
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt ddebug "Creating extra symlink: $_symlink"
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt inst_symlink $_symlink
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt }
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt done
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt}
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
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 Flyktfind_binary() {
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt if [[ -z ${1##/*} ]]; then
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt echo $1
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt return 0
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt fi
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt fi
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt type -P $1
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt}
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# Same as above, but specialized to install binary executables.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt# Install binary executable, and all shared library dependencies, if any.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flyktinst_binary() {
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt local _bin _target
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt _bin=$(find_binary "$1") || return 1
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt _target=${2:-$_bin}
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [[ -e $initdir/$_target ]] && return 0
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt [[ -L $_bin ]] && inst_symlink $_bin $_target && return 0
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt local _file _line
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt # I love bash!
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt [[ $_line = 'not a dynamic executable' ]] && break
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt if [[ $_line =~ $_so_regex ]]; then
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt _file=${BASH_REMATCH[1]}
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt [[ -e ${initdir}/$_file ]] && continue
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt inst_library "$_file"
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt continue
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt fi
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
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."
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt dfatal "$_line"
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt dfatal "dracut cannot create an initrd."
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt exit 1
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt fi
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt done
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt inst_simple "$_bin" "$_target"
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt}
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
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 Flyktinst_script() {
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt local _bin
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt _bin=$(find_binary "$1") || return 1
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt shift
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt local _line _shebang_regex
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt read -r -n 80 _line <"$_bin"
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 _shebang_regex='(#! *)(/[^ ]+).*'
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt [[ $_line =~ $_shebang_regex ]] || return 1
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@"
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt}
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt# same as above, but specialized for symlinks
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flyktinst_symlink() {
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt local _src=$1 _target=${2:-$1} _realsrc
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt strstr "$1" "/" || return 1
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt [[ -L $1 ]] || return 1
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt [[ -L $initdir/$_target ]] && return 0
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt _realsrc=$(readlink -f "$_src")
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt if ! [[ -e $initdir/$_realsrc ]]; then
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt if [[ -d $_realsrc ]]; then
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt inst_dir "$_realsrc"
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt else
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt inst "$_realsrc"
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt fi
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt fi
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"
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt}
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt# attempt to install any programs specified in a udev rule
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flyktinst_rule_programs() {
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt local _prog _bin
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
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 _bin=/lib/udev/$_prog
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt else
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt _bin=$(find_binary "$_prog") || {
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt continue;
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt }
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt fi
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt dracut_install "$_bin"
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt done
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt fi
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt}
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt
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 Flyktinst_rules() {
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt local _target=/etc/udev/rules.d _rule _found
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt inst_dir "/lib/udev/rules.d"
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt inst_dir "$_target"
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt for _rule in "$@"; do
7246333cb803b03440d3bd0bdaa233564d09b5aePatrik Flykt if [ "${rule#/}" = "$rule" ]; then
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt for r in /lib/udev/rules.d /etc/udev/rules.d; do
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt if [[ -f $r/$_rule ]]; then
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt _found="$r/$_rule"
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt inst_simple "$_found"
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt inst_rule_programs "$_found"
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt fi
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt done
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt fi
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt for r in '' ./ $dracutbasedir/rules.d/; do
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt if [[ -f ${r}$_rule ]]; then
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt _found="${r}$_rule"
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt inst_simple "$_found" "$_target/${_found##*/}"
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt inst_rule_programs "$_found"
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt fi
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt done
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt done
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt}
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt# general purpose installation function
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt# Same args as above.
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flyktinst() {
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt local _x
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt case $# in
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt 1) ;;
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 set $1 $3;;
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt *) dfatal "inst only takes 1 or 2 or 3 arguments"
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt exit 1;;
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt esac
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt for _x in inst_symlink inst_script inst_binary inst_simple; do
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt $_x "$@" && return 0
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt done
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt return 1
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt}
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt# install any of listed files
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt#
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#
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt# Example:
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt#
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt# inst_any -d /bin/foo /bin/bar /bin/baz
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt#
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt# Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt# initramfs.
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flyktinst_any() {
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt local to f
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt [[ $1 = '-d' ]] && to="$2" && shift 2
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt for f in "$@"; do
a34b57c0d43b8bf819ccd4f62c314b41b625454dPatrik Flykt if [[ -e $f ]]; then
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt [[ $to ]] && inst "$f" "$to" && return 0
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt inst "$f" && return 0
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt fi
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt done
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt return 1
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt}
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt
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
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flyktdracut_install() {
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt local _optional=no
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt if [[ $1 = '-o' ]]; then
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt _optional=yes
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt shift
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt fi
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt while (($# > 0)); do
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt if ! inst "$1" ; then
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt if [[ $_optional = yes ]]; then
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt dinfo "Skipping program $1 as it cannot be found and is" \
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt "flagged to be optional"
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt else
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt dfatal "Failed to install $1"
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt exit 1
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt fi
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt fi
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt shift
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt done
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt}
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
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 Flyktinstall_kmod_with_fw() {
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt # no need to go further if the module is already installed
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt && return 0
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt if [[ $omit_drivers ]]; then
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt local _kmod=${1##*/}
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt _kmod=${_kmod%.ko}
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt _kmod=${_kmod/-/_}
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt if [[ "$_kmod" =~ $omit_drivers ]]; then
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt dinfo "Omitting driver $_kmod"
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt return 1
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt fi
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~ $omit_drivers ]]; then
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt dinfo "Omitting driver $_kmod"
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt return 1
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt fi
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt fi
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [ -d "$initdir/.kernelmodseen" ] && \
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt > "$initdir/.kernelmodseen/${1##*/}"
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt inst_simple "$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt || return $?
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt local _modname=${1##*/} _fwdir _found _fw
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt _modname=${_modname%.ko*}
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt for _fw in $(modinfo -k $KERNEL_VER -F firmware $1 2>/dev/null); do
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt _found=''
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt for _fwdir in $fw_dir; do
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt _found=yes
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt fi
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt done
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 "\"${_modname}.ko\""
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt else
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt "\"${_modname}.ko\""
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt fi
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt fi
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt done
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt return 0
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt}
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
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 Flyktfor_each_kmod_dep() {
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt local _func=$1 _kmod=$2 _cmd _modpath _options _found=0
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt shift 2
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 $_func ${_modpath} || exit $?
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt _found=1
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt done
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [[ $_found -eq 0 ]] && exit 1
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt exit 0
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt )
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen}
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen
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
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flyktfilter_kernel_modules_by_path () (
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt local _modname _filtercmd
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+=' -o -name "*.ko.xz"'
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen _filtercmd+=' 2>/dev/null'
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen else
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen _filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen _filtercmd+='-k $KERNEL_VER 2>/dev/null'
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen fi
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen for _modname in $(eval $_filtercmd); do
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen case $_modname in
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen *.ko) "$2" "$_modname" && echo "$_modname";;
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen *.ko.gz) gzip -dc "$_modname" > $initdir/$$.ko
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen $2 $initdir/$$.ko && echo "$_modname"
a276e6d68606861b552140cbcc003f4af10626fcTom Gundersen rm -f $initdir/$$.ko
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt ;;
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt *.ko.xz) xz -dc "$_modname" > $initdir/$$.ko
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt $2 $initdir/$$.ko && echo "$_modname"
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt rm -f $initdir/$$.ko
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt ;;
esac
done
)
find_kernel_modules_by_path () (
if ! [[ $hostonly ]]; then
find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
-name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev/null
else
cut -d " " -f 1 </proc/modules \
| xargs modinfo -F filename -k $KERNEL_VER 2>/dev/null
fi
)
filter_kernel_modules () {
filter_kernel_modules_by_path drivers "$1"
}
find_kernel_modules () {
find_kernel_modules_by_path drivers
}
# instmods [-c] <kernel module> [<kernel module> ... ]
# instmods [-c] <kernel subsystem>
# install kernel modules along with all their dependencies.
# <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
instmods() {
[[ $no_kernel = yes ]] && return
# called [sub]functions inherit _fderr
local _fderr=9
local _check=no
if [[ $1 = '-c' ]]; then
_check=yes
shift
fi
function inst1mod() {
local _ret=0 _mod="$1"
case $_mod in
=*)
if [ -f $KERNEL_MODS/modules.${_mod#=} ]; then
( [[ "$_mpargs" ]] && echo $_mpargs
cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
| instmods
else
( [[ "$_mpargs" ]] && echo $_mpargs
find "$KERNEL_MODS" -path "*/${_mod#=}/*" -printf '%f\n' ) \
| instmods
fi
;;
--*) _mpargs+=" $_mod" ;;
i2o_scsi) return ;; # Do not load this diagnostic-only module
*)
_mod=${_mod##*/}
# if we are already installed, skip this module and go on
# to the next one.
[[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then
dinfo "Omitting driver ${_mod##$KERNEL_MODS}"
return
fi
# If we are building a host-specific initramfs and this
# module is not already loaded, move on to the next one.
[[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc/modules \
&& ! echo $add_drivers | grep -qe "\<${_mod}\>" \
&& return
# 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.
local _moddirname=${KERNEL_MODS%%/lib/modules/*}
[[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
# ok, load the module, all its dependencies, and any firmware
# it may require
for_each_kmod_dep install_kmod_with_fw $_mod \
--set-version $KERNEL_VER ${_moddirname} $_mpargs
((_ret+=$?))
;;
esac
return $_ret
}
function instmods_1() {
local _mod _mpargs
if (($# == 0)); then # filenames from stdin
while read _mod; do
inst1mod "${_mod%.ko*}" || {
if [ "$_check" = "yes" ]; then
dfatal "Failed to install $_mod"
return 1
fi
}
done
fi
while (($# > 0)); do # filenames as arguments
inst1mod ${1%.ko*} || {
if [ "$_check" = "yes" ]; then
dfatal "Failed to install $1"
return 1
fi
}
shift
done
return 0
}
local _ret _filter_not_found='FATAL: Module .* not found.'
set -o pipefail
# 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" \
| while read line; do [[ "$line" =~ $_filter_not_found ]] && echo $line || echo $line >&2 ;done | derror
_ret=$?
set +o pipefail
return $_ret
}
# 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
inst_libdir_file() {
if [[ "$1" == "-n" ]]; then
local _pattern=$1
shift 2
for _dir in $libdirs; do
for _i in "$@"; do
for _f in "$_dir"/$_i; do
[[ "$_i" =~ $_pattern ]] || continue
[[ -e "$_i" ]] && dracut_install "$_i"
done
done
done
else
for _dir in $libdirs; do
for _i in "$@"; do
for _f in "$_dir"/$_i; do
[[ -e "$_f" ]] && dracut_install "$_f"
done
done
done
fi
}
check_nspawn() {
[[ -d /sys/fs/cgroup/systemd ]]
}
do_test() {
if [[ $UID != "0" ]]; then
echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
exit 0
fi
# Detect lib paths
[[ $libdir ]] || for libdir in /lib64 /lib; do
[[ -d $libdir ]] && libdirs+=" $libdir" && break
done
[[ $usrlibdir ]] || for usrlibdir in /usr/lib64 /usr/lib; do
[[ -d $usrlibdir ]] && libdirs+=" $usrlibdir" && break
done
import_testdir
import_initdir
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
}