Search in sources :

Example 6 with KVMPhysicalDisk

use of com.cloud.agent.storage.KVMPhysicalDisk in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method execute.

private CopyVolumeAnswer execute(CopyVolumeCommand cmd) {
    boolean copyToSecondary = cmd.toSecondaryStorage();
    String volumePath = cmd.getVolumePath();
    StorageFilerTO pool = cmd.getPool();
    String secondaryStorageUrl = cmd.getSecondaryStorageURL();
    KVMStoragePool secondaryStoragePool = null;
    try {
        KVMStoragePool primaryPool = _storagePoolMgr.getStoragePool(pool.getUuid());
        String volumeName = UUID.randomUUID().toString();
        if (copyToSecondary) {
            String destVolumeName = volumeName + ".qcow2";
            KVMPhysicalDisk volume = primaryPool.getPhysicalDisk(cmd.getVolumePath());
            String volumeDestPath = "/volumes/" + cmd.getVolumeId() + File.separator;
            secondaryStoragePool = _storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl);
            secondaryStoragePool.createFolder(volumeDestPath);
            secondaryStoragePool.delete();
            secondaryStoragePool = _storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl + volumeDestPath);
            _storagePoolMgr.copyPhysicalDisk(volume, destVolumeName, secondaryStoragePool);
            return new CopyVolumeAnswer(cmd, true, null, null, volumeName);
        } else {
            volumePath = "/volumes/" + cmd.getVolumeId() + File.separator;
            secondaryStoragePool = _storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl + volumePath);
            KVMPhysicalDisk volume = secondaryStoragePool.getPhysicalDisk(cmd.getVolumePath() + ".qcow2");
            _storagePoolMgr.copyPhysicalDisk(volume, volumeName, primaryPool);
            return new CopyVolumeAnswer(cmd, true, null, null, volumeName);
        }
    } catch (CloudRuntimeException e) {
        return new CopyVolumeAnswer(cmd, false, e.toString(), null, null);
    } finally {
        if (secondaryStoragePool != null) {
            secondaryStoragePool.delete();
        }
    }
}
Also used : KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CopyVolumeAnswer(com.cloud.agent.api.storage.CopyVolumeAnswer) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO)

Example 7 with KVMPhysicalDisk

use of com.cloud.agent.storage.KVMPhysicalDisk in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method execute.

protected CreateVolumeFromSnapshotAnswer execute(final CreateVolumeFromSnapshotCommand cmd) {
    try {
        String snapshotPath = cmd.getSnapshotUuid();
        int index = snapshotPath.lastIndexOf("/");
        snapshotPath = snapshotPath.substring(0, index);
        KVMStoragePool secondaryPool = _storagePoolMgr.getStoragePoolByURI(cmd.getSecondaryStorageUrl() + snapshotPath);
        KVMPhysicalDisk snapshot = secondaryPool.getPhysicalDisk(cmd.getSnapshotName());
        String primaryUuid = cmd.getPrimaryStoragePoolNameLabel();
        KVMStoragePool primaryPool = _storagePoolMgr.getStoragePool(primaryUuid);
        String volUuid = UUID.randomUUID().toString();
        KVMPhysicalDisk disk = _storagePoolMgr.copyPhysicalDisk(snapshot, volUuid, primaryPool);
        return new CreateVolumeFromSnapshotAnswer(cmd, true, "", disk.getName());
    } catch (CloudRuntimeException e) {
        return new CreateVolumeFromSnapshotAnswer(cmd, false, e.toString(), null);
    }
}
Also used : KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CreateVolumeFromSnapshotAnswer(com.cloud.agent.api.CreateVolumeFromSnapshotAnswer)

Example 8 with KVMPhysicalDisk

use of com.cloud.agent.storage.KVMPhysicalDisk in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method execute.

