Logical Volume Management

Source: Internet
Author: User

First, the basic concept of LVM
When planning the partition size of a disk, sometimes it is not possible to determine the total amount of space to be used by this partition, and after partitioning the disk with Fdisk, the size of each partition is fixed, if the partition is set too large, the disk space is wasted, and the partition setting is too small, it will lead to the situation of insufficient space. The most common approach at this point is to repartition the partition, or link the directory of this partition to another partition by means of a soft connection, although it can solve the problem temporarily, but it brings trouble to management. How to solve these problems, LVM is a good way.
LVM, the abbreviation for Logical Volume Manager, is a logical volume management, which is a mechanism for managing disk partitions under Linux, and LVM is a logical layer between disk partitions and file systems. The administrator uses LVM to dynamically resize partitions without repartitioning the disk. If a new hard disk is added to the system, the additional hard disk space can be extended directly to the original partition via LVM.

Second, the use of LVM terminology
By using LVM technology, the underlying differences of disk partitioning are shielded, the concept of a volume is logically provided to the file system, and then the corresponding file system is established on these volumes, and before the LVM is known, it is familiar with several commonly used terminology in LVM, which mainly involves the following concepts.
? Physical storage device (physical media): refers to the system's storage device files, such as:/DEV/SDA,/dev/hdb, and so on.
? Physical volume (physical volume): referred to as PV, refers to a hard disk partition or a device that logically looks similar to a hard disk partition (such as a RAID device).
? Volume group (Volume Group): VG, similar to a physical hard disk in a non-LVM system, an LVM volume group has one or more physical volumes.
? Logical volumes (logical volume): For short, LV, similar to hard disk partitioning on non-LVM systems, the LV is built on the VG, and the file system can be created on the LV.
? PE (physical extent): the smallest storage unit that can be allocated in PV is a pe,pe size that can be specified, and the default is 4M.
? Le (logical extent): the smallest storage unit that can be allocated in LV is called Le, and in the same volume group, le is the same size as PE, and one by one corresponds.
Figure 1 clearly illustrates the correspondence between the various components of the LVM:

Figure 1 LVM Disk Organization chart

In Figure 1:
? There are two physical hard disks that make up the underlying structure of the LVM, and the two hard drives can be of different sizes and models.
? PV can be seen as a partition on a hard disk, so you can say that physical hard disk a divides two partitions, and physical hard disk B divides 3 partitions.
? The first three PV is then composed of a volume group VG1, and the latter two PV make up a volume group of VG2.
? Then two logical volumes LV1 and LV2 are divided on the volume group VG1, and a separate logical volume LV3 is divided on the volume group VG2.
? Finally, the file systems are created on logical volumes LV1, LV2, and LV3, respectively, to mount the/usr,/home, and/var partitions.

Iii. Installing LVM Tools
Confirm that the LVM is installed by using the following command:
[Email protected] ~]# Rpm-qa|grep LVM
Lvm2-2.02.16-3.el5
System-config-lvm-1.0.22-1.0.el5
If there is an output similar to the above, it indicates that the system has already installed LVM, if there is no output, then the system has not installed LVM, you can find the corresponding RPM package installation from the system installation CD or network, installation method is not described here.
LVM is now generally supported by the default Linux distribution kernel, so you can use the power provided by LVM as long as you have the LVM tools installed.

Iv. creation and management of LVM
The creation of LVM requires the following steps:

1. Create a physical partition
Before using LVM, you need to partition the partitions first, that is, partitioning the partitions with the FDISK command, which has been described in the previous section, slightly different, when creating partitions, you need to specify the partition type as Linux LVM, The corresponding ID is 8e (in fact LVM can also recognize the Linux default partition type 83),
Here we specify all partition types as 8e, assuming that two new drives are added under Linux, and the partitions are displayed as follows:
[Email protected] ~]# fdisk-l/DEV/SDB/DEV/SDC

disk/dev/sdb:8589 MB, 8589934592 bytes
255 heads, Sectors/track, 1044 cylinders
Units = Cylinders of 16065 * 8225280 bytes

