com.vmware.vim25.VirtualDisk Java Examples

The following examples show how to use com.vmware.vim25.VirtualDisk. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DiskManager.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Reconfigures VMware system disks and data disks.
 */
public void reconfigureDisks(VirtualMachineConfigSpec vmConfigSpec,
        ManagedObjectReference vmwInstance) throws Exception {

    logger.debug("");

    long systemDiskMB = (long) paramHandler.getConfigDiskSpaceMB();
    VirtualMachineConfigInfo configSpec = (VirtualMachineConfigInfo) vmw
            .getServiceUtil().getDynamicProperty(vmwInstance, "config");
    List<VirtualDevice> devices = configSpec.getHardware().getDevice();
    VirtualDisk vdSystemDisk = getVMSystemDisk(devices,
            configSpec.getName());

    configureSystemDisk(vmConfigSpec, systemDiskMB, vdSystemDisk);
    configureDataDisks(vmConfigSpec, devices, vdSystemDisk);
}
 
Example #2
Source File: VM.java    From development with Apache License 2.0 6 votes vote down vote up
private int getDataDiskKey() throws Exception {
    List<VirtualDevice> devices = configSpec.getHardware().getDevice();
    int countDisks = 0;
    int key = -1;
    for (VirtualDevice vdInfo : devices) {
        if (vdInfo instanceof VirtualDisk) {
            countDisks++;
            if (countDisks == 2) {
                key = ((VirtualDisk) vdInfo).getKey();
                break;
            }
        }
    }

    return key;
}
 
Example #3
Source File: VM.java    From development with Apache License 2.0 6 votes vote down vote up
private String getDiskSizeInGB(int disk) throws Exception {
    String size = "";
    List<VirtualDevice> devices = configSpec.getHardware().getDevice();
    int countDisks = 0;
    for (VirtualDevice vdInfo : devices) {
        if (vdInfo instanceof VirtualDisk) {
            countDisks++;
            if (countDisks == disk) {
                long gigabyte = ((VirtualDisk) vdInfo).getCapacityInKB()
                        / 1024 / 1024;
                size = Long.toString(gigabyte);
                break;
            }
        }
    }

    return size;
}
 
Example #4
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
@Override public VirtualMachine importVirtualMachineFromBackup(long zoneId, long domainId, long accountId, long userId, String vmInternalName, Backup backup) throws Exception {
    DatacenterMO dcMo = getDatacenterMO(zoneId);
    VirtualMachineMO vmToImport = dcMo.findVm(vmInternalName);
    if (vmToImport == null) {
        throw new CloudRuntimeException("Error finding VM: " + vmInternalName);
    }
    VirtualMachineConfigSummary configSummary = vmToImport.getConfigSummary();
    VirtualMachineRuntimeInfo runtimeInfo = vmToImport.getRuntimeInfo();
    List<VirtualDisk> virtualDisks = vmToImport.getVirtualDisks();
    String[] vmNetworkNames = vmToImport.getNetworks();
    VirtualDevice[] nicDevices = vmToImport.getNicDevices();

    Map<VirtualDisk, VolumeVO> disksMapping = getDisksMapping(backup, virtualDisks);
    Map<String, NetworkVO> networksMapping = getNetworksMapping(vmNetworkNames, accountId, zoneId, domainId);

    long guestOsId = getImportingVMGuestOs(configSummary);
    long serviceOfferingId = getImportingVMServiceOffering(configSummary, runtimeInfo);
    long templateId = getImportingVMTemplate(virtualDisks, dcMo, vmInternalName, guestOsId, accountId, disksMapping, backup);

    VMInstanceVO vm = getVM(vmInternalName, templateId, guestOsId, serviceOfferingId, zoneId, accountId, userId, domainId);
    syncVMVolumes(vm, virtualDisks, disksMapping, vmToImport, backup);
    syncVMNics(nicDevices, dcMo, networksMapping, vm);

    return vm;
}
 
Example #5
Source File: VMwareUtil.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public static VirtualMachineDiskInfoBuilder getDiskInfoBuilder(List<VirtualDevice> devices) throws Exception {
    VirtualMachineDiskInfoBuilder builder = new VirtualMachineDiskInfoBuilder();

    if (devices != null) {
        for (VirtualDevice device : devices) {
            if (device instanceof VirtualDisk) {
                VirtualDisk virtualDisk = (VirtualDisk)device;
                VirtualDeviceBackingInfo backingInfo = virtualDisk.getBacking();

                if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) {
                    VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)backingInfo;

                    String deviceBusName = VMwareUtil.getDeviceBusName(devices, virtualDisk);

                    while (diskBackingInfo != null) {
                        builder.addDisk(deviceBusName, diskBackingInfo.getFileName());

                        diskBackingInfo = diskBackingInfo.getParent();
                    }
                }
            }
        }
    }

    return builder;
}
 