protected BackupSnapshotAnswer execute(final BackupSnapshotCommand cmd) {
    Long dcId = cmd.getDataCenterId();
    Long accountId = cmd.getAccountId();
    Long volumeId = cmd.getVolumeId();
    String secondaryStoragePoolUrl = cmd.getSecondaryStorageUrl();
    String snapshotName = cmd.getSnapshotName();
    String snapshotPath = cmd.getVolumePath();
    String snapshotDestPath = null;
    String snapshotRelPath = null;
    String vmName = cmd.getVmName();
    KVMStoragePool secondaryStoragePool = null;
    try {
        Connect conn = LibvirtConnection.getConnection();
        secondaryStoragePool = _storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl);
        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;
        KVMStoragePool primaryPool = _storagePoolMgr.getStoragePool(cmd.getPrimaryStoragePoolNameLabel());
        KVMPhysicalDisk snapshotDisk = primaryPool.getPhysicalDisk(cmd.getVolumePath());
        Script command = new Script(_manageSnapshotPath, _cmdsTimeout, s_logger);
        command.add("-b", snapshotDisk.getPath());
        command.add("-n", snapshotName);
        command.add("-p", snapshotDestPath);
        command.add("-t", snapshotName);
        String result = command.execute();
        if (result != null) {
            s_logger.debug("Failed to backup snaptshot: " + result);
            return new BackupSnapshotAnswer(cmd, false, result, null, true);
        }
        /* Delete the snapshot on primary */
        DomainInfo.DomainState state = null;
        Domain vm = null;
        if (vmName != null) {
            try {
                vm = getDomain(conn, cmd.getVmName());
                state = vm.getInfo().state;
            } catch (LibvirtException e) {
            }
        }
        KVMStoragePool primaryStorage = _storagePoolMgr.getStoragePool(cmd.getPool().getUuid());
        if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING && !primaryStorage.isExternalSnapshot()) {
            String vmUuid = vm.getUUIDString();
            Object[] args = new Object[] { snapshotName, vmUuid };
            String snapshot = SnapshotXML.format(args);
            s_logger.debug(snapshot);
            DomainSnapshot snap = vm.snapshotLookupByName(snapshotName);
            snap.delete(0);
            /*
				 * libvirt on RHEL6 doesn't handle resume event emitted from
				 * qemu
				 */
            vm = getDomain(conn, cmd.getVmName());
            state = vm.getInfo().state;
            if (state == DomainInfo.DomainState.VIR_DOMAIN_PAUSED) {
                vm.resume();
            }
        } else {
            command = new Script(_manageSnapshotPath, _cmdsTimeout, s_logger);
            command.add("-d", snapshotDisk.getPath());
            command.add("-n", snapshotName);
            result = command.execute();
            if (result != null) {
                s_logger.debug("Failed to backup snapshot: " + result);
                return new BackupSnapshotAnswer(cmd, false, "Failed to backup snapshot: " + result, null, true);
            }
        }
    } catch (LibvirtException e) {
        return new BackupSnapshotAnswer(cmd, false, e.toString(), null, true);
    } catch (CloudRuntimeException e) {
        return new BackupSnapshotAnswer(cmd, false, e.toString(), null, true);
    } finally {
        if (secondaryStoragePool != null) {
            secondaryStoragePool.delete();
        }
    }
    return new BackupSnapshotAnswer(cmd, true, null, snapshotRelPath + File.separator + snapshotName, true);
}
Also used : Script(com.cloud.utils.script.Script) LibvirtException(org.libvirt.LibvirtException) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk) Connect(org.libvirt.Connect) DomainSnapshot(org.libvirt.DomainSnapshot) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) BackupSnapshotAnswer(com.cloud.agent.api.BackupSnapshotAnswer) DomainInfo(org.libvirt.DomainInfo) Domain(org.libvirt.Domain)

Example 9 with KVMPhysicalDisk

use of com.cloud.agent.storage.KVMPhysicalDisk in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method createPatchVbd.

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

Example 10 with KVMPhysicalDisk

use of com.cloud.agent.storage.KVMPhysicalDisk in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method execute.

