All Products
Search
Document Center

Elastic Compute Service:Use LVM to create an LV

Last Updated:Jul 13, 2023

This topic describes how to use Logical Volume Manager (LVM) to create a logical volume (LV) on multiple disks.

Background information

LVM is a mechanism that is used to manage disk partitions in Linux. LVM adds a logical layer on top of disks and partitions to allow you to manage and operate disk partitions in a more efficient and flexible manner. The size of an LV can be dynamically adjusted without data loss. Existing LVs remain unchanged even if you add new disks to instances.

The following figure shows the architecture of LVM.

image

Procedure

In this example, the /dev/vdb and /dev/vdc disks are used by LVM to create an LV.

Step 1: Create physical volumes (PVs)

  1. Create multiple disks and attach the disks to a Linux instance without initializing the disks.

    For more information, see Create a disk and Attach a data disk.

  2. Connect to an ECS instance.
    For more information about connection methods, see Connection methods.
  3. Run the following command to install LVM:

    sudo yum install -y lvm2
  4. Run the following command to query information about all disks on the instance:

    lsblk

    If the following command output is returned, the vdb and vdc disks on the instance can be used by LVM to create a scalable LV.

    image.png
  5. Run the following command to create PVs from disks. If you specify multiple disk device names, separate the device names with spaces.

    sudo pvcreate <Disk device name> ... <Disk device name>

    Example: To create two PVs from the /dev/vdb and /dev/vdc disks, run the following command:

    sudo pvcreate /dev/vdb /dev/vdc

    If the following command output is returned, the PVs are created.

    image.png

Step 2: Create a volume group (VG)

  1. Run the following commands to create a VG:

    sudo vgcreate <VG name> <PV name> ...... <PV name>

    Example: To create a VG that is named vg_01 from the /dev/vdb and /dev/vdc PVs, run the following command:

    sudo vgcreate vg_01 /dev/vdb /dev/vdc

    If the following command output is returned, the VG is created.

    image..png
  2. (Optional) Run the following command to add existing PVs to the VG:

    sudo vgextend <VG name> <PV name> ...... <PV name>
  3. Run the following command to query information about the VG:

    sudo vgs

    If the following command output is returned, the VG that is named vg_01 is created. The size of the VG is less than 80 GiB. This is because file systems occupy storage space in the VG.

    image.png

Step 3: Create an LV

  1. Run the following commands to create an LV in the VG:

    sudo lvcreate -L <LV size> -n <LV name> <VG name>
    • <LV size>: Specify the size of the LV. The size must be less than the amount of available space in the VG in which you want to create the LV.

    • <LV name>: Specify a name for the LV. Example: lv01.

    • <VG name>: Specify the name of the VG that you created in Step 2. Example: vg_01.

    Example: To create an LV that is named lv01 and whose size is 55 GiB, run the following command:

    sudo lvcreate -L 55g -n lv01 vg_01

    If the following command output is returned, the LV is created.

    image.png
  2. (Optional) Repeat the preceding step to create more LVs.

Step 4: Create and mount a file system

  1. Run the following command to obtain information about the LV, such as the path and the name of the LV and the VG to which the LV belongs:

    sudo lvdisplay
    image..png
    • LV Path: the path of the LV. Example: /dev/vg_01/lv01.

    • LV Name: the name of the LV. Example: lv01.

    • VG Name: the name of the VG to which the LV belongs. Example: vg_01.

    • LV Size: the size of the LV. Example: 55 GiB.

  2. Run the following command to create a file system on the LV:

    mkfs.<File system format> <LV path>

    You can create a file system of a specific format based on your business requirements. In these examples, an EXT4 or XFS file system is created on the LV whose path is /dev/vg_01/lv01.

    Create an EXT4 file system

    sudo mkfs.ext4 /dev/vg_01/lv01

    Create an XFS file system

    sudo mkfs.xfs /dev/vg_01/lv01
  3. Create a mount point. Example: /media/lv01.

    Note

    If you want to use an existing mount point, skip this step.

    sudo mkdir /media/lv01
  4. Run the following command to mount the file system to the mount point:

    sudo mount <LV path> <Mount point>

    Example: To mount the LV whose path is /dev/vg_01/lv01 to the /media/lv01 directory, run the following command:

    sudo mount /dev/vg_01/lv01 /media/lv01
  5. Run the following command to check whether the LV is mounted:

    df -h

    If the following command output is returned, the LV is mounted to the directory. The file system occupies some storage space on the LV.

    image.png

Step 5: Configure the LV to be automatically mounted on startup

To configure an LV to be automatically mounted on startup, add the mounting information of the LV to the /etc/fstab file.

  1. Run the following command to back up the etc/fstab file:

    sudo cp /etc/fstab /etc/fstab.bak
  2. Run the following command to add the mounting information of an LV to the /etc/fstab file:

    sudo sh -c "echo `blkid <Path of the LV> | awk '{print $2}' | sed 's/\"//g'` <Mount point of the LV> <File system type of the LV> defaults 0 0 >> /etc/fstab"

    Example: The lv01 LV resides in the /dev/vg_01/lv01 path, and an EXT4 file system is created on the LV. To configure lv01 to be automatically mounted to the /media/lv01 directory on startup, run the following command:

    sudo sh -c "echo `blkid /dev/vg_01/lv01 | awk '{print $2}' | sed 's/\"//g'` /media/lv01 ext4 defaults 0 0 >> /etc/fstab"
  3. Run the following command to check whether the mounting information of the LV is added to the file:

    cat /etc/fstab

    If the following command output is returned, the mounting information of the LV is added to the /etc/fstab file.

    image.png
  4. Verify the automatic mounting feature.

    1. Run the following command to remount all file systems that are configured in the /etc/fstab file: If no errors are reported, the LV is mounted to the specified mount point.

      sudo mount -a
    2. Run the following command to query the mounting information of the LV:

      df -Th

      If the following command output is returned, the LV is remounted to the specified directory with the specified file system. The automatic mounting feature takes effect.

      image.png