Device Boot Start End Blocks Id System
/DEV/SDB1 1 1044 8385898+ 5 Extended
/DEV/SDB5 1 2008062 8e Linux LVM
/DEV/SDB6 251 2008093+ 8e Linux LVM
/DEV/SDB7 501 2008093+ 8e Linux LVM
/DEV/SDB8 751 1044 2361523+ 8e Linux LVM

disk/dev/sdc:8589 MB, 8589934592 bytes
255 heads, Sectors/track, 1044 cylinders
Units = Cylinders of 16065 * 8225280 bytes

Device Boot Start End Blocks Id System
/DEV/SDC1 1 1044 8385898+ 5 Extended
/DEV/SDC5 1 499 4008154+ 8e Linux LVM
/DEV/SDC6 1044 4377681 8e Linux LVM
From the above output, the/DEV/SDB hard disk divides an extended partition and four logical partitions, each of which is a Linux LVM,/DEV/SDC disk partition with an extended partition and two logical partitions, and the partition type is Linux LVM.
The main purpose of this two hard drive example is to highlight the concept that LVM can operate across disks. That is, LVM can operate different partitions on multiple physical hard disks, and the LVM manages the disks, shielding the heterogeneity of the underlying physical devices.
Note that this is only a partitioning operation and the disk partition has not been formatted yet.

2. Create physical volume (PV)
The command to create a physical volume is pvcreate, which allows you to create all the disk partitions that you want to add to the volume group (VG) or the entire disk as a physical volume. Use the format:
pvcreate  disk partition or entire disk
Here, create a physical volume for all partitions in the first step, as follows:
[[email protected] ~]# pvcreate / DEV/SDB5/DEV/SDB6/DEV/SDB7/DEV/SDB8/DEV/SDC5/DEV/SDC6
  Physical Volume "/DEV/SDB5" successfully created
  Physical Volume "/DEV/SDB6" successfully created
  physical volume "/dev/sdb7" successfully created< br>  physical Volume "/DEV/SDB8" successfully created
  physical volume "/DEV/SDC5" successfully created
  Physical Volume "/DEV/SDC6" successfully created

3. Create a volume group
The command to create a volume group is vgcreate, using the format:
Vgcreate Volume group Name Physical volume
Volume group name: is the name of the volume group to be created.
Physical Volume: Specifies all disk partitions or entire disks that you want to add to this volume group.
Here we create two volume groups Myvg1 and MYVG2, while two volume groups span different disks, and the volume group MYVG1 consists of/DEV/SDB5 partitions of/DEV/SDB6,/DEV/SDC, and/DEV/SDC5 disks of/DEV/SDB disks. Similarly, the volume group MYVG2 consists of/DEV/SDC partitions of the/DEV/SDB7,/DEV/SDB8, and/dev/sdc6 disks of the/DEV/SDB disk. The operation is as follows:
[Email protected] ~]# vgcreate MYVG1/DEV/SDB5/DEV/SDB6/DEV/SDC5
Volume Group "MYVG1" successfully created
[Email protected] ~]# vgcreate MYVG2/DEV/SDB7/DEV/SDB8/DEV/SDC6
Volume Group "MYVG2" successfully created

4. Activating a volume group
After the volume group has been created, you can activate the volume group with the Vgchange command without restarting the system. Vgchange uses the following format:
Vgchange-a y Volume Group name (Activate volume group)
Vgchange-a N Volume Group name (deactivate volume group)
Here we activate the volume group MYVG1 and MYVG2, using the following command:
[Email protected] ~]# vgchange-a y myvg1
0 Logical Volume (s) in volume group ' MYVG1 ' now active
[Email protected] ~]# vgchange-a y myvg2
0 Logical Volume (s) in volume group ' myvg2 ' now active

5. Display volume group, physical volume property information
Vgdisplay is used to display the volume group information that is created, and the Pvdisplay command is used to display physical volume group information in the format:
Vgdisplay Volume Group Name
Pvdisplay Physical Volume name
To display the information for the volume group MYVG1, perform the following actions:
[Email protected] ~]# Vgdisplay MYVG1
---Volume Group---
VG Name MYVG1
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 1
VG Access Read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 3
ACT PV 3
VG Size 7.65 GB
PE Size 4.00 MB
Total PE 1958
Alloc pe/size 0/0
Free Pe/size 1958/7.65 GB
VG UUID H6RMCI-9F2C-QLPW-BNL2-07AP-J0H7-XPQ4FQ
From the output, you can see all of this volume group information, such as the volume group size is 7.65 GB, each PE size is 4M, the total number of PE is 1958.