Example #6
Source File: VmwareProcessClient.java    From primecloud-controller with GNU General Public License v2.0 6 votes vote down vote up
protected VirtualDisk getVirtualDisk(VirtualMachine machine, Integer scsiId) {
    // SCSIコントローラを取得
    VirtualSCSIController scsiController = getSCSIController(machine);

    // SCSIコントローラとSCSI IDが一致するディスクを取得
    VirtualDisk disk = null;
    for (VirtualDevice device : machine.getConfig().getHardware().getDevice()) {
        if (device instanceof VirtualDisk) {
            VirtualDisk tmpDisk = VirtualDisk.class.cast(device);
            if (tmpDisk.getControllerKey() != null && tmpDisk.getControllerKey().equals(scsiController.getKey())) {
                if (tmpDisk.getUnitNumber() != null && tmpDisk.getUnitNumber().equals(scsiId)) {
                    disk = tmpDisk;
                    break;
                }
            }
        }
    }

    if (disk == null) {
        // VirtualDiskが見つからない場合
        throw new AutoException("EPROCESS-000518", scsiId);
    }

    return disk;
}
 
Example #7
Source File: VmUtils.java    From cs-actions with Apache License 2.0 6 votes vote down vote up
private VirtualDisk createVirtualDisk(String volumeName, int controllerKey, int unitNumber, int key, VmInputs vmInputs,
                                      String parameter) throws Exception {
    VirtualDiskFlatVer2BackingInfo diskFileBacking = new VirtualDiskFlatVer2BackingInfo();
    diskFileBacking.setFileName(volumeName);
    if (Operation.CREATE.toString().equalsIgnoreCase(parameter)) {
        diskFileBacking.setDiskMode(DiskMode.PERSISTENT.toString());
    } else {
        diskFileBacking.setDiskMode(DiskMode.getValue(vmInputs.getVmDiskMode()));
    }

    VirtualDisk disk = new VirtualDisk();
    disk.setBacking(diskFileBacking);
    disk.setControllerKey(controllerKey);
    disk.setUnitNumber(unitNumber);
    disk.setKey(key);
    disk.setCapacityInKB(vmInputs.getLongVmDiskSize() * DISK_AMOUNT_MULTIPLIER);

    return disk;
}
 
Example #8
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
private Map<VirtualDisk, VolumeVO> getDisksMapping(Backup backup, List<VirtualDisk> virtualDisks) {
    final VMInstanceVO vm = _vmDao.findByIdIncludingRemoved(backup.getVmId());
    if (vm == null) {
        throw new CloudRuntimeException("Failed to find the volumes details from the VM backup");
    }
    List<Backup.VolumeInfo> backedUpVolumes = vm.getBackupVolumeList();
    Map<String, Boolean> usedVols = new HashMap<>();
    Map<VirtualDisk, VolumeVO> map = new HashMap<>();

    for (Backup.VolumeInfo backedUpVol : backedUpVolumes) {
        for (VirtualDisk disk : virtualDisks) {
            if (!map.containsKey(disk) && backedUpVol.getSize().equals(disk.getCapacityInBytes()) && !usedVols.containsKey(backedUpVol.getUuid())) {
                String volId = backedUpVol.getUuid();
                VolumeVO vol = _volumeDao.findByUuidIncludingRemoved(volId);
                usedVols.put(backedUpVol.getUuid(), true);
                map.put(disk, vol);
                s_logger.debug("VM restore mapping for disk " + disk.getBacking() + " (capacity: " + disk.getCapacityInBytes() + ") with volume ID" + vol.getId());
            }
        }
    }
    return map;
}
 
Example #9
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
/**
 * Get volumes for VM being imported
 */
