2

I am trying to upload a disk image for use on the Azure cloud platform. I've been following through these instructions, but image resizing is giving me a hard time.

I start out with a qcow2 image:

$ qemu-img info --output=json myimage.qcow2 
{
    "virtual-size": 8589934592,
    "filename": "myimage.qcow2",
    "cluster-size": 65536,
    "format": "qcow2",
    "actual-size": 1468272640,
    "format-specific": {
        "type": "qcow2",
        "data": {
            "compat": "0.10",
            "refcount-bits": 16
        }
    },
    "dirty-flag": false
}

I convert this image into a raw disk:

$ qemu-img convert -f qcow2 -O raw myimage.qcow2 myimage.img

Then, following along with the instructions, I round the size of to an even number of megabytes:

$ MB=$((1024 * 1024))
$ size=$(qemu-img info -f raw --output json "$1" |
  gawk 'match($0, /"virtual-size": ([0-9]+),/, val) {print val[1]}')
$ rounded_size=$((($size/$MB + 1) * $MB))
$ echo $rounded_size
8590983168

And resize the image:

$ qemu-img resize -f raw myimage.img $rounded_size

Which gets me:

$ qemu-img info -f raw --output=json myimage.img 
{
    "virtual-size": 8590983168,
    "filename": "myimage.img",
    "format": "raw",
    "actual-size": 1458573312,
    "dirty-flag": false
}

(That's exactly 8193 MB).

When I convert this to VHD format:

$ qemu-img convert -f raw -o subformat=fixed -O vpc myimage.img myimage.vhd

I end up with a file that is no longer the correct size:

$ ls -l myimage.vhd 
-rw-r--r--. 1 lars lars 8591450624 Apr 14 12:04 myimage.vhd

And when I try to upload this, Azure yells at me:

$ azure vm image create myimage myimage.vhd  --os Linux --location 'East US'
info:    Executing command vm image create
+ Retrieving storage accounts                                                  
info:    VHD size : 8193 MB
info:    Uploading 8390088.5 KB
...
info:    https://....blob.core.windows.net/vm-images/myimage.vhd was uploaded successfully
error:   The VHD https://....blob.core.windows.net/vm-images/myimage.vhd has an unsupported virtual size of 8591450112 bytes.  The size must be a whole number (in MBs).
info:    Error information has been recorded to /home/lars/.azure/azure.err
error:   vm image create command failed

How do I appease the Azure monster and make it stop yelling at me?

(FYI: I am using: qemu-img version 2.5.0 (qemu-2.5.0-10.fc23))

1 Answer 1

8

It looks like this is due to an incompatibility between qemu-img and Microsoft Azure (I hesitate to call it a "bug" because it appears that there are multiple "standards" covering vpc format images).

The problem is that qemu-img will, by default, create an image aligned to the nearest CHS geometry, while Azure wants images aligned to the nearest whole MB.

There is already a fix for this in the QEMU repository; the commit that fixes it is fb9245c, the commit message for which is:

block/vpc: give option to force the current_size field in .bdrv_create

When QEMU creates a VHD image, it goes by the original spec, calculating the current_size based on the nearest CHS geometry (with an exception for disks > 127GB).

Apparently, Azure will only allow images that are sized to the nearest MB, and the current_size as calculated from CHS cannot guarantee that.

Allow QEMU to create images similar to how Hyper-V creates images, by setting current_size to the specified virtual disk size. This introduces an option, force_size, to be passed to the vpc format during image creation, e.g.:

qemu-img convert -f raw -o force_size -O vpc test.img test.vhd

When using the "force_size" option, the creator app field used by QEMU will be "qem2" instead of "qemu", to indicate the difference. In light of this, we also add parsing of the "qem2" field during vpc_open.

I can confirm that with this change applied locally, I can run...

qemu-img convert -f raw -O vpc -o subformat=fixed,force_size myimage.raw myimage.vhd

...and generate a disk image that will successfully upload to Azure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.