Adding and Managing AlmaLinux 9 Swap Space

An essential part of maintaining the performance of an AlmaLinux 9 system involves ensuring that adequate swap space is available comparable to the memory demands placed on the system.

Therefore, this chapter provides an overview of swap management on AlmaLinux 9.

What is Swap Space?

Computer systems have a finite amount of physical memory available to the operating system. When the operating system approaches the available memory limit, it frees up space by writing memory pages to disk. When the operating system requires any of those pages, they are read back into memory. The disk area allocated for this task is referred to as swap space.

Recommended Swap Space for AlmaLinux 9

The swap recommended for AlmaLinux 9 depends on several factors, including the amount of memory in the system, the workload imposed on that memory, and whether the system is required to support hibernation. The current guidelines for AlmaLinux 9 swap space are as follows:

Amount of installed RAM

Recommended swap space

Recommended swap space if hibernation enabled

2GB or less

Installed RAM x 2

Installed RAM x 3

2GB – 8GB

Installed RAM x 1

Installed RAM x 2

8GB – 64GB

At least 4GB

Installed RAM x 1.5

64GB or more

At least 4GB

Hibernation not recommended

Table 33-1

When a system enters hibernation, the current system state is written to the hard disk, and the host machine is powered off. When the machine is subsequently powered on, the system’s state is restored from the hard disk drive. This differs from suspension, where the system state is stored in RAM. The machine then enters a sleep state whereby power is maintained to the system RAM while other devices are shut down.

 

You are reading a sample chapter from AlmaLinux 9 Essentials. Buy the full book now in eBook format.

Full book includes 34 chapters and 290 pages. Learn more.

Preview  Buy eBook 

 

Identifying Current Swap Space Usage

The current amount of swap used by an AlmaLinux 9 system may be identified in several ways. One option is to output the /proc/swaps file:

# cat /proc/swaps
Filename	Type		Size		Used		Priority
/dev/dm-1    partition	3932156	0		-2Code language: plaintext (plaintext)

Alternatively, the swapon command may be used:

# swapon
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition 3.7G   0B   -2Code language: plaintext (plaintext)

To view the amount of swap space relative to the overall available RAM, the free command may be used:

# free
               total        used        free      shared  buff/cache   available
Mem:         3601420     1577696     1396172      404412     1273236     2023724
Swap:        3932156           0     3932156Code language: plaintext (plaintext)

Adding a Swap File to an AlmaLinux 9 System

Additional swap space may be added to the system by creating a file and assigning it as swap. Begin by creating the swap file using the dd command. The size of the file can be changed by adjusting the count variable. The following command line, for example, creates a 2.0 GB file:

# dd if=/dev/zero of=/newswap bs=1024 count=2000000
2000000+0 records in
2000000+0 records out
2048000000 bytes (2.0 GB, 1.9 GiB) copied, 29.3601 s, 69.8 MB/s
Code language: plaintext (plaintext)

Before converting the file to a swap file, it is essential to make sure the file has secure permissions set:

 

You are reading a sample chapter from AlmaLinux 9 Essentials. Buy the full book now in eBook format.

Full book includes 34 chapters and 290 pages. Learn more.

Preview  Buy eBook 

 

# chmod 0600 /newswapCode language: plaintext (plaintext)

Once a suitable file has been created, it needs to be converted into a swap file using the mkswap command:

# mkswap /newswap
Setting up swapspace version 1, size = 1.9 GiB (2047995904 bytes)
no label, UUID=28d314e9-492f-46f8-bdcf-3a734c4426db5Code language: plaintext (plaintext)

With the swap file created and configured, it can be added to the system in real-time using the swapon utility:

# swapon /newswapCode language: plaintext (plaintext)

Re-running swapon should now report that the new file is now being used as swap:

# swapon
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition 3.7G   1M   -2
/newswap  file       1.9G   0B   -3Code language: plaintext (plaintext)

The swap space may be removed dynamically by using the swapoff utility as follows:

# swapoff /newswapCode language: plaintext (plaintext)