private void syncVMVolumes(VMInstanceVO vmInstanceVO, List<VirtualDisk> virtualDisks, Map<VirtualDisk, VolumeVO> disksMapping, VirtualMachineMO vmToImport, Backup backup)
        throws Exception {
    long zoneId = vmInstanceVO.getDataCenterId();
    long accountId = vmInstanceVO.getAccountId();
    long domainId = vmInstanceVO.getDomainId();
    long templateId = vmInstanceVO.getTemplateId();
    long instanceId = vmInstanceVO.getId();

    for (VirtualDisk disk : virtualDisks) {
        Long poolId = getPoolId(disk);
        Volume volume = null;
        if (disksMapping.containsKey(disk) && disksMapping.get(disk) != null) {
            volume = updateVolume(disk, disksMapping, vmToImport, poolId, vmInstanceVO);
        } else {
            volume = createVolume(disk, vmToImport, domainId, zoneId, accountId, instanceId, poolId, templateId, backup, true);
        }
        s_logger.debug("VM backup restored (updated/created) volume id:" + volume.getId() + " for VM id:" + instanceId);
    }
}
 
Example #10
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
/**
 * Get template ID for VM being imported. If it is not found, it is created
 */
private Long getImportingVMTemplate(List<VirtualDisk> virtualDisks, DatacenterMO dcMo, String vmInternalName, Long guestOsId, long accountId, Map<VirtualDisk, VolumeVO> disksMapping, Backup backup) throws Exception {
    for (VirtualDisk disk : virtualDisks) {
        if (isRootDisk(disk, disksMapping, backup)) {
            VolumeVO volumeVO = disksMapping.get(disk);
            if (volumeVO == null) {
                String templatePath = getRootDiskTemplatePath(disk);
                VirtualMachineMO template = getTemplate(dcMo, templatePath);
                Long poolId = getTemplatePoolId(template);
                Long templateSize = getTemplateSize(template, vmInternalName, disksMapping, backup);
                long templateId = getTemplateId(templatePath, vmInternalName, guestOsId, accountId);
                updateTemplateRef(templateId, poolId, templatePath, templateSize);
                return templateId;
            } else {
                return volumeVO.getTemplateId();
            }
        }
    }
    throw new CloudRuntimeException("Could not find ROOT disk for VM " + vmInternalName);
}
 
Example #11
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
private VolumeVO createVolume(VirtualDisk disk, VirtualMachineMO vmToImport, long domainId, long zoneId, long accountId, long instanceId, Long poolId, long templateId, Backup backup, boolean isImport) throws Exception {
    VMInstanceVO vm = _vmDao.findByIdIncludingRemoved(backup.getVmId());
    if (vm == null) {
        throw new CloudRuntimeException("Failed to find the backup volume information from the VM backup");
    }
    List<Backup.VolumeInfo> backedUpVolumes = vm.getBackupVolumeList();
    Volume.Type type = Volume.Type.DATADISK;
    Long size = disk.getCapacityInBytes();
    if (isImport) {
        for (Backup.VolumeInfo volumeInfo : backedUpVolumes) {
            if (volumeInfo.getSize().equals(disk.getCapacityInBytes())) {
                type = volumeInfo.getType();
            }
        }
    }
    VirtualDeviceBackingInfo backing = disk.getBacking();
    checkBackingInfo(backing);
    VirtualDiskFlatVer2BackingInfo info = (VirtualDiskFlatVer2BackingInfo)backing;
    String volumeName = getVolumeName(disk, vmToImport);
    Storage.ProvisioningType provisioningType = getProvisioningType(info);
    long diskOfferingId = getDiskOfferingId(size, provisioningType);
    Integer unitNumber = disk.getUnitNumber();
    VirtualMachineDiskInfo diskInfo = getDiskInfo(vmToImport, poolId, volumeName);
    return createVolumeRecord(type, volumeName, zoneId, domainId, accountId, diskOfferingId, provisioningType, size, instanceId, poolId, templateId, unitNumber, diskInfo);
}
 
Example #12
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public int getFreeUnitNumberOnIDEController(int controllerKey) throws Exception {
    int freeUnitNumber = 0;
    List<VirtualDevice> devices = (List<VirtualDevice>)_context.getVimClient().
            getDynamicProperty(_mor, "config.hardware.device");

    int deviceCount = 0;
    int ideDeviceUnitNumber = -1;
    if (devices != null && devices.size() > 0) {
        for (VirtualDevice device : devices) {
            if (device instanceof VirtualDisk && (controllerKey == device.getControllerKey())) {
                deviceCount++;
                ideDeviceUnitNumber = device.getUnitNumber();
            }
        }
    }
    if (deviceCount == 1) {
        if (ideDeviceUnitNumber == 0) {
            freeUnitNumber = 1;
        } // else freeUnitNumber is already initialized to 0
    } else if (deviceCount == 2) {
        throw new Exception("IDE controller with key [" + controllerKey + "] already has 2 device attached. Cannot attach more than the limit of 2.");
    }
    return freeUnitNumber;
}
 
