]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Linux container fixes, move driver in the daemon.
authorDaniel Veillard <veillard@redhat.com>
Thu, 27 Mar 2008 09:34:06 +0000 (09:34 +0000)
committerDaniel Veillard <veillard@redhat.com>
Thu, 27 Mar 2008 09:34:06 +0000 (09:34 +0000)
* src/lxc_conf.c src/lxc_conf.h src/lxc_driver.c src/remote_internal.c:
  applied patch from Dan Berrange to change the way the lxc driver
  works, migrating the support in the daemon, it also removes some
  compilation issues and teh way the configuration files are named.
Daniel

ChangeLog
src/lxc_conf.c
src/lxc_conf.h
src/lxc_driver.c
src/remote_internal.c

index f921ddc95cdcb669d24b8365acafaab0cc1e6aa8..129918222980884ff95bfbb6c47a5d33d3e98d1e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Thu Mar 27 10:30:12 CET 2008 Daniel Veillard <veillard@redhat.com>
+
+       * src/lxc_conf.c src/lxc_conf.h src/lxc_driver.c src/remote_internal.c:
+         applied patch from Dan Berrange to change the way the lxc driver
+         works, migrating the support in the daemon, it also removes some
+         compilation issues and teh way the configuration files are named.
+
 Thu Mar 27 09:09:34 CET 2008 Daniel Veillard <veillard@redhat.com>
 
        * src/storage_backend_iscsi.c: patch from Chris Lalancette to improve
