Search in sources :

Example 96 with KvmStoragePoolManager

use of com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager 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;
    }
    try {
        loadUefiProperties();
    } catch (FileNotFoundException e) {
        s_logger.error("uefi properties file not found due to: " + e.getLocalizedMessage());
    }
    _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());
    }
    String dpdk = (String) params.get("openvswitch.dpdk.enabled");
    if (_bridgeType == BridgeType.OPENVSWITCH && Boolean.parseBoolean(dpdk)) {
        dpdkSupport = true;
        dpdkOvsPath = (String) params.get("openvswitch.dpdk.ovs.path");
        if (dpdkOvsPath != null && !dpdkOvsPath.endsWith("/")) {
            dpdkOvsPath += "/";
        }
    }
    directDownloadTemporaryDownloadPath = (String) params.get("direct.download.temporary.download.location");
    if (StringUtils.isBlank(directDownloadTemporaryDownloadPath)) {
        directDownloadTemporaryDownloadPath = getDefaultDirectDownloadTemporaryPath();
    }
    cachePath = (String) params.get(HOST_CACHE_PATH_PARAMETER);
    if (StringUtils.isBlank(cachePath)) {
        cachePath = getDefaultCachePath();
    }
    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");
    }
    _patchScriptPath = Script.findScript(kvmScriptsDir, "patch.sh");
    if (_patchScriptPath == null) {
        throw new ConfigurationException("Unable to find patch.sh");
    }
    _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");
    }
    _vmActivityCheckPath = Script.findScript(kvmScriptsDir, "kvmvmactivity.sh");
    if (_vmActivityCheckPath == null) {
        throw new ConfigurationException("Unable to find kvmvmactivity.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-kvm-dhcp-host.sh");
    if (_ovsPvlanDhcpHostPath == null) {
        throw new ConfigurationException("Unable to find the ovs-pvlan-kvm-dhcp-host.sh");
    }
    _ovsPvlanVmPath = Script.findScript(networkScriptsDir, "ovs-pvlan-kvm-vm.sh");
    if (_ovsPvlanVmPath == null) {
        throw new ConfigurationException("Unable to find the ovs-pvlan-kvm-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;
    }
    String hooksDir = (String) params.get("rolling.maintenance.hooks.dir");
    value = (String) params.get("rolling.maintenance.service.executor.disabled");
    rollingMaintenanceExecutor = Boolean.parseBoolean(value) ? new RollingMaintenanceAgentExecutor(hooksDir) : new RollingMaintenanceServiceExecutor(hooksDir);
    _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("host.overcommit.mem.mb");
    // Support overcommit memory for host if host uses ZSWAP, KSM and other memory
    // compressing technologies
    _dom0OvercommitMem = NumbersUtil.parseInt(value, 0) * 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 (StringUtils.isNotEmpty(value)) {
            _rngBackendModel = RngBackendModel.valueOf(value.toUpperCase());
        }
        value = (String) params.get("vm.rng.path");
        if (StringUtils.isNotEmpty(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));
    }
    value = (String) params.get("vm.watchdog.model");
    if (StringUtils.isNotEmpty(value)) {
        _watchDogModel = WatchDogModel.valueOf(value.toUpperCase());
    }
    value = (String) params.get("vm.watchdog.action");
    if (StringUtils.isNotEmpty(value)) {
        _watchDogAction = WatchDogAction.valueOf(value.toUpperCase());
    }
    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());
    }
    // destroy default network, see https://libvirt.org/sources/java/javadoc/org/libvirt/Network.html
    try {
        Network network = conn.networkLookupByName("default");
        s_logger.debug("Found libvirt default network, destroying it and setting autostart to false");
        if (network.isActive() == 1) {
            network.destroy();
        }
        if (network.getAutostart()) {
            network.setAutostart(false);
        }
    } catch (final LibvirtException e) {
        s_logger.warn("Ignoring libvirt error.", e);
    }
    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);
    }
    final String cpuArchOverride = (String) params.get("guest.cpu.arch");
    if (StringUtils.isNotEmpty(cpuArchOverride)) {
        _guestCpuArch = cpuArchOverride;
        s_logger.info("Using guest CPU architecture: " + _guestCpuArch);
    }
    _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");
        }
    }
    final Map<String, String> bridges = new HashMap<String, String>();
    params.put("libvirt.host.bridges", bridges);
    params.put("libvirt.host.pifs", _pifs);
    params.put("libvirt.computing.resource", this);
    params.put("libvirtVersion", _hypervisorLibvirtVersion);
    configureVifDrivers(params);
    if (_pifs.get("private") == null) {
        s_logger.error("Failed to get private nic name");
        throw new ConfigurationException("Failed to get private nic name");
    }
    if (_pifs.get("public") == null) {
        s_logger.error("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 show default 0.0.0.0/0|head -1|awk '{print $3}'");
    if (_localGateway == null) {
        s_logger.warn("No default IPv4 gateway found");
    }
    _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.wait");
    _migrateWait = NumbersUtil.parseInt(value, -1);
    configureAgentHooks(params);
    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));
    }
    bridges.put("linklocal", _linkLocalBridgeName);
    bridges.put("public", _publicBridgeName);
    bridges.put("private", _privBridgeName);
    bridges.put("guest", _guestBridgeName);
    getVifDriver(TrafficType.Control).createControlNetwork(_linkLocalBridgeName);
    configureDiskActivityChecks(params);
    final KVMStorageProcessor storageProcessor = new KVMStorageProcessor(_storagePoolMgr, this);
    storageProcessor.configure(name, params);
    storageHandler = new StorageSubsystemCommandHandlerBase(storageProcessor);
    Boolean _iscsiCleanUpEnabled = Boolean.parseBoolean((String) params.get("iscsi.session.cleanup.enabled"));
    if (BooleanUtils.isTrue(_iscsiCleanUpEnabled)) {
        IscsiStorageCleanupMonitor isciCleanupMonitor = new IscsiStorageCleanupMonitor();
        final Thread cleanupMonitor = new Thread(isciCleanupMonitor);
        cleanupMonitor.start();
    } else {
        s_logger.info("iscsi session clean up is disabled");
    }
    return true;
}
Also used : LibvirtException(org.libvirt.LibvirtException) KVMStorageProcessor(com.cloud.hypervisor.kvm.storage.KVMStorageProcessor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) FileNotFoundException(java.io.FileNotFoundException) VirtualRoutingResource(com.cloud.agent.resource.virtualnetwork.VirtualRoutingResource) StorageSubsystemCommandHandlerBase(com.cloud.storage.resource.StorageSubsystemCommandHandlerBase) ConfigurationException(javax.naming.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) RollingMaintenanceServiceExecutor(com.cloud.hypervisor.kvm.resource.rolling.maintenance.RollingMaintenanceServiceExecutor) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(org.libvirt.Network) IscsiStorageCleanupMonitor(com.cloud.hypervisor.kvm.storage.IscsiStorageCleanupMonitor) JavaStorageLayer(com.cloud.storage.JavaStorageLayer) KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) Connect(org.libvirt.Connect) RollingMaintenanceAgentExecutor(com.cloud.hypervisor.kvm.resource.rolling.maintenance.RollingMaintenanceAgentExecutor) QemuImgFile(org.apache.cloudstack.utils.qemu.QemuImgFile) File(java.io.File)

