Adding more swap space to an Existing Logical Volume on Linux

We are having to do this on our VM development boxes, so we thought we would share this on our blog.

Find out where the swap space is held:
# swapon -s
Filename Type Size Used Priority
/dev/mapper/VolGroup00-LogVol01 partition 1015800 0 -1

Look at the logical volume:

lvdisplay
— Logical volume —
LV Name /dev/VolGroup00/LogVol00
VG Name VolGroup00
LV UUID FeQLua-ORb9-9B3f-lVem-c0nQ-FYjS-1etiif
LV Write Access read/write
LV Status available
# open 1
LV Size 18.91 GB
Current LE 605
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

— Logical volume —
LV Name /dev/VolGroup00/LogVol01
VG Name VolGroup00
LV UUID diY47d-Xu5Q-I82e-39sG-skDj-zklP-Z7G2Dl
LV Write Access read/write
LV Status available
# open 1
LV Size 992.00 MB
Current LE 31
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1

Just resizing will not work if there is not enough diskspace in the volume:
lvm lvresize /dev/VolGroup00/LogVol01 -L 2G
Extending logical volume LogVol01 to 2.00 GB
Insufficient free space: 33 extents needed, but only 0 available

We need to add another disk to the Volume group:
ls -al /dev/sda*
brw-r—– 1 root disk 8, 0 Oct 4 01:24 /dev/sda
brw-r—– 1 root disk 8, 1 Oct 4 01:25 /dev/sda1
brw-r—– 1 root disk 8, 2 Oct 4 01:24 /dev/sda2
[root@dbvisit51 ~]# sfdisk -s
/dev/sda: 31457280
/dev/sdb: 20971520
total: 52428800 blocks

Add a new partition on the existing disk (The physical disk has space)

fdisk /dev/sda

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (2611-3916, default 2611):
Using default value 2611
Last cylinder or +size or +sizeM or +sizeK (2611-3916, default 3916): 2872

Command (m for help): w
The partition table has been altered!

Resize the volume:
lvm lvresize /dev/VolGroup00/LogVol01 -L2.9G
Rounding up size to full physical extent 2.91 GB
Extending logical volume LogVol01 to 2.91 GB
Logical volume LogVol01 successfully resized

Remove the swap space so that we can increase it:
swapoff -v /dev/VolGroup00/LogVol01

Rebuild the swap space:
mkswap /dev/VolGroup00/LogVol01
Setting up swapspace version 1, size = 3120558 kB

Add swap space and check size again:

swapon -va
swapon on /dev/VolGroup00/LogVol01
[root@dbvisit51 ~]# swapon -s
Filename Type Size Used Priority
/dev/mapper/VolGroup00-LogVol01 partition 3047416 0 -3

All done, swap space now 3G.

About Arjen Visser


As the Founder and Technical Director of Dbvisit Arjen is passionate about helping to create software that is highly effective and simple to use. With over 20 years of database experience in the IT industry, his technical expertise and extensive IT knowledge covers a broad range of industries and areas.

Speak Your Mind