[libvirt] [PATCH v2 02/21] virfile: Introduce virFileSetupDev

Michal Privoznik mprivozn at redhat.com
Wed Dec 7 08:36:09 UTC 2016


This part of code that LXC currently uses will be reused so move
to a generic function.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 src/libvirt_private.syms |  2 ++
 src/lxc/lxc_container.c  | 20 ++------------
 src/lxc/lxc_controller.c | 14 +---------
 src/util/virfile.c       | 72 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virfile.h       |  6 ++++
 5 files changed, 84 insertions(+), 30 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index e6bf395f9..94eea50fd 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1556,6 +1556,7 @@ virDirRead;
 virFileAbsPath;
 virFileAccessibleAs;
 virFileActivateDirOverride;
+virFileBindMountDevice;
 virFileBuildPath;
 virFileClose;
 virFileDeleteTree;
@@ -1603,6 +1604,7 @@ virFileResolveLink;
 virFileRewrite;
 virFileRewriteStr;
 virFileSanitizePath;
+virFileSetupDev;
 virFileSkipRoot;
 virFileStripSuffix;
 virFileTouch;
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c
index dd013dfce..32c0c3a4a 100644
--- a/src/lxc/lxc_container.c
+++ b/src/lxc/lxc_container.c
@@ -1112,20 +1112,6 @@ static int lxcContainerMountFSDevPTS(virDomainDefPtr def,
     return ret;
 }
 
-static int lxcContainerBindMountDevice(const char *src, const char *dst)
-{
-    if (virFileTouch(dst, 0666) < 0)
-        return -1;
-
-    if (mount(src, dst, "none", MS_BIND, NULL) < 0) {
-        virReportSystemError(errno, _("Failed to bind %s on to %s"), src,
-                             dst);
-        return -1;
-    }
-
-    return 0;
-}
-
 static int lxcContainerSetupDevices(char **ttyPaths, size_t nttyPaths)
 {
     size_t i;
@@ -1149,7 +1135,7 @@ static int lxcContainerSetupDevices(char **ttyPaths, size_t nttyPaths)
     }
 
     /* We have private devpts capability, so bind that */
-    if (lxcContainerBindMountDevice("/dev/pts/ptmx", "/dev/ptmx") < 0)
+    if (virFileBindMountDevice("/dev/pts/ptmx", "/dev/ptmx") < 0)
         return -1;
 
     for (i = 0; i < nttyPaths; i++) {
@@ -1157,7 +1143,7 @@ static int lxcContainerSetupDevices(char **ttyPaths, size_t nttyPaths)
         if (virAsprintf(&tty, "/dev/tty%zu", i+1) < 0)
             return -1;
 
-        if (lxcContainerBindMountDevice(ttyPaths[i], tty) < 0) {
+        if (virFileBindMountDevice(ttyPaths[i], tty) < 0) {
             return -1;
             VIR_FREE(tty);
         }
@@ -1165,7 +1151,7 @@ static int lxcContainerSetupDevices(char **ttyPaths, size_t nttyPaths)
         VIR_FREE(tty);
 
         if (i == 0 &&
-            lxcContainerBindMountDevice(ttyPaths[i], "/dev/console") < 0)
+            virFileBindMountDevice(ttyPaths[i], "/dev/console") < 0)
             return -1;
     }
     return 0;
diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c
index 29f1179c0..2170b0ae2 100644
--- a/src/lxc/lxc_controller.c
+++ b/src/lxc/lxc_controller.c
@@ -1457,12 +1457,6 @@ static int virLXCControllerSetupDev(virLXCControllerPtr ctrl)
                     LXC_STATE_DIR, ctrl->def->name) < 0)
         goto cleanup;
 