Example 97 with KvmStoragePoolManager

use of com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager in project cloudstack by apache.

the class LibvirtCopyVolumeCommandWrapper method handleCopyDataFromVolumeToSecondaryStorageUsingSrcDetails.

private Answer handleCopyDataFromVolumeToSecondaryStorageUsingSrcDetails(CopyVolumeCommand command, LibvirtComputingResource libvirtComputingResource) {
    KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    PrimaryDataStoreTO srcPrimaryDataStore = null;
    KVMStoragePool secondaryStoragePool = null;
    Map<String, String> srcDetails = command.getSrcDetails();
    String srcPath = srcDetails.get(DiskTO.IQN);
    if (srcPath == null) {
        return new CopyVolumeAnswer(command, false, "No IQN was specified", null, null);
    }
    try {
        LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        String destVolumeName = libvirtUtilitiesHelper.generateUUIDName() + ".qcow2";
        String destVolumePath = command.getVolumePath() + File.separator;
        String secondaryStorageUrl = command.getSecondaryStorageURL();
        secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl);
        secondaryStoragePool.createFolder(File.separator + destVolumePath);
        storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
        secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl + File.separator + destVolumePath);
        VolumeObjectTO srcVolumeObjectTO = (VolumeObjectTO) command.getSrcData();
        srcPrimaryDataStore = (PrimaryDataStoreTO) srcVolumeObjectTO.getDataStore();
        storagePoolMgr.connectPhysicalDisk(srcPrimaryDataStore.getPoolType(), srcPrimaryDataStore.getUuid(), srcPath, srcDetails);
        KVMPhysicalDisk srcPhysicalDisk = storagePoolMgr.getPhysicalDisk(srcPrimaryDataStore.getPoolType(), srcPrimaryDataStore.getUuid(), srcPath);
        storagePoolMgr.copyPhysicalDisk(srcPhysicalDisk, destVolumeName, secondaryStoragePool, command.getWait() * 1000);
        return new CopyVolumeAnswer(command, true, null, null, destVolumePath + destVolumeName);
    } catch (final CloudRuntimeException e) {
        return new CopyVolumeAnswer(command, false, e.toString(), null, null);
    } finally {
        try {
            if (srcPrimaryDataStore != null) {
                storagePoolMgr.disconnectPhysicalDisk(srcPrimaryDataStore.getPoolType(), srcPrimaryDataStore.getUuid(), srcPath);
            }
        } catch (Exception e) {
            LOGGER.warn("Unable to disconnect from the source device.", e);
        }
        if (secondaryStoragePool != null) {
            storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
        }
    }
}
Also used : KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CopyVolumeAnswer(com.cloud.agent.api.storage.CopyVolumeAnswer) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 98 with KvmStoragePoolManager

