Search in sources :

Example 6 with VirtualMachineFileInfo

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

the class VirtualMachineMO method getSnapshotDescriptorDatastorePath.

public String getSnapshotDescriptorDatastorePath() throws Exception {
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("VirtualMachine");
    pSpec.setPathSet(new String[] { "name", "config.files" });
    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.FALSE);
    PropertyFilterSpec pfSpec = new PropertyFilterSpec();
    pfSpec.setPropSet(new PropertySpec[] { pSpec });
    pfSpec.setObjectSet(new ObjectSpec[] { oSpec });
    ObjectContent[] ocs = _context.getService().retrieveProperties(_context.getServiceContent().getPropertyCollector(), new PropertyFilterSpec[] { pfSpec });
    assert (ocs != null);
    String vmName = null;
    VirtualMachineFileInfo fileInfo = null;
    assert (ocs.length == 1);
    for (ObjectContent oc : ocs) {
        DynamicProperty[] props = oc.getPropSet();
        if (props != null) {
            assert (props.length == 2);
            for (DynamicProperty prop : props) {
                if (prop.getName().equals("name")) {
                    vmName = prop.getVal().toString();
                } else {
                    fileInfo = (VirtualMachineFileInfo) prop.getVal();
                }
            }
        }
    }
    assert (vmName != null);
    assert (fileInfo != null);
    // .vmsd file exists at the same directory of .vmx file
    DatastoreFile vmxFile = new DatastoreFile(fileInfo.getVmPathName());
    return vmxFile.getCompanionPath(vmName + ".vmsd");
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) ObjectContent(com.vmware.vim25.ObjectContent) ObjectSpec(com.vmware.vim25.ObjectSpec) PropertySpec(com.vmware.vim25.PropertySpec) DynamicProperty(com.vmware.vim25.DynamicProperty) VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo)

Example 7 with VirtualMachineFileInfo

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

the class VirtualMachineMO method getCurrentSnapshotDiskChainDatastorePaths.

public String[] getCurrentSnapshotDiskChainDatastorePaths(String diskDevice) throws Exception {
    HostMO hostMo = getRunningHost();
    List<Pair<ManagedObjectReference, String>> mounts = hostMo.getDatastoreMountsOnHost();
    VirtualMachineFileInfo vmFileInfo = getFileInfo();
    SnapshotDescriptor descriptor = getSnapshotDescriptor();
    SnapshotInfo[] snapshotInfo = descriptor.getCurrentDiskChain();
    List<String> diskDsFullPaths = new ArrayList<String>();
    for (int i = 0; i < snapshotInfo.length; i++) {
        SnapshotDescriptor.DiskInfo[] disks = snapshotInfo[i].getDisks();
        if (disks != null) {
            for (SnapshotDescriptor.DiskInfo disk : disks) {
                String deviceNameInDisk = disk.getDeviceName();
                if (diskDevice == null || diskDevice.equalsIgnoreCase(deviceNameInDisk)) {
                    String vmdkFullDsPath = getSnapshotDiskFileDatastorePath(vmFileInfo, mounts, disk.getDiskFileName());
                    diskDsFullPaths.add(vmdkFullDsPath);
                }
            }
        }
    }
    return diskDsFullPaths.toArray(new String[0]);
}
Also used : ArrayList(java.util.ArrayList) VirtualMachineSnapshotInfo(com.vmware.vim25.VirtualMachineSnapshotInfo) SnapshotInfo(com.cloud.hypervisor.vmware.mo.SnapshotDescriptor.SnapshotInfo) VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo) Pair(com.cloud.utils.Pair)

Example 8 with VirtualMachineFileInfo

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

the class VirtualMachineMO method getCurrentSnapshotDiskChainDatastorePaths.

public String[] getCurrentSnapshotDiskChainDatastorePaths(String diskDevice) throws Exception {
    HostMO hostMo = getRunningHost();
    List<Pair<ManagedObjectReference, String>> mounts = hostMo.getDatastoreMountsOnHost();
    VirtualMachineFileInfo vmFileInfo = getFileInfo();
    SnapshotDescriptor descriptor = getSnapshotDescriptor();
    SnapshotInfo[] snapshotInfo = descriptor.getCurrentDiskChain();
    List<String> diskDsFullPaths = new ArrayList<String>();
    for (int i = 0; i < snapshotInfo.length; i++) {
        SnapshotDescriptor.DiskInfo[] disks = snapshotInfo[i].getDisks();
        if (disks != null) {
            for (SnapshotDescriptor.DiskInfo disk : disks) {
                String deviceNameInDisk = disk.getDeviceName();
                if (diskDevice == null || diskDevice.equalsIgnoreCase(deviceNameInDisk)) {
                    String vmdkFullDsPath = getSnapshotDiskFileDatastorePath(vmFileInfo, mounts, disk.getDiskFileName());
                    diskDsFullPaths.add(vmdkFullDsPath);
                }
            }
        }
    }
    return diskDsFullPaths.toArray(new String[0]);
}
Also used : ArrayList(java.util.ArrayList) VirtualMachineSnapshotInfo(com.vmware.vim25.VirtualMachineSnapshotInfo) SnapshotInfo(com.cloud.hypervisor.vmware.mo.SnapshotDescriptor.SnapshotInfo) VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo) Pair(com.cloud.utils.Pair)

Example 9 with VirtualMachineFileInfo

use of com.vmware.vim25.VirtualMachineFileInfo 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;
}
Also used : ArrayList(java.util.ArrayList) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Pair(com.cloud.utils.Pair)