protected CreatePrivateTemplateAnswer execute(final CreatePrivateTemplateFromSnapshotCommand cmd) {
    String templateFolder = cmd.getAccountId() + File.separator + cmd.getNewTemplateId();
    String templateInstallFolder = "template/tmpl/" + templateFolder;
    String tmplName = UUID.randomUUID().toString();
    String tmplFileName = tmplName + ".qcow2";
    KVMStoragePool secondaryPool = null;
    KVMStoragePool snapshotPool = null;
    try {
        String snapshotPath = cmd.getSnapshotUuid();
        int index = snapshotPath.lastIndexOf("/");
        snapshotPath = snapshotPath.substring(0, index);
        snapshotPool = _storagePoolMgr.getStoragePoolByURI(cmd.getSecondaryStorageUrl() + snapshotPath);
        KVMPhysicalDisk snapshot = snapshotPool.getPhysicalDisk(cmd.getSnapshotName());
        secondaryPool = _storagePoolMgr.getStoragePoolByURI(cmd.getSecondaryStorageUrl());
        String templatePath = secondaryPool.getLocalPath() + File.separator + templateInstallFolder;
        _storage.mkdirs(templatePath);
        String tmplPath = templateInstallFolder + File.separator + tmplFileName;
        Script command = new Script(_createTmplPath, _cmdsTimeout, s_logger);
        command.add("-t", templatePath);
        command.add("-n", tmplFileName);
        command.add("-f", snapshot.getPath());
        command.execute();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(StorageLayer.InstanceConfigKey, _storage);
        Processor qcow2Processor = new QCOW2Processor();
        qcow2Processor.configure("QCOW2 Processor", params);
        FormatInfo info = qcow2Processor.process(templatePath, null, tmplName);
        TemplateLocation loc = new TemplateLocation(_storage, templatePath);
        loc.create(1, true, tmplName);
        loc.addFormat(info);
        loc.save();
        return new CreatePrivateTemplateAnswer(cmd, true, "", tmplPath, info.virtualSize, info.size, tmplName, info.format);
    } catch (ConfigurationException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.getMessage());
    } catch (InternalErrorException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.getMessage());
    } catch (IOException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.getMessage());
    } catch (CloudRuntimeException e) {
        return new CreatePrivateTemplateAnswer(cmd, false, e.getMessage());
    } finally {
        if (secondaryPool != null) {
            secondaryPool.delete();
        }
        if (snapshotPool != null) {
            snapshotPool.delete();
        }
    }
}
Also used : Script(com.cloud.utils.script.Script) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) Processor(com.cloud.storage.template.Processor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk) InternalErrorException(com.cloud.exception.InternalErrorException) IOException(java.io.IOException) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) KVMStoragePool(com.cloud.agent.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)

Aggregations

KVMPhysicalDisk (com.cloud.agent.storage.KVMPhysicalDisk)13 KVMStoragePool (com.cloud.agent.storage.KVMStoragePool)13 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)7 InternalErrorException (com.cloud.exception.InternalErrorException)4 Script (com.cloud.utils.script.Script)4 Connect (org.libvirt.Connect)4 LibvirtException (org.libvirt.LibvirtException)4 CreatePrivateTemplateAnswer (com.cloud.agent.api.storage.CreatePrivateTemplateAnswer)3 VolumeTO (com.cloud.agent.api.to.VolumeTO)3 DiskDef (com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef)3 AttachVolumeAnswer (com.cloud.agent.api.AttachVolumeAnswer)2 BackupSnapshotAnswer (com.cloud.agent.api.BackupSnapshotAnswer)2 CreateVolumeFromSnapshotAnswer (com.cloud.agent.api.CreateVolumeFromSnapshotAnswer)2 ManageSnapshotAnswer (com.cloud.agent.api.ManageSnapshotAnswer)2 CopyVolumeAnswer (com.cloud.agent.api.storage.CopyVolumeAnswer)2 PrimaryStorageDownloadAnswer (com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer)2 StorageFilerTO (com.cloud.agent.api.to.StorageFilerTO)2 Domain (org.libvirt.Domain)2 DomainInfo (org.libvirt.DomainInfo)2 DomainSnapshot (org.libvirt.DomainSnapshot)2