virfile: Introduce virFileSetupDev

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>
This commit is contained in:
Michal Privoznik 2016-11-10 16:17:48 +01:00
parent 48a12d3b25
commit 1a7c9a5d50
5 changed files with 84 additions and 30 deletions

View 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;

View 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;

View 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;

View 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) */

View 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 */