All Products
Search
Document Center

Elastic Compute Service:Create an LV

Last Updated:May 11, 2024

Logical Volume Manager (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 a logical volume (LV) can be dynamically adjusted, which does not cause data loss on disks. Existing LVs remain unchanged even if you add new disks to instances. This topic describes how to use LVM to create an LV on multiple disks. In the example, two new disks named /dev/vdb and /dev/vdc are used.

The following figure shows the architecture of LVM.

image
Note

LVM helps you flexibly manage disk partitions, but increases O&M complexity. You can also select multiple disks for isolation, which is similar to partitioning. A single disk does not need to be partitioned. For more information, see Create a file system on a raw disk. You can resize disks. For more information, see Overview.

Procedure

Step 1: Create physical volumes (PVs)

  1. Create and attach two disks to the destination instance without initializing the disks.

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

  2. Connect to an ECS instance.

    For information about the connection methods, see Connection method overview.

  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 command to create a VG:

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

    Example: To create a VG 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_01 VG is created. The size of the VG is less than 80 GiB, because file systems occupy storage space in the VG.

    image.png

Step 3: Create an LV

  1. Run the following command 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) To create multiple LVs in the VG, repeat Step 1.

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:

    sudo mkfs.<File system format> <LV path>

    You can create a file system in a specific format based on your business requirements. In the examples, an Ext4 or XFS file system is created on the LV that resides in the /dev/vg_01/lv01 directory.

    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 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 the lv01 LV 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 of the specific file system type is remounted to the specified directory. The automatic mounting feature takes effect.

      image.png

References

If the space of an LV is insufficient, you can use one of the following methods to extend the LV:

  1. Resize the disks on which the LV is created in the ECS console. For more information, see Step 1: Resize a disk to extend its capacity.

  2. Extend the LV by using LVM. For more information, see Use LVM to extend an LV.