[libvirt] [PATCH] util: mdev: support persistent devices with mdevctl

Jonathon Jongsma jjongsma at redhat.com
Tue Aug 13 22:02:25 UTC 2019


When a host is rebooted, mediated devices disappear from sysfs.  mdevctl
(https://github.com/mdevctl/mdevctl) is a utility for managing and
persisting these devices. It maintains a registry of mediated devices
and can start and stop them by UUID.

when libvirt attempts to create a new mediated device object, we currently
fail if the path does not exist in sysfs. This patch tries a little bit
harder by using mdevctl to attempt to activate the device.  The approach
is fairly naive -- it just calls 'mdevctl start -u $UUID' without
checking whether this UUID is registered with mdevctl or not.

See https://bugzilla.redhat.com/show_bug.cgi?id=1699274

Signed-off-by: Jonathon Jongsma <jjongsma at redhat.com>
---
NOTES:
- an argument could be made that we should simply do nothing here. mdevctl does
  have support for automatically activating the mediated device when the parent
  device becomes available (via udev). So if the administrator set up the mdev
  to start automatically, this patch should not even be necessary. That said, I
  think this patch could still be useful.
- As I said above, the approach is pretty naive. I could have checked whether
  the device was defined in mdevctl's registry and given a different error
  message ("try registering this device with mdevctl") in that scenario. But
  I chose to keep it simple.

 src/util/virmdev.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/src/util/virmdev.c b/src/util/virmdev.c
index 3d5488cdae..7a2f0adedc 100644
--- a/src/util/virmdev.c
+++ b/src/util/virmdev.c
@@ -25,6 +25,7 @@
 #include "virfile.h"
 #include "virstring.h"
 #include "viralloc.h"
+#include "vircommand.h"
 
 #define VIR_FROM_THIS VIR_FROM_NONE
 
@@ -148,9 +149,24 @@ virMediatedDeviceNew(const char *uuidstr, virMediatedDeviceModelType model)
         return NULL;
 
     if (!virFileExists(sysfspath)) {
-        virReportError(VIR_ERR_DEVICE_MISSING,
-                       _("mediated device '%s' not found"), uuidstr);
-        return NULL;
+        bool activated = false;
+        /* if the mdev device path doesn't exist, we may still be able to start
+         * the device using mdevctl
+         */
+        char *mdevctl = virFindFileInPath("mdevctl");
+        if (mdevctl) {
+            const char *runargs[] = { mdevctl, "start", "-u", uuidstr, NULL };
+            if (virRun(runargs, NULL) == 0) {
+                /* check to see if it the device exists now */
+                activated = virFileExists(sysfspath);
+            }
+        }
+
+        if (!activated) {
+            virReportError(VIR_ERR_DEVICE_MISSING,
+                           _("mediated device '%s' not found"), uuidstr);
+            return NULL;
+        }
     }
 
     if (VIR_ALLOC(dev) < 0)
-- 
2.21.0




More information about the libvir-list mailing list