this patch addes fuse support for libvirt lxc.
we can use fuse filesystem to generate sysinfo dynamically,
So we can isolate /proc/meminfo,cpuinfo and so on through
fuse filesystem.
we mount fuse filesystem for every container.the mount name
is Lxc-containename-fuse.
Signed-off-by: Gao feng <gaofeng(a)cn.fujitsu.com>
---
po/POTFILES.in | 1 +
src/Makefile.am | 9 ++-
src/lxc/lxc_controller.c | 10 +++
src/lxc/lxc_fuse.c | 152 ++++++++++++++++++++++++++++++++++++++++++++++
src/lxc/lxc_fuse.h | 40 ++++++++++++
src/lxc/lxc_process.c | 2 +
6 files changed, 212 insertions(+), 2 deletions(-)
create mode 100644 src/lxc/lxc_fuse.c
create mode 100644 src/lxc/lxc_fuse.h
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 7587c61..dedf83e 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -43,6 +43,7 @@ src/libvirt-qemu.c
src/locking/lock_driver_sanlock.c
src/locking/lock_manager.c
src/lxc/lxc_cgroup.c
+src/lxc/lxc_fuse.c
src/lxc/lxc_container.c
src/lxc/lxc_conf.c
src/lxc/lxc_controller.c
diff --git a/src/Makefile.am b/src/Makefile.am
index a9f8d94..5282a06 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -353,12 +353,14 @@ LXC_DRIVER_SOURCES = \
lxc/lxc_cgroup.c lxc/lxc_cgroup.h \
lxc/lxc_domain.c lxc/lxc_domain.h \
lxc/lxc_process.c lxc/lxc_process.h \
+ lxc/lxc_fuse.c lxc/lxc_fuse.h \
lxc/lxc_driver.c lxc/lxc_driver.h
LXC_CONTROLLER_SOURCES = \
lxc/lxc_conf.c lxc/lxc_conf.h \
lxc/lxc_container.c lxc/lxc_container.h \
lxc/lxc_cgroup.c lxc/lxc_cgroup.h \
+ lxc/lxc_fuse.c lxc/lxc_fuse.h \
lxc/lxc_controller.c
SECURITY_DRIVER_APPARMOR_HELPER_SOURCES = \
@@ -827,8 +829,9 @@ endif
libvirt_driver_lxc_impl_la_CFLAGS = \
$(LIBNL_CFLAGS) \
+ $(FUSE_CFLAGS) \
-I$(top_srcdir)/src/conf $(AM_CFLAGS)
-libvirt_driver_lxc_impl_la_LIBADD = $(CAPNG_LIBS) $(LIBNL_LIBS)
+libvirt_driver_lxc_impl_la_LIBADD = $(CAPNG_LIBS) $(LIBNL_LIBS) $(FUSE_LIBS)
if HAVE_LIBBLKID
libvirt_driver_lxc_impl_la_CFLAGS += $(BLKID_CFLAGS)
libvirt_driver_lxc_impl_la_LIBADD += $(BLKID_LIBS)
@@ -1536,6 +1539,7 @@ libvirt_lxc_SOURCES = \
libvirt_lxc_LDFLAGS = $(WARN_CFLAGS) $(AM_LDFLAGS)
libvirt_lxc_LDADD = \
$(NUMACTL_LIBS) \
+ $(FUSE_LIBS) \
libvirt-net-rpc-server.la \
libvirt-net-rpc.la \
libvirt_driver_security.la \
@@ -1553,7 +1557,8 @@ libvirt_lxc_LDADD += $(APPARMOR_LIBS)
endif
libvirt_lxc_CFLAGS = \
-I$(top_srcdir)/src/conf \
- $(AM_CFLAGS)
+ $(AM_CFLAGS) \
+ $(FUSE_CFLAGS)
if HAVE_LIBBLKID
libvirt_lxc_CFLAGS += $(BLKID_CFLAGS)
libvirt_lxc_LDADD += $(BLKID_LIBS)
diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c
index 4777d51..ff3bb6d 100644
--- a/src/lxc/lxc_controller.c
+++ b/src/lxc/lxc_controller.c
@@ -59,6 +59,7 @@
#include "lxc_conf.h"
#include "lxc_container.h"
#include "lxc_cgroup.h"
+#include "lxc_fuse.h"
#include "virnetdev.h"
#include "virnetdevveth.h"
#include "memory.h"
@@ -1329,6 +1330,7 @@ int main(int argc, char *argv[])
char *name = NULL;
size_t nveths = 0;
char **veths = NULL;
+ virThread thread;
int handshakeFd = -1;
int bg = 0;
const struct option options[] = {
@@ -1516,6 +1518,14 @@ int main(int argc, char *argv[])
}
}
+ rc = virThreadCreate(&thread, true, lxcRegisterFuse,
+ (void *)ctrl->def);
+ if (rc < 0) {
+ virReportSystemError(-rc, "%s",
+ _("Create Fuse filesystem failed"));
+ goto cleanup;
+ }
+
rc = virLXCControllerRun(ctrl);
cleanup:
diff --git a/src/lxc/lxc_fuse.c b/src/lxc/lxc_fuse.c
new file mode 100644
index 0000000..3ffe82d
--- /dev/null
+++ b/src/lxc/lxc_fuse.c
@@ -0,0 +1,152 @@
+/*
+ * Copyright (C) 2012 Fujitsu Limited.
+ *
+ * lxc_fuse.c: fuse filesystem support for libvirt lxc
+ *
+ * Authors:
+ * Gao feng <gaofeng at cn.fujitsu.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <config.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/mount.h>
+
+#include "virterror_internal.h"
+#include "lxc_fuse.h"
+
+
+#define VIR_FROM_THIS VIR_FROM_LXC
+
+#if HAVE_FUSE
+
+static int lxcProcGetattr(const char *path, struct stat *stbuf)
+{
+ int res = 0;
+
+ memset(stbuf, 0, sizeof(struct stat));
+
+ if (STREQ(path, "/")) {
+ stbuf->st_mode = S_IFDIR | 0755;
+ stbuf->st_nlink = 2;
+ } else {
+ res = -ENOENT;
+ }
+
+ return res;
+}
+
+static int lxcProcReaddir(const char *path, void *buf,
+ fuse_fill_dir_t filler,
+ off_t offset ATTRIBUTE_UNUSED,
+ struct fuse_file_info *fi ATTRIBUTE_UNUSED)
+{
+ if (STREQ(path, "/"))
+ return -ENOENT;
+
+ filler(buf, ".", NULL, 0);
+ filler(buf, "..", NULL, 0);
+
+ return 0;
+}
+
+static int lxcProcOpen(const char *path ATTRIBUTE_UNUSED,
+ struct fuse_file_info *fi ATTRIBUTE_UNUSED)
+{
+ return -ENOENT;
+}
+
+static int lxcProcRead(const char *path ATTRIBUTE_UNUSED,
+ char *buf ATTRIBUTE_UNUSED,
+ size_t size ATTRIBUTE_UNUSED,
+ off_t offset ATTRIBUTE_UNUSED,
+ struct fuse_file_info *fi ATTRIBUTE_UNUSED)
+{
+ return -ENOENT;
+}
+
+static struct fuse_operations lxcProcOper = {
+ .getattr = lxcProcGetattr,
+ .readdir = lxcProcReaddir,
+ .open = lxcProcOpen,
+ .read = lxcProcRead,
+};
+
+void lxcRegisterFuse(void *DomainDef)
+{
+ int argc = 4;
+ char *argv[argc];
+ char *path = NULL;
+ char *name = NULL;
+ virDomainDefPtr def = (virDomainDefPtr) DomainDef;
+
+ if (virAsprintf(&name, "Lxc-%s-fuse", def->name) < 0) {
+ virReportOOMError();
+ goto cleanup;
+ }
+
+ if (virAsprintf(&path, "%s/%s/", LXC_STATE_DIR, def->name) < 0)
{
+ virReportOOMError();
+ goto cleanup;
+ }
+
+ if (virFileMakePath(path) < 0) {
+ virReportSystemError(errno, _("Cannot create %s"), path);
+ goto cleanup;
+ }
+
+ argv[0] = name;
+ argv[1] = path;
+ argv[2] = (char *)"-odirect_io";
+ argv[3] = (char *)"-f";
+
+ if (fuse_main(argc, argv, &lxcProcOper, def) < 0)
+ virReportSystemError(errno, "%s", _("Cannot start fuse\n"));
+
+cleanup:
+ VIR_FREE(name);
+ VIR_FREE(path);
+ return;
+}
+
+void lxcUnregisterFuse(virDomainDefPtr def)
+{
+ char *path = NULL;
+
+ if (virAsprintf(&path, "%s/%s/", LXC_STATE_DIR, def->name) < 0)
{
+ virReportOOMError();
+ return;
+ }
+
+ if (umount(path) < 0)
+ virReportSystemError(errno, "%s",
+ _("umount fuse filesystem failed\n"));
+
+ VIR_FREE(path);
+}
+
+#else
+void lxcRegisterFuse(void *DomainDef ATTRIBUTE_UNUSED)
+{
+}
+
+void lxcUnregisterFuse(virDomainDefPtr def ATTRIBUTE_UNUSED)
+{
+}
+#endif
diff --git a/src/lxc/lxc_fuse.h b/src/lxc/lxc_fuse.h
new file mode 100644
index 0000000..d60c238
--- /dev/null
+++ b/src/lxc/lxc_fuse.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2012 Fujitsu Limited.
+ *
+ * lxc_fuse.c: fuse filesystem support for libvirt lxc
+ *
+ * Authors:
+ * Gao feng <gaofeng at cn.fujitsu.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef LXC_FUSE_H
+#define LXC_FUSE_H
+
+#define FUSE_USE_VERSION 26
+
+#include <config.h>
+#if HAVE_FUSE
+#include <fuse.h>
+#endif
+
+#include "lxc_conf.h"
+#include "util.h"
+#include "memory.h"
+
+extern void lxcRegisterFuse(void *DomainDef);
+extern void lxcUnregisterFuse(virDomainDefPtr def);
+#endif
diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c
index 12f6ae6..341a2b8 100644
--- a/src/lxc/lxc_process.c
+++ b/src/lxc/lxc_process.c
@@ -28,6 +28,7 @@
#include "lxc_process.h"
#include "lxc_domain.h"
#include "lxc_container.h"
+#include "lxc_fuse.h"
#include "datatypes.h"
#include "virfile.h"
#include "virpidfile.h"
@@ -542,6 +543,7 @@ int lxcVmTerminate(lxc_driver_t *driver,
return -1;
}
+ lxcUnregisterFuse(vm->def);
virSecurityManagerRestoreAllLabel(driver->securityManager,
vm->def, false);
virSecurityManagerReleaseLabel(driver->securityManager, vm->def);
--
1.7.7.6