How can I create and enable permanent 1GB swap file with salt stack that will work after reboot? salt.states.mount.swap
does not allow to define swap size. Furthermore I need to define swappiness. Currently I do it with echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf

- 393
- 4
- 9
-
Did I answer your question? – Dan Garthwaite Feb 02 '15 at 02:25
4 Answers
This is what I use on debian-based systems. It's an improved version of Dan's answer. It gets the available memory from a grain, multiplies it by 2, to create the swapfile with the appropriate size. It also makes an entry in /etc/fstab, if non-existent.
coreutils:
pkg.installed
/swapfile:
cmd.run:
- name: |
[ -f /swapfile ] || dd if=/dev/zero of=/swapfile bs=1M count={{ grains["mem_total"] * 2 }}
chmod 0600 /swapfile
mkswap /swapfile
swapon -a
- unless:
- file /swapfile 2>&1 | grep -q "Linux/i386 swap"
mount.swap:
- persist: true

- 81
- 1
- 3
-
1
-
Note: `swapon -a` probably doesn't do a lot as long as it isn't present in fstab Note2: You might want to check free disk space before creating potentially large files – Marki Feb 16 '21 at 19:10
-
Note3: I'm not sure `* 2` works just like that because computations inside yaml don't work AFAIK – Marki Feb 16 '21 at 19:33
I currently use this in production, works for me.
community_swap_file:
cmd.run:
- name: |
[ -f /.swapfile ] || dd if=/dev/zero of=/.swapfile bs=1M count=2048
chmod 0600 /.swapfile
mkswap /.swapfile
echo '/.swapfile none swap sw 0 0' >> /etc/fstab
swapon -a
- unless: file /.swapfile 2>&1 | grep -q "Linux/i386 swap"

- 2,962
- 1
- 19
- 31
-
Note: the "unless" grep is broken on Ubuntu 22, it should be looking for `Linux swap file` as the output is `/.swapfile: Linux swap file, 4k page size, little endian (...)`, if this isn't changed this will always fire, returning `mkswap: error: /.swapfile is mounted`, adding a new line in `fstab` etc. I'd recommend changing `unless` to just do `test -f /.swapfile` and remove `[ -f /.swapfile ]` from the `name` block – pzkpfw Jun 03 '22 at 09:48
Using fallocate
is instantaneous compared to dd
. Also, the state below will regenerate the swapfile if you change its size in the pillar. This version also omits the superfluous swapon -a
, which mount.swap
handles for you.
{% set swapfile = salt['pillar.get']('swapfile', {}) %}
{% set size = swapfile.get('size', grains["mem_total"]) %}
{% set path = swapfile.get('path', '/var/swapfile0') %}
{{ path }}:
cmd.run:
- name: |
swapon --show=NAME --noheadings | grep -q "^{{ path }}$" && swapoff {{ path }}
rm -f {{ path }}
fallocate -l {{ size }}M {{ path }}
chmod 0600 {{ path }}
mkswap {{ path }}
- unless: bash -c '[[ $(($(stat -c %s {{ path }}) / 1024**2)) = {{ size }} ]]'
mount.swap:
- persist: true

- 141
- 1
I'm doing a similar thing to Dan's answer:
configuring_swap:
cmd.run:
- name: |
[ -f /var/swap.1 ] && swapoff /var/swap.1
dd if=/dev/zero of=/var/swap.1 bs=1M count=3072
chmod 0600 /var/swap.1
mkswap /var/swap.1
swapon /var/swap.1
- unless:
- "[ `free -b | awk '/Swap/{print $2}'` -ge 3221225472 ]"
file.append:
- name: /etc/fstab
- text: /var/swap.1 swap swap defaults 0 0
- onlyif:
- file /var/swap.1 2>&1 | grep -q "Linux/i386 swap"
This looks at how much total swap the system has, if it's less than 3GB create a 3GB swap file and make sure it is persistent between reboots via /etc/fstab
. If you want to make sure your swappiness config is put in place you can do something like:
configure_swappiness:
file.append:
- name: /etc/sysctl.conf
- text: vm.swappiness = 10

- 11
- 1
- 4
-
Why do you append the entry to `/etc/fstab` manually instead of using `mount.swap`? – Cedric Meury Apr 25 '16 at 13:35
-