Skip to content

Instantly share code, notes, and snippets.

@philips
Last active March 17, 2017 21:03
Show Gist options
  • Save philips/7310435 to your computer and use it in GitHub Desktop.
Save philips/7310435 to your computer and use it in GitHub Desktop.
Setting up swap on coreos

Setup a swap file in the stateful partition

Run these commands as root to create a 512 megabyte swap.

fallocate -l 512m /media/state/512MiB.swap
chmod 600 /media/state/512MiB.swap
mkswap /media/state/512MiB.swap

Create a unit to initialize it on boot

Create a unit file at /media/state/units/swapon.service with these contents:

[Unit]
Description=Turn on swap

[Service]
Type=oneshot
ExecStart=/sbin/swapon /media/state/512MiB.swap

[Install]
WantedBy=local.target

Enable the unit and start using swap

systemctl enable --runtime /media/state/units/swapon.service
systemctl start swapon

On your next reboot the swap will be turned on automatically.

@shime
Copy link

shime commented Nov 3, 2014

I'm getting this error

swapon: /512MiB.swap: swapon failed: Invalid argument

@n0mer
Copy link

n0mer commented Jan 23, 2015

@shime , check this: coreos/docs#52

this is due filesystem type

@amcsi
Copy link

amcsi commented Mar 1, 2016

I'm getting this:

Mar 01 11:50:03 www.szeremi.org systemd[1]: swapon.service: Failed with result 'exit-code'.
Mar 01 11:50:03 www.szeremi.org swapon[23089]: swapon: stat of /media/state/512MiB.swap failed: No such file or directory

@ThomasLomas
Copy link

Thanks this worked well. 👍

@Kaijun
Copy link

Kaijun commented May 27, 2016

I'm using this config in Digital Ocean, but it doesn't turn on swap automatically after rebooting. any ideas?

@intellent
Copy link

Same problem as @Kaijun. Also in DO.

@vitor-tyburski
Copy link

@Kaijun and @intellent, remove the --runtime on systemctl enable command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment