Created
September 4, 2025 15:32
-
-
Save RobertKrawitz/201f50909504b64a7e22d0c05b9a4139 to your computer and use it in GitHub Desktop.
OCP code to set up swap. Be very careful when editing this!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| kind: MachineConfig | |
| apiVersion: machineconfiguration.openshift.io/v1 | |
| metadata: | |
| labels: | |
| machineconfiguration.openshift.io/role: worker | |
| name: 91-worker-swap | |
| spec: | |
| config: | |
| ignition: | |
| version: 3.4.0 | |
| systemd: | |
| units: | |
| - contents: | | |
| [Unit] | |
| Description=Provision and enable swap | |
| ConditionFirstBoot=no | |
| [Service] | |
| Type=oneshot | |
| ExecStart=/bin/sh -c 'declare -A used_disks; rootdisk=$(df /boot | tail -1 | sed "s/ .*//") && \ | |
| if [[ -n "${rootdisk:-}" ]] ; then used_disks["$(readlink -f "$rootdisk" | sed "s/[0-9]*\$//")"]=1; fi && \ | |
| if [[ -d /mnt/local-storage/localfile ]] ; then \ | |
| while read -r disk ; do \ | |
| if [[ -n "$disk" ]] ; then used_disks["$disk"]=1; fi ; \ | |
| done <<< "$(cd /mnt/local-storage/localfile/ && readlink -f * | sed "s/[0-9]*\$//")" ; fi && \ | |
| for d in /dev/sd? ; do \ | |
| if [[ -z "${used_disks[$d]:-}" && -b "$d" ]] ; then \ | |
| echo "Using disk $d for swap" && \ | |
| disk="$d" && \ | |
| sudo mkswap "$disk" && \ | |
| sudo swapon "$disk" && \ | |
| sudo systemctl set-property --runtime system.slice MemorySwapMax=0 IODeviceLatencyTargetSec="/ 50ms" && \ | |
| free -h ; \ | |
| break ; \ | |
| fi; \ | |
| done' | |
| [Install] | |
| RequiredBy=kubelet-dependencies.target | |
| enabled: true | |
| name: swap-provision.service |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Before applying this, you MUST ensure that you've applied a KubeletConfig that sets
failSwapOn: falseand waited for it to be applied to every node. Otherwise the kubelet will fail to start and cleaning it up is messy.Be very careful when editing this file. If you make a change resulting in this service failing, the kubelet will not start, you'll have to roll back the machineconfig, and then manually reboot the nodes this was applied to.