use of com.cloud.utils.qemu.QemuImg in project cosmic by MissionCriticalCloud.
the class LibvirtStorageAdaptor method createDiskFromTemplate.
@Override
public KvmPhysicalDisk createDiskFromTemplate(final KvmPhysicalDisk template, final String name, final PhysicalDiskFormat format, final Storage.ProvisioningType provisioningType, final long size, final KvmStoragePool destPool, final int timeout) {
logger.info("Creating volume " + name + " from template " + template.getName() + " in pool " + destPool.getUuid() + " (" + destPool.getType().toString() + ") with size " + size);
KvmPhysicalDisk disk = null;
if (destPool.getType() == StoragePoolType.RBD) {
disk = createDiskFromTemplateOnRbd(template, name, format, provisioningType, size, destPool, timeout);
} else {
try {
final String newUuid = name;
disk = destPool.createPhysicalDisk(newUuid, format, provisioningType, template.getVirtualSize());
if (disk == null) {
throw new CloudRuntimeException("Failed to create disk from template " + template.getName());
}
if (template.getFormat() == PhysicalDiskFormat.TAR) {
Script.runSimpleBashScript("tar -x -f " + template.getPath() + " -C " + disk.getPath(), timeout);
} else if (template.getFormat() == PhysicalDiskFormat.DIR) {
Script.runSimpleBashScript("mkdir -p " + disk.getPath());
Script.runSimpleBashScript("chmod 755 " + disk.getPath());
Script.runSimpleBashScript("tar -x -f " + template.getPath() + "/*.tar -C " + disk.getPath(), timeout);
} else if (format == PhysicalDiskFormat.QCOW2) {
final QemuImg qemu = new QemuImg(timeout);
final QemuImgFile destFile = new QemuImgFile(disk.getPath(), format);
if (size > template.getVirtualSize()) {
destFile.setSize(size);
} else {
destFile.setSize(template.getVirtualSize());
}
final Map<String, String> options = new HashMap<>();
options.put("preallocation", QemuImg.PreallocationType.getPreallocationType(provisioningType).toString());
switch(provisioningType) {
case THIN:
final QemuImgFile backingFile = new QemuImgFile(template.getPath(), template.getFormat());
qemu.create(destFile, backingFile, options);
break;
case SPARSE:
case FAT:
final QemuImgFile srcFile = new QemuImgFile(template.getPath(), template.getFormat());
qemu.convert(srcFile, destFile, options);
break;
default:
break;
}
} else if (format == PhysicalDiskFormat.RAW) {
final QemuImgFile sourceFile = new QemuImgFile(template.getPath(), template.getFormat());
final QemuImgFile destFile = new QemuImgFile(disk.getPath(), PhysicalDiskFormat.RAW);
if (size > template.getVirtualSize()) {
destFile.setSize(size);
} else {
destFile.setSize(template.getVirtualSize());
}
final QemuImg qemu = new QemuImg(timeout);
final Map<String, String> options = new HashMap<>();
qemu.convert(sourceFile, destFile, options);
}
} catch (final QemuImgException e) {
logger.error("Failed to create " + disk.getPath() + " due to a failed executing of qemu-img: " + e.getMessage());
}
}
return disk;
}
use of com.cloud.utils.qemu.QemuImg in project cosmic by MissionCriticalCloud.
the class LibvirtStorageAdaptor method createPhysicalDiskByQemuImg.
private KvmPhysicalDisk createPhysicalDiskByQemuImg(final String name, final KvmStoragePool pool, final PhysicalDiskFormat format, final Storage.ProvisioningType provisioningType, final long size) {
final String volPath = pool.getLocalPath() + "/" + name;
final String volName = name;
long virtualSize = 0;
long actualSize = 0;
final int timeout = 0;
final QemuImgFile destFile = new QemuImgFile(volPath);
destFile.setFormat(format);
destFile.setSize(size);
final QemuImg qemu = new QemuImg(timeout);
final Map<String, String> options = new HashMap<>();
if (pool.getType() == StoragePoolType.NetworkFilesystem) {
options.put("preallocation", QemuImg.PreallocationType.getPreallocationType(provisioningType).toString());
}
try {
qemu.create(destFile, options);
final Map<String, String> info = qemu.info(destFile);
virtualSize = Long.parseLong(info.get(new String("virtual_size")));
actualSize = new File(destFile.getFileName()).length();
} catch (final QemuImgException e) {
logger.error("Failed to create " + volPath + " due to a failed executing of qemu-img: " + e.getMessage());
}
final KvmPhysicalDisk disk = new KvmPhysicalDisk(volPath, volName, pool);
disk.setFormat(format);
disk.setSize(actualSize);
disk.setVirtualSize(virtualSize);
return disk;
}
Aggregations