Example 10 with VirtualMachineFileInfo

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

the class VirtualMachineMO method backupCurrentSnapshot.

// destName does not contain extension name
public void backupCurrentSnapshot(String deviceName, ManagedObjectReference morDestDs, String destDsDirectory, String destName, boolean includeBase) throws Exception {
    SnapshotDescriptor descriptor = getSnapshotDescriptor();
    SnapshotInfo[] snapshotInfo = descriptor.getCurrentDiskChain();
    if (snapshotInfo.length == 0) {
        String msg = "No snapshot found in this VM";
        throw new Exception(msg);
    }
    HostMO hostMo = getRunningHost();
    DatacenterMO dcMo = getOwnerDatacenter().first();
    List<Pair<ManagedObjectReference, String>> mounts = hostMo.getDatastoreMountsOnHost();
    VirtualMachineFileInfo vmFileInfo = getFileInfo();
    List<Ternary<String, String, String>> backupInfo = new ArrayList<Ternary<String, String, String>>();
    for (int i = 0; i < snapshotInfo.length; i++) {
        if (!includeBase && i == snapshotInfo.length - 1) {
            break;
        }
        SnapshotDescriptor.DiskInfo[] disks = snapshotInfo[i].getDisks();
        if (disks != null) {
            String destBaseFileName;
            String destFileName;
            String destParentFileName;
            for (SnapshotDescriptor.DiskInfo disk : disks) {
                if (deviceName == null || deviceName.equals(disk.getDeviceName())) {
                    String srcVmdkFullDsPath = getSnapshotDiskFileDatastorePath(vmFileInfo, mounts, disk.getDiskFileName());
                    Pair<DatastoreMO, String> srcDsInfo = getOwnerDatastore(srcVmdkFullDsPath);
                    Pair<VmdkFileDescriptor, byte[]> vmdkInfo = getVmdkFileInfo(srcVmdkFullDsPath);
                    String srcVmdkBaseFilePath = DatastoreFile.getCompanionDatastorePath(srcVmdkFullDsPath, vmdkInfo.first().getBaseFileName());
                    destFileName = destName + (snapshotInfo.length - i - 1) + ".vmdk";
                    if (vmdkInfo.first().getParentFileName() != null) {
                        destBaseFileName = destName + (snapshotInfo.length - i - 1) + "-delta.vmdk";
                        destParentFileName = destName + (snapshotInfo.length - i - 2) + ".vmdk";
                    } else {
                        destBaseFileName = destName + (snapshotInfo.length - i - 1) + "-flat.vmdk";
                        destParentFileName = null;
                    }
                    s_logger.info("Copy VMDK base file " + srcVmdkBaseFilePath + " to " + destDsDirectory + "/" + destBaseFileName);
                    srcDsInfo.first().copyDatastoreFile(srcVmdkBaseFilePath, dcMo.getMor(), morDestDs, destDsDirectory + "/" + destBaseFileName, dcMo.getMor(), true);
                    byte[] newVmdkContent = VmdkFileDescriptor.changeVmdkContentBaseInfo(vmdkInfo.second(), destBaseFileName, destParentFileName);
                    String vmdkUploadUrl = getContext().composeDatastoreBrowseUrl(dcMo.getName(), destDsDirectory + "/" + destFileName);
                    s_logger.info("Upload VMDK content file to " + destDsDirectory + "/" + destFileName);
                    getContext().uploadResourceContent(vmdkUploadUrl, newVmdkContent);
                    backupInfo.add(new Ternary<String, String, String>(destFileName, destBaseFileName, destParentFileName));
                }
            }
        }
    }
    byte[] vdiskInfo = VmwareHelper.composeDiskInfo(backupInfo, snapshotInfo.length, includeBase);
    String vdiskUploadUrl = getContext().composeDatastoreBrowseUrl(dcMo.getName(), destDsDirectory + "/" + destName + ".vdisk");
    getContext().uploadResourceContent(vdiskUploadUrl, vdiskInfo);
}
Also used : Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) VirtualMachineSnapshotInfo(com.vmware.vim25.VirtualMachineSnapshotInfo) SnapshotInfo(com.cloud.hypervisor.vmware.mo.SnapshotDescriptor.SnapshotInfo) VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo) Pair(com.cloud.utils.Pair)

Aggregations

VirtualMachineFileInfo (com.vmware.vim25.VirtualMachineFileInfo)20 Pair (com.cloud.utils.Pair)8 ArrayList (java.util.ArrayList)8 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)6 SnapshotInfo (com.cloud.hypervisor.vmware.mo.SnapshotDescriptor.SnapshotInfo)4 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)4 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)4 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)4 VirtualMachineSnapshotInfo (com.vmware.vim25.VirtualMachineSnapshotInfo)4 Ternary (com.cloud.utils.Ternary)2 DynamicProperty (com.vmware.vim25.DynamicProperty)2 ObjectContent (com.vmware.vim25.ObjectContent)2 ObjectSpec (com.vmware.vim25.ObjectSpec)2 VirtualDevice (com.vmware.vim25.VirtualDevice)2 VirtualDeviceBackingInfo (com.vmware.vim25.VirtualDeviceBackingInfo)2 VirtualDiskFlatVer2BackingInfo (com.vmware.vim25.VirtualDiskFlatVer2BackingInfo)2 VirtualLsiLogicController (com.vmware.vim25.VirtualLsiLogicController)2 VirtualMachineVideoCard (com.vmware.vim25.VirtualMachineVideoCard)2 BufferedReader (java.io.BufferedReader)2 BufferedWriter (java.io.BufferedWriter)2