use of org.ovirt.engine.core.common.businessentities.storage.QcowCompat in project ovirt-engine by oVirt.
the class OpenStackImageProviderProxy method setCowVirtualSizeAndQcowCompat.
private void setCowVirtualSizeAndQcowCompat(DiskImage diskImage, Image glanceImage) {
// For the qcow2 format we need to download the image header and read the virtual size from there
byte[] imgContent = new byte[72];
ImageDownload downloadImage = getClient().images().download(glanceImage.getId()).execute();
try (InputStream inputStream = downloadImage.getInputStream()) {
int bytesRead = inputStream.read(imgContent, 0, imgContent.length);
if (bytesRead != imgContent.length) {
throw new OpenStackImageException(OpenStackImageException.ErrorType.UNABLE_TO_DOWNLOAD_IMAGE, "Unable to read image header: " + bytesRead);
}
} catch (IOException e) {
throw new OpenStackImageException(OpenStackImageException.ErrorType.UNABLE_TO_DOWNLOAD_IMAGE, "Unable to download image");
}
ByteBuffer b = ByteBuffer.wrap(imgContent);
int qcow2Signature = b.getInt();
int qcow2Version = b.getInt();
QcowCompat qcowCompat = QcowCompat.forQcowHeaderVersion(qcow2Version);
if (qcow2Signature == QCOW2_SIGNATURE && qcowCompat != null && qcowCompat != QcowCompat.Undefined) {
b.position(QCOW2_SIZE_OFFSET);
diskImage.setSize(b.getLong());
diskImage.setQcowCompat(qcowCompat);
} else {
throw new OpenStackImageException(OpenStackImageException.ErrorType.UNRECOGNIZED_IMAGE_FORMAT, "Unable to recognize QCOW2 format");
}
}
Aggregations