For the complete documentation index, see llms.txt. This page is also available as Markdown.

25.4 Adding Swap Partitions

In some cases, it may be necessary to increase swap space. In FreeBSD, swap space can be implemented through traditional partitions, swap files, or ZFS volumes (ZVOLs). This section describes methods for adding swap space after system installation.

Since neither UFS nor ZFS file systems support partition shrinking, if a swap partition was not configured during system installation, swap space can only be added through a swap file created with dd or a ZFS volume.

Directory Structure

/
├── usr
   └── swap0                       # Swap file
├── etc
   ├── rc.conf                      # System startup configuration file
   └── fstab                        # Persistent mount configuration file
└── dev
    ├── md0                          # Memory disk (for swap file)
    ├── nda0p3                       # Swap partition
    └── zvol
        └── zroot
            └── swap                 # ZFS swap volume

Traditional Swap File Method Using the dd Command

Create an 8 GB swap file /usr/swap0 (1 GB = 1024 MB; for larger capacities, scale proportionally):

Set the swap file access permissions to allow only the owner to read and write:

To enable it immediately, first use mdconfig to configure the swap file as a memory disk device, then use swapon to activate the swap space. mdconfig maps the file to a memory disk, and swapon activates the swap device:

To make this persist across system reboots, add the following content to the /etc/rc.conf configuration file:

Using a ZFS Volume as Swap Space

Warning

The operations described in this section may affect system crash dump functionality.

Warning

According to the OpenZFS community documentation, on systems with extreme memory pressure, using a zvol as a swap device may cause the system to lock up regardless of how much swap space remains (see Swap deadlock in 0.7.9). Placing swap space on another partition may affect ZFS data integrity checking for swap space, so the upstream documentation recommends using swap space cautiously in extreme memory pressure scenarios. Additionally, swap space is critical for system hibernation functionality; if this feature is needed, ensure that the swap space capacity is at least equal to the system memory capacity.

Create an 8 GB zvol (ZFS block device volume) under the ZFS pool zroot for use as swap space:

The parameters of the above command are described as follows:

Parameter
Description

$(getconf PAGESIZE)

Returns the system page size (fixed at 4096 on FreeBSD/amd64), aligning the swap volume with the system page to improve performance

-o

Used to specify options, with the syntax -o property_name=property_value

-o logbias=throughput

ZFS will optimize synchronous operations, improving global throughput of the pool and effectively utilizing resources, which can enhance file write performance

-o sync=always

Forces all write operations to be synchronized in real time

-o primarycache=metadata

Controls the ARC caching policy to only cache metadata, not actual data blocks, preventing ARC from caching swap data into memory

-o com.sun:auto-snapshot=false

ZFS user-defined property, read by the third-party tool zfs-auto-snapshot (requires installing filesystems/zfstools via ports) — setting false disables automatic snapshots of this swap volume by that tool, since swap data does not need to be backed up

-V

Used to create a ZFS volume (zvol) instead of a ZFS file system

Note

In FreeBSD, the default ZFS pool name is zroot, and the volume name created here is swap.

Enable the ZFS zvol as swap space:

Add an entry for the ZFS zvol swap partition in the /etc/fstab file to enable it automatically at boot:

After writing the configuration, you can use the command swapon -a to check (-a activates all swap entries in /etc/fstab that are not marked noauto) and ensure there is no error output. To deactivate a swap space (such as before removing a device), run swapoff /dev/zvol/zroot/swap, which migrates swapped-out pages to other available swap space or physical memory.

References

Viewing Swap Space Usage

Display system swap space information in more human-readable units:

From the output, /dev/nda0p3 is the swap partition with a size of 2 GB and current usage of 0.

Last updated