Search in sources :

Example 1 with ImageDownload

use of com.woorea.openstack.glance.model.ImageDownload 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");
    }
}
Also used : QcowCompat(org.ovirt.engine.core.common.businessentities.storage.QcowCompat) InputStream(java.io.InputStream) ImageDownload(com.woorea.openstack.glance.model.ImageDownload) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ImageDownload (com.woorea.openstack.glance.model.ImageDownload)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ByteBuffer (java.nio.ByteBuffer)1 QcowCompat (org.ovirt.engine.core.common.businessentities.storage.QcowCompat)1