Extending Volume Group and Creating SWAP Partition

When working with virtual machines in VMware, you might need to manage disk space efficiently by extending a volume group and creating a SWAP partition. In this guide, we will cover how to check existing disks, extend a volume group, and configure SWAP space in a Linux environment.

Step 1. Check Existing Disks and Volume Groups

Before making any changes, it's essential to check the available disks and the current state of volume groups.

Run the following commands.

lsblk   # Lists all block devices, including disks and partitions
pvs     # Displays information about physical volumes
vgs     # Shows details of existing volume groups

Check Existing disks and volume groups

Step 2. Create a Physical Volume

If the new disk has not been initialized as a physical volume, you need to create one. Assuming the new disk is /dev/nvme0n2, use the following command:

pvcreate /dev/nvme0n2

This command initializes the disk to be used with LVM (Logical Volume Manager).

Physical Volume

Step 3. Extend the Volume Group

Identify the existing volume group where you want to add the new disk. If your volume group is named fedora, extend it using:

vgextend fedora /dev/nvme0n2

This command adds the newly created physical volume to the fedora volume group.

Volume Group

Step 4. Create a Logical Volume for SWAP

Now, create a logical volume named swaplv with a size of 5GB:

lvcreate -L 5G -n swaplv fedora

This will allocate 5GB from the volume group for SWAP space.

Logical Volume for SWAP

Step 5. Format the Logical Volume as a SWAP

To make the newly created logical volume usable as SWAP, format it with the following command:

mkswap /dev/fedora/swaplv

Logical Volume as a SWAP

Step 6. Enable the SWAP Partition

Activate the SWAP partition using:

swapon /dev/fedora/swaplv

SWAP Partition

Step 7. Verify the SWAP Partition

Ensure the SWAP partition is active:

swapon --show

This command lists all active SWAP spaces.

SWAP Partition

Step 8. Make the SWAP Partition Persistent

To ensure that the SWAP partition is enabled after a reboot, add an entry to /etc/fstab.

Edit the file

nano /etc/fstab

Add the following line at the end of the file:

/dev/fedora/swaplv none swap sw 0 0

Save and exit (CTRL + X, then Y and Enter).

SWAP Partition Persistent

Step 9. Reboot and Verify

Reboot the system to apply the changes:

reboot

After rebooting, confirm that the SWAP partition is still active:

swapon --show

Reboot and verify

Checking Memory Usage Before and After Configuration

Before creating and enabling the SWAP partition, check the memory usage:

free -h

Checking Memory Usage

After completing all the above steps, run the same command again to see the changes:

free -h

Checking Memory Usage

You should notice an increase in the available SWAP space, reflecting the newly added SWAP partition.

Conclusion

By following these steps, you have successfully extended a volume group, created a logical volume for SWAP, and configured it to persist after reboot. This ensures better memory management and system stability, especially for systems running resource-intensive applications.

Up Next
    Ebook Download
    View all
    Learn
    View all