From Gentoo Wiki
(Redirected from Swap file)
Jump to:navigation Jump to:search
This article has some todo items:
  • swapiness, remove swap files

In the Linux/Unix world, the term swap is generally used as a synonym for memory paging. Swap refers to both the act of moving memory pages between RAM and disk and the allocated space on the disk itself.

Linux can use any combination of swap partition and/or swap files together; however swap space may not be necessary depending on the necessary requirements for the system in question. For example, a laptop that will be suspending to disk (hibernation) requires all pages in memory to be stored to disk, so swap is necessary in this case. Server systems equipped with large amount of memory and running at a constant load might not require swap at all. For further details, see the dedicated Knowledge Base article.

Swap partition

As best practice, the Gentoo Handbook recommends, as part of the installation process, creating a swap partition with a size of twice the available system memory[1].

Swap partitions can be created and activated at any time as long as partitions are available and formatted correctly.


Presuming /dev/sda2 is the partition available to be used for swap:

root #mkswap /dev/sda2 # Format the partition for swap.
root #swapon /dev/sda2 # # Activate the swap partition.

Review the activated swaps with the swapon command:

root #swapon --show

To avoid manually activating the swap file across reboots, append a line (adjusting the path as necessary) to fstab:

FILE /etc/fstab
/dev/sda2 none swap sw 0 0

Swap files

In order to work around the more ridged constraints of disk partitions, an alternative is to use swap as an on-disk file. Files have the ability to be located inside disk partitions. This allows the system administrator the flexibility to resize or move the swap space as necessary to meet the demands of the system without having to open a partitioning tool.


The first step in creating a swap file is to allocate a file of the target swap size. Several standard utilities can be used for this purpose. fallocate (part of sys-apps/util-linux) will suffice for this example

root #fallocate -l 12GiB 12G-swapfile # Create a file.
root #chmod 600 12G-swapfile # Restrict security on the file to root access only.
root #mkswap 12G-swapfile # Format the file swap.
root #swapon 12G-swapfile # Activate the swap file.

Review the system swaps with the swapon command:

root #swapon --show

To avoid manually activating the swap file across reboots, append a line (adjusting the path as necessary) to fstab:

FILE /etc/fstab
/12G-swapfile none swap sw 0 0


When using btrfs, set required attributes on the swap file disabling copy-on-write and compression. [2]

root # cd /path/to/swapfile
root # truncate -s 0 ./swapfile
root # chattr +C ./swapfile
root # btrfs property set ./swapfile compression none

OpenRC configuration

When using swap files which are not on the root filesystem, the service ordering in OpenRC should be changed via /etc/conf.d/swap:

FILE /etc/conf.d/swap
# If you are only using local swap partitions, you should not change
# this file. Otherwise, you need to uncomment the below rc_before line
# followed by the appropriate rc_need line.
# If you are using swap files stored on local file systems, uncomment
# this line.
# If you are using swap files stored on network file systems or swap
# partitions stored on network block devices such as iSCSI, uncomment
# this line.

See also

  • Filesystem — a means to organize data expected to be retained after a program terminates by providing procedures to store, retrieve, and update data as well as manage the available space on the device(s) which contain it.
  • Zram — a Linux kernel feature and userspace tools for creating compressible RAM-based block devices.
  • Zswap — a lightweight compressed cache for swap pages.

External resources