lvm的扩展与缩减
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lvm的扩展与缩减相关的知识,希望对你有一定的参考价值。
这篇文章主要介绍lv在容量不足的情况下如何实现在线扩展或缩减。
注意:对于ext系列文件系统,可扩展可缩减(但缩减需要先取消挂载);
而对于xfs系列文件系统,仅仅可以扩展;
当然,lv的扩展需要vg有足够的空间,否则,只能先打pv的主意了,把vg先进行扩展;如下所示
[email protected] ~ ]#vgdisplay
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 4
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 1
Max PV 0
Cur PV 4
Act PV 4
VG Size <5.94 GiB
PE Size 16.00 MiB
Total PE 380
Alloc PE / Size 380 / <5.94 GiB
Free PE / Size 0 / 0
VG UUID 0PV32k-u0PM-0wh5-VyWR-A5qD-ERXa-2VPEiJ
创建pv
[[email protected] ~ ]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 50G 0 part /
├─sda3 8:3 0 30G 0 part /data
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 3G 0 part [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 1G 0 part
│ └─vg0-lv1 253:1 0 4.5G 0 lvm
└─sdb2 8:18 0 2G 0 part
├─vg0-lv0 253:0 0 1.5G 0 lvm /root/test1
└─vg0-lv1 253:1 0 4.5G 0 lvm
sdc 8:32 0 20G 0 disk
├─sdc1 8:33 0 1G 0 part
│ └─vg0-lv1 253:1 0 4.5G 0 lvm
└─sdc2 8:34 0 2G 0 part
└─vg0-lv1 253:1 0 4.5G 0 lvm
sdd 8:48 0 20G 0 disk
sde 8:64 0 20G 0 disk
sr0 11:0 1 8.8G 0 rom
以/dev/sdd和/dev/sde为例,进行创建
[[email protected] ~ ]#pvcreate /dev/sd{d,e}
Physical volume "/dev/sdd" successfully created.
Physical volume "/dev/sde" successfully created.
查看pv信息,很明显,没有vg归属的就是刚刚创建的pv
[[email protected] ~ ]#pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 vg0 lvm2 a-- 1008.00m 0
/dev/sdb2 vg0 lvm2 a-- 1.98g 0
/dev/sdc1 vg0 lvm2 a-- 1008.00m 0
/dev/sdc2 vg0 lvm2 a-- 1.98g 0
/dev/sdd lvm2 --- 20.00g 20.00g
/dev/sde lvm2 --- 20.00g 20.00g
扩展vg
未扩展之前:
[[email protected] ~ ]#vgdisplay
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 4
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 1
Max PV 0
Cur PV 4
Act PV 4
VG Size <5.94 GiB ## 大小6G
PE Size 16.00 MiB
Total PE 380 ## 总数量380
Alloc PE / Size 380 / <5.94 GiB
Free PE / Size 0 / 0 ## 空闲为0
VG UUID 0PV32k-u0PM-0wh5-VyWR-A5qD-ERXa-2VPEiJ
开搞:vgextend vgName pvName [pvName....]
[[email protected] ~ ]#vgextend vg0 /dev/sd{d,e}
Volume group "vg0" successfully extended
查看扩展后的vg信息
[[email protected] ~ ]#vgs
VG #PV #LV #SN Attr VSize VFree
vg0 6 2 0 wz--n- <45.91g <39.97g
[[email protected] ~ ]#vgdisplay
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 6
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 1
Max PV 0
Cur PV 6
Act PV 6
VG Size <45.91 GiB ## 增加40G
PE Size 16.00 MiB
Total PE 2938
Alloc PE / Size 380 / <5.94 GiB
Free PE / Size 2558 / <39.97 GiB ## 空闲为2558
VG UUID 0PV32k-u0PM-0wh5-VyWR-A5qD-ERXa-2VPEiJ
缩减vg
开搞:vgreduce vgName pvName [pvName....]
[[email protected] ~ ]#vgreduce vg0 /dev/sdd
Removed "/dev/sdd" from volume group "vg0"
查看vg是否缩减:
[[email protected] ~ ]#vgs
VG #PV #LV #SN Attr VSize VFree
vg0 5 2 0 wz--n- 25.92g 19.98g
[[email protected] ~ ]#vgdisplay
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 5
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 1
Max PV 0
Cur PV 5
Act PV 5
VG Size 25.92 GiB ## 大小由46G缩为26G
PE Size 16.00 MiB
Total PE 1659
Alloc PE / Size 380 / <5.94 GiB
Free PE / Size 1279 / 19.98 GiB ## 空闲PE数量减少
VG UUID 0PV32k-u0PM-0wh5-VyWR-A5qD-ERXa-2VPEiJ
扩展lv
未扩展之前的信息:
[[email protected] ~ ]#lvdisplay
--- Logical volume ---
LV Path /dev/vg0/lv0
LV Name lv0
VG Name vg0
LV UUID HFNlrP-1da7-rdiJ-Jqew-0ym5-Lx0H-twQup5
LV Write Access read/write
LV Creation host, time centos7.localdomain, 2018-08-15 16:31:01 +0800
LV Status available
# open 1
LV Size <1.47 GiB
Current LE 94
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
--- Logical volume ---
LV Path /dev/vg0/lv1
LV Name lv1
VG Name vg0
LV UUID L1RYsF-rNHs-y8xE-9ao1-WYci-CRIR-X5hkf4
LV Write Access read/write
LV Creation host, time centos7.localdomain, 2018-08-15 16:52:41 +0800
LV Status available
# open 0
LV Size <4.47 GiB
Current LE 286
Segments 4
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
开搞:lvextend -L [+][mMgGtT] /dev/vgName/lvName
上面的意思就是说可以直接-L指定扩展后的大小;或者-L +增加的大小都可以
## 给lv0增加10G
[[email protected] ~ ]#lvextend -L +10G /dev/vg0/lv0
Size of logical volume vg0/lv0 changed from <1.47 GiB (94 extents) to <11.47 GiB (734 extents).
Logical volume vg0/lv0 successfully resized.
查看扩展后的lv0
[[email protected] ~ ]#lvdisplay /dev/vg0/lv0
--- Logical volume ---
LV Path /dev/vg0/lv0
LV Name lv0
VG Name vg0
LV UUID HFNlrP-1da7-rdiJ-Jqew-0ym5-Lx0H-twQup5
LV Write Access read/write
LV Creation host, time centos7.localdomain, 2018-08-15 16:31:01 +0800
LV Status available
open 1
LV Size <11.47 GiB ## 瞬间增加10G
Current LE 734
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
df查看一下文件系统容量是否增加
[[email protected] ~ ]#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 50G 3.8G 47G 8% /
devtmpfs 599M 0 599M 0% /dev
tmpfs 615M 0 615M 0% /dev/shm
tmpfs 615M 18M 597M 3% /run
tmpfs 615M 0 615M 0% /sys/fs/cgroup
/dev/sda3 30G 1012M 29G 4% /data
/dev/sda1 1014M 155M 860M 16% /boot
tmpfs 123M 12K 123M 1% /run/user/42
tmpfs 123M 0 123M 0% /run/user/0
/dev/mapper/vg0-lv0 1.5G 4.5M 1.4G 1% /root/test1 ## 发现没有变化
使用resize2fs在线扩展ext系列文件系统(注意 :resize2fs仅仅适用于ext系列文件系统,对于xfs需要用xfs_growfs命令,并且命令后跟挂载点,而不是设备名)
[[email protected] ~ ]#resize2fs /dev/vg0/lv0
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg0/lv0 is mounted on /root/test1; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/vg0/lv0 is now 3006464 blocks long.
[[email protected] ~ ]#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 50G 3.8G 47G 8% /
devtmpfs 599M 0 599M 0% /dev
tmpfs 615M 0 615M 0% /dev/shm
tmpfs 615M 18M 597M 3% /run
tmpfs 615M 0 615M 0% /sys/fs/cgroup
/dev/sda3 30G 1012M 29G 4% /data
/dev/sda1 1014M 155M 860M 16% /boot
tmpfs 123M 12K 123M 1% /run/user/42
tmpfs 123M 0 123M 0% /run/user/0
/dev/mapper/vg0-lv0 12G 7.3M 11G 1% /root/test1 ## 同步成功
先扩展lv,再扩展文件系统,感觉好麻烦,可以在命令后面加参数-r或--resizefs,实现一步更新,并且支持xfs文件系统
[[email protected] ~ ]#lvextend -r -L +5G /dev/vg0/lv1
Size of logical volume vg0/lv1 changed from <4.47 GiB (286 extents) to <9.47 GiB (606 extents).
Logical volume vg0/lv1 successfully resized.
meta-data=/dev/mapper/vg0-lv1 isize=512 agcount=4, agsize=292864 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=1171456, 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 1171456 to 2482176
缩减lv——-仅支持ext系列
一般情况下不会缩减,因为有可能造成数据损坏,建议先备份数据
步骤1:先取消卸载;
[[email protected] ~ ]#umount test1
步骤2:检查文件系统;
[[email protected] ~ ]#fsck -f /dev/vg0/lv0
fsck from util-linux 2.23.2
e2fsck 1.42.9 (28-Dec-2013)
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/mapper/vg0-lv0: 11/417664 files (9.1% non-contiguous), 36107/1695744 blocks
步骤3:缩减文件系统;
[[email protected] ~ ]#resize2fs /dev/vg0/lv0 5G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/lv0 to 1310720 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 1310720 blocks long.
步骤4:缩减逻辑卷,有可能损坏数据,强烈建议备份
格式:lvreduce -L [-][mMgGtT] /dev/vgName/lvName
[[email protected] ~ ]#lvreduce -L 2G /dev/vg0/lv0
WARNING: Reducing active logical volume to 2.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg0/lv0? [y/n]: y
Size of logical volume vg0/lv0 changed from <6.47 GiB (414 extents) to 2.00 GiB (128 extents).
Logical volume vg0/lv0 successfully resized.
步骤5:验证
[[email protected] ~ ]#lvdisplay /dev/vg0/lv0
--- Logical volume ---
LV Path /dev/vg0/lv0
LV Name lv0
VG Name vg0
LV UUID HFNlrP-1da7-rdiJ-Jqew-0ym5-Lx0H-twQup5
LV Write Access read/write
LV Creation host, time centos7.localdomain, 2018-08-15 16:31:01 +0800
LV Status available
open 0
LV Size 2.00 GiB
Current LE 128
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
修改lv名和vg名
修改lv
格式:lvrename oldname newname
[[email protected] ~ ]#lvrename /dev/vg0/lv0 /dev/vg0/xin
Renamed "lv0" to "xin" in volume group "vg0"
[[email protected] ~ ]#lvdisplay /dev/vg0/xin
--- Logical volume ---
LV Path /dev/vg0/xin
LV Name xin
VG Name vg0
LV UUID HFNlrP-1da7-rdiJ-Jqew-0ym5-Lx0H-twQup5
LV Write Access read/write
LV Creation host, time centos7.localdomain, 2018-08-15 16:31:01 +0800
LV Status available
# open 1
LV Size <6.47 GiB
Current LE 414
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
修改vg
格式:vgrename oldname newname
[[email protected] ~ ]#vgrename vg0 VG0
Volume group "vg0" successfully renamed to "VG0"
[[email protected] ~ ]#vgdisplay VG0
--- Volume group ---
VG Name VG0
System ID
Format lvm2
Metadata Areas 5
Metadata Sequence No 10
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 5
Act PV 5
VG Size 25.92 GiB
PE Size 16.00 MiB
Total PE 1659
Alloc PE / Size 1020 / <15.94 GiB
Free PE / Size 639 / 9.98 GiB
VG UUID 0PV32k-u0PM-0wh5-VyWR-A5qD-ERXa-2VPEiJ
删除pv、vg、lv
[[email protected] ~ ]#lvremove /dev/vg0/lv0
[[email protected] ~ ]#vgremove vg0
Do you really want to remove volume group "vg0" containing 1 logical volumes? [y/n]: y
Volume group "vg0" successfully removed
[[email protected] ~ ]#pvremove /dev/sde
Labels on physical volume "/dev/sde" successfully wiped
总结:
扩展逻辑卷
1. 查看vg空间是否充裕,如果充裕,直接扩展;否则先扩展vg
vgdisplay
2. 扩展lv并同步文件系统
lvextend -l +100%FREE /dev/vg0/lv0
ext系列: resize2fs /dev/vg0/lv0
xfs系列: xfs_grow /mnt/lv0
或者一条命令搞定:
lvextend -r -l +100%FREE /dev/vg0/lv0
3.df -h 查看是否同步
缩减逻辑卷:
1.先取消挂载
umount /mnt/lv0
2.检查文件系统
fsck -f /dev/vg0/lv0
3.缩减文件系统
resize2fs /dev/vg0/lv0 1G
4.缩减逻辑卷
lvreduce -L 1G /dev/vg0/lv0 (注意:缩减的大小和文件系统对应)
5.mount挂载使用
以上是关于lvm的扩展与缩减的主要内容,如果未能解决你的问题,请参考以下文章