Increasing Partition Size on a VMWare Linux Virtual Machine

Naively, when creating my new VMWare ESXi virtual machines, I was expecting to be able to resize the disks with a one-liner as I have done in the past with container based virtualisation. Unfortunately it’s not quite that simple, the VM partitions will need manually resizing:

Increase the virtual disk size via Vsphere to the relevant amount (VM > edit > hard disk 1). This can be done whilst the VM is live.

Reboot the vm so it picks up the new disk size:
shutdown -r now

Check new disk size, for example this 10GB disk has been increased to 15GB, but its not yet allocated:
fdisk -l

Disk /dev/sda: 15.0 GB, 15032385536 bytes

255 heads, 63 sectors/track, 1827 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 1305 10377990 8e Linux LVM

To add a new partition do the following:
fdisk /dev/sda

n - new
p - primary
3 - partition number (use next available number)
Select default first available cylinder and default last available cylinder
t -type
3 - partition number
8e -set type as LVM
p -view the new partitions layout
w write out the new partitions layout to disk if it looks ok

At this stage you should see something like the following. You will probably get an error as it can’t be read yet, just reboot to apply:

Disk /dev/sda: 15.0 GB, 15032385536 bytes
255 heads, 63 sectors/track, 1827 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 1305 10377990 8e Linux LVM
/dev/sda3 1306 1827 4192965 8e Linux LVM

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

Convert the partition to a physical volume, so that LVM can use it:
pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created

Add the physical volume to the existing volume group:
vgextend VolGroup00 /dev/sda3
Volume group "VolGroup00" successfully extended

You should now see the free space in the volume group:
vgdisplay
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 6
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 13.84 GB
PE Size 32.00 MB
Total PE 443
Alloc PE / Size 316 / 9.88 GB
Free PE / Size 127 / 3.97 GB
VG UUID 5KfNoG-Tp5Y-Q2Nl-PjFZ-FnrA-e7eG-YvQEWc

Assign the free space in the volume group to the logical volume:
lvextend -l +100%FREE /dev/VolGroup00/LogVol00

Perform the online resize of the mounted filesystem:
resize2fs -p /dev/mapper/VolGroup00-LogVol00
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/mapper/VolGroup00-LogVol00 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/mapper/VolGroup00-LogVol00 to 3244032 (4k) blocks.
The filesystem on /dev/mapper/VolGroup00-LogVol00 is now 3244032 blocks long.

Job done, see the new size:
df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
12G 1.9G 9.6G 17% /
/dev/sda1 99M 20M 75M 21% /boot
tmpfs 376M 0 376M 0% /dev/shm

You can leave a response, or trackback from your own site.

Leave a Reply