Prevent access to image when filesystem resize is disabled

When resizing filesystems is disabled, the extend() function currently
accesses an image of a spawning instance to determine if it is
extendable. This check is not necessary as the image is not resized
afterwards.

When there is no method available to access the image (no libguestfs
or nbd installed), the superfluous access causes delays in the spawn
procedure.

With this fix, extend() returns if the resize should not be performed
according to the configuration before trying to access the image
in is_image_extendable().

Change-Id: Icb2f58b005539fec18b49d629ecd568d1f897e51
Closes-bug: #1438245
This commit is contained in:
Alexander Schmidt 2015-03-30 17:01:46 +02:00
parent b6e5d68c66
commit 5180094153
2 changed files with 28 additions and 9 deletions

View File

@ -142,6 +142,23 @@ class APITestCase(test.NoDBTestCase):
self.mox.ReplayAll()
api.extend(imgfile, imgsize, use_cow=use_cow)
@mock.patch.object(api, 'can_resize_image', return_value=True)
@mock.patch.object(api, 'is_image_extendable')
@mock.patch.object(utils, 'execute')
def test_extend_qcow_no_resize(self, mock_execute, mock_extendable,
mock_can_resize_image):
imgfile = tempfile.NamedTemporaryFile()
imgsize = 10
self.flags(resize_fs_using_block_device=False)
api.extend(imgfile, imgsize, use_cow=True)
mock_can_resize_image.assert_called_once_with(imgfile, imgsize)
mock_execute.assert_called_once_with('qemu-img', 'resize', imgfile,
imgsize)
self.assertFalse(mock_extendable.called)
def test_extend_raw_success(self):
imgfile = tempfile.NamedTemporaryFile()
imgsize = 10

View File

@ -179,6 +179,9 @@ def extend(image, size, use_cow=False):
utils.execute('qemu-img', 'resize', image, size)
if use_cow and not CONF.resize_fs_using_block_device:
return
# if we can't access the filesystem, we can't do anything more
if not is_image_extendable(image, use_cow):
return
@ -194,15 +197,14 @@ def extend(image, size, use_cow=False):
# NOTE(vish): attempts to resize filesystem
if use_cow:
if CONF.resize_fs_using_block_device:
# in case of non-raw disks we can't just resize the image, but
# rather the mounted device instead
mounter = mount.Mount.instance_for_format(
image, None, None, 'qcow2')
if mounter.get_dev():
safe_resize2fs(mounter.device,
run_as_root=True,
finally_call=mounter.unget_dev)
# in case of non-raw disks we can't just resize the image, but
# rather the mounted device instead
mounter = mount.Mount.instance_for_format(
image, None, None, 'qcow2')
if mounter.get_dev():
safe_resize2fs(mounter.device,
run_as_root=True,
finally_call=mounter.unget_dev)
else:
safe_resize2fs(image)