We use LVM for flexible volume management on Linux, so why not use LVM for swap space?
This allows users to increase the swap partition whenever they want.
Generally you need to create a swap space on Linux, but it is recommended that you create swap space on Linux when you have less RAM.
If you are upgrading your system RAM, it is necessary to add more swap space.
This allows you to manage the running of applications that require large amounts of memory.
Swap on Linux can be created in three ways.
- This may be a new swap partition.
- This may be a new swap file.
- You can extend this from an existing logical volume (LVM).
It’s recommended to create a dedicated swap partition instead of a swap file.
If you are looking for alternative ways to create swap space on Linux then you can read the following articles.
- 3 Easy ways to create or extend swap space in Linux
- Automatically Create/Remove and Mount swap file in Linux using Shell script
Why is swap Needed?
There are many reasons why you need a swap, but I’ve listed only a few reasons.
- If your system has very little RAM (less than 1 or 2 GB), you should use swap since most applications will run out of RAM quickly.
- You may need to create a swap on your system as suggested by the application vendor.
What is the Recommended swap Size on Linux?
Generally, the recommended swap size is twice the amount of RAM, but this is no longer applicable to modern computers. What is the amount of swap when you have 32GB or 64GB or 128GB of RAM?
Each of the distributions has a different approach to swap space, and the Red Hat recommendation for swap space is below.
Physical RAM | Recommended Swap Size | Recommended Swap Size with Hibernation |
2GB or less | Twice the installed RAM | 3 times the amount of RAM |
> 2GB – 8GB | The same amount of RAM | 2 times the amount of RAM |
> 8GB – 64GB | At least 4GB | 1.5 times the amount of RAM |
> 64GB or more | At least 4GB | Hibernation not recommended |
What is the Swap Space
Swap space in Linux is used when the amount of physical memory (RAM) is full. When physical RAM is full, inactive pages in memory are moved to the swap space.
This helps system to run the application continuously but it’s not considered a replacement for more RAM.
Swap space is located on hard drives so, it will not processing the request like physical RAM.
How to Create a Swap Partition Using LVM
Follow the following procedure for creating swap space because you already know how to create a logical volume.
Create a logical volume that you need. In my case I’m going to create the 5GB
of the swap partition.
$ sudo lvcreate -L 5G -n LogVol_swap1 vg00 Logical volume "LogVol_swap1" created.
Format the newly created swap space as follows.
$ sudo mkswap /dev/vg00/LogVol_swap1 Setting up swapspace version 1, size = 5 GiB (5368705024 bytes) no label, UUID=d278e9d6-4c37-4cb0-83e5-2745ca708582
Add the following entry to the /etc/fstab
file.
# vi /etc/fstab /dev/mapper/vg00-LogVol_swap1 swap swap defaults 0 0
Enable the swap partition.
$ sudo swapon -va swapon: /swapfile: already active -- ignored swapon: /dev/mapper/vg00-LogVol_swap1: found signature [pagesize=4096, signature=swap] swapon: /dev/mapper/vg00-LogVol_swap1: pagesize=4096, swapsize=5368709120, devsize=5368709120 swapon /dev/mapper/vg00-LogVol_swap1
Check if the swap space is added correctly.
$ cat /proc/swaps Filename Type Size Used Priority /swapfile file 1459804 526336 -1 /dev/dm-0 partition 5242876 0 -2 $ free -g total used free shared buff/cache available Mem: 1 1 0 0 0 0 Swap: 6 0 6
How to Extend a Swap Partition Using LVM
Follow the procedure below to extend the LVM swap partition.
Disable swap partitioning for the corresponding logical volume before resizing.
$ sudo swapoff -v /dev/vg00/LogVol_swap1 swapoff /dev/vg00/LogVol_swap1
Resize the logical volume. We are increasing the swap partition from 5GB to 11GB
.
$ sudo lvresize /dev/vg00/LogVol_swap1 -L +6G Size of logical volume vg00/LogVol_swap1 changed from 5.00 GiB (1280 extents) to 11.00 GiB (2816 extents). Logical volume vg00/LogVol_swap1 successfully resized.
Format before enable the new swap space.
$ sudo mkswap /dev/vg00/LogVol_swap1 mkswap: /dev/vg00/LogVol_swap1: warning: wiping old swap signature. Setting up swapspace version 1, size = 11 GiB (11811155968 bytes) no label, UUID=2e3b2ee0-ad0b-402c-bd12-5a9431b73623
Run the command below to enable the extended swap partition.
$ sudo swapon -va swapon: /swapfile: already active -- ignored swapon: /dev/mapper/vg00-LogVol_swap1: found signature [pagesize=4096, signature=swap] swapon: /dev/mapper/vg00-LogVol_swap1: pagesize=4096, swapsize=11811160064, devsize=11811160064 swapon /dev/mapper/vg00-LogVol_swap1
Finally check that the swap space is correctly added with the free command.
$ free -g total used free shared buff/cache available Mem: 1 1 0 0 0 0 Swap: 12 0 12 $ cat /proc/swaps Filename Type Size Used Priority /swapfile file 1459804 237024 -1 /dev/dm-0 partition 11534332 0 -2