use of com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager in project cloudstack by apache.

the class LibvirtCopyVolumeCommandWrapper method execute.

@Override
public Answer execute(final CopyVolumeCommand command, final LibvirtComputingResource libvirtComputingResource) {
    /**
     * This method is only used for copying files from Primary Storage TO
     * Secondary Storage
     *
     * It COULD also do it the other way around, but the code in the
     * ManagementServerImpl shows that it always sets copyToSecondary to
     * true
     */
    Map<String, String> srcDetails = command.getSrcDetails();
    if (srcDetails != null) {
        return handleCopyDataFromVolumeToSecondaryStorageUsingSrcDetails(command, libvirtComputingResource);
    }
    final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    final boolean copyToSecondary = command.toSecondaryStorage();
    final StorageFilerTO pool = command.getPool();
    final String secondaryStorageUrl = command.getSecondaryStorageURL();
    KVMStoragePool secondaryStoragePool = null;
    String volumePath;
    KVMStoragePool primaryPool;
    try {
        try {
            primaryPool = storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid());
        } catch (final CloudRuntimeException e) {
            if (e.getMessage().contains("not found")) {
                primaryPool = storagePoolMgr.createStoragePool(pool.getUuid(), pool.getHost(), pool.getPort(), pool.getPath(), pool.getUserInfo(), pool.getType());
            } else {
                return new CopyVolumeAnswer(command, false, e.getMessage(), null, null);
            }
        }
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final String volumeName = libvirtUtilitiesHelper.generateUUIDName();
        if (copyToSecondary) {
            final String destVolumeName = volumeName + ".qcow2";
            final KVMPhysicalDisk volume = primaryPool.getPhysicalDisk(command.getVolumePath());
            final String volumeDestPath = "/volumes/" + command.getVolumeId() + File.separator;
            secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl);
            secondaryStoragePool.createFolder(volumeDestPath);
            storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
            secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl + volumeDestPath);
            storagePoolMgr.copyPhysicalDisk(volume, destVolumeName, secondaryStoragePool, 0);
            return new CopyVolumeAnswer(command, true, null, null, volumeName);
        } else {
            volumePath = "/volumes/" + command.getVolumeId() + File.separator;
            secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl + volumePath);
            final KVMPhysicalDisk volume = secondaryStoragePool.getPhysicalDisk(command.getVolumePath() + ".qcow2");
            storagePoolMgr.copyPhysicalDisk(volume, volumeName, primaryPool, 0);
            return new CopyVolumeAnswer(command, true, null, null, volumeName);
        }
    } catch (final CloudRuntimeException e) {
        return new CopyVolumeAnswer(command, false, e.toString(), null, null);
    } finally {
        if (secondaryStoragePool != null) {
            storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
        }
    }
}
Also used : KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) CopyVolumeAnswer(com.cloud.agent.api.storage.CopyVolumeAnswer) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO)

Example 99 with KvmStoragePoolManager

use of com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager in project cloudstack by apache.

the class LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper method execute.

