From 549144f754a8b3adb9d7fdfa8f8f9ee186a52f1e Mon Sep 17 00:00:00 2001 From: wuyuting Date: Tue, 27 Jan 2015 02:50:28 +0800 Subject: [PATCH] Fetch_to_volume_format calls copy_volume using wrong parameter When creating a volume from an image, if qemu-img is not installed, fetch_to_volume_format will call volume_utils.copy_volume to copy image to volume. Copy_volume need the size of image in megabyte, but fetch_to_volume_format call it using size in bytes. Change-Id: Ia3b0f9168235a977a12232e27a5755ad11ec18f5 Closes-Bug: #1414867 --- cinder/image/image_utils.py | 4 +++- cinder/tests/test_image_utils.py | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cinder/image/image_utils.py b/cinder/image/image_utils.py index ff0570d05..bb1da6d00 100644 --- a/cinder/image/image_utils.py +++ b/cinder/image/image_utils.py @@ -25,6 +25,7 @@ we should look at maybe pushing this up to Oslo import contextlib +import math import os import tempfile @@ -238,7 +239,8 @@ def fetch_to_volume_format(context, image_service, LOG.debug('Copying image from %(tmp)s to volume %(dest)s - ' 'size: %(size)s' % {'tmp': tmp, 'dest': dest, 'size': image_meta['size']}) - volume_utils.copy_volume(tmp, dest, image_meta['size'], blocksize) + image_size_m = math.ceil(image_meta['size'] / units.Mi) + volume_utils.copy_volume(tmp, dest, image_size_m, blocksize) return data = qemu_img_info(tmp, run_as_root=run_as_root) diff --git a/cinder/tests/test_image_utils.py b/cinder/tests/test_image_utils.py index 5028c83f5..7217e726b 100644 --- a/cinder/tests/test_image_utils.py +++ b/cinder/tests/test_image_utils.py @@ -15,8 +15,11 @@ # under the License. """Unit tests for image utils.""" +import math + import mock from oslo_concurrency import processutils +from oslo_utils import units from cinder import exception from cinder.image import image_utils @@ -648,7 +651,8 @@ class TestFetchToVolumeFormat(test.TestCase): mock_conf.volume_copy_bps_limit = bps_limit tmp = mock_temp.return_value.__enter__.return_value image_service.show.return_value = {'disk_format': 'raw', - 'size': mock.sentinel.image_size} + 'size': 41126400} + image_size_m = math.ceil(41126400 / units.Mi) output = image_utils.fetch_to_volume_format( ctxt, image_service, image_id, dest, volume_format, blocksize, @@ -662,7 +666,7 @@ class TestFetchToVolumeFormat(test.TestCase): mock_fetch.assert_called_once_with(ctxt, image_service, image_id, tmp, user_id, project_id) self.assertFalse(mock_repl_xen.called) - mock_copy.assert_called_once_with(tmp, dest, mock.sentinel.image_size, + mock_copy.assert_called_once_with(tmp, dest, image_size_m, blocksize) self.assertFalse(mock_convert.called) -- 2.45.2