LVM扩容、缩减和删除

常用命令:

lvextend:LV扩容

vgextend:VG扩容

lvreduce:LV缩减

vgreduce:VG缩减

lvremove:LV删除

vgremove:VG删除

pvmove:PV数据移动到所属逻辑卷的其它物理卷

pvremove:PV删除

vgchange:VG状态改变

一、LVM扩容

1、LV扩容之前,先查看LV和所属VG可用容量(VFree)

[root@localhost ~]# lvs
  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  LV1  VG1  -wi-ao---- 4.00g        

[root@localhost ~]# pvs
  PV         VG   Fmt  Attr PSize PFree
  /dev/sdb1  VG1  lvm2 a--  2.00g    0
  /dev/sdb2  VG1  lvm2 a--  2.00g 8.00m
  /dev/sdb3       lvm2 ---  2.01g 2.01g

[root@localhost ~]# vgs
  VG       #PV     #LV     #SN     Attr       VSize     VFree
  VG1        2       1           0     wz--n-      4.01g         8.00m

LV1大小为4G,VG1的VFree大小8M。要给LV增加2G容量,VG1可用剩余仅为8M,因为此需要给VG1扩容,将一个新的分区sdb3添加到卷组中:

[root@localhost ~]# vgextend VG1 /dev/sdb3
  Volume group "VG1" successfully extended
[root@localhost ~]# vgs
  VG   #PV #LV #SN Attr   VSize VFree
  VG1    3   1   0 wz--n- 6.01g 2.01g

2、现在VG1的VFree为2.01G,可以直接给LV1扩容:

[root@localhost ~]# lvextend -L +2G /dev/mapper/VG1-LV1
  Size of logical volume VG1/LV1 changed from 4.00 GiB (1024 extents) to 6.00 GiB (1536 extents).
  Logical volume LV1 successfully resized

[root@localhost ~]# lvs

  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  LV1  VG1  -wi-ao---- 6.00g

注:也可以使用 lvextend -L 6G /dev/mapper/VG1-LV1直接扩容到6G

3、对文件系统进行扩容

LV1容量已扩容到6G,现在查下LV1的挂载点/LV1容量:

[root@localhost ~]# df -h /LV1
Filesystem                       Size      Used     Avail     Use%     Mounted on
/dev/mapper/VG1-LV1     3.9G     8.0M      3.7G   1%           /LV1

/LV1容量为3.9G,而刚刚已将LV1扩容到6G,这是为什么呢?

因为刚才仅仅扩容了逻辑卷LV1的容量,而没有文件系统扩容,因此需要对文件系统扩容:

[root@localhost ~]# resize2fs /dev/mapper/VG1-LV1
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/VG1-LV1 is mounted on /LV1; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/mapper/VG1-LV1 to 1572864 (4k) blocks.
The filesystem on /dev/mapper/VG1-LV1 is now 1572864 blocks long.

[root@localhost ~]# df -h
/dev/mapper/VG1-LV1  5.8G  8.0M  5.5G   1% /LV1

到此/LV1已经扩容成功!


二、LV缩减,先卸载挂载点,再对文件系统进行缩减,最后缩减逻辑卷LV

1、先卸载挂载点:

[root@localhost ~]# umount /LV1/
[root@localhost ~]# mount -l
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
/dev/sda3 on /usr type ext4 (rw)
/dev/sr0 on /mnt type iso9660 (ro) [CentOS_6.6_Final]
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)


2、缩减文件系统:

[root@localhost ~]# resize2fs /dev/mapper/VG1-LV1  4G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/VG1-LV1 to 1048576 (4k) blocks.
The filesystem on /dev/mapper/VG1-LV1 is now 1048576 blocks long.
注:将文件系统/dev/mapper/VG1-LV1缩减到4G


3、缩减逻辑卷LV:

[root@localhost ~]# lvreduce -L -2G /dev/mapper/VG1-LV1
  WARNING: Reducing active logical volume to 4.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce LV1? [y/n]: y
  Size of logical volume VG1/LV1 changed from 6.00 GiB (1536 extents) to 4.00 GiB (1024 extents).
  Logical volume LV1 successfully resized


4、挂载、查看lv和挂载点大小 :

[root@localhost ~]# mount /dev/mapper/VG1-LV1 /LV1/
[root@localhost ~]# df -h
/dev/mapper/VG1-LV1  3.9G  8.0M  3.7G   1% /LV1
[root@localhost ~]# lvs
  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  LV1  VG1  -wi-ao---- 4.00g                                                 


三、LVM删除(LVM删除之前,请对重要数据备份到其它分区或LVM分区上)

删除LV,VG顺序:卸载挂载的逻辑卷,删除相应的逻辑卷,再删除卷组,最后删除物理卷PV。


1、卸载挂载的逻辑卷

[root@localhost ~]# umount /LV1/


2、删除相应的逻辑卷LV

删除相应的逻辑卷前,需要先使用命令vgchange将活动的逻辑组停止:

[root@localhost ~]# vgchange -an VG1
  0 logical volume(s) in volume group "VG1" now active

注:-a是available的缩写,y表示yes,n表示no


查看逻辑卷LV路径:

[root@localhost ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/VG1/LV1


删除逻辑卷LV:

[root@localhost ~]# lvremove /dev/VG1/LV1
  Logical volume "LV1" successfully removed

3、删除逻辑卷组VG

[root@localhost ~]# vgremove VG1
  Volume group "VG1" successfully removed

查看卷组VG1是否删除成功:

[root@localhost ~]# vgdisplay
  No volume groups found


4、删除物理卷PV

查看PV信息

[root@localhost ~]# pvs
  PV         VG   Fmt  Attr PSize PFree
  /dev/sdb1       lvm2 ---  2.01g 2.01g
  /dev/sdb2       lvm2 ---  2.01g 2.01g
  /dev/sdb3       lvm2 ---  2.01g 2.01g

删除PV:

[root@localhost ~]# pvremove /dev/sdb{1,2,3}
  Labels on physical volume "/dev/sdb1" successfully wiped
  Labels on physical volume "/dev/sdb2" successfully wiped
  Labels on physical volume "/dev/sdb3" successfully wiped

验证PV删除成功与否:

[root@localhost ~]# pvs

如果只想将某个分区从物理卷中删除,首先使用使用pvmove将该物理馆上的数据移到别的物理卷,再使用vgreduce命令删除该物理卷。这样这个物理卷就像普通分区一样使用了。

[root@localhost ~]# pvmove /dev/sdb1
  /dev/sdb1: Moved: 0.6%
  /dev/sdb1: Moved: 39.6%
  /dev/sdb1: Moved: 78.6%
  /dev/sdb1: Moved: 100.0%

[root@localhost ~]# vgreduce VG1 /dev/sdb1
  Removed "/dev/sdb1" from volume group "VG1"