swapctl(8) -- System swap management tool

swapctl(8) helps manage system swap.

To list swap devices and files:

$ swapctl -l
Device      512-blocks     Used    Avail Capacity  Priority
/dev/sd0b      2109020        0  2109020     0%    0

Here we see partition /dev/sd0b, the swap partition, with 2109020 blocks (or 1GB of swap). 0% capacity is used, meaning all 1GB of swap is available.

On a system with heavy memory usage, all swap space may be used. In the following example, we will switch to a busier system:

$ swapctl -l
Device      512-blocks     Used    Avail Capacity  Priority
/dev/sd1b      2109020  2106160     2860   100%    0

Adding Swap Partition

To add a new swap partition, you need some free space to create a new disklabel partition.

In this example, we've just prepared a new disk sd1 with fdisk and have 20G of free space:

# disklabel -E sd1
Label editor (enter '?' for help at any prompt)
sd1> p g
OpenBSD area: 64-41943040; size: 20.0G; free: 20.0G
#                size           offset  fstype [fsize bsize   cpg]
  c:            20.0G                0  unused                    

We add a new swap partition sd1b since by convention, swap partitions use the letter b:

sd1> a b
offset: [64] 
size: [41942976] 1G
FS type: [swap] 
sd1*> p g
OpenBSD area: 64-41943040; size: 20.0G; free: 19.0G
#                size           offset  fstype [fsize bsize   cpg]
  b:             1.0G               64    swap                    
  c:            20.0G                0  unused                    
sd1*> w
sd1> q
No label changes.

NOTE: You can add a swap partition even if another swap partition already exists, so long as you have the free space and you specify the FS type is swap. It is not necessary to have a new disk with only a single swap partition.

Next, we find the DUID on the disklabel:

# disklabel sd1 | grep duid
duid: 23e250c235b4218f

Then we add this line at the bottom of /etc/fstab:

23e250c235b4218f.b none swap sw

This says that partition b of the device with duid 23e250c235b4218f has a mount point of none, and is of type swap.

Finally we call swapctl to add this new swap device:

# swapctl -A 
swapctl: adding 23e250c235b4218f.b as swap device at priority 0

We can confirm its presence:

# swapctl -l
Device      512-blocks     Used    Avail Capacity  Priority
/dev/sd0b      2109020        0  2109020     0%    0
/dev/sd1b      2104451        0  2104451     0%    0
Total          4213471        0  4213471     0%

Using Swap Files

If you lack free space on disk to create a swap partition, you can also use a swap file.

NOTE: A dedicated swap partition is preferred where possible.

First, we create the swap file in an existing partition with plenty of free space. We will use dd?. Replace count=1024 with the size of the swapfile you need in megabytes:

# dd if=/dev/zero of=/home/user/swapfile bs=1m count=1024
1024+0 records in
1024+0 records out
1073741824 bytes transferred in 14.092 secs (76192760 bytes/sec)

Then provide the path to swapctl:

# swapctl -a /home/user/swapfile

You can verify the swap file is now being used:

# swapctl -l
Device              512-blocks     Used    Avail Capacity  Priority
/dev/sd0b              2109020        0  2109020     0%    0
/home/user/swapfile    2097152        0  2097152     0%    0
Total                  6310623        0  6310623     0%