Creating Swap Files on a Btrfs File System
Swap space is used in Oracle Linux when the amount of physical memory (RAM) is full. If the system needs more memory resources, and the RAM is full, inactive pages in memory are moved to the swap space. Although swap space is helpful for systems with a small amount of RAM, don't use swap space as a replacement for more RAM. You can allocate swap space to a dedicated swap partition, which is the recommended method. Or, you can use a swap file; or, you can combine the use of swap partitions and swap files.
Swap files in Btrfs are supported with the following limitations:
-
A swap file can't be on a snapshotted subvolume. Instead, we recommend that you create a subvolume on which to place the swap file.
-
Btrfs doesn't support swap files on file systems that span several devices.
The following are step-by-step instructions for creating a swap file in Btrfs. Before creating the new swap file, calculate the size of the swap file in MB. Then, multiply that number by 1024 to find the number of blocks the file requires. For example, the block size of a 64 MB swap file is 65536.
-
Create an empty file, for example:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=65536
-
Set up the swap file by running the following command:
sudo mkswap /swapfile
-
Change the permissions on the file so that it's not world readable:
sudo chmod 0600 /swapfile
-
Enable the swap file at boot time by editing the
/etc/fstab
file as theroot
user to include the following entry:/swapfile swap swap defaults 0 0
-
Regenerate the mount units and register the new configuration in the
/etc/fstab
file:sudo systemctl daemon-reload
-
Activate the new swap file:
sudo swapon /swapfile
Running the previous command activates the new swap file immediately.
Or, you can run the following command to test whether the new swap file was successfully created by inspecting the active swap space:
sudo cat /proc/swaps
sudo free -h