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.
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)
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.
Connect to an ECS instance.
For information about the connection methods, see Connection method overview.
Run the following command to install LVM:
sudo yum install -y lvm2
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.
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.
Step 2: Create a volume group (VG)
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.
(Optional) Run the following command to add existing PVs to the VG:
sudo vgextend <VG name> <PV name> ... <PV name>
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.
Step 3: Create an LV
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.
(Optional) To create multiple LVs in the VG, repeat Step 1.
Step 4: Create and mount a file system
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
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.
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
orXFS
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
Create a mount point. Example:
/media/lv01
.NoteIf you want to use an existing mount point, skip this step.
sudo mkdir /media/lv01
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
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.
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.
Run the following command to back up the
etc/fstab
file:sudo cp /etc/fstab /etc/fstab.bak
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 anExt4
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"
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.Verify the automatic mounting feature.
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
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.
References
If the space of an LV is insufficient, you can use one of the following methods to extend the LV:
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.
Extend the LV by using LVM. For more information, see Use LVM to extend an LV.