Help & Documentation>Cloud Block Storage>Best Practice>Building LVM Logic Volumes with Multiple Elastic Cloud Disks

Building LVM Logic Volumes with Multiple Elastic Cloud Disks

Last updated: 2023-09-19 21:38:15

Introduction to LVM

Logical Volume Manager (LVM) creates a logical layer over your disks or partitions to divide them into physical extent (PE) units with the same size. This categorizes disks or partitions into a volume group (VG), on which you can create a logical volume (LV), and then create file systems on the LV. Different from direct disk partitioning, LVM allows elastic scaling of file system.
The file system is not limited by the size of a physical disk. Instead, it can be distributed among multiple disks: For example, you can purchase 3 elastic cloud disks of 4 TB, and use LVM to create a massive file system up to 12 TB.
You can resize the LVs dynamically instead of repartitioning your disks. When the LVM VG capacity cannot meet your needs, you can purchase a elastic cloud disk, attach it to your CVM instance, and add it to the LVM VG to expand capacity.

Building LVM

Note
This article illustrates how to create a dynamically resizable file system using LVM with three elastic cloud disks, as shown in the following figure:


Step 1: create a physical volume (PV)

1. Log in to the Linux CVM as the root user. See Logging in to Linux CVM.
2. Run the following command to create a PV.
pvcreate <disk path 1> ... <disk path N>
Taking /dev/vdc, /dev/vdd, and /dev/vde as examples, execute the following:
pvcreate /dev/vdc /dev/vdd /dev/vde
The following figure shows the command output when the creation is successful:

3. Run the following command to view physical volumes of the system.
lvmdiskscan | grep LVM




Step 2: create a volume group (VG)

1. Run the following command to create a VG.
vgcreate [-s <PE size>] <VG name> <PV path>
Assume you want to create a VG named "lvm_demo0", then run
vgcreate lvm_demo0 /dev/vdc /dev/vdd
The following figure shows the command output when the creation is successful:

When the prompt "Volume group “<Volume Group Name>” successfully created" appears, it indicates that the volume group has been created successfully.
Then you can run the following commands to add a new PV to the VG.
vgextend VG name New PV path
The following figure shows the command output when the operation is successful:

After the volume group is created, you can run commands such as vgs and vgdisplay to view the volume group information in the current system, as shown below:


Step 3: Create a logical volume (LV)

1. Run the following command to create a LV.
lvcreate [-L <size of logical volume>][ -n <name of logical volume>] <name of VG>
Assume you want to create an 8 GB logical volume named "lv_0", then run
lvcreate -L 8G -n lv_0 lvm_demo0
The following figure shows the command output when the creation is successful:

Note
Run the pvs command to see that only /dev/vdc has used 8GB, as shown below:


Step 4: create and mount a file system

1. Run the following command to create a file system on an existing LV.
mkfs.ext3 /dev/lvm_demo0/lv_0
2. Run the following command to create the mount node directory /vg0.
mkdir /vg0
3. Run the following command to mount the file system.
mount /dev/lvm_demo0/lv_0 /vg0
The following figure shows the command output when the mount is successful:


Step 5: resize the logical volume and file system dynamically

Note
The LV capacity can only be dynamically expanded when there is remaining capacity in the VG. After expanding the LV capacity, the size of the file system created on that LV must also be expanded.
1. Run the following command to extend the LV.
lvextend [-L +/- <Increase/Decrease Capacity>] <LV Path>
Assume you want to scale up the capacity of LV named "lv_0" by 4 GB, then run
lvextend -L +4G /dev/lvm_demo0/lv_0
The following figure shows the command output when the scaling is successful:

Note
Run the pvs command to see that /dev/vdc is now fully utilized and /dev/vdd has used 2GB, as shown below:

2. Run the following command to extend the file system.
resize2fs /dev/lvm_demo0/lv_0
The following figure shows the command output when the scaling is successful:

After the extension, run the following command to check whether the LV capacity is 12 GB.
df -h