6. Creating a logical Volume
The command to create a logical volume is lvcreate, and is commonly used in the following formats:
Lvcreate [-l Logical Volume Size |-L PE number]–n The volume group name that the logical volume name belongs to
which
-L: The size of the logical volume can be followed by K, M, G. such as 100M, 10G, etc.
-L: Calculates the size of the logical volume with the number of PE.
Here we create two logical volumes MYLV11 and MYLV12 under Volume group MYVG1, create a volume group Mylv2 under volume group MYVG2:
[Email protected] ~]# lvcreate-l 4g-n mylv11 myvg1
#在卷组myvg1中创建一个大小为4G的卷组mylv11
Logical volume "mylv11" created
[[email protected] ~]# vgdisplay myvg1|grep "Free PE"
#检查卷组myvg1中可用的卷组空间
Free Pe/size 934/3.65 GB
[Email protected] ~]# lvcreate-l 934-n mylv12 myvg1
#将卷组myvg1中剩余的空间全部分给逻辑卷mylv12
Logical volume "MYLV12" created
[[email protected] ~]# vgdisplay myvg1|grep "Free PE"
#检查卷组myvg1中是否还有剩余空间, from the output, the space is fully utilized, no waste.
Free Pe/size 0/0
[[email protected] ~]# vgdisplay myvg2|grep "Free PE"
#检查卷组myvg2的可用空间
Free Pe/size 2134/8.34 GB
[Email protected] ~]# lvcreate-l 2134-n mylv2 myvg2
#将卷组myvg2的所有空间分配给逻辑卷mylv2
Logical volume "Mylv2" created

7. Format logical volumes, create file systems
The file system is created on a logical volume, assuming that the Linux default Ext3 file system is used, and the next task is to format the file system with MKFS.EXT3:
[Email protected] ~]# mkfs.ext3/dev/myvg1/mylv11
[Email protected] ~]# MKFS.EXT3/DEV/MYVG1/MYLV12
[Email protected] ~]# Mkfs.ext3/dev/myvg2/mylv2
Finally, set up the Mount directory to mount these logical volumes:
[[Email protected] ~] #mkdir/mylv11
[[Email protected] ~] #mkdir/MYLV12
[Email protected] ~]# Mkdir/mylv2
[Email protected] ~]# mount/dev/myvg1/mylv11/mylv11
[Email protected] ~]# MOUNT/DEV/MYVG1/MYLV12/MYLV12
[Email protected] ~]# Mount/dev/myvg2/mylv2/mylv2
[Email protected] ~]# Df-h|grep MYLV
Filesystem Size used Avail use% mounted on
/dev/mapper/myvg1-mylv11 4.0G 137M 3.7G 4%/mylv11
/DEV/MAPPER/MYVG1-MYLV12 3.6G 72M 3.4G 3%/mylv12
/dev/mapper/myvg2-mylv2 8.3G 147M 7.7G 2%/mylv2
So far, the new disk device is ready to use, if you want to boot automatically mount, edit the/etc/fstab file, plus the addition of three logical partitions.

8. Add a new physical volume to a volume group
The command to add a new physical volume to the volume group is vgextend, using the format:
Vgextend Volume group name newly added physical volume
Followed by the above example, assume that the system added a fourth hard disk/DEV/SDD, the size of 4G, while the disk partition through FDISK, the corresponding partition is/DEV/SDD5, now to add the new hard disk partition to the volume group MYVG2, the operation is as follows:
[[email protected] ~]# vgdisplay myvg2|grep "Free PE" #首先检查myvg2卷是否有空闲空间, the output shows that all the space is exhausted.
Free Pe/size 0/0
[[email protected] ~]# pvcreate/dev/sdd5 #先将/dev/sdd5 convert to physical volume
Physical volume "/DEV/SDD5" successfully created
[Email protected] ~]# vgextend myvg2/dev/sdd5 #将新增物理卷添加到卷组myvg2中
Volume Group "MYVG2" successfully extended
[[email protected] ~]# vgdisplay myvg2|grep "Free PE" #此时剩余卷组空间还有4G, just the size of the new physical volume
Free Pe/size 1023/4.00 GB

