Skip to content

Commit

Permalink
Create a flag for force_to_raw for images
Browse files Browse the repository at this point in the history
 * Allows for deployers to use compressed images if they prefer
 * Flag defaults to current behavior
 * Fixes bug 932180
 * Re-enables fetch_to_raw which was being skipped
 * Removes obsolete and broken method from libvirt
 * Checksumming will be re-enabled in a later patch

Change-Id: I40c33cfd3997cae8d127fa45f8cf0aa071418170
  • Loading branch information
vishvananda committed Feb 16, 2012
1 parent 3e1cff3 commit e48c252
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 29 deletions.
4 changes: 2 additions & 2 deletions nova/tests/test_libvirt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1975,14 +1975,14 @@ def fake_statvfs(path):
self.assertEquals(4096000, fs_info['used'])

def test_fetch_image(self):
self.mox.StubOutWithMock(images, 'fetch')
self.mox.StubOutWithMock(images, 'fetch_to_raw')

context = 'opaque context'
target = '/tmp/targetfile'
image_id = '4'
user_id = 'fake'
project_id = 'fake'
images.fetch(context, image_id, target, user_id, project_id)
images.fetch_to_raw(context, image_id, target, user_id, project_id)

self.mox.ReplayAll()
libvirt_utils.fetch_image(context, target, image_id,
Expand Down
26 changes: 18 additions & 8 deletions nova/virt/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,22 @@
from nova import flags
import nova.image
from nova import log as logging
from nova.openstack.common import cfg
from nova import utils


FLAGS = flags.FLAGS
LOG = logging.getLogger(__name__)

image_opts = [
cfg.BoolOpt('force_raw_images',
default=True,
help='Force backing images to raw format'),
]

FLAGS = flags.FLAGS
FLAGS.register_opts(image_opts)


def fetch(context, image_href, path, _user_id, _project_id):
# TODO(vish): Improve context handling and add owner and auth data
Expand Down Expand Up @@ -72,20 +82,20 @@ def _qemu_img_info(path):

data = _qemu_img_info(path_tmp)

fmt = data.get("file format", None)
fmt = data.get("file format")
if fmt is None:
os.unlink(path_tmp)
raise exception.ImageUnacceptable(
reason=_("'qemu-img info' parsing failed."), image_id=image_href)

if fmt != "raw":
staged = "%s.converted" % path
if "backing file" in data:
backing_file = data['backing file']
os.unlink(path_tmp)
raise exception.ImageUnacceptable(image_id=image_href,
reason=_("fmt=%(fmt)s backed by: %(backing_file)s") % locals())
if "backing file" in data:
backing_file = data['backing file']
os.unlink(path_tmp)
raise exception.ImageUnacceptable(image_id=image_href,
reason=_("fmt=%(fmt)s backed by: %(backing_file)s") % locals())

if fmt != "raw" and FLAGS.force_raw_images:
staged = "%s.converted" % path
LOG.debug("%s was %s, converting to raw" % (image_href, fmt))
out, err = utils.execute('qemu-img', 'convert', '-O', 'raw',
path_tmp, staged)
Expand Down
17 changes: 1 addition & 16 deletions nova/virt/libvirt/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,21 +933,6 @@ def call_if_not_exists(base, fn, *args, **kwargs):
if size:
disk.extend(target, size)

@staticmethod
def _fetch_image(context, target, image_id, user_id, project_id):
"""Grab image to raw format"""
images.fetch_to_raw(context, image_id, target, user_id, project_id)

if FLAGS.checksum_base_images:
f = open(target, 'r')
checksum = utils.hash_file(f)
f.close()

checksum_fname = '%s.sha1' % target
fd = os.open(checksum_filename, os.O_WRONLY, mode=0444)
os.write(fd, checksum)
os.close(fd)

@staticmethod
def _create_local(target, local_size, unit='G', fs_format=None):
"""Create a blank image of specified size"""
Expand Down Expand Up @@ -1912,7 +1897,7 @@ def pre_block_migration(self, ctxt, instance_ref, disk_info_json):
cached_file = info['backing_file'].split('_')[0]

if not os.path.exists(backing_file):
self._cache_image(fn=self._fetch_image,
self._cache_image(fn=libvirt_utils.fetch_image,
context=ctxt,
target=info['path'],
fname=cached_file,
Expand Down
4 changes: 1 addition & 3 deletions nova/virt/libvirt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@

import os
import random
import shutil

from nova import exception
from nova import flags
from nova.openstack.common import cfg
from nova import utils
from nova.virt.disk import api as disk
from nova.virt import images


Expand Down Expand Up @@ -258,4 +256,4 @@ def get_fs_info(path):

def fetch_image(context, target, image_id, user_id, project_id):
"""Grab image"""
images.fetch(context, image_id, target, user_id, project_id)
images.fetch_to_raw(context, image_id, target, user_id, project_id)

0 comments on commit e48c252

Please sign in to comment.