Example #13
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public VirtualDisk[] getAllIndependentDiskDevice() throws Exception {
    List<VirtualDisk> independentDisks = new ArrayList<VirtualDisk>();
    VirtualDisk[] allDisks = getAllDiskDevice();
    if (allDisks.length > 0) {
        for (VirtualDisk disk : allDisks) {
            String diskMode = "";
            if (disk.getBacking() instanceof VirtualDiskFlatVer1BackingInfo) {
                diskMode = ((VirtualDiskFlatVer1BackingInfo)disk.getBacking()).getDiskMode();
            } else if (disk.getBacking() instanceof VirtualDiskFlatVer2BackingInfo) {
                diskMode = ((VirtualDiskFlatVer2BackingInfo)disk.getBacking()).getDiskMode();
            } else if (disk.getBacking() instanceof VirtualDiskRawDiskMappingVer1BackingInfo) {
                diskMode = ((VirtualDiskRawDiskMappingVer1BackingInfo)disk.getBacking()).getDiskMode();
            } else if (disk.getBacking() instanceof VirtualDiskSparseVer1BackingInfo) {
                diskMode = ((VirtualDiskSparseVer1BackingInfo)disk.getBacking()).getDiskMode();
            } else if (disk.getBacking() instanceof VirtualDiskSparseVer2BackingInfo) {
                diskMode = ((VirtualDiskSparseVer2BackingInfo)disk.getBacking()).getDiskMode();
            }

            if (diskMode.indexOf("independent") != -1) {
                independentDisks.add(disk);
            }
        }
    }

    return independentDisks.toArray(new VirtualDisk[0]);
}
 
Example #14
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
/**
 * Check if disk is ROOT disk
 */
private boolean isRootDisk(VirtualDisk disk, Map<VirtualDisk, VolumeVO> disksMapping, Backup backup) {
    if (!disksMapping.containsKey(disk)) {
        return false;
    }
    VolumeVO volumeVO = disksMapping.get(disk);
    if (volumeVO == null) {
        final VMInstanceVO vm = _vmDao.findByIdIncludingRemoved(backup.getVmId());
        if (vm == null) {
            throw new CloudRuntimeException("Failed to find the volumes details from the VM backup");
        }
        List<Backup.VolumeInfo> backedUpVolumes = vm.getBackupVolumeList();
        for (Backup.VolumeInfo backedUpVolume : backedUpVolumes) {
            if (backedUpVolume.getSize().equals(disk.getCapacityInBytes())) {
                return backedUpVolume.getType().equals(Volume.Type.ROOT);
            }
        }
    } else {
        return volumeVO.getVolumeType().equals(Volume.Type.ROOT);
    }
    throw new CloudRuntimeException("Could not determinate ROOT disk for VM to import");
}
 
Example #15
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public List<Pair<Integer, ManagedObjectReference>> getAllDiskDatastores() throws Exception {
    List<Pair<Integer, ManagedObjectReference>> disks = new ArrayList<Pair<Integer, ManagedObjectReference>>();

    List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");
    if (devices != null && devices.size() > 0) {
        for (VirtualDevice device : devices) {
            if (device instanceof VirtualDisk) {
                VirtualDeviceBackingInfo backingInfo = ((VirtualDisk)device).getBacking();
                if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) {
                    VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)backingInfo;
                    disks.add(new Pair<Integer, ManagedObjectReference>(new Integer(device.getKey()), diskBackingInfo.getDatastore()));
                }
            }
        }
    }

    return disks;
}
 
Example #16
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public VirtualMachineDiskInfoBuilder getDiskInfoBuilder() throws Exception {
    VirtualMachineDiskInfoBuilder builder = new VirtualMachineDiskInfoBuilder();

    List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");

    if (devices != null && devices.size() > 0) {
        for (VirtualDevice device : devices) {
            if (device instanceof VirtualDisk) {
                VirtualDeviceBackingInfo backingInfo = ((VirtualDisk)device).getBacking();
                if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) {
                    VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)backingInfo;

                    while (diskBackingInfo != null) {
                        String deviceBusName = getDeviceBusName(devices, device);
                        builder.addDisk(deviceBusName, diskBackingInfo.getFileName());
                        diskBackingInfo = diskBackingInfo.getParent();
                    }
                }
            }
        }
    }

    return builder;
}
 
