首页 > 代码库 > LVM逻辑卷管理
LVM逻辑卷管理
LVM逻辑卷管理
LVM(Logical Volume Manager)是基于内核的一种逻辑卷管理器,适合大存储设备,并允许动态的调整文件系统的大小。
LVM的概念示意图:
PE(physical Extend):LVM中的最小存储单位,默认PE的大小为4M。
PV(physical volume):物理卷是LVM中最底层的概念。
VG(volume group):卷组是LVM逻辑概念上的磁盘设备,是由单个或者多个PV组合后生成卷组。
LV(logical volume):逻辑卷是就是LVM逻辑意义上的分区。指定从卷组从拿走多少容量来创建逻辑卷,最后进行挂载使用。
流程:首先将一块或者多块硬盘创建成PV,之后创建一个新的卷组(VG),把PV加入到卷组(GV)里面,如果我们要使用多大LV,就从VG里面里面提取多大的空间(随机的),如上图所示。
创建LVM分区实例:
这里我们使用sdb,sdc这两块为1G的硬盘作为实验。创建一个为test_vg的卷组,然后从该卷组中创建出分别为500M和600M的逻辑卷。
使用fdisk -l查看分区表
[root@dianel ~]# fdisk -l Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/sdc: 1073 MB, 1073741824 bytes, 2097152 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
使用pvcreate创建物理卷,并用pvdisplay查看结果
[root@dianel ~]# pvcreate /dev/sdb /dev/sdc Physical volume "/dev/sdb" successfully created. Physical volume "/dev/sdc" successfully created. [root@dianel ~]# pvdisplay /dev/sdb /dev/sdc "/dev/sdb" is a new physical volume of "1.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb VG Name PV Size 1.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID TTLiRU-NAEM-7irI-ghgc-gbaT-vwv6-dvV3kQ "/dev/sdc" is a new physical volume of "1.00 GiB" --- NEW Physical volume --- PV Name /dev/sdc VG Name PV Size 1.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID m8QdiJ-wYN6-JSsd-AGGB-M1oC-jkf5-5OpDcU
使用cgcreate创建卷组并使用vgdisplay查看结果
[root@dianel ~]# vgcreate test_vg /dev/sdb /dev/sdc Volume group "test_vg" successfully created [root@dianel ~]# vgdisplay --- Volume group --- VG Name test_vg System ID Format lvm2 Metadata Areas 2 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 2 Act PV 2 VG Size 1.99 GiB PE Size 4.00 MiB Total PE 510 Alloc PE / Size 0 / 0 Free PE / Size 510 / 1.99 GiB VG UUID BfM0C3-IxyK-IwGG-eODI-ilnv-GOkK-b mNso4
使用lvcreate创建逻辑卷并使用lvdisplay查看结果
[root@dianel ~]# lvcreate -L 500M -n test_web test_vg Logical volume "test_web" created. [root@dianel ~]# lvcreate -L 600M -n test_data test_vg Logical volume "test_data" created. [root@dianel ~]# lvdisplay ......(省略部分) --- Logical volume --- LV Path /dev/test_vg/test_web LV Name test_web VG Name test_vg LV UUID R2ky1r-KQ0O-5dU1-23KV-9j5c-r3dx-bbzvCX LV Write Access read/write LV Creation host, time dianel, 2017-04-26 16:09:56 +0800 LV Status available # open 0 LV Size 500.00 MiB Current LE 125 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2 --- Logical volume --- LV Path /dev/test_vg/test_data LV Name test_data VG Name test_vg LV UUID poSaRr-46UO-qZzB-TJnJ-kIUY-7XzP-TKZhdZ LV Write Access read/write LV Creation host, time dianel, 2017-04-26 16:10:14 +0800 LV Status available # open 0 LV Size 600.00 MiB Current LE 150 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:3
格式化并且挂载
[root@dianel ~]# mkfs.xfs -f /dev/test_vg/test_data [root@dianel ~]# mkfs.xfs -f /dev/test_vg/test_web [root@dianel ~]# mkdir -p /test/{web,data} [root@dianel ~]# mount /dev/test_vg/test_data /test/data/ [root@dianel ~]# mount /dev/test_vg/test_web /test/web/ [root@dianel ~]# mount ..... /dev/mapper/test_vg-test_data on /test/data type xfs (rw,relatime,seclabel,attr2,inode64,noquota) /dev/mapper/test_vg-test_web on /test/web type xfs (rw,relatime,seclabel,attr2,inode64,noquota) [root@dianel ~]# df -h /dev/mapper/test_vg-test_data 597M 31M 567M 6% /test/data /dev/mapper/test_vg-test_web 497M 26M 472M 6% /test/web
修改LVM分区容量
如果VG内有足够的空间可以划分给LV,可以直接使用lvextend命令动态调整逻辑卷的大小。再使用xfs_growfs命令更新文件系统大小。
[root@dianel data]# lvextend -L +500M /dev/test_vg/test_data Size of logical volume test_vg/test_data changed from 600.00 MiB (150 extents) to 1.07 GiB (275 extents). Logical volume test_vg/test_data successfully resized. #查看LV已经扩展完成 [root@dianel data]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert test_data test_vg -wi-ao---- 1.07g test_web test_vg -wi-ao---- 500.00m #但是文件系统并没有变化 [root@dianel data]# df -h Filesystem Size Used Avail Use% Mounted on ..... /dev/mapper/test_vg-test_data 597M 31M 567M 6% /test/data /dev/mapper/test_vg-test_web 497M 26M 472M 6% /test/web #调整文件系统 [root@dianel data]# xfs_growfs /dev/test_vg/test_data [root@dianel data]# df -h Filesystem Size Used Avail Use% Mounted on ...... /dev/mapper/test_vg-test_data 1.1G 31M 1.1G 3% /test/data /dev/mapper/test_vg-test_web 497M 26M 472M 6% /test/web
如果(VG)卷组没有足够的空间时,这时需要动态调整卷组的大小了,即需要往卷组里面加入PV,也就是加硬盘。
#新添加了两块为分别为1G的硬盘 [root@dianel ~]# fdisk -l Disk /dev/sdd: 1073 MB...... Disk /dev/sde: 1073 MB...... #之前的卷组 [root@dianel ~]# vgdisplay test_vg --- Volume group --- VG Name test_vg ...... VG Size 1.99 GiB PE Size 4.00 MiB Total PE 510 Alloc PE / Size 400 / 1.56 GiB Free PE / Size 110 / 440.00 MiB #创建PV [root@dianel ~]# pvcreate /dev/sde /dev/sdd Physical volume "/dev/sde" successfully created. Physical volume "/dev/sdd" successfully created. #扩展VG [root@dianel ~]# vgextend test_vg /dev/sdd Volume group "test_vg" successfully extended [root@dianel ~]# vgextend test_vg /dev/sde Volume group "test_vg" successfully extended #现在的的VG [root@dianel ~]# vgdisplay test_vg --- Volume group --- VG Name test_vg ...... VG Size 3.98 GiB Total PE 1020 Free PE / Size 620 / 2.42 GiB
删除LVM分区: 不需要逻辑卷时,便可以删除逻辑卷。删除的顺序与创建时相反,先卸载文件系统=>>删除LG=>>删除VG=>>删除PV。
[root@dianel ~]# umount /dev/test_vg/test_data [root@dianel ~]# umount /dev/test_vg/test_web ++++++++++++++++++ [root@dianel ~]# lvremove /dev/test_vg/test_data Do you really want to remove active logical volume test_vg/test_data? [y/n]: y Logical volume "test_data" successfully removed [root@dianel ~]# lvremove /dev/test_vg/test_web Do you really want to remove active logical volume test_vg/test_web? [y/n]: y Logical volume "test_web" successfully removed #删除卷组 [root@dianel ~]# vgremove test_vg Volume group "test_vg" successfully removed ++++++++++++++++++++++ [root@dianel ~]# pvremove /dev/sdd /dev/sde Labels on physical volume "/dev/sdd" successfully wiped. Labels on physical volume "/dev/sde" successfully wiped.
2017/4/26 18:03:01
本文出自 “dianel简单不简单” 博客,转载请与作者联系!
LVM逻辑卷管理