Adding and Managing Fedora Swap Space

PreviousTable of Contents
Adding a New Disk to a Fedora Volume Group and Logical Volume

Take your Fedora Linux Skills to the Next Level
Fedora 31 Essentials book is now available in Print ($36.99) and eBook ($24.99) editions. Learn more...

Buy Print Preview Book

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

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 Fedora

The amount of swap recommended for Fedora 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 Fedora 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 a Fedora system may be identified in a number of ways. One option is to cat the /proc/swaps file:

# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       2031608 0       -1

Alternatively, use the swapon command:

# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       2031608 0       -1

Finally, the free command may also be used:

# free
             total       used       free     shared    buffers     cached
Mem:       1027220     969300      57920          0      57072     665576
-/+ buffers/cache:     246652     780568
Swap:      2031608          0    2031608

Adding a Swap File to a Fedora System

Additional swap may be added to the 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):

# 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

Configure the file as swap:

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

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

# swapon /newswap

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

/newswap    swap    swap   defaults 0 0

Once the swap space has been activated, verify that it is in use using the swapon –s command:

# swapon -s
Filename                                Type            Size          Used    Priority
/dev/dm-1                               partition       2031608 0       -1
/newswap                                file            127992  0       -2

De-activate the additional swap space at any time using the swapoff command as follows:

# swapoff /newswap

Adding Swap to a Fedora LVM Swap Volume

By default, Fedora 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 that swap space (for more information on LVM and how to add additional space, refer to the chapter entitled Adding a New Disk to a Fedora Volume Group and Logical Volume):

# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg_fed2/lv_root
  VG Name                vg_fed2
  LV UUID                XZ1Da9-1OdZ-em3Z-4Dfo-TqZl-V6WI-an3Ahq
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                13.56 GiB
  Current LE             434
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Name                /dev/vg_fed2/lv_swap
  VG Name                vg_fed2
  LV UUID                94ltyQ-aGe2-271U-tiZP-Xcuo-x8wm-E0rY5Z
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                1.94 GiB
  Current LE             62
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

Clearly the swap resides on logical volume /dev/vg_fed2/lv_swap which is part of volume group vg_fed2. 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
  vg_fed2   2   2   0 wz--n- 63.84G 992.00M

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:

# swapoff /dev/vg_fed2/lv_swap
# lvextend -L +900M /dev/vg_fed2/lv_swap
  Rounding up size to full physical extent 928.00 MB
  Extending logical volume LogVol01 to 2.88 GB
  Logical volume LogVol01 successfully resized

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

# mkswap /dev/vg_fed2/lv_swap
Setting up swapspace version 1, size = 3087003 kB

# swapon /dev/vg_fed2/lv_swap

Having made the changes check that the swap space has increased using the swapon –s command.

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:

# vgs
  VG         #PV #LV #SN Attr   VSize  VFree
  vg_fed2      1   2   0 wz--n- 15.50g    0

The above output indicates that no space 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/sdb is available for addition to the volume group. The first step is to turn this partition into a physical volume:

# pvcreate –ff /dev/sdb
  Physical volume "/dev/sdb" successfully created

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

# vgextend vg_fed2 /dev/sdb
  Volume group "vg_fed2" successfully extended

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

# vgs
  VG      #PV #LV #SN Attr   VSize  VFree
  vg_fed2   2   2   0 wz--n- 17.47g 1.97g

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:

# swapoff /dev/vg_fed2/lv_swap

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

# lvextend -L+1.9GB /dev/vg_fed2/lv_swap
  Rounding up size to full physical extent 1.91 GiB
  Extending logical volume lv_swap to 3.84 GiB
  Logical volume lv_swap successfully resized

Re-create the swap on the logical volume:

# mkswap /dev/vg_fed2/lv_swap

Next, turn swap back on:

# swapon /dev/vg_fed2/lv_swap

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

# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       4030456 0       -1

Similarly, the lvdisplay command will list the lv_swap volume as containing 3.84GB of space as opposed to the 1.94GB listed prior to the extension of the volume group and logical volume.

Take your Fedora Linux Skills to the Next Level
Fedora 31 Essentials book is now available in Print ($36.99) and eBook ($24.99) editions. Learn more...

Buy Print Preview Book



PreviousTable of Contents
Adding a New Disk to a Fedora Volume Group and Logical Volume