6

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

1
5

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"
8

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
3
  • 1
    Works on Arch Linux as well. – Cedric Meury Apr 25 '16 at 13:44
  • 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 at 19:10
  • Note3: I'm not sure * 2 works just like that because computations inside yaml don't work AFAIK – Marki Feb 16 at 19:33
3

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
0

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
2
  • Why do you append the entry to /etc/fstab manually instead of using mount.swap? – Cedric Meury Apr 25 '16 at 13:35
  • @Ced Old habits. – codybuell May 18 '16 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.