Finally, modify the /etc/fstab file to automatically add the new swap at system boot time by adding the following line:

 

You are reading a sample chapter from AlmaLinux 9 Essentials. Buy the full book now in eBook format.

Full book includes 34 chapters and 290 pages. Learn more.

Preview  Buy eBook 

 

/newswap swap swap defaults 0 0Code language: plaintext (plaintext)

Adding Swap as a Partition

As an alternative to designating a file as swap space, entire disk partitions may also be designated as swap. The steps to achieve this are the same as those for adding a swap file. Before allocating a partition to swap, ensure that any existing data on the corresponding filesystem is either backed up or no longer needed and that the filesystem has been unmounted.

Assuming that a partition exists on a disk drive represented by /dev/sdb1, for example, the first step would be to convert this into a swap partition, once again using the mkswap utility:

# mkswap /dev/sdb1
Setting up swapspace version 1, size = 14.5 GiB (15524163584 bytes)
no label, UUID=306b7fee-eb20-4679-9f14-f94548683557Code language: plaintext (plaintext)

Next, add the new partition to the system swap and verify that it has indeed been added:

# swapon /dev/sdb1
# swapon
NAME      TYPE       SIZE USED PRIO
/dev/dm-1 partition  3.7G 996K   -2
/dev/sdb1 partition 14.5G   0B   -3Code language: plaintext (plaintext)

Once again, the /etc/fstab file may be modified to automatically add the swap partition at boot time as follows:

/dev/sdb1 swap swap defaults 0 0Code language: plaintext (plaintext)

Adding Space to an AlmaLinux 9 LVM Swap Volume

On systems using Logical Volume Management, an alternative to adding swap via file or disk partition is to extend the logical volume used for the swap space.

 

You are reading a sample chapter from AlmaLinux 9 Essentials. Buy the full book now in eBook format.

Full book includes 34 chapters and 290 pages. Learn more.

Preview  Buy eBook 

 

The first step is to identify the current amount of swap available and the volume group and logical volume used for the swap space using the lvdisplay utility (for more information on LVM, refer to the chapter entitled “Adding a New Disk to a AlmaLinux 9 Volume Group and Logical Volume”):

# lvdisplay
  --- Logical volume ---
  LV Path                /dev/almalinux/swap
  LV Name                swap
  VG Name                almalinux
  LV UUID                MlUJL2-OLJB-0bIV-CeWs-Qwel-wUdT-0nmOv0
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2023-08-13 14:06:53 -0500
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
.
.
Code language: plaintext (plaintext)

Clearly, the swap resides on a logical volume named swap which is part of the volume group named almalinux. The next step is to verify if there is any space available on the volume group that can be allocated to the swap volume:

# vgs
  VG          #PV #LV #SN Attr   VSize   VFree 
  almalinux   2   2   0 wz--n- <38.41g <6.00g
Code language: plaintext (plaintext)

If the amount of space available is sufficient to meet additional swap requirements, turn off the swap and extend the swap logical volume to use as much of the available space as needed to meet the system’s swap requirements:

# swapoff /dev/almalinux/swap
# lvextend -L+8GB /dev/almalinux/swap
    Logical volume almalinux/swap successfully resized.Code language: plaintext (plaintext)

Next, reformat the swap volume and turn the swap back on:

# mkswap /dev/almalinux/swap
mkswap: /dev/almalinux/swap: warning: wiping old swap signature.
Setting up swapspace version 1, size = 12 GiB (12754874368 bytes)
no label, UUID=241a4818-e51c-4b8c-9bc9-1697fc2ce26e
 
# swapon /dev/almalinux/swapCode language: plaintext (plaintext)

Having made the changes, check that the swap space has increased:

 

You are reading a sample chapter from AlmaLinux 9 Essentials. Buy the full book now in eBook format.

Full book includes 34 chapters and 290 pages. Learn more.

Preview  Buy eBook 

 

# swapon
NAME      TYPE       SIZE USED PRIO
/dev/dm-1 partition  12G   0B   -2Code language: plaintext (plaintext)

