swapon is used to specify devices on which paging and swapping are to take place.
The device or file used is given by the specialfile parameter. It may be of the form -L label or -U uuid to indicate a device by label or uuid.
Calls to swapon normally occur in the system boot scripts making all swap devices available, so that the paging and swapping activity is interleaved across several devices and files.
swapoff disables swapping on the specified devices and files. When the -a flag is given, swapping is disabled on all known swap devices and files (as found in /proc/swaps or /etc/fstab).
You should not use swapon on a file with holes. This can be seen in the system log as
swapon: swapfile has holes.
The swap file implementation in the kernel expects to be able to write to the file directly, without the assistance of the filesystem. This is a problem on preallocated files (e.g. fallocate
(1)) on filesystems like XFS
, and on copy-on-write filesystems like btrfs
It is recommended to use dd(1) and /dev/zero to avoid holes on XFS and ext4.
swapon may not work correctly when using a swap file with some versions of btrfs. This is due to btrfs being a copy-on-write filesystem: the file location may not be static and corruption can result. Btrfs actively disallows the use of swap files on its filesystems by refusing to map the file.
One possible workaround is to map the swap file to a loopback device. This will allow the filesystem to determine the mapping properly but may come with a performance impact.
Swap over NFS may not work.
swapon automatically detects and rewrites a swap space signature with old software suspend data (e.g S1SUSPEND, S2SUSPEND, ...). The problem is that if we don't do it, then we get data corruption the next time an attempt at unsuspending is made.
swapon(2), swapoff(2), fstab(5), init(8), mkswap(8), rc(8), mount(8)