From 8705f49a4ad9c6f3a93f33e333475285a8f70a30 Mon Sep 17 00:00:00 2001 From: Rick Harris Date: Tue, 29 Jan 2013 07:26:48 +0000 Subject: Fix rebuild with volumes attached This patch assumes that the correct behavior for instance rebuild is to maintain attached volumes across a rebuild operation. Two important changes are: 1) Detaching all volumes during a rebuild so that they won't be 'in-use' when prep_block_devices is called to reattach them. 2) xenapi: Allowing additional volumes, not just root volumes, to be attached before boot. To handle this, we cycle through all block-device-mappings, not just the root-device, create the VDI, and later, create the VBD. Small changes include: * Using `connection_data` instead of `dev_params` (to match other parts of the code base) * Renaming `get_vdis_for_boot_from_vol` to `get_vdi_uuid_for_volume` to reflect its more general and simpler semantics. Fixes bug 1071547 Change-Id: Ie54a16be4bae2a718ed7d506f32777d0847b9089 --- nova/virt/xenapi/vm_utils.py | 76 +++++++++++++++++++++++++++----------------- nova/virt/xenapi/vmops.py | 17 +++++++++- 2 files changed, 62 insertions(+), 31 deletions(-) (limited to 'nova/virt') diff --git a/nova/virt/xenapi/vm_utils.py b/nova/virt/xenapi/vm_utils.py index 782353b2b..3972cf765 100644 --- a/nova/virt/xenapi/vm_utils.py +++ b/nova/virt/xenapi/vm_utils.py @@ -460,50 +460,66 @@ def create_vdi(session, sr_ref, instance, name_label, disk_type, virtual_size, return vdi_ref -def get_vdis_for_boot_from_vol(session, dev_params): - vdis = {} - sr_uuid, label, sr_params = volume_utils.parse_sr_info(dev_params) +def get_vdi_uuid_for_volume(session, connection_data): + sr_uuid, label, sr_params = volume_utils.parse_sr_info(connection_data) sr_ref = volume_utils.find_sr_by_uuid(session, sr_uuid) - # Try introducing SR if it is not present + if not sr_ref: sr_ref = volume_utils.introduce_sr(session, sr_uuid, label, sr_params) if sr_ref is None: raise exception.NovaException(_('SR not present and could not be ' 'introduced')) + + vdi_uuid = None + + if 'vdi_uuid' in connection_data: + session.call_xenapi("SR.scan", sr_ref) + vdi_uuid = connection_data['vdi_uuid'] else: - if 'vdi_uuid' in dev_params: - session.call_xenapi("SR.scan", sr_ref) - vdis = {'root': dict(uuid=dev_params['vdi_uuid'], - file=None, osvol=True)} - else: - try: - vdi_ref = volume_utils.introduce_vdi(session, sr_ref) - vdi_rec = session.call_xenapi("VDI.get_record", vdi_ref) - vdis = {'root': dict(uuid=vdi_rec['uuid'], - file=None, osvol=True)} - except volume_utils.StorageError, exc: - LOG.exception(exc) - volume_utils.forget_sr(session, sr_uuid) - return vdis + try: + vdi_ref = volume_utils.introduce_vdi(session, sr_ref) + vdi_rec = session.call_xenapi("VDI.get_record", vdi_ref) + vdi_uuid = vdi_rec['uuid'] + except volume_utils.StorageError, exc: + LOG.exception(exc) + volume_utils.forget_sr(session, sr_uuid) + + return vdi_uuid def get_vdis_for_instance(context, session, instance, name_label, image, image_type, block_device_info=None): + vdis = {} + if block_device_info: LOG.debug(_("block device info: %s"), block_device_info) - rootdev = block_device_info['root_device_name'] - if block_device.volume_in_mapping(rootdev, block_device_info, - strip=block_device.strip_prefix): - # call function to return the vdi in connection info of block - # device. - # make it a point to return from here. - bdm_root_dev = block_device_info['block_device_mapping'][0] - dev_params = bdm_root_dev['connection_info']['data'] - LOG.debug(dev_params) - return get_vdis_for_boot_from_vol(session, dev_params) - return _create_image(context, session, instance, name_label, image, - image_type) + root_device_name = block_device_info['root_device_name'] + + for bdm in block_device_info['block_device_mapping']: + if (block_device.strip_prefix(bdm['mount_device']) == + block_device.strip_prefix(root_device_name)): + # If we're a root-device, record that fact so we don't download + # a root image via Glance + type_ = 'root' + else: + # Otherwise, use mount_device as `type_` so that we have easy + # access to it in _attach_disks to create the VBD + type_ = bdm['mount_device'] + + connection_data = bdm['connection_info']['data'] + vdi_uuid = get_vdi_uuid_for_volume(session, connection_data) + if vdi_uuid: + vdis[type_] = dict(uuid=vdi_uuid, file=None, osvol=True) + + # If we didn't get a root VDI from volumes, then use the Glance image as + # the root device + if 'root' not in vdis: + create_image_vdis = _create_image( + context, session, instance, name_label, image, image_type) + vdis.update(create_image_vdis) + + return vdis @contextlib.contextmanager diff --git a/nova/virt/xenapi/vmops.py b/nova/virt/xenapi/vmops.py index e10b52ad6..cd3a2f2f0 100644 --- a/nova/virt/xenapi/vmops.py +++ b/nova/virt/xenapi/vmops.py @@ -26,6 +26,7 @@ import time from eventlet import greenthread import netaddr +from nova import block_device from nova.compute import api as compute from nova.compute import power_state from nova.compute import task_states @@ -503,8 +504,9 @@ class VMOps(object): ctx = nova_context.get_admin_context() instance_type = instance['instance_type'] - # DISK_ISO needs two VBDs: the ISO disk and a blank RW disk + # Attach (required) root disk if disk_image_type == vm_utils.ImageType.DISK_ISO: + # DISK_ISO needs two VBDs: the ISO disk and a blank RW disk LOG.debug(_("Detected ISO image type, creating blank VM " "for install"), instance=instance) @@ -532,6 +534,19 @@ class VMOps(object): DEVICE_ROOT, bootable=True, osvol=root_vdi.get('osvol')) + # Attach (optional) additional block-devices + for type_, vdi_info in vdis.items(): + # Additional block-devices for boot use their device-name as the + # type. + if not type_.startswith('/dev'): + continue + + # Convert device name to userdevice number, e.g. /dev/xvdb -> 1 + userdevice = ord(block_device.strip_prefix(type_)) - ord('a') + vm_utils.create_vbd(self._session, vm_ref, vdi_info['ref'], + userdevice, bootable=False, + osvol=vdi_info.get('osvol')) + # Attach (optional) swap disk swap_mb = instance_type['swap'] if swap_mb: -- cgit