Despite careful planning, sometimes a system does not run as expected. If more swap space is needed, it is simple enough to add. There are three ways to increase swap space: add a new hard drive, enable swap over NFS, or create 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, refer to Section 19.17, “Encrypting Swap Space” 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, “Adding Disks”. Section 12.2, “Initial Configuration” discusses partition layouts and swap partition size considerations.
Use swapon(8) to add a swap partition to the system. For example:
# swapon /dev/ada1s1bIt is possible to use any partition not currently mounted, even if it already contains data. Using swapon(8) on a partition that contains data will overwrite and destroy that data. Make sure that the partition to be added as swap is really the intended partition before running swapon(8).
To automatically add this swap partition on boot, add an
entry to /etc/fstab for the
partition:
/dev/ada1s1b none swap sw 0 0See 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. The following example will create a 64MB file named
/usr/swap0.
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 the custom
configuration file:
For information on building a custom kernel, refer to Chapter 9, Configuring the FreeBSD Kernel.
Create a swapfile
(/usr/swap0):
# dd if=/dev/zero of=/usr/swap0 bs=1024k count=64Set proper permissions on
(/usr/swap0):
# chmod 0600 /usr/swap0Enable the swap file in
/etc/rc.conf:
Reboot the machine or to enable the swap file immediately, type:
# mdconfig -a -t vnode -f /usr/swap0 -u 0 && swapon /dev/md0This, and other documents, can be downloaded from ftp://ftp.FreeBSD.org/pub/FreeBSD/doc/
For questions about FreeBSD, read the
documentation before
contacting <questions@FreeBSD.org>.
For questions about this documentation, e-mail <doc@FreeBSD.org>.