Chapter 13. Getting started with swap
Chapter 13. Getting started with swap Use the swap space to provide temporary storage for inactive processes and data, and prevent out-of-memory errors when physical memory is full. The swap space acts as an extension to the physical memory and allows the system to continue running smoothly even when physical memory is exhausted. Note that using swap space can slow down system performance, so optimizing the use of physical memory, before relying on swap space, can be more favorable. 13.1. Overview of swap space Swap space in Linux is used 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. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory. Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files. In years past, the recommended amount of swap space increased linearly with the amount of RAM in the system. However, modern systems often include hundreds of gigabytes of RAM. As a consequence, recommended swap space is considered a function of system memory workload, not system memory. 13.2. Recommended system swap space The recommended size of a swap partition depends on the amount of RAM in your system and whether you want sufficient memory for your system to hibernate. The recommended swap partition size is set automatically during installation. To allow for hibernation, however, you need to edit the swap space in the custom partitioning stage. The following recommendations are especially important on systems with low memory, such as 1 GB or less. Failure to allocate sufficient swap space on these systems can cause issues, such as instability or even render the installed system unbootable. Table 13.1. Recommended swap space Amount of RAM in the system Recommended swap space Recommended swap space if allowing for hibernation ⩽ 2 GB 2 times the amount of RAM 3 times the amount of RAM > 2 GB - 8 GB Equal to the amount of RAM 2 times the amount of RAM > 8 GB - 64 GB At least 4 GB 1.5 times the amount of RAM > 64 GB At least 4 GB Hibernation not recommended For border values such as 2 GB, 8 GB, or 64 GB of system RAM, choose swap size based on your needs or preference. If your system resources allow for it, increasing the swap space can lead to better performance. Note that distributing swap space over multiple storage devices also improves swap space performance, particularly on systems with fast drives, controllers, and interfaces. Important File systems and LVM2 volumes assigned as swap space should not be in use when being modified. Any attempts to modify swap fail if a system process or the kernel is using swap space. Use the free and cat /proc/swaps commands to verify how much and where swap is in use. Resizing swap space requires temporarily removing it from the system. This can be problematic if running applications rely on the additional swap space and might run into low-memory situations. Preferably, perform swap resizing from rescue mode, see Debug boot options . When prompted to mount the file system, select Skip . 13.3. Creating an LVM2 logical volume for swap You can create an LVM2 logical volume for swap. Assuming /dev/VolGroup00/LogVol02 is the swap volume you want to add. Prerequisites You have enough disk space. Procedure Create the LVM2 logical volume of size 2 GB : Format the new swap space: Add the following entry to the /etc/fstab file: Regenerate mount units so that your system registers the new configuration: Activate swap on the logical volume: Verification To test if the swap logical volume was successfully created and activated, inspect active swap space by using the following command: 13.4. Creating a swap file You can create a swap file to create a temporary storage space on a solid-state drive or hard disk when the system runs low on memory. Prerequisites You have enough disk space. Procedure Determine the size of the new swap file in megabytes and multiply by 1024 to determine the number of blocks. For example, the block size of a 64 MB swap file is 65536. Create an empty file: Replace 65536 with the value equal to the required block size. Set up the swap file with the command: Change the security of the swap file so it is not world readable. Edit the /etc/fstab file with the following entries to enable the swap file at boot time: The time the system boots, it activates the new swap file. Regenerate mount units so that your system registers the new /etc/fstab configuration: Activate the swap file immediately: Verification To test if the new swap file was successfully created and activated, inspect active swap space by using the following command: 13.5. Creating a swap volume by using the storage RHEL system role This section provides an example Ansible playbook. This playbook applies the storage role to create a swap volume, if it does not exist, or to modify the swap volume, if it already exist, on a block device by using the default parameters. Prerequisites You have prepared the control node and the managed nodes You are logged in to the control node as a user who can run playbooks on the managed nodes. The account you use to connect to the managed nodes has sudo permissions on them. Procedure Create a playbook file, for example ~/playbook.yml , with the following content: --- - name: Create a disk device with swap hosts: managed-node-01.example.com roles: - rhel-system-roles.storage vars: storage_volumes: - name: swap_fs type: disk disks: - /dev/sdb size: 15 GiB fs_type: swap The volume name ( swap_fs in the example) is currently arbitrary. The storage role identifies the volume by the disk device listed under the disks: attribute. Validate the playbook syntax: Note that this command only validates the syntax and does not protect against a wrong but valid configuration. Run the playbook: Additional resources /usr/share/ansible/roles/rhel-system-roles.storage/README.md file /usr/share/doc/rhel-system-roles/storage/ directory 13.6. Extending swap on an LVM2 logical volume You can extend swap space on an existing LVM2 logical volume. Assuming /dev/VolGroup00/LogVol01 is the volume you want to extend by 2 GB . Prerequisites You have enough disk space. Procedure Disable swapping for the associated logical volume: Resize the LVM2 logical volume by 2 GB : Format the new swap space: Enable the extended logical volume: Verification To test if the swap logical volume was successfully extended and activated, inspect active swap space: 13.7. Reducing swap on an LVM2 logical volume You can reduce swap on an LVM2 logical volume. Assuming /dev/VolGroup00/LogVol01 is the volume you want to reduce. Procedure Disable swapping for the associated logical volume: Clean the swap signature: Reduce the LVM2 logical volume by 512 MB: Format the new swap space: Activate swap on the logical volume: Verification To test if the swap logical volume was successfully reduced, inspect active swap space by using the following command: 13.8. Removing an LVM2 logical volume for swap You can remove an LVM2 logical volume for swap. Assuming /dev/VolGroup00/LogVol02 is the swap volume you want to remove. Procedure Disable swapping for the associated logical volume: Remove the LVM2 logical volume: Remove the following associated entry from the /etc/fstab file: Regenerate mount units to register the new configuration: Verification Test if the logical volume was successfully removed, inspect active swap space by using the following command: 13.9. Removing a swap file You can remove a swap file. Procedure Disable the /swapfile swap file: Remove its entry from the /etc/fstab file accordingly. Regenerate mount units so that your system registers the new configuration: Remove the actual file:
lvcreate VolGroup00 -n LogVol02 -L 2G mkswap /dev/VolGroup00/LogVol02 /dev/VolGroup00/LogVol02 none swap defaults 0 0 systemctl daemon-reload swapon -v /dev/VolGroup00/LogVol02 cat /proc/swaps total used free shared buff/cache available Mem: 30Gi 1.2Gi 28Gi 12Mi 994Mi 28Gi Swap: 22Gi 0B 22Gi free -h total used free shared buff/cache available Mem: 30Gi 1.2Gi 28Gi 12Mi 995Mi 28Gi Swap: 17Gi 0B 17Gi dd if=/dev/zero of=/swapfile bs=1024 count= 65536 mkswap /swapfile chmod 0600 /swapfile /swapfile none swap defaults 0 0 systemctl daemon-reload swapon /swapfile cat /proc/swaps free -h --- - name: Create a disk device with swap hosts: managed-node-01.example.com roles: - rhel-system-roles.storage vars: storage_volumes: - name: swap_fs type: disk disks: - /dev/sdb size: 15 GiB fs_type: swap ansible-playbook --syntax-check ~/playbook.yml ansible-playbook ~/playbook.yml swapoff -v /dev/VolGroup00/LogVol01 lvresize /dev/VolGroup00/LogVol01 -L +2G mkswap /dev/VolGroup00/LogVol01 swapon -v /dev/VolGroup00/LogVol01 cat /proc/swaps Filename Type Size Used Priority /dev/dm-1 partition 16322556 0 -2 /dev/dm-4 partition 7340028 0 -3 free -h total used free shared buff/cache available Mem: 30Gi 1.2Gi 28Gi 12Mi 994Mi 28Gi Swap: 22Gi 0B 22Gi swapoff -v /dev/VolGroup00/LogVol01 wipefs -a /dev/VolGroup00/LogVol01 lvreduce /dev/VolGroup00/LogVol01 -L -512M mkswap /dev/VolGroup00/LogVol01 swapon -v /dev/VolGroup00/LogVol01 cat /proc/swaps free -h swapoff -v /dev/VolGroup00/LogVol02 lvremove /dev/VolGroup00/LogVol02 /dev/VolGroup00/LogVol02 none swap defaults 0 0 systemctl daemon-reload cat /proc/swaps free -h swapoff -v /swapfile systemctl daemon-reload rm /swapfile