Reviewed-by: Daniel Barboza <dhbarboza82@gmail.com>

On Mon, Jun 15, 2015 at 6:38 PM, Aline Manera <alinefm@linux.vnet.ibm.com> wrote:
According to libvirt documentation, the device must be detached from host to
avoid problems in the host system.

In my tests, the host crashed after attaching a FC HBA as PCI passthrough
device to a guest using Kimchi. A host reboot was needed to recover the system.

For reference:
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Virtualization/chap-Virtualization-PCI_passthrough.html

Signed-off-by: Aline Manera <alinefm@linux.vnet.ibm.com>
---
 src/kimchi/i18n.py             |  1 +
 src/kimchi/model/vmhostdevs.py | 21 ++++++++++++++++++---
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/src/kimchi/i18n.py b/src/kimchi/i18n.py
index 00a6c7c..d2ffa34 100644
--- a/src/kimchi/i18n.py
+++ b/src/kimchi/i18n.py
@@ -126,6 +126,7 @@ messages = {
                         "For Intel CPU, add intel_iommu=on to your Kernel parameter in /boot/grub2/grub.conf. "
                         "For AMD CPU, add iommu=pt iommu=1."),
     "KCHVMHDEV0004E": _('"name" should be a device name string'),
+    "KCHVMHDEV0005E": _('The device %(name)s is probably in use by the host. Unable to attach it to the guest.'),

     "KCHVMIF0001E": _("Interface %(iface)s does not exist in virtual machine %(name)s"),
     "KCHVMIF0002E": _("Network %(network)s specified for virtual machine %(name)s does not exist"),
diff --git a/src/kimchi/model/vmhostdevs.py b/src/kimchi/model/vmhostdevs.py
index 946b1f6..ea75fce 100644
--- a/src/kimchi/model/vmhostdevs.py
+++ b/src/kimchi/model/vmhostdevs.py
@@ -25,6 +25,7 @@ from lxml import etree, objectify
 from lxml.builder import E

 from kimchi.exception import InvalidOperation, InvalidParameter, NotFoundError
+from kimchi.exception import OperationFailed
 from kimchi.model.config import CapabilitiesModel
 from kimchi.model.host import DeviceModel, DevicesModel
 from kimchi.model.utils import get_vm_config_flag
@@ -122,9 +123,23 @@ class VMHostDevsModel(object):
         dev_name = params['name']
         self._passthrough_device_validate(dev_name)
         dev_info = DeviceModel(conn=self.conn).lookup(dev_name)
-        attach_device = getattr(
-            self, '_attach_%s_device' % dev_info['device_type'])
-        return attach_device(vmid, dev_info)
+
+        with RollbackContext() as rollback:
+            try:
+                dev = self.conn.get().nodeDeviceLookupByName(dev_name)
+                dev.dettach()
+            except Exception:
+                raise OperationFailed('KCHVMHDEV0005E', {'name': dev_name})
+            else:
+                rollback.prependDefer(dev.reAttach)
+
+            attach_device = getattr(
+                self, '_attach_%s_device' % dev_info['device_type'])
+
+            info = attach_device(vmid, dev_info)
+            rollback.commitAll()
+
+        return info

     def _get_pci_device_xml(self, dev_info):
         if 'detach_driver' not in dev_info:
--
2.1.0

_______________________________________________
Kimchi-devel mailing list
Kimchi-devel@ovirt.org
http://lists.ovirt.org/mailman/listinfo/kimchi-devel



--
Daniel Henrique Barboza
Senior Software Engineer - IBM Linux Technology Center