From d392e9c6076481b45d6ef1710894e16e6da8595a Mon Sep 17 00:00:00 2001 From: Douglas Schilling Landgraf Date: Sat, 28 Dec 2024 16:32:41 -0500 Subject: [PATCH] add libkrun support initial code to add libkrun support Signed-off-by: Douglas Schilling Landgraf --- Makefile | 1 + rpm/qm.spec | 2 + tools/qm-change-podman-runtime | 79 ++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100755 tools/qm-change-podman-runtime diff --git a/Makefile b/Makefile index f509c454..1a008684 100644 --- a/Makefile +++ b/Makefile @@ -142,3 +142,4 @@ install: man all ## - Install QM files (including selinux) install -D -m 644 containers.conf ${DESTDIR}${DATADIR}/qm/containers.conf install -D -m 644 qm.container ${DESTDIR}${DATADIR}/containers/systemd/qm.container install -D -m 755 tools/qm-is-ostree ${DESTDIR}${DATADIR}/qm/qm-is-ostree + install -D -m 755 tools/qm-change-podman-runtime ${DESTDIR}${DATADIR}/qm/qm-change-podman-runtime diff --git a/rpm/qm.spec b/rpm/qm.spec index ddbb646f..cd43cc66 100644 --- a/rpm/qm.spec +++ b/rpm/qm.spec @@ -124,6 +124,7 @@ BuildRequires: selinux-policy >= %_selinux_policy_version BuildRequires: selinux-policy-devel >= %_selinux_policy_version Requires: parted +Requires: crun-krun Requires: containers-common Requires: selinux-policy >= %_selinux_policy_version Requires(post): selinux-policy-base >= %_selinux_policy_version @@ -374,6 +375,7 @@ fi %{_datadir}/qm/qm-storage-settings %{_datadir}/qm/comment-tz-local %{_datadir}/qm/qm-is-ostree +%{_datadir}/qm/qm-change-podman-runtime %ghost %dir %{_datadir}/containers %ghost %dir %{_datadir}/containers/systemd %{_datadir}/containers/systemd/qm.container diff --git a/tools/qm-change-podman-runtime b/tools/qm-change-podman-runtime new file mode 100755 index 00000000..bbf67b85 --- /dev/null +++ b/tools/qm-change-podman-runtime @@ -0,0 +1,79 @@ +#!/bin/bash + +# Script to dynamically change Podman runtime +# Usage: /usr/share/qm/qm-change-podman-runtime + +set -e + +# Usage function +usage() { + echo "Usage: /usr/share/qm/qm-change-podman-runtime " + echo + echo "Examples:" + echo " /usr/share/qm/qm-change-podman-runtime krun /usr/bin/krun" + echo " /usr/share/qm/qm-change-podman-runtime my-runtime /usr/bin/my-runtime,/usr/local/bin/my-runtime" + exit 1 +} + +# Ensure the script is run as root +if [ "$EUID" -ne 0 ]; then + echo "Error: This script must be run as root. Please use sudo or switch to the root user." + exit 1 +fi + +# Validate input arguments +if [ $# -ne 2 ]; then + usage +fi + +RUNTIME_NAME=$1 +RUNTIME_BINARY_PATHS=$2 +CONFIG_FILE="/etc/containers/containers.conf" +QM_CONTAINER_FILE="/usr/share/containers/systemd/qm.container" + +# Parse binary paths into TOML array format +BINARY_PATHS_TOML=$(echo "$RUNTIME_BINARY_PATHS" | sed 's/,/","/g' | sed 's/^/["/' | sed 's/$/"]/') + +# Create or modify the configuration file +if [ ! -f "$CONFIG_FILE" ]; then + echo "$CONFIG_FILE does not exist. Creating a new configuration file." + mkdir -p /etc/containers + cat << EOF > "$CONFIG_FILE" +runtime = "$RUNTIME_NAME" + +[runtimes] +$RUNTIME_NAME = $BINARY_PATHS_TOML +EOF +else + echo "Updating Podman configuration to set runtime $RUNTIME_NAME with paths $RUNTIME_BINARY_PATHS" + sed -i '/^runtime = /d' "$CONFIG_FILE" # Remove existing runtime setting + sed -i '/^\[runtimes\]/,$d' "$CONFIG_FILE" # Remove existing [runtimes] section + cat << EOF >> "$CONFIG_FILE" +runtime = "$RUNTIME_NAME" + +[runtimes] +$RUNTIME_NAME = $BINARY_PATHS_TOML +EOF +fi + +# Update qm.container file +if [ -f "$QM_CONTAINER_FILE" ]; then + echo "Updating $QM_CONTAINER_FILE to include Runtime=$RUNTIME_NAME in the Container section." + sed -i '/^Runtime=/d' "$QM_CONTAINER_FILE" # Remove any existing Runtime setting + sed -i '/^\[Container\]/a Runtime='"$RUNTIME_NAME" "$QM_CONTAINER_FILE" # Add the new Runtime entry +else + echo "Error: $QM_CONTAINER_FILE does not exist. Skipping update to qm.container." +fi + +# Reload Podman configuration +echo "Reloading Podman configuration..." +podman system migrate + +# Verify the runtime change +echo "Verifying the runtime change..." +if podman info | grep -q "runtime: $RUNTIME_NAME" -A5; then + echo "Runtime successfully set to $RUNTIME_NAME with paths $RUNTIME_BINARY_PATHS." +else + echo "Failed to set runtime to $RUNTIME_NAME." + exit 1 +fi