VMware and LVM – Increasing disk space within a Linux-based virtual machine

One of the great things about enterprise Linux distro installations, such as RedHat, SLES, etc is that most of them are defaulting to a LVM (Logical Volume Manager) filesystem which is a natural fit for Linux virtualized systems.

Using LVM with a virtualized hard drive makes it very easy to grow the disk space within minutes when the need arises. A good system administrator will already understand the LVM relationship between Physical Volumes, Volume Groups, and Logical Volumes.

Suppose you wanted to grow the disk space within a Linux virtual machine to 50GB:

The first step is to increase the VM vmdk file size:

This can be done via the VMware admin GUI by opening the settings of the VM and using the Expand utility to grow the hard drive size to 50 GB.

Another alternative is to use vmware-vdiskmanager.exe on the command line:

vmware-vdiskmanager.exe -x 50GB "Red Hat Enterprise Linux 5.4-cl1.vmdk"

Note that this action doesn’t have any resizing effect on the existing partitions of the virtual machine and only adds more unpartitioned space to the virtual hard drive.

Convert the unpartitioned space into usable filesystem so it can be included within the LVM filesystem.

Boot into the VM and su to the superuser account

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, it’s likely located in /dev/sda (note the size of /dev/sda which is roughly the new expanded size).

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)
 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)

reboot the system so the new partition is recognized by the system.

reboot

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

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

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
This entry was posted in Web/Tech. Bookmark the permalink.