eBPF-based security platform
contrib: support Fedora and RHEL hosts for the test VM
Puranjay Mohane55822bdfa8a
diff --git a/contrib/build-kernel-package.sh b/contrib/build-kernel-package.shindex a798ecba..4f987334 100755--- a/contrib/build-kernel-package.sh+++ b/contrib/build-kernel-package.sh@@ -51,7 +51,7 @@ if [[ -z "${KERNEL_LOCALVERSION:-}" ]]; thenKERNEL_LOCALVERSION="-shirudo-${KERNEL_TAG#v}"fifi-PACKAGE_FORMAT="${PACKAGE_FORMAT:-deb}"+PACKAGE_FORMAT="${PACKAGE_FORMAT:-$TEST_VM_HOST_PKG_FORMAT}"case "$PACKAGE_FORMAT" indeb)PACKAGE_SUFFIX="debs"diff --git a/contrib/build-test-vm-bundle.sh b/contrib/build-test-vm-bundle.shindex 40893ca2..81b2794a 100755--- a/contrib/build-test-vm-bundle.sh+++ b/contrib/build-test-vm-bundle.sh@@ -46,11 +46,12 @@ validate_package_cert()die "TEST_VM_PACKAGE_CERT is not a readable, non-empty regular file: $PACKAGE_CERT"}+# require_command COMMAND DEB-PACKAGE [RPM-PACKAGE]require_command(){- local command_name="$1" package_name="$2"+ local command_name="$1" deb_package="$2" rpm_package="${3:-$2}"command -v "$command_name" >/dev/null 2>&1 ||- die "missing host command: $command_name (install with: sudo apt-get install -y $package_name)"+ die "missing host command: $command_name (install with: $(test_vm_install_cmd "$(test_vm_pkg "$deb_package" "$rpm_package")"))"}check_libguestfs_host()@@ -82,6 +83,8 @@ prepare_ubuntu_libguestfs_kernel(){local cache_dir index metadata package version kernel_version filename deb tmplocal module_package module_filename kernel_info module_info module_root++ require_command dpkg-deb dpkgcache_dir="$LIBGUESTFS_KERNEL_CACHE/$UBUNTU_CODENAME"index="$cache_dir/Packages.gz"metadata="$cache_dir/kernel.info"@@ -187,13 +190,15 @@ prepare_ubuntu_libguestfs_kernel()return 0}+# Each entry is command:Debian package:RPM package.for dependency in \- "flock:util-linux" "curl:curl" "sha256sum:coreutils" "awk:gawk" \- "qemu-img:qemu-utils" "virt-customize:libguestfs-tools" \- "dpkg-deb:dpkg" "openssl:openssl" \- "gzip:gzip" "depmod:kmod"; do- IFS=: read -r command_name package_name <<<"$dependency"- require_command "$command_name" "$package_name"+ "flock:util-linux:util-linux" "curl:curl:curl" \+ "sha256sum:coreutils:coreutils" "awk:gawk:gawk" \+ "qemu-img:qemu-utils:qemu-img" \+ "virt-customize:libguestfs-tools:guestfs-tools" \+ "openssl:openssl:openssl" "gzip:gzip:gzip" "depmod:kmod:kmod"; do+ IFS=: read -r command_name deb_package rpm_package <<<"$dependency"+ require_command "$command_name" "$deb_package" "$rpm_package"donecheck_libguestfs_host@@ -267,7 +272,7 @@ bundle_is_current()}command -v flock >/dev/null 2>&1 ||- die "flock is required; install it with: sudo apt-get install -y util-linux"+ die "flock is required; install it with: $(test_vm_install_cmd util-linux)"validate_package_certdiff --git a/contrib/prepare-test-vm-runtime.sh b/contrib/prepare-test-vm-runtime.shindex 1ce81f91..9060937e 100755--- a/contrib/prepare-test-vm-runtime.sh+++ b/contrib/prepare-test-vm-runtime.sh@@ -6,6 +6,9 @@set -euo pipefail+# shellcheck source=contrib/test-vm-arch.sh+. "$(dirname "${BASH_SOURCE[0]}")/test-vm-arch.sh"+ROOTFS=CACHE_DIR=PACKAGE_FILE=@@ -35,9 +38,9 @@ donedie "runtime disk size must be a positive QEMU size such as 4G or 512M"command -v qemu-img >/dev/null 2>&1 ||- die "qemu-img is required; install it with: sudo apt-get install -y qemu-utils"+ die "qemu-img is required; install it with: $(test_vm_install_cmd "$(test_vm_pkg qemu-utils qemu-img)")"command -v virt-customize >/dev/null 2>&1 ||- die "virt-customize is required; install it with: sudo apt-get install -y libguestfs-tools"+ die "virt-customize is required; install it with: $(test_vm_install_cmd "$(test_vm_pkg libguestfs-tools guestfs-tools)")"IMAGE="$CACHE_DIR/$RUNTIME_KEY.qcow2"if [[ -s "$IMAGE" ]]; thendiff --git a/contrib/test-vm-arch.sh b/contrib/test-vm-arch.shindex d1a691ad..c5b89cdc 100644--- a/contrib/test-vm-arch.sh+++ b/contrib/test-vm-arch.sh@@ -2,11 +2,12 @@# shellcheck shell=sh disable=SC2034# Copyright (c) 2026 Authors of shirudo#-# Host architecture names for the test VM tooling. Sourced by the contrib-# scripts and evaluated by testing/Makefile, so POSIX sh. TEST_VM_ARCH-# overrides the detected architecture. Any other than x86-64 and arm64-# gets the x86-64 names, so that host-side targets still evaluate, and-# TEST_VM_ARCH_SUPPORTED=0, which refuses a kernel build and a boot.+# Host architecture and package manager for the test VM tooling. Sourced+# by the contrib scripts and evaluated by testing/Makefile, so POSIX sh.+# TEST_VM_ARCH and TEST_VM_HOST_PKG override the detection. An+# architecture other than x86-64 and arm64 gets the x86-64 names, so that+# host-side targets still evaluate, and TEST_VM_ARCH_SUPPORTED=0, which+# refuses a kernel build and a boot.: "${TEST_VM_ARCH:=$(uname -m)}"@@ -20,6 +21,7 @@ esacif [ "$TEST_VM_ARCH" = aarch64 ]; thenTEST_VM_ARCH_QEMU=qemu-system-aarch64TEST_VM_ARCH_QEMU_DEB=qemu-system-arm+ TEST_VM_ARCH_QEMU_RPM=qemu-system-aarch64-coreTEST_VM_ARCH_CONSOLE=ttyAMA0TEST_VM_ARCH_KIMAGE=ImageTEST_VM_ARCH_KIMAGE_SRC=arch/arm64/boot/Image@@ -28,9 +30,49 @@ if [ "$TEST_VM_ARCH" = aarch64 ]; thenelseTEST_VM_ARCH_QEMU=qemu-system-x86_64TEST_VM_ARCH_QEMU_DEB=qemu-system-x86+ TEST_VM_ARCH_QEMU_RPM=qemu-system-x86-coreTEST_VM_ARCH_CONSOLE=ttyS0TEST_VM_ARCH_KIMAGE=bzImageTEST_VM_ARCH_KIMAGE_SRC=arch/x86/boot/bzImageTEST_VM_ARCH_DEB=amd64TEST_VM_ARCH_MIRROR=https://archive.ubuntu.com/ubuntufi++# dnf on Fedora, RHEL and their derivatives as os-release names them, apt+# anywhere else.+if [ -z "${TEST_VM_HOST_PKG:-}" ]; then+ TEST_VM_HOST_PKG=apt+ if [ -r /etc/os-release ]; then+ case " $(. /etc/os-release; echo "${ID:-} ${ID_LIKE:-}") " in+ *" fedora "*|*" rhel "*|*" centos "*) TEST_VM_HOST_PKG=dnf ;;+ esac+ fi+fi+if [ "$TEST_VM_HOST_PKG" = dnf ]; then+ TEST_VM_HOST_PKG_FORMAT=rpm+else+ TEST_VM_HOST_PKG=apt+ TEST_VM_HOST_PKG_FORMAT=deb+fi++# test_vm_pkg DEB RPM: the name of the package on this host.+test_vm_pkg()+{+ if [ "$TEST_VM_HOST_PKG" = dnf ]; then+ printf '%s\n' "$2"+ else+ printf '%s\n' "$1"+ fi+}++# test_vm_install_cmd PACKAGE...: the command that installs them here.+test_vm_install_cmd()+{+ if [ "$TEST_VM_HOST_PKG" = dnf ]; then+ printf 'sudo dnf install -y'+ else+ printf 'sudo apt-get install -y --no-install-recommends'+ fi+ printf ' %s' "$@"+ printf '\n'+}diff --git a/contrib/test-vm.sh b/contrib/test-vm.shindex f7bdc11a..b608fef8 100755--- a/contrib/test-vm.sh+++ b/contrib/test-vm.sh@@ -35,7 +35,8 @@ fiREPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"# shellcheck source=contrib/test-vm-arch.sh. "${REPO_ROOT}/contrib/test-vm-arch.sh"-QEMU_BIN="$TEST_VM_ARCH_QEMU"+# TEST_VM_QEMU names another emulator, such as RHEL's /usr/libexec/qemu-kvm.+QEMU_BIN="${TEST_VM_QEMU:-$TEST_VM_ARCH_QEMU}"VM_ROOT_INPUT="${TEST_VM_WORK_DIR:-${REPO_ROOT}/build/test-vm}"if [[ "$VM_ROOT_INPUT" == /* ]]; thenVM_ROOT="$VM_ROOT_INPUT"@@ -112,18 +113,19 @@ HOST_MEMORY_REPORT=""FAILURE_LOG_DIR=""FINAL_STATUS=1LEAVE_RUNNING=0+# Each entry is command:Debian package:RPM package.HOST_DEPENDENCIES=(- "curl:curl"- "sha256sum:coreutils"- "awk:mawk"- "${QEMU_BIN}:${TEST_VM_ARCH_QEMU_DEB}"- "qemu-img:qemu-utils"- "swtpm:swtpm"- "cloud-localds:cloud-image-utils"- "timeout:coreutils"- "flock:util-linux"- "ssh-keygen:openssh-client"- "ss:iproute2"+ "curl:curl:curl"+ "sha256sum:coreutils:coreutils"+ "awk:mawk:gawk"+ "${QEMU_BIN}:${TEST_VM_ARCH_QEMU_DEB}:${TEST_VM_ARCH_QEMU_RPM}"+ "qemu-img:qemu-utils:qemu-img"+ "swtpm:swtpm:swtpm"+ "cloud-localds:cloud-image-utils:cloud-utils"+ "timeout:coreutils:coreutils"+ "flock:util-linux:util-linux"+ "ssh-keygen:openssh-client:openssh-clients"+ "ss:iproute2:iproute")SSH_PORT_MIN=22000@@ -285,23 +287,23 @@ validate_bpm_oci()print_dependency_install_command(){- printf ' sudo apt-get install -y --no-install-recommends'- printf ' %q' "$@"- printf '\n'+ printf ' %s\n' "$(test_vm_install_cmd "$@")"}check_host_dependencies(){local command_name+ local deb_packagelocal packagelocal requirement+ local rpm_packagelocal -a missing_commands=()local -a missing_packages=()local -A package_seen=()for requirement in "${HOST_DEPENDENCIES[@]}"; do- command_name="${requirement%%:*}"- package="${requirement#*:}"+ IFS=: read -r command_name deb_package rpm_package <<<"$requirement"+ package="$(test_vm_pkg "$deb_package" "$rpm_package")"if ! command -v "$command_name" >/dev/null 2>&1; thenmissing_commands+=("$command_name")if [[ -z "${package_seen[$package]+present}" ]]; then@@ -1170,7 +1172,7 @@ validate_package_certvalidate_bpm_tree_configvalidate_bpm_ociif [[ "$VM_INITRD" == 1 ]]; then- HOST_DEPENDENCIES+=("dracut:dracut-core" "lsinitrd:dracut-core" "file:file")+ HOST_DEPENDENCIES+=("dracut:dracut-core:dracut" "lsinitrd:dracut-core:dracut" "file:file:file")[[ "$EUID" == 0 ]] || die "TEST_VM_INITRD=1 builds the initrd with dracut, which needs root"ficheck_host_dependenciesdiff --git a/testing/Makefile b/testing/Makefileindex 2ce5dafc..8c2529be 100644--- a/testing/Makefile+++ b/testing/Makefile@@ -25,7 +25,7 @@ test-vm-arch = $(shell TEST_VM_ARCH='$(TEST_VM_ARCH)'; \KERNEL_PATCHES ?= $(if $(wildcard $(ROOT)/kernel/*.patch),$(ROOT)/kernel/,)override KERNEL_PATCHES := $(if $(strip $(KERNEL_PATCHES)),$(call root-path,$(KERNEL_PATCHES)),)-PACKAGE_FORMAT ?= deb+PACKAGE_FORMAT ?= $(call test-vm-arch,TEST_VM_HOST_PKG_FORMAT)TEST_VM_WORK_DIR ?= build/test-vmoverride TEST_VM_WORK_DIR := $(call root-path,$(TEST_VM_WORK_DIR))diff --git a/testing/t0015-test-vm-cli.sh b/testing/t0015-test-vm-cli.shindex 08f903ac..5888cf5a 100755--- a/testing/t0015-test-vm-cli.sh+++ b/testing/t0015-test-vm-cli.sh@@ -44,7 +44,7 @@ test_expect_success 'test-vm prints an install command for missing dependencies'ln -s /bin/true "$WORK/fake-bin/$command_name" || return 1done &&printf kernel >"$WORK/bzImage" &&- ! PATH="$WORK/fake-bin" \+ ! PATH="$WORK/fake-bin" TEST_VM_ARCH=x86_64 TEST_VM_HOST_PKG=apt \TEST_VM_WORK_DIR="$WORK/missing-dependency" \TEST_VM_KERNEL="$WORK/bzImage" "$RUNNER" \>"$WORK/missing-dependency.out" 2>&1 &&@@ -64,7 +64,7 @@ test_expect_success 'test-vm requires the QEMU emulator of the host architectureln -s /bin/true "$WORK/arm-bin/$command_name" || return 1done &&printf kernel >"$WORK/Image" &&- ! PATH="$WORK/arm-bin" TEST_VM_ARCH=aarch64 \+ ! PATH="$WORK/arm-bin" TEST_VM_ARCH=aarch64 TEST_VM_HOST_PKG=apt \TEST_VM_WORK_DIR="$WORK/missing-arm-qemu" \TEST_VM_KERNEL="$WORK/Image" "$RUNNER" \>"$WORK/missing-arm-qemu.out" 2>&1 &&@@ -73,6 +73,98 @@ test_expect_success 'test-vm requires the QEMU emulator of the host architecturegrep -q "qemu-system-arm" "$WORK/missing-arm-qemu.out"'+test_expect_success 'test-vm prints dnf package names on a Fedora or RHEL host' '+ ! PATH="$WORK/fake-bin" TEST_VM_ARCH=x86_64 TEST_VM_HOST_PKG=dnf \+ TEST_VM_WORK_DIR="$WORK/missing-dependency-dnf" \+ TEST_VM_KERNEL="$WORK/bzImage" "$RUNNER" \+ >"$WORK/missing-dependency-dnf.out" 2>&1 &&+ grep -q "sudo dnf install -y cloud-utils" \+ "$WORK/missing-dependency-dnf.out" &&+ grep -q "openssh-clients" "$WORK/missing-dependency-dnf.out" &&+ ! grep -q "apt-get" "$WORK/missing-dependency-dnf.out" &&+ ! PATH="$WORK/arm-bin" TEST_VM_ARCH=aarch64 TEST_VM_HOST_PKG=dnf \+ TEST_VM_WORK_DIR="$WORK/missing-arm-qemu-dnf" \+ TEST_VM_KERNEL="$WORK/Image" "$RUNNER" \+ >"$WORK/missing-arm-qemu-dnf.out" 2>&1 &&+ grep -q "qemu-system-aarch64-core" "$WORK/missing-arm-qemu-dnf.out"+'++test_expect_success 'TEST_VM_QEMU names another emulator' '+ mkdir "$WORK/kvm-bin" &&+ ln -s /bin/bash "$WORK/kvm-bin/bash" &&+ ln -s /usr/bin/dirname "$WORK/kvm-bin/dirname" &&+ for command_name in curl sha256sum awk qemu-kvm qemu-img swtpm \+ cloud-localds timeout tee flock ssh-keygen ss+ do+ ln -s /bin/true "$WORK/kvm-bin/$command_name" || return 1+ done &&+ ! PATH="$WORK/kvm-bin" TEST_VM_ARCH=x86_64 \+ TEST_VM_WORK_DIR="$WORK/no-qemu" \+ TEST_VM_KERNEL="$WORK/bzImage" "$RUNNER" \+ >"$WORK/no-qemu.out" 2>&1 &&+ grep -q "missing host commands: qemu-system-x86_64" "$WORK/no-qemu.out" &&+ ! PATH="$WORK/kvm-bin" TEST_VM_ARCH=x86_64 TEST_VM_QEMU=qemu-kvm \+ TEST_VM_WORK_DIR="$WORK/qemu-kvm" \+ TEST_VM_KERNEL="$WORK/bzImage" "$RUNNER" \+ >"$WORK/qemu-kvm.out" 2>&1 &&+ ! grep -q "missing host commands" "$WORK/qemu-kvm.out" &&+ grep -q "host VM bundle is missing" "$WORK/qemu-kvm.out"+'++test_expect_success 'the host package manager is detected from os-release' '+ sed "s|/etc/os-release|$WORK/os-release|g" ../contrib/test-vm-arch.sh \+ >"$WORK/arch-os.sh" &&+ host_pkg() {+ printf "%s\n" "$@" >"$WORK/os-release" &&+ (unset TEST_VM_HOST_PKG; . "$WORK/arch-os.sh"; echo "$TEST_VM_HOST_PKG")+ } &&+ test "$(host_pkg ID=fedora)" = dnf &&+ test "$(host_pkg ID=centos "ID_LIKE=\"rhel fedora\"")" = dnf &&+ test "$(host_pkg ID=rocky "ID_LIKE=\"rhel centos fedora\"")" = dnf &&+ test "$(host_pkg ID=ubuntu ID_LIKE=debian)" = apt &&+ test "$(host_pkg ID=debian)" = apt &&+ (+ TEST_VM_HOST_PKG=dnf &&+ . ../contrib/test-vm-arch.sh &&+ test "$TEST_VM_HOST_PKG_FORMAT" = rpm &&+ test "$(test_vm_install_cmd a b)" = "sudo dnf install -y a b"+ )+'++test_expect_success 'the bundle and runtime scripts print hints for the host package manager' '+ mkdir "$WORK/bundle-bin" &&+ ln -s /usr/bin/dirname "$WORK/bundle-bin/dirname" &&+ ln -s /usr/bin/uname "$WORK/bundle-bin/uname" &&+ for command_name in flock curl sha256sum awk qemu-img+ do+ ln -s /bin/true "$WORK/bundle-bin/$command_name" || return 1+ done &&+ ! PATH="$WORK/bundle-bin" TEST_VM_HOST_PKG=dnf \+ TEST_VM_WORK_DIR="$WORK/bundle-dnf" /bin/bash \+ ../contrib/build-test-vm-bundle.sh >"$WORK/bundle-dnf.out" 2>&1 &&+ grep -q "virt-customize (install with: sudo dnf install -y guestfs-tools)" \+ "$WORK/bundle-dnf.out" &&+ printf rootfs >"$WORK/hint-rootfs" &&+ ! PATH="$WORK/bundle-bin" TEST_VM_HOST_PKG=dnf /bin/bash \+ ../contrib/prepare-test-vm-runtime.sh --rootfs "$WORK/hint-rootfs" \+ --cache-dir "$WORK/hint-cache" \+ --package-file ../contrib/test-vm-runtime-packages \+ --runtime-key hint --key-output "$WORK/hint-key" \+ >"$WORK/runtime-dnf.out" 2>&1 &&+ grep -q "virt-customize is required; install it with: sudo dnf install -y guestfs-tools" \+ "$WORK/runtime-dnf.out"+'++test_expect_success 'the kernel package format follows the host package manager' '+ printf "print:\n\t@echo \$(PACKAGE_FORMAT)\n" >"$WORK/print.mk" &&+ format() {+ make -s -C .. -f testing/Makefile -f "$WORK/print.mk" print "$@"+ } &&+ test "$(TEST_VM_HOST_PKG=dnf format)" = rpm &&+ test "$(TEST_VM_HOST_PKG=apt format)" = deb &&+ test "$(TEST_VM_HOST_PKG=dnf format PACKAGE_FORMAT=deb)" = deb+'+test_expect_success 'test-vm boots arm64 on the virt machine with PCI disks' 'grep -q "virt,gic-version=host" "$RUNNER" &&grep -q "virtio-blk-pci,drive=rootdisk" "$RUNNER" &&