Skip to content

Instantly share code, notes, and snippets.

@facundovictor
Forked from ivanalejandro0/swapon.service
Last active July 7, 2016 17:51
Show Gist options
  • Save facundovictor/67bf20e5b4811d79a5e97ebb69162a0c to your computer and use it in GitHub Desktop.
Save facundovictor/67bf20e5b4811d79a5e97ebb69162a0c to your computer and use it in GitHub Desktop.
Swap service for CoreOS

swapon.service

A auto swap file configured in a single systemd service.

  1. Copy the service into: /etc/systemd/system/swapon.service
  2. Load the service:systemctl enable /etc/systemd/system/swapon.service
  3. Start the service: systemctl start swapon

Considerations before swapon:

  • The file must be in a partition with enough space
  • The file should have Copy-On-Write disabled to avoid fragmentation (If the underlying filesystem is btrfs or ZFS).
  • The file should have blocks allocated and marked as uninitialized, or should be zeroed. This scripts uses the first approach (Faster than zeroing).
  • The file should be protected to being accessed from other users.
  • A loop device needs to be associated with the file. This make the file accessible as a block device, and can be mounted as it were a disk device.

Swappiness:

Is a kernel parameter that let tweak the way Linux swaps. It's a number from 0 to 100. In essence, higher values lead to more pages being swapped, and lower values lead to more applications being kept in memory, even if they are idle.

If memory is swapped out too quickly then application response time drops, because when the application it's in its CPU quantum, the system has to swap the application back into memory, which will make it feel slow.

The default value for swappiness is 60. You can alter it temporarily (until you next reboot) by typing as root:

echo 50 > /proc/sys/vm/swappiness

If you want to alter it permanently then you need to change the vm.swappiness parameter in the /etc/sysctl.conf file.

References:
# /etc/systemd/system/swapon.service
# from: https://www.matthowlett.com/notes/2015/08/01/coreos-swap.html
# sudo systemctl enable swapon.service
# restart
# or
# sudo systemctl start swapon
[Unit]
Description=Turn on swap
[Service]
Type=oneshot
Environment="SWAPFILE=/1024MiB.swap"
RemainAfterExit=true
ExecStartPre=/usr/bin/touch ${SWAPFILE}
# Disable Copy-On-Write to avoid fragmentation (If the underlying filesystem is btrfs or ZFS).
ExecStartPre=/usr/bin/chattr +C ${SWAPFILE}
# Allocating blocks and marking them as uninitialized (Faster than zeroing).
ExecStartPre=/usr/bin/fallocate -l 1024m ${SWAPFILE}
ExecStartPre=/usr/bin/chmod 600 ${SWAPFILE}
ExecStartPre=/usr/sbin/mkswap ${SWAPFILE}
# Associate a loop device with regular the file. This make the file accessible as a block device, and can be mounted as it were a disk device.
ExecStartPre=/usr/sbin/losetup -f ${SWAPFILE}
ExecStart=/usr/bin/sh -c "/sbin/swapon $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"
ExecStop=/usr/bin/sh -c "/sbin/swapoff $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"
# Detach the swap file associated with the loop device.
ExecStopPost=/usr/bin/sh -c "/usr/sbin/losetup -d $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment