[libvirt] PATCH: 6/7: Persist live domain config across restarts

Daniel P. Berrange berrange at redhat.com
Tue Aug 5 16:21:12 UTC 2008


Internally the drivers track the current live configuration, and the new
inactive config for running domains. When the libvirtd process is restarted
though, this data is lost for any active LXC domains. This patch makes the
LXC driver persist the live config to /var/run/libvirt/lxc/NAME.xml so it
can be tracked across restarts

It required a small change to the domain XML APis to make the autostart
symlink processing optional when deleting a config file

 domain_conf.c |   24 ++++++++++++------------
 lxc_driver.c  |   27 +++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 12 deletions(-)


Daniel

diff -r cf1cf3a1d4d6 src/domain_conf.c
--- a/src/domain_conf.c	Tue Aug 05 16:50:51 2008 +0100
+++ b/src/domain_conf.c	Tue Aug 05 16:50:59 2008 +0100
@@ -1087,13 +1087,11 @@
         break;
 
     case VIR_DOMAIN_CHR_TYPE_PTY:
-        /* @path attribute is an output only property - pty is auto-allocted */
-        break;
-
     case VIR_DOMAIN_CHR_TYPE_DEV:
     case VIR_DOMAIN_CHR_TYPE_FILE:
     case VIR_DOMAIN_CHR_TYPE_PIPE:
-        if (path == NULL) {
+        if (path == NULL &&
+            def->type != VIR_DOMAIN_CHR_TYPE_PTY) {
             virDomainReportError(conn, VIR_ERR_INTERNAL_ERROR,
                                  "%s", _("Missing source path attribute for char device"));
             goto error;
@@ -3124,15 +3122,17 @@
         virDomainReportError(conn, VIR_ERR_NO_MEMORY, NULL);
         goto cleanup;
     }
-    if (asprintf(&autostartLink, "%s/%s",
-                 autostartDir, dom->def->name) < 0) {
-        autostartLink = NULL;
-        virDomainReportError(conn, VIR_ERR_NO_MEMORY, NULL);
-        goto cleanup;
+    if (autostartDir) {
+        if (asprintf(&autostartLink, "%s/%s",
+                     autostartDir, dom->def->name) < 0) {
+            autostartLink = NULL;
+            virDomainReportError(conn, VIR_ERR_NO_MEMORY, NULL);
+            goto cleanup;
+        }
+
+        /* Not fatal if this doesn't work */
+        unlink(autostartLink);
     }
-
-    /* Not fatal if this doesn't work */
-    unlink(autostartLink);
 
     if (unlink(configFile) < 0 &&
         errno != ENOENT) {
diff -r cf1cf3a1d4d6 src/lxc_driver.c
--- a/src/lxc_driver.c	Tue Aug 05 16:50:51 2008 +0100
+++ b/src/lxc_driver.c	Tue Aug 05 16:50:59 2008 +0100
@@ -399,6 +399,7 @@
     close(vm->monitor);
 
     virFileDeletePid(driver->stateDir, vm->def->name);
+    virDomainDeleteConfig(conn, driver->stateDir, NULL, vm);
 
     vm->state = VIR_DOMAIN_SHUTOFF;
     vm->pid = -1;
@@ -615,6 +616,12 @@
     if (signum == 0)
         signum = SIGINT;
 
+    if (vm->pid <= 0) {
+        lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
+                 _("invalid PID %d for container"), vm->pid);
+        return -1;
+    }
+
     if (kill(vm->pid, signum) < 0) {
         if (errno != ESRCH) {
             lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
@@ -741,6 +748,12 @@
         lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
                  _("Failed to read pid file %s/%s.pid: %s"),
                  driver->stateDir, vm->def->name, strerror(errno));
+        rc = -1;
+        goto cleanup;
+    }
+
+    /* Persist the live configuration */
+    if (virDomainSaveConfig(conn, driver->stateDir, vm->def) < 0) {
         rc = -1;
         goto cleanup;
     }
@@ -960,6 +973,8 @@
 
     vm = lxc_driver->domains;
     while (vm) {
+        char *config = NULL;
+        virDomainDefPtr tmp;
         int rc;
         if ((vm->monitor = lxcMonitorClient(NULL, lxc_driver, vm)) < 0) {
             vm = vm->next;
@@ -972,6 +987,18 @@
             vm->monitor = -1;
             vm = vm->next;
             continue;
+        }
+
+        if (asprintf(&config, "%s/%s.xml",
+                     lxc_driver->stateDir, vm->def->name) < 0)
+            continue;
+
+        /* Try and load the live config */
+        tmp = virDomainDefParseFile(NULL, lxc_driver->caps, config);
+        VIR_FREE(config);
+        if (tmp) {
+            vm->newDef = vm->def;
+            vm->def = tmp;
         }
 
         if (vm->pid != 0) {

-- 
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|




More information about the libvir-list mailing list