def attach_disk_to_vm(self, vm_ref, instance, adapter_type, disk_type, vmdk_path=None, disk_size=None, linked_clone=False, device_name=None, disk_io_limits=None): """Attach disk to VM by reconfiguration.""" instance_name = instance.name client_factory = self._session.vim.client.factory devices = vm_util.get_hardware_devices(self._session, vm_ref) (controller_key, unit_number, controller_spec) = vm_util.allocate_controller_key_and_unit_number( client_factory, devices, adapter_type) vmdk_attach_config_spec = vm_util.get_vmdk_attach_config_spec( client_factory, disk_type, vmdk_path, disk_size, linked_clone, controller_key, unit_number, device_name, disk_io_limits) if controller_spec: vmdk_attach_config_spec.deviceChange.append(controller_spec) LOG.debug("Reconfiguring VM instance %(instance_name)s to attach " "disk %(vmdk_path)s or device %(device_name)s with type " "%(disk_type)s", {'instance_name': instance_name, 'vmdk_path': vmdk_path, 'device_name': device_name, 'disk_type': disk_type}, instance=instance) vm_util.reconfigure_vm(self._session, vm_ref, vmdk_attach_config_spec) LOG.debug("Reconfigured VM instance %(instance_name)s to attach " "disk %(vmdk_path)s or device %(device_name)s with type " "%(disk_type)s", {'instance_name': instance_name, 'vmdk_path': vmdk_path, 'device_name': device_name, 'disk_type': disk_type}, instance=instance)
def _attach_volume_iscsi(self, connection_info, instance, adapter_type=None): """Attach iscsi volume storage to VM instance.""" vm_ref = vm_util.get_vm_ref(self._session, instance) # Attach Volume to VM LOG.debug("_attach_volume_iscsi: %s", connection_info, instance=instance) data = connection_info['data'] # Discover iSCSI Target device_name = self._iscsi_discover_target(data)[0] if device_name is None: raise exception.StorageError( reason=_("Unable to find iSCSI Target")) if adapter_type is None: # Get the vmdk file name that the VM is pointing to hardware_devices = vm_util.get_hardware_devices(self._session, vm_ref) adapter_type = vm_util.get_scsi_adapter_type(hardware_devices) self.attach_disk_to_vm(vm_ref, instance, adapter_type, 'rdmp', device_name=device_name) LOG.debug("Attached ISCSI: %s", connection_info, instance=instance)
def _get_vmdk_backed_disk_device(self, vm_ref, connection_info_data): # Get the vmdk file name that the VM is pointing to hardware_devices = vm_util.get_hardware_devices(self._session, vm_ref) # Get disk uuid disk_uuid = self._get_volume_uuid(vm_ref, connection_info_data['volume_id']) device = vm_util.get_vmdk_backed_disk_device(hardware_devices, disk_uuid) if not device: raise exception.DiskNotFound(message=_("Unable to find volume")) return device
def _detach_volume_vmdk(self, connection_info, instance): """Detach volume storage to VM instance.""" vm_ref = vm_util.get_vm_ref(self._session, instance) # Detach Volume from VM LOG.debug("_detach_volume_vmdk: %s", connection_info, instance=instance) data = connection_info['data'] volume_ref = self._get_volume_ref(data['volume']) device = self._get_vmdk_backed_disk_device(vm_ref, data) hardware_devices = vm_util.get_hardware_devices(self._session, vm_ref) adapter_type = None for hw_device in hardware_devices: if hw_device.key == device.controllerKey: adapter_type = vm_util.CONTROLLER_TO_ADAPTER_TYPE.get( hw_device.__class__.__name__) break # IDE does not support disk hotplug if adapter_type == constants.ADAPTER_TYPE_IDE: state = vm_util.get_vm_state(self._session, instance) if state != power_state.SHUTDOWN: raise exception.Invalid( _('%s does not support disk ' 'hotplug.') % adapter_type) disk_type = vm_util._get_device_disk_type(device) self._consolidate_vmdk_volume(instance, vm_ref, device, volume_ref, adapter_type=adapter_type, disk_type=disk_type) self.detach_disk_from_vm(vm_ref, instance, device) # Remove key-value pair <volume_id, vmdk_uuid> from instance's # extra config. Setting value to empty string will remove the key. self._update_volume_details(vm_ref, data['volume_id'], "") LOG.debug("Detached VMDK: %s", connection_info, instance=instance)
def _detach_volume_iscsi(self, connection_info, instance): """Detach volume storage to VM instance.""" vm_ref = vm_util.get_vm_ref(self._session, instance) # Detach Volume from VM LOG.debug("_detach_volume_iscsi: %s", connection_info, instance=instance) data = connection_info['data'] # Discover iSCSI Target device_name, uuid = self._iscsi_get_target(data) if device_name is None: raise exception.StorageError( reason=_("Unable to find iSCSI Target")) # Get the vmdk file name that the VM is pointing to hardware_devices = vm_util.get_hardware_devices(self._session, vm_ref) device = vm_util.get_rdm_disk(hardware_devices, uuid) if device is None: raise exception.DiskNotFound(message=_("Unable to find volume")) self.detach_disk_from_vm(vm_ref, instance, device, destroy_disk=True) LOG.debug("Detached ISCSI: %s", connection_info, instance=instance)
def _get_vmdk_base_volume_device(self, volume_ref): # Get the vmdk file name that the VM is pointing to hardware_devices = vm_util.get_hardware_devices(self._session, volume_ref) return vm_util.get_vmdk_volume_disk(hardware_devices)