How to create/add swap space using a storage device?
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
swapspace on SAN disks usinglvm? - 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.
- Exceptions:
- For creating a filesystem file for swap:
- Follow the directions in: "How do I add swap using a filesystem file to my Red Hat Enterprise Linux system?"
Resolution
- The procedure to create swap space after installation on SAN LUNs is similar to that on internal disk. It is possible to create
swapspace on a SAN disk usinglvm.- prepare the swap device, if needed
- create (format) the swap device
- add the swap device to /etc/fstab
- activate the swap device
- 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:
-
Create the
LVMlogical volume, for example with 1GB (-L 1G).VolGroup00is the vg name in which the LVLogVol02is being created:# lvcreate VolGroup00 -n LogVol02 -L 1G -
Format the new
swapspace:# mkswap /dev/VolGroup00/LogVol02 -
Add the following entry to the
/etc/fstabfile:/dev/VolGroup00/LogVol02 swap swap defaults 0 0 -
Enable the extended logical volume as swap:
# swapon -va -
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
swapspace on a device or device's partition, assuming for example that/dev/mapper/mpath22p1is theswapdevice to be added. If the device was a local device/dev/sdxor a partition/dev/sdx1then just substitute that for/dev/mapper/mpath22p1.
- Verify the device, partition and/or multipath device is present within the configuration and not used.
- Format the new `swap` space:
# mkswap /dev/mapper/mpath22p1- 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 withswaponwill 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- Enable the device as swap:
# swapon -va- 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".
- "Creating an LVM2 logical volume for swap"
- "Extending swap on an LVM2 logical volume"
- "Creating a swap file (on a filesystem)"
Similar for RHEL-9:
- Extending swap on an LVM2 logical volume
- Creating an LVM2 logical volume for swap
- Creating a swap file
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.