Wednesday, December 24, 2014

Extending VMware VM’s VMDK disk with command line vmkfstools

Those who have recently upgraded to vSphere 5.5 may have noticed that if you’ve also gone ahead and upgraded the virtual machine’s hardware to version 10 would result in the inability to edit the virtual machine’s settings via the vSphere Client because you are now forced to use the vSphere Web Client as the following message indicates:

You cannot use the vSphere Client to edit the settings of virtual machines of version 10 or higher.

Use the vSphere Web Client to edit the settings of this virtual machine.

image

Most administrators at this point can proceed by logging onto the vSphere Web Client to edit the settings but what if vCenter was down?  Connecting directly to the host wouldn’t allow you to edit the settings either.  The workaround to this is to actually update the client to vSphere Client 5.5 Update 2 which would result in the following message when you attempt to edit a virtual machine’s settings:

You are about to edit a virtual machine of version 10 or higher. Only version 8 features will be available for Edit using this client.

If you want to edit the advanced hardware features of this virtual machine, please use the vSphere Web Client to login to vCenter.

image

Various advanced properties won’t be available but actions such as extending a hard disk is allowed.

There has been times where I’ve found myself stuck in a datacenter without access to the internet which therefore means upgrading the vSphere Client to 5.5 Update 2 would not be possible.  The workaround to this is to use the command line via SSH or console to extend the disk and the following outline the steps to do this.

Step #1 – Check for Snapshots

The first step for this process is to ensure that there are no snapshots created on the VM that you are attempting to extend the VMDK.  There are plenty of ways to determine whether there are snapshots such as browsing into the directory of the virtual machine and looking for -delta.vmdk files as shown in the following screenshot where ls was executed before and after a snapshot was created:

image

Another method which I favour in using is to use the vim-cmd command to list snapshot information for the virtual machine.  The reason why I favour the vim-cmd command is because it is available via the ESXi’s console with no reliance on PowerCLI which requires vCenter to be up and if vCenter was up, you could just use the Web Client to increase the disk size.

Before I begin, note the following handy CLI reference site which I use quite frequently:
http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2012964

Begin by SSH or accessing the console of the ESXi server that the VM is hosted on and execute the following command to list the VMs:

vim-cmd vmsvc/getallvms | more

The VM we’re interested in is the -FS-01 server:

image

Note the VMID # on the left which in this case is 21.

With the VMID obtained, proceed by executing the following command to list the snapshots:

vim-cmd vmsvc/snapshot.get [VMID]

For this example, we’ll execute the following:

vim-cmd vmsvc/snapshot.get 21

image

Note that there is a snapshot named Snappy for this VM.

To remove all snapshots on this VM, continue by executing the following command:

vim-cmd vmsvc/snapshot.removeall [VMID]

For this example, we’ll execute the following:

vim-cmd vmsvc/snapshot.removeall 21

image

You can verify that the snapshots are gone by executing:

vim-cmd vmsvc/snapshot.get 21

… again.

Step #2 – Expand VMDK with VMKFSTOOLS

Before I begin, note that expanding a disk with vmkfstools requires the VM be powered off and I am unable to find a way to expand the disk while the VM is powered on even though the vSphere Client allows it so when I do, I’ll update this post.  To shutdown the VM, execute the following command:

vim-cmd vmsvc/power.shutdown vmid

For this example, we’ll execute the following:

vim-cmd vmsvc/power.shutdown 21

With the VM powered down, continue by browsing to the directory of the VM and execute:

du -ah

… to list all the files and the size:

image

The disk we’re going to expand for this example is named -FS-01_3.vmdk which is currently at 1.0G.  The command we’ll use to expand the disk is:

vmkfstools -X <sizeOfVMDK>GB <diskname>.vmdk

… where we’ll need to define the size of the VMDK and the VMDK file name.  Note that the size defined here is the total size so the final size of the VMDK would be so if we use 9GB, it would expand the VMDK to the size of 9GB and not 9 + 1 = 10GB.  Also, the VMDK filename we’ll be using is the FS-01_3.vmdk and NOT the FS-01_3-flat.vmdk file even though that file has a size associated to it.

For this example, we’ll be executing:

vmkfstools -X 9GB FS-01_3.vmdk

image

--------------------------------------------------------------------------------------------------------------------------------------------------------------

Note that you’ll receive the following error if you forget to shut down the VM prior to executing this command:

Failed to extend disk : Failed to lock the file (16392).

image

--------------------------------------------------------------------------------------------------------------------------------------------------------------

Rerun du -ah to see the updated size:

image

Now power on VM with the following command:

vim-cmd vmsvc/power.on vmid

For this example, we’ll be executing:

vim-cmd vmsvc/power.on 21

Proceed and expand the volume in the guest VM’s operating system (i.e. Computer Management):

image

image

3 comments:

JeezoPeezo said...

Terence,

Thanks for the post about expanding the disk via cmd line. This is going to be super useful.

Thanks,

Rob Jaudon

Anonymous said...

Thank You!! Using your instructions as a guideline, we were able to modify them and successfully create a new drive and add it to a live system. This prevented an outage.

Tim S.

Anonymous said...

THANKS! This was a life saver.