Search in sources :

Example 11 with KVMStoragePool

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

the class LibvirtComputingResource method execute.

protected PrimaryStorageDownloadAnswer execute(final PrimaryStorageDownloadCommand cmd) {
    String tmplturl = cmd.getUrl();
    int index = tmplturl.lastIndexOf("/");
    String mountpoint = tmplturl.substring(0, index);
    String tmpltname = null;
    if (index < tmplturl.length() - 1) {
        tmpltname = tmplturl.substring(index + 1);
    }
    KVMPhysicalDisk tmplVol = null;
    KVMStoragePool secondaryPool = null;
    try {
        secondaryPool = _storagePoolMgr.getStoragePoolByURI(mountpoint);
        /* Get template vol */
        if (tmpltname == null) {
            secondaryPool.refresh();
            List<KVMPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
            if (disks == null || disks.isEmpty()) {
                return new PrimaryStorageDownloadAnswer("Failed to get volumes from pool: " + secondaryPool.getUuid());
            }
            for (KVMPhysicalDisk disk : disks) {
                if (disk.getName().endsWith("qcow2")) {
                    tmplVol = disk;
                    break;
                }
            }
            if (tmplVol == null) {
                return new PrimaryStorageDownloadAnswer("Failed to get template from pool: " + secondaryPool.getUuid());
            }
        } else {
            tmplVol = secondaryPool.getPhysicalDisk(tmpltname);
        }
        /* Copy volume to primary storage */
        KVMStoragePool primaryPool = _storagePoolMgr.getStoragePool(cmd.getPoolUuid());
        KVMPhysicalDisk primaryVol = _storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool);
        return new PrimaryStorageDownloadAnswer(primaryVol.getName(), primaryVol.getSize());
    } catch (CloudRuntimeException e) {
        return new PrimaryStorageDownloadAnswer(e.toString());
    } finally {
        if (secondaryPool != null) {
            secondaryPool.delete();
        }
    }
}
Also used : PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 12 with KVMStoragePool

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

the class LibvirtComputingResource method createVbd.

protected void createVbd(Connect conn, VirtualMachineTO vmSpec, String vmName, LibvirtVMDef vm) throws InternalErrorException, LibvirtException, URISyntaxException {
    List<VolumeTO> disks = Arrays.asList(vmSpec.getDisks());
    Collections.sort(disks, new Comparator<VolumeTO>() {

        @Override
        public int compare(VolumeTO arg0, VolumeTO arg1) {
            return arg0.getDeviceId() > arg1.getDeviceId() ? 1 : -1;
        }
    });
    for (VolumeTO volume : disks) {
        KVMPhysicalDisk physicalDisk = null;
        KVMStoragePool pool = null;
        if (volume.getType() == Volume.Type.ISO && volume.getPath() != null) {
            String volPath = volume.getPath();
            int index = volPath.lastIndexOf("/");
            String volDir = volPath.substring(0, index);
            String volName = volPath.substring(index + 1);
            KVMStoragePool secondaryStorage = _storagePoolMgr.getStoragePoolByURI(volDir);
            physicalDisk = secondaryStorage.getPhysicalDisk(volName);
        } else if (volume.getType() != Volume.Type.ISO) {
            pool = _storagePoolMgr.getStoragePool(volume.getPoolUuid());
            physicalDisk = pool.getPhysicalDisk(volume.getPath());
        }
        String volPath = null;
        if (physicalDisk != null) {
            volPath = physicalDisk.getPath();
        }
        DiskDef.diskBus diskBusType = getGuestDiskModel(vmSpec.getOs());
        DiskDef disk = new DiskDef();
        if (volume.getType() == Volume.Type.ISO) {
            if (volPath == null) {
                /* Add iso as placeholder */
                disk.defISODisk(null);
            } else {
                disk.defISODisk(volPath);
            }
        } else {
            int devId = (int) volume.getDeviceId();
            if (volume.getType() == Volume.Type.DATADISK) {
                disk.defFileBasedDisk(physicalDisk.getPath(), devId, DiskDef.diskBus.VIRTIO, DiskDef.diskFmtType.QCOW2);
            } else {
                disk.defFileBasedDisk(physicalDisk.getPath(), devId, diskBusType, DiskDef.diskFmtType.QCOW2);
            }
        }
        vm.getDevices().addDevice(disk);
    }
    if (vmSpec.getType() != VirtualMachine.Type.User) {
        if (_sysvmISOPath != null) {
            DiskDef iso = new DiskDef();
            iso.defISODisk(_sysvmISOPath);
            vm.getDevices().addDevice(iso);
        }
        createPatchVbd(conn, vmName, vm, vmSpec);
    }
}
Also used : VolumeTO(com.cloud.agent.api.to.VolumeTO) DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk)

Example 13 with KVMStoragePool

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

