Search in sources :

Example 6 with VirtualDiskFlatVer2BackingInfo

use of com.vmware.vim25.VirtualDiskFlatVer2BackingInfo in project CloudStack-archive by CloudStack-extras.

the class VmwareHelper method prepareDiskDevice.

// vmdkDatastorePath: [datastore name] vmdkFilePath
public static VirtualDevice prepareDiskDevice(VirtualMachineMO vmMo, int controllerKey, String[] vmdkDatastorePathChain, ManagedObjectReference morDs, int deviceNumber, int contextNumber) throws Exception {
    assert (vmdkDatastorePathChain != null);
    assert (vmdkDatastorePathChain.length >= 1);
    VirtualDisk disk = new VirtualDisk();
    VirtualDiskFlatVer2BackingInfo backingInfo = new VirtualDiskFlatVer2BackingInfo();
    backingInfo.setDatastore(morDs);
    backingInfo.setFileName(vmdkDatastorePathChain[0]);
    backingInfo.setDiskMode(VirtualDiskMode.persistent.toString());
    if (vmdkDatastorePathChain.length > 1) {
        String[] parentDisks = new String[vmdkDatastorePathChain.length - 1];
        for (int i = 0; i < vmdkDatastorePathChain.length - 1; i++) parentDisks[i] = vmdkDatastorePathChain[i + 1];
        setParentBackingInfo(backingInfo, morDs, parentDisks);
    }
    disk.setBacking(backingInfo);
    if (controllerKey < 0)
        controllerKey = vmMo.getIDEDeviceControllerKey();
    if (deviceNumber < 0)
        deviceNumber = vmMo.getNextDeviceNumber(controllerKey);
    disk.setControllerKey(controllerKey);
    disk.setKey(-contextNumber);
    disk.setUnitNumber(deviceNumber);
    VirtualDeviceConnectInfo connectInfo = new VirtualDeviceConnectInfo();
    connectInfo.setConnected(true);
    connectInfo.setStartConnected(true);
    disk.setConnectable(connectInfo);
    return disk;
}
Also used : VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualDeviceConnectInfo(com.vmware.vim25.VirtualDeviceConnectInfo) VirtualDisk(com.vmware.vim25.VirtualDisk)

Example 7 with VirtualDiskFlatVer2BackingInfo

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

the class VmwareHelper method prepareDiskDevice.

// vmdkDatastorePath: [datastore name] vmdkFilePath
public static VirtualDevice prepareDiskDevice(VirtualMachineMO vmMo, VirtualDisk device, int controllerKey, String[] vmdkDatastorePathChain, ManagedObjectReference morDs, int deviceNumber, int contextNumber) throws Exception {
    assert (vmdkDatastorePathChain != null);
    assert (vmdkDatastorePathChain.length >= 1);
    VirtualDisk disk;
    VirtualDiskFlatVer2BackingInfo backingInfo;
    if (device != null) {
        disk = device;
        backingInfo = (VirtualDiskFlatVer2BackingInfo) disk.getBacking();
    } else {
        disk = new VirtualDisk();
        backingInfo = new VirtualDiskFlatVer2BackingInfo();
        backingInfo.setDatastore(morDs);
        backingInfo.setDiskMode(VirtualDiskMode.PERSISTENT.value());
        disk.setBacking(backingInfo);
        int ideControllerKey = vmMo.getIDEDeviceControllerKey();
        if (controllerKey < 0)
            controllerKey = ideControllerKey;
        if (deviceNumber < 0) {
            deviceNumber = vmMo.getNextDeviceNumber(controllerKey);
        }
        disk.setControllerKey(controllerKey);
        disk.setKey(-contextNumber);
        disk.setUnitNumber(deviceNumber);
        VirtualDeviceConnectInfo connectInfo = new VirtualDeviceConnectInfo();
        connectInfo.setConnected(true);
        connectInfo.setStartConnected(true);
        disk.setConnectable(connectInfo);
    }
    backingInfo.setFileName(vmdkDatastorePathChain[0]);
    if (vmdkDatastorePathChain.length > 1) {
        String[] parentDisks = new String[vmdkDatastorePathChain.length - 1];
        for (int i = 0; i < vmdkDatastorePathChain.length - 1; i++) parentDisks[i] = vmdkDatastorePathChain[i + 1];
        setParentBackingInfo(backingInfo, morDs, parentDisks);
    }
    return disk;
}
Also used : VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualDeviceConnectInfo(com.vmware.vim25.VirtualDeviceConnectInfo) VirtualDisk(com.vmware.vim25.VirtualDisk)