Adding Swap Space to the Volume Group

In the above section, we extended the swap logical volume to use space already available in the volume group. If no space is available in the volume group, it must be added before extending the swap.

Begin by checking the status of the volume group:

# vgs
  VG               #PV #LV #SN Attr   VSize   VFree
  almalinux        1   3   0 wz--n- <297.09g    0
Code language: plaintext (plaintext)

The above output indicates that no space is available within the volume group. However, suppose we have a requirement to add 14 GB to the swap on the system. This will require the addition of more space to the volume group. For this example, it will be assumed that a disk that is 16 GB in size and represented by /dev/sdb is available for addition to the volume group. Therefore, the first step is to turn this partition into a physical volume using pvcreate:

# pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.
Code language: plaintext (plaintext)

If the creation fails with a message similar to “Device /dev/sdb excluded by a filter”, it may be necessary to wipe the disk before creating the physical volume:

# wipefs -a /dev/sdb
/dev/sdb: 8 bytes were erased at offset 0x00000200 (gpt): 45 46 49 20 50 41 52 54
/dev/sdb: 8 bytes were erased at offset 0x1fffffe00 (gpt): 45 46 49 20 50 41 52 54
/dev/sdb: 2 bytes were erased at offset 0x000001fe (PMBR): 55 aa
/dev/sdb: calling ioctl to re-read partition table: SuccessCode language: plaintext (plaintext)

Next, the volume group needs to be extended to use this additional physical volume:

 

You are reading a sample chapter from AlmaLinux 9 Essentials. Buy the full book now in eBook format.

Full book includes 34 chapters and 290 pages. Learn more.

Preview  Buy eBook 

 

# vgextend almalinux /dev/sdb
  Volume group "almalinux" successfully extended
Code language: plaintext (plaintext)

At this point, the vgs command should report the addition of space from /dev/sdb to the volume group:

# vgs
  VG             #PV #LV #SN Attr   VSize   VFree  
  almalinux      2   3   0   wz--n- 311.54g <14.46g
Code language: plaintext (plaintext)

Now that the additional space is available in the volume group, the swap logical volume may be extended to utilize the space. But first, turn off the swap using the swapoff utility:

# swapoff /dev/almalinux/swapCode language: plaintext (plaintext)

Next, extend the logical volume to use the new space:

# lvextend -L+14GB /dev/almalinux/swap
  Size of logical volume almalinux/swap changed from 3.75 GiB (960 extents) to 17.75 GiB (4544 extents).
  Logical volume almalinux/swap successfully resized.
Code language: plaintext (plaintext)

Re-create the swap on the logical volume:

# mkswap /dev/almalinux/swap
mkswap: /dev/almalinux/swap: warning: wiping old swap signature.
Setting up swapspace version 1, size = 11.9 GiB (12754874368 bytes)
no label, UUID=241a4818-e51c-4b8c-9bc9-1697fc2ce26e
Code language: plaintext (plaintext)

Next, turn swap back on:

 

You are reading a sample chapter from AlmaLinux 9 Essentials. Buy the full book now in eBook format.

Full book includes 34 chapters and 290 pages. Learn more.

Preview  Buy eBook 

 

# swapon /dev/almalinux/swapCode language: plaintext (plaintext)

Finally, use the swapon command to verify the addition of the swap space to the system:

# swapon
NAME      TYPE       SIZE USED PRIO
/dev/dm-1 partition 17.7G   0B   -2Code language: plaintext (plaintext)

Summary

Swap space is vital to any operating system when memory resources become constrained. By swapping out memory areas to disk, the system can continue to function and meet the needs of the processes and applications running on it.

AlmaLinux 9 has a set of guidelines recommending the amount of disk-based swap space that should be allocated depending on the amount of RAM installed in the system. When these recommendations prove insufficient, additional swap space can be added to the system, typically without rebooting. This chapter outlines that swap space can be added as a file, disk, or disk partition or by extending existing logical volumes configured as swap space.


Categories