<div dir="ltr">Reviewed-by: Daniel Barboza &lt;<a href="mailto:dhbarboza82@gmail.com">dhbarboza82@gmail.com</a>&gt;</div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jun 15, 2015 at 6:38 PM, Aline Manera <span dir="ltr">&lt;<a href="mailto:alinefm@linux.vnet.ibm.com" target="_blank">alinefm@linux.vnet.ibm.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">According to libvirt documentation, the device must be detached from host to<br>
avoid problems in the host system.<br>
<br>
In my tests, the host crashed after attaching a FC HBA as PCI passthrough<br>
device to a guest using Kimchi. A host reboot was needed to recover the system.<br>
<br>
For reference:<br>
<a href="https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Virtualization/chap-Virtualization-PCI_passthrough.html" rel="noreferrer" target="_blank">https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Virtualization/chap-Virtualization-PCI_passthrough.html</a><br>
<br>
Signed-off-by: Aline Manera &lt;<a href="mailto:alinefm@linux.vnet.ibm.com">alinefm@linux.vnet.ibm.com</a>&gt;<br>
---<br>
 src/kimchi/i18n.py             |  1 +<br>
 src/kimchi/model/vmhostdevs.py | 21 ++++++++++++++++++---<br>
 2 files changed, 19 insertions(+), 3 deletions(-)<br>
<br>
diff --git a/src/kimchi/i18n.py b/src/kimchi/i18n.py<br>
index 00a6c7c..d2ffa34 100644<br>
--- a/src/kimchi/i18n.py<br>
+++ b/src/kimchi/i18n.py<br>
@@ -126,6 +126,7 @@ messages = {<br>
                         &quot;For Intel CPU, add intel_iommu=on to your Kernel parameter in /boot/grub2/grub.conf. &quot;<br>
                         &quot;For AMD CPU, add iommu=pt iommu=1.&quot;),<br>
     &quot;KCHVMHDEV0004E&quot;: _(&#39;&quot;name&quot; should be a device name string&#39;),<br>
+    &quot;KCHVMHDEV0005E&quot;: _(&#39;The device %(name)s is probably in use by the host. Unable to attach it to the guest.&#39;),<br>
<br>
     &quot;KCHVMIF0001E&quot;: _(&quot;Interface %(iface)s does not exist in virtual machine %(name)s&quot;),<br>
     &quot;KCHVMIF0002E&quot;: _(&quot;Network %(network)s specified for virtual machine %(name)s does not exist&quot;),<br>
diff --git a/src/kimchi/model/vmhostdevs.py b/src/kimchi/model/vmhostdevs.py<br>
index 946b1f6..ea75fce 100644<br>
--- a/src/kimchi/model/vmhostdevs.py<br>
+++ b/src/kimchi/model/vmhostdevs.py<br>
@@ -25,6 +25,7 @@ from lxml import etree, objectify<br>
 from lxml.builder import E<br>
<br>
 from kimchi.exception import InvalidOperation, InvalidParameter, NotFoundError<br>
+from kimchi.exception import OperationFailed<br>
 from kimchi.model.config import CapabilitiesModel<br>
 from kimchi.model.host import DeviceModel, DevicesModel<br>
 from kimchi.model.utils import get_vm_config_flag<br>
@@ -122,9 +123,23 @@ class VMHostDevsModel(object):<br>
         dev_name = params[&#39;name&#39;]<br>
         self._passthrough_device_validate(dev_name)<br>
         dev_info = DeviceModel(conn=self.conn).lookup(dev_name)<br>
-        attach_device = getattr(<br>
-            self, &#39;_attach_%s_device&#39; % dev_info[&#39;device_type&#39;])<br>
-        return attach_device(vmid, dev_info)<br>
+<br>
+        with RollbackContext() as rollback:<br>
+            try:<br>
+                dev = self.conn.get().nodeDeviceLookupByName(dev_name)<br>
+                dev.dettach()<br>
+            except Exception:<br>
+                raise OperationFailed(&#39;KCHVMHDEV0005E&#39;, {&#39;name&#39;: dev_name})<br>
+            else:<br>
+                rollback.prependDefer(dev.reAttach)<br>
+<br>
+            attach_device = getattr(<br>
+                self, &#39;_attach_%s_device&#39; % dev_info[&#39;device_type&#39;])<br>
+<br>
+            info = attach_device(vmid, dev_info)<br>
+            rollback.commitAll()<br>
+<br>
+        return info<br>
<br>
     def _get_pci_device_xml(self, dev_info):<br>
         if &#39;detach_driver&#39; not in dev_info:<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.1.0<br>
<br>
_______________________________________________<br>
Kimchi-devel mailing list<br>
<a href="mailto:Kimchi-devel@ovirt.org">Kimchi-devel@ovirt.org</a><br>
<a href="http://lists.ovirt.org/mailman/listinfo/kimchi-devel" rel="noreferrer" target="_blank">http://lists.ovirt.org/mailman/listinfo/kimchi-devel</a><br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">Daniel Henrique Barboza<div>Senior Software Engineer - IBM Linux Technology Center </div></div></div>
</div>