Categories: Linux

CentOS 磁盘LVM磁盘扩容

有一台VMware的虚拟机默认的模板是40G的硬盘,然后可以扩容到100G。此操作就是用来把100G扩容出来。只要按照粗体字就可以扩容成功。

df -h

Filesystem            Size  Used Avail Use% Mounted on

 /dev/mapper/VolGroup00LogVol00 19.88G  3.2G   40G   8% /

 /dev/sda1              99M   19M   76M  20% /boot

 tmpfs                1014M     0 1014M   0% /dev/sh

Find the device where the unpartitioned space is:

fdisk -l

Disk /dev/sda: 53.6 GB, 53687091200 bytes

 255 heads, 63 sectors/track, 6527 cylinders

 Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System

 /dev/sda1   *           1          13      104391   83  Linux

 /dev/sda2              14        2610    20860402+  8e  Linux LVM

since this is a virtual machine and this is the first disk expansion, its likely located in /dev/sda (note the size of /dev/sda which is roughly the new expanded size). Note there are totally 6527 cylinders of the disk but we only been used 2610 since we just added 30GB more.

So let’s Create a new partition that takes up the remaining space and is of filesystem type 8e (LVM):

fdisk /dev/sda

n   (new)

p   (primary)

3   (partition number, since 1st and 2nd partition already exists)

then press Enter twice to let linux automatically select default first available cylinder to the default last cylinder.

t    (type)

3   (partition number)

8e (set type to LVM)

p   (view the new partitions layout)

w  (write out the new partitions layout to disk)

q  (quit LVM)

完成后执行命令

partprobe
这样就不用重启

The new partition layout is now:

Disk /dev/sda: 53.6 GB, 53687091200 bytes

 255 heads, 63 sectors/track, 6527 cylinders

 Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System

 /dev/sda1   *           1          13      104391   83  Linux

 /dev/sda2              14        2610    20860402+  8e  Linux LVM

 /dev/sda3            2611        6527    31463302+  8e  Linux LVM

Note now we have used all of the free space.The next step is to use LVM to take the newly formed partition and turn it into a new Physical Volume, add it to a Volume Group, and finally assimilate its free space into a Logical Volume.

Convert /dev/sda3 partition into a Physical Volume so LVM can make use of it:

pvcreate /dev/sda3

Add the new Physical Volume to the Volume Group as additional free space:

vgextend VolGroup00 /dev/sda3

vgdisplay 

(note the free space now in the Volume Group which can now be assigned to a Logical Volume)

— Volume group —

 VG Name               VolGroup00

 System ID

 Format                lvm2

 Metadata Areas        2

 Metadata Sequence No  4

 VG Access             read/write

 VG Status             resizable

 MAX LV                0

 Cur LV                2

 Open LV               2

 Max PV                0

 Cur PV                2

 Act PV                2

 VG Size               49.88 GB

 PE Size               32.00 MB

 Total PE              1596

 Alloc PE / Size       636 / 19.88 GB

 Free  PE / Size       960 / 30.00 GB

 VG UUID               0JB6GV-gFJW-onuN-7Xq1-OKim-n5gM-EVPUKB

Have the Logical Volume (within the Volume Group) overtake the remaining free space of the Volume Group:

lvextend -l +100%FREE /dev/VolGroup00/LogVol00

vgdisplay

(note the free space in Volume Group is now gone since it was all assigned to a Logical Volume)

— Volume group —

 VG Name               VolGroup00

 System ID

 Format                lvm2

 Metadata Areas        2

 Metadata Sequence No  5

 VG Access             read/write

 VG Status             resizable

 MAX LV                0

 Cur LV                2

 Open LV               2

 Max PV                0

 Cur PV                2

 Act PV                2

 VG Size               49.88 GB

 PE Size               32.00 MB

 Total PE              1596

 Alloc PE / Size       1596 / 49.88 GB

  Free  PE / Size       0 / 0 

 VG UUID               0JB6GV-gFJW-onuN-7Xq1-OKim-n5gM-EVPUKB

Trigger online resizing of the live and mounted filesystem so the new disk space can be utilized immediately:

 resize2fs -p /dev/mapper/VolGroup00-LogVol00

xfs用以下命令来扩磁盘空间:

xfs_growfs /dev/mapper/centos-home

Now, the system has a bigger diskspace to play around with.

df -h

Filesystem            Size  Used Avail Use% Mounted on

 /dev/mapper/VolGroup00-LogVol00 45G  3.2G   40G   8% /

 /dev/sda1              99M   19M   76M  20% /boot

 tmpfs                1014M     0 1014M   0% /dev/shm

修正日志:

20120415:加入了如何查找VG的部分,使前后阅读起来更加流畅;修正了部分错误的文字说明

王哥哥

Share
Published by
王哥哥

Recent Posts

异地组网新秀——节点小宝测试

有时候,我们想要搭建异地组网,…

6 小时 ago

DELL SC9000 级联

 省去了心跳线(橙色) 图画的…

2 周 ago

VBR 核心概念和最佳实践

 https://www.ve…

2 周 ago