@Override
public Answer execute(final CreatePrivateTemplateFromSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
    final String templateFolder = command.getAccountId() + File.separator + command.getNewTemplateId();
    final String templateInstallFolder = "template/tmpl/" + templateFolder;
    final String tmplName = libvirtUtilitiesHelper.generateUUIDName();
    final String tmplFileName = tmplName + ".qcow2";
    KVMStoragePool secondaryPool = null;
    KVMStoragePool snapshotPool = null;
    final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    try {
        String snapshotPath = command.getSnapshotUuid();
        final int index = snapshotPath.lastIndexOf("/");
        snapshotPath = snapshotPath.substring(0, index);
        snapshotPool = storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl() + snapshotPath);
        secondaryPool = storagePoolMgr.getStoragePoolByURI(command.getSecondaryStorageUrl());
        final KVMPhysicalDisk snapshot = snapshotPool.getPhysicalDisk(command.getSnapshotName());
        final String templatePath = secondaryPool.getLocalPath() + File.separator + templateInstallFolder;
        final StorageLayer storage = libvirtComputingResource.getStorage();
        storage.mkdirs(templatePath);
        final String tmplPath = templateInstallFolder + File.separator + tmplFileName;
        final String createTmplPath = libvirtComputingResource.createTmplPath();
        final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout();
        final Script scriptCommand = new Script(createTmplPath, cmdsTimeout, s_logger);
        scriptCommand.add("-t", templatePath);
        scriptCommand.add("-n", tmplFileName);
        scriptCommand.add("-f", snapshot.getPath());
        scriptCommand.execute();
        final Processor qcow2Processor = libvirtUtilitiesHelper.buildQCOW2Processor(storage);
        final FormatInfo info = qcow2Processor.process(templatePath, null, tmplName);
        final TemplateLocation loc = libvirtUtilitiesHelper.buildTemplateLocation(storage, templatePath);
        loc.create(1, true, tmplName);
        loc.addFormat(info);
        loc.save();
        return new CreatePrivateTemplateAnswer(command, true, "", tmplPath, info.virtualSize, info.size, tmplName, info.format);
    } catch (final ConfigurationException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
    } catch (final InternalErrorException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
    } catch (final IOException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
    } catch (final CloudRuntimeException e) {
        return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
    } finally {
        if (secondaryPool != null) {
            storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid());
        }
        if (snapshotPool != null) {
            storagePoolMgr.deleteStoragePool(snapshotPool.getType(), snapshotPool.getUuid());
        }
    }
}
Also used : Script(com.cloud.utils.script.Script) KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) StorageLayer(com.cloud.storage.StorageLayer) Processor(com.cloud.storage.template.Processor) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) InternalErrorException(com.cloud.exception.InternalErrorException) IOException(java.io.IOException) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) ConfigurationException(javax.naming.ConfigurationException) TemplateLocation(com.cloud.storage.template.TemplateLocation) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CreatePrivateTemplateAnswer(com.cloud.agent.api.storage.CreatePrivateTemplateAnswer) FormatInfo(com.cloud.storage.template.Processor.FormatInfo)

Example 100 with KvmStoragePoolManager

use of com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager in project cloudstack by apache.

the class LibvirtBackupSnapshotCommandWrapper method execute.

