From e44751162b09c5b57557b89db27656b5bd23341c Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Mon, 6 Aug 2012 12:17:43 -0700 Subject: Allow nova to guess device if not passed to attach partial fix for bug 1004328 Only the xen hypervisor actually respects the device name that is passed in attach_volume. For other hypervisors it makes much more sense to automatically generate a unique name. This patch generates a non-conflicting device name if one is not passed to attach_volume. It also validates the passed in volume name to make sure another device isn't already attached there. A corresponding change to novaclient and horizon will greatly improve the user experience of attaching a volume. It moves some common code out of metadata/base so that it can be used to get a list of block devices. The code was functionally tested as well and block device name generation works properly. This adds a new method to the rpcapi to validate a device name. It also adds server_id to the volumes extension, since it was omitted by mistake. The next step is to modify the libvirt driver to match the serial number of the device to the volume uuid so that the volume can always be found at /dev/disk/by-id/virtio-. DocImpact Change-Id: I0b9454fc50a5c93b4aea38545dcee98f68d7e511 --- nova/api/metadata/base.py | 51 ++------------------------- nova/api/openstack/compute/contrib/volumes.py | 8 +++-- 2 files changed, 7 insertions(+), 52 deletions(-) (limited to 'nova/api') diff --git a/nova/api/metadata/base.py b/nova/api/metadata/base.py index aa18eceb0..d9710dc37 100644 --- a/nova/api/metadata/base.py +++ b/nova/api/metadata/base.py @@ -45,11 +45,6 @@ flags.DECLARE('dhcp_domain', 'nova.network.manager') FLAGS.register_opts(metadata_opts) -_DEFAULT_MAPPINGS = {'ami': 'sda1', - 'ephemeral0': 'sda2', - 'root': block_device.DEFAULT_ROOT_DEV_NAME, - 'swap': 'sda3'} - VERSIONS = [ '1.0', '2007-01-19', @@ -387,50 +382,8 @@ def get_metadata_by_address(address): def _format_instance_mapping(ctxt, instance): - root_device_name = instance['root_device_name'] - if root_device_name is None: - return _DEFAULT_MAPPINGS - - mappings = {} - mappings['ami'] = block_device.strip_dev(root_device_name) - mappings['root'] = root_device_name - default_ephemeral_device = instance.get('default_ephemeral_device') - if default_ephemeral_device: - mappings['ephemeral0'] = default_ephemeral_device - default_swap_device = instance.get('default_swap_device') - if default_swap_device: - mappings['swap'] = default_swap_device - ebs_devices = [] - - # 'ephemeralN', 'swap' and ebs - for bdm in db.block_device_mapping_get_all_by_instance( - ctxt, instance['uuid']): - if bdm['no_device']: - continue - - # ebs volume case - if (bdm['volume_id'] or bdm['snapshot_id']): - ebs_devices.append(bdm['device_name']) - continue - - virtual_name = bdm['virtual_name'] - if not virtual_name: - continue - - if block_device.is_swap_or_ephemeral(virtual_name): - mappings[virtual_name] = bdm['device_name'] - - # NOTE(yamahata): I'm not sure how ebs device should be numbered. - # Right now sort by device name for deterministic - # result. - if ebs_devices: - nebs = 0 - ebs_devices.sort() - for ebs in ebs_devices: - mappings['ebs%d' % nebs] = ebs - nebs += 1 - - return mappings + bdms = db.block_device_mapping_get_all_by_instance(ctxt, instance['uuid']) + return block_device.instance_block_mapping(instance, bdms) def ec2_md_print(data): diff --git a/nova/api/openstack/compute/contrib/volumes.py b/nova/api/openstack/compute/contrib/volumes.py index e566a95f7..99d713cef 100644 --- a/nova/api/openstack/compute/contrib/volumes.py +++ b/nova/api/openstack/compute/contrib/volumes.py @@ -339,7 +339,7 @@ class VolumeAttachmentController(object): raise exc.HTTPUnprocessableEntity() volume_id = body['volumeAttachment']['volumeId'] - device = body['volumeAttachment']['device'] + device = body['volumeAttachment'].get('device') msg = _("Attach volume %(volume_id)s to instance %(server_id)s" " at %(device)s") % locals() @@ -347,15 +347,17 @@ class VolumeAttachmentController(object): try: instance = self.compute_api.get(context, server_id) - self.compute_api.attach_volume(context, instance, - volume_id, device) + device = self.compute_api.attach_volume(context, instance, + volume_id, device) except exception.NotFound: raise exc.HTTPNotFound() # The attach is async attachment = {} attachment['id'] = volume_id + attachment['serverId'] = server_id attachment['volumeId'] = volume_id + attachment['device'] = device # NOTE(justinsb): And now, we have a problem... # The attach is async, so there's a window in which we don't see -- cgit