LVM is short for LogicalVolumeManager, which provides a higher level of disk storage management capabilities for hosts. LVM helps system administrators easily allocate storage space for applications and users. Logical volumes under LVM management can be changed or removed as needed. In addition, LVM can provide custom name identifiers for managed logical volumes.
LVM is short for LogicalVolumeManager, which provides a higher level of disk storage management capabilities for hosts. LVM helps system administrators easily allocate storage space for applications and users. Logical volumes under LVM management can be changed or removed as needed. In addition, LVM can provide custom name identifiers for managed logical volumes. Therefore, LVM facilitates the management of the storage system and increases the scalability of the system.
Currently, LVM has two versions in Linux: LVM1 and LVM2. LVM2 is not only an upgrade to the version and function of Linux logical volume management, the architecture is based on a new kernel storage subsystem (DM, device-mapper. This storage subsystem provides a lightweight and scalable volume management facility. In addition to the original LVM volume management function, Linux logical volume management will provide users with more storage management solutions, such as images, encrypted volumes, and multi-path technology. This section takes LVM 2 as an example to describe how to create and manage LVM.
Concepts and terminologies related to LVM
LVM is a logical layer between the disk partition and the file system. it shields the underlying disk partition layout for the file system, provides a logical disk volume, and then establishes a file system on the disk volume.
Many terms related to LVM are involved in the above LVM structure. the detailed descriptions of these terms are as follows:
Physical volume (physical volume, PV)
A physical volume refers to a hard disk partition, or an entire hard disk or a created soft RAID. it is the basic storage device of LVM, the difference from a common physical storage medium is that the device package contains LVM-related management parameters.
Volume group (volume group, VG)
A volume Group is a storage pool composed of one or more physical volumes. one or more LVM partitions (logical volumes) can be created in the volume group ).
Logical volume (LV)
The logical volume of LVM is similar to the hard disk partition in a non-LVM system. it is built on a volume group and is a standard block device. a file system can be built on a logical volume. We can make such an idea to understand the relationship between the above three: If PV is compared to a plate of the Earth, VG is a Earth, because the Earth is composed of multiple plates, if we divide a region on the Earth and mark it as Asia, Asia is equivalent to a LV.
Physical extent, PE)
Physical volumes are the basic unit of storage, and are the minimum unit of LVM addressing.
Logical Block (logical extent, LE)
Logical volumes are the basic unit of storage. in the same volume group, the size of LE and PE are equal and one-to-one.
Volume Group description Area (Volume Group Descriptor Area, VGDA)
Just as the disk stores the metadata containing the partition information in the partition table at the starting position of the partition, the metadata related to the logical volume and volume Group is stored in the VGDA of the physical volume. VGDA includes PV descriptor, VG descriptor, LV descriptor, and PE descriptor. When LVM is started, the system activates the VG and loads the VGDA to the memory to identify the actual physical storage location of the LV. When the system performs I/O operations, it accesses the actual physical location based on the ing mechanism established by VGDA.
Create logical volume
To create a logical volume, follow these steps:
Create a partition
Create a physical volume
Create a volume Group
Activate a volume Group
Create logical volume
Create a file system
The following describes the entire process of creating a logical volume through a specific instance.
[Instance 1]
Create a volume group using the/dev/sdb,/dev/sdc,/dev/sdd disks, and then create two logical volumes on the volume group, and specify the ing mode of a logical volume as strip ing. the implementation steps are as follows:
1. create a physical volume
The command for creating a physical volume is "pvcreate", which can be used to create a partition or disk to be added to a volume group as a physical volume. The procedure is as follows:
# Pvcreate/dev/sdb/dev/sdc/dev/sdd
Physical volume "/dev/sdb" successfully created
Physical volume "/dev/sdc" successfully created
Physical volume "/dev/sdd" successfully created
2. create a volume Group
The command for creating a volume group is "vgcreate". the physical volume created using "pvcreate" is created as a complete volume group, as shown below:
# Vgcreate vgtest/dev/sdb/dev/sdc/dev/sdd
Volume group "vgtest" successfully created
The first parameter of the vgcreate command is to specify the name of the volume group as "vgtest". The name can be customized as needed, followed by the physical volume added to the volume group. You can also specify the PE size when using vgcreate to create a volume group. you need to add the-s parameter. the PE size range is 8 KB to 16 GB, and it must always be a multiple of 2. Up to 65534 PES can exist in a volume group. The default PE size is 4 MB, which indicates that all logical volumes created in the volume group are expanded or reduced in 4 MB increments, therefore, a 4 mb pe determines that the maximum capacity of a single logical volume is 256 GB. to create a larger logical volume, you must specify a larger PE when creating a volume group.
3. create a logical volume
After creating a volume group, you can use the "lvcreate" command to create a logical volume. when creating a logical volume, you can also specify the logical block and physical block ing mode, there are two logical volume ing modes:
Linear mode ?? Maps physical blocks in a certain range to logical blocks in sequence, which is also the default ing method. For example, map the LE from 1 to 25 in a logical volume to PV1 and the LE from 26-50 to PV2.
Strip mode ?? Maps logical blocks to different physical volumes in the form of strip. this method is similar to RAID0 mentioned earlier. this method can improve the read/write performance of logical volumes. For example, map LE 1 of a logical volume to PE1 of PV1, LE 2 to PE1 of PV2, and LE3 to PE1 of PV3.
To create a logical volume, perform the following operations:
# Lvcreate-I 3-I 4-L 180 M-n lvtest1 vgtest
Logical volume "lvtest1" created
The above prompt indicates that a logical volume named "lvtest1" is successfully created. The following describes several parameters in the command:
-I: Creates a logical volume using the strip mode ing. The value of this parameter is used to specify that the created logical volume is mapped to several PVS.
-I: specify the block size used when the strip mode is used. unit: KB. The value must be 2N (N ≥ 2 ).
-L: specifies the size of the logical volume to be created. The Unit K, M, G, and T can be used to represent KB, MB, GB, and TB.
-N: specifies the name of the created logical volume, which can be defined as needed.
When you use the "-I" parameter to specify the number of PVS, make sure that the specified PVs are not completely allocated to any logical volume. Otherwise, the creation will fail, second, if the PV size is not the same, the created logical volume can only take the minimum value.
Next, use the remaining space to create the second logical volume. run the "vgdisplay" command to view the remaining space of the current volume group, as shown below:
# Vgdisplay
-Volume group-
VG Name vgtest
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 11
VG Access read/write
VG Status resizable
Max lv 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 3
Act PV 3
VG Size 300.00 MB
PE Size 4.00 MB
Total PE 75
Alloc PE/Size 45/180 .00 MB
Free PE/Size 30/120 .00 MB
Vg uuid 1Nbgxo-U4Ux-Y5Hb-TDzT-d5Fp-Iov7-wKFHG5
From the above information, we can see that the "vgtest" volume group still has 30 idle PES. divide all the free spaces into another logical volume as follows:
# Lvcreate-l 30-n lvtest2 vgtest
Logical volume "lvtest2" created
Here, "-l" is used to specify the number of logical volumes. the default value is 4 MB. Therefore, the size of the created logical volume is 120 MB. this parameter has the same effect as "-L.
4. create a file system
The method for creating a file system on a logical volume is the same as that for creating a file system on a partition. for example, to create a file system in ext3 format on a logical volume, run the following command:
# Mkfs. ext3/dev/vgtest/lvtest1
# Mkfs. ext3/dev/vgtest/lvtest2
The preceding steps are used to create a logical volume in Linux.
LVM maintenance
During routine LVM maintenance, operations such as logical volume expansion and new physical volume addition are usually required, the following describes common LVM management operations based on the logical volume created in instance 1:
1. activate the volume Group
If the volume group is not activated, the volume Group and logical volume cannot be accessed. by default, the volume group created in LVM 2 is activated. the command to manually activate the volume group is:
# Vgchange-a y vgtest
2 logical volume (s) in volume group "vgtest" now active
"-A" is used to set whether the specified volume Group is available. the parameter "y" indicates that the volume Group is activated. if it is set to "n", the volume Group is suspended.
2. add a new physical volume to the volume Group
When the volume group space is insufficient, you can add a new physical volume to expand the volume group capacity. you can use the "vgextend" command to achieve this, as shown below:
# Vgextend vgtest/dev/sde
Volume group "vgtest" successfully extended
"/Dev/sde" is the new physical volume, provided that the device has been initialized using "pvcreate.
3. remove a physical volume from the volume Group
To remove a physical volume from a volume group, first make sure that the physical volume to be removed is not in use by any logical volume. you can view the physical volume information by running the "pvdisplay" command, as follows:
# Pvdisplay/dev/sde
-Physical volume-
PV Name/dev/sde
VG Name vgtest
PV Size 100.00 MB/not usable 0
Allocatable yes
PE Size (kbytes) 4096
Total PE 25
Free PE 25
Allocated PE 0
Pvuuid ee2IFC-UHUq-Xg67-icwt-AhdJ-7kRz-JdDjiq
If "Total PE" is equal to "FreePE", it indicates that the physical volume is not used. if a physical volume is being used by the logical volume, you need to transfer the data of the physical volume to another place and then remove it. The command to remove a physical volume is vgreduce, as shown below:
# Vgreduce vgtest/dev/sde
Removed "/dev/sde" from volume group "vgtest"
4. transfer data between physical volumes
When an error is found on a disk in a logical volume or data needs to be transferred from the physical volume (for example, replacing a physical device with a large capacity or higher speed ), you can use "pvmove" to transfer data between physical volumes. for example, to transfer data from a physical volume "/dev/sdd" to "/dev/sde", perform the following operations:
# Pvmove/dev/sdd/dev/sde
/Dev/sdd: Moved: 100.0%
Specifically, the size of/dev/sde must be greater than or equal to/dev/sdd. second, run the "modprobedm-mirror" command to load the dm-mirror module before "pvmove, because "pvmove" requires this module when transferring data, and the system does not load this module by default.
5. extend the logical volume
As mentioned above, LVM provides the function to easily adjust the logical volume size. the command to expand the logical volume size is "lvextent", for example, adjust the space of "lvtest2" to 160 MB as follows:
# Lvextend-L 160 M/dev/vgtest/lvtest2
Extending logical volume lvtest2 to 160.00 MB
Logical volume lvtest2 successfully resized
You can also increase the corresponding size on the original base, as shown below:
# Lvextend-L + 40 M/dev/vgtest/lvtest2
Extending logical volume lvtest2 to 160.00 MB
Logical volume lvtest2 successfully resized
The logical volume does not take effect immediately after expansion. you need to use the "resize2fs" command to reload the size of the logical volume. This command is only applicable to ext2/ext3 file systems (if it is the reiserfs file system, use the "resize_reiserfs" command). if the logical volume is in use, uninstall the logical volume before executing the "resize2fs" command. The operation is as follows:
# Umount/dev/vgtest/lvtest2
# Resize2fs/dev/vgtest/lvtest2
Resize2fs 1.35 (28-Feb-2004)
The filesystem is already 163840 blocks long. Nothing to do!
# Mount/dev/vgtest/lvtest2/mnt/lvtest2/
These operations can be directly implemented using the "ext2online" command, so that you do not need to detach the logical volume or perform other operations, as shown below:
# Ext2online/dev/vgtest/lvtest2
Ext2online v1.1.18? 2001/03/18 for EXT2FS 0.5b
6. delete a logical volume
Before deleting a logical volume, you must uninstall it. the command is "lvremove", as shown below:
# Lvremove/dev/vgtest/lvtest2
Do you really want to remove active logical volume "lvtest2 ″? [Y/n]: y
Logical volume "lvtest2" successfully removed
Enter "y" to confirm deletion of the logical volume.
7. remove a volume Group
Before removing a volume group, make sure that no logical volume exists in the volume group, or you can use vgchange to manually stop the volume group, to remove a volume group, perform the following operations:
# Vgremove vgtest
Volume group "vgtest" successfully removed
8. delete a physical volume
Before deleting a physical volume, make sure that the physical volume has been removed from the volume group. The command to delete the physical volume is "pvremove", as shown below:
# Pvremove/dev/sde
Labels on physical volume "/dev/sde" successfully wiped
In addition, LVM can also provide snapshot and other functions. Its snapshot can quickly back up data in the current logical volume, thus greatly reducing the burden on backup data.