Search in sources :

Example 81 with Connect

use of org.libvirt.Connect in project cloudstack by apache.

the class LibvirtComputingResource method configure.

@Override
public boolean configure(final String name, final Map<String, Object> params) throws ConfigurationException {
    boolean success = super.configure(name, params);
    if (!success) {
        return false;
    }
    _storage = new JavaStorageLayer();
    _storage.configure("StorageLayer", params);
    String domrScriptsDir = (String) params.get("domr.scripts.dir");
    if (domrScriptsDir == null) {
        domrScriptsDir = getDefaultDomrScriptsDir();
    }
    String hypervisorScriptsDir = (String) params.get("hypervisor.scripts.dir");
    if (hypervisorScriptsDir == null) {
        hypervisorScriptsDir = getDefaultHypervisorScriptsDir();
    }
    String kvmScriptsDir = (String) params.get("kvm.scripts.dir");
    if (kvmScriptsDir == null) {
        kvmScriptsDir = getDefaultKvmScriptsDir();
    }
    String networkScriptsDir = (String) params.get("network.scripts.dir");
    if (networkScriptsDir == null) {
        networkScriptsDir = getDefaultNetworkScriptsDir();
    }
    String storageScriptsDir = (String) params.get("storage.scripts.dir");
    if (storageScriptsDir == null) {
        storageScriptsDir = getDefaultStorageScriptsDir();
    }
    final String bridgeType = (String) params.get("network.bridge.type");
    if (bridgeType == null) {
        _bridgeType = BridgeType.NATIVE;
    } else {
        _bridgeType = BridgeType.valueOf(bridgeType.toUpperCase());
    }
    params.put("domr.scripts.dir", domrScriptsDir);
    _virtRouterResource = new VirtualRoutingResource(this);
    success = _virtRouterResource.configure(name, params);
    if (!success) {
        return false;
    }
    _host = (String) params.get("host");
    if (_host == null) {
        _host = "localhost";
    }
    _dcId = (String) params.get("zone");
    if (_dcId == null) {
        _dcId = "default";
    }
    _pod = (String) params.get("pod");
    if (_pod == null) {
        _pod = "default";
    }
    _clusterId = (String) params.get("cluster");
    _updateHostPasswdPath = Script.findScript(hypervisorScriptsDir, VRScripts.UPDATE_HOST_PASSWD);
    if (_updateHostPasswdPath == null) {
        throw new ConfigurationException("Unable to find update_host_passwd.sh");
    }
    _modifyVlanPath = Script.findScript(networkScriptsDir, "modifyvlan.sh");
    if (_modifyVlanPath == null) {
        throw new ConfigurationException("Unable to find modifyvlan.sh");
    }
    _versionstringpath = Script.findScript(kvmScriptsDir, "versions.sh");
    if (_versionstringpath == null) {
        throw new ConfigurationException("Unable to find versions.sh");
    }
    _patchViaSocketPath = Script.findScript(kvmScriptsDir + "/patch/", "patchviasocket.py");
    if (_patchViaSocketPath == null) {
        throw new ConfigurationException("Unable to find patchviasocket.py");
    }
    _heartBeatPath = Script.findScript(kvmScriptsDir, "kvmheartbeat.sh");
    if (_heartBeatPath == null) {
        throw new ConfigurationException("Unable to find kvmheartbeat.sh");
    }
    _createvmPath = Script.findScript(storageScriptsDir, "createvm.sh");
    if (_createvmPath == null) {
        throw new ConfigurationException("Unable to find the createvm.sh");
    }
    _manageSnapshotPath = Script.findScript(storageScriptsDir, "managesnapshot.sh");
    if (_manageSnapshotPath == null) {
        throw new ConfigurationException("Unable to find the managesnapshot.sh");
    }
    _resizeVolumePath = Script.findScript(storageScriptsDir, "resizevolume.sh");
    if (_resizeVolumePath == null) {
        throw new ConfigurationException("Unable to find the resizevolume.sh");
    }
    _createTmplPath = Script.findScript(storageScriptsDir, "createtmplt.sh");
    if (_createTmplPath == null) {
        throw new ConfigurationException("Unable to find the createtmplt.sh");
    }
    _securityGroupPath = Script.findScript(networkScriptsDir, "security_group.py");
    if (_securityGroupPath == null) {
        throw new ConfigurationException("Unable to find the security_group.py");
    }
    _ovsTunnelPath = Script.findScript(networkScriptsDir, "ovstunnel.py");
    if (_ovsTunnelPath == null) {
        throw new ConfigurationException("Unable to find the ovstunnel.py");
    }
    _routerProxyPath = Script.findScript("scripts/network/domr/", "router_proxy.sh");
    if (_routerProxyPath == null) {
        throw new ConfigurationException("Unable to find the router_proxy.sh");
    }
    _ovsPvlanDhcpHostPath = Script.findScript(networkScriptsDir, "ovs-pvlan-dhcp-host.sh");
    if (_ovsPvlanDhcpHostPath == null) {
        throw new ConfigurationException("Unable to find the ovs-pvlan-dhcp-host.sh");
    }
    _ovsPvlanVmPath = Script.findScript(networkScriptsDir, "ovs-pvlan-vm.sh");
    if (_ovsPvlanVmPath == null) {
        throw new ConfigurationException("Unable to find the ovs-pvlan-vm.sh");
    }
    String value = (String) params.get("developer");
    final boolean isDeveloper = Boolean.parseBoolean(value);
    if (isDeveloper) {
        params.putAll(getDeveloperProperties());
    }
    _pool = (String) params.get("pool");
    if (_pool == null) {
        _pool = "/root";
    }
    final String instance = (String) params.get("instance");
    _hypervisorType = HypervisorType.getType((String) params.get("hypervisor.type"));
    if (_hypervisorType == HypervisorType.None) {
        _hypervisorType = HypervisorType.KVM;
    }
    _hypervisorURI = (String) params.get("hypervisor.uri");
    if (_hypervisorURI == null) {
        _hypervisorURI = LibvirtConnection.getHypervisorURI(_hypervisorType.toString());
    }
    _networkDirectSourceMode = (String) params.get("network.direct.source.mode");
    _networkDirectDevice = (String) params.get("network.direct.device");
    String startMac = (String) params.get("private.macaddr.start");
    if (startMac == null) {
        startMac = "00:16:3e:77:e2:a0";
    }
    String startIp = (String) params.get("private.ipaddr.start");
    if (startIp == null) {
        startIp = "192.168.166.128";
    }
    _pingTestPath = Script.findScript(kvmScriptsDir, "pingtest.sh");
    if (_pingTestPath == null) {
        throw new ConfigurationException("Unable to find the pingtest.sh");
    }
    _linkLocalBridgeName = (String) params.get("private.bridge.name");
    if (_linkLocalBridgeName == null) {
        if (isDeveloper) {
            _linkLocalBridgeName = "cloud-" + instance + "-0";
        } else {
            _linkLocalBridgeName = "cloud0";
        }
    }
    _publicBridgeName = (String) params.get("public.network.device");
    if (_publicBridgeName == null) {
        _publicBridgeName = "cloudbr0";
    }
    _privBridgeName = (String) params.get("private.network.device");
    if (_privBridgeName == null) {
        _privBridgeName = "cloudbr1";
    }
    _guestBridgeName = (String) params.get("guest.network.device");
    if (_guestBridgeName == null) {
        _guestBridgeName = _privBridgeName;
    }
    _privNwName = (String) params.get("private.network.name");
    if (_privNwName == null) {
        if (isDeveloper) {
            _privNwName = "cloud-" + instance + "-private";
        } else {
            _privNwName = "cloud-private";
        }
    }
    _localStoragePath = (String) params.get("local.storage.path");
    if (_localStoragePath == null) {
        _localStoragePath = "/var/lib/libvirt/images/";
    }
    /* Directory to use for Qemu sockets like for the Qemu Guest Agent */
    _qemuSocketsPath = new File("/var/lib/libvirt/qemu");
    String _qemuSocketsPathVar = (String) params.get("qemu.sockets.path");
    if (_qemuSocketsPathVar != null && StringUtils.isNotBlank(_qemuSocketsPathVar)) {
        _qemuSocketsPath = new File(_qemuSocketsPathVar);
    }
    final File storagePath = new File(_localStoragePath);
    _localStoragePath = storagePath.getAbsolutePath();
    _localStorageUUID = (String) params.get("local.storage.uuid");
    if (_localStorageUUID == null) {
        _localStorageUUID = UUID.randomUUID().toString();
    }
    value = (String) params.get("scripts.timeout");
    _timeout = Duration.standardSeconds(NumbersUtil.parseInt(value, 30 * 60));
    value = (String) params.get("stop.script.timeout");
    _stopTimeout = NumbersUtil.parseInt(value, 120) * 1000;
    value = (String) params.get("cmds.timeout");
    _cmdsTimeout = NumbersUtil.parseInt(value, 7200) * 1000;
    value = (String) params.get("vm.memballoon.disable");
    if (Boolean.parseBoolean(value)) {
        _noMemBalloon = true;
    }
    _videoHw = (String) params.get("vm.video.hardware");
    value = (String) params.get("vm.video.ram");
    _videoRam = NumbersUtil.parseInt(value, 0);
    value = (String) params.get("host.reserved.mem.mb");
    // Reserve 1GB unless admin overrides
    _dom0MinMem = NumbersUtil.parseInt(value, 1024) * 1024 * 1024L;
    value = (String) params.get("kvmclock.disable");
    if (Boolean.parseBoolean(value)) {
        _noKvmClock = true;
    }
    value = (String) params.get("vm.rng.enable");
    if (Boolean.parseBoolean(value)) {
        _rngEnable = true;
        value = (String) params.get("vm.rng.model");
        if (!Strings.isNullOrEmpty(value)) {
            _rngBackendModel = RngBackendModel.valueOf(value.toUpperCase());
        }
        value = (String) params.get("vm.rng.path");
        if (!Strings.isNullOrEmpty(value)) {
            _rngPath = value;
        }
        value = (String) params.get("vm.rng.rate.bytes");
        _rngRateBytes = NumbersUtil.parseInt(value, new Integer(_rngRateBytes));
        value = (String) params.get("vm.rng.rate.period");
        _rngRatePeriod = NumbersUtil.parseInt(value, new Integer(_rngRatePeriod));
    }
    LibvirtConnection.initialize(_hypervisorURI);
    Connect conn = null;
    try {
        conn = LibvirtConnection.getConnection();
        if (_bridgeType == BridgeType.OPENVSWITCH) {
            if (conn.getLibVirVersion() < 10 * 1000 + 0) {
                throw new ConfigurationException("Libvirt version 0.10.0 required for openvswitch support, but version " + conn.getLibVirVersion() + " detected");
            }
        }
    } catch (final LibvirtException e) {
        throw new CloudRuntimeException(e.getMessage());
    }
    if (HypervisorType.KVM == _hypervisorType) {
        /* Does node support HVM guest? If not, exit */
        if (!IsHVMEnabled(conn)) {
            throw new ConfigurationException("NO HVM support on this machine, please make sure: " + "1. VT/SVM is supported by your CPU, or is enabled in BIOS. " + "2. kvm modules are loaded (kvm, kvm_amd|kvm_intel)");
        }
    }
    _hypervisorPath = getHypervisorPath(conn);
    try {
        _hvVersion = conn.getVersion();
        _hvVersion = _hvVersion % 1000000 / 1000;
        _hypervisorLibvirtVersion = conn.getLibVirVersion();
        _hypervisorQemuVersion = conn.getVersion();
    } catch (final LibvirtException e) {
        s_logger.trace("Ignoring libvirt error.", e);
    }
    _guestCpuMode = (String) params.get("guest.cpu.mode");
    if (_guestCpuMode != null) {
        _guestCpuModel = (String) params.get("guest.cpu.model");
        if (_hypervisorLibvirtVersion < 9 * 1000 + 10) {
            s_logger.warn("Libvirt version 0.9.10 required for guest cpu mode, but version " + prettyVersion(_hypervisorLibvirtVersion) + " detected, so it will be disabled");
            _guestCpuMode = "";
            _guestCpuModel = "";
        }
        params.put("guest.cpu.mode", _guestCpuMode);
        params.put("guest.cpu.model", _guestCpuModel);
    }
    final String cpuFeatures = (String) params.get("guest.cpu.features");
    if (cpuFeatures != null) {
        _cpuFeatures = new ArrayList<String>();
        for (final String feature : cpuFeatures.split(" ")) {
            if (!feature.isEmpty()) {
                _cpuFeatures.add(feature);
            }
        }
    }
    final String[] info = NetUtils.getNetworkParams(_privateNic);
    _monitor = new KVMHAMonitor(null, info[0], _heartBeatPath);
    final Thread ha = new Thread(_monitor);
    ha.start();
    _storagePoolMgr = new KVMStoragePoolManager(_storage, _monitor);
    _sysvmISOPath = (String) params.get("systemvm.iso.path");
    if (_sysvmISOPath == null) {
        final String[] isoPaths = { "/usr/share/cloudstack-common/vms/systemvm.iso" };
        for (final String isoPath : isoPaths) {
            if (_storage.exists(isoPath)) {
                _sysvmISOPath = isoPath;
                break;
            }
        }
        if (_sysvmISOPath == null) {
            s_logger.debug("Can't find system vm ISO");
        }
    }
    switch(_bridgeType) {
        case OPENVSWITCH:
            getOvsPifs();
            break;
        case NATIVE:
        default:
            getPifs();
            break;
    }
    if (_pifs.get("private") == null) {
        s_logger.debug("Failed to get private nic name");
        throw new ConfigurationException("Failed to get private nic name");
    }
    if (_pifs.get("public") == null) {
        s_logger.debug("Failed to get public nic name");
        throw new ConfigurationException("Failed to get public nic name");
    }
    s_logger.debug("Found pif: " + _pifs.get("private") + " on " + _privBridgeName + ", pif: " + _pifs.get("public") + " on " + _publicBridgeName);
    _canBridgeFirewall = canBridgeFirewall(_pifs.get("public"));
    _localGateway = Script.runSimpleBashScript("ip route |grep default|awk '{print $3}'");
    if (_localGateway == null) {
        s_logger.debug("Failed to found the local gateway");
    }
    _mountPoint = (String) params.get("mount.path");
    if (_mountPoint == null) {
        _mountPoint = "/mnt";
    }
    value = (String) params.get("vm.migrate.downtime");
    _migrateDowntime = NumbersUtil.parseInt(value, -1);
    value = (String) params.get("vm.migrate.pauseafter");
    _migratePauseAfter = NumbersUtil.parseInt(value, -1);
    value = (String) params.get("vm.migrate.speed");
    _migrateSpeed = NumbersUtil.parseInt(value, -1);
    if (_migrateSpeed == -1) {
        //get guest network device speed
        _migrateSpeed = 0;
        final String speed = Script.runSimpleBashScript("ethtool " + _pifs.get("public") + " |grep Speed | cut -d \\  -f 2");
        if (speed != null) {
            final String[] tokens = speed.split("M");
            if (tokens.length == 2) {
                try {
                    _migrateSpeed = Integer.parseInt(tokens[0]);
                } catch (final NumberFormatException e) {
                    s_logger.trace("Ignoring migrateSpeed extraction error.", e);
                }
                s_logger.debug("device " + _pifs.get("public") + " has speed: " + String.valueOf(_migrateSpeed));
            }
        }
        params.put("vm.migrate.speed", String.valueOf(_migrateSpeed));
    }
    final Map<String, String> bridges = new HashMap<String, String>();
    bridges.put("linklocal", _linkLocalBridgeName);
    bridges.put("public", _publicBridgeName);
    bridges.put("private", _privBridgeName);
    bridges.put("guest", _guestBridgeName);
    params.put("libvirt.host.bridges", bridges);
    params.put("libvirt.host.pifs", _pifs);
    params.put("libvirt.computing.resource", this);
    params.put("libvirtVersion", _hypervisorLibvirtVersion);
    configureVifDrivers(params);
    configureDiskActivityChecks(params);
    final KVMStorageProcessor storageProcessor = new KVMStorageProcessor(_storagePoolMgr, this);
    storageProcessor.configure(name, params);
    storageHandler = new StorageSubsystemCommandHandlerBase(storageProcessor);
    return true;
}
Also used : KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) LibvirtException(org.libvirt.LibvirtException) KVMStorageProcessor(com.cloud.hypervisor.kvm.storage.KVMStorageProcessor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Connect(org.libvirt.Connect) VirtualRoutingResource(com.cloud.agent.resource.virtualnetwork.VirtualRoutingResource) StorageSubsystemCommandHandlerBase(com.cloud.storage.resource.StorageSubsystemCommandHandlerBase) ConfigurationException(javax.naming.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) JavaStorageLayer(com.cloud.storage.JavaStorageLayer) File(java.io.File)

Example 82 with Connect

use of org.libvirt.Connect in project cloudstack by apache.

the class LibvirtGetVmStatsCommandWrapper method execute.

@Override
public Answer execute(final GetVmStatsCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final List<String> vmNames = command.getVmNames();
    try {
        final HashMap<String, VmStatsEntry> vmStatsNameMap = new HashMap<String, VmStatsEntry>();
        for (final String vmName : vmNames) {
            final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
            final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
            try {
                final VmStatsEntry statEntry = libvirtComputingResource.getVmStat(conn, vmName);
                if (statEntry == null) {
                    continue;
                }
                vmStatsNameMap.put(vmName, statEntry);
            } catch (LibvirtException e) {
                s_logger.warn("Can't get vm stats: " + e.toString() + ", continue");
            }
        }
        return new GetVmStatsAnswer(command, vmStatsNameMap);
    } catch (final LibvirtException e) {
        s_logger.debug("Can't get vm stats: " + e.toString());
        return new GetVmStatsAnswer(command, null);
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) HashMap(java.util.HashMap) Connect(org.libvirt.Connect) VmStatsEntry(com.cloud.agent.api.VmStatsEntry) GetVmStatsAnswer(com.cloud.agent.api.GetVmStatsAnswer)

Example 83 with Connect

use of org.libvirt.Connect in project cloudstack by apache.

the class LibvirtManageSnapshotCommandWrapper method execute.

@Override
public Answer execute(final ManageSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final String snapshotName = command.getSnapshotName();
    final String snapshotPath = command.getSnapshotPath();
    final String vmName = command.getVmName();
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
        DomainState state = null;
        Domain vm = null;
        if (vmName != null) {
            try {
                vm = libvirtComputingResource.getDomain(conn, command.getVmName());
                state = vm.getInfo().state;
            } catch (final LibvirtException e) {
                s_logger.trace("Ignoring libvirt error.", e);
            }
        }
        final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
        final StorageFilerTO pool = command.getPool();
        final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid());
        final KVMPhysicalDisk disk = primaryPool.getPhysicalDisk(command.getVolumePath());
        if (state == DomainState.VIR_DOMAIN_RUNNING && !primaryPool.isExternalSnapshot()) {
            final MessageFormat snapshotXML = new MessageFormat("   <domainsnapshot>" + "       <name>{0}</name>" + "          <domain>" + "            <uuid>{1}</uuid>" + "        </domain>" + "    </domainsnapshot>");
            final String vmUuid = vm.getUUIDString();
            final Object[] args = new Object[] { snapshotName, vmUuid };
            final String snapshot = snapshotXML.format(args);
            s_logger.debug(snapshot);
            if (command.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) {
                vm.snapshotCreateXML(snapshot);
            } else {
                final DomainSnapshot snap = vm.snapshotLookupByName(snapshotName);
                snap.delete(0);
            }
            /*
                 * libvirt on RHEL6 doesn't handle resume event emitted from
                 * qemu
                 */
            vm = libvirtComputingResource.getDomain(conn, command.getVmName());
            state = vm.getInfo().state;
            if (state == 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,
                 * but that's not possible with RBD since there is no way to store the memory
                 * contents in RBD.
                 *
                 * So we rely on the Java bindings for RBD to create our snapshot
                 *
                 * This snapshot might not be 100% consistent due to writes still being in the
                 * memory of the Virtual Machine, but if the VM runs a kernel which supports
                 * barriers properly (>2.6.32) this won't be any different then pulling the power
                 * cord out of a running machine.
                 */
            if (primaryPool.getType() == StoragePoolType.RBD) {
                try {
                    final Rados r = new Rados(primaryPool.getAuthUserName());
                    r.confSet("mon_host", primaryPool.getSourceHost() + ":" + primaryPool.getSourcePort());
                    r.confSet("key", primaryPool.getAuthSecret());
                    r.confSet("client_mount_timeout", "30");
                    r.connect();
                    s_logger.debug("Succesfully connected to Ceph cluster at " + r.confGet("mon_host"));
                    final IoCTX io = r.ioCtxCreate(primaryPool.getSourceDir());
                    final Rbd rbd = new Rbd(io);
                    final RbdImage image = rbd.open(disk.getName());
                    if (command.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) {
                        s_logger.debug("Attempting to create RBD snapshot " + disk.getName() + "@" + snapshotName);
                        image.snapCreate(snapshotName);
                    } else {
                        s_logger.debug("Attempting to remove RBD snapshot " + disk.getName() + "@" + snapshotName);
                        image.snapRemove(snapshotName);
                    }
                    rbd.close(image);
                    r.ioCtxDestroy(io);
                } catch (final Exception e) {
                    s_logger.error("A RBD snapshot operation on " + disk.getName() + " failed. The error was: " + e.getMessage());
                }
            } else {
                /* VM is not running, create a snapshot by ourself */
                final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout();
                final String manageSnapshotPath = libvirtComputingResource.manageSnapshotPath();
                final Script scriptCommand = new Script(manageSnapshotPath, cmdsTimeout, s_logger);
                if (command.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) {
                    scriptCommand.add("-c", disk.getPath());
                } else {
                    scriptCommand.add("-d", snapshotPath);
                }
                scriptCommand.add("-n", snapshotName);
                final String result = scriptCommand.execute();
                if (result != null) {
                    s_logger.debug("Failed to manage snapshot: " + result);
                    return new ManageSnapshotAnswer(command, false, "Failed to manage snapshot: " + result);
                }
            }
        }
        return new ManageSnapshotAnswer(command, command.getSnapshotId(), disk.getPath() + File.separator + snapshotName, true, null);
    } catch (final LibvirtException e) {
        s_logger.debug("Failed to manage snapshot: " + e.toString());
        return new ManageSnapshotAnswer(command, false, "Failed to manage snapshot: " + e.toString());
    }
}
Also used : Script(com.cloud.utils.script.Script) KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) LibvirtException(org.libvirt.LibvirtException) MessageFormat(java.text.MessageFormat) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) Connect(org.libvirt.Connect) DomainSnapshot(org.libvirt.DomainSnapshot) Rados(com.ceph.rados.Rados) ManageSnapshotAnswer(com.cloud.agent.api.ManageSnapshotAnswer) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) LibvirtException(org.libvirt.LibvirtException) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) DomainState(org.libvirt.DomainInfo.DomainState) Rbd(com.ceph.rbd.Rbd) RbdImage(com.ceph.rbd.RbdImage) IoCTX(com.ceph.rados.IoCTX) Domain(org.libvirt.Domain)

