How to extend an LVM swap partition in Linux

Question: Given an LVM partition, how can we extend it to give some addtional space?

Swap filesystem

Swap is used if there is not enough memory available for your application. It’s normal and can be a good thing for Linux systems to use swap, even if there is still available RAM. But, it’s not just used if there is not enough memory.

How to extend LVM based swap filesystem

To increase the space for LVM Swap partition follow the steps given below:

1. Verify availability of the new space.

# fdisk -l /dev/sda
Disk /dev/sda: 4294 MB, 4294967296 bytes, 8388608 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 byte

2. Create additional partition for the new swap partition.

# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-8388607, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-8388607, default 8388607): 4056
Partition 1 of type Linux and of size 1004.5 KiB is set

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-8388607, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-8388607, default 8388607): 4056
Partition 1 of type Linux and of size 1004.5 KiB is set

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

3. Activate the new partition.

# partprobe

4. Verify the new partition is available.

# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 12G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 11.5G 0 part
├─vg_lv_root (dm-0) 251:0 0 10.3G 0 lvm /
└─vg_lv_swap (dm-1) 251:1 0 1.2G 0 lvm [SWAP]
└─sda3 8:3 0 1G 0 part

sr0 11:0 1 1024M 0 rom

Note: a reboot may be needed if the change does not show at this point.

5. Create a new physical volume on the LUN.

# pvcreate /dev/sda3

6. Add the new volume to the volume group for the swap volume. Our examples use SwapVG and /dev/sda3; replace with the volume names and devices as appropriate to your deployment.

# vgextend SwapVG /dev/sda3

7. Disable swapping for the associated physical volume.

# swapoff -v /dev/SwapVG/SwapLV

8. Resize the logical volume to the desired size.

# lvresize /dev/SwapVG/SwapLV -L +8G

9. Format the extended swap volume.

# mkswap /dev/SwapVG/SwapLV

10. Enable the logical volume.

# swapon -va

11. View the new swap size.

# cat /proc/swaps
Filename Type Size Used Priority
/dev/dm-1 partition 1257468 0 -1
# free
total used free shared buffers cached
Mem: 1784432 196920 1587512 516 12624 77268
-/+ buffers/cache: 107028 1677404
Swap: 1257468 0 1257468
Related Post