Changes

Jump to: navigation, search

Adding and Managing RHEL 5 Swap Space

6,970 bytes added, 19:17, 9 August 2010
New page: An important part of maintaining the performance of a Red hat Enterprise Linux 5 system involves ensuring that adequate swap space is available comparable to memory demands placed on the s...
An important part of maintaining the performance of a Red hat Enterprise Linux 5 system involves ensuring that adequate swap space is available comparable to memory demands placed on the system. The goal of this chapter, therefore, is to provide an overview of swap management on RHEL 5.

== 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 5 ==

The amount of swap recommended for RHEL 5 depends on a number of factors including the amount of memory in the system and the workload imposed on that memory. The current guidelines for Red Hat Enterprise Linux 5 swap space are as follows:

* 4GB of RAM requires a minimum of 2GB of swap space
* 4GB to 16GB RAM requires a minimum of 4GB of swap space
* 16GB to 64GB of RAM requires a minimum of 8GB of swap space
* 64GB to 256GB of RAM requires a minimum of 16GB of swap space

== Identifying Current Swap Space Usage ==

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

<pre>
# cat /proc/swaps
Filename Type Size Used Priority
/dev/mapper/VolGroup00-LogVol01 partition 5144568 0 -1
</pre>

Alternatively, use the swapon command:

<pre>
# swapon -s
Filename Type Size Used Priority
/dev/mapper/VolGroup00-LogVol01 partition 5144568 0 -1
</pre>

Finally, the free command may also be used:

<pre>
# free
total used free shared buffers cached
Mem: 1999872 1533140 466732 0 267444 786488
-/+ buffers/cache: 479208 1520664
Swap: 5144568 0 5144568
</pre>

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

Additional swap may be added to system by creating a file and assigning it as swap. This is achieved as follows.
Create the swap file using the dd command (the size can be changed by adjusting the count= variable; the following creates a 131MB file):

<pre>
# dd if=/dev/zero of=/newswap bs=1024 count=128000
128000+0 records in
128000+0 records out
131072000 bytes (131 MB) copied, 1.7639 seconds, 74.3 MB/s
</pre>

Configure the file as swap:

<pre>
# mkswap /newswap
Setting up swapspace version 1, size = 131067 kB
</pre>

Add the swap file to the system in real-time:

<pre>
# swapon /newswap
</pre>

Finally, modify the /etc/fstab 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 to an RHEL LVM Swap Volume ==

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

The first step is to identify the current amount of swap available and the volume group and logical volume used for the swap space (for more information on LVM, refer to the chapter entitled [[Adding a New Disk Drive to an RHEL 5 System]]):

<pre>
# swapon –s
Filename Type Size Used Priority
/dev/mapper/VolGroup00-LogVol01 partition 2064376 0 -1
</pre>

Clearly the swap resides on logical volume LogVol-01 which in part of volume group VolGroup00. 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
VolGroup00 2 2 0 wz--n- 63.84G 992.00M
</pre>

If the amount of space available is sufficient to meet additional swap requirements, turn off the swap and extend the volume group to use the additional space:

<pre>
# lvextend -L +900M /dev/VolGroup00/LogVol01
Rounding up size to full physical extent 928.00 MB
Extending logical volume LogVol01 to 2.88 GB
Logical volume LogVol01 successfully resized
</pre>

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

<pre>
# mkswap /dev/VolGroup00/LogVol01
Setting up swapspace version 1, size = 3087003 kB
# swapon /dev/VolGroup00/LogVol01
</pre>

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

<pre>
# swapon -s
Filename Type Size Used Priority
/dev/mapper/VolGroup00-LogVol01 partition 3014648 0 -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
VolGroup00 2 2 0 wz--n- 63.84G 64.00M
</pre>

The above output indicates that only 64MB is available within the volume group. Suppose, however, that we have a requirement to add 2GB 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 partition that is 2GB is size and represented by /dev/sdc is available for addition to the volume group. The first step is to turn this partition into a physical volume:

<pre>
# pvcreate /dev/sdc
Physical volume "/dev/sdc" successfully created
</pre>

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

<pre>
# vgextend VolGroup00 /dev/sdc
Volume group "VolGroup00" successfully extended
</pre>

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

<pre>
# vgs
VG #PV #LV #SN Attr VSize VFree
VolGroup00 3 2 0 wz--n- 65.81G 2.03G
</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:

<pre>
# swapoff /dev/VolGroup00/LogVol01
</pre>

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

<pre>
# lvextend -L+2GB /dev/VolGroup00/LogVol01
Extending logical volume LogVol01 to 4.88 GB
Logical volume LogVol01 successfully resized
</pre>

Re-create the swap on the logical volume:

<pre>
# mkswap /dev/VolGroup00/LogVol01
</pre>

Next, turn swap back on:

<pre>
# swapon /dev/VolGroup00/LogVol01
</pre>

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

<pre>
# swapon -s
Filename Type Size Used Priority
/dev/mapper/VolGroup00-LogVol01 partition 5111800 0 -2
</pre>

Navigation menu