test-functions revision e63b61be5350dbe92ea12e1eeb96dde251ed9292
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#!/bin/bash
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# ex: ts=8 sw=4 sts=4 et filetype=sh
32a4456cc252689f51f383d150d34ed636bfec4dMichal SchmidtPATH=/sbin:/bin:/usr/sbin:/usr/bin
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtexport PATH
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal SchmidtKERNEL_VER=${KERNEL_VER-$(uname -r)}
32a4456cc252689f51f383d150d34ed636bfec4dMichal SchmidtKERNEL_MODS="/lib/modules/$KERNEL_VER/"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtif ! ROOTLIBDIR=$(pkg-config --variable=systemdutildir systemd); then
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt echo "WARNING! Cannot determine rootlibdir from pkg-config, assuming /usr/lib/systemd" >&2
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt ROOTLIBDIR=/usr/lib/systemd
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtfi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal SchmidtBASICTOOLS="sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe"
32a4456cc252689f51f383d150d34ed636bfec4dMichal SchmidtDEBUGTOOLS="df free ls stty cat ps ln ip route dmesg dhclient mkdir cp ping dhclient strace less grep id tty touch du sort hostname"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtfunction find_qemu_bin() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # SUSE and Red Hat call the binary qemu-kvm
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # Debian and Gentoo call it kvm
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a kvm qemu-kvm 2>/dev/null | grep '^/' -m1)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [ "$ARCH" ] || ARCH=$(uname -m)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt case $ARCH in
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt x86_64)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # QEMU's own build system calls it qemu-system-x86_64
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-x86_64 2>/dev/null | grep '^/' -m1)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt ;;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt i*86)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # new i386 version of QEMU
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-i386 2>/dev/null | grep '^/' -m1)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # i386 version of QEMU
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu 2>/dev/null | grep '^/' -m1)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt ;;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt esac
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if [ ! -e "$QEMU_BIN" ]; then
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt echo "Could not find a suitable QEMU binary" >&2
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return 1
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtrun_qemu() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if [ -f /etc/machine-id ]; then
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt read MACHINE_ID < /etc/machine-id
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [ -z "$INITRD" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/initrd" ] \
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt && INITRD="/boot/$MACHINE_ID/$KERNEL_VER/initrd"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [ -z "$KERNEL_BIN" ] && [ -e "/boot/$MACHINE_ID/$KERNEL_VER/linux" ] \
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt && KERNEL_BIN="/boot/$MACHINE_ID/$KERNEL_VER/linux"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [ "$KERNEL_BIN" ] || KERNEL_BIN=/boot/vmlinuz-$KERNEL_VER
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [ "$INITRD" ] || INITRD=/boot/initramfs-${KERNEL_VER}.img
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [ "$QEMU_SMP" ] || QEMU_SMP=1
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt find_qemu_bin || return 1
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt KERNEL_APPEND="root=/dev/sda1 \
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtsystemd.log_level=debug \
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtraid=noautodetect \
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtloglevel=2 \
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtinit=$ROOTLIBDIR/systemd \
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtro \
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtconsole=ttyS0 \
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtselinux=0 \
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt$KERNEL_APPEND \
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt QEMU_OPTIONS="-machine accel=kvm:tcg \
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt-smp $QEMU_SMP \
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt-net none \
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt-m 512M \
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt-nographic \
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt-kernel $KERNEL_BIN \
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if [ "$INITRD" ]; then
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt QEMU_OPTIONS="$QEMU_OPTIONS -initrd $INITRD"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt ( set -x
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND" $TESTDIR/rootdisk.img ) || return 1
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtrun_nspawn() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt set -x
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt ../../systemd-nspawn --boot --directory=$TESTDIR/nspawn-root $ROOTLIBDIR/systemd $KERNEL_APPEND
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtsetup_basic_environment() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # create the basic filesystem layout
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt setup_basic_dirs
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt install_systemd
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt install_missing_libraries
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt install_config_files
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt create_rc_local
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt install_basic_tools
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt install_libnss
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt install_pam
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt install_dbus
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt install_fonts
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt install_keymaps
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt install_terminfo
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt install_execs
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt install_plymouth
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt install_debug_tools
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt install_ld_so_conf
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt strip_binaries
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt install_depmod_files
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt generate_module_dependencies
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # softlink mtab
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt ln -fs /proc/self/mounts $initdir/etc/mtab
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtinstall_dmevent() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt instmods dm_crypt =crypto
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt type -P dmeventd >/dev/null && dracut_install dmeventd
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst_libdir_file "libdevmapper-event.so*"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst_rules 10-dm.rules 13-dm-disk.rules 95-dm-notify.rules
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtinstall_systemd() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # install compiled files
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt (cd $TEST_BASE_DIR/..; set -x; make DESTDIR=$initdir install)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # remove unneeded documentation
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt rm -fr $initdir/usr/share/{man,doc}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # we strip binaries since debug symbols increase binaries size a lot
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # and it could fill the available space
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt strip_binaries
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtinstall_missing_libraries() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # install possible missing libraries
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt for i in $initdir/{sbin,bin}/* $initdir/lib/systemd/*; do
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst_libs $i
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt done
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtcreate_empty_image() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt rm -f "$TESTDIR/rootdisk.img"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # Create the blank file to use as a root filesystem
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt dd if=/dev/null of="$TESTDIR/rootdisk.img" bs=1M seek=300
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt LOOPDEV=$(losetup --show -P -f $TESTDIR/rootdisk.img)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [ -b "$LOOPDEV" ] || return 1
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt echo "LOOPDEV=$LOOPDEV" >> $STATEFILE
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt sfdisk "$LOOPDEV" <<EOF
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt,290M
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt,
32a4456cc252689f51f383d150d34ed636bfec4dMichal SchmidtEOF
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt mkfs.ext3 -L systemd "${LOOPDEV}p1"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtcheck_result_nspawn() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt ret=1
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt [[ -e $TESTDIR/nspawn-root/testok ]] && ret=0
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt [[ -f $TESTDIR/nspawn-root/failed ]] && cp -a $TESTDIR/nspawn-root/failed $TESTDIR
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt cp -a $TESTDIR/nspawn-root/var/log/journal $TESTDIR
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt ls -l $TESTDIR/journal/*/*.journal
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt test -s $TESTDIR/failed && ret=$(($ret+1))
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return $ret
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstrip_binaries() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt ddebug "Strip binaries"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | xargs strip --strip-unneeded | ddebug
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtcreate_rc_local() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt mkdir -p $initdir/etc/rc.d
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt cat >$initdir/etc/rc.d/rc.local <<EOF
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt#!/bin/bash
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtexit 0
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal SchmidtEOF
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt chmod 0755 $initdir/etc/rc.d/rc.local
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtinstall_execs() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt # install any Execs from the service files
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/lib/systemd/system/*.service \
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt | while read i; do
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt i=${i##Exec*=}; i=${i##-}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt inst $i
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt done
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtgenerate_module_dependencies() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if [[ -d $initdir/lib/modules/$KERNEL_VER ]] && \
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt ! depmod -a -b "$initdir" $KERNEL_VER; then
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt dfatal "\"depmod -a $KERNEL_VER\" failed."
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt exit 1
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt fi
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtinstall_depmod_files() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt inst /lib/modules/$KERNEL_VER/modules.order
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt inst /lib/modules/$KERNEL_VER/modules.builtin
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtinstall_plymouth() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt # install plymouth, if found... else remove plymouth service files
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt # dracut_install plymouth plymouthd
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt # else
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt rm -f $initdir/{usr/lib,etc}/systemd/system/plymouth* $initdir/{usr/lib,etc}/systemd/system/*/plymouth*
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt # fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtinstall_ld_so_conf() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt cp -a /etc/ld.so.conf* $initdir/etc
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt ldconfig -r "$initdir"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtinstall_config_files() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst /etc/sysconfig/init
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst /etc/passwd
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst /etc/shadow
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst /etc/group
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst /etc/shells
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst /etc/nsswitch.conf
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst /etc/pam.conf
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst /etc/securetty
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst /etc/os-release
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst /etc/localtime
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # we want an empty environment
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt > $initdir/etc/environment
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt > $initdir/etc/machine-id
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt # set the hostname
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt echo systemd-testsuite > $initdir/etc/hostname
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt # fstab
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt cat >$initdir/etc/fstab <<EOF
32a4456cc252689f51f383d150d34ed636bfec4dMichal SchmidtLABEL=systemd / ext3 rw 0 1
32a4456cc252689f51f383d150d34ed636bfec4dMichal SchmidtEOF
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtinstall_basic_tools() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ $BASICTOOLS ]] && dracut_install $BASICTOOLS
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt dracut_install -o sushell
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # in Debian ldconfig is just a shell script wrapper around ldconfig.real
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt dracut_install -o ldconfig.real
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtinstall_debug_tools() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt [[ $DEBUGTOOLS ]] && dracut_install $DEBUGTOOLS
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtinstall_libnss() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # install libnss_files for login
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt NSS_LIBS=$(LD_DEBUG=files getent passwd 2>&1 >/dev/null |sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}')
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt dracut_install $NSS_LIBS
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtinstall_dbus() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst $ROOTLIBDIR/system/dbus.socket
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt inst $ROOTLIBDIR/system/dbus.service
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt find \
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt /etc/dbus-1 /usr/share/dbus-1 -xtype f \
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt | while read file; do
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt inst $file
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt done
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtinstall_pam() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt find \
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt /etc/pam.d \
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt /etc/security \
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt /lib64/security \
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt /lib/security -xtype f \
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt | while read file; do
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt inst $file
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt done
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtinstall_keymaps() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt for i in \
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt /usr/lib/kbd/keymaps/include/* \
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt /usr/lib/kbd/keymaps/i386/include/* \
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt /usr/lib/kbd/keymaps/i386/qwerty/us.*; do
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt [[ -f $i ]] || continue
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt inst $i
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt done
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtinstall_fonts() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt for i in \
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt /usr/lib/kbd/consolefonts/eurlatgr* \
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt /usr/lib/kbd/consolefonts/latarcyrheb-sun16*; do
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt [[ -f $i ]] || continue
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt inst $i
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt done
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtinstall_terminfo() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt [ -f ${_terminfodir}/l/linux ] && break
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt done
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt dracut_install -o ${_terminfodir}/l/linux
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtsetup_testsuite() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt cp $TEST_BASE_DIR/testsuite.target $initdir/etc/systemd/system/
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt sed "s#@SYSTEMCTL@#$(type -P systemctl)#g" $TEST_BASE_DIR/end.service.in > $initdir/etc/systemd/system/end.service
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt mkdir -p $initdir/etc/systemd/system/testsuite.target.wants
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt ln -fs $TEST_BASE_DIR/testsuite.service $initdir/etc/systemd/system/testsuite.target.wants/testsuite.service
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt ln -fs $TEST_BASE_DIR/end.service $initdir/etc/systemd/system/testsuite.target.wants/end.service
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt # make the testsuite the default target
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt ln -fs testsuite.target $initdir/etc/systemd/system/default.target
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtsetup_nspawn_root() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt rm -fr $TESTDIR/nspawn-root
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt ddebug "cp -ar $initdir $TESTDIR/nspawn-root"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt cp -ar $initdir $TESTDIR/nspawn-root
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt # we don't mount in the nspawn root
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt rm -f $TESTDIR/nspawn-root/etc/fstab
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtsetup_basic_dirs() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt mkdir -p $initdir/run
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt mkdir -p $initdir/etc/systemd/system
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt mkdir -p $initdir/var/log/journal
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt 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
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if [ -L "/$d" ]; then
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt inst_symlink "/$d"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt else
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt inst_dir "/$d"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt fi
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt done
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt ln -sfn /run "$initdir/var/run"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt ln -sfn /run/lock "$initdir/var/lock"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtinst_libs() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt local _bin=$1
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt local _file _line
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ $_line = 'not a dynamic executable' ]] && break
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if [[ $_line =~ $_so_regex ]]; then
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt _file=${BASH_REMATCH[1]}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ -e ${initdir}/$_file ]] && continue
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt inst_library "$_file"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt continue
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if [[ $_line =~ not\ found ]]; then
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt dfatal "Missing a shared library required by $_bin."
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt dfatal "Run \"ldd $_bin\" to find out what it is."
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt dfatal "$_line"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt dfatal "dracut cannot create an initrd."
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt exit 1
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt done
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtimport_testdir() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt STATEFILE=".testdir"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ -e $STATEFILE ]] && . $STATEFILE
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt TESTDIR=$(mktemp --tmpdir=/var/tmp -d -t systemd-test.XXXXXX)
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt echo "TESTDIR=\"$TESTDIR\"" > $STATEFILE
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt export TESTDIR
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt fi
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtimport_initdir() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt initdir=$TESTDIR/root
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt export initdir
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt## @brief Converts numeric logging level to the first letter of level name.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt#
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# @param lvl Numeric logging level in range from 1 to 6.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# @retval 1 if @a lvl is out of range.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# @retval 0 if @a lvl is correct.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# @result Echoes first letter of level name.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt_lvl2char() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt case "$1" in
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt 1) echo F;;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt 2) echo E;;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt 3) echo W;;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt 4) echo I;;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt 5) echo D;;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt 6) echo T;;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt *) return 1;;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt esac
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt## @brief Internal helper function for _do_dlog()
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt#
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# @param lvl Numeric logging level.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# @param msg Message.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# @retval 0 It's always returned, even if logging failed.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt#
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# @note This function is not supposed to be called manually. Please use
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# dtrace(), ddebug(), or others instead which wrap this one.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt#
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# This function calls _do_dlog() either with parameter msg, or if
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# none is given, it will read standard input and will use every line as
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# a message.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# This enables:
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# dwarn "This is a warning"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# echo "This is a warning" | dwarn
32a4456cc252689f51f383d150d34ed636bfec4dMichal SchmidtLOG_LEVEL=4
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtdlog() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [ -z "$LOG_LEVEL" ] && return 0
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [ $1 -le $LOG_LEVEL ] || return 0
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt local lvl="$1"; shift
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt local lvlc=$(_lvl2char "$lvl") || return 0
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if [ $# -ge 1 ]; then
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt echo "$lvlc: $*"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt else
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt while read line; do
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt echo "$lvlc: " "$line"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt done
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt## @brief Logs message at TRACE level (6)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# @param msg Message.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# @retval 0 It's always returned, even if logging failed.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtdtrace() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt set +x
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt dlog 6 "$@"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [ -n "$debug" ] && set -x || :
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt## @brief Logs message at DEBUG level (5)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# @param msg Message.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# @retval 0 It's always returned, even if logging failed.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtddebug() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# set +x
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt dlog 5 "$@"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# [ -n "$debug" ] && set -x || :
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt## @brief Logs message at INFO level (4)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# @param msg Message.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# @retval 0 It's always returned, even if logging failed.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtdinfo() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt set +x
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt dlog 4 "$@"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [ -n "$debug" ] && set -x || :
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt## @brief Logs message at WARN level (3)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# @param msg Message.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# @retval 0 It's always returned, even if logging failed.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtdwarn() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt set +x
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt dlog 3 "$@"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [ -n "$debug" ] && set -x || :
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt## @brief Logs message at ERROR level (2)
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt#
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# @param msg Message.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# @retval 0 It's always returned, even if logging failed.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtderror() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# set +x
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt dlog 2 "$@"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# [ -n "$debug" ] && set -x || :
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt## @brief Logs message at FATAL level (1)
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt#
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# @param msg Message.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# @retval 0 It's always returned, even if logging failed.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtdfatal() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt set +x
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt dlog 1 "$@"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [ -n "$debug" ] && set -x || :
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# Generic substring function. If $2 is in $1, return 0.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstrstr() { [ "${1#*$2*}" != "$1" ]; }
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# normalize_path <path>
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# Prints the normalized path, where it removes any duplicated
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# and trailing slashes.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# Example:
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# $ normalize_path ///test/test//
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# /test/test
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtnormalize_path() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt shopt -q -s extglob
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt set -- "${1//+(\/)//}"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt shopt -q -u extglob
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt echo "${1%/}"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# convert_abs_rel <from> <to>
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# Prints the relative path, when creating a symlink to <to> from <from>.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# Example:
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# $ convert_abs_rel /usr/bin/test /bin/test-2
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# ../../bin/test-2
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtconvert_abs_rel() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt local __current __absolute __abssize __cursize __newpath
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt local -i __i __level
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # corner case #1 - self looping link
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # corner case #2 - own dir link
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt IFS="/" __current=($1)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt IFS="/" __absolute=($2)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt __abssize=${#__absolute[@]}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt __cursize=${#__current[@]}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt while [[ ${__absolute[__level]} == ${__current[__level]} ]]
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt do
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt (( __level++ ))
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if (( __level > __abssize || __level > __cursize ))
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt then
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt break
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt done
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt for ((__i = __level; __i < __cursize-1; __i++))
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt do
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if ((__i > __level))
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt then
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt __newpath=$__newpath"/"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt __newpath=$__newpath".."
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt done
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt for ((__i = __level; __i < __abssize; __i++))
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt do
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if [[ -n $__newpath ]]
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt then
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt __newpath=$__newpath"/"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt fi
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt __newpath=$__newpath${__absolute[__i]}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt done
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt echo "$__newpath"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# Install a directory, keeping symlinks as on the original system.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# will create ${initdir}/lib64, ${initdir}/lib64/file,
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# and a symlink ${initdir}/lib -> lib64.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtinst_dir() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ -e ${initdir}/"$1" ]] && return 0 # already there
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt local _dir="$1" _part="${1%/*}" _file
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt _dir="$_part $_dir"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt _part=${_part%/*}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt done
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # iterate over parent directories
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt for _file in $_dir; do
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ -e "${initdir}/$_file" ]] && continue
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if [[ -L $_file ]]; then
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst_symlink "$_file"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt else
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # create directory
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt mkdir -m 0755 -p "${initdir}/$_file" || return 1
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt chmod u+w "${initdir}/$_file"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt done
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# $1 = file to copy to ramdisk
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# $2 (optional) Name for the file on the ramdisk
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# Location of the image dir is assumed to be $initdir
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# We never overwrite the target if it exists.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtinst_simple() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ -f "$1" ]] || return 1
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt strstr "$1" "/" || return 1
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt local _src=$1 target="${2:-$1}"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if ! [[ -d ${initdir}/$target ]]; then
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ -e ${initdir}/$target ]] && return 0
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ -L ${initdir}/$target ]] && return 0
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fi
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt # install checksum files also
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt ddebug "Installing $_src"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt cp --sparse=always -pfL "$_src" "${initdir}/$target"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# find symlinks linked to given library file
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# $1 = library file
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# Function searches for symlinks by stripping version numbers appended to
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# library filename, checks if it points to the same target and finally
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# prints the list of symlinks to stdout.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt#
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# Example:
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# rev_lib_symlinks libfoo.so.8.1
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# output: libfoo.so.8 libfoo.so
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# (Only if libfoo.so.8 and libfoo.so exists on host system.)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtrev_lib_symlinks() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ ! $1 ]] && return 0
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt local fn="$1" orig="$(readlink -f "$1")" links=''
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt [[ ${fn} =~ .*\.so\..* ]] || return 1
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt until [[ ${fn##*.} == so ]]; do
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fn="${fn%.*}"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt done
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt echo "${links}"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# Same as above, but specialized to handle dynamic libraries.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# It handles making symlinks according to how the original library
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# is referenced.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtinst_library() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt strstr "$1" "/" || return 1
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt [[ -e $initdir/$_dest ]] && return 0
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if [[ -L $_src ]]; then
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt # install checksum files also
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt fi
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt _reallib=$(readlink -f "$_src")
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt inst_simple "$_reallib" "$_reallib"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt inst_dir "${_dest%/*}"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt ln -sfn $(convert_abs_rel "${_dest}" "${_reallib}") "${initdir}/${_dest}"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt else
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt inst_simple "$_src" "$_dest"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt fi
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt # Create additional symlinks. See rev_symlinks description.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt [[ ! -e $initdir/$_symlink ]] && {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt ddebug "Creating extra symlink: $_symlink"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt inst_symlink $_symlink
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt }
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt done
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# find a binary. If we were not passed the full path directly,
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# search in the usual places to find the binary.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtfind_binary() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if [[ -z ${1##/*} ]]; then
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt echo $1
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt return 0
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt type -P $1
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# Same as above, but specialized to install binary executables.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# Install binary executable, and all shared library dependencies, if any.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtinst_binary() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt local _bin _target
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt _bin=$(find_binary "$1") || return 1
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt _target=${2:-$_bin}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ -e $initdir/$_target ]] && return 0
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ -L $_bin ]] && inst_symlink $_bin $_target && return 0
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt local _file _line
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt # I love bash!
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt [[ $_line = 'not a dynamic executable' ]] && break
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if [[ $_line =~ $_so_regex ]]; then
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt _file=${BASH_REMATCH[1]}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt [[ -e ${initdir}/$_file ]] && continue
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt inst_library "$_file"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt continue
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if [[ $_line =~ not\ found ]]; then
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt dfatal "Missing a shared library required by $_bin."
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt dfatal "Run \"ldd $_bin\" to find out what it is."
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt dfatal "$_line"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt dfatal "dracut cannot create an initrd."
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt exit 1
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt fi
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt done
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst_simple "$_bin" "$_target"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# same as above, except for shell scripts.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# If your shell script does not start with shebang, it is not a shell script.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtinst_script() {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt local _bin
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt _bin=$(find_binary "$1") || return 1
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt shift
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt local _line _shebang_regex
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt read -r -n 80 _line <"$_bin"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt # If debug is set, clean unprintable chars to prevent messing up the term
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]')
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt _shebang_regex='(#! *)(/[^ ]+).*'
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt [[ $_line =~ $_shebang_regex ]] || return 1
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# same as above, but specialized for symlinks
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtinst_symlink() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt local _src=$1 _target=${2:-$1} _realsrc
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt strstr "$1" "/" || return 1
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ -L $1 ]] || return 1
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ -L $initdir/$_target ]] && return 0
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt _realsrc=$(readlink -f "$_src")
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if ! [[ -e $initdir/$_realsrc ]]; then
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if [[ -d $_realsrc ]]; then
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst_dir "$_realsrc"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt else
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst "$_realsrc"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt ln -sfn $(convert_abs_rel "${_target}" "${_realsrc}") "$initdir/$_target"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# attempt to install any programs specified in a udev rule
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtinst_rule_programs() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt local _prog _bin
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if [ -x /lib/udev/$_prog ]; then
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt _bin=/lib/udev/$_prog
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt else
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt _bin=$(find_binary "$_prog") || {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt continue;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt }
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt dracut_install "$_bin"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt done
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# udev rules always get installed in the same place, so
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# create a function to install them to make life simpler.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtinst_rules() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt local _target=/etc/udev/rules.d _rule _found
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst_dir "/lib/udev/rules.d"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst_dir "$_target"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt for _rule in "$@"; do
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if [ "${rule#/}" = "$rule" ]; then
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt for r in /lib/udev/rules.d /etc/udev/rules.d; do
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if [[ -f $r/$_rule ]]; then
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt _found="$r/$_rule"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst_simple "$_found"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst_rule_programs "$_found"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt done
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt for r in '' ./ $dracutbasedir/rules.d/; do
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if [[ -f ${r}$_rule ]]; then
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt _found="${r}$_rule"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst_simple "$_found" "$_target/${_found##*/}"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst_rule_programs "$_found"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt done
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt done
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt# general purpose installation function
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt# Same args as above.
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidtinst() {
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt local _x
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt case $# in
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt 1) ;;
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt 2) [[ ! $initdir && -d $2 ]] && export initdir=$2
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt [[ $initdir = $2 ]] && set $1;;
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt 3) [[ -z $initdir ]] && export initdir=$2
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt set $1 $3;;
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt *) dfatal "inst only takes 1 or 2 or 3 arguments"
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt exit 1;;
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt esac
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt for _x in inst_symlink inst_script inst_binary inst_simple; do
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt $_x "$@" && return 0
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt done
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return 1
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# install any of listed files
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt#
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# If first argument is '-d' and second some destination path, first accessible
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# source is installed into this path, otherwise it will installed in the same
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# path as source. If none of listed files was installed, function return 1.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# On first successful installation it returns with 0 status.
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt#
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# Example:
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt# inst_any -d /bin/foo /bin/bar /bin/baz
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt# initramfs.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtinst_any() {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt local to f
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt [[ $1 = '-d' ]] && to="$2" && shift 2
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt for f in "$@"; do
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if [[ -e $f ]]; then
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt [[ $to ]] && inst "$f" "$to" && return 0
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt inst "$f" && return 0
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt fi
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt done
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt return 1
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
# dracut_install [-o ] <file> [<file> ... ]
# Install <file> to the initramfs image
# -o optionally install the <file> and don't fail, if it is not there
dracut_install() {
local _optional=no
if [[ $1 = '-o' ]]; then
_optional=yes
shift
fi
while (($# > 0)); do
if ! inst "$1" ; then
if [[ $_optional = yes ]]; then
dinfo "Skipping program $1 as it cannot be found and is" \
"flagged to be optional"
else
dfatal "Failed to install $1"
exit 1
fi
fi
shift
done
}
# Install a single kernel module along with any firmware it may require.
# $1 = full path to kernel module to install
install_kmod_with_fw() {
# no need to go further if the module is already installed
[[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
&& return 0
[[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
if [[ $omit_drivers ]]; then
local _kmod=${1##*/}
_kmod=${_kmod%.ko}
_kmod=${_kmod/-/_}
if [[ "$_kmod" =~ $omit_drivers ]]; then
dinfo "Omitting driver $_kmod"
return 1
fi
if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~ $omit_drivers ]]; then
dinfo "Omitting driver $_kmod"
return 1
fi
fi
[ -d "$initdir/.kernelmodseen" ] && \
> "$initdir/.kernelmodseen/${1##*/}"
inst_simple "$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
|| return $?
local _modname=${1##*/} _fwdir _found _fw
_modname=${_modname%.ko*}
for _fw in $(modinfo -k $KERNEL_VER -F firmware $1 2>/dev/null); do
_found=''
for _fwdir in $fw_dir; do
if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
_found=yes
fi
done
if [[ $_found != yes ]]; then
if ! grep -qe "\<${_modname//-/_}\>" /proc/modules; then
dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
"\"${_modname}.ko\""
else
dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
"\"${_modname}.ko\""
fi
fi
done
return 0
}
# Do something with all the dependencies of a kernel module.
# Note that kernel modules depend on themselves using the technique we use
# $1 = function to call for each dependency we find
# It will be passed the full path to the found kernel module
# $2 = module to get dependencies for
# rest of args = arguments to modprobe
# _fderr specifies FD passed from surrounding scope
for_each_kmod_dep() {
local _func=$1 _kmod=$2 _cmd _modpath _options _found=0
shift 2
modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
while read _cmd _modpath _options; do
[[ $_cmd = insmod ]] || continue
$_func ${_modpath} || exit $?
_found=1
done
[[ $_found -eq 0 ]] && exit 1
exit 0
)
}
# filter kernel modules to install certain modules that meet specific
# requirements.
# $1 = search only in subdirectory of /kernel/$1
# $2 = function to call with module name to filter.
# This function will be passed the full path to the module to test.
# The behavior of this function can vary depending on whether $hostonly is set.
# If it is, we will only look at modules that are already in memory.
# If it is not, we will look at all kernel modules
# This function returns the full filenames of modules that match $1
filter_kernel_modules_by_path () (
local _modname _filtercmd
if ! [[ $hostonly ]]; then
_filtercmd='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
_filtercmd+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
_filtercmd+=' -o -name "*.ko.xz"'
_filtercmd+=' 2>/dev/null'
else
_filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
_filtercmd+='-k $KERNEL_VER 2>/dev/null'
fi
for _modname in $(eval $_filtercmd); do
case $_modname in
*.ko) "$2" "$_modname" && echo "$_modname";;
*.ko.gz) gzip -dc "$_modname" > $initdir/$$.ko
$2 $initdir/$$.ko && echo "$_modname"
rm -f $initdir/$$.ko
;;
*.ko.xz) xz -dc "$_modname" > $initdir/$$.ko
$2 $initdir/$$.ko && echo "$_modname"
rm -f $initdir/$$.ko
;;
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
}