Skip to content

Commit

Permalink
add libkrun support
Browse files Browse the repository at this point in the history
initial code to add libkrun support

Signed-off-by: Douglas Schilling Landgraf <[email protected]>
  • Loading branch information
dougsland committed Dec 28, 2024
1 parent cac7300 commit d392e9c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions rpm/qm.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
79 changes: 79 additions & 0 deletions tools/qm-change-podman-runtime
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/bin/bash

# Script to dynamically change Podman runtime
# Usage: /usr/share/qm/qm-change-podman-runtime <runtime-name> <runtime-binary-paths>

set -e

# Usage function
usage() {
echo "Usage: /usr/share/qm/qm-change-podman-runtime <runtime-name> <runtime-binary-paths>"
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

0 comments on commit d392e9c

Please sign in to comment.