Wednesday, March 4, 2009

Expanding VMware virtual disk for Linux

Recently we had to resize a VMware virtual machine disk. The original disk allocated for the virtual machine was only 8 GiB when it was noticed 32 GiB would have been desired. Luckily the virtual disk can be expanded with the VMware web interface or using the command line tools. Also, the Linux file systems, at least ext3, can be resized, so it is possible to resize virtual machines without having to reinstall.

A note of caution: What I describe belowed worked for me. I also took a backup from the virtual machine before doing any of the operations. There is no guarantee that this is the best, correct or even recommended way of doing the the resize.

About the terms I have used:
  • host is the physical server that runs VMware Server. In our case version 2.0
  • guest is the virtual machine that runs on the host
Expanding the virtual disk
The first task was to expand the original 8 GiB virtual disk to 32 GiB. Here is what I did on the host:

host% sudo /opt/vmware/bin/vmware-vdiskmanager -x 32GB devel.vmdk
Grow: 100% done.
Disk expansion completed successfully.

WARNING: If the virtual disk is partitioned, you must use a third-party
utility in the virtual machine to expand the size of the
partitions. For more information, see:
http://www.vmware.com/support/kb/enduser/std_adp.php?p_faqid=1647

Now that the VMware disk image has been resized, what does it look like from the guest operating system? Boot up the guest and check with fdisk command:

guest% sudo fdisk -l /dev/sda
Disk /dev/sda: 34.3 GB, 34359738368 bytes
255 heads, 63 sectors/track, 4177 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000df028

Device Boot Start End Blocks Id System
/dev/sda1 * 1 1044 8385898+ 83 Linux

As one can see, the disk is now larger, but the Linux partition is still the same size as before resize.

The next steps are to expand the partition and its file system. In this case since we are resizing the root partition, the operation can not be done from the host operating itself. One method is to boot a live CD that contains the necessary tools.

Expanding the Linux partition and file system
For expanding the Linux partition and its file system, I decided to try GParted the Gnome Partition Editor which is conveniently available as a live CD.


The main screen of GParted shows what the current configuration is. There is 8 GiB of allocated space for sda1 and 24 GiB space available for expansion.



The resizing operation was simple. I clicked on "Resize/Move" to open a new window with a slider for moving and resizing the partition. I dragged the slider to cover the whole partition and clicked "Apply" from the main window. After the operation was completed, the partition was ready. Choosing "Partition -> Information" brings up this window:

The result
So did it work? Yes, just as expected.

After shutting the the GParted live CD and rebooting from the hard disk, everything was as expected. Host root file system was 32GiB, fdisk did not complain and no oddities were observed. Here are a couple of examples:


host% df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 32G 5.7G 25G 19% /
[rest of the output removed]

host% sudo fdisk -l /dev/sda
Disk /dev/sda: 34.3 GB, 34359738368 bytes
255 heads, 63 sectors/track, 4177 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000df028

Device Boot Start End Blocks Id System
/dev/sda1 * 1 4177 33551721 83 Linux

1 comment:

Mikko Ohtamaa said...

Nice article Heikki.

I did the same thing just few days ago. I got everything to the point GParted should have applied the changes. It just spit out some nonsense and useless error message (can't remember) and I ended up just mounting another volume in virtual Linux and moving huge stuff there.