Example 8 with VirtualDiskFlatVer2BackingInfo

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

the class VmwareHelper method prepareDiskDevice.

// vmdkDatastorePath: [datastore name] vmdkFilePath
public static VirtualDevice prepareDiskDevice(VirtualMachineMO vmMo, int controllerKey, String vmdkDatastorePath, int sizeInMb, ManagedObjectReference morDs, int deviceNumber, int contextNumber) throws Exception {
    VirtualDisk disk = new VirtualDisk();
    VirtualDiskFlatVer2BackingInfo backingInfo = new VirtualDiskFlatVer2BackingInfo();
    backingInfo.setDiskMode(VirtualDiskMode.PERSISTENT.value());
    backingInfo.setThinProvisioned(true);
    backingInfo.setEagerlyScrub(false);
    backingInfo.setDatastore(morDs);
    backingInfo.setFileName(vmdkDatastorePath);
    disk.setBacking(backingInfo);
    int ideControllerKey = vmMo.getIDEDeviceControllerKey();
    if (controllerKey < 0)
        controllerKey = ideControllerKey;
    if (deviceNumber < 0) {
        deviceNumber = vmMo.getNextDeviceNumber(controllerKey);
    }
    disk.setControllerKey(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;
}
Also used : VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualDeviceConnectInfo(com.vmware.vim25.VirtualDeviceConnectInfo) VirtualDisk(com.vmware.vim25.VirtualDisk)

Example 9 with VirtualDiskFlatVer2BackingInfo

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

the class VmwareStorageProcessor method getManagedIqnsFromVirtualDisks.

private List<String> getManagedIqnsFromVirtualDisks(List<VirtualDisk> virtualDisks) {
    List<String> managedIqns = new ArrayList<String>();
    if (virtualDisks != null) {
        for (VirtualDisk virtualDisk : virtualDisks) {
            if (virtualDisk.getBacking() instanceof VirtualDiskFlatVer2BackingInfo) {
                VirtualDiskFlatVer2BackingInfo backingInfo = (VirtualDiskFlatVer2BackingInfo) virtualDisk.getBacking();
                String path = backingInfo.getFileName();
                String search = "[-";
                int index = path.indexOf(search);
                if (index > -1) {
                    path = path.substring(index + search.length());
                    String search2 = "-0]";
                    index = path.lastIndexOf(search2);
                    if (index > -1) {
                        path = path.substring(0, index);
                        if (path.startsWith("iqn.")) {
                            managedIqns.add(path);
                        }
                    }
                }
            }
        }
    }
    return managedIqns;
}
Also used : VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) ArrayList(java.util.ArrayList) VirtualDisk(com.vmware.vim25.VirtualDisk)

Example 10 with VirtualDiskFlatVer2BackingInfo

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

the class VmwareResource method execute.