the class LibvirtComputingResource method cleanupDisk.

private boolean cleanupDisk(Connect conn, DiskDef disk) {
    // need to umount secondary storage
    String path = disk.getDiskPath();
    String poolUuid = null;
    if (path != null) {
        String[] token = path.split("/");
        if (token.length > 3) {
            poolUuid = token[2];
        }
    }
    if (poolUuid == null) {
        return true;
    }
    try {
        KVMStoragePool pool = _storagePoolMgr.getStoragePool(poolUuid);
        if (pool != null) {
            pool.delete();
        }
        return true;
    } catch (CloudRuntimeException e) {
        return false;
    }
}
Also used : KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 14 with KVMStoragePool

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

the class LibvirtComputingResource method execute.

protected ManageSnapshotAnswer execute(final ManageSnapshotCommand cmd) {
    String snapshotName = cmd.getSnapshotName();
    String snapshotPath = cmd.getSnapshotPath();
    String vmName = cmd.getVmName();
    try {
        Connect conn = LibvirtConnection.getConnection();
        DomainInfo.DomainState state = null;
        Domain vm = null;
        if (vmName != null) {
            try {
                vm = getDomain(conn, cmd.getVmName());
                state = vm.getInfo().state;
            } catch (LibvirtException e) {
            }
        }
        KVMStoragePool primaryPool = _storagePoolMgr.getStoragePool(cmd.getPool().getUuid());
        KVMPhysicalDisk disk = primaryPool.getPhysicalDisk(cmd.getVolumePath());
        if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING && !primaryPool.isExternalSnapshot()) {
            String vmUuid = vm.getUUIDString();
            Object[] args = new Object[] { snapshotName, vmUuid };
            String snapshot = SnapshotXML.format(args);
            s_logger.debug(snapshot);
            if (cmd.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) {
                vm.snapshotCreateXML(snapshot);
            } else {
                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 {
            /* VM is not running, create a snapshot by ourself */
            final Script command = new Script(_manageSnapshotPath, _cmdsTimeout, s_logger);
            if (cmd.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) {
                command.add("-c", disk.getPath());
            } else {
                command.add("-d", snapshotPath);
            }
            command.add("-n", snapshotName);
            String result = command.execute();
            if (result != null) {
                s_logger.debug("Failed to manage snapshot: " + result);
                return new ManageSnapshotAnswer(cmd, false, "Failed to manage snapshot: " + result);
            }
        }
        return new ManageSnapshotAnswer(cmd, cmd.getSnapshotId(), disk.getPath() + File.separator + snapshotName, true, null);
    } catch (LibvirtException e) {
        s_logger.debug("Failed to manage snapshot: " + e.toString());
        return new ManageSnapshotAnswer(cmd, false, "Failed to manage snapshot: " + e.toString());
    }
}
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) ManageSnapshotAnswer(com.cloud.agent.api.ManageSnapshotAnswer) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) DomainInfo(org.libvirt.DomainInfo) Domain(org.libvirt.Domain)

Example 15 with KVMStoragePool

use of com.cloud.agent.storage.KVMStoragePool 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)

Aggregations

KVMStoragePool (com.cloud.agent.storage.KVMStoragePool)19 KVMPhysicalDisk (com.cloud.agent.storage.KVMPhysicalDisk)13 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)12 Script (com.cloud.utils.script.Script)7 CreatePrivateTemplateAnswer (com.cloud.agent.api.storage.CreatePrivateTemplateAnswer)6 AttachVolumeAnswer (com.cloud.agent.api.AttachVolumeAnswer)5 BackupSnapshotAnswer (com.cloud.agent.api.BackupSnapshotAnswer)5 CreateVolumeFromSnapshotAnswer (com.cloud.agent.api.CreateVolumeFromSnapshotAnswer)5 DeleteSnapshotBackupAnswer (com.cloud.agent.api.DeleteSnapshotBackupAnswer)5 ManageSnapshotAnswer (com.cloud.agent.api.ManageSnapshotAnswer)5 PrimaryStorageDownloadAnswer (com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer)5 Answer (com.cloud.agent.api.Answer)4 CheckHealthAnswer (com.cloud.agent.api.CheckHealthAnswer)4 CheckNetworkAnswer (com.cloud.agent.api.CheckNetworkAnswer)4 CheckVirtualMachineAnswer (com.cloud.agent.api.CheckVirtualMachineAnswer)4 FenceAnswer (com.cloud.agent.api.FenceAnswer)4 GetHostStatsAnswer (com.cloud.agent.api.GetHostStatsAnswer)4 GetStorageStatsAnswer (com.cloud.agent.api.GetStorageStatsAnswer)4 GetVmStatsAnswer (com.cloud.agent.api.GetVmStatsAnswer)4 GetVncPortAnswer (com.cloud.agent.api.GetVncPortAnswer)4