|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
3 | | -set -Eeou pipefail |
| 3 | +set -Eeoux pipefail |
4 | 4 |
|
5 | | -echo "Cleaning DNF cache..." |
6 | | -sudo dnf clean all && sudo rm -r /var/cache/dnf |
| 5 | +echo "Setting up IBM container runtime for rootless containers" |
7 | 6 |
|
8 | | -echo "Installing/upgrading crun..." |
9 | | -sudo dnf upgrade -y crun --disableplugin=subscription-manager || \ |
10 | | -sudo dnf install -y crun --disableplugin=subscription-manager || \ |
11 | | -sudo yum upgrade -y crun --disableplugin=subscription-manager || \ |
12 | | -sudo yum install -y crun --disableplugin=subscription-manager |
| 7 | +# Enable lingering for the user - allows systemd user services without an active login session |
| 8 | +echo "Enabling lingering for user $(whoami)..." |
| 9 | +sudo loginctl enable-linger "$(whoami)" || true |
13 | 10 |
|
14 | | -if ! crun --version &>/dev/null; then |
15 | | - echo "❌ crun installation failed" |
16 | | - exit 1 |
17 | | -fi |
| 11 | +# Delegate cgroup controllers for rootless containers (required for cgroup v2) |
| 12 | +# This allows rootless podman/minikube to manage CPU, memory, IO limits |
| 13 | +echo "Setting up cgroup delegation for rootless containers..." |
| 14 | +sudo mkdir -p /etc/systemd/system/user@.service.d |
| 15 | +sudo tee /etc/systemd/system/user@.service.d/delegate.conf > /dev/null << 'CGROUP_EOF' |
| 16 | +[Service] |
| 17 | +Delegate=cpu cpuset io memory pids |
| 18 | +CGROUP_EOF |
| 19 | +sudo systemctl daemon-reload || true |
18 | 20 |
|
19 | | -current_version=$(crun --version | head -n1) |
20 | | -echo "✅ Using crun: ${current_version}" |
| 21 | +# Setup XDG_RUNTIME_DIR for rootless podman |
| 22 | +uid=$(id -u) |
| 23 | +runtime_dir="/run/user/${uid}" |
| 24 | +if [[ ! -d "${runtime_dir}" ]]; then |
| 25 | + sudo mkdir -p "${runtime_dir}" |
| 26 | + sudo chown "$(whoami):$(whoami)" "${runtime_dir}" |
| 27 | + sudo chmod 700 "${runtime_dir}" |
| 28 | +fi |
| 29 | +export XDG_RUNTIME_DIR="${runtime_dir}" |
21 | 30 |
|
22 | | -# Clean up any existing conflicting configurations |
23 | | -echo "Cleaning up existing container configurations..." |
24 | | -rm -f ~/.config/containers/containers.conf 2>/dev/null || true |
25 | | -sudo rm -f /root/.config/containers/containers.conf 2>/dev/null || true |
26 | | -sudo rm -f /etc/containers/containers.conf 2>/dev/null || true |
| 31 | +# Set up D-Bus session bus address for rootless podman networking |
| 32 | +if [[ -S "${runtime_dir}/bus" ]]; then |
| 33 | + export DBUS_SESSION_BUS_ADDRESS="unix:path=${runtime_dir}/bus" |
| 34 | + echo "Using existing D-Bus session at ${DBUS_SESSION_BUS_ADDRESS}" |
| 35 | +else |
| 36 | + echo "No D-Bus session found, attempting to start one..." |
| 37 | + systemctl --user start dbus.socket 2>/dev/null || true |
| 38 | + if [[ -S "${runtime_dir}/bus" ]]; then |
| 39 | + export DBUS_SESSION_BUS_ADDRESS="unix:path=${runtime_dir}/bus" |
| 40 | + echo "Started D-Bus session at ${DBUS_SESSION_BUS_ADDRESS}" |
| 41 | + fi |
| 42 | +fi |
27 | 43 |
|
28 | | -crun_path=$(which crun) |
29 | | -echo "Using crun path: ${crun_path}" |
| 44 | +# Write environment to file for other scripts to source |
| 45 | +cat > "${HOME}/.podman_env" << EOF |
| 46 | +export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR}" |
| 47 | +export DBUS_SESSION_BUS_ADDRESS="${DBUS_SESSION_BUS_ADDRESS:-}" |
| 48 | +EOF |
| 49 | +echo "Wrote podman environment to ${HOME}/.podman_env" |
30 | 50 |
|
31 | | -config="[containers] |
32 | | -cgroup_manager = \"cgroupfs\" |
| 51 | +# Clean up stale podman state (fixes "cannot re-exec process to join the existing user namespace") |
| 52 | +echo "Cleaning up stale podman state..." |
| 53 | +pkill -9 -u "$(id -u)" -f "podman" 2>/dev/null || true |
| 54 | +pkill -9 -u "$(id -u)" -f "conmon" 2>/dev/null || true |
| 55 | +rm -rf "${XDG_RUNTIME_DIR}/containers" 2>/dev/null || true |
| 56 | +rm -rf "${XDG_RUNTIME_DIR}/libpod" 2>/dev/null || true |
| 57 | +rm -rf "${HOME}/.local/share/containers/storage/libpod" 2>/dev/null || true |
| 58 | +rm -rf "${HOME}/.local/share/containers/storage/overlay-containers" 2>/dev/null || true |
| 59 | +sleep 1 |
33 | 60 |
|
34 | | -[engine] |
35 | | -runtime = \"crun\"" |
| 61 | +# Install crun |
| 62 | +echo "Installing crun..." |
| 63 | +sudo dnf clean all || true |
| 64 | +sudo dnf install -y crun --disableplugin=subscription-manager || \ |
| 65 | +sudo yum install -y crun --disableplugin=subscription-manager || true |
36 | 66 |
|
| 67 | +# Configure rootless podman |
37 | 68 | mkdir -p ~/.config/containers |
38 | | -echo "${config}" > ~/.config/containers/containers.conf |
39 | 69 |
|
40 | | -sudo mkdir -p /root/.config/containers |
41 | | -echo "${config}" | sudo tee /root/.config/containers/containers.conf >/dev/null |
| 70 | +cat > ~/.config/containers/containers.conf << 'EOF' |
| 71 | +[containers] |
| 72 | +cgroup_manager = "cgroupfs" |
| 73 | +
|
| 74 | +[network] |
| 75 | +# Use slirp4netns instead of pasta for rootless networking |
| 76 | +default_rootless_network_cmd = "slirp4netns" |
| 77 | +EOF |
| 78 | + |
| 79 | +cat > ~/.config/containers/storage.conf << EOF |
| 80 | +[storage] |
| 81 | +driver = "overlay" |
| 82 | +runroot = "${XDG_RUNTIME_DIR}/containers" |
| 83 | +graphroot = "${HOME}/.local/share/containers/storage" |
| 84 | +EOF |
42 | 85 |
|
43 | | -echo "✅ Configured crun" |
| 86 | +echo "Done" |
0 commit comments