9. To modify the size of a logical volume
The main function of LVM is to dynamically adjust the size of the partition, in fact, to modify the size of the logical volume, modify the logical volume required to use the command has Lvextend (extended logical volume), Lvreduce (reduced logical volume) and ext2resize (Modify the file system size), The following are described separately.
Lvextend is similar to the format used by Lvreduce, as follows:
Lvextend [-L (+size) –l (+pe number)] Logical volume name
Lvreduce [-L (-size) –l (-pe number)] Logical volume name
-L: "+size" means that the specified size is increased for the logical volume space, and "-size" indicates the size specified for the logical volume space reduction. A size indicates that the logical volume is increased (lvextend) or reduced (lvreduce) to the specified space size. For example, "+300" means that the logical volume space is increased by 300M, and " -2g" means that the space of the logical volume is reduced by 2G.
-L: The same as the "-l" usage, the difference is that the "-l" option represents the size of the logical volume with a PE number.
Ext2resize is used in the following format:
Ext2resize device file or logical volume space size
Modifying logical volumes is divided into two cases of expanding the logical volume size and reducing the size of the logical volume, which is described in turn.
(1) Expansion of logical volume space
The general steps for expanding the logical volume size are:
? Extending logical volume space with the extended Logical Volume command lvextend
? Uninstalling a logical volume partition that is already mounted
? Use Ext2resize directives to modify file system size for space expansion
? Mount the expanded logical volume partition
In the last section, we added a new physical volume to the volume group MYVG2, and now we want to extend the new physical volume space to the logical volume MYLV2 as follows:
To expand the size of the logical volume MYLV2 first, execute the following command:
[Email protected] ~]# lvextend-l +1023/dev/myvg2/mylv2
Extending logical Volume Mylv2 to 12.33 GB
Logical Volume Mylv2 successfully resized
Note that "+1023" after the "-l" option represents the increased number of PE, which is obtained by the command "Vgdisplay myvg2|grep" Free PE "in the previous section, and of course the"-L-+4g "is used instead of"-l +1023 ".
The next task is to unload the/DEV/MYVG2/MYLV2 partition and prepare to modify the file system size.
[[Email protected] ~] #umount/dev/myvg2/mylv2 or execute Umount/mylv2
The tool that modifies the file system size is the ext2resize directive, which may not be included in some Linux distributions, and Redhat Linux does not have this command by default, so you need to install the RPM package for this instruction first. Download a rpm file similar to ext2resize-1.1.19-1.i386.rpm and install it.
[Email protected] ~]# RPM-IVH ext2resize-1.1.19-1.i386.rpm
Preparing ... ###########################[100%]
1:ext2resize ##########################[100%]
When the installation is complete, a ext2resize instruction will appear, which can be used to modify the file system size.
The third step, modify the file system size, execute the following command:
[Email protected] ~]# Ext2resize/dev/myvg2/mylv2
Ext2resize V1.1.18-2001/03/18 for Ext2fs 0.5b
Since the extended partition has been added to the logical volume MYLV2, the above operation is actually refreshing the logical volume for the change to take effect.
Finally, mount the logical volume to the file system and execute the following command:
[Email protected] ~]# Mount/dev/myvg2/mylv2/mylv2
[Email protected] ~]# df-h |grep Mylv2
Filesystem Size used Avail use% mounted on
/dev/mapper/myvg2-mylv2 13G 147M 12G 2%/mylv2
As you can see, the logical volume/DEV/MYVG2/MYLV2 space has been increased to 13G, and the dynamic expansion of the logical volume succeeded.
(2) Reduce the space of logical volumes
The general steps for reducing a logical volume are:
? Uninstalling a logical volume partition that is already mounted
? Use Ext2resize directives to modify file system size for space reduction
? Reduce logical volume space with reduced logical volume command lvreduce
? mount a reduced logical volume partition
Suppose we want to/dev/myvg2/mylv2 the logical volume above by 2G, we can do this.
First unload the logical volume/DEV/MYVG2/MYLV2 partition:
Umount/mylv2
Then use the ext2resize instruction to reduce the file system size:
Ext2resize/dev/myvg2/mylv2 11G
Then using the lvreduce instruction to reduce the logical volume/DEV/MYVG2/MYLV2 to 11G, the main purpose of this step is to release the reduced space from/dev/myvg2/mylv2, which can be used by other logical volumes.
[Email protected] ~]# lvreduce-l 11g/dev/myvg2/mylv2 #缩减逻辑卷/dev/myvg2/mylv2 to 11G
Warning:reducing Active Logical Volume to 11.00 GB
This May DESTROY YOUR DATA (filesystem etc)
Does really want to reduce mylv2? [y/n]: y #这里输入y
Reducing logical volume MYLV2 to 11.00 GB
Logical Volume Mylv2 successfully resized
[Email protected] ~]# Ext2resize/dev/myvg2/mylv2 #将修改结果加载到文件系统
Ext2resize V1.1.18-2001/03/18 for Ext2fs 0.5b
Ext2resize:new size is same as current (2883584)
Finally, re-mount the logical volume/DEV/MYVG2/MYLV2
[[email protected] ~]# mount/dev/myvg2/mylv2/mylv2 #挂载逻辑卷分区到/mylv2 Directory
[[email protected] ~]# df-h|grep mylv2 #查看挂载情况, you can see that the logical volume partition has been reduced to 11G
Filesystem Size used Avail use% mounted on
/dev/mapper/myvg2-mylv2 11G 147M 11G 2%/mylv2