Example 84 with Connect

use of org.libvirt.Connect in project cloudstack by apache.

the class LibvirtMigrateCommandWrapper method execute.

@Override
public Answer execute(final MigrateCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final String vmName = command.getVmName();
    String result = null;
    List<InterfaceDef> ifaces = null;
    List<DiskDef> disks = null;
    Domain dm = null;
    Connect dconn = null;
    Domain destDomain = null;
    Connect conn = null;
    String xmlDesc = null;
    List<Ternary<String, Boolean, String>> vmsnapshots = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
        ifaces = libvirtComputingResource.getInterfaces(conn, vmName);
        disks = libvirtComputingResource.getDisks(conn, vmName);
        dm = conn.domainLookupByName(vmName);
        /*
                We replace the private IP address with the address of the destination host.
                This is because the VNC listens on the private IP address of the hypervisor,
                but that address is ofcourse different on the target host.

                MigrateCommand.getDestinationIp() returns the private IP address of the target
                hypervisor. So it's safe to use.

                The Domain.migrate method from libvirt supports passing a different XML
                description for the instance to be used on the target host.

                This is supported by libvirt-java from version 0.50.0

                CVE-2015-3252: Get XML with sensitive information suitable for migration by using
                               VIR_DOMAIN_XML_MIGRATABLE flag (value = 8)
                               https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainXMLFlags

                               Use VIR_DOMAIN_XML_SECURE (value = 1) prior to v1.0.0.
             */
        // 1000000 equals v1.0.0
        final int xmlFlag = conn.getLibVirVersion() >= 1000000 ? 8 : 1;
        final String target = command.getDestinationIp();
        xmlDesc = dm.getXMLDesc(xmlFlag);
        xmlDesc = replaceIpForVNCInDescFile(xmlDesc, target);
        // delete the metadata of vm snapshots before migration
        vmsnapshots = libvirtComputingResource.cleanVMSnapshotMetadata(dm);
        dconn = libvirtUtilitiesHelper.retrieveQemuConnection("qemu+tcp://" + command.getDestinationIp() + "/system");
        //run migration in thread so we can monitor it
        s_logger.info("Live migration of instance " + vmName + " initiated");
        final ExecutorService executor = Executors.newFixedThreadPool(1);
        final Callable<Domain> worker = new MigrateKVMAsync(libvirtComputingResource, dm, dconn, xmlDesc, vmName, command.getDestinationIp());
        final Future<Domain> migrateThread = executor.submit(worker);
        executor.shutdown();
        long sleeptime = 0;
        while (!executor.isTerminated()) {
            Thread.sleep(100);
            sleeptime += 100;
            if (sleeptime == 1000) {
                // wait 1s before attempting to set downtime on migration, since I don't know of a VIR_DOMAIN_MIGRATING state
                final int migrateDowntime = libvirtComputingResource.getMigrateDowntime();
                if (migrateDowntime > 0) {
                    try {
                        final int setDowntime = dm.migrateSetMaxDowntime(migrateDowntime);
                        if (setDowntime == 0) {
                            s_logger.debug("Set max downtime for migration of " + vmName + " to " + String.valueOf(migrateDowntime) + "ms");
                        }
                    } catch (final LibvirtException e) {
                        s_logger.debug("Failed to set max downtime for migration, perhaps migration completed? Error: " + e.getMessage());
                    }
                }
            }
            if (sleeptime % 1000 == 0) {
                s_logger.info("Waiting for migration of " + vmName + " to complete, waited " + sleeptime + "ms");
            }
            // pause vm if we meet the vm.migrate.pauseafter threshold and not already paused
            final int migratePauseAfter = libvirtComputingResource.getMigratePauseAfter();
            if (migratePauseAfter > 0 && sleeptime > migratePauseAfter && dm.getInfo().state == DomainState.VIR_DOMAIN_RUNNING) {
                s_logger.info("Pausing VM " + vmName + " due to property vm.migrate.pauseafter setting to " + migratePauseAfter + "ms to complete migration");
                try {
                    dm.suspend();
                } catch (final LibvirtException e) {
                    // pause could be racy if it attempts to pause right when vm is finished, simply warn
                    s_logger.info("Failed to pause vm " + vmName + " : " + e.getMessage());
                }
            }
        }
        s_logger.info("Migration thread for " + vmName + " is done");
        destDomain = migrateThread.get(10, TimeUnit.SECONDS);
        if (destDomain != null) {
            for (final DiskDef disk : disks) {
                libvirtComputingResource.cleanupDisk(disk);
            }
        }
    } catch (final LibvirtException e) {
        s_logger.debug("Can't migrate domain: " + e.getMessage());
        result = e.getMessage();
    } catch (final InterruptedException e) {
        s_logger.debug("Interrupted while migrating domain: " + e.getMessage());
        result = e.getMessage();
    } catch (final ExecutionException e) {
        s_logger.debug("Failed to execute while migrating domain: " + e.getMessage());
        result = e.getMessage();
    } catch (final TimeoutException e) {
        s_logger.debug("Timed out while migrating domain: " + e.getMessage());
        result = e.getMessage();
    } finally {
        try {
            if (dm != null && result != null) {
                // restore vm snapshots in case of failed migration
                if (vmsnapshots != null) {
                    libvirtComputingResource.restoreVMSnapshotMetadata(dm, vmName, vmsnapshots);
                }
            }
            if (dm != null) {
                if (dm.isPersistent() == 1) {
                    dm.undefine();
                }
                dm.free();
            }
            if (dconn != null) {
                dconn.close();
            }
            if (destDomain != null) {
                destDomain.free();
            }
        } catch (final LibvirtException e) {
            s_logger.trace("Ignoring libvirt error.", e);
        }
    }
    if (result != null) {
    } else {
        libvirtComputingResource.destroyNetworkRulesForVM(conn, vmName);
        for (final InterfaceDef iface : ifaces) {
            // We don't know which "traffic type" is associated with
            // each interface at this point, so inform all vif drivers
            final List<VifDriver> allVifDrivers = libvirtComputingResource.getAllVifDrivers();
            for (final VifDriver vifDriver : allVifDrivers) {
                vifDriver.unplug(iface);
            }
        }
    }
    return new MigrateAnswer(command, result == null, result, null);
}
Also used : LibvirtException(org.libvirt.LibvirtException) Ternary(com.cloud.utils.Ternary) Connect(org.libvirt.Connect) VifDriver(com.cloud.hypervisor.kvm.resource.VifDriver) MigrateAnswer(com.cloud.agent.api.MigrateAnswer) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) DiskDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef) MigrateKVMAsync(com.cloud.hypervisor.kvm.resource.MigrateKVMAsync) ExecutorService(java.util.concurrent.ExecutorService) Domain(org.libvirt.Domain) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 85 with Connect

