How to Create and Expand a Volume Group in Linux

Estimated reading: 5 minutes 316 views

Managing storage in Linux is crucial for system administrators. In this guide, we’ll walk through the process of creating a volume group from a partition and then expanding it by adding a new partition. This is particularly useful for managing dynamic storage needs and efficiently using available disk space.

For this guide, we will use a sample VPS with the following disk configuration:

[root@luminous-draft ~]# fdisk -l
Disk /dev/vda: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: gpt
Disk identifier: C8F3EA69-6D03-4D04-B932-B0EC3C79743C

Device      Start      End  Sectors  Size Type
/dev/vda1    2048     4095     2048    1M BIOS boot
/dev/vda2    4096   266239   262144  128M EFI System
/dev/vda3  266240 41943006 41676767 19.9G Linux filesystem


Disk /dev/vdb: 500 GiB, 536870912000 bytes, 1048576000 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/vdc: 500 GiB, 536870912000 bytes, 1048576000 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

In this setup, we have:

  • /dev/vda (20 GiB) as the main system disk.
  • /dev/vdb (500 GiB) and /dev/vdc (500 GiB) which will be used to create and expand our volume group.

Step 1: Install LVM2

Before proceeding with creating a volume group, make sure that lvm2 is installed on your system. If the pvcreate command is not found, you need to install lvm2.

1.1 Install LVM2

To install lvm2, run the following command depending on your Linux distribution:

  • On CentOS/RHEL/Fedora:
    yum install lvm2
  • On Ubuntu/Debian:
    apt-get install lvm2

Once installed, verify the installation by running:

pvcreate --version

This should display the version of pvcreate, confirming that lvm2 is installed successfully.

Step 2: Create a Volume Group from a Partition

To begin, we need to create a volume group from an existing partition. In this example, we assume you have a disk /dev/vdb with 500 GiB that you want to use.

2.1 Create a Physical Volume (PV)

The first step is to create a Physical Volume (PV) from the disk. This PV will later be used to create a volume group.

Run the following command:

pvcreate /dev/vdb

This command initializes the disk so that it can be used in a volume group.

2.2 Create a Volume Group (VG)

Now, create a volume group from the physical volume you just created. Let’s call the volume group vg_data:

vgcreate vg_data /dev/vdb

This command creates the volume group vg_data, which now contains the physical volume /dev/vdb.

2.3 Create a Logical Volume (LV)

Next, create a logical volume within the volume group. Let’s name it lv_data and use the entire available space:

lvcreate -n lv_data -l 100%FREE vg_data

This command creates the logical volume lv_data within the volume group vg_data using all available space.

2.4 Format and Mount the Logical Volume

After creating the logical volume, format it with a file system, such as ext4:

mkfs.ext4 /dev/vg_data/lv_data

Then, create a directory where the volume will be mounted, and mount it:

mkdir /data
mount /dev/vg_data/lv_data /data

To ensure the volume is automatically mounted after reboot, add an entry to /etc/fstab:

/dev/vg_data/lv_data /data ext4 defaults 0 0

Step 3: Expand the Volume Group with a New Partition

After some time, you may find that your storage needs have increased, and you need more space. Fortunately, Logical Volume Management (LVM) makes it easy to expand existing storage by adding new partitions.

3.1 Create a Physical Volume from the New Disk

Suppose you have a new disk /dev/vdc with 500 GiB that you want to add to the existing volume group.

First, create a physical volume from the new disk:

pvcreate /dev/vdc

This command initializes the new disk so that it can be added to a volume group.

3.2 Add the New Physical Volume to the Volume Group

Now, add the newly created physical volume to the existing volume group vg_data:

vgextend vg_data /dev/vdc

This command extends the volume group vg_data to include the new physical volume /dev/vdc.

3.3 Extend the Logical Volume

After expanding the volume group, the next step is to extend the logical volume to use the newly added space:

lvextend -l +100%FREE /dev/vg_data/lv_data

This command extends the logical volume lv_data to use all of the free space available in the volume group.

3.4 Resize the File System

Finally, you need to resize the file system so that it can utilize the newly added space. Depending on the file system, use the appropriate command:

  • For ext4:
    resize2fs /dev/vg_data/lv_data
  • For xfs:
    xfs_growfs /dev/vg_data/lv_data

Conclusion

Managing storage with Logical Volume Management (LVM) in Linux is a powerful way to handle dynamic storage requirements. By following these steps, you can easily create a volume group, add logical volumes, and expand them as your storage needs grow. This approach allows you to efficiently allocate disk space and avoid the need for disruptive reconfigurations in the future.

Whether you’re managing a personal server or an enterprise environment, understanding how to create and expand volume groups is an essential skill for ensuring your systems have the storage they need.

Share this Doc

How to Create and Expand a Volume Group in Linux

Or copy link

CONTENTS