@Override
public Answer execute(final BackupSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final Long dcId = command.getDataCenterId();
    final Long accountId = command.getAccountId();
    final Long volumeId = command.getVolumeId();
    final String secondaryStoragePoolUrl = command.getSecondaryStorageUrl();
    final String snapshotName = command.getSnapshotName();
    String snapshotDestPath = null;
    String snapshotRelPath = null;
    final String vmName = command.getVmName();
    KVMStoragePool secondaryStoragePool = null;
    final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
        secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl);
        final String ssPmountPath = secondaryStoragePool.getLocalPath();
        snapshotRelPath = File.separator + "snapshots" + File.separator + dcId + File.separator + accountId + File.separator + volumeId;
        snapshotDestPath = ssPmountPath + File.separator + "snapshots" + File.separator + dcId + File.separator + accountId + File.separator + volumeId;
        final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPrimaryStoragePoolNameLabel());
        final KVMPhysicalDisk snapshotDisk = primaryPool.getPhysicalDisk(command.getVolumePath());
        final String manageSnapshotPath = libvirtComputingResource.manageSnapshotPath();
        final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout();
        /**
         * RBD snapshots can't be copied using qemu-img, so we have to use
         * the Java bindings for librbd here.
         *
         * These bindings will read the snapshot and write the contents to
         * the secondary storage directly
         *
         * It will stop doing so if the amount of time spend is longer then
         * cmds.timeout
         */
        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(snapshotDisk.getName(), snapshotName);
                final File fh = new File(snapshotDestPath);
                try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fh))) {
                    final int chunkSize = 4194304;
                    long offset = 0;
                    s_logger.debug("Backuping up RBD snapshot " + snapshotName + " to  " + snapshotDestPath);
                    while (true) {
                        final byte[] buf = new byte[chunkSize];
                        final int bytes = image.read(offset, buf, chunkSize);
                        if (bytes <= 0) {
                            break;
                        }
                        bos.write(buf, 0, bytes);
                        offset += bytes;
                    }
                    s_logger.debug("Completed backing up RBD snapshot " + snapshotName + " to  " + snapshotDestPath + ". Bytes written: " + toHumanReadableSize(offset));
                } catch (final IOException ex) {
                    s_logger.error("BackupSnapshotAnswer:Exception:" + ex.getMessage());
                }
                r.ioCtxDestroy(io);
            } catch (final RadosException e) {
                s_logger.error("A RADOS operation failed. The error was: " + e.getMessage());
                return new BackupSnapshotAnswer(command, false, e.toString(), null, true);
            } catch (final RbdException e) {
                s_logger.error("A RBD operation on " + snapshotDisk.getName() + " failed. The error was: " + e.getMessage());
                return new BackupSnapshotAnswer(command, false, e.toString(), null, true);
            }
        } else {
            final Script scriptCommand = new Script(manageSnapshotPath, cmdsTimeout, s_logger);
            scriptCommand.add("-b", snapshotDisk.getPath());
            scriptCommand.add("-n", snapshotName);
            scriptCommand.add("-p", snapshotDestPath);
            scriptCommand.add("-t", snapshotName);
            final String result = scriptCommand.execute();
            if (result != null) {
                s_logger.debug("Failed to backup snaptshot: " + result);
                return new BackupSnapshotAnswer(command, false, result, null, true);
            }
        }
        /* Delete the snapshot on primary */
        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 KVMStoragePool primaryStorage = storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPool().getUuid());
        if (state == DomainState.VIR_DOMAIN_RUNNING && !primaryStorage.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);
            final DomainSnapshot snap = vm.snapshotLookupByName(snapshotName);
            if (snap != null) {
                snap.delete(0);
            } else {
                throw new CloudRuntimeException("Unable to find vm snapshot with name -" + snapshotName);
            }
            /*
                 * 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 {
            final Script scriptCommand = new Script(manageSnapshotPath, cmdsTimeout, s_logger);
            scriptCommand.add("-d", snapshotDisk.getPath());
            scriptCommand.add("-n", snapshotName);
            final String result = scriptCommand.execute();
            if (result != null) {
                s_logger.debug("Failed to backup snapshot: " + result);
                return new BackupSnapshotAnswer(command, false, "Failed to backup snapshot: " + result, null, true);
            }
        }
    } catch (final LibvirtException e) {
        return new BackupSnapshotAnswer(command, false, e.toString(), null, true);
    } catch (final CloudRuntimeException e) {
        return new BackupSnapshotAnswer(command, false, e.toString(), null, true);
    } finally {
        if (secondaryStoragePool != null) {
            storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
        }
    }
    return new BackupSnapshotAnswer(command, true, null, snapshotRelPath + File.separator + snapshotName, true);
}
Also used : LibvirtException(org.libvirt.LibvirtException) RadosException(com.ceph.rados.exceptions.RadosException) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) Rbd(com.ceph.rbd.Rbd) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) BufferedOutputStream(java.io.BufferedOutputStream) Script(com.cloud.utils.script.Script) KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) MessageFormat(java.text.MessageFormat) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) Connect(org.libvirt.Connect) Rados(com.ceph.rados.Rados) DomainSnapshot(org.libvirt.DomainSnapshot) IOException(java.io.IOException) DomainState(org.libvirt.DomainInfo.DomainState) FileOutputStream(java.io.FileOutputStream) RbdImage(com.ceph.rbd.RbdImage) IoCTX(com.ceph.rados.IoCTX) BackupSnapshotAnswer(com.cloud.agent.api.BackupSnapshotAnswer) Domain(org.libvirt.Domain) File(java.io.File) RbdException(com.ceph.rbd.RbdException)

Aggregations

Answer (com.cloud.agent.api.Answer)100 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)89 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)89 Test (org.junit.Test)89 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)70 KvmStoragePoolManager (com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager)61 KVMStoragePool (com.cloud.hypervisor.kvm.storage.KVMStoragePool)51 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)49 StoragePool (com.cloud.storage.StoragePool)48 UnsupportedAnswer (com.cloud.agent.api.UnsupportedAnswer)45 KvmStoragePool (com.cloud.hypervisor.kvm.storage.KvmStoragePool)45 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)45 LibvirtException (org.libvirt.LibvirtException)45 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)45 AttachAnswer (com.cloud.storage.command.AttachAnswer)44 StorageFilerTO (com.cloud.agent.api.to.StorageFilerTO)43 Connect (org.libvirt.Connect)37 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)36 KVMPhysicalDisk (com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk)35 KvmPhysicalDisk (com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk)31