index e0896eccf3fa03221dfe3a9a36b169fc9d709e38..eda663de7081e562eaea5a1cce69a0bc8488927c 100644 (file)
@@ -593,7 +593,6 @@ int lxcSaveVMDef(virConnectPtr conn,
                  lxc_vm_def_t *def)
 {
     int rc = -1;
-    char uuidstr[VIR_UUID_STRING_BUFLEN];
 
     if (vm->configFile[0] == '\0') {
         if ((rc = virFileMakePath(driver->configDir))) {
@@ -603,16 +602,15 @@ int lxcSaveVMDef(virConnectPtr conn,
             goto save_complete;
         }
 
-        virUUIDFormat(def->uuid, uuidstr);
-        if (virFileBuildPath(driver->configDir, uuidstr, ".xml",
+        if (virFileBuildPath(driver->configDir, vm->def->name, ".xml",
                             vm->configFile, PATH_MAX) < 0) {
             lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                      _("cannot construct config file path"));
             goto save_complete;
         }
 
-        strncpy(vm->configFileBase, uuidstr, PATH_MAX);
-        strncat(vm->configFileBase, ".xml", PATH_MAX - strlen(uuidstr));
+        strncpy(vm->configFileBase, vm->def->name, PATH_MAX-1);
+        strncat(vm->configFileBase, ".xml", PATH_MAX - strlen(vm->def->name)-1);
 
     }
 
@@ -631,7 +629,6 @@ static lxc_vm_t * lxcLoadConfig(lxc_driver_t *driver,
 {
     lxc_vm_def_t *containerDef;
     lxc_vm_t * vm;
-    char uuidstr[VIR_UUID_STRING_BUFLEN];
 
     containerDef = lxcParseVMDef(NULL, xmlData, file);
     if (NULL == containerDef) {
@@ -639,9 +636,8 @@ static lxc_vm_t * lxcLoadConfig(lxc_driver_t *driver,
         return NULL;
     }
 
-    virUUIDFormat(containerDef->uuid, uuidstr);
-    if (!virFileMatchesNameSuffix(file, uuidstr, ".xml")) {
-        DEBUG0("Container uuid does not match config file name");
+    if (!virFileMatchesNameSuffix(file, containerDef->name, ".xml")) {
+        DEBUG0("Container name does not match config file name");
         lxcFreeVMDef(containerDef);
         return NULL;
     }
@@ -662,14 +658,12 @@ static lxc_vm_t * lxcLoadConfig(lxc_driver_t *driver,
     return vm;
 }
 
-int lxcLoadDriverConfig(virConnectPtr conn)
+int lxcLoadDriverConfig(lxc_driver_t *driver)
 {
-    lxc_driver_t *driverPtr = (lxc_driver_t*)conn->privateData;
-
     /* Set the container configuration directory */
-    driverPtr->configDir = strdup(SYSCONF_DIR "/libvirt/lxc");
-    if (NULL == driverPtr->configDir) {
-        lxcError(conn, NULL, VIR_ERR_NO_MEMORY, "configDir");
+    driver->configDir = strdup(SYSCONF_DIR "/libvirt/lxc");
+    if (NULL == driver->configDir) {
+        lxcError(NULL, NULL, VIR_ERR_NO_MEMORY, "configDir");
         return -1;
     }
 
@@ -702,19 +696,18 @@ load_complete:
     return rc;
 }
 
-int lxcLoadContainerInfo(virConnectPtr conn)
+int lxcLoadContainerInfo(lxc_driver_t *driver)
 {
     int rc = -1;
-    lxc_driver_t *driverPtr = (lxc_driver_t*)conn->privateData;
     DIR *dir;
     struct dirent *dirEntry;
 
-    if (!(dir = opendir(driverPtr->configDir))) {
+    if (!(dir = opendir(driver->configDir))) {
         if (ENOENT == errno) {
             /* no config dir => no containers */
             rc = 0;
         } else {
-            lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
+            lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                      _("failed to open config directory: %s"), strerror(errno));
         }
 
@@ -730,7 +723,7 @@ int lxcLoadContainerInfo(virConnectPtr conn)
             continue;
         }
 
-        lxcLoadContainerConfigFile(driverPtr, dirEntry->d_name);
+        lxcLoadContainerConfigFile(driver, dirEntry->d_name);
     }
 
     closedir(dir);
@@ -758,12 +751,13 @@ char *lxcGenerateXML(virConnectPtr conn,
     }
 
     if (lxcIsActiveVM(vm)) {
-        if (virBufferVSprintf(buf, "<domain type='linuxcontainer' id='%d'>\n",
-                             vm->def->id) < 0) {
+        if (virBufferVSprintf(buf, "<domain type='%s' id='%d'>\n",
+                             LXC_DOMAIN_TYPE, vm->def->id) < 0) {
             goto no_memory;
         }
     } else {
-        if (virBufferAddLit(buf, "<domain type='linuxcontainer'>\n") < 0) {
+       if (virBufferVSprintf(buf, "<domain type='%s'>\n",
+                             LXC_DOMAIN_TYPE) < 0) {
             goto no_memory;
         }
     }
@@ -940,3 +934,12 @@ int lxcDeleteConfig(virConnectPtr conn,
 
 #endif /* WITH_LXC */
 
+
+/*
+ * Local variables:
+ *  indent-tabs-mode: nil
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ *  tab-width: 4
+ * End:
+ */
index 85f87d076be0f0f9fbb824a81dfdfcafe164cc13..450093bf1a1ea362130b6f0f23401bbb11ac97d1 100644 (file)
@@ -102,12 +102,12 @@ int lxcSaveVMDef(virConnectPtr conn,
                  lxc_driver_t *driver,
                  lxc_vm_t *vm,
                  lxc_vm_def_t *def);
-int lxcLoadDriverConfig(virConnectPtr conn);
+int lxcLoadDriverConfig(lxc_driver_t *driver);
 int lxcSaveConfig(virConnectPtr conn,
                   lxc_driver_t *driver,
                   lxc_vm_t *vm,
                   lxc_vm_def_t *def);
-int lxcLoadContainerInfo(virConnectPtr conn);
+int lxcLoadContainerInfo(lxc_driver_t *driver);
 int lxcLoadContainerConfigFile(lxc_driver_t *driver,
                                const char *file);
 lxc_vm_t * lxcAssignVMDef(virConnectPtr conn,
index 699c19597626f38fd10ec7d7c5b855211afb124f..c88fc2f0fb70503399efc983ff0e5bd72ec4d92a 100644 (file)
 #define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt, __VA_ARGS__)
 #define DEBUG0(msg) VIR_DEBUG(__FILE__, "%s", msg)
 
-static int lxcStartup(virConnectPtr conn);
-static int lxcShutdown(virConnectPtr conn);
+/*
+ * GLibc headers are behind the kernel, so we define these
+ * constants if they're not present already.
+ */
+
+#ifndef CLONE_NEWPID
+#define CLONE_NEWPID  0x20000000
+#endif
+#ifndef CLONE_NEWUTS
+#define CLONE_NEWUTS  0x04000000
+#endif
+#ifndef CLONE_NEWUSER
+#define CLONE_NEWUSER 0x10000000
+#endif
+#ifndef CLONE_NEWIPC
+#define CLONE_NEWIPC  0x08000000
+#endif
+
+static int lxcStartup(void);
+static int lxcShutdown(void);
+static lxc_driver_t *lxc_driver;
 
 /* Functions */
 static int lxcDummyChild( void *argv ATTRIBUTE_UNUSED )
@@ -105,6 +124,9 @@ static virDrvOpenStatus lxcOpen(virConnectPtr conn,
         goto declineConnection;
     }
 
+    if (lxc_driver == NULL)
+        goto declineConnection;
+
     /* Verify uri was specified */
     if ((NULL == uri) || (NULL == uri->scheme)) {
         goto declineConnection;
@@ -115,15 +137,7 @@ static virDrvOpenStatus lxcOpen(virConnectPtr conn,
         goto declineConnection;
     }
 
-    /* Check that this is a container enabled kernel */
-    if(0 != lxcCheckContainerSupport()) {
-        goto declineConnection;
-    }
-
-    /* initialize driver data */
-    if (0 > lxcStartup(conn)) {
-        goto declineConnection;
-    }
+    conn->privateData = lxc_driver;
 
     return VIR_DRV_OPEN_SUCCESS;
 
@@ -133,7 +147,8 @@ declineConnection:
 
 static int lxcClose(virConnectPtr conn)
 {
-    return lxcShutdown(conn);
+    conn->privateData = NULL;
+    return 0;
 }
 
 static virDomainPtr lxcDomainLookupByID(virConnectPtr conn,
@@ -360,26 +375,29 @@ static char *lxcDomainDumpXML(virDomainPtr dom,
     return lxcGenerateXML(dom->conn, driver, vm, vm->def);
 }
 
-static int lxcStartup(virConnectPtr conn)
+
+static int lxcStartup(void)
 {
-    lxc_driver_t *driver;
+    lxc_driver = calloc(1, sizeof(lxc_driver_t));
+    if (NULL == lxc_driver) {
+        return -1;
+    }
 
-    driver = calloc(1, sizeof(lxc_driver_t));
-    if (NULL == driver) {
+    /* Check that this is a container enabled kernel */
+    if(0 != lxcCheckContainerSupport()) {
         return -1;
     }
 
-    conn->privateData = driver;
 
     /* Call function to load lxc driver configuration information */
-    if (lxcLoadDriverConfig(conn) < 0) {
-        lxcShutdown(conn);
+    if (lxcLoadDriverConfig(lxc_driver) < 0) {
+        lxcShutdown();
         return -1;
     }
 
     /* Call function to load the container configuration files */
-    if (lxcLoadContainerInfo(conn) < 0) {
-        lxcShutdown(conn);
+    if (lxcLoadContainerInfo(lxc_driver) < 0) {
+        lxcShutdown();
         return -1;
     }
 
@@ -392,19 +410,37 @@ static void lxcFreeDriver(lxc_driver_t *driver)
     free(driver);
 }
 
-static int lxcShutdown(virConnectPtr conn)
+static int lxcShutdown(void)
 {
-    lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
-    lxc_vm_t *vms = driver->vms;
+    lxc_vm_t *vms = lxc_driver->vms;
 
     lxcFreeVMs(vms);
-    driver->vms = NULL;
-    lxcFreeDriver(driver);
-    conn->privateData = NULL;
+    lxc_driver->vms = NULL;
+    lxcFreeDriver(lxc_driver);
+
+    return 0;
+}
 
+/**
+ * lxcActive:
+ *
+ * Checks if the LXC daemon is active, i.e. has an active domain
+ *
+ * Returns 1 if active, 0 otherwise
+ */
+static int
+lxcActive(void) {
+    /* If we've any active networks or guests, then we
+     * mark this driver as active
+     */
+    if (lxc_driver->nactivevms)
+        return 1;
+
+    /* Otherwise we're happy to deal with a shutdown */
     return 0;
 }
 
+
 /* Function Tables */
 static virDriver lxcDriver = {
     VIR_DRV_LXC, /* the number virDrvNo */
@@ -466,9 +502,18 @@ static virDriver lxcDriver = {
     NULL, /* getFreeMemory */
 };
 
+
+static virStateDriver lxcStateDriver = {
+    lxcStartup,
+    lxcShutdown,
+    NULL, /* reload */
+    lxcActive,
+};
+
 int lxcRegister(void)
 {
     virRegisterDriver(&lxcDriver);
+    virRegisterStateDriver(&lxcStateDriver);
     return 0;
 }
 
index 09094819bb51c079fa25c70fe2ce1ab580e75fec..0d589f390ddf88b5849c8d42289356d6c900dcfe 100644 (file)
@@ -843,6 +843,12 @@ remoteOpen (virConnectPtr conn,
         rflags |= VIR_DRV_OPEN_REMOTE_UNIX;
     }
 #endif
+#if WITH_LXC
+    if (uri &&
+        uri->scheme && STREQ (uri->scheme, "lxc")) {
+        rflags |= VIR_DRV_OPEN_REMOTE_UNIX;
+    }
+#endif
 
     priv->magic = DEAD;
     priv->sock = -1;