-    if (virFileMakePath(dev) < 0) {
-        virReportSystemError(errno,
-                             _("Failed to make path %s"), dev);
-        goto cleanup;
-    }
-
     /*
      * tmpfs is limited to 64kb, since we only have device nodes in there
      * and don't want to DOS the entire OS RAM usage
@@ -1472,14 +1466,8 @@ static int virLXCControllerSetupDev(virLXCControllerPtr ctrl)
                     "mode=755,size=65536%s", mount_options) < 0)
         goto cleanup;
 
-    VIR_DEBUG("Mount devfs on %s type=tmpfs flags=%x, opts=%s",
-              dev, MS_NOSUID, opts);
-    if (mount("devfs", dev, "tmpfs", MS_NOSUID, opts) < 0) {
-        virReportSystemError(errno,
-                             _("Failed to mount devfs on %s type %s (%s)"),
-                             dev, "tmpfs", opts);
+    if (virFileSetupDev(dev, opts) < 0)
         goto cleanup;
-    }
 
     if (lxcContainerChown(ctrl->def, dev) < 0)
         goto cleanup;
diff --git a/src/util/virfile.c b/src/util/virfile.c
index 1f0bfa906..cc585c1e1 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -32,6 +32,9 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/wait.h>
+#if defined(HAVE_SYS_MOUNT_H)
+# include <sys/mount.h>
+#endif
 #include <unistd.h>
 #include <dirent.h>
 #include <dirname.h>
@@ -3557,3 +3560,72 @@ int virFileIsSharedFS(const char *path)
                                  VIR_FILE_SHFS_SMB |
                                  VIR_FILE_SHFS_CIFS);
 }
+
+
+#if defined(HAVE_SYS_MOUNT_H)
+int
+virFileSetupDev(const char *path,
+                const char *mount_options)
+{
+    const unsigned long mount_flags = MS_NOSUID;
+    const char *mount_fs = "tmpfs";
+    int ret = -1;
+
+    if (virFileMakePath(path) < 0) {
+        virReportSystemError(errno,
+                             _("Failed to make path %s"), path);
+        goto cleanup;
+    }
+
+    VIR_DEBUG("Mount devfs on %s type=tmpfs flags=%lx, opts=%s",
+              path, mount_flags, mount_options);
+    if (mount("devfs", path, mount_fs, mount_flags, mount_options) < 0) {
+        virReportSystemError(errno,
+                             _("Failed to mount devfs on %s type %s (%s)"),
+                             path, mount_fs, mount_options);
+        goto cleanup;
+    }
+
+    ret = 0;
+ cleanup:
+    return ret;
+}
+
+
+int
+virFileBindMountDevice(const char *src,
+                       const char *dst)
+{
+    if (virFileTouch(dst, 0666) < 0)
+        return -1;
+
+    if (mount(src, dst, "none", MS_BIND, NULL) < 0) {
+        virReportSystemError(errno, _("Failed to bind %s on to %s"), src,
+                             dst);
+        return -1;
+    }
+
+    return 0;
+}
+
+#else /* !defined(HAVE_SYS_MOUNT_H) */
+
+int
+virFileSetupDev(const char *path ATTRIBUTE_UNUSED,
+                const char *mount_options ATTRIBUTE_UNUSED)
+{
+    virReportSystemError(ENOSYS, "%s",
+                         _("mount is not supported on this platform."));
+    return -1;
+}
+
+
+int
+virFileBindMountDevice(const char *src ATTRIBUTE_UNUSED,
+                       const char *dst ATTRIBUTE_UNUSED)
+{
+    virReportSystemError(ENOSYS, "%s",
+                         _("mount is not supported on this platform."));
+    return -1;
+}
+#endif /* !defined(HAVE_SYS_MOUNT_H) */
diff --git a/src/util/virfile.h b/src/util/virfile.h
index 5b810956e..5e3bfc00c 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -311,4 +311,10 @@ int virFileGetHugepageSize(const char *path,
                            unsigned long long *size);
 int virFileFindHugeTLBFS(virHugeTLBFSPtr *ret_fs,
                          size_t *ret_nfs);
+
+int virFileSetupDev(const char *path,
+                    const char *mount_options);
+
+int virFileBindMountDevice(const char *src,
+                           const char *dst);
 #endif /* __VIR_FILE_H */
-- 
2.11.0




More information about the libvir-list mailing list