Search in sources :

Example 51 with VirtualDevice

use of com.vmware.vim25.VirtualDevice in project cloudstack by apache.

the class VirtualMachineMO method cloneFromDiskChain.

public VirtualMachineMO cloneFromDiskChain(String clonedVmName, int cpuSpeedMHz, int memoryMb, String[] disks, ManagedObjectReference morDs) throws Exception {
    assert (disks != null);
    assert (disks.length >= 1);
    HostMO hostMo = getRunningHost();
    VirtualMachineMO clonedVmMo = HypervisorHostHelper.createWorkerVM(hostMo, new DatastoreMO(hostMo.getContext(), morDs), clonedVmName);
    if (clonedVmMo == null)
        throw new Exception("Unable to find just-created blank VM");
    boolean bSuccess = false;
    try {
        VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
        VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
        VirtualDevice device = VmwareHelper.prepareDiskDevice(clonedVmMo, null, -1, disks, morDs, -1, 1);
        deviceConfigSpec.setDevice(device);
        deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
        vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
        clonedVmMo.configureVm(vmConfigSpec);
        bSuccess = true;
        return clonedVmMo;
    } finally {
        if (!bSuccess) {
            clonedVmMo.detachAllDisks();
            clonedVmMo.destroy();
        }
    }
}
Also used : VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualDevice(com.vmware.vim25.VirtualDevice)

Example 52 with VirtualDevice

use of com.vmware.vim25.VirtualDevice in project cloudstack by apache.

the class VirtualMachineMO method getDiskDevice.

// return pair of VirtualDisk and disk device bus name(ide0:0, etc)
public Pair<VirtualDisk, String> getDiskDevice(String vmdkDatastorePath) throws Exception {
    List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");
    ArrayList<Pair<VirtualDisk, String>> partialMatchingDiskDevices = new ArrayList<Pair<VirtualDisk, String>>();
    DatastoreFile dsSrcFile = new DatastoreFile(vmdkDatastorePath);
    String srcBaseName = dsSrcFile.getFileBaseName();
    String trimmedSrcBaseName = VmwareHelper.trimSnapshotDeltaPostfix(srcBaseName);
    s_logger.info("Look for disk device info for volume : " + vmdkDatastorePath + " with base name: " + srcBaseName);
    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;
                    do {
                        s_logger.info("Test against disk backing : " + diskBackingInfo.getFileName());
                        DatastoreFile dsBackingFile = new DatastoreFile(diskBackingInfo.getFileName());
                        String backingBaseName = dsBackingFile.getFileBaseName();
                        if (backingBaseName.equalsIgnoreCase(srcBaseName)) {
                            String deviceNumbering = getDeviceBusName(devices, device);
                            s_logger.info("Disk backing : " + diskBackingInfo.getFileName() + " matches ==> " + deviceNumbering);
                            return new Pair<VirtualDisk, String>((VirtualDisk) device, deviceNumbering);
                        }
                        if (backingBaseName.contains(trimmedSrcBaseName)) {
                            String deviceNumbering = getDeviceBusName(devices, device);
                            partialMatchingDiskDevices.add(new Pair<VirtualDisk, String>((VirtualDisk) device, deviceNumbering));
                        }
                        diskBackingInfo = diskBackingInfo.getParent();
                    } while (diskBackingInfo != null);
                }
            }
        }
    }
    // No disk device was found with an exact match for the volume path, hence look for disk device that matches the trimmed name.
    s_logger.info("No disk device with an exact match found for volume : " + vmdkDatastorePath + ". Look for disk device info against trimmed base name: " + srcBaseName);
    if (partialMatchingDiskDevices != null) {
        if (partialMatchingDiskDevices.size() == 1) {
            VirtualDiskFlatVer2BackingInfo matchingDiskBackingInfo = (VirtualDiskFlatVer2BackingInfo) partialMatchingDiskDevices.get(0).first().getBacking();
            s_logger.info("Disk backing : " + matchingDiskBackingInfo.getFileName() + " matches ==> " + partialMatchingDiskDevices.get(0).second());
            return partialMatchingDiskDevices.get(0);
        } else if (partialMatchingDiskDevices.size() > 1) {
            s_logger.warn("Disk device info lookup for volume: " + vmdkDatastorePath + " failed as multiple disk devices were found to match" + " volume's trimmed base name: " + trimmedSrcBaseName);
            return null;
        }
    }
    s_logger.warn("Disk device info lookup for volume: " + vmdkDatastorePath + " failed as no matching disk device found");
    return null;
}
Also used : VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualDevice(com.vmware.vim25.VirtualDevice) ArrayList(java.util.ArrayList) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) VirtualDisk(com.vmware.vim25.VirtualDisk) Pair(com.cloud.utils.Pair)

Example 53 with VirtualDevice

use of com.vmware.vim25.VirtualDevice in project cloudstack by apache.