use of org.libvirt.Connect in project cloudstack by apache.

the class LibvirtSecurityGroupRulesCommandWrapper method execute.

@Override
public Answer execute(final SecurityGroupRulesCmd command, final LibvirtComputingResource libvirtComputingResource) {
    String vif = null;
    String brname = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName());
        final List<InterfaceDef> nics = libvirtComputingResource.getInterfaces(conn, command.getVmName());
        vif = nics.get(0).getDevName();
        brname = nics.get(0).getBrName();
    } catch (final LibvirtException e) {
        return new SecurityGroupRuleAnswer(command, false, e.toString());
    }
    final boolean result = libvirtComputingResource.addNetworkRules(command.getVmName(), Long.toString(command.getVmId()), command.getGuestIp(), command.getGuestIp6(), command.getSignature(), Long.toString(command.getSeqNum()), command.getGuestMac(), command.stringifyRules(), vif, brname, command.getSecIpsString());
    if (!result) {
        s_logger.warn("Failed to program network rules for vm " + command.getVmName());
        return new SecurityGroupRuleAnswer(command, false, "programming network rules failed");
    } else {
        s_logger.debug("Programmed network rules for vm " + command.getVmName() + " guestIp=" + command.getGuestIp() + ",ingress numrules=" + command.getIngressRuleSet().size() + ",egress numrules=" + command.getEgressRuleSet().size());
        return new SecurityGroupRuleAnswer(command);
    }
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) SecurityGroupRuleAnswer(com.cloud.agent.api.SecurityGroupRuleAnswer)

Aggregations

Connect (org.libvirt.Connect)113 LibvirtException (org.libvirt.LibvirtException)112 Answer (com.cloud.agent.api.Answer)47 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)42 Test (org.junit.Test)40 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)39 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)39 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)39 InternalErrorException (com.cloud.exception.InternalErrorException)33 Domain (org.libvirt.Domain)30 URISyntaxException (java.net.URISyntaxException)25 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)24 NicTO (com.cloud.agent.api.to.NicTO)23 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)22 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)19 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)16 ConfigurationException (javax.naming.ConfigurationException)15 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)13 HashMap (java.util.HashMap)11