How to Extend Swap Space on an LVM Disk in Linux
How to Extend Swap Space on an LVM Disk in Linux
Linux System Administration / LVM Guide
This guide explains how to safely extend swap space when swap is configured on an LVM logical volume. It covers how to identify the current swap configuration, check available space in the Volume Group, and prepare the system before resizing the swap logical volume.
Introduction
Swap space is an area of disk storage that Linux can use when physical RAM becomes constrained. It provides additional virtual memory capacity, although it is significantly slower than RAM because it resides on storage.
On Linux servers running Oracle Database, Oracle E-Business Suite, application servers, middleware, or other memory-intensive workloads, insufficient swap space can become an operational concern.
If the server is using LVM and the swap area is configured on an LVM logical volume, the swap logical volume can be extended without repartitioning the entire disk.
The general process is:
Check current swap
↓
Identify swap Logical Volume
↓
Check Volume Group free space
↓
Disable swap
↓
Extend Logical Volume
↓
Reinitialize swap
↓
Enable swap
↓
Verify new swap size
Red Hat documents essentially this procedure for extending swap on an existing LVM logical volume: disable swap, resize the LV, initialize the swap area, enable it again, and verify the resulting size.
What Is Swap Space?
Swap is disk-backed storage that Linux can use as an extension of virtual memory.
When physical RAM is under memory pressure, the Linux kernel may move less-active memory pages from RAM to swap. This frees RAM for other workloads.
For example, a server might have:
| Resource | Example |
|---|---|
| Physical RAM | 16 GB |
| Current Swap | 4 GB |
| Required Swap | 8 GB |
| Additional Swap Required | 4 GB |
If the existing swap is an LVM logical volume and the Volume Group has sufficient unallocated space, the swap LV can be extended by the required amount.
Why Would You Need to Extend Swap?
There are several reasons an administrator may need additional swap space.
- The server has insufficient virtual memory capacity for its workload.
- A new application requires additional memory.
- Oracle Database or another enterprise application has increased its memory requirements.
- The server experiences memory pressure during peak workloads.
- The original swap allocation was too small.
- A system migration or application upgrade introduced higher memory requirements.
Increasing swap is not a substitute for adding sufficient physical RAM. Heavy swap activity can significantly degrade system performance because storage is much slower than RAM.
Understanding LVM Swap Layout
Before extending swap, it is important to understand how the storage is organized.
A typical LVM configuration looks like this:
Physical Disk
|
+---- Physical Volume (PV)
|
+---- Volume Group (VG)
|
+---- Logical Volume (LV)
|
+---- Swap
For example:
/dev/sda
|
+-- /dev/sda2
|
+-- Physical Volume
|
+-- VolGroup00
|
+-- LogVol00 → /
|
+-- LogVol01 → swap
|
+-- Free Space
In this example, the swap area is located on:
/dev/VolGroup00/LogVol01
If the Volume Group contains enough free extents, the swap logical volume can be extended directly.
LVM Terminology You Should Know
Physical Volume — PV
A Physical Volume is a disk or disk partition initialized for use by LVM.
Examples:
/dev/sda2 /dev/sdb /dev/mapper/mpath0
Volume Group — VG
A Volume Group is a pool of storage created from one or more Physical Volumes.
For example:
VolGroup00
Logical Volumes are allocated from this storage pool.
Logical Volume — LV
A Logical Volume is a virtual block device created inside a Volume Group.
For example:
/dev/VolGroup00/LogVol01
The logical volume may be used as:
- Root filesystem
- Application filesystem
- Database filesystem
- Swap
- Other block-storage purposes
Prerequisites
Before modifying swap, make sure you have:
- Root or equivalent administrative privileges.
- An LVM logical volume currently being used for swap.
- Sufficient free space in the relevant Volume Group.
- A maintenance window or a low-risk period for temporarily disabling swap.
- A recent backup or recovery plan for production systems.
The most important prerequisite is sufficient free space in the Volume Group. Red Hat's documented LVM procedure for extending swap assumes that the Volume Group has enough available storage.
Check the Current Swap Size
The first step is to determine how much swap is currently configured.
Run:
free -h
Example:
total used free shared buff/cache available Mem: 15Gi 4.2Gi 1.1Gi 512Mi 9.7Gi 10Gi Swap: 4Gi 0B 4Gi
The important line is:
Swap: 4Gi 0B 4Gi
This indicates that approximately 4 GB of swap is currently available.
Check Active Swap Devices
Use:
swapon --show
On many Linux distributions you can also use:
cat /proc/swaps
Example:
NAME TYPE SIZE USED PRIO /dev/mapper/VolGroup00-swap partition 4G 0B -2
This confirms that swap is currently active on an LVM device.
The exact device name may differ between distributions and LVM naming conventions.
Identify the Swap Logical Volume
You can inspect the block-device layout with:
lsblk
For a more detailed view:
lsblk -f
Example:
NAME FSTYPE SIZE MOUNTPOINT sda LVM2_member 100G ├─sda1 xfs 1G /boot └─sda2 LVM2_member 99G ├─VolGroup00-root xfs 40G / ├─VolGroup00-home xfs 20G /home └─VolGroup00-swap swap 4G [SWAP]
In this example, the swap logical volume is:
/dev/VolGroup00/swap
Check the Logical Volume
Use:
lvdisplay
Or specify the swap logical volume directly:
lvdisplay /dev/VolGroup00/swap
You may see output similar to:
--- Logical volume --- LV Path /dev/VolGroup00/swap LV Name swap VG Name VolGroup00 LV Size 4.00 GiB Current LE 1024
The values will vary depending on the system.
Check the Volume Group Free Space
This is one of the most important checks before attempting to extend the swap LV.
Run:
vgs
You can also use:
vgdisplay
Example:
VG #PV #LV #SN Attr VSize VFree VolGroup00 1 3 0 wz--n- 99.00g 20.00g
The important value is:
VFree = 20.00g
If you want to increase swap by 8 GB, the Volume Group must have at least enough free space to satisfy that extension.
Example Scenario
Suppose the server currently has:
| Parameter | Current Value |
|---|---|
| RAM | 16 GB |
| Current Swap | 4 GB |
| Required Swap | 8 GB |
| Additional Swap | 4 GB |
| VG Free Space | 20 GB |
The Volume Group has enough free space, so the swap logical volume can potentially be extended by 4 GB.
The final layout would be approximately:
Before: Swap LV = 4 GB VG Free = 20 GB After: Swap LV = 8 GB VG Free = 16 GB
Check Whether Swap Is Currently in Use
Before disabling swap, check how much swap is being used.
Run:
free -h
and:
swapon --show
If swap usage is significant, you should carefully evaluate available RAM before running swapoff.
Running swapoff forces the kernel to move swapped pages back into RAM. If the server does not have sufficient available memory, disabling swap can create severe memory pressure and may cause processes to be killed by the Out-Of-Memory mechanism.
Always check available memory before disabling swap on a production server.
Back Up the Current Configuration
Before modifying the swap configuration, record the current state.
Run:
free -h swapon --show lsblk -f vgs lvs
Also inspect the swap entry in:
cat /etc/fstab
You may see an entry similar to:
/dev/mapper/VolGroup00-swap swap swap defaults 0 0
or:
UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx none swap defaults 0 0
Record the existing configuration before making changes.
Important: Swap UUID
Modern Linux installations may identify swap using a UUID rather than only the device path.
You can check the current UUID with:
lsblk -no UUID /dev/VolGroup00/swap
For example:
550e8400-e29b-41d4-a716-446655440000
This becomes particularly important because reinitializing swap with a normal mkswap command can generate a new UUID.
Current Red Hat documentation specifically describes preserving the existing swap UUID with mkswap --uuid, or alternatively generating a new UUID and updating the relevant system configuration.
For an extension procedure, do not blindly overwrite the swap configuration. First determine whether your system references the swap device by UUID and whether the existing UUID needs to be preserved.
Check the Operating System Version
The exact commands and configuration details can vary between Linux distributions and releases.
Check the operating system:
cat /etc/os-release
On Red Hat Enterprise Linux systems, you may also use:
cat /etc/redhat-release
Example:
Red Hat Enterprise Linux Server release 7.x
This guide is particularly useful for RHEL/CentOS/Oracle Linux-style environments using LVM, but the underlying LVM concepts also apply to other Linux distributions.
What If the Volume Group Has No Free Space?
If vgs reports:
VFree = 0
you cannot simply extend the swap logical volume.
You must first provide additional storage to the Volume Group.
A typical process is:
Add disk / partition / LUN
↓
Create or identify Physical Volume
↓
pvcreate
↓
vgextend
↓
Verify VFree
↓
Extend swap LV
This scenario will be covered in detail in Part 3.
Do Not Run the Resize Command Yet
At this stage, we have only inspected the system.
The safe sequence is:
1. Identify swap LV 2. Check current swap 3. Check swap usage 4. Check VG free space 5. Check /etc/fstab 6. Record swap UUID 7. Confirm sufficient RAM 8. Plan the maintenance operation 9. Only then modify the LV
How to Extend Swap Space on an LVM Disk — Part 2
Part 2 — Performing the Swap Extension
In Part 1, we identified the existing swap logical volume, checked the current swap usage, verified the Volume Group free space, and reviewed the swap UUID configuration. In this part, we will perform the actual LVM swap extension and verify the result.
Before You Begin
Make sure you have completed the checks described in Part 1.
For this example, assume the server has the following configuration:
| Item | Example |
|---|---|
| Swap LV | /dev/VolGroup00/swap |
| Current Swap | 4 GB |
| Additional Swap | 4 GB |
| Final Swap | 8 GB |
| Volume Group | VolGroup00 |
Your actual device name, Volume Group name, and required swap size may be different. Always substitute the values from your own server.
Do not copy the example device name blindly. Verify the actual swap device with swapon --show, lsblk -f, and lvs before executing any resizing command.
Step 1 — Check Current Memory and Swap
First, record the current memory and swap status:
free -h
Example:
total used free shared buff/cache available Mem: 16Gi 4.0Gi 1.2Gi 500Mi 10Gi 11Gi Swap: 4Gi 0B 4Gi
The server currently has approximately 4 GB of swap.
Also check the active swap device:
swapon --show
Example:
NAME TYPE SIZE USED PRIO
/dev/mapper/VolGroup00-swap
partition 4G 0B -2
Step 2 — Check the Volume Group Free Space Again
Before making the change, verify that sufficient space is still available:
vgs
Or:
vgdisplay VolGroup00
Example:
VG #PV #LV #SN Attr VSize VFree VolGroup00 1 3 0 wz--n- 99.00g 20.00g
Since the Volume Group has 20 GB free and we only need another 4 GB, the LV can be extended.
Step 3 — Record the Existing Swap UUID
This step is particularly important on modern Linux systems.
Check the existing UUID:
lsblk -no UUID /dev/VolGroup00/swap
Example:
550e8400-e29b-41d4-a716-446655440000
Save this value somewhere temporarily.
Also check whether /etc/fstab uses this UUID:
grep -i swap /etc/fstab
For example:
UUID=550e8400-e29b-41d4-a716-446655440000 none swap defaults 0 0
If the existing swap UUID is referenced by the system, preserving that UUID avoids unnecessary configuration changes.
Current Red Hat documentation specifically recommends retrieving the old UUID and using mkswap --uuid when extending an LVM swap volume, so the existing configuration remains synchronized.
Step 4 — Check Swap Usage Before Disabling It
Before running swapoff, check whether swap is currently being used:
free -h
and:
swapon --show
If swap usage is very low or zero and sufficient RAM is available, the operation is generally easier to perform.
If swap usage is high, investigate the memory pressure first.
swapoff attempts to move swapped pages back into physical RAM. Do not assume that a server with a large swap allocation can safely disable it simply because the swap command itself is available.
Step 5 — Disable the Existing Swap
Once you have confirmed that it is safe to temporarily disable swap, run:
swapoff -v /dev/VolGroup00/swap
The -v option provides verbose output.
Red Hat's documented LVM swap-extension procedure begins by disabling the associated swap logical volume with swapoff.
Verify that it is no longer active:
swapon --show
If the command returns no entry for the swap LV, it has been successfully disabled.
You can also check:
cat /proc/swaps
Step 6 — Verify the Logical Volume Is Inactive
Check the LV:
lvs
For more detailed information:
lvdisplay /dev/VolGroup00/swap
Example:
LV VG Attr LSize swap VolGroup00 -wi-a----- 4.00g
The exact attributes may vary depending on the LVM version and system configuration.
Step 7 — Extend the Swap Logical Volume
Now extend the logical volume by the required amount.
For example, to add 4 GB:
lvextend -L +4G /dev/VolGroup00/swap
You can also use:
lvresize -L +4G /dev/VolGroup00/swap
Both commands can resize an LVM logical volume; Red Hat's current swap documentation uses lvresize for this specific procedure.
You should see output similar to:
Size of logical volume VolGroup00/swap changed from 4.00 GiB to 8.00 GiB. Logical volume VolGroup00/swap successfully resized.
Step 8 — Verify the New Logical Volume Size
Do not immediately enable swap. First verify that the LV was actually extended.
Run:
lvs
Or:
lvdisplay /dev/VolGroup00/swap
The size should now show approximately 8 GB:
LV VG Attr LSize swap VolGroup00 -wi-a----- 8.00g
You can also verify the Volume Group free space:
vgs
The free space should have decreased by approximately 4 GB.
Step 9 — Reinitialize the Swap Area
After increasing the logical volume, the swap signature must cover the newly enlarged area.
This is done with mkswap.
However, there are two important approaches regarding the UUID.
Recommended Approach — Preserve the Existing UUID
If you recorded the old UUID in Step 3, use:
mkswap --uuid 550e8400-e29b-41d4-a716-446655440000 /dev/VolGroup00/swap
Replace the example UUID with the actual UUID from your server.
This recreates the swap signature while preserving the identifier already referenced by your system configuration.
This approach is particularly useful when /etc/fstab and the boot configuration already refer to the existing swap UUID. Red Hat's current documentation explicitly describes this method for extending swap on an LVM logical volume.
For an existing production swap LV, preserving the current UUID is usually the cleanest approach because it avoids unnecessary changes to /etc/fstab and boot parameters.
Alternative — Generate a New UUID
You can instead run:
mkswap /dev/VolGroup00/swap
This creates a new swap signature and normally generates a new UUID.
Afterward, obtain the new UUID:
lsblk -no UUID /dev/VolGroup00/swap
Example:
8f3c7a4d-7f7b-4f1a-a0a1-123456789abc
If the old UUID was referenced in /etc/fstab, update it to the new UUID.
For example:
UUID=8f3c7a4d-7f7b-4f1a-a0a1-123456789abc none swap defaults 0 0
Current RHEL documentation also notes that when a new swap UUID is generated, the relevant kernel resume configuration must be synchronized with the new identifier.
Step 10 — Verify the Swap UUID
After running mkswap, verify the UUID:
lsblk -f /dev/VolGroup00/swap
Or:
blkid /dev/VolGroup00/swap
Example:
/dev/VolGroup00/swap: UUID="550e8400-e29b-41d4-a716-446655440000" TYPE="swap"
If you deliberately preserved the old UUID, confirm that it is unchanged.
Step 11 — Enable the Enlarged Swap
Now activate the enlarged swap logical volume:
swapon -v /dev/VolGroup00/swap
You should receive output indicating that the swap device has been enabled.
Red Hat's current procedure uses swapon -v to activate the extended swap LV.
Step 12 — Verify Active Swap
Check the active swap devices:
swapon --show
Example:
NAME TYPE SIZE USED PRIO
/dev/mapper/VolGroup00-swap
partition 8G 0B -2
You can also use:
cat /proc/swaps
The swap size should now reflect the enlarged logical volume.
Step 13 — Verify with free -h
Finally, run:
free -h
Example:
total used free shared buff/cache available Mem: 16Gi 4.1Gi 1.1Gi 500Mi 10Gi 11Gi Swap: 8Gi 0B 8Gi
The swap allocation has successfully increased from 4 GB to 8 GB.
This verification method is also documented by Red Hat for LVM swap extensions.
Step 14 — Verify /etc/fstab
Check the persistent swap configuration:
grep -i swap /etc/fstab
If you preserved the existing UUID, the existing entry should normally remain unchanged.
For example:
UUID=550e8400-e29b-41d4-a716-446655440000 none swap defaults 0 0
If you generated a new UUID, ensure that /etc/fstab contains the new UUID.
Step 15 — Test the Persistent Configuration
After confirming the swap is active, you can test the configuration represented in /etc/fstab.
First, make sure the current swap is active and the configuration is correct.
Then you can test:
swapon -a
If the swap is already active, swapon -a normally does not create a second activation of the same swap device.
Then verify:
swapon --show free -h
Complete Command Sequence
For the example scenario where the existing swap is /dev/VolGroup00/swap and we want to add 4 GB while preserving the existing UUID, the sequence is:
# 1. Check current swap free -h swapon --show # 2. Record current UUID lsblk -no UUID /dev/VolGroup00/swap # 3. Check VG free space vgs # 4. Disable swap swapoff -v /dev/VolGroup00/swap # 5. Extend swap LV by 4 GB lvresize -L +4G /dev/VolGroup00/swap # 6. Recreate swap while preserving the old UUID mkswap --uuid <OLD_UUID> /dev/VolGroup00/swap # 7. Enable swap swapon -v /dev/VolGroup00/swap # 8. Verify swapon --show cat /proc/swaps free -h lsblk -f
This follows the current Red Hat-documented extension workflow, with the UUID-preservation detail included.
Common Error 1 — swapoff: Cannot Allocate Memory
You may encounter an error similar to:
swapoff: ... Cannot allocate memory
This generally indicates that the system cannot bring all swapped pages back into RAM with the available memory conditions.
Check:
free -h
Also identify memory-intensive processes:
top
or:
ps aux --sort=-%mem | head
Do not repeatedly execute swapoff on a memory-constrained production server without understanding why the operation is failing.
Common Error 2 — Insufficient Free Space in Volume Group
If you attempt:
lvresize -L +4G /dev/VolGroup00/swap
and the Volume Group does not have sufficient free extents, the operation will fail.
Check:
vgs
If:
VFree = 0
then additional storage must be added to the Volume Group before the swap LV can be extended.
That situation is covered in detail in Part 3.
Common Error 3 — Swapon Reports Invalid Argument
If swapon reports an error after resizing, check whether the swap signature was correctly recreated.
Run:
blkid /dev/VolGroup00/swap
You should see:
TYPE="swap"
If the swap signature is missing, recreate it with the appropriate mkswap command, taking the UUID considerations discussed above into account.
Common Error 4 — Swap UUID Does Not Match
If /etc/fstab references an old UUID but mkswap generated a new UUID, the persistent configuration can become inconsistent.
Check:
grep -i swap /etc/fstab lsblk -no UUID /dev/VolGroup00/swap
If they differ, either restore the original UUID using:
mkswap --uuid <OLD_UUID> /dev/VolGroup00/swap
or update the relevant system configuration to use the new UUID.
Important: Do Not Use Filesystem Resize Commands
A swap logical volume does not contain a conventional filesystem such as XFS or ext4.
Therefore, after extending the LV, you do not use commands such as:
xfs_growfs resize2fs
Instead, the swap area itself is reinitialized with:
mkswap
and then activated with:
swapon
This distinction is important when working with LVM.
Verify the Final LVM Layout
After the operation, run:
lsblk lvs vgs swapon --show free -h
A successful configuration should show:
Swap LV ↓ Expanded LVM Logical Volume ↓ New swap signature ↓ Active swap device ↓ Correct swap size reported by free -h
Example: Before and After
| Parameter | Before | After |
|---|---|---|
| Swap LV | 4 GB | 8 GB |
| VG Free Space | 20 GB | 16 GB |
| Active Swap | 4 GB | 8 GB |
How to Extend Swap Space on an LVM Disk — Part 3
Part 3 — Adding Storage When the Volume Group Has No Free Space
In Part 1 and Part 2, we learned how to identify an LVM-based swap volume and extend it when free space already exists inside the Volume Group. In this final part, we will handle the more common production scenario where the Volume Group has little or no free space.
What If the Volume Group Has No Free Space?
Suppose you run:
vgs
and receive something similar to:
VG #PV #LV #SN Attr VSize VFree VolGroup00 1 3 0 wz--n- 99.00g 0
The important value is:
VFree = 0
This means that all available extents in the Volume Group are already allocated.
In this situation, the swap logical volume cannot be extended until additional space is made available to the Volume Group.
There are two common solutions:
- Add a new disk or LUN, create a new Physical Volume, and add it to the existing Volume Group.
-
Increase the size of an existing disk/PV and then use
pvresizeto make the additional capacity available to LVM.
Red Hat documents vgextend for adding Physical Volumes to an existing Volume Group.
Scenario 1 — Add a New Disk to the Server
Suppose the existing server has:
/dev/sda
|
+-- LVM
|
+-- VolGroup00
|
+-- root
+-- swap
+-- other LVs
The Volume Group has no free space.
A new 20 GB disk is added to the server:
/dev/sdb
The objective is:
New Disk ↓ Physical Volume ↓ Existing Volume Group ↓ Free VG Space ↓ Extend Swap LV
Step 1 — Identify the New Disk
Never assume the new disk name.
First inspect the current block devices:
lsblk
You can also use:
lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS
Example:
NAME SIZE TYPE FSTYPE MOUNTPOINTS sda 100G disk ├─sda1 1G part xfs /boot └─sda2 99G part LVM2_member ├─VolGroup00-root ├─VolGroup00-home └─VolGroup00-swap sdb 20G disk
In this example:
/dev/sdb
is the newly added 20 GB disk.
Be extremely careful when identifying the new disk. Running pvcreate on the wrong disk can destroy existing storage metadata and potentially make data inaccessible.
Step 2 — Verify That the Disk Is Not Already in Use
Before initializing a disk as an LVM Physical Volume, check it carefully.
Run:
lsblk -f
Also check:
pvs
and:
blkid /dev/sdb
If the disk contains existing data or belongs to another storage configuration, stop and investigate before proceeding.
Step 3 — Create a Physical Volume
If /dev/sdb has been confirmed as the correct unused disk, initialize it as an LVM Physical Volume:
pvcreate /dev/sdb
Example output:
Physical volume "/dev/sdb" successfully created.
Verify:
pvs
You should see something similar to:
PV VG Fmt Attr PSize PFree /dev/sda2 VolGroup00 lvm2 a-- <99g 0 /dev/sdb lvm2 --- 20g 20g
The new PV currently has approximately 20 GB available.
Step 4 — Add the New PV to the Existing Volume Group
Now extend the existing Volume Group:
vgextend VolGroup00 /dev/sdb
Example:
Volume group "VolGroup00" successfully extended
This is the standard LVM method for adding a Physical Volume to an existing Volume Group.
Step 5 — Verify the Expanded Volume Group
Run:
vgs
Example:
VG #PV #LV #SN Attr VSize VFree VolGroup00 2 3 0 wz--n- 119.00g 20.00g
The important change is:
VFree = 20.00g
The Volume Group now has enough free space to extend the swap LV.
Step 6 — Extend the Swap Logical Volume
Assume the existing swap LV is:
/dev/VolGroup00/swap
and you want to add 8 GB.
First check its current size:
lvs /dev/VolGroup00/swap
Then follow the procedure described in Part 2:
swapoff -v /dev/VolGroup00/swap lvresize -L +8G /dev/VolGroup00/swap mkswap --uuid <OLD_UUID> /dev/VolGroup00/swap swapon -v /dev/VolGroup00/swap
Current Red Hat documentation uses the same sequence for extending an LVM swap volume and specifically documents preserving the existing swap UUID with mkswap --uuid.
Step 7 — Verify the Enlarged Swap
Run:
swapon --show
Then:
free -h
Example:
total used free shared buff/cache available Mem: 16Gi 4.2Gi 1.0Gi 500Mi 10Gi 11Gi Swap: 12Gi 0B 12Gi
If the swap was originally 4 GB and 8 GB was added, the final swap capacity should now be approximately 12 GB.
Scenario 2 — The Existing Disk Has Been Expanded
Adding a completely new disk is not the only way to increase LVM capacity.
Another common situation is:
Original Disk
↓
100 GB
↓
Disk expanded by storage administrator
↓
150 GB
The operating system may initially continue to report the original size until the kernel and partition/PV configuration recognize the additional capacity.
After confirming that the underlying storage has really been expanded, the additional capacity can potentially be made available to LVM with pvresize.
Step 8 — Verify the New Disk Size
First inspect the disk:
lsblk
You can also check:
fdisk -l
Suppose the disk now appears as:
/dev/sda 150G
but the existing LVM Physical Volume still reports approximately 100 GB.
Check:
pvs
For example:
PV VG PSize PFree /dev/sda2 VolGroup00 99.00g 0
In this example, the disk may be larger while the partition containing the PV has not yet been expanded.
Step 9 — Expand the Partition if Required
If the PV resides inside a partition, such as:
/dev/sda2
the partition itself may need to be extended before pvresize can use the new disk capacity.
This is a storage-layout-dependent operation.
Do not blindly run partitioning commands on a production database server.
First determine whether the PV is:
- A whole disk such as
/dev/sdb - A partition such as
/dev/sda2 - A multipath device
- A SAN/LUN device
- A virtual disk
The correct partition-resize procedure depends on the environment and partition table.
Do not use pvresize as a substitute for enlarging a partition that has not actually been extended. The PV must have additional underlying block-device capacity available.
Step 10 — Resize the Physical Volume
Once the underlying PV device has actually been enlarged, run:
pvresize /dev/sda2
Replace /dev/sda2 with the actual PV.
Example output may indicate that the Physical Volume has been resized successfully.
Verify:
pvs
The PV should now report the increased size and available free space.
Step 11 — Verify the Volume Group
Run:
vgs
For example:
VG #PV #LV #SN Attr VSize VFree VolGroup00 1 3 0 wz--n- 149.00g 50.00g
The newly available space is now part of the Volume Group.
You can use that free space to extend the swap LV.
Step 12 — Extend the Swap LV
Suppose the swap LV is currently:
4 GB
and you want to increase it to:
16 GB
The required increase is:
+12 GB
Use:
swapoff -v /dev/VolGroup00/swap lvresize -L +12G /dev/VolGroup00/swap mkswap --uuid <OLD_UUID> /dev/VolGroup00/swap swapon -v /dev/VolGroup00/swap
Then verify:
free -h swapon --show lvs vgs
Adding a Separate Swap Logical Volume
Extending the existing swap LV is not the only solution.
In some environments, you may prefer to create an additional swap LV instead of resizing the existing one.
For example, if the Volume Group has 8 GB available:
lvcreate -L 8G -n swap2 VolGroup00
Then initialize it:
mkswap /dev/VolGroup00/swap2
Enable it:
swapon /dev/VolGroup00/swap2
Verify:
swapon --show
You may then see:
NAME TYPE SIZE USED PRIO
/dev/mapper/VolGroup00-swap
partition 4G 0B -2
/dev/mapper/VolGroup00-swap2
partition 8G 0B -3
This provides a total of approximately 12 GB of active swap.
Configure Additional Swap in /etc/fstab
If the additional swap LV should remain active after reboot, add it to /etc/fstab.
First obtain its UUID:
blkid /dev/VolGroup00/swap2
Example:
UUID="12345678-abcd-1234-abcd-123456789abc" TYPE="swap"
Then add an appropriate entry to /etc/fstab:
UUID=12345678-abcd-1234-abcd-123456789abc none swap defaults 0 0
Test:
swapon -a
Then verify:
swapon --show
Swap Priority
Linux can use multiple swap areas.
You can see the priority with:
swapon --show
For example:
NAME TYPE SIZE USED PRIO
/dev/mapper/VolGroup00-swap
partition 8G 0B -2
/dev/mapper/VolGroup00-swap2
partition 4G 0B -3
Swap areas with higher priority are preferred before lower-priority swap areas.
In most ordinary configurations, you do not need to manually change swap priorities simply because you have added additional swap.
Production Considerations
1. Check Available RAM
Before disabling swap:
free -h
Make sure there is sufficient memory to absorb the pages currently residing in swap.
2. Check Database Workloads
On Oracle Database servers, examine memory-intensive processes before performing the operation.
For example:
ps -eo pid,user,%mem,%cpu,cmd --sort=-%mem | head -20
If the server is already under significant memory pressure, consider scheduling the change during a maintenance period.
3. Keep a Record of the Existing Configuration
Before making changes, save:
free -h swapon --show lsblk -f pvs vgs lvs grep -i swap /etc/fstab
This gives you a useful before-and-after comparison.
4. Verify Backups
LVM commands are powerful storage-management commands. Before making production storage changes, ensure that appropriate backups and recovery procedures are available.
Common Problems and Troubleshooting
Problem 1 — VFree Is Zero
Run:
vgs
If:
VFree = 0
you must add capacity to the VG before extending the swap LV.
Possible solutions:
- Add a new disk and use
pvcreate+vgextend. - Expand an existing disk and use
pvresize. - Use an additional swap LV if sufficient VG space becomes available.
Problem 2 — pvcreate on the Wrong Disk
This is one of the most dangerous mistakes.
Always run:
lsblk -f pvs blkid
before initializing a disk.
pvcreate /dev/sdX
Replace /dev/sdX only after positively identifying the intended unused device.
Problem 3 — swapoff Fails
Check:
free -h swapon --show
If swap usage is high, determine which processes are consuming memory:
ps aux --sort=-%mem | head -20
Do not force the operation without understanding the memory situation.
Problem 4 — Swap Is Missing After Reboot
Check:
grep -i swap /etc/fstab blkid swapon --show
A common cause is that the swap UUID referenced in /etc/fstab no longer matches the actual swap UUID.
If a new UUID was generated during mkswap, update the persistent configuration accordingly.
On RHEL systems using the swap UUID for resume configuration, the kernel parameter may also need to be synchronized with the new UUID. Red Hat documents this explicitly.
Problem 5 — New Disk Does Not Appear
If a newly attached disk does not appear in:
lsblk
the issue may be at the virtualization, SAN, cloud, multipath, or operating-system device-detection layer rather than LVM itself.
Resolve disk visibility first. Do not run pvcreate until the intended block device is correctly identified.
Useful Commands for LVM and Swap Troubleshooting
The following commands are particularly useful:
| Command | Purpose |
|---|---|
free -h |
Display RAM and swap usage |
swapon --show |
Display active swap areas |
cat /proc/swaps |
Display kernel swap information |
lsblk -f |
Display block-device and filesystem information |
blkid |
Display device UUIDs and types |
pvs |
Display Physical Volumes |
vgs |
Display Volume Groups and free space |
lvs |
Display Logical Volumes |
lvdisplay |
Display detailed LV information |
pvdisplay |
Display detailed PV information |
Complete Decision Guide
The following decision tree can help determine which procedure to use.
Need more swap?
|
v
Is swap on LVM?
|
Yes
|
v
Does VG have free space?
/ \
Yes No
| |
| v
| Is underlying disk
| expandable?
| / \
| Yes No
| | |
| | v
| | Add new disk/LUN
| | |
| | v
| | pvcreate
| | |
| | v
| | vgextend
| |
| v
| pvresize
| |
+---+
|
v
Extend swap LV
|
v
mkswap
|
v
swapon
|
v
Verify
Best Practices
-
Always verify the target device.
Never assume
/dev/sdb,/dev/sdc, or another device is the new disk. -
Check VG free space before resizing.
Use
vgsorlvs -o lv_name,lv_size,vg_name,vg_size,vg_free. - Check available RAM before swapoff. A swap extension requires temporarily disabling the existing swap LV.
- Record the existing swap UUID. This is especially important if the system uses UUID-based swap and resume configuration.
-
Preserve the existing UUID when appropriate.
Using
mkswap --uuid <OLD_UUID>can avoid unnecessary configuration changes. - Do not use filesystem resize commands on swap. Swap is not an XFS or ext4 filesystem.
-
Verify after every major storage operation.
Use
pvs,vgs,lvs,lsblk, andswapon --show. - Be especially careful on Oracle Database servers. Schedule storage changes appropriately and monitor memory pressure.
Frequently Asked Questions
Can I extend swap without rebooting?
Yes. When swap is implemented as an LVM logical volume, it can normally be disabled, resized, recreated, and re-enabled without rebooting, provided the server has sufficient available RAM and the storage operation is performed correctly. Red Hat documents this online procedure.
Do I need to resize the filesystem?
No. Swap is not a conventional filesystem such as XFS or ext4. After extending the LV, recreate the swap signature with mkswap and activate it with swapon.
What if the Volume Group has no free space?
Add capacity to the Volume Group. This can be done by adding a new PV with pvcreate and vgextend, or by expanding an existing underlying device and using pvresize after the block device/partition has actually been enlarged.
Can I create another swap LV instead?
Yes. Linux can use multiple swap areas. Creating a second swap LV can be a practical alternative when you do not want to modify the existing swap LV.
Why is my swap size unchanged after lvresize?
Because extending the LV does not by itself complete the swap configuration. The swap signature must cover the enlarged LV, after which the swap area must be activated again.
Why did the swap UUID change?
A normal mkswap operation can generate a new UUID. If the system references the old UUID, use mkswap --uuid <OLD_UUID> to preserve it, or update /etc/fstab and relevant kernel resume configuration to the new UUID.
Final Verification Checklist
After completing the swap extension, verify all of the following:
| Check | Command |
|---|---|
| RAM and swap | free -h |
| Active swap | swapon --show |
| Kernel swap information | cat /proc/swaps |
| Block devices | lsblk -f |
| Physical Volumes | pvs |
| Volume Groups | vgs |
| Logical Volumes | lvs |
| Swap UUID | blkid /dev/VolGroup00/swap |
| Persistent configuration | grep -i swap /etc/fstab |
Conclusion
Extending swap space on an LVM-based Linux server is straightforward when the storage layout is understood and the operation is performed in the correct order.
When free space already exists in the Volume Group, the operation is relatively simple: disable swap, extend the logical volume, recreate the swap signature, activate swap, and verify the result.
When the Volume Group is full, additional storage must first be made available. This can be accomplished by adding a new Physical Volume with pvcreate and vgextend, or by expanding an existing storage device and making its additional capacity available to LVM with pvresize.
The most important operational considerations are to identify storage devices correctly, check available RAM before running swapoff, preserve or correctly update the swap UUID, and verify every stage of the operation.
Key Takeaway
Always follow the sequence:
Check → Identify → Backup/Record → Add Storage if Required → Extend VG/PV → swapoff → Extend LV → mkswap → Preserve/Update UUID → swapon → Verify
Following this sequence minimizes the risk of storage or boot-configuration problems when increasing swap on an LVM-based Linux server.
Official Technical References
- Red Hat Enterprise Linux — Extending swap on an LVM logical volume. [Red Hat documentation](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html/managing_storage_devices/getting-started-with-swap?utm_source=chatgpt.com)
-
Red Hat Enterprise Linux — Extending an LVM Volume Group with
vgextend. [Red Hat LVM documentation](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html/configuring_and_managing_logical_volumes/managing-lvm-volume-groups?utm_source=chatgpt.com) - Oracle Linux — Configuring Logical Volumes. [Oracle Linux LVM documentation](https://docs.oracle.com/en/learn/ol-lvm/?utm_source=chatgpt.com)
Comments
Post a Comment