package com.vmware.samples.vm;
import java.util.*;
import com.vmware.apputils.version.*;
import com.vmware.vim25.*;
import com.vmware.apputils.version.VersionUtil;
import java.util.ArrayList;
import java.util.Iterator;
import com.vmware.apputils.*;import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;/**
*<pre>
*This sample is used to create a linked clone from an existing snapshot.
*Each independent disk needs a DiskLocator with diskMoveType as moveAllDiskBackingsAndDisallowSharing.
*
*<b>To run this samples following parameters are used :</b>
*
*vmname [required] : Name of the virtual machine
*snapshotname [required] : Name of the snaphot
*clonename [required] : Name of the clonename
*
*<b>Command:</b>
*<b>Create a linked clone </b>
*run.bat com.vmware.samples.vm.vmlinkedclone
*--username [username] --password [password] --vmname [myVM]
*--snapshotname [sname] -- clonename [cname]
*</pre>
*/public class VMLinkedClone1 extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = -1925386075159098213L;
private ExtendedAppUtil ecb = null;
private static AppUtil cb = null; private static ManagedObjectReference provisionChkr = null;
private static VersionUtil versionUtil = null;
private String targ[];
private String arg[];
String a,b,c;
private JPanel jp1, jp2;
private JLabel jl[] = new JLabel [3];
private JTextField jt[] = new JTextField[3];
private JButton jb1, jb2;
public VMLinkedClone1(){}
public VMLinkedClone1(String adf) throws Exception
{
super("链接克隆虚拟机");
setSize(500, 400);
setLocation(400, 320);
jp1 = new JPanel();
jl[0] = new JLabel(" vmname :");
jl[1] = new JLabel("snapshotname:");
jl[2] = new JLabel(" clonename :");
for (int i = 0; i < 3; i++) {
jt[i] = new JTextField();
jl[i].setHorizontalAlignment(SwingConstants.CENTER);
jp1.add(jl[i]);
jp1.add(jt[i]);
}
jp2 = new JPanel();
jb1 = new JButton("保存");
jb1.setBounds(50, 0, 100, 30);
jb1.addActionListener(this);
jb2 = new JButton("取消");
jb2.setBounds(230, 0, 100, 30);
jb2.addActionListener(this);
jp2.add(jb1);
jp2.add(jb2);
jp1.setBounds(50, 25, 350, 280);
jp1.setLayout(new GridLayout(8, 2, 0, 10));
jp2.setBounds(50, 305, 400, 75);
jp2.setLayout(null);
this.add(jp1);
this.add(jp2);
this.setLayout(null);
setVisible(true);
this.setResizable(false);
}
private void createLinkedClone(String [] args, String cookieString)
throws Exception { ArrayList apiVersions = VersionUtil.getSupportedVersions(
cb.get_option("url"));
if(!VersionUtil.isApiVersionSupported(apiVersions,"4.0")) {
System.out.println("Linked clone feature is for K/L only and not "
+ "supported by the old VI SDK 2.0 API");
return;
}
ecb = ExtendedAppUtil.initialize("VMLinkedClone"
,VMLinkedClone1.constructOptions()
,args);
ecb.connect(cookieString);
ManagedObjectReference vmMOR = getMOR(ecb.get_option("vmname"),
"VirtualMachine", null);
if(vmMOR != null) {
ManagedObjectReference snapMOR = getSnapshotReference(vmMOR,
ecb.get_option("snapshotname"));
if(snapMOR != null) {
ArrayList independent_virtual_disk_keys =
getIndependenetVirtualDiskKeys(vmMOR); VirtualMachineRelocateSpec rSpec = new VirtualMachineRelocateSpec();
if(independent_virtual_disk_keys.size() > 0) {
ManagedObjectReference [] ds
= (ManagedObjectReference [])
ecb.getServiceUtil3().getDynamicProperty(vmMOR,
"datastore"); VirtualMachineRelocateSpecDiskLocator [] diskLocator =
new VirtualMachineRelocateSpecDiskLocator
[independent_virtual_disk_keys.size()]; Iterator it = independent_virtual_disk_keys.iterator();
int count = 0;
while(it.hasNext()) {
diskLocator[count] =
new VirtualMachineRelocateSpecDiskLocator();
diskLocator[count].setDatastore(ds[0]);
diskLocator[count].setDiskMoveType
(VirtualMachineRelocateDiskMoveOptions._moveAllDiskBackingsAndDisallowSharing);
diskLocator[count].setDiskId((Integer)it.next());
count = count + 1;
}
rSpec.setDiskMoveType
(VirtualMachineRelocateDiskMoveOptions._createNewChildDiskBacking);
rSpec.setDisk(diskLocator);
}
else {
rSpec.setDiskMoveType
(VirtualMachineRelocateDiskMoveOptions._createNewChildDiskBacking);
}
VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
cloneSpec.setPowerOn(false);
cloneSpec.setTemplate(false);
cloneSpec.setLocation(rSpec);
cloneSpec.setSnapshot(snapMOR); try {
ManagedObjectReference parentMOR =
(ManagedObjectReference)
ecb.getServiceUtil3().getDynamicProperty(vmMOR,"parent");
ManagedObjectReference taskMOR =
ecb.getServiceConnection3().getService().cloneVM_Task
(vmMOR,
parentMOR,
ecb.get_option
("clonename"),
cloneSpec);
String status = ecb.getServiceUtil3().waitForTask(taskMOR);
if(status.equalsIgnoreCase("failure")) {
System.out.println("Failure -: Virtual Machine " +
"cannot be cloned");
}
if(status.equalsIgnoreCase("sucess")) {
System.out.println("Virtual Machine Cloned successfully.");
}
}
catch(Exception e) {
if(e instanceof org.apache.axis.AxisFault) {
org.apache.axis.AxisFault fault = (org.apache.axis.AxisFault)e;
org.w3c.dom.Element [] errors = fault.getFaultDetails();
for(int i=0; i<errors.length; i++) {
if(errors[i].toString().indexOf("DuplicateName") != -1) {
System.out.println("Virtual Machine with the same " +
"name already exists");
}
else if(errors[i].toString().indexOf("InvalidArgument") != -1) {
System.out.println("Specification is invalid");
}
else if(errors[i].toString().indexOf("InvalidName") != -1) {
System.out.println("Virtual Machine name is empty or " +
"too long");
}
else if(errors[i].toString().indexOf("RuntimeFault") != -1) {
System.out.println(errors[i].toString());
}
else {
System.out.println(errors[i].toString());
}
}
}
else {
throw e;
}
}
}
else {
System.out.println("Snapshot " + ecb.get_option("snapshotname") +
" doesn't exist");
}
}
else {
System.out.println("Virtual Machine " + ecb.get_option("vmname") +
" doesn't exist");
}
}
import java.util.*;
import com.vmware.apputils.version.*;
import com.vmware.vim25.*;
import com.vmware.apputils.version.VersionUtil;
import java.util.ArrayList;
import java.util.Iterator;
import com.vmware.apputils.*;import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;/**
*<pre>
*This sample is used to create a linked clone from an existing snapshot.
*Each independent disk needs a DiskLocator with diskMoveType as moveAllDiskBackingsAndDisallowSharing.
*
*<b>To run this samples following parameters are used :</b>
*
*vmname [required] : Name of the virtual machine
*snapshotname [required] : Name of the snaphot
*clonename [required] : Name of the clonename
*
*<b>Command:</b>
*<b>Create a linked clone </b>
*run.bat com.vmware.samples.vm.vmlinkedclone
*--username [username] --password [password] --vmname [myVM]
*--snapshotname [sname] -- clonename [cname]
*</pre>
*/public class VMLinkedClone1 extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = -1925386075159098213L;
private ExtendedAppUtil ecb = null;
private static AppUtil cb = null; private static ManagedObjectReference provisionChkr = null;
private static VersionUtil versionUtil = null;
private String targ[];
private String arg[];
String a,b,c;
private JPanel jp1, jp2;
private JLabel jl[] = new JLabel [3];
private JTextField jt[] = new JTextField[3];
private JButton jb1, jb2;
public VMLinkedClone1(){}
public VMLinkedClone1(String adf) throws Exception
{
super("链接克隆虚拟机");
setSize(500, 400);
setLocation(400, 320);
jp1 = new JPanel();
jl[0] = new JLabel(" vmname :");
jl[1] = new JLabel("snapshotname:");
jl[2] = new JLabel(" clonename :");
for (int i = 0; i < 3; i++) {
jt[i] = new JTextField();
jl[i].setHorizontalAlignment(SwingConstants.CENTER);
jp1.add(jl[i]);
jp1.add(jt[i]);
}
jp2 = new JPanel();
jb1 = new JButton("保存");
jb1.setBounds(50, 0, 100, 30);
jb1.addActionListener(this);
jb2 = new JButton("取消");
jb2.setBounds(230, 0, 100, 30);
jb2.addActionListener(this);
jp2.add(jb1);
jp2.add(jb2);
jp1.setBounds(50, 25, 350, 280);
jp1.setLayout(new GridLayout(8, 2, 0, 10));
jp2.setBounds(50, 305, 400, 75);
jp2.setLayout(null);
this.add(jp1);
this.add(jp2);
this.setLayout(null);
setVisible(true);
this.setResizable(false);
}
private void createLinkedClone(String [] args, String cookieString)
throws Exception { ArrayList apiVersions = VersionUtil.getSupportedVersions(
cb.get_option("url"));
if(!VersionUtil.isApiVersionSupported(apiVersions,"4.0")) {
System.out.println("Linked clone feature is for K/L only and not "
+ "supported by the old VI SDK 2.0 API");
return;
}
ecb = ExtendedAppUtil.initialize("VMLinkedClone"
,VMLinkedClone1.constructOptions()
,args);
ecb.connect(cookieString);
ManagedObjectReference vmMOR = getMOR(ecb.get_option("vmname"),
"VirtualMachine", null);
if(vmMOR != null) {
ManagedObjectReference snapMOR = getSnapshotReference(vmMOR,
ecb.get_option("snapshotname"));
if(snapMOR != null) {
ArrayList independent_virtual_disk_keys =
getIndependenetVirtualDiskKeys(vmMOR); VirtualMachineRelocateSpec rSpec = new VirtualMachineRelocateSpec();
if(independent_virtual_disk_keys.size() > 0) {
ManagedObjectReference [] ds
= (ManagedObjectReference [])
ecb.getServiceUtil3().getDynamicProperty(vmMOR,
"datastore"); VirtualMachineRelocateSpecDiskLocator [] diskLocator =
new VirtualMachineRelocateSpecDiskLocator
[independent_virtual_disk_keys.size()]; Iterator it = independent_virtual_disk_keys.iterator();
int count = 0;
while(it.hasNext()) {
diskLocator[count] =
new VirtualMachineRelocateSpecDiskLocator();
diskLocator[count].setDatastore(ds[0]);
diskLocator[count].setDiskMoveType
(VirtualMachineRelocateDiskMoveOptions._moveAllDiskBackingsAndDisallowSharing);
diskLocator[count].setDiskId((Integer)it.next());
count = count + 1;
}
rSpec.setDiskMoveType
(VirtualMachineRelocateDiskMoveOptions._createNewChildDiskBacking);
rSpec.setDisk(diskLocator);
}
else {
rSpec.setDiskMoveType
(VirtualMachineRelocateDiskMoveOptions._createNewChildDiskBacking);
}
VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
cloneSpec.setPowerOn(false);
cloneSpec.setTemplate(false);
cloneSpec.setLocation(rSpec);
cloneSpec.setSnapshot(snapMOR); try {
ManagedObjectReference parentMOR =
(ManagedObjectReference)
ecb.getServiceUtil3().getDynamicProperty(vmMOR,"parent");
ManagedObjectReference taskMOR =
ecb.getServiceConnection3().getService().cloneVM_Task
(vmMOR,
parentMOR,
ecb.get_option
("clonename"),
cloneSpec);
String status = ecb.getServiceUtil3().waitForTask(taskMOR);
if(status.equalsIgnoreCase("failure")) {
System.out.println("Failure -: Virtual Machine " +
"cannot be cloned");
}
if(status.equalsIgnoreCase("sucess")) {
System.out.println("Virtual Machine Cloned successfully.");
}
}
catch(Exception e) {
if(e instanceof org.apache.axis.AxisFault) {
org.apache.axis.AxisFault fault = (org.apache.axis.AxisFault)e;
org.w3c.dom.Element [] errors = fault.getFaultDetails();
for(int i=0; i<errors.length; i++) {
if(errors[i].toString().indexOf("DuplicateName") != -1) {
System.out.println("Virtual Machine with the same " +
"name already exists");
}
else if(errors[i].toString().indexOf("InvalidArgument") != -1) {
System.out.println("Specification is invalid");
}
else if(errors[i].toString().indexOf("InvalidName") != -1) {
System.out.println("Virtual Machine name is empty or " +
"too long");
}
else if(errors[i].toString().indexOf("RuntimeFault") != -1) {
System.out.println(errors[i].toString());
}
else {
System.out.println(errors[i].toString());
}
}
}
else {
throw e;
}
}
}
else {
System.out.println("Snapshot " + ecb.get_option("snapshotname") +
" doesn't exist");
}
}
else {
System.out.println("Virtual Machine " + ecb.get_option("vmname") +
" doesn't exist");
}
}
throws Exception {
ArrayList independenetVirtualDiskKeys= new ArrayList();
VirtualHardware hw =
(VirtualHardware)ecb.getServiceUtil3().getDynamicProperty(vmMOR,
"config.hardware"); VirtualDevice [] deviceArray = hw.getDevice();
for(int i=0; i<deviceArray.length; i++) {
if(deviceArray[i].getClass().getCanonicalName().indexOf("VirtualDisk") != -1) {
VirtualDisk vDisk = (VirtualDisk)deviceArray[i];
String diskMode = "";
if(vDisk.getBacking().getClass().getCanonicalName().indexOf
("VirtualDiskFlatVer1BackingInfo") != -1) {
diskMode =
((VirtualDiskFlatVer1BackingInfo)vDisk.getBacking()).getDiskMode();
}
else if(vDisk.getBacking().getClass().getCanonicalName().indexOf
("VirtualDiskFlatVer2BackingInfo") != -1) {
diskMode =
((VirtualDiskFlatVer2BackingInfo)vDisk.getBacking()).getDiskMode();
}
else if(vDisk.getBacking().getClass().getCanonicalName().indexOf
("VirtualDiskRawDiskMappingVer1BackingInfo") != -1) {
diskMode =
((VirtualDiskRawDiskMappingVer1BackingInfo)vDisk.getBacking()).getDiskMode();
}
else if(vDisk.getBacking().getClass().getCanonicalName().indexOf
("VirtualDiskSparseVer1BackingInfo") != -1) {
diskMode =
((VirtualDiskSparseVer1BackingInfo)vDisk.getBacking()).getDiskMode();
}
else if(vDisk.getBacking().getClass().getCanonicalName().indexOf
("VirtualDiskSparseVer2BackingInfo") != -1) {
diskMode =
((VirtualDiskSparseVer2BackingInfo)vDisk.getBacking()).getDiskMode();
}
if(diskMode.indexOf("independent") != -1) {
independenetVirtualDiskKeys.add(vDisk.getKey());
}
}
}
return independenetVirtualDiskKeys;
} private ManagedObjectReference getSnapshotReference(ManagedObjectReference vmmor,
String snapName)
throws Exception {
VirtualMachineSnapshotInfo snapInfo = getSnapshotInfo(vmmor);
ManagedObjectReference snapmor = null;
if (snapInfo != null) {
VirtualMachineSnapshotTree[] snapTree = snapInfo.getRootSnapshotList();
snapmor = traverseSnapshotInTree(snapTree, snapName);
}
return snapmor;
} private VirtualMachineSnapshotInfo getSnapshotInfo
(ManagedObjectReference vmmor) throws Exception {
ObjectContent[] snaps = ecb.getServiceUtil3().getObjectProperties(
null, vmmor, new String[] { "snapshot" }
); VirtualMachineSnapshotInfo snapInfo = null;
if (snaps != null && snaps.length > 0) {
ObjectContent snapobj = snaps[0];
DynamicProperty[] snapary = snapobj.getPropSet();
if (snapary != null && snapary.length > 0) {
snapInfo = ((VirtualMachineSnapshotInfo)(snapary[0]).getVal());
}
}
return snapInfo;
} private ManagedObjectReference traverseSnapshotInTree(
VirtualMachineSnapshotTree[] snapTree,
String findName) {
ManagedObjectReference snapmor = null;
if (snapTree == null) {
return snapmor;
}
for (int i = 0; i < snapTree.length && snapmor == null; i++) {
VirtualMachineSnapshotTree node = snapTree[i]; if (findName != null && node.getName().equals(findName)) {
snapmor = node.getSnapshot();
} else {
VirtualMachineSnapshotTree[] childTree = node.getChildSnapshotList();
snapmor = traverseSnapshotInTree(childTree, findName);
}
}
return snapmor;
} private ManagedObjectReference getMOR(String name,
String type,
ManagedObjectReference root)
throws Exception{
ManagedObjectReference nameMOR
= (ManagedObjectReference)
ecb.getServiceUtil3().getDecendentMoRef(root,type, name); if(nameMOR ==null) {
System.out.println("Error:: "+ name+ " not found");
return null;
}
else {
return nameMOR;
}
} public static OptionSpec[] constructOptions() {
OptionSpec [] useroptions = new OptionSpec[3];
useroptions[0] = new OptionSpec("vmname","String",1,
"Name of the virtual machine"
,null);
useroptions[1] = new OptionSpec("snapshotname","String",1,
"Name of the snapshot",
null);
useroptions[2] = new OptionSpec("clonename","String",1,
"Name of the linked clone",
null);
return useroptions;
}
public void startclone() throws Exception {
String arg[]={"--url"," https://211.64.94.132/sdk",
"--username" ,"cuixj",
"--password", "xp",
"--vmname",a,
"--snapshotname", b,
"--clonename",c,
"--ignorecert"};
VMLinkedClone1 app = new VMLinkedClone1("dde");
cb = AppUtil.initialize("VMLinkedClone",
VMLinkedClone1.constructOptions(),arg);
cb.connect();
String cookieString = VersionUtil.getCookieString(cb);
app.createLinkedClone(arg,cookieString);
cb.disConnect();
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == jb1) {
for (int i = 0; i < (jt.length-1); i++) {
if (jt[i].getText().length() == 0) {
JOptionPane.showMessageDialog(this, "信息填写不完整");
return;
}
}
a = jt[0].getText();
b = jt[1].getText();
c = jt[2].getText();
try
{
startclone();
}
catch(Exception ex)
{
}
}
else
{
dispose();
}
} /* public static void main(String[] args) throws Exception {
VMLinkedClone app = new VMLinkedClone();
cb = AppUtil.initialize("VMLinkedClone",
VMLinkedClone.constructOptions(),args);
cb.connect();
String cookieString = VersionUtil.getCookieString(cb);
app.createLinkedClone(args,cookieString);
cb.disConnect();
}*/
// public void V123(){
public static void main(String args[])throws Exception{
try {
new VMLinkedClone1("dgh");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/vmware/hyp/lifecycle/CreateVM
at Student.virtualmain1.actionPerformed(virtualmain1.java:104)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java
将程序中最后的main该为一个方法 public void V123(){
在另一个程序中直接调用此方法try {
new VMLinkedClone1 ("sdf");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
什么地方不妥?望各位指教