Search in sources :

Example 1 with DiskDef

use of com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method attachOrDetachISO.

protected synchronized String attachOrDetachISO(Connect conn, String vmName, String isoPath, boolean isAttach) throws LibvirtException, URISyntaxException, InternalErrorException {
    String isoXml = null;
    if (isoPath != null && isAttach) {
        int index = isoPath.lastIndexOf("/");
        String path = isoPath.substring(0, index);
        String name = isoPath.substring(index + 1);
        KVMStoragePool secondaryPool = _storagePoolMgr.getStoragePoolByURI(path);
        KVMPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name);
        isoPath = isoVol.getPath();
        DiskDef iso = new DiskDef();
        iso.defISODisk(isoPath);
        isoXml = iso.toString();
    } else {
        DiskDef iso = new DiskDef();
        iso.defISODisk(null);
        isoXml = iso.toString();
    }
    List<DiskDef> disks = getDisks(conn, vmName);
    String result = attachOrDetachDevice(conn, true, vmName, isoXml);
    if (result == null && !isAttach) {
        for (DiskDef disk : disks) {
            if (disk.getDeviceType() == DiskDef.deviceType.CDROM) {
                cleanupDisk(conn, disk);
            }
        }
    }
    return result;
}
Also used : DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk)

Example 2 with DiskDef

use of com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method execute.

protected Answer execute(StopCommand cmd) {
    final String vmName = cmd.getVmName();
    Long bytesReceived = new Long(0);
    Long bytesSent = new Long(0);
    State state = null;
    synchronized (_vms) {
        state = _vms.get(vmName);
        _vms.put(vmName, State.Stopping);
    }
    try {
        Connect conn = LibvirtConnection.getConnection();
        List<DiskDef> disks = getDisks(conn, vmName);
        destroy_network_rules_for_vm(conn, vmName);
        String result = stopVM(conn, vmName, defineOps.UNDEFINE_VM);
        if (result == null) {
            for (DiskDef disk : disks) {
                if (disk.getDeviceType() == DiskDef.deviceType.CDROM && disk.getDiskPath() != null)
                    cleanupDisk(conn, disk);
            }
        }
        final String result2 = cleanupVnet(conn, cmd.getVnet());
        if (result != null && result2 != null) {
            result = result2 + result;
        }
        state = State.Stopped;
        return new StopAnswer(cmd, result, 0, bytesSent, bytesReceived);
    } catch (LibvirtException e) {
        return new StopAnswer(cmd, e.getMessage());
    } finally {
        synchronized (_vms) {
            if (state != null) {
                _vms.put(vmName, state);
            } else {
                _vms.remove(vmName);
            }
        }
    }
}
Also used : DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) LibvirtException(org.libvirt.LibvirtException) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect) StopAnswer(com.cloud.agent.api.StopAnswer)

Example 3 with DiskDef

use of com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method getDisks.

protected List<DiskDef> getDisks(Connect conn, String vmName) {
    LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
    Domain dm = null;
    try {
        dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
        parser.parseDomainXML(dm.getXMLDesc(0));
        return parser.getDisks();
    } catch (LibvirtException e) {
        s_logger.debug("Failed to get dom xml: " + e.toString());
        return new ArrayList<DiskDef>();
    } catch (Exception e) {
        s_logger.debug("Failed to get dom xml: " + e.toString());
        return new ArrayList<DiskDef>();
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
        } catch (LibvirtException e) {
        }
    }
}
Also used : DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) LibvirtException(org.libvirt.LibvirtException) Domain(org.libvirt.Domain) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) LibvirtException(org.libvirt.LibvirtException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException)

Example 4 with DiskDef

use of com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method attachOrDetachDisk.

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;
    try {
        if (!attach) {
            dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
            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 (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();
        }
    }
}
Also used : DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) InternalErrorException(com.cloud.exception.InternalErrorException) Domain(org.libvirt.Domain)

Example 5 with DiskDef

use of com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method createPatchVbd.

private void createPatchVbd(Connect conn, String vmName, LibvirtVMDef vm, VirtualMachineTO vmSpec) throws LibvirtException, InternalErrorException {
    List<DiskDef> disks = vm.getDevices().getDisks();
    DiskDef rootDisk = disks.get(0);
    VolumeTO rootVol = getVolume(vmSpec, Volume.Type.ROOT);
    KVMStoragePool pool = _storagePoolMgr.getStoragePool(rootVol.getPoolUuid());
    KVMPhysicalDisk disk = pool.createPhysicalDisk(UUID.randomUUID().toString(), KVMPhysicalDisk.PhysicalDiskFormat.RAW, 10L * 1024 * 1024);
    /* Format/create fs on this disk */
    final Script command = new Script(_createvmPath, _timeout, s_logger);
    command.add("-f", disk.getPath());
    String result = command.execute();
    if (result != null) {
        s_logger.debug("Failed to create data disk: " + result);
        throw new InternalErrorException("Failed to create data disk: " + result);
    }
    String datadiskPath = disk.getPath();
    /* add patch disk */
    DiskDef patchDisk = new DiskDef();
    patchDisk.defFileBasedDisk(datadiskPath, 1, rootDisk.getBusType(), DiskDef.diskFmtType.RAW);
    disks.add(patchDisk);
    String bootArgs = vmSpec.getBootArgs();
    patchSystemVm(bootArgs, datadiskPath, vmName);
}
Also used : Script(com.cloud.utils.script.Script) DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) VolumeTO(com.cloud.agent.api.to.VolumeTO) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk) InternalErrorException(com.cloud.exception.InternalErrorException)

Aggregations

DiskDef (com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef)9 KVMPhysicalDisk (com.cloud.agent.storage.KVMPhysicalDisk)3 KVMStoragePool (com.cloud.agent.storage.KVMStoragePool)3 InternalErrorException (com.cloud.exception.InternalErrorException)3 LibvirtException (org.libvirt.LibvirtException)3 VolumeTO (com.cloud.agent.api.to.VolumeTO)2 InterfaceDef (com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef)2 State (com.cloud.vm.VirtualMachine.State)2 IOException (java.io.IOException)2 ConfigurationException (javax.naming.ConfigurationException)2 Connect (org.libvirt.Connect)2 Domain (org.libvirt.Domain)2 StartAnswer (com.cloud.agent.api.StartAnswer)1 StopAnswer (com.cloud.agent.api.StopAnswer)1 StopCommand (com.cloud.agent.api.StopCommand)1 NicTO (com.cloud.agent.api.to.NicTO)1 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)1 LibvirtVMDef (com.cloud.agent.resource.computing.LibvirtVMDef)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 Script (com.cloud.utils.script.Script)1