Skip to content

Instantly share code, notes, and snippets.

@ForbiddenEra
Last active August 29, 2015 14:16
Show Gist options
  • Save ForbiddenEra/52c8280f68df8cadb553 to your computer and use it in GitHub Desktop.
Save ForbiddenEra/52c8280f68df8cadb553 to your computer and use it in GitHub Desktop.
CoreOS swap auto-setup
# add this to your cloud config
# or a systemctl unit file on all nodes
# or as a fleetctl global unit (add [X-Fleet]\nglobal=true)
# will auto detect a nodes memory and create a swap file the same size, turn off CoW, mount a loopback device
# (actually, it touches the file first THEN disables COW!)
# fully dynamic and non-interfering, will find the first free loopback & unmount/delete on stop
- name: swap-auto.service
command: start
enabled: true
content:
[Unit]
Description=Setup Swap Service
Documentation=http://shaped.ca
[Service]
Type=oneshot
Environment="SWAP_PATH=/swap" "SWAP_FILE=swap.fs"
ExecStartPre=-/usr/bin/rm -rf ${SWAP_PATH}
ExecStartPre=/usr/bin/mkdir ${SWAP_PATH}
ExecStartPre=/usr/bin/touch ${SWAP_PATH}/${SWAP_FILE}
ExecStartPre=/usr/bin/chattr +C ${SWAP_PATH}/${SWAP_FILE}
ExecStartPre=/bin/bash -c "fallocate -l $(free -h --si|awk '/^Mem:/{print $2}') ${SWAP_PATH}/${SWAP_FILE}"
ExecStartPre=/usr/bin/chmod 600 ${SWAP_PATH}/${SWAP_FILE}
ExecStartPre=/usr/sbin/mkswap ${SWAP_PATH}/${SWAP_FILE}
ExecStartPre=/usr/sbin/losetup -f ${SWAP_PATH}/${SWAP_FILE}
ExecStart=/usr/bin/sh -c "/sbin/swapon $(/usr/sbin/losetup -j ${SWAP_PATH}/${SWAP_FILE} | /usr/bin/cut -d : -f 1)"
ExecStop=/usr/bin/sh -c "/sbin/swapoff $(/usr/sbin/losetup -j ${SWAP_PATH}/${SWAP_FILE} | /usr/bin/cut -d : -f 1)"
ExecStopPost=/usr/bin/sh -c "/usr/sbin/losetup -d $(/usr/sbin/losetup -j ${SWAP_PATH}/${SWAP_FILE} | /usr/bin/cut -d : -f 1)"
ExecStopPost=-/usr/bin/rm -rf ${SWAP_PATH}
RemainAfterExit=true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment