Changes

Adding and Managing RHEL Swap Space

12,215 bytes added, 14:04, 11 June 2019
Created page with "{{#pagetitle: Adding and Managing RHEL 8 Swap Space }} <table border="0" cellspacing="0" width="100%"><tr> <td width="20%">Adding Storage to a RHEL Volume Group and Logical..."
{{#pagetitle: Adding and Managing RHEL 8 Swap Space }}
<table border="0" cellspacing="0" width="100%"><tr>
<td width="20%">[[Adding Storage to a RHEL Volume Group and Logical Volume|Previous]]<td align="center">[[Red Hat Enterprise Linux Essentials|Table of Contents]]<td width="20%" align="right"></td>
<tr>
<td width="20%">Adding Storage to a RHEL 8 Volume Group and Logical Volume<td align="center"><td width="20%" align="right"></td>
</table>
<hr>


<htmlet>rhel8</htmlet>


An important part of maintaining the performance of a RHEL 8 system involves ensuring that adequate swap space is available comparable to the memory demands placed on the system. The goal of this chapter, therefore, is to provide an overview of swap management on RHEL 8.

== What is Swap Space? ==

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

== Recommended Swap Space for RHEL 8 ==

The amount of swap recommended for RHEL 8 depends on a number of 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 RHEL 8 swap space are as follows:


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 state of the system is restored from the hard disk drive. This differs from suspension where the state of system 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.

== Identifying Current Swap Space Usage ==

The current amount of swap used by a RHEL 8 system may be identified in a number of ways. One option is to output the ''/proc/swaps'' file:

<pre>
# cat /proc/swaps
Filename Type Size Used Priority
/dev/dm-1 partition 4169724 41484 -2
</pre>

Alternatively, the ''swapon'' command may be used:

<pre>
# swapon
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 4G 40.5M -2
</pre>

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

<pre>
# free
total used free shared buff/cache available
Mem: 4035436 1428276 2224596 21968 382564 2360172
Swap: 4169724 41484 4128240
</pre>

== Adding a Swap File to a RHEL 8 System ==

Additional swap 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:

<pre>
# 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, 3.62697 s, 565 MB/s
</pre>

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

<pre>
# chmod 0600 /newswap
</pre>

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

<pre>
# mkswap /newswap
Setting up swapspace version 1, size = 1.9 GiB (2047995904 bytes)
no label, UUID=4ffc238d-7fde-4367-bd98-c5c46407e535
</pre>

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

<pre>
# swapon /newswap
</pre>

Rerunning swapon should now report that the new file is now being used as swap:

<pre>
# swapon
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 4G 0B -2
/newswap file 1.9G 0B -3
</pre>

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

<pre>
# swapoff /newswap
</pre>

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

<pre>
/newswap swap swap defaults 0 0
</pre>

== 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 largely the same as those for adding a swap file. Before allocating a partition to swap, however, make sure 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:

<pre>
# mkswap /dev/sdb1
mkswap: /dev/sdb1: warning: wiping old xfs signature.
Setting up swapspace version 1, size = 8 GiB (8587833344 bytes)
no label, UUID=a899c8ec-c410-4569-ba18-ddea03370c7f
</pre>

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

<pre>
# swapon /dev/sdb1
# swapon
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 4G 0B -2
/dev/sdb1 partition 8G 0B -3
</pre>

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

<pre>
/dev/sdb1 swap swap defaults 0 0
</pre>

== Adding Space to a RHEL 8 LVM Swap Volume ==

By default, RHEL 8 configures swap space using Logical Volume Management (LVM). An alternative to adding swap via file or disk partition, therefore, is to extend the logical volume used for the swap space.

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 RHEL Volume Group and Logical Volume|Adding a New Disk to a RHEL 8Volume Group and Logical Volume]]):

