Today I needed to resize a VMWare Virtual Disk (vmdk) for a Windows Server 2003 image running on a Red Hat Enterprise Linux 5 host using LVM to manage the physical, local disk space. In my case, my logical volume was too small to accommodate the vmdk expansion and so I had to grow my logical volume before I could begin the VMWare portion of the work.
I must preface all of this, of course, by stating that you must make a complete backup of your virtual machine before doing something as invasive as this. While this process is reasonably safe there is always the potential for disaster. Take precautions.
The lvextend command is used to increase the size of the logical volume. You can view your current logical volumes with lvdisplay. I use the -L+ syntax as a safety measure to be sure that my drive is getting larger and not shrinking accidentally due to a typo. In this example I am expanding the /dev/VolGroup00/lvxen logical volume by an additional 80GB.
lvextend -L+80G /dev/VolGroup00/lvvmware
This first step can be completed while the virtual machine is still running. It will happily extend your available space in the background. Our next step, however, requires that you power down your virtual machine before continuing.
Now that we have created space on our logical volume we need to expand the Linux local filesystem before we can expand the virtual filesystem running on top of it. Assuming that we are using the current standard Ext3 this is very simple:
e2fsck -f /dev/VolGroup00/lvvmware
Obviously for my purposes I use a /vmware directory structure for holding all of my disk images. You will need to adjust as needed for your own setup. /var/vmware is another common option.
Now we just enlarge the virtual disk itself. We will do this through the vmware-vdiskmanager command. You will need to execute this command on your vmdk and not your flat-vmdk even though this seems counter-intuitive when looking at your directory structure.
vmware-vdiskmanager -x 22GB ph-w2k3-ad.vmdk
This concludes the easy part. Now you have plenty of logical disk space available for Windows but in order to expand the System drive of Windows you will need to use a third party tool. Windows Server 2003 is unable to make partition changes that affect the running system.
If you are like me, you will want to fire up your virtual machine just to make sure that everything is okay after the disk change, but you will need to turn it off again before we make changes to the partition table.
There are many tools that can be used for this task but I decided to use GParted, which is available as a live CD which you can download for free. For the version that I used, I just cd’d into /tmp and used this command to get my copy of GParted’s bootable CD ISO file.
Using your VMWare Server Console (or through the command line) you will need to set your Windows Server image to boot from the GParted ISO which you just downloaded. Then go ahead and “start this virtual machine.”
You will likely need to hit “Esc” as soon as the virtual machine starts so that you can select to boot from CD. I keep my Virtual BIOS set to boot directly to the hard drive under normal circumstances because it is faster and I don’t want to accidentally boot to some CD media unless I really, really mean it.
Once GParted starts you will be given a boot menu. The default option works fine in more cases and worked fine for me. You will need to select your keyboard layout and then you will be taken to GParted’s graphical partition manager screen.
Once in the GParted Partition Manager you should see the current partition that you had before we started, in my case called /dev/sda1 and marked as being an NTFS file system. Mine also shows the “unallocated” partition space into which I will be expanding my /dev/sda1 partition.
Start by selection the partition which you are seeking to resize (sda1 for me) and then select “Resize/Move”. This will open the Resize/Move window. Do not alter the first numer – “Free Space Preceding”, this is for “moving” your partition. You only want to alter the second number – “New Size.” If you are doing like me and have created some empty space specifically for this purpose then you will simply set this number to the “Maximum Size” as displayed in the window. Then select “Resize/Move” to continue.
Once you have completed that step you can visually confirm that the disk now looks the way that you want it to look. If you look at the bottom of the window you will see that there is “1 operation pending.” If everything looks alright go ahead and click “Apply” to commit your changes and to resize your partition.
Once the resizing completes you are safe to reboot your virtual machine into Windows again. Double click the “Exit” button on the GParted desktop. Reboot should already be selected so just choose OK to continue.
When Windows starts it will detect the drive configuration change and force a disk consistency check. Allow it to run through this process and when it completes the system will restart automatically. Once Windows restarts you should see that your drive has been resized.