CentOS6虚拟机扩展磁盘空间—lvm

本实验基于CentOS 6.9,虚拟机有两块磁盘,/dev/sda作为系统盘,/dev/sdb原来是一块20G的磁盘,现在修改虚拟机的设置,扩展到40G。操作的是/dev/sdb

一、 我们在pv和vg都是20G的时刻,在vg_1上建立两个lv
lvcreate -L 5G -n lv_b1 vg_1 #建立一个5G的lv,名字叫lv_b1

[root@CentOS69 ~]# lvcreate -L 14.79G -n lv_b2 vg_1 #建立一个14.97G的lv_b2
Rounding up size to full physical extent 14.79 GiB
Logical volume "lv_b2" created.
[root@CentOS69 ~]# lvs
LV VG Attr LSize Pool OrIGin Data% Meta% Move Log Cpy%Sync Convert
lv_b1 vg_1 -wi-a----- 5.00g
lv_b2 vg_1 -wi-a----- 14.79g

然后,建立两个目录,把这两个lv挂载
[root@CentOS69 ~]# mkdir /mount1
[root@CentOS69 ~]# mkdir /mount2
[root@CentOS69 ~]# mkfs -t ext4 /dev/vg_1/lv_b1 #格式化
[root@CentOS69 ~]# mkfs -t ext4 /dev/vg_1/lv_b2 #格式化

[root@CentOS69 ~]# mount /dev/mapper/vg_1-lv_b1 /mount1 #挂载
[root@CentOS69 ~]# mount /dev/mapper/vg_1-lv_b2 /mount2 #挂载

[root@CentOS69 ~]# cp /etc/fstab /mount1 #复制一个文件到/mount1
[root@CentOS69 ~]# cp /etc/fstab /mount2 #复制一个文件到/mount2

二、 准备工作,安装需要的软件,先需要安装epel的yum源,然后才气安装后面的包,growpart这个软件是我们需要用到的,该软件在epel源内里

  1. 安装epel:
    yum install -y http://dl.Fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

  2. 安装相关工具: yum install cloud-init cloud-utils-growpart dracut-modules-growroot -y

  3. 重新天生initramfs
    dracut -f #笼罩天生initramfs文件

三、 扩展以前的分区
注重:虚拟机在关机且没有快照的情况下才气举行扩展,对于阿里云来讲,磁盘扩容以后,需要在控制台重启服务器,才气看到增添的磁盘空间。

  1. 查看磁盘原有的分区信息:
    [root@CentOS69 ~]# lSBLk
    NAME MAJ:MiN RM SIZE RO TYPE MOUNTPOINT
    sdb 8:16 0 40G 0 disk #磁盘/dev/sdb显示为40GB
    ├─sdb1 8:17 0 203.9M 0 part
    └─sdb2 8:18 0 19.8G 0 part
    ├─vg_1-lv_b1 (dm-2) 253:2 0 5G 0 lvm /mount1
    └─vg_1-lv_b2 (dm-3) 253:3 0 14.8G 0 lvm /mount2

2.对磁盘分区举行扩容,将Free的空间都分配给/dev/sdb2
[root@CentOS69 ~]# growpart /dev/sdb 2
CHANGED: partition=2 start=417690 old: size=41511960 end=41929650 new: size=83457675,end=83875365

