Merge "Pass block_device_info when delete an encrypted lvm"

This commit is contained in:
Jenkins 2015-09-17 21:15:47 +00:00 committed by Gerrit Code Review
commit 6484b01a5b
2 changed files with 86 additions and 5 deletions

View File

@ -92,6 +92,7 @@ from nova.virt.libvirt import firewall
from nova.virt.libvirt import guest as libvirt_guest from nova.virt.libvirt import guest as libvirt_guest
from nova.virt.libvirt import host from nova.virt.libvirt import host
from nova.virt.libvirt import imagebackend from nova.virt.libvirt import imagebackend
from nova.virt.libvirt.storage import dmcrypt
from nova.virt.libvirt.storage import lvm from nova.virt.libvirt.storage import lvm
from nova.virt.libvirt.storage import rbd_utils from nova.virt.libvirt.storage import rbd_utils
from nova.virt.libvirt import utils as libvirt_utils from nova.virt.libvirt import utils as libvirt_utils
@ -13876,6 +13877,85 @@ class LibvirtDriverTestCase(test.NoDBTestCase):
self.assertEqual(cpumodel.MODE_HOST_MODEL, vcpu_model.mode) self.assertEqual(cpumodel.MODE_HOST_MODEL, vcpu_model.mode)
self.assertEqual(vcpu_model, vcpu_model_1) self.assertEqual(vcpu_model, vcpu_model_1)
@mock.patch.object(lvm, 'get_volume_size', return_value=10)
@mock.patch.object(host.Host, "get_guest")
@mock.patch.object(dmcrypt, 'delete_volume')
@mock.patch('nova.virt.libvirt.driver.LibvirtDriver.unfilter_instance')
@mock.patch('nova.virt.libvirt.driver.LibvirtDriver._undefine_domain')
@mock.patch.object(objects.Instance, 'save')
def test_cleanup_lvm_encrypted(self, mock_save, mock_undefine_domain,
mock_unfilter, mock_delete_volume,
mock_get_guest, mock_get_size):
drv = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)
instance = objects.Instance(uuid='fake-uuid', id=1,
ephemeral_key_uuid='000-000-000')
instance.system_metadata = {}
block_device_info = {'root_device_name': '/dev/vda',
'ephemerals': [],
'block_device_mapping': []}
self.flags(images_type="lvm",
group='libvirt')
dom_xml = """
<domain type="kvm">
<devices>
<disk type="block">
<driver name='qemu' type='raw' cache='none'/>
<source dev="/dev/mapper/fake-dmcrypt"/>
<target dev="vda" bus="virtio" serial="1234"/>
</disk>
</devices>
</domain>
"""
dom = mock.MagicMock()
dom.XMLDesc.return_value = dom_xml
guest = libvirt_guest.Guest(dom)
mock_get_guest.return_value = guest
drv.cleanup(self.context, instance, 'fake_network', destroy_vifs=False,
block_device_info=block_device_info)
mock_delete_volume.assert_called_once_with('/dev/mapper/fake-dmcrypt')
@mock.patch.object(lvm, 'get_volume_size', return_value=10)
@mock.patch.object(host.Host, "get_guest")
@mock.patch.object(dmcrypt, 'delete_volume')
def _test_cleanup_lvm(self, mock_delete_volume, mock_get_guest, mock_size,
encrypted=False):
drv = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)
instance = objects.Instance(uuid='fake-uuid', id=1,
ephemeral_key_uuid='000-000-000')
block_device_info = {'root_device_name': '/dev/vda',
'ephemerals': [],
'block_device_mapping': []}
dev_name = 'fake-dmcrypt' if encrypted else 'fake'
dom_xml = """
<domain type="kvm">
<devices>
<disk type="block">
<driver name='qemu' type='raw' cache='none'/>
<source dev="/dev/mapper/%s"/>
<target dev="vda" bus="virtio" serial="1234"/>
</disk>
</devices>
</domain>
""" % dev_name
dom = mock.MagicMock()
dom.XMLDesc.return_value = dom_xml
guest = libvirt_guest.Guest(dom)
mock_get_guest.return_value = guest
drv._cleanup_lvm(instance, block_device_info)
if encrypted:
mock_delete_volume.assert_called_once_with(
'/dev/mapper/fake-dmcrypt')
else:
self.assertFalse(mock_delete_volume.called)
def test_cleanup_lvm(self):
self._test_cleanup_lvm()
def test_cleanup_encrypted_lvm(self):
self._test_cleanup_lvm(encrypted=True)
def test_vcpu_model_to_config(self): def test_vcpu_model_to_config(self):
drv = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True) drv = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)

View File

@ -914,7 +914,7 @@ class LibvirtDriver(driver.ComputeDriver):
if destroy_disks: if destroy_disks:
# NOTE(haomai): destroy volumes if needed # NOTE(haomai): destroy volumes if needed
if CONF.libvirt.images_type == 'lvm': if CONF.libvirt.images_type == 'lvm':
self._cleanup_lvm(instance) self._cleanup_lvm(instance, block_device_info)
if CONF.libvirt.images_type == 'rbd': if CONF.libvirt.images_type == 'rbd':
self._cleanup_rbd(instance) self._cleanup_rbd(instance)
@ -942,9 +942,10 @@ class LibvirtDriver(driver.ComputeDriver):
self._undefine_domain(instance) self._undefine_domain(instance)
def _detach_encrypted_volumes(self, instance): def _detach_encrypted_volumes(self, instance, block_device_info):
"""Detaches encrypted volumes attached to instance.""" """Detaches encrypted volumes attached to instance."""
disks = jsonutils.loads(self.get_instance_disk_info(instance)) disks = jsonutils.loads(self.get_instance_disk_info(instance,
block_device_info))
encrypted_volumes = filter(dmcrypt.is_encrypted, encrypted_volumes = filter(dmcrypt.is_encrypted,
[disk['path'] for disk in disks]) [disk['path'] for disk in disks])
for path in encrypted_volumes: for path in encrypted_volumes:
@ -980,10 +981,10 @@ class LibvirtDriver(driver.ComputeDriver):
def _cleanup_rbd(self, instance): def _cleanup_rbd(self, instance):
LibvirtDriver._get_rbd_driver().cleanup_volumes(instance) LibvirtDriver._get_rbd_driver().cleanup_volumes(instance)
def _cleanup_lvm(self, instance): def _cleanup_lvm(self, instance, block_device_info):
"""Delete all LVM disks for given instance object.""" """Delete all LVM disks for given instance object."""
if instance.get('ephemeral_key_uuid') is not None: if instance.get('ephemeral_key_uuid') is not None:
self._detach_encrypted_volumes(instance) self._detach_encrypted_volumes(instance, block_device_info)
disks = self._lvm_disks(instance) disks = self._lvm_disks(instance)
if disks: if disks: