diff --git a/package/harvester-os/files/etc/systemd/system/rke2-shutdown.service b/package/harvester-os/files/etc/systemd/system/rke2-shutdown.service new file mode 100644 index 000000000..0b54a8436 --- /dev/null +++ b/package/harvester-os/files/etc/systemd/system/rke2-shutdown.service @@ -0,0 +1,13 @@ +[Unit] +Description=Kill RKE2 Containers for clean unmount +DefaultDependencies=no +RefuseManualStart=yes +Before=reboot.target halt.target shutdown.target poweroff.target umount.target final.target + +[Install] +RequiredBy=reboot.target halt.target shutdown.target poweroff.target umount.target final.target + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/usr/sbin/rke2-kill-containers.sh \ No newline at end of file diff --git a/package/harvester-os/files/system/oem/91_rke2-shutdown.yaml b/package/harvester-os/files/system/oem/91_rke2-shutdown.yaml new file mode 100644 index 000000000..ec1833314 --- /dev/null +++ b/package/harvester-os/files/system/oem/91_rke2-shutdown.yaml @@ -0,0 +1,7 @@ +name: "Enable RKE2 shutdown service" +stages: + initramfs: + - name: "enable rke2-shutdown.service" + if: 'grep -q root=LABEL=COS_ACTIVE /proc/cmdline && [ -n "$(blkid -L COS_ACTIVE)" ]' + commands: + - systemctl enable rke2-shutdown.service \ No newline at end of file diff --git a/package/harvester-os/files/usr/sbin/rke2-kill-containers.sh b/package/harvester-os/files/usr/sbin/rke2-kill-containers.sh new file mode 100755 index 000000000..5f12d7b0c --- /dev/null +++ b/package/harvester-os/files/usr/sbin/rke2-kill-containers.sh @@ -0,0 +1,25 @@ +#!/bin/bash -x +function list_units() { + systemctl list-units --type=scope --no-legend | grep -Eo "(cri-containerd-.+\.scope)" +} + +function get_unit_status() { + local unit_name="$1" + systemctl status "${unit_name}" +} + +systemctl stop rke2-server.service || true +systemctl stop rke2-agent.service || true + +unit_list=($(list_units)) + +max_concurrency=$(($(nproc) + 1)) +if [ ${#unit_list[@]} -lt $max_concurrency ]; then + max_concurrency=${#unit_list[@]} +fi + +printf '%s\0' "${unit_list[@]}" | xargs -0 -n1 -P "$max_concurrency" sh -c 'sudo systemctl stop --kill-who=all "$@" && echo "Finished $@"' _ | { grep -v '^Finished' || true; } | { grep -v 'xargs:' || true; } & +wait + +echo "Finished stopping all units." +exit 0 \ No newline at end of file