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源里面
-
安装epel:
yum install -y http://dl.Fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm -
安装相关工具: yum install cloud-init cloud-utils-growpart dracut-modules-growroot -y
- 重新生成initramfs
dracut -f #覆盖生成initramfs文件
三、 扩展以前的分区信息
注意:虚拟机在关机且没有快照的情况下才能进行扩展,对于阿里云来讲,磁盘扩容以后,需要在控制台重启服务器,才能看到增加的磁盘空间。
- 查看磁盘原有的分区信息:
[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
- 从内核读取分区信息
[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. - 此时需要重启虚拟机,才能读到新增的容量
[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
-
扩容之前的PV大小
[root@CentOS69 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb2 vg_1 lvm2 a--u 19.79g 19.79g - 进行扩容
[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来看一下,是否能看到扩容以后的分区
- 扩容前的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文档
- 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
- 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
-
扩容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. - 查看空间
[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
七、对文件系统进行扩容
- 查看已挂载的文件系统的空间
[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分别进行扩容;最后面是让文件系统扩容。