[libvirt] [PATCH RFC 2/7] virfile: Introduce virFilePopulateDevices

Michal Privoznik mprivozn at redhat.com
Mon Nov 14 16:43:26 UTC 2016


Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 src/libvirt_private.syms |  1 +
 src/lxc/lxc_controller.c | 34 ++++++++++++----------------------
 src/util/virfile.c       | 31 +++++++++++++++++++++++++++++++
 src/util/virfile.h       | 12 ++++++++++++
 4 files changed, 56 insertions(+), 22 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 42650d1..868905e 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1588,6 +1588,7 @@ virFileMatchesNameSuffix;
 virFileNBDDeviceAssociate;
 virFileOpenAs;
 virFileOpenTty;
+virFilePopulateDevices;
 virFilePrintf;
 virFileReadAll;
 virFileReadAllQuiet;
diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c
index 1bb868a..130b09f 100644
--- a/src/lxc/lxc_controller.c
+++ b/src/lxc/lxc_controller.c
@@ -1497,42 +1497,32 @@ static int virLXCControllerPopulateDevices(virLXCControllerPtr ctrl)
     size_t i;
     int ret = -1;
     char *path = NULL;
-    const struct {
-        int maj;
-        int min;
-        mode_t mode;
-        const char *path;
-    } devs[] = {
+    virFileDevices devs[] = {
         { LXC_DEV_MAJ_MEMORY, LXC_DEV_MIN_NULL, 0666, "/null" },
         { LXC_DEV_MAJ_MEMORY, LXC_DEV_MIN_ZERO, 0666, "/zero" },
         { LXC_DEV_MAJ_MEMORY, LXC_DEV_MIN_FULL, 0666, "/full" },
         { LXC_DEV_MAJ_MEMORY, LXC_DEV_MIN_RANDOM, 0666, "/random" },
         { LXC_DEV_MAJ_MEMORY, LXC_DEV_MIN_URANDOM, 0666, "/urandom" },
         { LXC_DEV_MAJ_TTY, LXC_DEV_MIN_TTY, 0666, "/tty" },
+        { 0, 0, 0, NULL},
     };
 
     if (virLXCControllerSetupDev(ctrl) < 0)
         goto cleanup;
 
+    if (virAsprintf(&path, "/%s/%s.dev", LXC_STATE_DIR, ctrl->def->name) < 0)
+        goto cleanup;
+
+    if (virFilePopulateDevices(path, devs) < 0)
+        goto cleanup;
+
     /* Populate /dev/ with a few important bits */
     for (i = 0; i < ARRAY_CARDINALITY(devs); i++) {
-        if (virAsprintf(&path, "/%s/%s.dev/%s",
-                        LXC_STATE_DIR, ctrl->def->name, devs[i].path) < 0)
-            goto cleanup;
-
-        dev_t dev = makedev(devs[i].maj, devs[i].min);
-        if (mknod(path, S_IFCHR, dev) < 0 ||
-            chmod(path, devs[i].mode)) {
-            virReportSystemError(errno,
-                                 _("Failed to make device %s"),
-                                 path);
-            goto cleanup;
-        }
-
-        if (lxcContainerChown(ctrl->def, path) < 0)
-            goto cleanup;
-
         VIR_FREE(path);
+        if (virAsprintf(&path, "/%s/%s.dev/%s",
+                        LXC_STATE_DIR, ctrl->def->name, devs[i].path) < 0 ||
+            lxcContainerChown(ctrl->def, path) < 0)
+            goto cleanup;
     }
 
     ret = 0;
diff --git a/src/util/virfile.c b/src/util/virfile.c
index a45279a..eae4db4 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -3501,3 +3501,34 @@ int virFileIsSharedFS(const char *path)
                                  VIR_FILE_SHFS_SMB |
                                  VIR_FILE_SHFS_CIFS);
 }
+
+
+int
+virFilePopulateDevices(const char *prefix,
+                       const virFileDevices *const devs)
+{
+    size_t i;
+    int ret = -1;
+    char *path = NULL;
+
+    for (i = 0; devs && devs[i].path; i++) {
+        if (virAsprintf(&path, "%s/%s", prefix, devs[i].path) < 0)
+            goto cleanup;
+
+        dev_t dev = makedev(devs[i].maj, devs[i].min);
+        if (mknod(path, S_IFCHR, dev) < 0 ||
+            chmod(path, devs[i].mode)) {
+            virReportSystemError(errno,
+                                 _("Failed to make device %s"),
+                                 path);
+            goto cleanup;
+        }
+
+        VIR_FREE(path);
+    }
+
+    ret = 0;
+ cleanup:
+    VIR_FREE(path);
+    return ret;
+}
diff --git a/src/util/virfile.h b/src/util/virfile.h
index b4ae6ea..3b3dd13 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -307,4 +307,16 @@ int virFileGetHugepageSize(const char *path,
                            unsigned long long *size);
 int virFileFindHugeTLBFS(virHugeTLBFSPtr *ret_fs,
                          size_t *ret_nfs);
+
+typedef struct _virFileDevices virFileDevices;
+typedef virFileDevices *virFileDevicesPtr;
+struct _virFileDevices {
+    int maj;
+    int min;
+    mode_t mode;
+    const char *path;
+};
+
+int virFilePopulateDevices(const char *prefix,
+                           const virFileDevices *const devs);
 #endif /* __VIR_FILE_H */
-- 
2.8.4




More information about the libvir-list mailing list