Thursday, 8 December 2011

Expanding a LVM linux partition

Linux is something of a black art to me, so when I was required to resize a Centos (RedHat) virtual machine's hard disk, it took quite a bit of effort to do. As a Windows person I was very disappointed that I had to jump through so many hoops.

First I though I'd just boot up off a GParted ISO and resize it like I would do with any other machine.. only to find out that it didn't support LVM... and just refused to go any further.
So.. here's how to do it...

Assuming you have already expanded the Virtual HDD in VMware.

1 Log on to the VM as root

Let's not mess around with SU and all that business, we're doing some pretty dangerous stuff here, and we should know what the risks are!

2 Open a command prompt


df
Filesystem           1K-blocks      Used Available Use% Mounted on /dev/mapper/VolGroup00-LogVol00
                      26377276   3810884  21204884  16% /
/dev/sda1               101086     19650     76217  21% /boot
tmpfs                  1544956         0   1544956   0% /dev/shm

The 26377276 references the old size of the HDD. It's 30GB actually but after formatting etc it comes to 26Gb

Make a note of the Volume Group name /dev/mapper/VolGroup00-LogVol00, you're going to need it later.

3 Create a new partition that will contain a new physical volume

Run the print command to view the partitions


parted /dev/sda print


Model: VMware Virtual disk (scsi)
Disk /dev/sda: 64.4GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      32.3kB  107MB   107MB   primary  ext3         boot 
 2      107MB   32.2GB  32.1GB  primary               lvm  

You can see that I have a 64.4GB HDD but the partitions only extend to 32.2GB... so I have loads spare.
I need to create a new primary partition to use this space:

parted /dev/sda "mkpart primary 32.2GB 64.4GB"


Do another print to check you now have a new partition

parted /dev/sda print

Model: VMware Virtual disk (scsi)
Disk /dev/sda: 64.4GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      32.3kB  107MB   107MB   primary  ext3         boot 
 2      107MB   32.2GB  32.1GB  primary               lvm  
 3      32.2GB  64.4GB  32.2GB  primary                    

OK, that looks good!


4 Create a new physical volume

We need to create a new physical volume. Let's look at what we already have:

lvm pvs
  /dev/hda: open failed: No medium found
  PV         VG         Fmt  Attr PSize  PFree
  /dev/sda2  VolGroup00 lvm2 a-   29.88G    0 

OK, so I already have /dev/sda2, so let's call the new one /dev/sda3 

lvm pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created

and let's check that it was created properly

lvm pvs
  /dev/hda: open failed: No medium found
  PV         VG         Fmt  Attr PSize  PFree 
  /dev/sda2  VolGroup00 lvm2 a-   29.88G     0 
  /dev/sda3             lvm2 a-   30.00G 30.00G

Hey.. that looks good..it even shows that I have 30GB free!


5 Add this new physical volume to the volume group

lvm vgextend VolGroup00 /dev/sda3
  Volume group "VolGroup00" successfully extended

   
6 Extend the logical volume in the volume group

Running the lvm vgdisplay command allows us to see the Free PE statistics. I want to use all this space, so make a note of it.

lvm vgdisplay VolGroup00
  --- 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               59.84 GB
  PE Size               32.00 MB
  Total PE              1915
  Alloc PE / Size       956 / 29.88 GB
  Free  PE / Size       959 / 29.97 GB
  VG UUID               doO2t7-BlLf-1rW5-mbNU-zvUq-pzUb-ZAsBD4

The lvm lvextend command expands the logical volume. The /dev/mapper/VolGroup00-LogVol00 comes from step 2... I told you to remember it!

lvm lvextend -l+959 /dev/mapper/VolGroup00-LogVol00
  Extending logical volume LogVol00 to 55.94 GB
  Logical volume LogVol00 successfully resized


7 Resize the file system

After extending the volume group and the logical volume, it is now possible to resize the file system. This is done using ext2online. First I verify the file system size, perform the resize, and then verify the size again.

df -h 
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       26G  3.7G   21G  16% /
/dev/sda1              99M   20M   75M  21% /boot
tmpfs                 1.5G     0  1.5G   0% /dev/shm

resize2fs /dev/mapper/VolGroup00-LogVol00

df -h 
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       55G  3.7G   48G   8% /
/dev/sda1              99M   20M   75M  21% /boot
tmpfs                 1.5G     0  1.5G   0% /dev/shm


Congratulations, you have resized your hard disk!

I hope this helps you out.. please click on an advert to show your thanks.