10. How to delete physical volumes, volume groups, and logical volumes
The command to remove the physical volume is pvremove, the command to remove the physical volume from the volume Group is vgreduce, and the command to delete the volume group is vgremove, and the command to delete the logical volume is lvremove.
Before deleting a volume group, you need to understand the composition of the volume group and the logical volume, for example, to remove the volume group MYVG1, you need to know the relationship is: Volume group myvg1 have/DEV/SDB5,/DEV/SDB6 and/DEV/SDC5 three physical volumes, Then two logical volumes MYLV11 and MYLV12 are divided under Volume group MYVG1.
As an example, the order in which a volume group is deleted is to unload the logical volume partition---Remove all logical volumes on the volume group--Delete the volume group, and remove the volume group Myvg1 as follows:
[Email protected] ~]# umount/dev/myvg1/mylv11 #首先卸载逻辑卷mylv11对应的分区
[Email protected] ~]# lvremove/dev/myvg1/mylv11 #删除逻辑卷mylv11
Do you really want to remove active logical Volume "mylv11"? [y/n]: Y
Logical volume "mylv11" successfully removed
[[email protected] ~]# lvremove/dev/myvg1/mylv12 #由于没有卸载逻辑卷mylv12对应的分区, failed to delete logical volume
Can ' t remove open Logical Volume "MYLV12"
[Email protected] ~]# UMOUNT/DEV/MYVG1/MYLV12 #卸载逻辑卷mylv12对应的分区
[Email protected] ~]# LVREMOVE/DEV/MYVG1/MYLV12 #成功删除逻辑卷mylv12
Do you really want to remove active logical Volume "MYLV12"? [y/n]: Y
Logical volume "MYLV12" successfully removed
[Email protected] ~]# vgreduce MYVG1/DEV/SDB5 #这里是从卷组myvg1中移除一个物理卷
Removed "/DEV/SDB5" from volume Group "MYVG1"
[Email protected] ~]# PVREMOVE/DEV/SDB5 #这里是删除物理卷/DEV/SDB5
Labels on physical volume "/DEV/SDB5" successfully wiped
[Email protected] ~]# vgremove myvg1 #这里是删除卷组myvg1
Volume Group "MYVG1" successfully removed

Reprint: http://ixdba.blog.51cto.com/2895551/562111

There is also a better article: http://dreamfire.blog.51cto.com/418026/1084729

Logical Volume Management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.