Search in sources :

Example 1 with WatchDogDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVmDef.WatchDogDef in project cosmic by MissionCriticalCloud.

the class LibvirtDomainXmlParser method parseDomainXml.

public boolean parseDomainXml(final String domXml) {
    final DocumentBuilder builder;
    try {
        builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        final InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(domXml));
        final Document doc = builder.parse(is);
        final Element rootElement = doc.getDocumentElement();
        desc = getTagValue("description", rootElement);
        final Element devices = (Element) rootElement.getElementsByTagName("devices").item(0);
        final NodeList disks = devices.getElementsByTagName("disk");
        for (int i = 0; i < disks.getLength(); i++) {
            final Element disk = (Element) disks.item(i);
            final String type = disk.getAttribute("type");
            final LibvirtDiskDef def = new LibvirtDiskDef();
            if (type.equalsIgnoreCase("network")) {
                final String diskFmtType = getAttrValue("driver", "type", disk);
                final String diskCacheMode = getAttrValue("driver", "cache", disk);
                final String diskPath = getAttrValue("source", "name", disk);
                final String protocol = getAttrValue("source", "protocol", disk);
                final String authUserName = getAttrValue("auth", "username", disk);
                final String poolUuid = getAttrValue("secret", "uuid", disk);
                final String host = getAttrValue("host", "name", disk);
                final int port = Integer.parseInt(getAttrValue("host", "port", disk));
                final String diskLabel = getAttrValue("target", "dev", disk);
                final String bus = getAttrValue("target", "bus", disk);
                LibvirtDiskDef.DiskFmtType fmt = null;
                if (diskFmtType != null) {
                    fmt = LibvirtDiskDef.DiskFmtType.valueOf(diskFmtType.toUpperCase());
                }
                def.defNetworkBasedDisk(diskPath, host, port, authUserName, poolUuid, diskLabel, LibvirtDiskDef.DiskBus.valueOf(bus.toUpperCase()), LibvirtDiskDef.DiskProtocol.valueOf(protocol.toUpperCase()), fmt);
                def.setCacheMode(LibvirtDiskDef.DiskCacheMode.valueOf(diskCacheMode.toUpperCase()));
            } else {
                final String diskFmtType = getAttrValue("driver", "type", disk);
                final String diskCacheMode = getAttrValue("driver", "cache", disk);
                final String diskFile = getAttrValue("source", "file", disk);
                final String diskDev = getAttrValue("source", "dev", disk);
                final String diskLabel = getAttrValue("target", "dev", disk);
                final String bus = getAttrValue("target", "bus", disk);
                final String device = disk.getAttribute("device");
                if (type.equalsIgnoreCase("file")) {
                    if (device.equalsIgnoreCase("disk")) {
                        LibvirtDiskDef.DiskFmtType fmt = null;
                        if (diskFmtType != null) {
                            fmt = LibvirtDiskDef.DiskFmtType.valueOf(diskFmtType.toUpperCase());
                        }
                        def.defFileBasedDisk(diskFile, diskLabel, LibvirtDiskDef.DiskBus.valueOf(bus.toUpperCase()), fmt);
                    } else if (device.equalsIgnoreCase("cdrom")) {
                        def.defIsoDisk(diskFile);
                    }
                } else if (type.equalsIgnoreCase("block")) {
                    def.defBlockBasedDisk(diskDev, diskLabel, LibvirtDiskDef.DiskBus.valueOf(bus.toUpperCase()));
                }
                if (diskCacheMode != null) {
                    def.setCacheMode(LibvirtDiskDef.DiskCacheMode.valueOf(diskCacheMode.toUpperCase()));
                }
            }
            final NodeList iotune = disk.getElementsByTagName("iotune");
            if (iotune != null && iotune.getLength() != 0) {
                final String bytesReadRateStr = getTagValue("read_bytes_sec", (Element) iotune.item(0));
                if (bytesReadRateStr != null) {
                    final Long bytesReadRate = Long.parseLong(bytesReadRateStr);
                    def.setBytesReadRate(bytesReadRate);
                }
                final String bytesWriteRateStr = getTagValue("write_bytes_sec", (Element) iotune.item(0));
                if (bytesWriteRateStr != null) {
                    final Long bytesWriteRate = Long.parseLong(bytesWriteRateStr);
                    def.setBytesWriteRate(bytesWriteRate);
                }
                final String iopsReadRateStr = getTagValue("read_iops_sec", (Element) iotune.item(0));
                if (iopsReadRateStr != null) {
                    final Long iopsReadRate = Long.parseLong(iopsReadRateStr);
                    def.setIopsReadRate(iopsReadRate);
                }
                final String iopsWriteRateStr = getTagValue("write_iops_sec", (Element) iotune.item(0));
                if (iopsWriteRateStr != null) {
                    final Long iopsWriteRate = Long.parseLong(iopsWriteRateStr);
                    def.setIopsWriteRate(iopsWriteRate);
                }
            }
            diskDefs.add(def);
        }
        final NodeList nics = devices.getElementsByTagName("interface");
        for (int i = 0; i < nics.getLength(); i++) {
            final Element nic = (Element) nics.item(i);
            final String type = nic.getAttribute("type");
            final String mac = getAttrValue("mac", "address", nic);
            final String dev = getAttrValue("target", "dev", nic);
            final String model = getAttrValue("model", "type", nic);
            final InterfaceDef def = new InterfaceDef();
            final NodeList bandwidth = nic.getElementsByTagName("bandwidth");
            Integer networkRateKBps = 0;
            if (bandwidth != null && bandwidth.getLength() != 0) {
                final Integer inbound = Integer.valueOf(getAttrValue("inbound", "average", (Element) bandwidth.item(0)));
                final Integer outbound = Integer.valueOf(getAttrValue("outbound", "average", (Element) bandwidth.item(0)));
                if (inbound.equals(outbound)) {
                    networkRateKBps = inbound;
                }
            }
            if (type.equalsIgnoreCase("network")) {
                final String network = getAttrValue("source", "network", nic);
                def.defPrivateNet(network, dev, mac, NicModel.valueOf(model.toUpperCase()), networkRateKBps);
            } else if (type.equalsIgnoreCase("bridge")) {
                final String bridge = getAttrValue("source", "bridge", nic);
                def.defBridgeNet(bridge, dev, mac, NicModel.valueOf(model.toUpperCase()), networkRateKBps);
            } else if (type.equalsIgnoreCase("ethernet")) {
                final String scriptPath = getAttrValue("script", "path", nic);
                def.defEthernet(dev, mac, NicModel.valueOf(model.toUpperCase()), scriptPath, networkRateKBps);
            }
            interfaces.add(def);
        }
        final Element graphic = (Element) devices.getElementsByTagName("graphics").item(0);
        if (graphic != null) {
            final String port = graphic.getAttribute("port");
            if (port != null) {
                try {
                    vncPort = Integer.parseInt(port);
                    if (vncPort != -1) {
                        vncPort = vncPort - 5900;
                    } else {
                        vncPort = null;
                    }
                } catch (final NumberFormatException nfe) {
                    vncPort = null;
                }
            }
        }
        final NodeList rngs = devices.getElementsByTagName("rng");
        for (int i = 0; i < rngs.getLength(); i++) {
            RngDef def = null;
            final Element rng = (Element) rngs.item(i);
            final String backendModel = getAttrValue("backend", "model", rng);
            final String path = getTagValue("backend", rng);
            if (Strings.isNullOrEmpty(backendModel)) {
                def = new RngDef(path);
            } else {
                def = new RngDef(path, RngBackendModel.valueOf(backendModel.toUpperCase()));
            }
            rngDefs.add(def);
        }
        final NodeList watchDogs = devices.getElementsByTagName("watchdog");
        for (int i = 0; i < watchDogs.getLength(); i++) {
            WatchDogDef def = null;
            final Element watchDog = (Element) watchDogs.item(i);
            final String action = watchDog.getAttribute("action");
            final String model = watchDog.getAttribute("model");
            if (Strings.isNullOrEmpty(action)) {
                def = new WatchDogDef(WatchDogModel.valueOf(model.toUpperCase()));
            } else {
                def = new WatchDogDef(WatchDogAction.valueOf(action.toUpperCase()), WatchDogModel.valueOf(model.toUpperCase()));
            }
            watchDogDefs.add(def);
        }
        return true;
    } catch (final ParserConfigurationException e) {
        s_logger.debug(e.toString());
    } catch (final SAXException e) {
        s_logger.debug(e.toString());
    } catch (final IOException e) {
        s_logger.debug(e.toString());
    }
    return false;
}
Also used : InputSource(org.xml.sax.InputSource) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.InterfaceDef) RngDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.RngDef) LibvirtDiskDef(com.cloud.hypervisor.kvm.resource.xml.LibvirtDiskDef) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) WatchDogDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.WatchDogDef) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 2 with WatchDogDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVmDef.WatchDogDef in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResource method createVmFromSpec.

