以下基于CentOS7
LVM的全名为Logical Volume Manager,中文翻译为逻辑卷管理器。LVM将几个物理的分区(磁盘)通过软件组合成一块看起来是独立的大磁盘(VG),然后再将磁盘划分成可使用的分区(LV),最终就能像磁盘分区一样,格式化后就可以挂载使用了。
名词
- 物理卷(Physical Volume PV)
实际的分区需要调整系统标识符(System ID)成为8e(LVM标识符),然后再经过pvcreate将它转换为底层的物理卷(pv)。 - 卷组(Volume Group VG)
由多个PV组合而成的 - 物理扩展块(Physical Extend PE)
LVM默认使用4MB的PE数据块,这个PE有点像文件系统里面的block大小,调整PE会影响到LVM的容量大小,不过Centos6.x以后,直接使用了lvm2的各项格式功能,以及64位系统,这个限制已经不存在了。 - 逻辑卷(Logical Volume LV)
- 最终VG被切割成LV,LV最后可以被格式化使用的类似分区的东西。
建立LV时出现的问题
在虚拟机中添加SCSI格式的磁盘做PV时出现如下报错:
[root@localhost scripts]# pvcreate /dev/sdd
Device /dev/sdd excluded by a filter.
导致问题的原因是添加的磁盘是在另一个虚拟机中新建的,已经有了分区表,现在的虚拟机并不能识别磁盘的分区表,运行parted命令重做分区表
解决方法:
[root@localhost ~]# parted /dev/sdd
GNU Parted 3.1
Using /dev/sdd
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel msdos
Warning: The existing disk label on /dev/sdd will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? yes
(parted) quit
Information: You may need to update /etc/fstab.
分区表重做后执行pvcreate /dev/sde时报错
[root@localhost ~]# pvcreate /dev/sde
Can't open /dev/sde exclusively. Mounted filesystem?
解决方法:
重新启动系统即可
再次运行pvcreate,问是否擦除dos签名,输入y,就可以将磁盘创建为PV了。
PV创建(physical volume)
[root@localhost ~]# pvcreate /dev/sde
WARNING: dos signature detected on /dev/sde at offset 510. Wipe it? [y/n]: y
Wiping dos signature on /dev/sde.
Physical volume "/dev/sde" successfully created.
执行pvscan,查看所有pv
[root@localhost ~]# pvscan
PV /dev/sda3 VG centos lvm2 [16.00 GiB / 4.00 MiB free]
PV /dev/sdf lvm2 [1.00 GiB]
PV /dev/sdc lvm2 [1.00 GiB]
PV /dev/sdb lvm2 [1.00 GiB]
PV /dev/sdd lvm2 [1.00 GiB]
PV /dev/sde lvm2 [1.00 GiB]
Total: 6 [21.00 GiB] / in use: 1 [16.00 GiB] / in no VG: 5 [5.00 GiB]
详细的列出PV信息
[root@localhost ~]# pvdisplay /dev/sdb
"/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 #在此PV内的PE大小
Total PE 0 #共划分多少PE
Free PE 0 #没被LV用掉的PE
Allocated PE 0 #还可以分配的PE数量
PV UUID vO85dV-j5up-FaiW-kdOm-ZQ4y-Q8If-FGigaA
#由于PE是在建立VG时才设置参数,因此在这里看到的PV里面的PE都是0,而且没有多余的PE可供分配(allocatable)
VG创建(Volume Group)
# -s指定PE的大小,单位可以是M,G,T(大小写均可),默认为4M
[root@localhost ~]# vgcreate -s 16M xpyvg /dev/sd{b..e}
Volume group "xpyvg" successfully created
[root@localhost ~]# vgscan
Reading volume groups from cache.
Found volume group "centos" using metadata type lvm2 #系统安装时做的
Found volume group "xpyvg" using metadata type lvm2 #上面手动创建的
[root@localhost ~]# pvscan
PV /dev/sda3 VG centos lvm2 [16.00 GiB / 4.00 MiB free]
PV /dev/sdb VG xpyvg lvm2 [1008.00 MiB / 1008.00 MiB free]
PV /dev/sdc VG xpyvg lvm2 [1008.00 MiB / 1008.00 MiB free]
PV /dev/sdd VG xpyvg lvm2 [1008.00 MiB / 1008.00 MiB free]
PV /dev/sde VG xpyvg lvm2 [1008.00 MiB / 1008.00 MiB free]
PV /dev/sdf lvm2 [1.00 GiB]
#有4个PV被用去,剩下/dev/sdf没有使用
[root@localhost ~]# vgdisplay xpyvg
--- Volume group ---
VG Name xpyvg
System ID
Format lvm2
Metadata Areas 4
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 4
Act PV 4
VG Size <3.94 GiB #整个VG的大小
PE Size 16.00 MiB #内部每个PE的大小
Total PE 252 #PE的数量
Alloc PE / Size 0 / 0
Free PE / Size 252 / <3.94 GiB #尚可分配给LV的PE数量/总容量
VG UUID 6DsvsW-c1n8-9hS0-4o8R-F7Ko-M2os-roLD5f
# 最后三行指的就是PE能够使用的情况,由于尚未划分出LV,因此所有的PE均可自由使用
增加VG的容量
[root@localhost ~]# vgextend xpyvg /dev/sdf
Volume group "xpyvg" successfully extended
[root@localhost ~]# vgdisplay xpyvg
--- Volume group ---
VG Name xpyvg
System ID
Format lvm2
Metadata Areas 5
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 5
Act PV 5
VG Size 4.92 GiB
PE Size 16.00 MiB
Total PE 315
Alloc PE / Size 0 / 0
Free PE / Size 315 / 4.92 GiB
VG UUID 6DsvsW-c1n8-9hS0-4o8R-F7Ko-M2os-roLD5f
移除pv
移除pv前先执行pvmove将pv上的数据移动到其它的pv,然后执行pvreduce /path/to/somewhere即可
LV阶段(logical volume)
创建LV,将xpyvg的2G分给xpylv
#-L指定逻辑卷大小,-n指定名称
[root@localhost ~]# lvcreate -L 2G -n xpylv xpyvg
Logical volume "xpylv" created.
[root@localhost ~]# lvscan
ACTIVE '/dev/centos/root' [10.00 GiB] inherit
ACTIVE '/dev/centos/swap' [1.00 GiB] inherit
ACTIVE '/dev/centos/home' [5.00 GiB] inherit
ACTIVE '/dev/xpyvg/xpylv' [2.00 GiB] inherit
[root@localhost ~]# lvdisplay /dev/xpyvg/xpylv
--- Logical volume ---
LV Path /dev/xpyvg/xpylv
LV Name xpylv
VG Name xpyvg
LV UUID fvYo1F-7lvE-zCGe-ODWT-xJYe-uGBS-biuEoe
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2020-12-20 09:24:04 -0500
LV Status available
# open 0
LV Size 2.00 GiB
Current LE 128
Segments 3
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:3
文件系统阶段
- 格式化
[root@localhost ~]# mkfs.xfs /dev/xpyvg/xpylv
meta-data=/dev/xpyvg/xpylv isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=524288, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
- 挂载
[root@localhost ~]# mkdir /srv/lvm
[root@localhost ~]# mount /dev/xpyvg/xpylv /srv/lvm
[root@localhost lvm]# cp -a /etc /srv/lvm
[root@localhost lvm]# df -Th /srv/lvm
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/xpyvg-xpylv xfs 2.0G 64M 2.0G 4% /srv/lvm
- 放大LV容量
- VG阶段需要有剩余的容量
- LV阶段产生更多的可用容量
- 文件系统阶段的放大
给/srv/lvm增加500MB的容量
[root@localhost lvm]# vgdisplay xpyvg
--- Volume group ---
VG Name xpyvg
System ID
Format lvm2
Metadata Areas 5
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 5
Act PV 5
VG Size 4.92 GiB
PE Size 16.00 MiB
Total PE 315
Alloc PE / Size 128 / 2.00 GiB
Free PE / Size 187 / 2.92 GiB
VG UUID 6DsvsW-c1n8-9hS0-4o8R-F7Ko-M2os-roLD5f
#放大LV,利用lvresize或者lvextend的功能来增加
[root@localhost lvm]# lvresize -L +500M /dev/xpyvg/xpylv
Rounding size to boundary between physical extents: 512.00 MiB.
Size of logical volume xpyvg/xpylv changed from 2.00 GiB (128 extents) to 2.50 GiB (160 extents).
Logical volume xpyvg/xpylv successfully resized.
[root@localhost lvm]# df -Th /srv/lvm
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/xpyvg-xpylv xfs 2.0G 64M 2.0G 4% /srv/lvm
[root@localhost lvm]# lvscan
ACTIVE '/dev/centos/root' [10.00 GiB] inherit
ACTIVE '/dev/centos/swap' [1.00 GiB] inherit
ACTIVE '/dev/centos/home' [5.00 GiB] inherit
ACTIVE '/dev/xpyvg/xpylv' [2.50 GiB] inherit
#LV放大到2.5G,但文件系统却没有增加
[root@localhost lvm]# xfs_info /srv/lvm
meta-data=/dev/mapper/xpyvg-xpylv isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=524288, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
#如果文件系统为ext2/3/4,使用resize2fs命令,重新分配文件系统空间
[root@localhost lvm]# xfs_growfs /srv/lvm #这一步很重要
meta-data=/dev/mapper/xpyvg-xpylv isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=524288, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 524288 to 655360
[root@localhost lvm]# xfs_info /srv/lvm
meta-data=/dev/mapper/xpyvg-xpylv isize=512 agcount=5, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=655360, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost lvm]# df -Th /srv/lvm
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/xpyvg-xpylv xfs 2.5G 64M 2.5G 3% /srv/lvm
#两次xfs_info的结果会发现(1)整个区块群组的数量增加一个,那个区块群组就是记录新的设备容量的文件系统所在,在(2)中整体的区块数量增加了