3.查看分区的空间信息,再次执行lsblk,
[root@CentOS69 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 40G 0 disk
├─sdb1 8:17 0 203.9M 0 part
└─sdb2 8:18 0 19.8G 0 part
├─vg_1-lv_b1 (dm-2) 253:2 0 5G 0 lvm /mount1
└─vg_1-lv_b2 (dm-3) 253:3 0 14.8G 0 lvm /mount2

  1. 从内核读取分区信息
    [root@CentOS69 ~]# partprobe
    Warning: WARNING: the kernel fAIled to re-read the partition tABLe on /dev/sda (装备或资源忙). As a result, it may not reflect all of your changes until after reboot.
  2. 此时需要重启虚拟机,才气读到新增的容量
    [root@CentOS69 ~]#reboot #重启后,lsblk能看到/dev/sdb2的空间巨细为39.8G
    [root@CentOS69 ~]# mount /dev/mapper/vg_1-lv_b1 /mount1
    [root@CentOS69 ~]# mount /dev/mapper/vg_1-lv_b2 /mount2

然则sdb2是一个lvm,不能直接使用resize2fs扩容。

四、 对pv举行扩容,由于我们是在虚拟机上对/dev/sdb2磁盘分区举行了扩容,以是我们使用pvresize对/dev/sdb2的PV举行扩容,扩容后,其巨细显示为39.79g

  1. 扩容之前的PV巨细
    [root@CentOS69 ~]# pvs
    PV VG Fmt Attr PSize PFree
    /dev/sdb2 vg_1 lvm2 a--u 19.79g 19.79g

  2. 举行扩容
    [root@CentOS69 ~]# pvresize /dev/sdb2
    Physical volume "/dev/sdb2" changed
    1 physical volume(s) resized / 0 physical volume(s) not resized
    [root@CentOS69 ~]# pvs
    PV VG Fmt Attr PSize PFree
    /dev/sdb2 vg_1 lvm2 a--u 39.79g 39.79g

若是上述的下令不能生效,那么可以手工指定pv的巨细,在实验中,我使用上述下令有时不生效,下面是直接设定pv的巨细的

[root@CentOS69 ~]# pvresize --setphysicalvolumesize 39.8G /dev/sdb2
WARNING: /dev/sdb2: Overriding real size. You could lose data.
Physical volume "/dev/sdb2" changed
1 physical volume(s) resized / 0 physical volume(s) not resized
[root@CentOS69 ~]# pvs
Device /dev/sdb2 has size of 41511960 sectors which is smaller than corresponding PV size of 83464601 sectors. Was device resized?
One or more devices used as PVs in VG vg_1 hAVe changed sizes.
PV VG Fmt Attr PSize PFree
/dev/sda2 vg_centos69 lvm2 a--u 29.51g 0
/dev/sdb2 vg_1 lvm2 a--u 39.80g 20.00g

五、 pv扩展完成以后,vg直接可以识别出39.8G空间了。并显示出有20G的空缺空间

vgextend VGtest /dev/sdb #这是网上的扩容下令

[root@CentOS69 ~]# vgs
Device /dev/sdb2 has size of 41511960 sectors which is smaller than corresponding PV size of 83464601 sectors. Was device resized?
One or more devices used as PVs in VG vg_1 have changed sizes.
VG #PV #LV #SN Attr VSize VFree
vg_1 1 2 0 wz--n- 39.80g 20.00g

六、lv扩容
在扩容前,推荐使用lsblk来看一下,是否能看到扩容以后的分区

  1. 扩容前的lv

[root@CentOS69 ~]# lvs
Device /dev/sdb2 has size of 41511960 sectors which is smaller than corresponding PV size of 83464601 sectors. Was device resized?
One or more devices used as PVs in VG vg_1 have changed sizes.
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_b1 vg_1 -wi-a----- 5.00g
lv_b2 vg_1 -wi-a----- 14.79g

扩容的man文档

  1. Extends the size of the logical volume "vg01/lvol10" by 54MiB on physical volume /dev/sdk3.
    This is only possible if /dev/sdk3 is a member of volume group vg01 and there are enough
    free physical extents in it:

lvextend -L +54 /dev/vg01/lvol10 /dev/sdk3

  1. Extends the size of logical volume "vg01/lvol01" by the amount of free space on physical
    volume /dev/sdk3. This is equivalent to specifying "-l +100%PVS" on the command line:

lvextend /dev/vg01/lvol01 /dev/sdk3

  1. 扩容lv,加其增添20G空间
    [root@CentOS69 ~]# lvextend -L +20G /dev/vg_1/lv_b2
    Size of logical volume vg_1/lv_b2 changed from 14.79 GiB (3787 extents) to 34.79 GiB (8907 extents).
    Logical volume lv_b2 successfully resized.

  2. 查看空间
    [root@CentOS69 ~]# lvs
    LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
    lv_b1 vg_1 -wi-ao---- 5.00g
    lv_b2 vg_1 -wi-ao---- 34.79g

网上另一种对lv举行扩容的方式:
lvresize -l +100%FREE -r /dev/vg_1/lv_b2

七、对文件系统举行扩容

  1. 查看已挂载的文件系统的空间
    [root@CentOS69 ~]# df -h
    Filesystem Size Used Avail Use% Mounted on
    /dev/mapper/vg_1-lv_b1 4.8G 10M 4.6G 1% /mount1
    /dev/mapper/vg_1-lv_b2 15G 37M 14G 1% /mount2

2.扩容
[root@CentOS69 ~]# resize2fs -p /dev/vg_1/lv_b2
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_1/lv_b2 is mounted on /mount2; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 3
Performing an on-line resize of /dev/vg_1/lv_b2 to 9120768 (4k) blocks.
The filesystem on /dev/vg_1/lv_b2 is now 9120768 blocks long.

3.接下来,再使用df来查看的时刻,扩容乐成了
[root@CentOS69 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_1-lv_b1 4.8G 10M 4.6G 1% /mount1
/dev/mapper/vg_1-lv_b2 35G 37M 34G 1% /mount2

总结:
扩容的思绪是:先给虚拟机增添磁盘空间,对于操作系统来讲,这是物理层面的空间增添; 接着,让内核能识别出多的空间,并把它分配给分区;由于使用了lvm,接下来要从PV到VG到lv划分举行扩容;最后面是让文件系统扩容。