private Answer execute(ResizeVolumeCommand cmd) {
    String path = cmd.getPath();
    String vmName = cmd.getInstanceName();
    long newSize = cmd.getNewSize() / 1024;
    long oldSize = cmd.getCurrentSize() / 1024;
    boolean useWorkerVm = false;
    VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
    String poolId = cmd.getPoolUuid();
    VirtualMachineMO vmMo = null;
    DatastoreMO dsMo = null;
    ManagedObjectReference morDS = null;
    String vmdkDataStorePath = null;
    try {
        if (newSize < oldSize) {
            throw new Exception("VMware doesn't support shrinking volume from larger size: " + oldSize / (1024 * 1024) + " GB to a smaller size: " + newSize / (1024 * 1024) + " GB");
        } else if (newSize == oldSize) {
            return new ResizeVolumeAnswer(cmd, true, "success", newSize * 1024);
        }
        if (vmName.equalsIgnoreCase("none")) {
            // we need to spawn a worker VM to attach the volume to and
            // resize the volume.
            useWorkerVm = true;
            vmName = getWorkerName(getServiceContext(), cmd, 0);
            morDS = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, poolId);
            dsMo = new DatastoreMO(hyperHost.getContext(), morDS);
            s_logger.info("Create worker VM " + vmName);
            vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, vmName);
            if (vmMo == null) {
                throw new Exception("Unable to create a worker VM for volume resize");
            }
            synchronized (this) {
                vmdkDataStorePath = VmwareStorageLayoutHelper.getLegacyDatastorePathFromVmdkFileName(dsMo, path + ".vmdk");
                vmMo.attachDisk(new String[] { vmdkDataStorePath }, morDS);
            }
        }
        // find VM through datacenter (VM is not at the target host yet)
        vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
        if (vmMo == null) {
            String msg = "VM " + vmName + " does not exist in VMware datacenter";
            s_logger.error(msg);
            throw new Exception(msg);
        }
        Pair<VirtualDisk, String> vdisk = vmMo.getDiskDevice(path);
        if (vdisk == null) {
            if (s_logger.isTraceEnabled())
                s_logger.trace("resize volume done (failed)");
            throw new Exception("No such disk device: " + path);
        }
        // IDE virtual disk cannot be re-sized if VM is running
        if (vdisk.second() != null && vdisk.second().contains("ide")) {
            throw new Exception("Re-sizing a virtual disk over IDE controller is not supported in VMware hypervisor. " + "Please re-try when virtual disk is attached to a VM using SCSI controller.");
        }
        if (vdisk.second() != null && !vdisk.second().toLowerCase().startsWith("scsi")) {
            s_logger.error("Unsupported disk device bus " + vdisk.second());
            throw new Exception("Unsupported disk device bus " + vdisk.second());
        }
        VirtualDisk disk = vdisk.first();
        if ((VirtualDiskFlatVer2BackingInfo) disk.getBacking() != null && ((VirtualDiskFlatVer2BackingInfo) disk.getBacking()).getParent() != null) {
            s_logger.error("Resize is not supported because Disk device has Parent " + ((VirtualDiskFlatVer2BackingInfo) disk.getBacking()).getParent().getUuid());
            throw new Exception("Resize is not supported because Disk device has Parent " + ((VirtualDiskFlatVer2BackingInfo) disk.getBacking()).getParent().getUuid());
        }
        String vmdkAbsFile = getAbsoluteVmdkFile(disk);
        if (vmdkAbsFile != null && !vmdkAbsFile.isEmpty()) {
            vmMo.updateAdapterTypeIfRequired(vmdkAbsFile);
        }
        disk.setCapacityInKB(newSize);
        VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
        VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
        deviceConfigSpec.setDevice(disk);
        deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.EDIT);
        vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
        if (!vmMo.configureVm(vmConfigSpec)) {
            throw new Exception("Failed to configure VM to resize disk. vmName: " + vmName);
        }
        return new ResizeVolumeAnswer(cmd, true, "success", newSize * 1024);
    } catch (Exception e) {
        s_logger.error("Unable to resize volume", e);
        String error = "Failed to resize volume: " + e.getMessage();
        return new ResizeVolumeAnswer(cmd, false, error);
    } finally {
        try {
            if (useWorkerVm == true) {
                s_logger.info("Destroy worker VM after volume resize");
                vmMo.detachDisk(vmdkDataStorePath, false);
                vmMo.destroy();
            }
        } catch (Throwable e) {
            s_logger.info("Failed to destroy worker VM: " + vmName);
        }
    }
}
Also used : VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) ResizeVolumeAnswer(com.cloud.agent.api.storage.ResizeVolumeAnswer) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException) VirtualDisk(com.vmware.vim25.VirtualDisk) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

VirtualDiskFlatVer2BackingInfo (com.vmware.vim25.VirtualDiskFlatVer2BackingInfo)29 VirtualDisk (com.vmware.vim25.VirtualDisk)20 VirtualDeviceBackingInfo (com.vmware.vim25.VirtualDeviceBackingInfo)11 Pair (com.cloud.utils.Pair)9 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)8 VirtualDeviceConnectInfo (com.vmware.vim25.VirtualDeviceConnectInfo)8 ArrayList (java.util.ArrayList)8 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)7 VirtualDevice (com.vmware.vim25.VirtualDevice)7 VirtualDiskRawDiskMappingVer1BackingInfo (com.vmware.vim25.VirtualDiskRawDiskMappingVer1BackingInfo)6 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)5 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)5 VirtualDiskFlatVer1BackingInfo (com.vmware.vim25.VirtualDiskFlatVer1BackingInfo)4 VirtualDiskSparseVer1BackingInfo (com.vmware.vim25.VirtualDiskSparseVer1BackingInfo)4 VirtualDiskSparseVer2BackingInfo (com.vmware.vim25.VirtualDiskSparseVer2BackingInfo)4 IOException (java.io.IOException)3 VirtualMachineFileInfo (com.vmware.vim25.VirtualMachineFileInfo)2 ResizeVolumeAnswer (com.cloud.agent.api.storage.ResizeVolumeAnswer)1 CloudException (com.cloud.exception.CloudException)1 InternalErrorException (com.cloud.exception.InternalErrorException)1