Building LVM Logic Volumes with Multiple Elastic Cloud Disks

Last updated: 2021-07-20 15:02:24

    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 three 4-TB elastic cloud disks, and use LVM to create a massive file system nearly 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:

    The following example uses three elastic cloud disks to create a dynamically resizable file system through LVM.

    Step 1: create a physical volume (PV)

    1. Log in to the Linux CVM instance as the root user.
    2. Run the following command to create a PV.
      pvcreate <disk path 1> ... <disk path N>
      
      Taking /dev/vdc, /dev/vdd and /dev/vde disks as an example, run the following command.
      pvcreate /dev/vdc /dev/vdd /dev/vde
      
      The result of a successful creation is shown below:
    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 <specified PE size>] <VG name> <PV path>.
      
      Take creating a VG named lvm_demo0 as an example, run the following command:
      vgcreate lvm_demo0 /dev/vdc /dev/vdd
      
      The result of a successful creation is shown below:

      When Volume group" "successfully created is displayed, the VG has been created.
      • Then you can run the following commands to add a new PV to the VG.
        vgextend [VG name] [Path of the new PV]
        
        The result of a successful addition is shown below:
      • After the VG is created, you can run vgs, vgdisplay or other commands to query information about VGs of the system, as shown below:

    Step 3: create a logical volume (LV)

    1. Run the following command to create a LV.
      lvcreate [-L <LV size>][-n <LV name>] <VG name>.
      
      Take creating an 8 GB logical volume named lv_0 as an example, run the following command:
      lvcreate -L 8G -n lv_0 lvm_demo0
      
      The result of a successful creation is shown below:
      Note:

      Run the pvs command. You can see that only the capacity of the /dev/vdc disk is reduced by 8 GB, 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 point /vg0.

      mkdir /vg0
      
    3. Run the following command to mount the file system.

      mount /dev/lvm_demo0/lv_0 /vg0
      

      The result of a successful mount is shown below:

    Step 5: resize the logical volume and file system dynamically

    Note:

    LVs can be extended dynamically only when the VG capacity is not used up. After increasing the LV capacity, you need to extend the file system created on this LV.

    1. Run the following command to extend the LV.
      lvextend [-L +/- <scale capacity>] <LV path>
      
      Take extending the LV named lv_0 by 4 GB as an example, run the following command:
      lvextend -L +4G /dev/lvm_demo0/lv_0
      
      The result of a successful extension is shown below:
      Note:

      Run the pvs command. You can find that the /dev/vdc disk capacity has been fully used, and the /dev/vdd disk capacity has been used by 2 GB, as shown below:

    2. Run the following command to extend the file system.
      resize2fs /dev/lvm_demo0/lv_0
      
      The result of a successful extension is shown below:

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