Extend SWAP with LVM

The SWAP is a partition on your harddrive, that can be used as an extension of you memory. If the systemmemory is nearly full, the os will begin to transfer less frequently used data from the ram to the swap. It is also useful if you want to set you system to sleep. In that case the content of the RAM will be written into the swap.

Add Diskspace

First of all we need additional diskspace to enlarge the swap. This can be done by adding another drive to your LVM configuration, growing the partition or adding another VMDK if you are in a virtual environment.

Resize VMDK

If you enlarge your VMDK, you need to rescan your scsi bus for size changes. Otherwise the OS will not recognize it.

[tux@server]$ rescan-scsi-bus.sh -s

If this command should not be installed on you system, you can do so by running

[tux@server]$ dnf install sg3_utils

Extend Partition

If you have unpartioned space on your drive, you can also resize your partition with parted or fdisk. I will show parted in this example.

[tux@server]$ parted /dev/sdb

(parted) print
Model: VMware Virtual disk (scsi)
Disk /dev/sdb: 17.2GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  16.1GB  16.1GB  primary               lvm

(parted) resizepart 1
End?  [16.1GB]? 100%

(parted) quit

Resize LVM

Now we have changed the partition or the disk-size but we need to adjust the logical volume inside the LVM configuration. To do so we need to extend the physical volume of our modified disk.

[tux@server]$ pvresize /dev/sdb1
  Physical volume "/dev/sdb1" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized

The volume group will automaticaly grow if you change the physical volume. So no need to adjust ist. But we need to extend the logical volume. In this case I will give it all the free space.

[tux@server]$ lvextend -l +100%FREE /dev/vg_swap/lv_swap

Extend SWAP

No we have the diskspace but the OS will not start to use it as swap right away. We need to configure that as well.

First we want to clear the swap and move all the data back into memory. Otherwise we would loose this data, which could lead to instability if the os wants to access data from memory that is no longer present.

[tux@server]$ swapoff -a

After that we need to create the “swap-filesystem” on our new swap-partition. Unlike xfs and ext4 you can extend a “swap-filesystem” and need to recreate it. That is the reason for clearing the swap beforehand.

[tux@server]$ mkswap /dev/vg_swap/lv_swap
mkswap: /dev/vg_swap/lv_swap: warning: wiping old swap signature.
Setting up swapspace version 1, size = 16 GiB (17175670784 bytes)
no label, UUID=2952b272-e177-4cd2-a7b7-bb2355d13686

Now we need to active the swap again.

[tux@server]$ swapon -a

To check if you changes were successful, check your swapsize.

[tux@server]$ free -h
         total     used    free    shared  buff/cache   available
Mem:     15Gi      8,1Gi   1,8Gi   1,9Gi   7,4Gi        7,0Gi
Swap:    7,8Gi     0B      7,8Gi

Target specific SWAP Spaces

It is possible to have multiple SWAP-Spaces on e.g. multiple disks. If you don’t want to use swapon and swapoff with the -a flag, you can target one single swap space at a time. This is useful if you cant transfer all data from your SWAP into the system memory.

[tux@server]$ swapoff /dev/sdb

[tux@server]$ swapon /dev/sdb
profile picture of the author

Jannik Rehkemper

I'm an professional Linux Administrator and Hobby Programmer. My training as an IT-Professional started in 2019 and ended in 2022. Since 2023 I'm working as an Linux Administrator.