How to create/add swap space using a storage device?

Solution Verified - Updated

Environment

  • Red Hat Enterprise Linux (RHEL) 5, 6, 7, 8, 9, 10

Issue

  • How to create swap space after installing the OS?
  • Is it possible to create swap space on SAN disks using lvm ?
  • Is it possible to create swap space on LVM disk in Red Hat Enterprise Linux?
  • How to create swap space on LVM partition after installation of Red Hat Enterprise Linux?
  • How to add additional swap device after OS installation.

Resolution

  • The procedure to create swap space after installation on SAN LUNs is similar to that on internal disk. It is possible to create swap space on a SAN disk using lvm.
    1. prepare the swap device, if needed
    2. create (format) the swap device
    3. add the swap device to /etc/fstab
    4. activate the swap device
    5. verify activated swap includes device
Note  If you have trouble creating and adding a swap device, capture the following diagnostics to submit to Red Hat along with a support case:
# hexdump -n 65536 -C <swap-device-name>  > hexdump.log  # for example /dev/mapper/mpath22p1 
# strace -v swapon -va &> swapon.log
# cat /etc/fstab &> fstab.log

Refer following steps to create swap space on the LVM partition, assuming for example that /dev/VolGroup00/LogVol02 is the swap volume to add:

  1. Create the LVM logical volume, for example with 1GB (-L 1G). VolGroup00 is the vg name in which the LV LogVol02 is being created:

    # lvcreate VolGroup00 -n LogVol02 -L 1G
    
  2. Format the new swap space:

    # mkswap /dev/VolGroup00/LogVol02
    
  3. Add the following entry to the /etc/fstab file:

    /dev/VolGroup00/LogVol02 swap swap defaults 0 0
    
  4. Enable the extended logical volume as swap:

    # swapon -va
    
  5. To test if the swap space was successfully created, execute one of the following commands:

       # swapon -s
    
       # cat /proc/swaps
            
        or
            
       # free
    

The steps for using a physical device like sdX or multipath device is mostly the same steps.

  • Refer following steps to create swap space on a device or device's partition, assuming for example that /dev/mapper/mpath22p1 is the swap device to be added. If the device was a local device /dev/sdx or a partition /dev/sdx1 then just substitute that for /dev/mapper/mpath22p1.
  1. Verify the device, partition and/or multipath device is present within the configuration and not used.
     
  2. Format the new `swap` space:
    # mkswap /dev/mapper/mpath22p1
  3. Add the following entry to the `/etc/fstab` file:
    Note  If the device added to fstab is a sdX path under a multipath device, then attempting to activate with swapon will fail with 'invalid device' error -- you must specify the multipath device name, or its dm- equivalent, if adding swap using a multipath device.
    /dev/mapper/mpath22p1 swap swap defaults 0 0
  4. Enable the device as swap:
    # swapon -va
  5. To test if the swap space was successfully created, execute one of the following commands:
    # swapon -s
    # cat /proc/swaps
     
    or
     
    # free

Additional References

Although the following are located within the RHEL 8 documentation set, these steps are still valid for RHEL prior releases back to RHEL 5. For example, compare RHEL 5 with RHEL 8 for "Creating an LVM2 logical volume for swap".

Similar for RHEL-9:

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.