Package org.libvirt

Examples of org.libvirt.Domain


    }

    protected synchronized String attachOrDetachDisk(Connect conn, boolean attach, String vmName,
            KVMPhysicalDisk attachingDisk, int devId) throws LibvirtException, InternalErrorException {
        List<DiskDef> disks = null;
        Domain dm = null;
        DiskDef diskdef = null;
        KVMStoragePool attachingPool = attachingDisk.getPool();
        try {
            if (!attach) {
                dm = conn.domainLookupByName(vmName);
                LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
                String xml = dm.getXMLDesc(0);
                parser.parseDomainXML(xml);
                disks = parser.getDisks();

                for (DiskDef disk : disks) {
                    String file = disk.getDiskPath();
                    if (file != null && file.equalsIgnoreCase(attachingDisk.getPath())) {
                        diskdef = disk;
                        break;
                    }
                }
                if (diskdef == null) {
                    throw new InternalErrorException("disk: " + attachingDisk.getPath() + " is not attached before");
                }
            } else {
                diskdef = new DiskDef();
                if (attachingPool.getType() == StoragePoolType.RBD) {
                    diskdef.defNetworkBasedDisk(attachingDisk.getPath(),
                            attachingPool.getSourceHost(), attachingPool.getSourcePort(),
                            attachingPool.getAuthUserName(), attachingPool.getUuid(), devId,
                            DiskDef.diskBus.VIRTIO, diskProtocol.RBD);
                } else if (attachingDisk.getFormat() == PhysicalDiskFormat.QCOW2) {
                    diskdef.defFileBasedDisk(attachingDisk.getPath(), devId,
                            DiskDef.diskBus.VIRTIO, DiskDef.diskFmtType.QCOW2);
                } else if (attachingDisk.getFormat() == PhysicalDiskFormat.RAW) {
                    diskdef.defBlockBasedDisk(attachingDisk.getPath(), devId,
                            DiskDef.diskBus.VIRTIO);
                }
            }

            String xml = diskdef.toString();
            return attachOrDetachDevice(conn, attach, vmName, xml);
        } finally {
            if (dm != null) {
                dm.free();
            }
        }
    }
View Full Code Here


        String snapshotName = UUID.randomUUID().toString();
        String vmName = volume.getVmName();
        try {
            Connect conn = LibvirtConnection.getConnectionByVmName(vmName);
            DomainInfo.DomainState state = null;
            Domain vm = null;
            if (vmName != null) {
                try {
                    vm = this.resource.getDomain(conn, vmName);
                    state = vm.getInfo().state;
                } catch (LibvirtException e) {
                    s_logger.trace("Ignoring libvirt error.", e);
                }
            }

            KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(),
                    primaryStore.getUuid());

            KVMPhysicalDisk disk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(),
                    primaryStore.getUuid(), volume.getPath());
            if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING && !primaryPool.isExternalSnapshot()) {
                String vmUuid = vm.getUUIDString();
                Object[] args = new Object[] { snapshotName, vmUuid };
                String snapshot = SnapshotXML.format(args);
                s_logger.debug(snapshot);

                vm.snapshotCreateXML(snapshot);
                /*
                 * libvirt on RHEL6 doesn't handle resume event emitted from
                 * qemu
                 */
                vm = this.resource.getDomain(conn, vmName);
                state = vm.getInfo().state;
                if (state == DomainInfo.DomainState.VIR_DOMAIN_PAUSED) {
                    vm.resume();
                }
            } else {
                /**
                 * For RBD we can't use libvirt to do our snapshotting or any Bash scripts.
                 * libvirt also wants to store the memory contents of the Virtual Machine,
View Full Code Here

            + domainInfo.domainInfo.nrVirtCpu + "\n\tcpu=" + domainInfo.domainInfo.cpuTime);
    }

    public DomainInfo getDomainInfo(String domainName) throws LibvirtException {
        try {
            Domain domain = getDomain(domainName);
            try {
                DomainInfo info = new DomainInfo();
                info.domainInfo = domain.getInfo();
                info.name = domainName;
                info.uuid = domain.getUUIDString();

                return info;
            } finally {
                domain.free();
            }
        } catch (LibvirtException e) {
            log.error("Error looking up domain with name " + domainName, e);
            throw e;
        }
View Full Code Here

        }
    }

    public DomainInfo getDomainInfo(int id) throws LibvirtException {
        try {
            Domain domain = connection.domainLookupByID(id);
            if (domain == null) {
                throw new IllegalArgumentException("No domain found with ID: " + id);
            }
            try {
                DomainInfo info = new DomainInfo();
                info.domainInfo = domain.getInfo();
                info.name = domain.getName();
                info.uuid = domain.getUUIDString();

                return info;
            } finally {
                domain.free();
            }
        } catch (LibvirtException e) {
            log.error("Error looking up domain with id " + id, e);
            throw e;
        }
View Full Code Here

            throw e;
        }
    }

    public String getDomainXML(String domainName) throws LibvirtException {
        Domain domain = getDomain(domainName);
        try {
            return domain.getXMLDesc(0);
        } finally {
            domain.free();
        }
    }
View Full Code Here

            domain.free();
        }
    }

    public int domainReboot(String domainName) throws LibvirtException {
        Domain domain = getDomain(domainName);
        try {
            domain.reboot(0);
            return SUCCESS;
        } finally {
            domain.free();
        }
    }
View Full Code Here

        connection.restore(toPath);
        return SUCCESS;
    }

    public int domainDestroy(String domainName) throws LibvirtException {
        Domain domain = getDomain(domainName);
        try {
            domain.destroy();
            return SUCCESS;
        } finally {
            domain.free();
        }
    }
View Full Code Here

            domain.free();
        }
    }

    public int domainDelete(String domainName) throws LibvirtException {
        Domain domain = getDomain(domainName);
        try {
            DomainState state = domain.getInfo().state;
   
            if ((state != DomainState.VIR_DOMAIN_SHUTDOWN) && (state != DomainState.VIR_DOMAIN_SHUTOFF)) {
                domain.destroy();
            }
            domain.undefine();
           
            return SUCCESS;
        } finally {
            domain.free();           
        }
    }
View Full Code Here

            domain.free();           
        }
    }

    public int domainSave(String domainName, String toPath) throws LibvirtException {
        Domain domain = getDomain(domainName);
        try {
            domain.save(toPath);
            return SUCCESS;
        } finally {
            domain.free();
        }
    }
View Full Code Here

            domain.free();
        }
    }

    public int domainResume(String domainName) throws LibvirtException {
        Domain domain = getDomain(domainName);
        try {
            domain.resume();
            return SUCCESS;
        } finally {
            domain.free();
        }
    }
View Full Code Here

TOP

Related Classes of org.libvirt.Domain

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.