public LibvirtVmDef createVmFromSpec(final VirtualMachineTO vmTo) {
    final LibvirtVmDef vm = new LibvirtVmDef();
    vm.setDomainName(vmTo.getName());
    String uuid = vmTo.getUuid();
    uuid = getUuid(uuid);
    vm.setDomUuid(uuid);
    vm.setDomDescription(vmTo.getOs());
    vm.setPlatformEmulator(vmTo.getPlatformEmulator());
    final MetadataTO metadataTo = vmTo.getMetadata();
    if (metadataTo != null) {
        final MetadataDef metadata = new MetadataDef();
        metadata.getNodes().put("domainUuid", metadataTo.getDomainUuid());
        vm.addComponent(metadata);
    }
    final GuestDef guest = new GuestDef();
    guest.setGuestType(GuestDef.GuestType.KVM);
    vm.setHvsType(HypervisorType.KVM.toString().toLowerCase());
    vm.setLibvirtVersion(hypervisorLibvirtVersion);
    vm.setQemuVersion(hypervisorQemuVersion);
    guest.setGuestArch(vmTo.getArch());
    guest.setMachineType("pc");
    guest.setUuid(uuid);
    guest.setManufacturer(vmTo.getManufacturer());
    guest.setBootOrder(GuestDef.BootOrder.CDROM);
    guest.setBootOrder(GuestDef.BootOrder.HARDISK);
    vm.addComponent(guest);
    final GuestResourceDef grd = new GuestResourceDef();
    if (vmTo.getMinRam() != vmTo.getMaxRam() && !libvirtComputingResourceProperties.getVmMemballoonDisable()) {
        grd.setMemBalloning(true);
        grd.setCurrentMem(vmTo.getMinRam() / 1024);
        grd.setMemorySize(vmTo.getMaxRam() / 1024);
    } else {
        grd.setMemorySize(vmTo.getMaxRam() / 1024);
    }
    final int vcpus = vmTo.getCpus();
    grd.setVcpuNum(vcpus);
    vm.addComponent(grd);
    final CpuModeDef cmd = new CpuModeDef();
    cmd.setMode(getGuestCpuMode());
    cmd.setModel(getGuestCpuModel());
    cmd.setCpuflags(vmTo.getCpuflags());
    if (vmTo.getType() == VirtualMachine.Type.User) {
        cmd.setFeatures(getCpuFeatures());
    }
    // multi cores per socket, for larger core configs
    if (vcpus % 6 == 0) {
        final int sockets = vcpus / 6;
        cmd.setTopology(6, sockets);
    } else if (vcpus % 4 == 0) {
        final int sockets = vcpus / 4;
        cmd.setTopology(4, sockets);
    }
    vm.addComponent(cmd);
    final CpuTuneDef ctd = new CpuTuneDef();
    if (VirtualMachine.Type.DomainRouter.equals(vmTo.getType())) {
        ctd.setShares(vmTo.getCpus() * libvirtComputingResourceProperties.getGuestCpuSharesRouter());
    } else {
        ctd.setShares(vmTo.getCpus() * libvirtComputingResourceProperties.getGuestCpuShares());
    }
    vm.addComponent(ctd);
    final FeaturesDef features = new FeaturesDef();
    features.addFeatures("pae");
    features.addFeatures("apic");
    features.addFeatures("acpi");
    vm.addComponent(features);
    final TermPolicy term = new TermPolicy();
    if (VirtualMachine.Type.DomainRouter.equals(vmTo.getType())) {
        term.setCrashPolicy(getRouterTermpolicyCrash());
        term.setPowerOffPolicy(getRouterTermpolicyPowerOff());
        term.setRebootPolicy(getRouterTermpolicyReboot());
    } else {
        term.setCrashPolicy(getVmTermpolicyCrash());
        term.setPowerOffPolicy(getVmTermpolicyPowerOff());
        term.setRebootPolicy(getVmTermpolicyReboot());
    }
    vm.addComponent(term);
    final ClockDef clock = new ClockDef();
    if (vmTo.getOs().startsWith("Windows")) {
        clock.setClockOffset(ClockDef.ClockOffset.LOCALTIME);
    } else if (vmTo.getType() != VirtualMachine.Type.User || isGuestVirtIoCapable(vmTo.getOs())) {
        if (hypervisorLibvirtVersion >= 9 * 1000 + 10) {
            clock.addTimer("kvmclock", null, null, isKvmclockDisabled());
        }
    }
    // Recommended default clock/timer settings - https://bugzilla.redhat.com/show_bug.cgi?id=1053847
    clock.addTimer("rtc", "catchup", null);
    clock.addTimer("pit", "delay", null);
    vm.addComponent(clock);
    final DevicesDef devices = new DevicesDef();
    devices.setEmulatorPath(hypervisorPath);
    devices.setGuestType(guest.getGuestType());
    final SerialDef serial = new SerialDef("pty", null, (short) 0);
    devices.addDevice(serial);
    final QemuGuestAgentDef guestagent = new QemuGuestAgentDef();
    devices.addDevice(guestagent);
    if (libvirtComputingResourceProperties.getVmRngEnable()) {
        final RngDef rngDevice = new RngDef(rngPath, rngBackendModel);
        devices.addDevice(rngDevice);
    }
    final WatchDogDef watchDog = new WatchDogDef(libvirtComputingResourceProperties.getVmWatchdogAction(), libvirtComputingResourceProperties.getVmWatchdogModel());
    devices.addDevice(watchDog);
    final VideoDef videoCard = new VideoDef(libvirtComputingResourceProperties.getVmVideoHardware(), libvirtComputingResourceProperties.getVmVideoRam());
    devices.addDevice(videoCard);
    final ConsoleDef console = new ConsoleDef("pty", null, null, (short) 0);
    devices.addDevice(console);
    // add the VNC port passwd here, get the passwd from the vmInstance.
    final String passwd = vmTo.getVncPassword();
    final GraphicDef grap = new GraphicDef("vnc", (short) 0, true, passwd, null);
    devices.addDevice(grap);
    final InputDef input = new InputDef("tablet", "usb");
    devices.addDevice(input);
    LibvirtDiskDef.DiskBus diskBusType = getDiskModelFromVmDetail(vmTo);
    if (diskBusType == null) {
        diskBusType = getGuestDiskModel(vmTo.getPlatformEmulator());
    }
    // If we're using virtio scsi, then we need to add a virtual scsi controller
    if (diskBusType == LibvirtDiskDef.DiskBus.SCSI) {
        vmTo.getName();
        final ScsiDef sd = new ScsiDef((short) 0, 0, 0, 9, 0);
        devices.addDevice(sd);
        logger.debug("Adding SCSI definition for " + vmTo.getName() + ":\n" + sd.toString());
    }
    vm.addComponent(devices);
    return vm;
}
Also used : ScsiDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.ScsiDef) TermPolicy(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.TermPolicy) MetadataTO(com.cloud.agent.api.to.MetadataTO) CpuTuneDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.CpuTuneDef) DevicesDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.DevicesDef) GuestResourceDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.GuestResourceDef) ClockDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.ClockDef) MetadataDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.MetadataDef) GraphicDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.GraphicDef) VideoDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.VideoDef) RngDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.RngDef) SerialDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.SerialDef) ConsoleDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.ConsoleDef) InputDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.InputDef) LibvirtDiskDef(com.cloud.hypervisor.kvm.resource.xml.LibvirtDiskDef) CpuModeDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.CpuModeDef) FeaturesDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.FeaturesDef) WatchDogDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.WatchDogDef) GuestDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.GuestDef) QemuGuestAgentDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.QemuGuestAgentDef)

Aggregations

RngDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.RngDef)2 WatchDogDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.WatchDogDef)2 LibvirtDiskDef (com.cloud.hypervisor.kvm.resource.xml.LibvirtDiskDef)2 MetadataTO (com.cloud.agent.api.to.MetadataTO)1 ClockDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.ClockDef)1 ConsoleDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.ConsoleDef)1 CpuModeDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.CpuModeDef)1 CpuTuneDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.CpuTuneDef)1 DevicesDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.DevicesDef)1 FeaturesDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.FeaturesDef)1 GraphicDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.GraphicDef)1 GuestDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.GuestDef)1 GuestResourceDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.GuestResourceDef)1 InputDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.InputDef)1 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.InterfaceDef)1 MetadataDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.MetadataDef)1 QemuGuestAgentDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.QemuGuestAgentDef)1 ScsiDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.ScsiDef)1 SerialDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.SerialDef)1 TermPolicy (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.TermPolicy)1