Logical Volume Manager (LVM) is a Linux mechanism for dynamically managing disks and disk partitions. This topic describes how to use LVM to create a logical volume (LV) on multiple disks of a Linux Elastic Compute Service (ECS) instance.

Prerequisites

Background information

In the LVM architecture, a logical layer is created on top of disks and disk partitions to help you manage disk partitions in a more flexible manner. The size of an LV can be dynamically adjusted without losing existing data. The existing LV remains unchanged even if you add new data disks.

Notice
  • To prevent data loss, an LV cannot be created on disks that contain data.
  • Each disk snapshot can back up only the data of a single disk. If you use a disk snapshot to roll back a disk after LVM is used to partition the disk, data inconsistency occurs. We recommend that you use snapshot-consistent groups to back up data. For more information, see Create snapshots for multiple disks together by creating an instance snapshot.

Step 1: Create physical volumes (PVs)

  1. Connect to an ECS instance as the root user.
    For more information about connection methods, see Connection methods.
  2. Run the following command to view information about all disks on the instance:
    lsblk
    A command output similar to the following one indicates that you can create a scalable LV on five disks by using LVM. lsblk
  3. If LVM version 2 (LVM2) is not installed on your instance, run the following command to install LVM2:
    yum install -y lvm2
  4. Run the following command to create PVs:
    pvcreate <Device name of data disk 1> ... <Device name of data disk N>
    In this example, run the following command to create PVs for the /dev/vdb, /dev/vdc, /dev/vdd, /dev/vde, and /dev/vdf data disks. Separate the device names of multiple data disks with spaces. In actual operations, replace the device names with the device names of your disks.
    pvcreate /dev/vdb /dev/vdc /dev/vdd /dev/vde /dev/vdf
    A command output similar to the following one is returned. pvcreate
  5. Run the following command to view information about the created PVs on the instance:
    lvmdiskscan | grep LVM
    A command output similar to the following one is returned. lvmdiskscan

Step 2: Create a volume group (VG)

  1. Run the following command to create a VG:
    vgcreate <VG name> <Name of PV 1> ......<Name of PV N>
    In this example, run the following command to create the lvm_01 VG and add the /dev/vdb, /dev/vdc, /dev/vdd, /dev/vde, and /dev/vdf PVs. Separate multiple PV names with spaces. In actual operations, replace the VG and PV names with your VG and PV names.
    vgcreate lvm_01 /dev/vdb /dev/vdc /dev/vdd /dev/vde /dev/vdf
    A command output similar to the following one is returned. vgcreate
  2. Optional:Run the following command to add new PVs to the VG:
    vgextend <VG name>  <Name of PV 1> ......<Name of PV N>
    In this example, run the following command to add the /dev/vdg PV to the lvm_01 VG. If you want to add multiple PVs, separate the PV names with spaces.
    vgextend lvm_01 /dev/vdg
    A command output similar to the following one is returned. vgextend
  3. Run the following command to view the VG information:
    vgs
    A command output similar to the following one is returned. vgs

Step 3: Create an LV

  1. Run the following command to create an LV:
    lvcreate [-L <LV size>][ -n <LV name>] <VG name>
    Note
    • LV size: The LV size must be smaller than the remaining free space of the VG. The unit can be MiB, GiB, or TiB.
    • LV name: You can specify a name for the LV.
    • VG name: the name of an existing VG.
    In this example, run the following command to create a 150 GiB LV:
    lvcreate -L 150g -n lv01 lvm_01
    A command output similar to the following one is returned. lvcreate
  2. Run the following command to view the LV details:
    lvdisplay
    A command output similar to the following one is returned. lvdisplay

Step 4: Create and mount a file system

  1. Run the following command to create a file system on the LV:
    mkfs.<File system format> <LV path>
    In these examples, create an ext4 file system and an xfs file system. You can run one of the following commands to create a file system of a specified format that suits your needs.
    • Create an ext4 file system
      mkfs.ext4 /dev/lvm_01/lv01
      A command output similar to the following one is returned. ext4
    • Create an xfs file system
      mkfs.xfs /dev/lvm_01/lv01
      A command output similar to the following one is returned. xfs
  2. Run the following command to create a mount point. Example: /media/lv01.
    If you want to use an existing mount point, skip this step.
    mkdir /media/lv01
  3. Run the following command to mount the file system:

    In this example, set the LV path to /dev/lvm_01/lv01 and set the mount point to /media /lv01. In actual operations, modify the LV path and mount point based on your needs.

    mount /dev/lvm_01/lv01 /media/lv01
  4. Run the following command to view the mount information of the LV:
    df -h
    A command output similar to the following one is returned. df