use of com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk in project cloudstack by apache.
the class LibvirtComputingResource method getVolumePath.
public String getVolumePath(final Connect conn, final DiskTO volume) throws LibvirtException, URISyntaxException {
final DataTO data = volume.getData();
final DataStoreTO store = data.getDataStore();
if (volume.getType() == Volume.Type.ISO && data.getPath() != null) {
final NfsTO nfsStore = (NfsTO) store;
final String isoPath = nfsStore.getUrl() + File.separator + data.getPath();
final int index = isoPath.lastIndexOf("/");
final String path = isoPath.substring(0, index);
final String name = isoPath.substring(index + 1);
final KVMStoragePool secondaryPool = _storagePoolMgr.getStoragePoolByURI(path);
final KVMPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name);
return isoVol.getPath();
} else {
return data.getPath();
}
}
use of com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResource method templateToPrimaryDownload.
// this is much like PrimaryStorageDownloadCommand, but keeping it separate
public KvmPhysicalDisk templateToPrimaryDownload(final String templateUrl, final KvmStoragePool primaryPool, final String volUuid) {
final int index = templateUrl.lastIndexOf("/");
final String mountpoint = templateUrl.substring(0, index);
String templateName = null;
if (index < templateUrl.length() - 1) {
templateName = templateUrl.substring(index + 1);
}
KvmPhysicalDisk templateVol = null;
KvmStoragePool secondaryPool = null;
try {
secondaryPool = storagePoolMgr.getStoragePoolByUri(mountpoint);
/* Get template vol */
if (templateName == null) {
secondaryPool.refresh();
final List<KvmPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
if (disks == null || disks.isEmpty()) {
logger.error("Failed to get volumes from pool: " + secondaryPool.getUuid());
return null;
}
for (final KvmPhysicalDisk disk : disks) {
if (disk.getName().endsWith("qcow2")) {
templateVol = disk;
break;
}
}
if (templateVol == null) {
logger.error("Failed to get template from pool: " + secondaryPool.getUuid());
return null;
}
} else {
templateVol = secondaryPool.getPhysicalDisk(templateName);
}
/* Copy volume to primary storage */
final KvmPhysicalDisk primaryVol = storagePoolMgr.copyPhysicalDisk(templateVol, volUuid, primaryPool, 0);
return primaryVol;
} catch (final CloudRuntimeException e) {
logger.error("Failed to download template to primary storage", e);
return null;
} finally {
if (secondaryPool != null) {
storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid());
}
}
}
use of com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResource method createVbd.
public void createVbd(final Connect conn, final VirtualMachineTO vmSpec, final String vmName, final LibvirtVmDef vm) throws InternalErrorException, LibvirtException, URISyntaxException {
final List<DiskTO> disks = Arrays.asList(vmSpec.getDisks());
Collections.sort(disks, new Comparator<DiskTO>() {
@Override
public int compare(final DiskTO arg0, final DiskTO arg1) {
return arg0.getDiskSeq() > arg1.getDiskSeq() ? 1 : -1;
}
});
for (final DiskTO volume : disks) {
KvmPhysicalDisk physicalDisk = null;
KvmStoragePool pool = null;
final DataTO data = volume.getData();
if (volume.getType() == Volume.Type.ISO && data.getPath() != null) {
final NfsTO nfsStore = (NfsTO) data.getDataStore();
final String volPath = nfsStore.getUrl() + File.separator + data.getPath();
final int index = volPath.lastIndexOf("/");
final String volDir = volPath.substring(0, index);
final String volName = volPath.substring(index + 1);
final KvmStoragePool secondaryStorage = storagePoolMgr.getStoragePoolByUri(volDir);
physicalDisk = secondaryStorage.getPhysicalDisk(volName);
} else if (volume.getType() != Volume.Type.ISO) {
final PrimaryDataStoreTO store = (PrimaryDataStoreTO) data.getDataStore();
physicalDisk = storagePoolMgr.getPhysicalDisk(store.getPoolType(), store.getUuid(), data.getPath());
pool = physicalDisk.getPool();
}
String volPath = null;
if (physicalDisk != null) {
volPath = physicalDisk.getPath();
}
// check for disk activity, if detected we should exit because vm is running elsewhere
if (diskActivityCheckEnabled && physicalDisk != null && physicalDisk.getFormat() == PhysicalDiskFormat.QCOW2) {
logger.debug("Checking physical disk file at path " + volPath + " for disk activity to ensure vm is not running elsewhere");
try {
HypervisorUtils.checkVolumeFileForActivity(volPath, diskActivityCheckTimeoutSeconds, diskActivityInactiveThresholdMilliseconds, diskActivityCheckFileSizeMin);
} catch (final IOException ex) {
throw new CloudRuntimeException("Unable to check physical disk file for activity", ex);
}
logger.debug("Disk activity check cleared");
}
// if params contains a rootDiskController key, use its value (this is what other HVs are doing)
LibvirtDiskDef.DiskBus diskBusType = getDiskModelFromVmDetail(vmSpec);
if (diskBusType == null) {
diskBusType = getGuestDiskModel(vmSpec.getPlatformEmulator());
logger.debug("disk bus type for " + vmName + " derived from getPlatformEmulator: " + vmSpec.getPlatformEmulator() + ", diskbustype is: " + diskBusType.toString());
}
final LibvirtDiskDef disk = new LibvirtDiskDef();
if (volume.getType() == Volume.Type.ISO) {
if (volPath == null) {
/* Add iso as placeholder */
disk.defIsoDisk(null);
} else {
disk.defIsoDisk(volPath);
}
} else {
final int devId = volume.getDiskSeq().intValue();
if (diskBusType == LibvirtDiskDef.DiskBus.SCSI) {
disk.setQemuDriver(true);
disk.setDiscard(DiscardType.UNMAP);
}
if (pool.getType() == StoragePoolType.RBD) {
/*
* For RBD pools we use the secret mechanism in libvirt. We store the secret under the UUID of the pool,
* that's why we pass the pool's UUID as the authSecret
*/
disk.defNetworkBasedDisk(physicalDisk.getPath().replace("rbd:", ""), pool.getSourceHost(), pool.getSourcePort(), pool.getAuthUserName(), pool.getUuid(), devId, diskBusType, DiskProtocol.RBD, LibvirtDiskDef.DiskFmtType.RAW);
} else if (pool.getType() == StoragePoolType.Gluster) {
final String mountpoint = pool.getLocalPath();
final String path = physicalDisk.getPath();
final String glusterVolume = pool.getSourceDir().replace("/", "");
disk.defNetworkBasedDisk(glusterVolume + path.replace(mountpoint, ""), pool.getSourceHost(), pool.getSourcePort(), null, null, devId, diskBusType, DiskProtocol.GLUSTER, LibvirtDiskDef.DiskFmtType.QCOW2);
} else if (pool.getType() == StoragePoolType.CLVM || physicalDisk.getFormat() == PhysicalDiskFormat.RAW) {
disk.defBlockBasedDisk(physicalDisk.getPath(), devId, diskBusType);
} else {
if (volume.getType() == Volume.Type.DATADISK) {
disk.defFileBasedDisk(physicalDisk.getPath(), devId, LibvirtDiskDef.DiskBus.VIRTIO, LibvirtDiskDef.DiskFmtType.QCOW2);
} else {
disk.defFileBasedDisk(physicalDisk.getPath(), devId, diskBusType, LibvirtDiskDef.DiskFmtType.QCOW2);
}
}
}
if (data instanceof VolumeObjectTO) {
final VolumeObjectTO volumeObjectTo = (VolumeObjectTO) data;
disk.setSerial(volumeObjectTo.getDeviceId() + "-" + diskUuidToSerial(volumeObjectTo.getUuid()));
if (volumeObjectTo.getBytesReadRate() != null && volumeObjectTo.getBytesReadRate() > 0) {
disk.setBytesReadRate(volumeObjectTo.getBytesReadRate());
}
if (volumeObjectTo.getBytesWriteRate() != null && volumeObjectTo.getBytesWriteRate() > 0) {
disk.setBytesWriteRate(volumeObjectTo.getBytesWriteRate());
}
if (volumeObjectTo.getIopsReadRate() != null && volumeObjectTo.getIopsReadRate() > 0) {
disk.setIopsReadRate(volumeObjectTo.getIopsReadRate());
}
if (volumeObjectTo.getIopsWriteRate() != null && volumeObjectTo.getIopsWriteRate() > 0) {
disk.setIopsWriteRate(volumeObjectTo.getIopsWriteRate());
}
if (volumeObjectTo.getCacheMode() != null) {
disk.setCacheMode(LibvirtDiskDef.DiskCacheMode.valueOf(volumeObjectTo.getCacheMode().toString().toUpperCase()));
}
}
logger.debug("Adding disk: " + disk.toString());
vm.getDevices().addDevice(disk);
}
if (vmSpec.getType() != VirtualMachine.Type.User) {
final String sysvmIsoPath = getSysvmIsoPath();
if (sysvmIsoPath != null) {
final LibvirtDiskDef iso = new LibvirtDiskDef();
iso.defIsoDisk(sysvmIsoPath);
vm.getDevices().addDevice(iso);
}
}
}
use of com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk in project cosmic by MissionCriticalCloud.
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();
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: " + 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);
}
use of com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk in project cosmic by MissionCriticalCloud.
the class LibvirtCreatePrivateTemplateFromVolumeCommandWrapper method execute.
@Override
public Answer execute(final CreatePrivateTemplateFromVolumeCommand command, final LibvirtComputingResource libvirtComputingResource) {
final String secondaryStorageUrl = command.getSecondaryStorageUrl();
KvmStoragePool secondaryStorage = null;
KvmStoragePool primary = null;
final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
try {
final String templateFolder = command.getAccountId() + File.separator + command.getTemplateId() + File.separator;
final String templateInstallFolder = "/template/tmpl/" + templateFolder;
secondaryStorage = storagePoolMgr.getStoragePoolByUri(secondaryStorageUrl);
try {
primary = storagePoolMgr.getStoragePool(command.getPool().getType(), command.getPrimaryStoragePoolNameLabel());
} catch (final CloudRuntimeException e) {
if (e.getMessage().contains("not found")) {
primary = storagePoolMgr.createStoragePool(command.getPool().getUuid(), command.getPool().getHost(), command.getPool().getPort(), command.getPool().getPath(), command.getPool().getUserInfo(), command.getPool().getType());
} else {
return new CreatePrivateTemplateAnswer(command, false, e.getMessage());
}
}
final KvmPhysicalDisk disk = primary.getPhysicalDisk(command.getVolumePath());
final String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateInstallFolder;
final StorageLayer storage = libvirtComputingResource.getStorage();
storage.mkdirs(tmpltPath);
if (primary.getType() != StoragePoolType.RBD) {
final String createTmplPath = libvirtComputingResource.createTmplPath();
final int cmdsTimeout = libvirtComputingResource.getCmdsTimeout();
final Script scriptCommand = new Script(createTmplPath, cmdsTimeout, s_logger);
scriptCommand.add("-f", disk.getPath());
scriptCommand.add("-t", tmpltPath);
scriptCommand.add("-n", command.getUniqueName() + ".qcow2");
final String result = scriptCommand.execute();
if (result != null) {
s_logger.debug("failed to create template: " + result);
return new CreatePrivateTemplateAnswer(command, false, result);
}
} else {
s_logger.debug("Converting RBD disk " + disk.getPath() + " into template " + command.getUniqueName());
final QemuImgFile srcFile = new QemuImgFile(KvmPhysicalDisk.rbdStringBuilder(primary.getSourceHost(), primary.getSourcePort(), primary.getAuthUserName(), primary.getAuthSecret(), disk.getPath()));
srcFile.setFormat(PhysicalDiskFormat.RAW);
final QemuImgFile destFile = new QemuImgFile(tmpltPath + "/" + command.getUniqueName() + ".qcow2");
destFile.setFormat(PhysicalDiskFormat.QCOW2);
final QemuImg q = new QemuImg(0);
try {
q.convert(srcFile, destFile);
} catch (final QemuImgException e) {
s_logger.error("Failed to create new template while converting " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage());
}
final File templateProp = new File(tmpltPath + "/template.properties");
if (!templateProp.exists()) {
templateProp.createNewFile();
}
String templateContent = "filename=" + command.getUniqueName() + ".qcow2" + System.getProperty("line.separator");
final DateFormat dateFormat = new SimpleDateFormat("MM_dd_yyyy");
final Date date = new Date();
templateContent += "snapshot.name=" + dateFormat.format(date) + System.getProperty("line.separator");
try (FileOutputStream templFo = new FileOutputStream(templateProp)) {
templFo.write(templateContent.getBytes("UTF-8"));
templFo.flush();
} catch (final IOException ex) {
s_logger.error("CreatePrivateTemplateAnswer:Exception:" + ex.getMessage());
}
}
final Map<String, Object> params = new HashMap<>();
params.put(StorageLayer.InstanceConfigKey, storage);
final Processor qcow2Processor = new QCOW2Processor();
qcow2Processor.configure("QCOW2 Processor", params);
final FormatInfo info = qcow2Processor.process(tmpltPath, null, command.getUniqueName());
final TemplateLocation loc = new TemplateLocation(storage, tmpltPath);
loc.create(1, true, command.getUniqueName());
loc.addFormat(info);
loc.save();
return new CreatePrivateTemplateAnswer(command, true, null, templateInstallFolder + command.getUniqueName() + ".qcow2", info.virtualSize, info.size, command.getUniqueName(), ImageFormat.QCOW2);
} catch (final InternalErrorException e) {
return new CreatePrivateTemplateAnswer(command, false, e.toString());
} catch (final IOException e) {
return new CreatePrivateTemplateAnswer(command, false, e.toString());
} catch (final ConfigurationException e) {
return new CreatePrivateTemplateAnswer(command, false, e.toString());
} catch (final CloudRuntimeException e) {
return new CreatePrivateTemplateAnswer(command, false, e.toString());
} finally {
if (secondaryStorage != null) {
storagePoolMgr.deleteStoragePool(secondaryStorage.getType(), secondaryStorage.getUuid());
}
}
}
Aggregations