Example #17
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #18
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
protected VolumeVO updateVolume(VirtualDisk disk, Map<VirtualDisk, VolumeVO> disksMapping, VirtualMachineMO vmToImport, Long poolId, VirtualMachine vm) throws Exception {
    VolumeVO volume = disksMapping.get(disk);
    String volumeName = getVolumeName(disk, vmToImport);
    volume.setPath(volumeName);
    volume.setPoolId(poolId);
    VirtualMachineDiskInfo diskInfo = getDiskInfo(vmToImport, poolId, volumeName);
    volume.setChainInfo(new Gson().toJson(diskInfo));
    volume.setInstanceId(vm.getId());
    volume.setState(Volume.State.Ready);
    volume.setAttached(new Date());
    _volumeDao.update(volume.getId(), volume);
    if (volume.getRemoved() != null) {
        _volumeDao.unremove(volume.getId());
        if (vm.getType() == Type.User) {
            UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VOLUME_CREATE, volume.getAccountId(), volume.getDataCenterId(), volume.getId(), volume.getName(), volume.getDiskOfferingId(), null, volume.getSize(),
                    Volume.class.getName(), volume.getUuid(), volume.isDisplayVolume());
            _resourceLimitService.incrementResourceCount(vm.getAccountId(), Resource.ResourceType.volume, volume.isDisplayVolume());
            _resourceLimitService.incrementResourceCount(vm.getAccountId(), Resource.ResourceType.primary_storage, volume.isDisplayVolume(), volume.getSize());
        }
    }
    return volume;
}
 
Example #19
Source File: SiocManagerImpl.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
private VolumeVO getVolumeFromVirtualDisk(VMInstanceVO vmInstance, long storagePoolId, List<VirtualDevice> allDevices,
        VirtualDisk disk) throws Exception {
    List<VolumeVO> volumes = volumeDao.findByInstance(vmInstance.getId());

    if (volumes == null || volumes.size() == 0) {
        String errMsg = "Error: The VMware virtual disk '" + disk + "' could not be mapped to a CloudStack volume. " +
                "There were no volumes for the VM with the following ID: " + vmInstance.getId() + ".";

        throw new Exception(errMsg);
    }

    VirtualMachineDiskInfoBuilder diskInfoBuilder = VMwareUtil.getDiskInfoBuilder(allDevices);

    for (VolumeVO volume : volumes) {
        Long poolId = volume.getPoolId();

        if (poolId != null && poolId == storagePoolId) {
            StoragePoolVO storagePool = storagePoolDao.findById(poolId);
            String path = storagePool.getPath();
            String charToSearchFor = "/";
            int index = path.lastIndexOf(charToSearchFor) + charToSearchFor.length();
            String datastoreName = path.substring(index);
            VirtualMachineDiskInfo diskInfo = diskInfoBuilder.getDiskInfoByBackingFileBaseName(volume.getPath(), datastoreName);

            if (diskInfo != null) {
                String deviceBusName = VMwareUtil.getDeviceBusName(allDevices, disk);

                if (deviceBusName.equals(diskInfo.getDiskDeviceBusName())) {
                    return volume;
                }
            }
        }
    }

    return null;
}
 
Example #20
Source File: VMwareUtil.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public static String getDeviceBusName(List<VirtualDevice> allDevices, VirtualDisk disk) throws Exception {
    for (VirtualDevice device : allDevices) {
        if (device.getKey() == disk.getControllerKey()) {
            if (device instanceof VirtualIDEController) {
                return String.format("ide%d:%d", ((VirtualIDEController)device).getBusNumber(), disk.getUnitNumber());
            } else if (device instanceof VirtualSCSIController) {
                return String.format("scsi%d:%d", ((VirtualSCSIController)device).getBusNumber(), disk.getUnitNumber());
            } else {
                throw new Exception("The device controller is not supported.");
            }
        }
    }

    throw new Exception("The device controller could not be located.");
}
 
Example #21
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
/**
 * Get pool ID for disk
 */
