No matter how well you plan, sometimes a system does not run as you expect. If you find you need more swap space, it is simple enough to add. You have three ways to increase swap space: adding a new hard drive, enabling swap over NFS, and creating a swap file on an existing partition.
For information on how to encrypt swap space, what options for this task exist and why it should be done, please refer to Section 19.17 of the Handbook.
Adding a new hard drive for swap gives better performance than adding a partition on an existing drive. Setting up partitions and hard drives is explained in Section 19.3. Section 12.2 discusses partition layouts and swap partition size considerations.
Use swapon(8) to add a swap partition to the system. For example:
To automatically add this swap partition on boot, add an entry to /etc/fstab for the partition:
/dev/ada1s1b none swap sw 0 0
See fstab(5) for an explanation of the entries in /etc/fstab.
Swapping over NFS is only recommended if you do not have a local hard disk to swap to; NFS swapping will be limited by the available network bandwidth and puts an additional burden on the NFS server.
You can create a file of a specified size to use as a swap file. In our example here we will use a 64MB file called /usr/swap0. You can use any name you want, of course.
Example 12-1. Creating a Swapfile on FreeBSD
The GENERIC kernel already includes the memory disk driver (md(4)) required for this operation. When building a custom kernel, make sure to include the following line in your custom configuration file:
For information on building your own kernel, please refer to Chapter 9.
Create a swapfile (/usr/swap0):
# dd if=/dev/zero of=/usr/swap0 bs=1024k count=64
Set proper permissions on (/usr/swap0):
# chmod 0600 /usr/swap0
Enable the swap file in /etc/rc.conf:
swapfile="/usr/swap0" # Set to name of swapfile if aux swapfile desired.
Reboot the machine or to enable the swap file immediately, type:
# mdconfig -a -t vnode -f /usr/swap0 -u 0 && swapon /dev/md0