How To Create, Extend, Remove Swap Partition in Redhat/Centos LVM

February 25, 2011 | By in FILE SYSTEM,LINUX HOWTO
| 3 Replies More

Swap memory is required when the system requires more memory than it is physically available. The kernel swaps out less used pages and gives memory to the current process that needs the memory immediately. So a page of memory is copied to the pre-configured space on the hard disk. Disk speed is much slower compared to memory speed. Swapping pages give more space for current application in the memory (RAM) and make the application run faster.

Swap space is located on hard drives, which have a slower access time than physical memory. Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files. It is recommended that the size of your swap space should be equal to twice your computer's RAM, or 32 MB, whichever amount is larger, but no more than 2048 MB (or 2 GB) because modern computer has RAM more than 4096 MB (or 4GB). From Ubuntu 17.04 version swap partition is replaced with swapfile by default for fresh installations.

In some scenarios, system admin needs to increase swap space. Below commands help you to expand the existing swap space.

1. Create a "normal" swap partition

Step 1: Check if you have enough space on disk to create new partition for swap. Let's see our available disk

# fdisk -l

Disk /dev/sda: 10.7 GB, 10720641024 bytes, 20938752 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/sdb: 536 MB, 536870912 bytes, 1048576 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x66fe37bd

We will use /dev/sdb disk for our swap. Check the swap with free -m command; we have:

# free -m
 total used free shared buff/cache available
Mem: 988 88 645 50 254 813
Swap: 0 0 0

You can see that we don't have a swap partition. We can also use the command below for verification

# swapon -s

You see that we don't have a return. It means that there is no swap

Step 2: Create new partition using tool like fdisk:

# fdisk /dev/sdb
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

Command (m for help):

You can type m command for the help which will list you different possibilities. We will create a new partition for our swap with n command

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-1048575, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-1048575, default 1048575): 
Using default value 1048575
Partition 1 of type Linux and of size 511 MiB is set

To define now our partition as swap type, we will use t command

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 82

Changed type of partition 'Linux' to 'Linux swap / Solaris'

The Hex code for swap partition on Linux is 82. Now we will save the changes with w command

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

Calling ioctl() to re-read partition table.
Syncing disks.

Make the new partition as swap. Change toggle id to 82 (for swap). Let's check with fdisk -l command:

# fdisk -l

Disk /dev/sda: 10.7 GB, 10720641024 bytes, 20938752 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/sdb: 536 MB, 536870912 bytes, 1048576 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x66fe37bd

 Device Boot Start End Blocks Id System
/dev/sdb1 2048 1048575 523264 82 Linux swap / Solaris

You can see the mention Linux swap on the last line.

Step 3: After Defining our partition, we need to format it for "swap mode" so run mkswap command on the newly created swap partition:

# mkswap -f /dev/sdb
mkswap: /dev/sdb: warning: wiping old swap signature.
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=c4696894-0b09-4fbe-87bb-a34d6d307a4e

Step 4: Now that our swap partition is formatted, we need to enable the swap space so run swapon command to enable it:

# swapon /dev/sdb

Step 5: Verify the newly added swap space using the command below:

# free -m
 total used free shared buff/cache available
Mem: 988 88 646 50 254 814
Swap: 511 0 511

Step 6: Add newly created swap partition to /etc/fstab file. It should look as below:

/dev/sdb swap swap defaults 0 0

2. Create swap partition for lvm

You can have an LVM installation on your server and you need to create a swap partition. The procedure is not exactly the same because of "lvm mode"

Step 1:  We must first create the LVM2 logical volume of size 8 GB:

#lvcreate rootvg -n swapvol -L 8G

Step 2: After creating the logical volume, we need to format the new swap space:

# mkswap /dev/rootvg/swapvol

Step 3: To be sure that our swap partition will be mounted automatically even if we restart the server, we need to add the following entry to the /etc/fstab file:

/dev/rootvg/swapvol swap swap defaults 0 0

Step 4: Now we need to enable the extended logical volume:

# swapon -v /dev/rootvg/swapvol

To test if the logical volume was successfully created, use swapon -s or free -m  command to inspect the swap space.

3. Extend swap partition for lvm

You can need to extend your swap partition because the actual swap size doesn't satisfy your job. With lvm, it is possible to directly increase the size of an existing partition as below.

Step 1: You must first identify the swap volume group which is dev/rootvg/swapvol in our case. You need first to disable the current swapping

# swapoff -v /dev/rootvg/swapvol

Step 2: Now you must resize the volume group to indicate the space to increase

# lvm lvresize /dev/rootvg/swapvol -L +8G

We want to increase from 8 GB to 16 GB

Step 3: Now we need to format the space

# mkswap /dev/rootvg/swapvol

Step 4: Now we need to activate the swap for devices marked as swap in /etc/fstab

# swapon -va

4. Remove swap partition for lvm

For some reasons, you can need to remove you swap partition in lvm mode.

Step 1: To remove a swap partition, you first need to disable the swapping for the associated logical volume whether it is lvm or something else there

# swapoff -v /dev/rootvg/swapvol

Step 2: The second principle is to remove the volume so you need to delete the swap partition entirely.

lvremove /dev/rootvg/swapvol

Step 3: Now we need to remove the following entry from the /etc/fstab file

/dev/rootvg/swapvol swap swap defaults 0 0

Conclusion

Swap partition is important on a server and you can need to manipulate it. We have explored different types of operations to do on swap partition.

Filed Under : FILE SYSTEM, HOWTOS, LINUX HOWTO

Tagged With :

Free Linux Ebook to Download

Comments (3)

Trackback URL | Comments RSS Feed

  1. Tom says:

    Zach ,

    Thanks for the info. I was looking swap partition on LVM and info you have provided was useful to me. Keep posting.

  2. Monal Sharma says:

    correction in step 5:

    cat /proc/swaps

    not

    cat /proc/swap

Leave a Reply

All comments are subject to moderation.