CentOS6虛擬機擴展磁盤空間—lvmcentos
本實驗基於CentOS 6.9,虛擬機有兩塊磁盤,/dev/sda做爲系統盤,/dev/sdb原來是一塊20G的磁盤,如今修改虛擬機的配置,擴展到40G。操做的是/dev/sdb服務器
1、 咱們在pv和vg都是20G的時候,在vg_1上建立兩個lv
lvcreate -L 5G -n lv_b1 vg_1 #建立一個5G的lv,名字叫lv_b1app
[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 ide
而後,建立兩個目錄,把這兩個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 #掛載ui
[root@CentOS69 ~]# cp /etc/fstab /mount1 #複製一個文件到/mount1
[root@CentOS69 ~]# cp /etc/fstab /mount2 #複製一個文件到/mount2阿里雲
2、 準備工做,安裝須要的軟件,先須要安裝epel的yum源,而後才能安裝後面的包,growpart這個軟件是咱們須要用到的,該軟件在epel源裏面spa
安裝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 -yorm
3、 擴展之前的分區信息
注意:虛擬機在關機且沒有快照的狀況下才能進行擴展,對於阿里雲來說,磁盤擴容之後,須要在控制檯重啓服務器,才能看到增長的磁盤空間。
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
可是sdb2是一個lvm,不能直接使用resize2fs擴容。
4、 對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
若是上述的命令不能生效,那麼能夠手工指定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
5、 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
6、lv擴容
在擴容前,推薦使用lsblk來看一下,是否能看到擴容之後的分區
[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文檔
lvextend -L +54 /dev/vg01/lvol10 /dev/sdk3
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.
網上另外一種對lv進行擴容的方法:
lvresize -l +100%FREE -r /dev/vg_1/lv_b2
7、對文件系統進行擴容
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分別進行擴容;最後面是讓文件系統擴容。