<pre>
# lvdisplay
--- Logical volume ---
LV Path /dev/rhel/swap
LV Name swap
VG Name rhel
LV UUID fwOZsF-ROwu-6eLe-2KDR-JZ0d-Pn4o-K5B0Vb
LV Write Access read/write
LV Creation host, time rhel80desktop, 2019-02-14 11:12:07 -0500
LV Status available
# open 2
LV Size &lt;3.98 GiB
Current LE 1018
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
.
.
</pre>

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

<pre>
# vgs
VG #PV #LV #SN Attr VSize VFree
rhel 2 3 0 wz--n- 197.66g &lt;22.00g
</pre>

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:

<pre>
# swapoff /dev/rhel/swap
# lvextend -L+8GB /dev/rhel/swap
Logical volume rhel/swap successfully resized.
</pre>

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

<pre>
# mkswap /dev/rhel/swap
mkswap: /dev/rhel/swap: warning: wiping old swap signature.
Setting up swapspace version 1, size = 12 GiB (12754874368 bytes)
no label, UUID=241a4818-e51c-4b8c-9bc9-1697fc2ce26e
&#160;
# swapon /dev/rhel/swap
</pre>

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

<pre>
# swapon
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 12G 0B -2
</pre>

== Adding Swap Space to the Volume Group ==

In the above section we extended the swap logical volume to use space that was already available in the volume group. If no space is available in the volume group then it will need to be added before the swap can be extended.

Begin by checking the status of the volume group:

<pre>
# vgs
VG #PV #LV #SN Attr VSize VFree
rhel 1 3 0 wz--n- &lt;189.67g 0
</pre>

The above output indicates that no space is available within the volume group. Suppose, however, that we have a requirement to add 8 GB to the swap on the system. Clearly, this will require the addition of more space to the volume group. For the purposes of this example it will be assumed that a disk that is 8 GB in size and represented by ''/dev/sdb'' is available for addition to the volume group. The first step is to turn this partition into a physical volume:

<pre>
# pvcreate /dev/sdb
Physical volume &quot;/dev/sdb&quot; successfully created.
</pre>

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:

<pre>
# 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: Success
</pre>

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

<pre>
# vgextend rhel /dev/sdb
Volume group &quot;rhel&quot; successfully extended
</pre>

At this point the ''vgs'' command should report the addition of the 8 GB of space to the volume group:

<pre>
# vgs
VG #PV #LV #SN Attr VSize VFree
rhel 2 3 0 wz--n- 197.66g &lt;8.00g
</pre>

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

<pre>
# swapoff /dev/rhel/swap
</pre>

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

<pre>
# lvextend -L+7.9GB /dev/rhel/swap
Rounding size to boundary between physical extents: 7.90 GiB.
Size of logical volume rhel/swap changed from &lt;3.98 GiB (1018 extents) to &lt;11.88 GiB (3041 extents).
Logical volume rhel/swap successfully resized.
</pre>

Re-create the swap on the logical volume:

<pre>
# mkswap /dev/rhel/swap
mkswap: /dev/rhel/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
</pre>

Next, turn swap back on:

<pre>
# swapon /dev/rhel/swap
</pre>

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

<pre>
# swapon
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 11.9G 0B -2
</pre>

== Summary ==

Swap space is a vital component of just about any operating system in terms of handling situations where memory resources become constrained. By swapping out areas of memory to disk, the system is able to continue to function and meet the needs of the processes and applications running on it.

RHEL 8 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. In situations where these recommendations prove to be insufficient, additional swap space can be added to the system, typically without the need to reboot. As outlined in this chapter, swap space can be added in the form of a file, disk or disk partition or by extending existing logical volumes that have been configured as swap space.


<htmlet>rhel8</htmlet>


<hr>
<table border="0" cellspacing="0" width="100%"><tr>
<td width="20%">[[Adding Storage to a RHEL Volume Group and Logical Volume|Previous]]<td align="center">[[Red Hat Enterprise Linux Essentials|Table of Contents]]<td width="20%" align="right"></td>
<tr>
<td width="20%">Adding Storage to a RHEL 8 Volume Group and Logical Volume<td align="center"><td width="20%" align="right"></td>
</table>