private Long getPoolId(VirtualDisk disk) {
    VirtualDeviceBackingInfo backing = disk.getBacking();
    checkBackingInfo(backing);
    VirtualDiskFlatVer2BackingInfo info = (VirtualDiskFlatVer2BackingInfo)backing;
    String[] fileNameParts = info.getFileName().split(" ");
    String datastoreUuid = StringUtils.substringBetween(fileNameParts[0], "[", "]");
    return getPoolIdFromDatastoreUuid(datastoreUuid);
}
 
Example #22
Source File: VmwareHelper.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #23
Source File: VmwareHelper.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public static VirtualDisk getRootDisk(VirtualDisk[] disks) {
    if (disks.length == 1)
        return disks[0];

    // TODO : for now, always return the first disk as root disk
    return disks[0];
}
 
Example #24
Source File: VmwareHelper.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public static String getDiskDeviceFileName(VirtualDisk diskDevice) {
    VirtualDeviceBackingInfo backingInfo = diskDevice.getBacking();
    if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) {
        final String vmdkName = ((VirtualDiskFlatVer2BackingInfo)backingInfo).getFileName().replace(".vmdk", "");
        if (vmdkName.contains("/")) {
            return vmdkName.split("/", 2)[1];
        }
        return vmdkName;
    }
    return null;
}
 
Example #25
Source File: VmwareStorageProcessor.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
private List<String> getManagedDatastoreNamesFromVirtualDisks(List<VirtualDisk> virtualDisks) {
    List<String> managedDatastoreNames = new ArrayList<>();

    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.")) {
                            managedDatastoreNames.add("-" + path + "-0");
                        }
                    }
                }
            }
        }
    }

    return managedDatastoreNames;
}
 
Example #26
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
/**
 * Get dest datastore mor
 */
private ManagedObjectReference getDestStoreMor(VirtualMachineMO vmMo) throws Exception {
    VirtualDisk vmDisk = vmMo.getVirtualDisks().get(0);
    VirtualDeviceBackingInfo backing = vmDisk.getBacking();
    checkBackingInfo(backing);
    VirtualDiskFlatVer2BackingInfo info = (VirtualDiskFlatVer2BackingInfo)backing;
    return info.getDatastore();
}
 
Example #27
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
/**
 * Find restored volume based on volume info
 */
private VirtualDisk findRestoredVolume(Backup.VolumeInfo volumeInfo, VirtualMachineMO vm) throws Exception {
    List<VirtualDisk> virtualDisks = vm.getVirtualDisks();
    for (VirtualDisk disk : virtualDisks) {
        if (disk.getCapacityInBytes().equals(volumeInfo.getSize())) {
            return disk;
        }
    }
    throw new CloudRuntimeException("Volume to restore could not be found");
}
 
Example #28
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public int getNumberOfVirtualDisks() throws Exception {
    List<VirtualDevice> devices = (List<VirtualDevice>)_context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");

    s_logger.info("Counting disk devices attached to VM " + getVmName());
    int count = 0;

    if (devices != null && devices.size() > 0) {
        for (VirtualDevice device : devices) {
            if (device instanceof VirtualDisk) {
                count++;
            }
        }
    }
    return count;
}
 
Example #29
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
/**
 * Get dest volume full path
 */
private String getDestVolumeFullPath(VirtualDisk restoredDisk, VirtualMachineMO restoredVm, VirtualMachineMO vmMo) throws Exception {
    VirtualDisk vmDisk = vmMo.getVirtualDisks().get(0);
    String vmDiskPath = vmMo.getVmdkFileBaseName(vmDisk);
    String vmDiskFullPath = getVolumeFullPath(vmMo.getVirtualDisks().get(0));
    String restoredVolumePath = restoredVm.getVmdkFileBaseName(restoredDisk);
    return vmDiskFullPath.replace(vmDiskPath, restoredVolumePath);
}
 
Example #30
Source File: VMwareGuru.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
/**
 * Get root disk template path
 */
private String getRootDiskTemplatePath(VirtualDisk rootDisk) {
    VirtualDeviceBackingInfo backing = rootDisk.getBacking();
    checkBackingInfo(backing);
    VirtualDiskFlatVer2BackingInfo info = (VirtualDiskFlatVer2BackingInfo)backing;
    VirtualDiskFlatVer2BackingInfo parent = info.getParent();
    return (parent != null) ? getVolumeNameFromFileName(parent.getFileName()) : getVolumeNameFromFileName(info.getFileName());
}