Skip to content

Commit

Permalink
volume upload-to-image fails for iso disk-format
Browse files Browse the repository at this point in the history
upload volume to image fails if disk-format is iso with error
u"qemu-img: Unknown file format 'iso'\n".

Raising HTTP 400 error if disk-format is other than
'raw', 'vmdk', 'vdi', 'qcow2'.

APIImpact
Closes-Bug: #1540938
(cherry picked from commit c5df007)

Change-Id: Ibeddb62876fc66560c2019c62ccf5abb3100f63e
  • Loading branch information
abhishekkekane authored and Jay Conroy committed Jul 28, 2016
1 parent 9c7a2ab commit 911ee41
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
21 changes: 17 additions & 4 deletions cinder/api/contrib/volume_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from cinder.api import xmlutil
from cinder import exception
from cinder.i18n import _
from cinder.image import image_utils
from cinder import utils
from cinder import volume

Expand Down Expand Up @@ -272,10 +273,22 @@ def _volume_upload_image(self, req, id, body):
raise webob.exc.HTTPNotFound(explanation=error.msg)

authorize(context, "upload_image")
image_metadata = {"container_format": params.get("container_format",
"bare"),
"disk_format": params.get("disk_format", "raw"),
"name": params["image_name"]}
# check for valid disk-format
disk_format = params.get("disk_format", "raw")
if not image_utils.validate_disk_format(disk_format):
msg = _("Invalid disk-format '%(disk_format)s' is specified. "
"Allowed disk-formats are %(allowed_disk_formats)s.") % {
"disk_format": disk_format,
"allowed_disk_formats": ", ".join(
image_utils.VALID_DISK_FORMATS)
}
raise webob.exc.HTTPBadRequest(explanation=msg)

image_metadata = {"container_format": params.get(
"container_format", "bare"),
"disk_format": disk_format,
"name": params["image_name"]}

try:
response = self.volume_api.copy_volume_to_image(context,
volume,
Expand Down
11 changes: 11 additions & 0 deletions cinder/image/image_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@
CONF.register_opts(image_helper_opts)


# NOTE(abhishekk): qemu-img convert command supports raw, qcow2, qed,
# vdi, vmdk and vhd disk-formats but glance doesn't support qed and
# vhd(vpc) disk-formats.
# Ref: http://docs.openstack.org/image-guide/convert-images.html
VALID_DISK_FORMATS = ('raw', 'vmdk', 'vdi', 'qcow2')


def validate_disk_format(disk_format):
return disk_format in VALID_DISK_FORMATS


def qemu_img_info(path, run_as_root=True):
"""Return an object containing the parsed output from qemu-img info."""
cmd = ('env', 'LC_ALL=C', 'qemu-img', 'info', path)
Expand Down
14 changes: 14 additions & 0 deletions cinder/tests/unit/api/contrib/test_volume_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,20 @@ def stub_upload_volume_to_image_service_raise(self, context, volume,
id,
body)

def test_copy_volume_to_image_invalid_disk_format(self):
id = 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'
vol = {"container_format": 'bare',
"disk_format": 'iso',
"image_name": 'image_name',
"force": True}
body = {"os-volume_upload_image": vol}
req = fakes.HTTPRequest.blank('/v2/tenant1/volumes/%s/action' % id)
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller._volume_upload_image,
req,
id,
body)

def test_copy_volume_to_image_valueerror(self):
def stub_upload_volume_to_image_service_raise(self, context, volume,
metadata, force):
Expand Down

0 comments on commit 911ee41

Please sign in to comment.