use of com.cloud.agent.resource.kvm.xml.LibvirtDiskDef in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResource method getVmDiskStat.
public List<VmDiskStatsEntry> getVmDiskStat(final Connect conn, final String vmName) throws LibvirtException {
Domain dm = null;
try {
dm = getDomain(conn, vmName);
final List<VmDiskStatsEntry> stats = new ArrayList<>();
final List<LibvirtDiskDef> disks = getDisks(conn, vmName);
for (final LibvirtDiskDef disk : disks) {
if (disk.getDeviceType() != DeviceType.DISK) {
break;
}
final DomainBlockStats blockStats = dm.blockStats(disk.getDiskLabel());
// for example, path = /mnt/pool_uuid/disk_path/
final String path = disk.getDiskPath();
String diskPath = null;
if (path != null) {
final String[] token = path.split("/");
if (token.length > 3) {
diskPath = token[3];
final VmDiskStatsEntry stat = new VmDiskStatsEntry(vmName, diskPath, blockStats.wr_req, blockStats.rd_req, blockStats.wr_bytes, blockStats.rd_bytes);
stats.add(stat);
}
}
}
return stats;
} finally {
if (dm != null) {
dm.free();
}
}
}
use of com.cloud.agent.resource.kvm.xml.LibvirtDiskDef 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() == VolumeType.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 = this.storagePoolMgr.getStoragePoolByUri(volDir);
physicalDisk = secondaryStorage.getPhysicalDisk(volName);
} else if (volume.getType() != VolumeType.ISO) {
final PrimaryDataStoreTO store = (PrimaryDataStoreTO) data.getDataStore();
physicalDisk = this.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 (this.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, this.diskActivityCheckTimeoutSeconds, this.diskActivityInactiveThresholdMilliseconds, this.diskActivityCheckFileSizeMin);
} catch (final IOException ex) {
throw new CloudRuntimeException("Unable to check physical disk file for activity", ex);
}
logger.debug("Disk activity check cleared");
}
final LibvirtDiskDef disk = new LibvirtDiskDef();
if (volume.getType() == VolumeType.ISO) {
if (volPath == null) {
/* Add iso as placeholder */
disk.defIsoDisk(null);
} else {
disk.defIsoDisk(volPath);
}
} else {
final int devId = volume.getDiskSeq().intValue();
if (volume.getDiskController() == DiskControllerType.SCSI) {
disk.setQemuDriver(true);
disk.setDiscard(DiscardType.UNMAP);
}
disk.setImageFormat(volume.getDiskFormat());
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, volume.getDiskController(), DiskProtocol.RBD, ImageFormat.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, volume.getDiskController(), DiskProtocol.GLUSTER, ImageFormat.QCOW2);
} else if (pool.getType() == StoragePoolType.CLVM || pool.getType() == StoragePoolType.LVM) {
disk.defBlockBasedDisk(physicalDisk.getPath(), devId, volume.getDiskController());
} else if (pool.getType() == StoragePoolType.NetworkFilesystem) {
disk.defFileBasedDisk(physicalDisk.getPath(), devId, volume.getDiskController(), volume.getDiskFormat());
} else {
disk.defFileBasedDisk(physicalDisk.getPath(), devId, volume.getDiskController(), volume.getDiskFormat());
}
}
if (data instanceof VolumeObjectTO) {
final VolumeObjectTO volumeObjectTo = (VolumeObjectTO) data;
disk.setSerial(volumeObjectTo.getDeviceId() + "-" + diskUuidToSerial(volumeObjectTo.getUuid()));
disk.setDeviceId(volumeObjectTo.getDeviceId().intValue());
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.getIopsTotalRate() != null && volumeObjectTo.getIopsTotalRate() > 0) {
disk.setIopsTotalRate(volumeObjectTo.getIopsTotalRate());
}
if (volumeObjectTo.getCacheMode() != null) {
disk.setCacheMode(LibvirtDiskDef.DiskCacheMode.valueOf(volumeObjectTo.getCacheMode().toString().toUpperCase()));
}
if (volumeObjectTo.getFormat() != null) {
physicalDisk.setFormat(physicalDisk.getPhysicalDiskFormatFromImageFormat(volumeObjectTo.getFormat()));
}
}
logger.debug("Adding disk: " + disk.toString());
vm.getDevices().addDevice(disk);
}
if (vmSpec.getType() != VirtualMachineType.User) {
final String sysvmIsoPath = getSysvmIsoPath();
if (sysvmIsoPath != null) {
final LibvirtDiskDef iso = new LibvirtDiskDef();
iso.defIsoDisk(sysvmIsoPath);
vm.getDevices().addDevice(iso);
}
}
}
use of com.cloud.agent.resource.kvm.xml.LibvirtDiskDef in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testGetVmStat.
@Test
public void testGetVmStat() throws LibvirtException {
final Connect connect = Mockito.mock(Connect.class);
final Domain domain = Mockito.mock(Domain.class);
final DomainInfo domainInfo = new DomainInfo();
Mockito.when(domain.getInfo()).thenReturn(domainInfo);
Mockito.when(connect.domainLookupByName(VMNAME)).thenReturn(domain);
final NodeInfo nodeInfo = new NodeInfo();
nodeInfo.cpus = 8;
nodeInfo.memory = 8 * 1024 * 1024;
nodeInfo.sockets = 2;
nodeInfo.threads = 2;
nodeInfo.model = "Foo processor";
Mockito.when(connect.nodeInfo()).thenReturn(nodeInfo);
// this is testing the interface stats, returns an increasing number of sent and received bytes
Mockito.when(domain.interfaceStats(Matchers.anyString())).thenAnswer(new org.mockito.stubbing.Answer<DomainInterfaceStats>() {
// increment with less than a KB, so this should be less than 1 KB
static final int increment = 1000;
int rxBytes = 1000;
int txBytes = 1000;
@Override
public DomainInterfaceStats answer(final InvocationOnMock invocation) throws Throwable {
final DomainInterfaceStats domainInterfaceStats = new DomainInterfaceStats();
domainInterfaceStats.rx_bytes = this.rxBytes += increment;
domainInterfaceStats.tx_bytes = this.txBytes += increment;
return domainInterfaceStats;
}
});
Mockito.when(domain.blockStats(Matchers.anyString())).thenAnswer(new org.mockito.stubbing.Answer<DomainBlockStats>() {
// a little less than a KB
static final int increment = 1000;
int rdBytes = 0;
int wrBytes = 1024;
@Override
public DomainBlockStats answer(final InvocationOnMock invocation) throws Throwable {
final DomainBlockStats domainBlockStats = new DomainBlockStats();
domainBlockStats.rd_bytes = this.rdBytes += increment;
domainBlockStats.wr_bytes = this.wrBytes += increment;
return domainBlockStats;
}
});
final LibvirtComputingResource libvirtComputingResource = new LibvirtComputingResource() {
@Override
public List<InterfaceDef> getInterfaces(final Connect conn, final String vmName) {
final InterfaceDef interfaceDef = new InterfaceDef();
return Arrays.asList(interfaceDef);
}
@Override
public List<LibvirtDiskDef> getDisks(final Connect conn, final String vmName) {
final LibvirtDiskDef diskDef = new LibvirtDiskDef();
return Arrays.asList(diskDef);
}
};
libvirtComputingResource.getVmStat(connect, VMNAME);
final VmStatsEntry vmStat = libvirtComputingResource.getVmStat(connect, VMNAME);
// network traffic as generated by the logic above, must be greater than zero
Assert.assertTrue(vmStat.getNetworkReadKBs() > 0);
Assert.assertTrue(vmStat.getNetworkWriteKBs() > 0);
// IO traffic as generated by the logic above, must be greater than zero
Assert.assertTrue(vmStat.getDiskReadKBs() > 0);
Assert.assertTrue(vmStat.getDiskWriteKBs() > 0);
}
use of com.cloud.agent.resource.kvm.xml.LibvirtDiskDef in project cosmic by MissionCriticalCloud.
the class LibvirtVMDefTest method testDiskDef.
public void testDiskDef() {
final String filePath = "/var/lib/libvirt/images/disk.qcow2";
final String diskLabel = "sda";
final int deviceId = 1;
final LibvirtDiskDef disk = new LibvirtDiskDef();
final DiskControllerType bus = DiskControllerType.SCSI;
final ImageFormat imageFormat = ImageFormat.QCOW2;
final LibvirtDiskDef.DiskCacheMode cacheMode = LibvirtDiskDef.DiskCacheMode.WRITEBACK;
disk.defFileBasedDisk(filePath, diskLabel, bus, imageFormat);
disk.setCacheMode(cacheMode);
disk.setDeviceId(deviceId);
assertEquals(filePath, disk.getDiskPath());
assertEquals(diskLabel, disk.getDiskLabel());
assertEquals(bus.toString().toLowerCase(), disk.getBusType().toString().toLowerCase());
assertEquals(LibvirtDiskDef.DeviceType.DISK, disk.getDeviceType());
final String xmlDef = disk.toString();
final String expectedXml = "<disk device='disk' type='file'>\n<driver name='qemu' type='" + imageFormat.toString().toLowerCase() + "' cache='" + cacheMode.toString() + "' />\n" + "<source file='" + filePath + "'/>\n<target dev='" + diskLabel + "' bus='" + bus.toString().toLowerCase() + "'/>\n" + "<address type='drive' controller='0' bus='0' target='0' unit='1'/></disk>\n";
assertEquals(xmlDef, expectedXml);
}
use of com.cloud.agent.resource.kvm.xml.LibvirtDiskDef in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResource method attachOrDetachIso.
public synchronized String attachOrDetachIso(final Connect conn, final String vmName, String isoPath, final boolean isAttach) throws LibvirtException, URISyntaxException, InternalErrorException {
String isoXml = null;
if (isoPath != null && isAttach) {
final int index = isoPath.lastIndexOf("/");
final String path = isoPath.substring(0, index);
final String name = isoPath.substring(index + 1);
final KvmStoragePool secondaryPool = this.storagePoolMgr.getStoragePoolByUri(path);
final KvmPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name);
isoPath = isoVol.getPath();
final LibvirtDiskDef iso = new LibvirtDiskDef();
iso.defIsoDisk(isoPath);
isoXml = iso.toString();
} else {
final LibvirtDiskDef iso = new LibvirtDiskDef();
iso.defIsoDisk(null);
isoXml = iso.toString();
}
final List<LibvirtDiskDef> disks = getDisks(conn, vmName);
final String result = attachOrDetachDevice(conn, true, vmName, isoXml);
if (result == null && !isAttach) {
for (final LibvirtDiskDef disk : disks) {
if (disk.getDeviceType() == LibvirtDiskDef.DeviceType.CDROM) {
cleanupDisk(disk);
}
}
}
return result;
}
Aggregations