use 'os_type' in ephemeral filename only if mkfs defined

Currently for undefined os-types it will use the default mkfs
command, but use the meta 'os_type' in the name of the
ephemeral file (e.g. ephemeral_20_abcdef). Which can result
in a lot of files (DoS?)
This change will only use 'os_type' in the ephemeral filename
if there is a specific mkfs command defined, otherwise it will
use 'default' (e.g. ephemeral_20_default)

Modifed the tests to test for:
  os_type=''
  os_type=None
  os_type='test' - with no mkfs command specified
  os_type='test' - with a mkfs command specified

Closes-Bug: 1253980

Change-Id: Ie4c10f99ce690c5e4ef181624bd688c38923855c
This commit is contained in:
Ryan Moore 2013-11-29 14:21:19 +00:00 committed by Thierry Carrez
parent 2efc5bca21
commit 6e455cd97f
3 changed files with 32 additions and 5 deletions

View File

@ -3787,7 +3787,7 @@ class LibvirtConnTestCase(test.TestCase):
self.mox.ReplayAll() self.mox.ReplayAll()
conn._chown_disk_config_for_instance(instance) conn._chown_disk_config_for_instance(instance)
def test_create_image_plain(self): def _test_create_image_plain(self, os_type='', filename='', mkfs=False):
gotFiles = [] gotFiles = []
def fake_image(self, instance, name, image_type=''): def fake_image(self, instance, name, image_type=''):
@ -3823,11 +3823,15 @@ class LibvirtConnTestCase(test.TestCase):
instance_ref = self.test_instance instance_ref = self.test_instance
instance_ref['image_ref'] = 1 instance_ref['image_ref'] = 1
instance = db.instance_create(self.context, instance_ref) instance = db.instance_create(self.context, instance_ref)
instance['os_type'] = os_type
conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False) conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
self.stubs.Set(conn, 'to_xml', fake_none) self.stubs.Set(conn, 'to_xml', fake_none)
self.stubs.Set(conn, '_create_domain_and_network', fake_none) self.stubs.Set(conn, '_create_domain_and_network', fake_none)
self.stubs.Set(conn, 'get_info', fake_get_info) self.stubs.Set(conn, 'get_info', fake_get_info)
if mkfs:
self.stubs.Set(nova.virt.disk.api, '_MKFS_COMMAND',
{os_type: 'mkfs.ext3 --label %(fs_label)s %(target)s'})
image_meta = {'id': instance['image_ref']} image_meta = {'id': instance['image_ref']}
disk_info = blockinfo.get_disk_info(CONF.libvirt.virt_type, disk_info = blockinfo.get_disk_info(CONF.libvirt.virt_type,
@ -3842,11 +3846,31 @@ class LibvirtConnTestCase(test.TestCase):
wantFiles = [ wantFiles = [
{'filename': '356a192b7913b04c54574d18c28d46e6395428ab', {'filename': '356a192b7913b04c54574d18c28d46e6395428ab',
'size': 10 * unit.Gi}, 'size': 10 * unit.Gi},
{'filename': 'ephemeral_20_default', {'filename': filename,
'size': 20 * unit.Gi}, 'size': 20 * unit.Gi},
] ]
self.assertEqual(gotFiles, wantFiles) self.assertEqual(gotFiles, wantFiles)
def test_create_image_plain_os_type_blank(self):
self._test_create_image_plain(os_type='',
filename='ephemeral_20_default',
mkfs=False)
def test_create_image_plain_os_type_none(self):
self._test_create_image_plain(os_type=None,
filename='ephemeral_20_default',
mkfs=False)
def test_create_image_plain_os_type_set_no_fs(self):
self._test_create_image_plain(os_type='test',
filename='ephemeral_20_default',
mkfs=False)
def test_create_image_plain_os_type_set_with_fs(self):
self._test_create_image_plain(os_type='test',
filename='ephemeral_20_test',
mkfs=True)
def test_create_image_with_swap(self): def test_create_image_with_swap(self):
gotFiles = [] gotFiles = []

View File

@ -95,6 +95,10 @@ for s in CONF.virt_mkfs:
_DEFAULT_MKFS_COMMAND = mkfs_command _DEFAULT_MKFS_COMMAND = mkfs_command
def get_fs_type_for_os_type(os_type):
return os_type if _MKFS_COMMAND.get(os_type) else 'default'
def mkfs(os_type, fs_label, target, run_as_root=True): def mkfs(os_type, fs_label, target, run_as_root=True):
"""Format a file or block device using """Format a file or block device using
a user provided command for each os type. a user provided command for each os type.

View File

@ -2466,9 +2466,8 @@ class LibvirtDriver(driver.ComputeDriver):
project_id=instance['project_id']) project_id=instance['project_id'])
# Lookup the filesystem type if required # Lookup the filesystem type if required
os_type_with_default = instance['os_type'] os_type_with_default = disk.get_fs_type_for_os_type(
if not os_type_with_default: instance['os_type'])
os_type_with_default = 'default'
ephemeral_gb = instance['ephemeral_gb'] ephemeral_gb = instance['ephemeral_gb']
if 'disk.local' in disk_mapping: if 'disk.local' in disk_mapping: