Skip to content
Skip to the article
In Linux: 9 articles
Linux

Swap Configuration

Create a swap file, enable compressed zswap caching, and build mirrored swap on LVM RAID1 with dm-integrity checksums.

Updated
Applies to
  • Ubuntu 24.04
  • Ubuntu 26.04
  • Linux 6.x
  • LVM 2.03
Tags
  • linux
  • swap
  • zswap
  • lvm
  • ubuntu
Reading time
4 min

Recipes for adding and tuning swap on Linux servers. Run every command as root.

Check Current Swap

See which swap devices are active and how much memory and swap are in use:

swapon --show
free -h

Create a Swap File

A swap file is the quickest way to add swap to a server without repartitioning. This works on ext4 and XFS:

fallocate -l 4G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap sw 0 0' >> /etc/fstab

The fstab line re-enables the swap file at boot. Ubuntu installs often already have a /swap.img entry, so check swapon --show before adding a second file.

On Btrfs

Btrfs swap files must be uncompressed, have copy-on-write disabled, and be outside any snapshotted subvolume. btrfs-progs 6.1 and later handle all of this in one command:

btrfs subvolume create /swap
btrfs filesystem mkswapfile --size 4G /swap/swapfile
swapon /swap/swapfile
echo '/swap/swapfile none swap defaults 0 0' >> /etc/fstab

Remove a Swap File

Turn the swap file off, delete it, and remove its line from /etc/fstab:

swapoff /swapfile
rm /swapfile
sed -i '\|^/swapfile |d' /etc/fstab

Adjust Swappiness

vm.swappiness (default 60) controls how eagerly the kernel swaps. Lower values keep more application memory in RAM, which usually suits database servers:

sysctl vm.swappiness=10
echo 'vm.swappiness=10' > /etc/sysctl.d/99-swappiness.conf

Enable zswap

zswap keeps a compressed cache of pages in RAM in front of the swap device. Pages that would have gone to disk are compressed instead, which cuts swap I/O sharply. zswap still needs a real swap device or file behind it.

Check whether it's already on:

cat /sys/module/zswap/parameters/enabled

Turn it on immediately (until the next reboot):

echo 1 > /sys/module/zswap/parameters/enabled
echo zstd > /sys/module/zswap/parameters/compressor

To make it permanent on GRUB systems, add the parameters to GRUB_CMDLINE_LINUX_DEFAULT in /etc/default/grub:

GRUB_CMDLINE_LINUX_DEFAULT="quiet zswap.enabled=1 zswap.compressor=zstd"

Then apply and reboot:

update-grub

Check the result with grep -r . /sys/module/zswap/parameters/. On NixOS, add the same parameters to boot.kernelParams. On Proxmox hosts that boot with systemd-boot, see Proxmox Kernel Updates.

Mirrored Swap on LVM with Integrity Checking

On a server with two spare disks, you can build an LVM volume group that mirrors swap (and any other logical volumes) across both disks with dm-integrity checksums. If one disk fails, the system keeps running. If a sector silently returns bad data, LVM detects it through the checksum and reads the good copy from the other disk.

1. Prepare the Disks

Identify the disks by their stable IDs:

ls -l /dev/disk/by-id/ | grep -v part

Warning

The next commands erase partition tables and signatures on the named disks. Triple-check the IDs. Everything on those disks will be lost.

wipefs -a /dev/disk/by-id/<DISK1_ID> /dev/disk/by-id/<DISK2_ID>
pvcreate /dev/disk/by-id/<DISK1_ID> /dev/disk/by-id/<DISK2_ID>
vgcreate <VG_NAME> /dev/disk/by-id/<DISK1_ID> /dev/disk/by-id/<DISK2_ID>

2. Create the Mirrored Swap Volume

Create a two-way RAID1 logical volume with integrity enabled:

lvcreate --type raid1 -m 1 \
  --raidintegrity y \
  --raidintegrityblocksize 4096 \
  --raidintegritymode bitmap \
  -L <SWAP_SIZE> -n swap <VG_NAME>
  • --raidintegrityblocksize 4096 matches the 4 KiB page size that swap writes in.

  • --raidintegritymode bitmap is faster than the default journal mode. The trade-off is that after a crash, recently written blocks can fail their checksums and are rebuilt from the mirror. Use the default journal mode for data that matters more than swap.

You can use the rest of the volume group for another mirrored volume, for example for backups. The integrity block size must not be larger than the block size of the filesystem you put on it. 4096 suits ext4 and XFS defaults, and 512 is the most compatible:

lvcreate --type raid1 -m 1 \
  --raidintegrity y \
  --raidintegrityblocksize 4096 \
  -l 100%FREE -n backup <VG_NAME>
mkfs.ext4 /dev/<VG_NAME>/backup

3. Enable the Swap Volume

mkswap /dev/<VG_NAME>/swap
swapon /dev/<VG_NAME>/swap
echo '/dev/<VG_NAME>/swap none swap sw 0 0' >> /etc/fstab

4. Monitor the Mirror

Watch the initial sync, and check for integrity mismatches later:

lvs -a -o name,segtype,sync_percent,devices <VG_NAME>
lvs -o+integritymismatches <VG_NAME>

A non-zero mismatch count means LVM caught and corrected bad data from one disk. Check that disk's SMART status with smartctl -a.

Sources

This article is in the public domain (CC0 1.0), code samples included. Use it however helps you.