How to Promote a Virtual Machine’s Disks?

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;
}
}

This entry was posted in vSphere API and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  • NEED HELP?


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__ doublecloud.org.

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.