class VolumeOps(object): """ Management class for Volume-related tasks """ def __init__(self, session): self.XenAPI = session.get_imported_xenapi() self._session = session # Load XenAPI module in the helper classes respectively VolumeHelper.XenAPI = self.XenAPI VMHelper.XenAPI = self.XenAPI def create_volume_for_sm(self, volume, sr_uuid): LOG.debug("Creating volume for Storage Manager") sm_vol_rec = {} try: sr_ref = self._session.call_xenapi("SR.get_by_uuid", sr_uuid) except self.XenAPI.Failure, exc: LOG.exception(exc) raise StorageError(_('Unable to get SR using uuid')) #Create VDI label = 'vol-' + hex(volume['id'])[:-1] # size presented to xenapi is in bytes, while euca api is in GB vdi_size = volume['size'] * 1024 * 1024 * 1024 vdi_ref = VMHelper.create_vdi(self._session, sr_ref, label, vdi_size, False) vdi_rec = self._session.call_xenapi("VDI.get_record", vdi_ref) sm_vol_rec['vdi_uuid'] = vdi_rec['uuid'] return sm_vol_rec
def plug(self, instance, network, mapping, vm_ref=None, device=None): if not vm_ref: vm_ref = VMHelper.lookup(self._session, instance.name) if not device: device = 0 if mapping.get('should_create_vlan'): network_ref = self._ensure_vlan_bridge(network) else: network_ref = NetworkHelper.find_network_with_bridge( self._session, network['bridge']) vif_rec = {} vif_rec['device'] = str(device) vif_rec['network'] = network_ref vif_rec['VM'] = vm_ref vif_rec['MAC'] = mapping['mac'] vif_rec['MTU'] = '1500' vif_rec['other_config'] = {} if "rxtx_cap" in mapping: vif_rec['qos_algorithm_type'] = "ratelimit" vif_rec['qos_algorithm_params'] = \ {"kbps": str(mapping['rxtx_cap'] * 1024)} else: vif_rec['qos_algorithm_type'] = "" vif_rec['qos_algorithm_params'] = {} return vif_rec
def detach_volume(self, connection_info, instance_name, mountpoint): """Detach volume storage to VM instance""" # Before we start, check that the VM exists vm_ref = VMHelper.lookup(self._session, instance_name) if vm_ref is None: raise exception.InstanceNotFound(instance_id=instance_name) # Detach VBD from VM LOG.debug(_("Detach_volume: %(instance_name)s, %(mountpoint)s") % locals()) device_number = VolumeHelper.mountpoint_to_number(mountpoint) try: vbd_ref = VMHelper.find_vbd_by_number(self._session, vm_ref, device_number) except StorageError, exc: LOG.exception(exc) raise Exception(_('Unable to locate volume %s') % mountpoint)
def detach_volume(self, connection_info, instance_name, mountpoint): """Detach volume storage to VM instance""" # Before we start, check that the VM exists vm_ref = VMHelper.lookup(self._session, instance_name) if vm_ref is None: raise exception.InstanceNotFound(instance_id=instance_name) # Detach VBD from VM LOG.debug( _("Detach_volume: %(instance_name)s, %(mountpoint)s") % locals()) device_number = VolumeHelper.mountpoint_to_number(mountpoint) try: vbd_ref = VMHelper.find_vbd_by_number(self._session, vm_ref, device_number) except StorageError, exc: LOG.exception(exc) raise Exception(_('Unable to locate volume %s') % mountpoint)
def attach_volume(self, connection_info, instance_name, mountpoint): """Attach volume storage to VM instance""" # Before we start, check that the VM exists vm_ref = VMHelper.lookup(self._session, instance_name) if vm_ref is None: raise exception.InstanceNotFound(instance_id=instance_name) # NOTE: No Resource Pool concept so far LOG.debug( _("Attach_volume: %(connection_info)s, %(instance_name)s," " %(mountpoint)s") % locals()) driver_type = connection_info['driver_volume_type'] if driver_type not in ['iscsi', 'xensm']: raise exception.VolumeDriverNotFound(driver_type=driver_type) data = connection_info['data'] if 'name_label' not in data: label = 'tempSR-%s' % data['volume_id'] else: label = data['name_label'] del data['name_label'] if 'name_description' not in data: desc = 'Disk-for:%s' % instance_name else: desc = data['name_description'] LOG.debug(connection_info) sr_params = {} if u'sr_uuid' not in data: sr_params = VolumeHelper.parse_volume_info(connection_info, mountpoint) uuid = "FA15E-D15C-" + str(sr_params['id']) sr_params['sr_type'] = 'iscsi' else: uuid = data['sr_uuid'] for k in data['introduce_sr_keys']: sr_params[k] = data[k] sr_params['name_description'] = desc # Introduce SR try: sr_ref = self.introduce_sr(uuid, label, sr_params) LOG.debug(_('Introduced %(label)s as %(sr_ref)s.') % locals()) except self.XenAPI.Failure, exc: LOG.exception(exc) raise StorageError(_('Unable to introduce Storage Repository'))
def attach_volume(self, connection_info, instance_name, mountpoint): """Attach volume storage to VM instance""" # Before we start, check that the VM exists vm_ref = VMHelper.lookup(self._session, instance_name) if vm_ref is None: raise exception.InstanceNotFound(instance_id=instance_name) # NOTE: No Resource Pool concept so far LOG.debug(_("Attach_volume: %(connection_info)s, %(instance_name)s," " %(mountpoint)s") % locals()) driver_type = connection_info['driver_volume_type'] if driver_type not in ['iscsi', 'xensm']: raise exception.VolumeDriverNotFound(driver_type=driver_type) data = connection_info['data'] if 'name_label' not in data: label = 'tempSR-%s' % data['volume_id'] else: label = data['name_label'] del data['name_label'] if 'name_description' not in data: desc = 'Disk-for:%s' % instance_name else: desc = data['name_description'] LOG.debug(connection_info) sr_params = {} if u'sr_uuid' not in data: sr_params = VolumeHelper.parse_volume_info(connection_info, mountpoint) uuid = "FA15E-D15C-" + str(sr_params['id']) sr_params['sr_type'] = 'iscsi' else: uuid = data['sr_uuid'] for k in data['introduce_sr_keys']: sr_params[k] = data[k] sr_params['name_description'] = desc # Introduce SR try: sr_ref = self.introduce_sr(uuid, label, sr_params) LOG.debug(_('Introduced %(label)s as %(sr_ref)s.') % locals()) except self.XenAPI.Failure, exc: LOG.exception(exc) raise StorageError(_('Unable to introduce Storage Repository'))
def plug(self, instance, network, mapping, vm_ref=None, device=None): if not vm_ref: vm_ref = VMHelper.lookup(self._session, instance.name) if not device: device = 0 # with OVS model, always plug into an OVS integration bridge # that is already created network_ref = NetworkHelper.find_network_with_bridge(self._session, FLAGS.xenapi_ovs_integration_bridge) vif_rec = {} vif_rec['device'] = str(device) vif_rec['network'] = network_ref vif_rec['VM'] = vm_ref vif_rec['MAC'] = mapping['mac'] vif_rec['MTU'] = '1500' vif_rec['qos_algorithm_type'] = "" vif_rec['qos_algorithm_params'] = {} # OVS on the hypervisor monitors this key and uses it to # set the iface-id attribute vif_rec['other_config'] = \ {"nicira-iface-id": mapping['vif_uuid']} return vif_rec
raise exception.InstanceNotFound(instance_id=instance_name) # Detach VBD from VM LOG.debug(_("Detach_volume: %(instance_name)s, %(mountpoint)s") % locals()) device_number = VolumeHelper.mountpoint_to_number(mountpoint) try: vbd_ref = VMHelper.find_vbd_by_number(self._session, vm_ref, device_number) except StorageError, exc: LOG.exception(exc) raise Exception(_('Unable to locate volume %s') % mountpoint) try: sr_ref = VolumeHelper.find_sr_from_vbd(self._session, vbd_ref) VMHelper.unplug_vbd(self._session, vbd_ref) except StorageError, exc: LOG.exception(exc) raise Exception(_('Unable to detach volume %s') % mountpoint) try: VMHelper.destroy_vbd(self._session, vbd_ref) except StorageError, exc: LOG.exception(exc) raise Exception(_('Unable to destroy vbd %s') % mountpoint) # Forget SR only if no other volumes on this host are using it try: VolumeHelper.purge_sr(self._session, sr_ref) except StorageError, exc: LOG.exception(exc) raise Exception(_('Error purging SR %s') % sr_ref)
if vm_ref is None: raise exception.InstanceNotFound(instance_id=instance_name) # Detach VBD from VM LOG.debug( _("Detach_volume: %(instance_name)s, %(mountpoint)s") % locals()) device_number = VolumeHelper.mountpoint_to_number(mountpoint) try: vbd_ref = VMHelper.find_vbd_by_number(self._session, vm_ref, device_number) except StorageError, exc: LOG.exception(exc) raise Exception(_('Unable to locate volume %s') % mountpoint) try: sr_ref = VolumeHelper.find_sr_from_vbd(self._session, vbd_ref) VMHelper.unplug_vbd(self._session, vbd_ref) except StorageError, exc: LOG.exception(exc) raise Exception(_('Unable to detach volume %s') % mountpoint) try: VMHelper.destroy_vbd(self._session, vbd_ref) except StorageError, exc: LOG.exception(exc) raise Exception(_('Unable to destroy vbd %s') % mountpoint) # Forget SR only if no other volumes on this host are using it try: VolumeHelper.purge_sr(self._session, sr_ref) except StorageError, exc: LOG.exception(exc) raise Exception(_('Error purging SR %s') % sr_ref)