Expanding a VM’s Hard Drive

Today, I needed to expand the hard drive in a VMware Workstation 6.5 image. It’s a simple process, but can be non-trivial if you don’t know where to start. I’ve posted the steps below, (much more for my reference than anyone else’s 😉 )

  1. Open a command prompt, and CD to the directory which contains the VMDK file you wish to expand.
  2. Use the command “C:\Program Files\VMware\VMware Workstation\vmware-vdiskmanager.exe –x 6GB ‘Windows XP Professional’”, where ‘Windows XP Professional’ is the name of the Virtual Machine Disk File. This will increase the disk allocation to 6GB, (this may take a while).
  3. Now to join the unallocated space to the primary partition – Ensure your image is powered off, and is set to mount the gparted ISO, (can be downloaded here). This can be changed in the image options under Settings -> CD/DVD. Select the ‘Use ISO image:’ option and point it to the gparted ISO.
  4. Next Power on the Virtual Machine, press ESC at the BIOS screen to get to the Boot Menu. Select CD-ROM as the Boot device.
  5. Gnome Partition Editor will load, press Enter at the boot screen. Select all options as they are asked as default, (ensure you select English as your language.)
  6. To complete the join of the unallocated space to your partition, complete the following steps:

• Once the partition editor loads, click on /dev/sda1 in the partition list.
• Click the Resize/Move button.
• Click and drag the arrow to extend the size of the partition, make sure you do a resize (double arrow) and not a move (four way arrow) so you should have 0mb free space preceding and following, and then click the Resize/Move button.
• Next click the Apply button and then the operation will start, you can expand Details to see the progress, once completed click the Close button.
• Click the power button in the top left corner, then select reboot.
• Edit the VM and remove the ISO from the CD/ROM device.
• When the OS restarts it will do a Check Disk, let this complete, Windows will prompt for a reboot after you login.
• Reboot and load Disk Management (diskpart.exe -> Show volume) and your Primary Partition will be the new size without any unallocated space.

See, it’s as easy as that 🙂 …

Automating Virtual Machine operations on ESXi Server from C#

VMware provides two really useful API’s for automating virtual machine (VM) tasks on both VMware Workstation and VMware ESXi server.

  • VI Infrastructure API
  • VIX API

These are extremely easy to use from C#. In a QA environment, the automation of VM’s can be hugely benifical, wheather attempting to automate an environment for build sanity checks or functional tests.

This post will outline the basics of using the VIX API from C#, in order to perform operations on VMware ESXi server. If you don’t have access to an ESXi server, you can install it on a VM, it’s free to download from the VMware website!

For starters, you will need to install the API’s on your development machine. In order to download, you will need to create a VMware account, which you may already have if you have downloaded Workstation or ESXi server in the past. If you dont, you can create an account for free. Once logged into your account, you can download both API’s from the ‘Support & Downloads’ section.

Let me explain the differance between these two API’s. From VMware’s own documentation:

The VI API provides access to the VMware Infrastructure management components—the managed objects that can be used to manage, monitor, and control life-cycle operations of virtual machines and other VMware infrastructure components (datacenters, datastores, networks, and so on).”

VIX on the other hand, is used to automate the actual operations on VM’s, such as booting them, copying in files, getting/setting VM environment varibles and other tasks you may wish to perform. The coolest part of VIX is that a wrapper for C# exists, created by Daniel Doubrovkine over at dblock.org. This wrapper, ‘VMwareTasks’, provides a simple object-orientated approach to VIX, which will be familar to C# developers. Download the wrapper here.

Now for the basics of using the VIX API and VMwareTasks wrapper. Create a new console application project in Visual Studio. You will need to add a reference to the VMwareTasks DLL, which is located in the ‘bin’ directory when you extract the VMwareTasks download.

Look how simple it is to power on a VM!


// Declare a new virtual host
VMWareVirtualHost host = new VMWareVirtualHost();

// Connect to the ESXi server
host.ConnectToVMWareVIServer("192.168.1.39", "root", "password123");

// Power on an existing VM by name
VMWareVirtualMachine machine = host.Open("[datastore1] XPP_SP2.vmx");
machine.PowerOn();

The simple code above just connects to an ESXi server, and powers on an existing VM, but you can see how easy it is to perform operations on VM’s.

Here’s how to create and revert to a snapshot:


VMWareVirtualHost host = new VMWareVirtualHost();
host.ConnectToVMWareVIServer("192.168.1.39", "root", "password123");
VMWareVirtualMachine machine = host.Open("[datastore1] Vista_EN.vmx");
machine.PowerOn();
machine.Login("Tester", "testing");

string snapShotName = "base";
machine.Snapshots.CreateSnapshot(snapShotName, "Clean");
machine.PowerOff();

VMWareSnapshot snapshot = machine.Snapshots.GetNamedSnapshot("base");
snapshot.RevertToSnapshot();

Or to create a directory:


machine.CreateDirectoryInGuest(@"C:\TestDir");

You can see from the above examples how easy it is to perform operations on VM’s using these API’s. Install the API’s and play around with the functionality, I guarantee you’ll be impressed!