Skip to content

HOWTO use a zvol as a swap device

Doug Nickerson edited this page Nov 9, 2020 · 12 revisions

CAUTION: for now swap on zvol may lead to deadlock, in this case please send your logs here.

Info

Linux swaps based on sysctl vm.page-cluster (https://www.kernel.org/doc/Documentation/sysctl/vm.txt) it defaults to 3 which is 2^3 pages (8 pages per swap operation, on x86 8 * 4kb = 32kb) thus blocksize could potentially be based on page-cluster. If using with zswap compression shouldn't be used as zswap will punt uncompressable pages to disk, zswap will also 'dedupe' same pages vs a compressed zvol which will still need to store same but swapped pages.

Step 1: Create a volume dataset (zvol) for use as a swap device:

# zfs create -V 4G -b $(getconf PAGESIZE) -o compression=zle \
      -o logbias=throughput -o sync=standard \
      -o primarycache=metadata -o secondarycache=none \
      -o com.sun:auto-snapshot=false rpool/swap

You can adjust the size (the 4G part) to your needs.

The compression algorithm is set to zle because it is the cheapest available algorithm. With ashift=12 (4 kiB blocks on disk), the common case of a 4 kiB page size means that no compression algorithm can reduce I/O. The exception is all-zero pages, which are dropped by ZFS; but some form of compression has to be enabled to get this behavior. If your pool uses ashift=9, you could use compression=lz4.

Step 2: Format the swap device:

# mkswap -f /dev/zvol/rpool/swap

Step 3: Update /etc/fstab:

# echo /dev/zvol/rpool/swap none swap defaults 0 0 >> /etc/fstab

Warning: Always use long /dev/zvol aliases in configuration files. Never use a short /dev/zdX device name.

Step 4: Enable the swap device:

# swapon -av

Swap lockups

On systems with extremely high memory pressure, using a zvol for swap can result in lockup, regardless of how much swap is still available. This issue is currently being investigated in https://github.com/zfsonlinux/zfs/issues/7734