Home > vSphere API > How to Promote a Virtual Machine’s Disks?

How to Promote a Virtual Machine’s Disks?

August 10th, 2010 Leave a comment Go to comments

What does a promotion mean for a virtual machine’s disks? When you get a promotion, you may have more salary, a better title, bigger office space, etc. For sure a virtual disk cannot earn salary and doesn’t care about title, but it can occupy bigger space in datastore.

In my previous blog, I discussed how to create linked virtual machines using vSphere API. These linked virtual machines share a common disk as base, therefore the total disk consumption is significantly reduced. When a virtual disk is promoted, it gets its own “office” other than sharing it with others.

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

The vSphere API to promote virtual disks is promoteDisks_Task defined with VirtualMachine type. It has a tricky parameter called “unlink” (type: boolean) . According to the API reference:

1. If the unlink parameter is true, any disk backing which is shared shared by multiple virtual machines is copied so that this virtual machine has its own unshared version. Copied files always end up in the virtual machine’s home directory.

2. Any disk backing which is not shared between multiple virtual machines and is not associated with a snapshot is consolidated with its child backing.

Now when should you use true or false? “If the unlink parameter is true, the net effect of this operation is improved read performance, at the cost of disk space. If the unlink parameter is false the net effect is improved read performance at the cost of inhibiting future sharing.”

Here is the sample code that illustrates the usage of the API:

package com.vmware.vim25.mo.samples.vm;

import java.net.URL;

import com.vmware.vim25.TaskInfoState;
import com.vmware.vim25.VirtualDevice;
import com.vmware.vim25.VirtualDisk;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.Task;
import com.vmware.vim25.mo.VirtualMachine;

/**
* @author Steve Jin (<a href="mailto:sjin@vmware.com">sjin@vmware.com</a>)
* This sample consolidates or copies a linked clone. It works only with vSphere 4 and after.
* You should not run it against VI 3.5 or earlier.
*/

public class VmPromoteDisk
{
public static void main(String[] args) throws Exception
{
if (args.length != 6)
{
System.out.println("java VMPromoteDisk <SdkUrl> <username> <password> <VmName> <ifUnlink> <diskName>");
System.out.println("java VMPromoteDisk <a href="https://8.8.8.8/sdk">https://8.8.8.8/sdk</a> root password myVM1 true diskName");
return;
}
String vmName = args[3];
boolean unlink = "true".equalsIgnoreCase(args[4])? true : false;
String diskName = args[5];

ServiceInstance si = new ServiceInstance(new URL(args[0]), args[1], args[2], true);
VirtualMachine vm = (VirtualMachine) new InventoryNavigator(si.getRootFolder()).searchManagedEntity("VirtualMachine", vmName);

promoteDeltaDisk(vm, unlink, diskName);

si.getServerConnection().logout();
}

private static void promoteDeltaDisk(VirtualMachine vm, boolean unlink, String diskName) throws Exception
{
VirtualDisk vDisk = findVirtualDisk(vm, diskName);
if(vDisk == null)
{
System.out.println("Virtual Disk " + diskName + " not found");
return;
}

try
{
Task task = vm.promoteDisks_Task(unlink, new VirtualDisk[]{vDisk});

task.waitForTask();
if(task.getTaskInfo().getState() == TaskInfoState.error)
{
System.out.println("Failure: Virtual disk cannot be promoted.");
}
if(task.getTaskInfo().getState() == TaskInfoState.success)
{
System.out.println("Virtual disk is promoted successfully.");
}
}
catch(Exception e)
{
System.out.println("Exception while promoting virtual disk: " + e);
}
}

private static VirtualDisk findVirtualDisk(VirtualMachine vm, String diskName) throws Exception
{
VirtualDevice[] devices = (VirtualDevice[]) vm.getPropertyByPath("config.hardware.device");

for(int i=0; i<devices.length; i++)
{
if(devices[i] instanceof VirtualDisk)
{
VirtualDisk vDisk = (VirtualDisk)devices[i];
if(diskName.equalsIgnoreCase(vDisk.getDeviceInfo().getLabel()))
{
return vDisk;
}
}
}
return null;
}
}

  1. No comments yet.
  1. No trackbacks yet.