use of com.vmware.vim25.VirtualDeviceBackingInfo in project CloudStack-archive by CloudStack-extras.
the class VirtualMachineMO method getDiskDatastorePathChain.
@Deprecated
public List<Pair<String, ManagedObjectReference>> getDiskDatastorePathChain(VirtualDisk disk, boolean followChain) throws Exception {
VirtualDeviceBackingInfo backingInfo = disk.getBacking();
if (!(backingInfo instanceof VirtualDiskFlatVer2BackingInfo)) {
throw new Exception("Unsupported VirtualDeviceBackingInfo");
}
List<Pair<String, ManagedObjectReference>> pathList = new ArrayList<Pair<String, ManagedObjectReference>>();
VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo) backingInfo;
if (!followChain) {
pathList.add(new Pair<String, ManagedObjectReference>(diskBackingInfo.getFileName(), diskBackingInfo.getDatastore()));
return pathList;
}
Pair<DatacenterMO, String> dcPair = getOwnerDatacenter();
VirtualMachineFileInfo vmFilesInfo = getFileInfo();
DatastoreFile snapshotDirFile = new DatastoreFile(vmFilesInfo.getSnapshotDirectory());
DatastoreFile vmxDirFile = new DatastoreFile(vmFilesInfo.getVmPathName());
do {
if (diskBackingInfo.getParent() != null) {
pathList.add(new Pair<String, ManagedObjectReference>(diskBackingInfo.getFileName(), diskBackingInfo.getDatastore()));
diskBackingInfo = diskBackingInfo.getParent();
} else {
// try getting parent info from VMDK file itself
byte[] content = null;
try {
String url = getContext().composeDatastoreBrowseUrl(dcPair.second(), diskBackingInfo.getFileName());
content = getContext().getResourceContent(url);
if (content == null || content.length == 0) {
break;
}
pathList.add(new Pair<String, ManagedObjectReference>(diskBackingInfo.getFileName(), diskBackingInfo.getDatastore()));
} catch (Exception e) {
// if snapshot directory has been changed to place other than default. VMware has a bug
// that its corresponding disk backing info is not updated correctly. therefore, we will try search
// in snapshot directory one more time
DatastoreFile currentFile = new DatastoreFile(diskBackingInfo.getFileName());
String vmdkFullDsPath = snapshotDirFile.getCompanionPath(currentFile.getFileName());
String url = getContext().composeDatastoreBrowseUrl(dcPair.second(), vmdkFullDsPath);
content = getContext().getResourceContent(url);
if (content == null || content.length == 0) {
break;
}
pathList.add(new Pair<String, ManagedObjectReference>(vmdkFullDsPath, diskBackingInfo.getDatastore()));
}
VmdkFileDescriptor descriptor = new VmdkFileDescriptor();
descriptor.parse(content);
if (descriptor.getParentFileName() != null && !descriptor.getParentFileName().isEmpty()) {
// create a fake one
VirtualDiskFlatVer2BackingInfo parentDiskBackingInfo = new VirtualDiskFlatVer2BackingInfo();
parentDiskBackingInfo.setDatastore(diskBackingInfo.getDatastore());
String parentFileName = descriptor.getParentFileName();
if (parentFileName.startsWith("/")) {
int fileNameStartPos = parentFileName.lastIndexOf("/");
parentFileName = parentFileName.substring(fileNameStartPos + 1);
parentDiskBackingInfo.setFileName(vmxDirFile.getCompanionPath(parentFileName));
} else {
parentDiskBackingInfo.setFileName(snapshotDirFile.getCompanionPath(parentFileName));
}
diskBackingInfo = parentDiskBackingInfo;
} else {
break;
}
}
} while (diskBackingInfo != null);
return pathList;
}
use of com.vmware.vim25.VirtualDeviceBackingInfo in project CloudStack-archive by CloudStack-extras.
the class VmwareHelper method prepareDiskDevice.
// vmdkDatastorePath: [datastore name] vmdkFilePath, create delta disk based on disk from template
public static VirtualDevice prepareDiskDevice(VirtualMachineMO vmMo, int controllerKey, String vmdkDatastorePath, int sizeInMb, ManagedObjectReference morDs, VirtualDisk templateDisk, int deviceNumber, int contextNumber) throws Exception {
assert (templateDisk != null);
VirtualDeviceBackingInfo parentBacking = templateDisk.getBacking();
assert (parentBacking != null);
// TODO Not sure if we need to check if the disk in template and the new disk needs to share the
// same datastore
VirtualDisk disk = new VirtualDisk();
if (parentBacking instanceof VirtualDiskFlatVer1BackingInfo) {
VirtualDiskFlatVer1BackingInfo backingInfo = new VirtualDiskFlatVer1BackingInfo();
backingInfo.setDiskMode(((VirtualDiskFlatVer1BackingInfo) parentBacking).getDiskMode());
backingInfo.setDatastore(morDs);
backingInfo.setFileName(vmdkDatastorePath);
backingInfo.setParent((VirtualDiskFlatVer1BackingInfo) parentBacking);
disk.setBacking(backingInfo);
} else if (parentBacking instanceof VirtualDiskFlatVer2BackingInfo) {
VirtualDiskFlatVer2BackingInfo backingInfo = new VirtualDiskFlatVer2BackingInfo();
backingInfo.setDiskMode(((VirtualDiskFlatVer2BackingInfo) parentBacking).getDiskMode());
backingInfo.setDatastore(morDs);
backingInfo.setFileName(vmdkDatastorePath);
backingInfo.setParent((VirtualDiskFlatVer2BackingInfo) parentBacking);
disk.setBacking(backingInfo);
} else if (parentBacking instanceof VirtualDiskRawDiskMappingVer1BackingInfo) {
VirtualDiskRawDiskMappingVer1BackingInfo backingInfo = new VirtualDiskRawDiskMappingVer1BackingInfo();
backingInfo.setDiskMode(((VirtualDiskRawDiskMappingVer1BackingInfo) parentBacking).getDiskMode());
backingInfo.setDatastore(morDs);
backingInfo.setFileName(vmdkDatastorePath);
backingInfo.setParent((VirtualDiskRawDiskMappingVer1BackingInfo) parentBacking);
disk.setBacking(backingInfo);
} else if (parentBacking instanceof VirtualDiskSparseVer1BackingInfo) {
VirtualDiskSparseVer1BackingInfo backingInfo = new VirtualDiskSparseVer1BackingInfo();
backingInfo.setDiskMode(((VirtualDiskSparseVer1BackingInfo) parentBacking).getDiskMode());
backingInfo.setDatastore(morDs);
backingInfo.setFileName(vmdkDatastorePath);
backingInfo.setParent((VirtualDiskSparseVer1BackingInfo) parentBacking);
disk.setBacking(backingInfo);
} else if (parentBacking instanceof VirtualDiskSparseVer2BackingInfo) {
VirtualDiskSparseVer2BackingInfo backingInfo = new VirtualDiskSparseVer2BackingInfo();
backingInfo.setDiskMode(((VirtualDiskSparseVer2BackingInfo) parentBacking).getDiskMode());
backingInfo.setDatastore(morDs);
backingInfo.setFileName(vmdkDatastorePath);
backingInfo.setParent((VirtualDiskSparseVer2BackingInfo) parentBacking);
disk.setBacking(backingInfo);
} else {
throw new Exception("Unsupported disk backing: " + parentBacking.getClass().getCanonicalName());
}
if (controllerKey < 0)
controllerKey = vmMo.getIDEDeviceControllerKey();
disk.setControllerKey(controllerKey);
if (deviceNumber < 0)
deviceNumber = vmMo.getNextDeviceNumber(controllerKey);
disk.setKey(-contextNumber);
disk.setUnitNumber(deviceNumber);
disk.setCapacityInKB(sizeInMb * 1024);
VirtualDeviceConnectInfo connectInfo = new VirtualDeviceConnectInfo();
connectInfo.setConnected(true);
connectInfo.setStartConnected(true);
disk.setConnectable(connectInfo);
return disk;
}
use of com.vmware.vim25.VirtualDeviceBackingInfo in project cloudstack by apache.
the class VmwareResource method postNvpConfigBeforeStart.
private static void postNvpConfigBeforeStart(VirtualMachineMO vmMo, VirtualMachineTO vmSpec) throws Exception {
/**
* We need to configure the port on the DV switch after the host is
* connected. So make this happen between the configure and start of
* the VM
*/
int nicIndex = 0;
for (NicTO nicTo : sortNicsByDeviceId(vmSpec.getNics())) {
if (nicTo.getBroadcastType() == BroadcastDomainType.Lswitch) {
// We need to create a port with a unique vlan and pass the key to the nic device
s_logger.trace("Nic " + nicTo.toString() + " is connected to an NVP logicalswitch");
VirtualDevice nicVirtualDevice = vmMo.getNicDeviceByIndex(nicIndex);
if (nicVirtualDevice == null) {
//FIXME Generic exceptions are bad
throw new Exception("Failed to find a VirtualDevice for nic " + nicIndex);
}
VirtualDeviceBackingInfo backing = nicVirtualDevice.getBacking();
if (backing instanceof VirtualEthernetCardDistributedVirtualPortBackingInfo) {
// This NIC is connected to a Distributed Virtual Switch
VirtualEthernetCardDistributedVirtualPortBackingInfo portInfo = (VirtualEthernetCardDistributedVirtualPortBackingInfo) backing;
DistributedVirtualSwitchPortConnection port = portInfo.getPort();
String portKey = port.getPortKey();
String portGroupKey = port.getPortgroupKey();
String dvSwitchUuid = port.getSwitchUuid();
s_logger.debug("NIC " + nicTo.toString() + " is connected to dvSwitch " + dvSwitchUuid + " pg " + portGroupKey + " port " + portKey);
ManagedObjectReference dvSwitchManager = vmMo.getContext().getVimClient().getServiceContent().getDvSwitchManager();
ManagedObjectReference dvSwitch = vmMo.getContext().getVimClient().getService().queryDvsByUuid(dvSwitchManager, dvSwitchUuid);
// Get all ports
DistributedVirtualSwitchPortCriteria criteria = new DistributedVirtualSwitchPortCriteria();
criteria.setInside(true);
criteria.getPortgroupKey().add(portGroupKey);
List<DistributedVirtualPort> dvPorts = vmMo.getContext().getVimClient().getService().fetchDVPorts(dvSwitch, criteria);
DistributedVirtualPort vmDvPort = null;
List<Integer> usedVlans = new ArrayList<Integer>();
for (DistributedVirtualPort dvPort : dvPorts) {
// Find the port for this NIC by portkey
if (portKey.equals(dvPort.getKey())) {
vmDvPort = dvPort;
}
VMwareDVSPortSetting settings = (VMwareDVSPortSetting) dvPort.getConfig().getSetting();
VmwareDistributedVirtualSwitchVlanIdSpec vlanId = (VmwareDistributedVirtualSwitchVlanIdSpec) settings.getVlan();
s_logger.trace("Found port " + dvPort.getKey() + " with vlan " + vlanId.getVlanId());
if (vlanId.getVlanId() > 0 && vlanId.getVlanId() < 4095) {
usedVlans.add(vlanId.getVlanId());
}
}
if (vmDvPort == null) {
throw new Exception("Empty port list from dvSwitch for nic " + nicTo.toString());
}
DVPortConfigInfo dvPortConfigInfo = vmDvPort.getConfig();
VMwareDVSPortSetting settings = (VMwareDVSPortSetting) dvPortConfigInfo.getSetting();
VmwareDistributedVirtualSwitchVlanIdSpec vlanId = (VmwareDistributedVirtualSwitchVlanIdSpec) settings.getVlan();
BoolPolicy blocked = settings.getBlocked();
if (blocked.isValue() == Boolean.TRUE) {
s_logger.trace("Port is blocked, set a vlanid and unblock");
DVPortConfigSpec dvPortConfigSpec = new DVPortConfigSpec();
VMwareDVSPortSetting edittedSettings = new VMwareDVSPortSetting();
// Unblock
blocked.setValue(Boolean.FALSE);
blocked.setInherited(Boolean.FALSE);
edittedSettings.setBlocked(blocked);
// Set vlan
int i;
for (i = 1; i < 4095; i++) {
if (!usedVlans.contains(i))
break;
}
// FIXME should be a determined
vlanId.setVlanId(i);
// based on usage
vlanId.setInherited(false);
edittedSettings.setVlan(vlanId);
dvPortConfigSpec.setSetting(edittedSettings);
dvPortConfigSpec.setOperation("edit");
dvPortConfigSpec.setKey(portKey);
List<DVPortConfigSpec> dvPortConfigSpecs = new ArrayList<DVPortConfigSpec>();
dvPortConfigSpecs.add(dvPortConfigSpec);
ManagedObjectReference task = vmMo.getContext().getVimClient().getService().reconfigureDVPortTask(dvSwitch, dvPortConfigSpecs);
if (!vmMo.getContext().getVimClient().waitForTask(task)) {
throw new Exception("Failed to configure the dvSwitch port for nic " + nicTo.toString());
}
s_logger.debug("NIC " + nicTo.toString() + " connected to vlan " + i);
} else {
s_logger.trace("Port already configured and set to vlan " + vlanId.getVlanId());
}
} else if (backing instanceof VirtualEthernetCardNetworkBackingInfo) {
// This NIC is connected to a Virtual Switch
// Nothing to do
} else if (backing instanceof VirtualEthernetCardOpaqueNetworkBackingInfo) {
//if NSX API VERSION >= 4.2, connect to br-int (nsx.network), do not create portgroup else previous behaviour
//OK, connected to OpaqueNetwork
} else {
s_logger.error("nic device backing is of type " + backing.getClass().getName());
//FIXME Generic exceptions are bad
throw new Exception("Incompatible backing for a VirtualDevice for nic " + nicIndex);
}
}
nicIndex++;
}
}
use of com.vmware.vim25.VirtualDeviceBackingInfo in project cloudstack by apache.
the class VirtualMachineMO method getDiskDatastorePathChain.
@Deprecated
public List<Pair<String, ManagedObjectReference>> getDiskDatastorePathChain(VirtualDisk disk, boolean followChain) throws Exception {
VirtualDeviceBackingInfo backingInfo = disk.getBacking();
if (!(backingInfo instanceof VirtualDiskFlatVer2BackingInfo)) {
throw new Exception("Unsupported VirtualDeviceBackingInfo");
}
List<Pair<String, ManagedObjectReference>> pathList = new ArrayList<Pair<String, ManagedObjectReference>>();
VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo) backingInfo;
if (!followChain) {
pathList.add(new Pair<String, ManagedObjectReference>(diskBackingInfo.getFileName(), diskBackingInfo.getDatastore()));
return pathList;
}
Pair<DatacenterMO, String> dcPair = getOwnerDatacenter();
VirtualMachineFileInfo vmFilesInfo = getFileInfo();
DatastoreFile snapshotDirFile = new DatastoreFile(vmFilesInfo.getSnapshotDirectory());
DatastoreFile vmxDirFile = new DatastoreFile(vmFilesInfo.getVmPathName());
do {
if (diskBackingInfo.getParent() != null) {
pathList.add(new Pair<String, ManagedObjectReference>(diskBackingInfo.getFileName(), diskBackingInfo.getDatastore()));
diskBackingInfo = diskBackingInfo.getParent();
} else {
// try getting parent info from VMDK file itself
byte[] content = null;
try {
String url = getContext().composeDatastoreBrowseUrl(dcPair.second(), diskBackingInfo.getFileName());
content = getContext().getResourceContent(url);
if (content == null || content.length == 0) {
break;
}
pathList.add(new Pair<String, ManagedObjectReference>(diskBackingInfo.getFileName(), diskBackingInfo.getDatastore()));
} catch (Exception e) {
// if snapshot directory has been changed to place other than default. VMware has a bug
// that its corresponding disk backing info is not updated correctly. therefore, we will try search
// in snapshot directory one more time
DatastoreFile currentFile = new DatastoreFile(diskBackingInfo.getFileName());
String vmdkFullDsPath = snapshotDirFile.getCompanionPath(currentFile.getFileName());
String url = getContext().composeDatastoreBrowseUrl(dcPair.second(), vmdkFullDsPath);
content = getContext().getResourceContent(url);
if (content == null || content.length == 0) {
break;
}
pathList.add(new Pair<String, ManagedObjectReference>(vmdkFullDsPath, diskBackingInfo.getDatastore()));
}
VmdkFileDescriptor descriptor = new VmdkFileDescriptor();
descriptor.parse(content);
if (descriptor.getParentFileName() != null && !descriptor.getParentFileName().isEmpty()) {
// create a fake one
VirtualDiskFlatVer2BackingInfo parentDiskBackingInfo = new VirtualDiskFlatVer2BackingInfo();
parentDiskBackingInfo.setDatastore(diskBackingInfo.getDatastore());
String parentFileName = descriptor.getParentFileName();
if (parentFileName.startsWith("/")) {
int fileNameStartPos = parentFileName.lastIndexOf("/");
parentFileName = parentFileName.substring(fileNameStartPos + 1);
parentDiskBackingInfo.setFileName(vmxDirFile.getCompanionPath(parentFileName));
} else {
parentDiskBackingInfo.setFileName(snapshotDirFile.getCompanionPath(parentFileName));
}
diskBackingInfo = parentDiskBackingInfo;
} else {
break;
}
}
} while (diskBackingInfo != null);
return pathList;
}
use of com.vmware.vim25.VirtualDeviceBackingInfo in project cloudstack by apache.
the class VirtualMachineMO method getDiskCurrentTopBackingFileInChain.
public String getDiskCurrentTopBackingFileInChain(String deviceBusName) throws Exception {
List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");
if (devices != null && devices.size() > 0) {
for (VirtualDevice device : devices) {
if (device instanceof VirtualDisk) {
s_logger.info("Test against disk device, controller key: " + device.getControllerKey() + ", unit number: " + device.getUnitNumber());
VirtualDeviceBackingInfo backingInfo = ((VirtualDisk) device).getBacking();
if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) {
VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo) backingInfo;
String deviceNumbering = getDeviceBusName(devices, device);
if (deviceNumbering.equals(deviceBusName))
return diskBackingInfo.getFileName();
}
}
}
}
return null;
}
Aggregations