> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/ask/flat/chapter-25-storage-management/di-25.4-jie-xin-zeng-jiao-huan-fen-qu.md).

# 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

```sh
/
├── 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):

```sh
# dd if=/dev/zero of=/usr/swap0 bs=1M count=8192 status=progress  # bs=1M means writing in 1 MiB block size; status=progress shows progress
  8416919552 bytes (8417 MB, 8027 MiB) transferred 4.011s, 2098 MB/s # The above is dd real-time progress output
8192+0 records in
8192+0 records out
8589934592 bytes transferred in 4.071005 secs (2110028088 bytes/sec)
```

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

```sh
# chmod 0600 /usr/swap0
```

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:

```sh
# mdconfig -a -t vnode -f /usr/swap0 -u 0 && swapon /dev/md0
```

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

```ini
swapfile="/usr/swap0"
```

## 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](https://github.com/openzfs/zfs/issues/7734)). 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:

```sh
# zfs create -V 8G -b $(getconf PAGESIZE) -o logbias=throughput -o sync=always -o primarycache=metadata -o com.sun:auto-snapshot=false zroot/swap
```

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:

```sh
# swapon /dev/zvol/zroot/swap
```

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

```ini
/dev/zvol/zroot/swap none swap sw 0 0
```

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

* OpenZFS Project. FAQ\[EB/OL]. \[2026-03-25]. <https://openzfs.github.io/openzfs-docs/Project%20and%20Community/FAQ.html>. Official OpenZFS FAQ, providing best practices for ZFS volume configuration.
* Oracle. Oracle Solaris ZFS Administration Guide\[EB/OL]. <https://docs.oracle.com/cd/E19253-01/819-7065/givdo/index.html>. Official Oracle ZFS technical documentation (based on Solaris 10, some information is outdated; refer to the official OpenZFS documentation at <https://openzfs.github.io/openzfs-docs/>).
* FreeBSD Project. swapon(8) -- specify additional devices for paging and swapping\[EB/OL]. \[2026-04-17]. <https://man.freebsd.org/cgi/man.cgi?query=swapon&sektion=8>. Swap space management tool manual page.
* FreeBSD Project. swapinfo(8) -- display system swap space usage\[EB/OL]. \[2026-04-17]. <https://man.freebsd.org/cgi/man.cgi?query=swapinfo&sektion=8>. Swap space usage information query tool manual page.

## Viewing Swap Space Usage

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

```sh
# swapinfo -h
Device              Size     Used    Avail Capacity
/dev/nda0p3         2.0G       0B     2.0G     0%
```

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.bsdcn.org/ask/flat/chapter-25-storage-management/di-25.4-jie-xin-zeng-jiao-huan-fen-qu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