the class VirtualMachineMO method getNextDeviceNumber.

public int getNextDeviceNumber(int controllerKey) throws Exception {
    List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");
    List<Integer> existingUnitNumbers = new ArrayList<Integer>();
    int deviceNumber = 0;
    int scsiControllerKey = getGenericScsiDeviceControllerKeyNoException();
    if (devices != null && devices.size() > 0) {
        for (VirtualDevice device : devices) {
            if (device.getControllerKey() != null && device.getControllerKey().intValue() == controllerKey) {
                existingUnitNumbers.add(device.getUnitNumber());
            }
        }
    }
    while (true) {
        // Next device number should be the lowest device number on the key that is not in use and is not reserved.
        if (!existingUnitNumbers.contains(Integer.valueOf(deviceNumber))) {
            if (controllerKey != scsiControllerKey || !VmwareHelper.isReservedScsiDeviceNumber(deviceNumber))
                break;
        }
        ++deviceNumber;
    }
    return deviceNumber;
}
Also used : VirtualDevice(com.vmware.vim25.VirtualDevice) ArrayList(java.util.ArrayList)

Example 54 with VirtualDevice

use of com.vmware.vim25.VirtualDevice in project cloudstack by apache.

the class VirtualMachineMO method tearDownDevice.

public void tearDownDevice(VirtualDevice device) throws Exception {
    VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
    VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
    deviceConfigSpec.setDevice(device);
    deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
    vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
    if (!configureVm(vmConfigSpec)) {
        throw new Exception("Failed to detach devices");
    }
}
Also used : VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec)

Example 55 with VirtualDevice

use of com.vmware.vim25.VirtualDevice in project cloudstack by apache.

the class VirtualMachineMO method moveAllVmDiskFiles.

// this method relies on un-offical VMware API
@Deprecated
public void moveAllVmDiskFiles(DatastoreMO destDsMo, String destDsDir, boolean followDiskChain) throws Exception {
    VirtualDevice[] disks = getAllDiskDevice();
    DatacenterMO dcMo = getOwnerDatacenter().first();
    if (disks != null) {
        for (VirtualDevice disk : disks) {
            List<Pair<String, ManagedObjectReference>> vmdkFiles = getDiskDatastorePathChain((VirtualDisk) disk, followDiskChain);
            for (Pair<String, ManagedObjectReference> fileItem : vmdkFiles) {
                DatastoreMO srcDsMo = new DatastoreMO(_context, fileItem.second());
                DatastoreFile srcFile = new DatastoreFile(fileItem.first());
                DatastoreFile destFile = new DatastoreFile(destDsMo.getName(), destDsDir, srcFile.getFileName());
                Pair<VmdkFileDescriptor, byte[]> vmdkDescriptor = null;
                vmdkDescriptor = getVmdkFileInfo(fileItem.first());
                s_logger.info("Move VM disk file " + srcFile.getPath() + " to " + destFile.getPath());
                srcDsMo.moveDatastoreFile(fileItem.first(), dcMo.getMor(), destDsMo.getMor(), destFile.getPath(), dcMo.getMor(), true);
                if (vmdkDescriptor != null) {
                    String vmdkBaseFileName = vmdkDescriptor.first().getBaseFileName();
                    String baseFilePath = srcFile.getCompanionPath(vmdkBaseFileName);
                    destFile = new DatastoreFile(destDsMo.getName(), destDsDir, vmdkBaseFileName);
                    s_logger.info("Move VM disk file " + baseFilePath + " to " + destFile.getPath());
                    srcDsMo.moveDatastoreFile(baseFilePath, dcMo.getMor(), destDsMo.getMor(), destFile.getPath(), dcMo.getMor(), true);
                }
            }
        }
    }
}
Also used : VirtualDevice(com.vmware.vim25.VirtualDevice) Pair(com.cloud.utils.Pair) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

VirtualDevice (com.vmware.vim25.VirtualDevice)49 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)23 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)23 VirtualDisk (com.vmware.vim25.VirtualDisk)22 ArrayList (java.util.ArrayList)20 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)19 Pair (com.cloud.utils.Pair)15 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)15 VirtualDeviceConnectInfo (com.vmware.vim25.VirtualDeviceConnectInfo)15 VirtualDiskFlatVer2BackingInfo (com.vmware.vim25.VirtualDiskFlatVer2BackingInfo)15 VirtualEthernetCard (com.vmware.vim25.VirtualEthernetCard)11 VirtualDeviceBackingInfo (com.vmware.vim25.VirtualDeviceBackingInfo)8 IOException (java.io.IOException)8 CloudException (com.cloud.exception.CloudException)6 InternalErrorException (com.cloud.exception.InternalErrorException)6 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 ConnectException (java.net.ConnectException)6 RemoteException (java.rmi.RemoteException)6 ConfigurationException (javax.naming.ConfigurationException)6