diff --git a/nova/tests/virt/libvirt/test_libvirt.py b/nova/tests/virt/libvirt/test_libvirt.py index c17698502a6..ea2aacde757 100644 --- a/nova/tests/virt/libvirt/test_libvirt.py +++ b/nova/tests/virt/libvirt/test_libvirt.py @@ -3047,6 +3047,48 @@ def test_create_images_and_backing(self): conn._create_images_and_backing(self.context, self.test_instance, "/fake/instance/dir", disk_info_json) + def test_create_images_and_backing_ephemeral_gets_created(self): + conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False) + disk_info_json = jsonutils.dumps( + [{u'backing_file': u'fake_image_backing_file', + u'disk_size': 10747904, + u'path': u'disk_path', + u'type': u'qcow2', + u'virt_disk_size': 25165824}, + {u'backing_file': u'ephemeral_1_default', + u'disk_size': 393216, + u'over_committed_disk_size': 1073348608, + u'path': u'disk_eph_path', + u'type': u'qcow2', + u'virt_disk_size': 1073741824}]) + + base_dir = os.path.join(CONF.instances_path, + CONF.base_dir_name) + self.test_instance.update({'name': 'fake_instance', + 'user_id': 'fake-user', + 'os_type': None, + 'project_id': 'fake-project'}) + + with contextlib.nested( + mock.patch.object(conn, '_fetch_instance_kernel_ramdisk'), + mock.patch.object(libvirt_driver.libvirt_utils, 'fetch_image'), + mock.patch.object(conn, '_create_ephemeral') + ) as (fetch_kernel_ramdisk_mock, fetch_image_mock, + create_ephemeral_mock): + conn._create_images_and_backing(self.context, self.test_instance, + "/fake/instance/dir", + disk_info_json) + self.assertEqual(len(create_ephemeral_mock.call_args_list), 1) + m_args, m_kwargs = create_ephemeral_mock.call_args_list[0] + self.assertEqual( + os.path.join(base_dir, 'ephemeral_1_default'), + m_kwargs['target']) + self.assertEqual(len(fetch_image_mock.call_args_list), 1) + m_args, m_kwargs = fetch_image_mock.call_args_list[0] + self.assertEqual( + os.path.join(base_dir, 'fake_image_backing_file'), + m_kwargs['target']) + def test_create_images_and_backing_disk_info_none(self): conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False) self.mox.StubOutWithMock(conn, '_fetch_instance_kernel_ramdisk') diff --git a/nova/virt/libvirt/driver.py b/nova/virt/libvirt/driver.py index 500ce516f0d..19875fe2392 100644 --- a/nova/virt/libvirt/driver.py +++ b/nova/virt/libvirt/driver.py @@ -4210,13 +4210,28 @@ def _create_images_and_backing(self, context, instance, instance_dir, image = self.image_backend.image(instance, instance_disk, CONF.libvirt_images_type) - image.cache(fetch_func=libvirt_utils.fetch_image, - context=context, - filename=cache_name, - image_id=instance['image_ref'], - user_id=instance['user_id'], - project_id=instance['project_id'], - size=info['virt_disk_size']) + if cache_name.startswith('ephemeral'): + image.cache(fetch_func=self._create_ephemeral, + fs_label=cache_name, + os_type=instance["os_type"], + filename=cache_name, + size=info['virt_disk_size'], + ephemeral_size=instance['ephemeral_gb']) + elif cache_name.startswith('swap'): + inst_type = flavors.extract_flavor(instance) + swap_mb = inst_type['swap'] + image.cache(fetch_func=self._create_swap, + filename="swap_%s" % swap_mb, + size=swap_mb * (1024 ** 2), + swap_mb=swap_mb) + else: + image.cache(fetch_func=libvirt_utils.fetch_image, + context=context, + filename=cache_name, + image_id=instance['image_ref'], + user_id=instance['user_id'], + project_id=instance['project_id'], + size=info['virt_disk_size']) # if image has kernel and ramdisk, just download # following normal way.