]> xenbits.xensource.com Git - libvirt.git/commitdiff
virfile: Introduce virFileSetupDev
authorMichal Privoznik <mprivozn@redhat.com>
Thu, 10 Nov 2016 15:17:48 +0000 (16:17 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Thu, 15 Dec 2016 08:25:16 +0000 (09:25 +0100)
This part of code that LXC currently uses will be reused so move
to a generic function.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
src/libvirt_private.syms
src/lxc/lxc_container.c
src/lxc/lxc_controller.c
src/util/virfile.c
src/util/virfile.h

index d730a17c91899187817333a800155302c7060e77..f57b8db9d69692a5758959aab6295595dc6bc92c 100644 (file)
@@ -1556,6 +1556,7 @@ virDirRead;
 virFileAbsPath;
 virFileAccessibleAs;
 virFileActivateDirOverride;
+virFileBindMountDevice;
 virFileBuildPath;
 virFileClose;
 virFileDeleteTree;
@@ -1603,6 +1604,7 @@ virFileResolveLink;
 virFileRewrite;
 virFileRewriteStr;
 virFileSanitizePath;
+virFileSetupDev;
 virFileSkipRoot;
 virFileStripSuffix;
 virFileTouch;
index dd013dfcee6b791badac6163a27521763da44804..32c0c3a4a596b2fa937f16c785890ca6ac79d8ec 100644 (file)
@@ -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;
index 29f1179c033e94b71f62d5e96a6705e66b46e387..2170b0ae2f99395459247a5e3098972247555edf 100644 (file)
@@ -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;
index 1f0bfa906f43d302617c8290a21a023eb3186bd2..cc585c1e10fc7ddcb64745b9aceb0bac754c11f3 100644 (file)
@@ -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) */
index 5b810956e19c75d9e273fb67888511e07d7598c6..5e3bfc00cadab2833bcaa1b8e8082a46e7a09ad1 100644 (file)
@@ -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 */