Skip to content

eBPF-based security platform

contrib: run the libguestfs appliance directly with the VM's emulator

Puranjay Mohancf51557bc5f6
diff --git a/contrib/prepare-test-vm-runtime.sh b/contrib/prepare-test-vm-runtime.sh
index 9060937e..6d443f62 100755
--- a/contrib/prepare-test-vm-runtime.sh
+++ b/contrib/prepare-test-vm-runtime.sh
@@ -56,6 +56,17 @@ GUESTFS_RUNTIME_DIR="${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}/shirudo-guestfs-run}"
mkdir -p "$GUESTFS_CACHE_DIR"
mkdir -p "$GUESTFS_TMP_DIR"
mkdir -p "$GUESTFS_RUNTIME_DIR"
+
+# libguestfs defaults to its libvirt backend and to the emulator it was
+# built with, usually /usr/libexec/qemu-kvm, and a host with only
+# qemu-system-<arch> has neither. Run the appliance directly with the
+# emulator the VM uses, unless the caller chose otherwise.
+export LIBGUESTFS_BACKEND="${LIBGUESTFS_BACKEND:-direct}"
+if [[ -z "${LIBGUESTFS_HV:-}" ]]; then
+ LIBGUESTFS_HV="$(command -v "${TEST_VM_QEMU:-$TEST_VM_ARCH_QEMU}" || true)"
+ [[ -z "$LIBGUESTFS_HV" ]] || export LIBGUESTFS_HV
+fi
+
TMP_IMAGE="$CACHE_DIR/.${RUNTIME_KEY}.tmp.$$"
MARKER="${TMP_IMAGE}.marker"
cleanup() { rm -f -- "$TMP_IMAGE" "$MARKER"; }
diff --git a/testing/t0015-test-vm-cli.sh b/testing/t0015-test-vm-cli.sh
index c7a0249f..6bb9ea79 100755
--- a/testing/t0015-test-vm-cli.sh
+++ b/testing/t0015-test-vm-cli.sh
@@ -188,6 +188,37 @@ test_expect_success 'the bundle and runtime scripts print hints for the host pac
"$WORK/runtime-dnf.out"
'
+test_expect_success 'the runtime image is built with the direct libguestfs backend and the VM emulator' '
+ mkdir -p "$WORK/gf-bin" &&
+ printf "%s\n" "#!/bin/sh" "exit 0" >"$WORK/gf-bin/emulator" &&
+ printf "%s\n" "#!/bin/sh" \
+ "echo \"\$LIBGUESTFS_BACKEND \${LIBGUESTFS_HV-unset}\" >\"\$GF_OUT\"" \
+ >"$WORK/gf-bin/virt-customize" &&
+ printf "%s\n" "#!/bin/sh" \
+ "[ \"\$1\" = create ] || exit 0" \
+ "for a; do last=\$a; done; : >\"\$last\"" >"$WORK/gf-bin/qemu-img" &&
+ chmod +x "$WORK/gf-bin/emulator" "$WORK/gf-bin/virt-customize" \
+ "$WORK/gf-bin/qemu-img" &&
+ printf rootfs >"$WORK/gf-rootfs" &&
+ guestfs_env() {
+ rm -rf "$WORK/gf-cache" &&
+ env "$@" PATH="$WORK/gf-bin:$PATH" GF_OUT="$WORK/gf-out" \
+ TMPDIR="$WORK" LIBGUESTFS_CACHEDIR="$WORK/gf-cache" \
+ ../contrib/prepare-test-vm-runtime.sh \
+ --rootfs "$WORK/gf-rootfs" --cache-dir "$WORK/gf-cache/img" \
+ --package-file ../contrib/test-vm-runtime-packages \
+ --runtime-key gf --key-output "$WORK/gf-cache/key" \
+ >/dev/null 2>&1 &&
+ cat "$WORK/gf-out"
+ } &&
+ test "$(guestfs_env TEST_VM_QEMU="$WORK/gf-bin/emulator")" = \
+ "direct $WORK/gf-bin/emulator" &&
+ test "$(guestfs_env TEST_VM_QEMU="$WORK/gf-bin/emulator" \
+ LIBGUESTFS_BACKEND=libvirt LIBGUESTFS_HV=/chosen/qemu)" = \
+ "libvirt /chosen/qemu" &&
+ test "$(guestfs_env TEST_VM_QEMU="$WORK/gf-bin/none")" = "direct unset"
+'
+
test_expect_success 'the kernel package format follows the host package manager' '
printf "print:\n\t@echo \$(PACKAGE_FORMAT)\n" >"$WORK/print.mk" &&
format() {