LVM利用Linux内核的device-mapper来实现存储系统的虚拟化(系统分区独立于底层硬件)。 通过LVM,你可以实现存储空间的抽象化并在上面建立虚拟分区(virtual partitions),可以更简便地扩大和缩小分区,可以增删分区时无需担心某个硬盘上没有足够的连续空间。1
LVM的基本组成块(building blocks)如下:
物理卷Physical volume (PV):可以在上面建立卷组的媒介,可以是硬盘分区,也可以是硬盘本身或者回环文件(loopback file)。物理卷包括一个特殊的header,其余部分被切割为一块块物理区域(physical extents)。
卷组Volume group (VG):将一组物理卷收集为一个管理单元。
逻辑卷Logical volume (LV):虚拟分区,由物理区域(physical extents)组成。
物理区域Physical extent (PE):硬盘可供指派给逻辑卷的最小单位(通常为4MB)。
逻辑存储单元logical extent(LE):
PE是物理存储单元,而LE则是逻辑存储单元,也即为lv中的逻辑存储单元,和pe的大小是一样的。从vg中划分lv,实际上是从vg中划分vg中的pe,只不过划分lv后它不再称为pe,而是成为le。
dm(device mapper):将一个或多个底层块设备组织成一个逻 辑设备的模块
linux 中设备名:/dev/dm-#
为了方便使用,LVM 存在两个软链接
- /dev/mapper/VG_NAME-LV_NAME
- /dev/VG_NAME/LV_NAME
LV是从VG中划分出来的,LV中的PE很可能来自于多个PV。在向LV存储数据时,有多种存储机制,其中两种是:
尽管striped读写性能较好也不建议使用该模式,因为lvm的着重点在于弹性容量扩展而非性能,要实现性能应该使用RAID来实现,而且使用striped模式时要进行容量的扩展和收缩将比较麻烦。默认的是使用线性模式。2
在通过磁盘创建LVM时,需要在修改分区本身的分区类别,如果直接使用磁盘,则不需要修改。LVM的分区类型为:8e。
dm: device mapper:将一个或多个底层块设备组织成一个逻 辑设备的模块 设备名:/dev/dm-#
用于显示pv信息的命令有:pvs
,pvscan
,pvdisplay
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 VolGroup lvm2 a--u 19.51g 0
/dev/sdb1 vg0 lvm2 a--u 5.01g 8.00m
/dev/sdc1 vg0 lvm2 a--u 5.01g 5.01g
/dev/sdd1 vg0 lvm2 a--u 5.01g 5.01g
/dev/sde1 vg0 lvm2 a--u 5.01g 5.01g
[root@localhost ~]# pvscan -u
PV /dev/sdb1 with UUID rvCk7p-MunV-2rbS-Ciwl-5zVX-USHk-0ue8Hi VG vg0 lvm2 [5.01 GiB / 8.00 MiB free]
PV /dev/sdc1 with UUID uEZ433-e2ou-QjwG-Faqb-OKPu-09fM-RpKF2e VG vg0 lvm2 [5.01 GiB / 5.01 GiB free]
PV /dev/sdd1 with UUID MM6l3C-7Okj-eYSX-PdIu-2Xki-ZnOi-u9wmUj VG vg0 lvm2 [5.01 GiB / 5.01 GiB free]
PV /dev/sde1 with UUID BksyNI-HUjB-0hSV-egHG-3GDw-BPkZ-S5Pand VG vg0 lvm2 [5.01 GiB / 5.01 GiB free]
PV /dev/sda2 with UUID 0flXKZ-P8ZB-at1K-pmnr-XTIf-LimS-i05Tn6 VG VolGroup lvm2 [19.51 GiB / 0 free]
Total: 5 [39.54 GiB] / in use: 5 [39.54 GiB] / in no VG: 0 [0 ]
[root@localhost ~]# pvscan
PV /dev/sdb1 VG vg0 lvm2 [5.01 GiB / 8.00 MiB free]
PV /dev/sdc1 VG vg0 lvm2 [5.01 GiB / 5.01 GiB free]
PV /dev/sdd1 VG vg0 lvm2 [5.01 GiB / 5.01 GiB free]
PV /dev/sde1 VG vg0 lvm2 [5.01 GiB / 5.01 GiB free]
PV /dev/sda2 VG VolGroup lvm2 [19.51 GiB / 0 free]
Total: 5 [39.54 GiB] / in use: 5 [39.54 GiB] / in no VG: 0 [0 ]
[root@localhost ~]# pvdisplay /dev/sdb1
--- Physical volume ---
PV Name /dev/sdb1
VG Name vg0
PV Size 5.01 GiB / not usable 2.10 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 1282
Free PE 2
Allocated PE 1280
PV UUID rvCk7p-MunV-2rbS-Ciwl-5zVX-USHk-0ue8Hi
[root@localhost ~]# pvdisplay /dev/sdb1 -m
--- Physical volume ---
PV Name /dev/sdb1
VG Name vg0
PV Size 5.01 GiB / not usable 2.10 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 1282
Free PE 2
Allocated PE 1280
PV UUID rvCk7p-MunV-2rbS-Ciwl-5zVX-USHk-0ue8Hi
--- Physical Segments ---
Physical extent 0 to 1279:
Logical volume /dev/vg0/lv0
Logical extents 0 to 1279
Physical extent 1280 to 1281:
FREE
使用pvcreate 命令创建pv
[root@localhost ~]# pvcreate /dev/sd{b,c,d,e}1
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdc1" successfully created
Physical volume "/dev/sdd1" successfully created
Physical volume "/dev/sde1" successfully created
使用pvremove命令删除对应的pv
用于删除一个存在的物理卷
[root@localhost ~]# pvremove /dev/sd{b,c,d,e}1
Labels on physical volume "/dev/sdb1" successfully wiped
Labels on physical volume "/dev/sdc1" successfully wiped
Labels on physical volume "/dev/sdd1" successfully wiped
Labels on physical volume "/dev/sde1" successfully wiped
使用pvmove
pvmove -n vgname /dev/sdb1
将sdb1中属于vgname的数据移动到vgname逻辑卷的其他区域
pvresize :扩展本身的空间
[root@localhost ~]# pvcreate /dev/sde
Physical volume "/dev/sde" successfully created
[root@localhost ~]# pvs /dev/sde
PV VG Fmt Attr PSize PFree
/dev/sde lvm2 ---- 40.00g 40.00g
之后虚拟机扩展硬盘本身空间到50G
[root@localhost ~]# pvresize /dev/sde
Physical volume "/dev/sde" changed
1 physical volume(s) resized / 0 physical volume(s) not resized
[root@localhost ~]# pvs /dev/sde
PV VG Fmt Attr PSize PFree
/dev/sde lvm2 ---- 50.00g 50.00g
pvchange:允许管理员改变物理卷的分配许可。如果物理卷出现故障,可以使用pvchange命令禁止分配物理卷上的PE。
[root@localhost ~]# pvchange -u /dev/sde
Physical volume "/dev/sde" changed
1 physical volume changed / 0 physical volumes not changed
[root@localhost ~]# blkid /dev/sde
/dev/sde: UUID="eLC0Ea-brQU-bDJ6-DylC-c2yL-AcqG-0heGXE" TYPE="LVM2_member"
[root@localhost ~]# pvchange -x n /dev/sde
Allocatability not supported by orphan lvm2 format PV /dev/sde
Physical volume /dev/sde not changed
0 physical volumes changed / 1 physical volume not changed
[root@localhost ~]# pvchange -x y /dev/sde
Allocatability not supported by orphan lvm2 format PV /dev/sde
Physical volume /dev/sde not changed
0 physical volumes changed / 1 physical volume not changed
vgcreate
:用于创建LVM卷组
[root@localhost ~]# vgcreate -p 3 -l 1 vg0 /dev/sdb1 /dev/sdc1 /dev/sde /dev/sdd1
No space for '/dev/sdd1' - volume group 'vg0' holds max 3 physical volume(s).
Unable to add physical volume '/dev/sdd1' to volume group 'vg0'.
[root@localhost ~]# vgcreate -p 3 -l 1 vg0 /dev/sdb1 /dev/sdc1 /dev/sde
Volume group "vg0" successfully created
[root@localhost ~]# lvcreate -L 5G -n lv0 vg0
Logical volume "lv0" created.
[root@localhost ~]# lvcreate -L 5G -n lv1 vg0
Maximum number of logical volumes (1) reached in volume group vg0
[root@localhost ~]#
vgs
: 显示逻辑卷的概要信息[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
VolGroup 1 2 0 wz--n- 19.51g 0
vg0 3 1 0 wz--n- 60.01g 55.01g
vgscan
: 查找所有的卷组[root@localhost ~]# vgscan
Reading all physical volumes. This may take a while...
Found volume group "vg0" using metadata type lvm2
Found volume group "VolGroup" using metadata type lvm2
vgdisplay
: 显示卷组详细信息
[root@localhost ~]# vgdisplay vg0
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 1
Cur LV 1
Open LV 0
Max PV 3
Cur PV 3
Act PV 3
VG Size 60.01 GiB
PE Size 4.00 MiB
Total PE 15363
Alloc PE / Size 1280 / 5.00 GiB
Free PE / Size 14083 / 55.01 GiB
VG UUID QmnfQY-xKbj-7uUV-H0uZ-b4Lu-SA9p-2iPYDm
vgremove
: 用于用户删除LVM卷组
[root@localhost ~]# vgremove vg0
WARNING: Locking disabled. Be careful! This could corrupt your metadata.
Do you really want to remove volume group "vg0" containing 1 logical volumes? [y/n]: y
Logical volume "lv0" successfully removed
Volume group "vg0" successfully removed
vgrename
: 重命名卷组的名称[root@localhost ~]# vgrename vg0 vg100
WARNING: Locking disabled. Be careful! This could corrupt your metadata.
Volume group "vg0" successfully renamed to "vg100"
[root@localhost ~]# vgchange -an vg100
WARNING: Locking disabled. Be careful! This could corrupt your metadata.
0 logical volume(s) in volume group "vg100" now active
[root@localhost ~]# vgchange -ay vg100
WARNING: Locking disabled. Be careful! This could corrupt your metadata.
1 logical volume(s) in volume group "vg100" now active
主要用于将卷组更换主机时使用
[root@localhost ~]# vgchange -a n vg0
0 logical volume(s) in volume group "vg0" now active
[root@localhost ~]# vgexport vg0
Volume group "vg0" successfully exported
[root@localhost ~]# vgdisplay vg0
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 3
VG Access read/write
VG Status exported/resizable
MAX LV 1
Cur LV 1
Open LV 0
Max PV 3
Cur PV 3
Act PV 3
VG Size 60.01 GiB
PE Size 4.00 MiB
Total PE 15363
Alloc PE / Size 1280 / 5.00 GiB
Free PE / Size 14083 / 55.01 GiB
VG UUID QmnfQY-xKbj-7uUV-H0uZ-b4Lu-SA9p-2iPYDm
[root@localhost ~]# vgimport vg0
Volume group "vg0" successfully imported
[root@localhost ~]# vgdisplay vg0
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 1
Cur LV 1
Open LV 0
Max PV 3
Cur PV 3
Act PV 3
VG Size 60.01 GiB
PE Size 4.00 MiB
Total PE 15363
Alloc PE / Size 1280 / 5.00 GiB
Free PE / Size 14083 / 55.01 GiB
VG UUID QmnfQY-xKbj-7uUV-H0uZ-b4Lu-SA9p-2iPYDm
vgextend : 用于动态扩展LVM卷组,它通过向卷组中添加物理卷(pv)来增加卷组的容量
vgreduce:通过删除LVM卷组中的物理卷(pv)来减少卷组容量
如果对应的pv在使用,则无法移除
[root@localhost ~]# vgextend vg100 /dev/sde
WARNING: Locking disabled. Be careful! This could corrupt your metadata.
Volume group "vg100" successfully extended
[root@localhost ~]# vgreduce vg100 /dev/sde
WARNING: Locking disabled. Be careful! This could corrupt your metadata.
Removed "/dev/sde" from volume group "vg100"
[root@localhost ~]# vgreduce vg100 /dev/sdb1
WARNING: Locking disabled. Be careful! This could corrupt your metadata.
Physical volume "/dev/sdb1" still in use
lvs
: 查看逻辑卷大致信息[root@localhost ~]# lvs
WARNING: Locking disabled. Be careful! This could corrupt your metadata.
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_root VolGroup -wi-ao---- 17.51g
lv_swap VolGroup -wi-ao---- 2.00g
lv100 vg100 -wi-a----- 5.00g
lvscan
: 扫描系统中的所有逻辑卷[root@localhost ~]# lvscan
WARNING: Locking disabled. Be careful! This could corrupt your metadata.
ACTIVE '/dev/vg100/lv100' [5.00 GiB] inherit
ACTIVE '/dev/VolGroup/lv_root' [17.51 GiB] inherit
ACTIVE '/dev/VolGroup/lv_swap' [2.00 GiB] inherit
lvdisplay
: 用于显示LVM逻辑卷空间大小、读写状态和快照信息等属性。[root@localhost ~]# lvdisplay /dev/vg100/lv100
WARNING: Locking disabled. Be careful! This could corrupt your metadata.
--- Logical volume ---
LV Path /dev/vg100/lv100
LV Name lv100
VG Name vg100
LV UUID 9YzC3b-rAJs-Lh8u-cogN-3fdK-P8ii-bvwCPc
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2018-10-06 00:15:26 +0800
LV Status available
# open 0
LV Size 5.00 GiB
Current LE 1280
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
取消其中警报的方式:编辑/etc/lvm/lvm.conf中开启lock,locking_type = 1
lvcreate
: 用于创建LVM的逻辑卷。
[root@localhost ~]# lvcreate -n lv1 -l 100 vg100
Logical volume "lv1" created.
#一般PE大小为4M 所以通过上面那个命令创建出来的逻辑卷是400M
[root@localhost ~]# lvcreate -n lv2 -l 60%VG vg100
Logical volume "lv2" created.
#占用VG%60空间创建逻辑卷
[root@localhost ~]# lvdisplay /dev/vg100/lv2
--- Logical volume ---
LV Path /dev/vg100/lv2
LV Name lv2
VG Name vg100
LV UUID hRGa2T-OuIl-RH1s-5OYq-h31J-Z2Fk-MTtMBs
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2018-10-06 01:21:36 +0800
LV Status available
# open 0
LV Size 39.01 GiB
Current LE 9987
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:4
[root@localhost ~]# lvcreate -n lv3 -l 60%FREE vg100
Logical volume "lv3" created.
#使用剩余可用空间的60%创建逻辑卷
[root@localhost ~]# lvdisplay /dev/vg100/lv3
--- Logical volume ---
LV Path /dev/vg100/lv3
LV Name lv3
VG Name vg100
LV UUID 4M70ie-eMOo-7pdr-r81f-yNLi-Bfv4-urNrIJ
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2018-10-06 01:22:46 +0800
LV Status available
# open 0
LV Size 12.37 GiB
Current LE 3166
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:5
lvremove
: 用于删除指定LVM逻辑卷。
[root@localhost ~]# lvremove /dev/vg100/lv3
Do you really want to remove active logical volume lv3? [y/n]: y
Logical volume "lv3" successfully removed
lvextend
: 用于在线扩展逻辑卷的空间大小,而不中断应用程序对逻辑卷的访问。
+ :表示增加#空间,如果没有+表示直接增加到对应空间大小
[root@localhost ~]# lvextend -L +4G /dev/vg100/lv100
Size of logical volume vg100/lv100 changed from 9.00 GiB (2304 extents) to 13.00 GiB (3328 extents).
Logical volume lv100 successfully resized.
[root@localhost ~]# resize2fs /dev/vg100/lv100
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg100/lv100 is mounted on /mnt; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg100/lv100 to 3407872 (4k) blocks.
The filesystem on /dev/vg100/lv100 is now 3407872 blocks long.
如果直接在lvextend之后 -r ,可以直接扩展文件系统大小。
[root@localhost ~]# lvextend -L +4G -r /dev/vg100/lv100
Size of logical volume vg100/lv100 changed from 13.00 GiB (3328 extents) to 17.00 GiB (4352 extents).
Logical volume lv100 successfully resized.
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg100-lv100 is mounted on /mnt; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/mapper/vg100-lv100 to 4456448 (4k) blocks.
The filesystem on /dev/mapper/vg100-lv100 is now 4456448 blocks long.
lvreduce
: 用于减少LVM逻辑卷占用的空间大小。
用法与
lvextend
类似
lvreduce 中使用-
,与lvextend中+
用法类似
另外在减少逻辑卷空间之前,需要卸载以及e2fsck 检查文件系统
[root@localhost ~]# e2fsck -f /dev/vg100/lv0
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg100/lv0: 11/655360 files (0.0% non-contiguous), 76783/2621440 blocks
[root@localhost ~]# lvreduce -L -5G -r /dev/vg100/lv0
fsck from util-linux-ng 2.17.2
/dev/mapper/vg100-lv0: clean, 11/655360 files, 76783/2621440 blocks
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/vg100-lv0 to 1310720 (4k) blocks.
The filesystem on /dev/mapper/vg100-lv0 is now 1310720 blocks long.
Size of logical volume vg100/lv0 changed from 10.00 GiB (2560 extents) to 5.00 GiB (1280 extents).
Logical volume lv0 successfully resized.
lvresize 可以同时实现
lvreduce
,lvextend
,用法与这两个命令用法一样。
lvrename
:重命令逻辑卷
[root@localhost ~]# lvrename /dev/vg100/lv0 /dev/vg100/lv100
Renamed "lv0" to "lv100" in volume group "vg100"
注:硬盘中存在逻辑卷
快照是特殊的逻辑卷,它是在生成快照时存在的逻辑卷的准确拷贝。 对于需要备份或者复制的现有数据集临时拷贝以及其它操作来说, 快照是最合适的选择。
快照只有在它们和原来的逻辑卷不同时才会消耗空间 。在生成快照时会分配给它一定的空间,但只有在原来的逻辑卷或 者快照有所改变才会使用这些空间。 当原来的逻辑卷中有所改变时,会将旧的数据复制到快照中。快照中只含有原来的逻辑卷中更改的数据或者自生成快照后的快 照中更改的数据。 建立快照的卷大小只需要原始逻辑卷的15%~20%就够了,也可 以使用lvextend放大快照 。
逻辑卷中的快照可以直接通过lvcreate
创建有快照功能的逻辑卷实现这个功能。直接指明逻辑卷需要创建的逻辑卷可以在原有的卷组中创建快照卷信息。创建的快照卷无需创建文件系统即可使用。
lvcreate -n lv0-snapshot -L 1G -s -p r /dev/vg0/lv0
lvconvert --merge /dev/vg0/lv0-snapshot
[root@localhost /]# lvcreate -L 1G -s -n lv100-snapshot-1 -p r /dev/vg100/lv100
Logical volume "lv100-snapshot-1" created.
[root@localhost /]# mount /dev/vg100/lv100-snapshot-1 /media/
mount: block device /dev/mapper/vg100-lv100--snapshot--1 is write-protected, mounting read-only
[root@localhost /]# cd /media/
[root@localhost media]# ls
adjtime cron.hourly fstab issue.net mtab profile.d rsyslog.d sysconfig
aliases cron.monthly gai.conf kdump-adv-conf my.cnf protocols rwtab sysctl.conf
aliases.db crontab gcrypt kdump.conf netconfig quotagrpadmins rwtab.d
[root@localhost media]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
18G 1.2G 16G 8% /
tmpfs 939M 0 939M 0% /dev/shm
/dev/sda1 477M 40M 412M 9% /boot
/dev/mapper/vg100-lv100
4.8G 40M 4.6G 1% /mnt
/dev/mapper/vg100-lv100--snapshot--1
4.8G 40M 4.6G 1% /media
[root@localhost /]# lvconvert --merge vg100/lv100-snapshot
Merging of volume lv100-snapshot started.
lv100: Merged: 100.0%
Merge of snapshot into logical volume lv100 has finished.
Logical volume "lv100-snapshot" successfully removed
#通过lvconvert --merge 命令回复数据, 如果使用这个命令之后快照会自动消失。恢复数据时,需要卸载快照的逻辑卷否则恢复会失败。
可以看到目录创建快照的目录中有原来的逻辑卷信息,快照卷的空间在挂载时可以看到和原来的逻辑卷空间相同,但是实际只有1G
Skipping clustered volume group
。需要在配置文件/etc/lvm/lvm.conf
关闭lock,locking_type = 0
维基百科 : LVM (简体中文) ↩︎
博客园 : 第6章 LVM详解 ↩︎