Skip to contentSkip to conversation

Migration support for "xl"

PATCH In review Ian Jackson 3 participants 29 messages Mar 25, 2010 – Apr 1, 2010

lore
Share & tools
lore
#0 lore raw

cover Migration support for "xl"

This patch series provides support for "xl migrate".

There are a number of bugfixes and extensions to libxl, followed by a new savefile format (which can contain the domain config file and is more extensible), followed by actual migration support.

This should probably not go into 4.0 :-).

#1 lore raw

PATCH 1/11 libxl: Make logging functions preserve errno

This is needed by the following patches. It makes it much more convenient for libxl functions to return the errno value from the failure, when they fail.

---
 tools/libxl/libxl_internal.c |    5 ++++-
 tools/libxl/libxl_internal.h |    2 ++
 2 files changed, 6 insertions(+), 1 deletions(-)
tools/libxl/libxl_internal.c +4 -1
index f48e9eb..4dcdabe 100644
@@ -156,11 +156,13 @@ void xl_logv(struct libxl_ctx *ctx, int loglevel, int errnoval,
 {
     char *enomem = "[out of memory formatting log message]";
     char *s;
-    int rc;
+    int rc, esave;
 
     if (!ctx->log_callback)
         return;
 
+    esave = errno;
+    
     rc = vasprintf(&s, fmt, ap);
     if (rc<0) { s = enomem; goto x; }
 
@@ -180,6 +182,7 @@ void xl_logv(struct libxl_ctx *ctx, int loglevel, int errnoval,
     ctx->log_callback(ctx->log_userdata, loglevel, file, line, func, s);
     if (s != enomem)
         free(s);
+    errno = esave;
 }
 
 void xl_log(struct libxl_ctx *ctx, int loglevel, int errnoval,
tools/libxl/libxl_internal.h +2 -0
index 300c108..10e72e5 100644
@@ -47,6 +47,7 @@
 #define XL_LOG_ERRNO(ctx, loglevel, _f, _a...)
 #define XL_LOG_ERRNOVAL(ctx, loglevel, errnoval, _f, _a...)
 #endif
+  /* all of these macros preserve errno (saving and restoring) */
 
 #define XL_LOG_DEBUG 3
 #define XL_LOG_INFO 2
@@ -56,6 +57,7 @@
 /* logging */
 void xl_logv(struct libxl_ctx *ctx, int errnoval, int loglevel, const char *file, int line, const char *func, char *fmt, va_list al);
 void xl_log(struct libxl_ctx *ctx, int errnoval, int loglevel, const char *file, int line, const char *func, char *fmt, ...);
+  /* these functions preserve errno (saving and restoring) */
 
 
 typedef enum {
Signature · 1 line
1.5.6.5
#2 lore raw

PATCH 2/11 libxl: Report error if logfile rotation fails

In reply to Ian Jackson

Check the return values from renames and errors from stat in libxl_create_logfile (which, misleadingly, does not actually create the logfile).

---
 tools/libxl/libxl_utils.c |   28 +++++++++++++++++++++++++---
 1 files changed, 25 insertions(+), 3 deletions(-)
tools/libxl/libxl_utils.c +25 -3
index 1ba9431..afc852a 100644
@@ -101,11 +101,25 @@ int libxl_is_stubdom(struct libxl_ctx *ctx, uint32_t domid, uint32_t *target_dom
     return 1;
 }
 
+static int logrename(struct libxl_ctx *ctx, const char *old, const char *new) {
+    int r;
+
+    r = rename(old, new);
+    if (r) {
+        if (errno == ENOENT) return 0; /* ok */
+
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to rotate logfile - could not"
+                     " rename %s to %s", old, new);
+        return ERROR_FAIL;
+    }
+    return 0;
+}
+
 int libxl_create_logfile(struct libxl_ctx *ctx, char *name, char **full_name)
 {
     struct stat stat_buf;
     char *logfile, *logfile_new;
-    int i;
+    int i, rc;
 
     logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log", name);
     if (stat(logfile, &stat_buf) == 0) {
@@ -115,11 +129,19 @@ int libxl_create_logfile(struct libxl_ctx *ctx, char *name, char **full_name)
         for (i = 9; i > 0; i--) {
             logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log.%d", name, i);
             logfile_new = libxl_sprintf(ctx, "/var/log/xen/%s.log.%d", name, i + 1);
-            rename(logfile, logfile_new);
+            rc = logrename(ctx, logfile, logfile_new);
+            if (rc) return rc;
         }
         logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log", name);
         logfile_new = libxl_sprintf(ctx, "/var/log/xen/%s.log.1", name);
-        rename(logfile, logfile_new);
+
+        rc = logrename(ctx, logfile, logfile_new);
+        if (rc) return rc;
+    } else {
+        if (errno != ENOENT)
+            XL_LOG_ERRNO(ctx, XL_LOG_WARNING, "problem checking existence of"
+                         " logfile %s, which might have needed to be rotated",
+                         name);
     }
     *full_name = strdup(logfile);
     return 0;
Signature · 1 line
1.5.6.5
#3 lore raw

PATCH 3/11 libxl: New utility functions in for reading and writing files.

In reply to Ian Jackson

We introduce these functions in libxl_utils.h:

int libxl_read_file_contents(struct libxl_ctx *ctx, const char *filename,

                               void **data_r, int *datalen_r);
  int libxl_read_exactly(struct libxl_ctx *ctx, int fd, void *data, ssize_t sz,
                         const char *filename, const char *what);
  int libxl_write_exactly(struct libxl_ctx *ctx, int fd, const void *data,
                          ssize_t sz, const char *filename, const char *what);

They will be needed by the following patches. They have to be in libxl.a rather than libxutil.a because they will be used, amongst other places, in libxl itself.

---
 tools/libxl/libxl_utils.c |  103 +++++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_utils.h |   18 ++++++++
 2 files changed, 121 insertions(+), 0 deletions(-)
tools/libxl/libxl_utils.c +103 -0
index afc852a..3a1f095 100644
@@ -26,6 +26,7 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <assert.h>
 
 #include "libxl_utils.h"
 #include "libxl_internal.h"
@@ -177,3 +178,105 @@ out:
     return rc;
 }
 
+int libxl_read_file_contents(struct libxl_ctx *ctx, const char *filename,
+                             void **data_r, int *datalen_r) {
+    FILE *f = 0;
+    uint8_t *data = 0;
+    int datalen = 0;
+    int e;
+    struct stat stab;
+    ssize_t rs;
+    
+    f = fopen(filename, "r");
+    if (!f) {
+        if (errno == ENOENT) return ENOENT;
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to open %s", filename);
+        goto xe;
+    }
+
+    if (fstat(fileno(f), &stab)) {
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to fstat %s", filename);
+        goto xe;
+    }
+
+    if (!S_ISREG(stab.st_mode)) {
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "%s is not a plain file", filename);
+        errno = ENOTTY;
+        goto xe;
+    }
+
+    if (stab.st_size > INT_MAX) {
+        XL_LOG(ctx, XL_LOG_ERROR, "file %s is far too large", filename);
+        errno = EFBIG;
+        goto xe;
+    }
+
+    datalen = stab.st_size;
+
+    if (stab.st_size && data_r) {
+        data = malloc(datalen);
+        if (!data) goto xe;
+
+        rs = fread(data, 1, datalen, f);
+        if (rs != datalen) {
+            if (ferror(f))
+                XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to read %s", filename);
+            else if (feof(f))
+                XL_LOG(ctx, XL_LOG_ERROR, "%s changed size while we"
+                       " were reading it", filename);
+            else
+                abort();
+            goto xe;
+        }
+    }
+
+    if (fclose(f)) {
+        f = 0;
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to close %s", filename);
+        goto xe;
+    }
+
+    if (data_r) *data_r = data;
+    if (datalen_r) *datalen_r = datalen;
+
+    return 0;
+
+ xe:
+    e = errno;
+    assert(e != ENOENT);
+    if (f) fclose(f);
+    if (data) free(data);
+    return e;
+}
+
+#define READ_WRITE_EXACTLY(rw, zero_is_eof, constdata)                    \
+                                                                          \
+  int libxl_##rw##_exactly(struct libxl_ctx *ctx, int fd,                 \
+                           constdata void *data, ssize_t sz,              \
+                           const char *filename, const char *what) {      \
+      ssize_t got;                                                        \
+                                                                          \
+      while (sz > 0) {                                                    \
+          got = rw(fd, data, sz);                                         \
+          if (got == -1) {                                                \
+              if (errno == EINTR) continue;                               \
+              XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to " #rw " %s%s%s", \
+                           what?what:"", what?" from ":"", filename);     \
+              return errno;                                               \
+          }                                                               \
+          if (got == 0) {                                                 \
+              XL_LOG(ctx, XL_LOG_ERROR,                                   \
+                     zero_is_eof                                          \
+                     ? "file/stream truncated reading %s%s%s"             \
+                     : "file/stream write returned 0! writing %s%s%s",    \
+                     what?what:"", what?" from ":"", filename);           \
+              return EPROTO;                                              \
+          }                                                               \
+          sz -= got;                                                      \
+          data = (char*)data + got;                                       \
+      }                                                                   \
+      return 0;                                                           \
+  }
+
+READ_WRITE_EXACTLY(read, 1, /* */)
+READ_WRITE_EXACTLY(write, 0, const)
tools/libxl/libxl_utils.h +18 -0
index 05fa4e3..65e7e31 100644
@@ -26,5 +26,23 @@ int libxl_is_stubdom(struct libxl_ctx *ctx, uint32_t domid, uint32_t *target_dom
 int libxl_create_logfile(struct libxl_ctx *ctx, char *name, char **full_name);
 int libxl_string_to_phystype(struct libxl_ctx *ctx, char *s, libxl_disk_phystype *phystype);
 
+int libxl_read_file_contents(struct libxl_ctx *ctx, const char *filename,
+                             void **data_r, int *datalen_r);
+  /* Reads the contents of the plain file filename into a mallocd
+   * buffer.  Returns 0 or errno.  Any errors other than ENOENT are logged.
+   * If the file is empty, *data_r and *datalen_r are set to 0.
+   * On error, *data_r and *datalen_r are undefined.
+   * data_r and/or datalen_r may be 0.
+   */
+
+int libxl_read_exactly(struct libxl_ctx *ctx, int fd, void *data, ssize_t sz,
+                       const char *filename, const char *what);
+int libxl_write_exactly(struct libxl_ctx *ctx, int fd, const void *data,
+                        ssize_t sz, const char *filename, const char *what);
+  /* Returns 0 or errno.  If file is truncated on reading, returns
+   * EPROTO and you have no way to tell how much was read.  Errors are
+   * logged using filename (which is only used for logging) and what
+   * (which may be 0). */
+    
 #endif
 
Signature · 1 line
1.5.6.5
#4 lore raw

PATCH 4/11 libxl: libxl_domain_restore: Put fd back to blocking mode

In reply to Ian Jackson

libxl_domain_restore calls, indirectly, xc_domain_restore. The latter, when doing a live migration, sets the fd from blocking mode (which it must be on entry, or things go wrong) to nonblocking mode and leaves it this way. Arguably this is a bug in libxc, but to avoid disrupting any callers we fix it in libxl.

So libxl_domain_restore now puts the fd back into blocking mode before returning.

---
 tools/libxl/libxl.c |   15 ++++++++++++++-
 1 files changed, 14 insertions(+), 1 deletions(-)
tools/libxl/libxl.c +14 -1
index 0b1b6db..308273d 100644
@@ -221,7 +221,7 @@ int libxl_domain_restore(struct libxl_ctx *ctx, libxl_domain_build_info *info,
                          libxl_device_model_info *dm_info)
 {
     char **vments = NULL, **localents = NULL;
-    int i, ret;
+    int i, ret, esave, flags;
 
     ret = build_pre(ctx, domid, info, state);
     if (ret) goto out;
@@ -259,6 +259,19 @@ int libxl_domain_restore(struct libxl_ctx *ctx, libxl_domain_build_info *info,
     else
         dm_info->saved_state = NULL;
 out:
+    esave = errno;
+
+    flags = fcntl(fd, F_GETFL);
+    if (flags == -1) {
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "unable to get flags on restore fd");
+    } else {
+        flags &= ~O_NONBLOCK;
+        if (fcntl(fd, F_SETFL, flags) == -1)
+            XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "unable to put restore fd"
+                         " back to blocking mode");
+    }
+
+    errno = esave;
     return ret;
 }
 
Signature · 1 line
1.5.6.5
#5 lore raw

PATCH 5/11 libxl: Provide libxl_domain_rename

In reply to Ian Jackson

Provide a new function libxl_domain_rename. It can check that the domain being renamed has the expected name, to avoid races.

Use the new function to set the name during domain creation.

---
 tools/libxl/libxl.c |   83 +++++++++++++++++++++++++++++++++++++++++++++++++-
 tools/libxl/libxl.h |    6 ++++
 2 files changed, 87 insertions(+), 2 deletions(-)
tools/libxl/libxl.c +81 -2
index 308273d..59859ab 100644
@@ -82,7 +82,7 @@ int libxl_ctx_set_log(struct libxl_ctx *ctx, libxl_log_callback log_callback, vo
 int libxl_domain_make(struct libxl_ctx *ctx, libxl_domain_create_info *info,
                        uint32_t *domid)
 {
-    int flags, ret, i;
+    int flags, ret, i, rc;
     char *uuid_string;
     char *rw_paths[] = { "device", "device/suspend/event-channel" , "data"};
     char *ro_paths[] = { "cpu", "memory", "device", "error", "drivers",
@@ -146,7 +146,8 @@ retry_transaction:
 
     xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vm", dom_path), vm_path, strlen(vm_path));
     xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vss", dom_path), vss_path, strlen(vss_path));
-    xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/name", dom_path), info->name, strlen(info->name));
+    rc = libxl_domain_rename(ctx, *domid, 0, info->name, t);
+    if (rc) return rc;
 
     for (i = 0; i < ARRAY_SIZE(rw_paths); i++) {
         char *path = libxl_sprintf(ctx, "%s/%s", dom_path, rw_paths[i]);
@@ -175,6 +176,84 @@ retry_transaction:
     return 0;
 }
 
+int libxl_domain_rename(struct libxl_ctx *ctx, uint32_t domid,
+                        const char *old_name, const char *new_name,
+                        xs_transaction_t trans) {
+    char *dom_path = 0;
+    const char *name_path;
+    char *got_old_name;
+    unsigned int got_old_len;
+    xs_transaction_t our_trans = 0;
+    int rc;
+
+    dom_path = libxl_xs_get_dompath(ctx, domid);
+    if (!dom_path) goto x_nomem;
+
+    name_path= libxl_sprintf(ctx, "%s/name", dom_path);
+    if (!name_path) goto x_nomem;
+
+ retry_transaction:
+    if (!trans) {
+        trans = our_trans = xs_transaction_start(ctx->xsh);
+        if (!our_trans) {
+            XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, errno,
+                            "create xs transaction for domain (re)name");
+            goto x_fail;
+        }
+    }
+
+    if (old_name) {
+        got_old_name = xs_read(ctx->xsh, trans, name_path, &got_old_len);
+        if (!got_old_name) {
+            XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, errno, "check old name"
+                            " for domain %"PRIu32" allegedly named `%s'",
+                            domid, old_name);
+            goto x_fail;
+        }
+        if (strcmp(old_name, got_old_name)) {
+            XL_LOG(ctx, XL_LOG_ERROR, "domain %"PRIu32" allegedly named "
+                   "`%s' is actually named `%s' - racing ?",
+                   domid, old_name, got_old_name);
+            free(got_old_name);
+            goto x_fail;
+        }
+        free(got_old_name);
+    }
+    if (!xs_write(ctx->xsh, trans, name_path,
+                  new_name, strlen(new_name))) {
+        XL_LOG(ctx, XL_LOG_ERROR, "failed to write new name `%s'"
+               " for domain %"PRIu32" previously named `%s'",
+               domid, new_name, old_name);
+        goto x_fail;
+    }
+
+    if (our_trans) {
+        if (!xs_transaction_end(ctx->xsh, our_trans, 0)) {
+            trans = our_trans = 0;
+            if (errno != EAGAIN) {
+                XL_LOG(ctx, XL_LOG_ERROR, "failed to commit new name `%s'"
+                       " for domain %"PRIu32" previously named `%s'",
+                       domid, new_name, old_name);
+                goto x_fail;
+            }
+            XL_LOG(ctx, XL_LOG_DEBUG, "need to retry rename transaction"
+                   " for domain %"PRIu32" (name_path=\"%s\", new_name=\"%s\")",
+                   domid, name_path, new_name);
+            goto retry_transaction;
+        }
+        our_trans = 0;
+    }
+
+    rc = 0;
+ x_rc:
+    if (dom_path) libxl_free(ctx, dom_path);
+    if (our_trans) xs_transaction_end(ctx->xsh, our_trans, 1);
+    return rc;
+
+ x_fail:  rc = ERROR_FAIL;  goto x_rc;
+ x_nomem: rc = ERROR_NOMEM; goto x_rc;
+}
+
 int libxl_domain_build(struct libxl_ctx *ctx, libxl_domain_build_info *info, uint32_t domid, libxl_domain_build_state *state)
 {
     char **vments = NULL, **localents = NULL;
tools/libxl/libxl.h +6 -0
index 84e412a..2d76087 100644
@@ -296,6 +296,12 @@ int libxl_free_waiter(libxl_waiter *waiter);
 int libxl_event_get_domain_death_info(struct libxl_ctx *ctx, uint32_t domid, libxl_event *event, xc_domaininfo_t *info);
 int libxl_event_get_disk_eject_info(struct libxl_ctx *ctx, uint32_t domid, libxl_event *event, libxl_device_disk *disk);
 
+int libxl_domain_rename(struct libxl_ctx *ctx, uint32_t domid,
+                        const char *old_name, const char *new_name,
+                        xs_transaction_t trans);
+  /* if old_name is NULL, any old name is OK; otherwise we check
+   * transactionally that the domain has the old old name; if
+   * trans is not 0 we use caller's transaction and caller must do retries */
 
 int libxl_domain_pause(struct libxl_ctx *ctx, uint32_t domid);
 int libxl_domain_unpause(struct libxl_ctx *ctx, uint32_t domid);
Signature · 1 line
1.5.6.5
#6 lore raw

Re: [PATCH 05/11] libxl: Provide libxl_domain_rename

Quoted 4 lines · Ian Jackson

Provide a new function libxl_domain_rename. It can check that the domain being renamed has the expected name, to avoid races.

Use the new function to set the name during domain creation.

this looks really ugly (transaction in transaction in domain make), and has a really complicated code path (which doesn't look right either on the first read), but

my main objection is that libxl has no business handling the uniqueness of names or whatever you want to do here. libxl get a name string from whoever is using it (xl in your case), so if you want somehow to have a special feature make it so in xl. in other words, no policies in libxl.

also NULL is for pointer, 0 for integer values.

Signature · 1 line
Vincent
#7 lore raw

Re: [PATCH 05/11] libxl: Provide libxl_domain_rename

Vincent Hanquez writes ("Re: [Xen-devel] [PATCH 05/11] libxl: Provide libxl_domain_rename"):

Quoted 9 lines · Ian Jackson
Quoted 4 lines

Provide a new function libxl_domain_rename. It can check that the domain being renamed has the expected name, to avoid races.

Use the new function to set the name during domain creation.

this looks really ugly (transaction in transaction in domain make), and has a really complicated code path (which doesn't look right either on the first read), but

There is no transaction in transaction. This arrangement is necessary because the (re)name action which is part of domain creation is already part of a transaction. I didn't much like the "goto"-based retry loop but that's the way it's done in the domain creation too.

Quoted 4 lines

my main objection is that libxl has no business handling the uniqueness of names or whatever you want to do here. libxl get a name string from whoever is using it (xl in your case), so if you want somehow to have a special feature make it so in xl. in other words, no policies in libxl.

You are confused. The purpose of this check is to check that the existing name is as expected, not that the name is not unique (although the lack of checking that the name is unique is also a bug).

The reason we need to check that the existing name is as expected is to ensure that we effectively "lock" the domain against multiple simultaneous migration attempts.

Quoted 1 line

also NULL is for pointer, 0 for integer values.

0 is a perfectly acceptable null pointer constant.

Ian.

#8 lore raw

Re: [PATCH 05/11] libxl: Provide libxl_domain_rename

Quoted 3 lines · Ian Jackson

You are confused. The purpose of this check is to check that the existing name is as expected, not that the name is not unique (although the lack of checking that the name is unique is also a bug).

I'm not confused, there's no need to check anything, libxl is here to plumb whatever name the user of the library has chosen for its domain into a xenstore entry: the name is opaque to libxl, and should remains as such.

if you want some kind of migration lock, put in xl where you define the policy.

Quoted 1 line

0 is a perfectly acceptable null pointer constant.

working != acceptable.

Signature · 1 line
Vincent
#9 lore raw

Re: [PATCH 05/11] libxl: Provide libxl_domain_rename

Vincent Hanquez writes ("Re: [Xen-devel] [PATCH 05/11] libxl: Provide libxl_domain_rename"):

Quoted 2 lines

if you want some kind of migration lock, put in xl where you define the policy.

That is exactly what I have done. The lock is implemented using the name in xenstore, which xl modifies in a transaction. However, following the principle that libxl callers should not access xenstore directly, this is mediated by the domain rename function.

libxl callers who do not care about the old name when setting the new one can simply pass 0 for that argument to the rename function.

Ian.

#10 lore raw

Re: [PATCH 05/11] libxl: Provide libxl_domain_rename

In reply to Vincent Hanquez
Quoted 5 lines · Vincent Hanquez
Quoted 3 lines · Ian Jackson

You are confused. The purpose of this check is to check that the existing name is as expected, not that the name is not unique (although the lack of checking that the name is unique is also a bug).

Quoted 8 lines

I'm not confused, there's no need to check anything, libxl is here to plumb whatever name the user of the library has chosen for its domain into a xenstore entry: the name is opaque to libxl, and should remains as such.

if you want some kind of migration lock, put in xl where you define the policy.

the domain name is not opaque to libxenlight:

- some public libxenlight structs contain the domain name;

- libxenlight writes the domain name to xenstore;

- libxenlight offers functions to convert domids into domain names and vice versa;

all this before any of Ian's patches.

I think this proves that libxenlight takes care of domain names already and this patch just adds another function to the library to complete its functionalities, without breaking any unwritten rule.

But if you prefer we can move this function to libxl_utils.c.

#11 lore raw

PATCH 6/11 libxl: Expose functions for helping with subprocesses.

In reply to Ian Jackson

* Expose libxl_fork in libxl_utils.h
* Expose libxl_pipe in libxl_utils.h
* Make libxl_exec put SIGPIPE back (so that libxl callers may have SIGPIPE ignored)

xl would like to use libxl_fork (which is like fork(2) except that it logs errors) and also a similar function libxl_pipe. So put these in libxl_utils.[ch] and use them in libxl.c as appropriate, to avoid having to duplicate code between xl and libxl.

Also, make sure that subprocesses spawned by libxl have SIGPIPE set back to SIG_DFL as they are entitled to expect. This means that a libxl caller which sets SIGPIPE to SIG_IGN is no longer buggy. (This is relevant for xl migration, because xl would like to be such a caller.)

---
 tools/libxl/libxl.c       |   12 +++---------
 tools/libxl/libxl_exec.c  |   18 +++++-------------
 tools/libxl/libxl_utils.c |   23 +++++++++++++++++++++++
 tools/libxl/libxl_utils.h |    4 ++++
 4 files changed, 35 insertions(+), 22 deletions(-)
tools/libxl/libxl.c +3 -9
index 59859ab..711f0a0 100644
@@ -1238,15 +1238,9 @@ int libxl_device_disk_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_di
                 char buf[1024], *dev;
                 dev = get_blktap2_device(ctx, disk->physpath, device_disk_string_of_phystype(disk->phystype));
                 if (dev == NULL) {
-                    if (pipe(p) < 0) {
-                        XL_LOG(ctx, XL_LOG_ERROR, "Failed to create a pipe");
-                        return -1;
-                    }
-                    rc = fork();
-                    if (rc < 0) {
-                        XL_LOG(ctx, XL_LOG_ERROR, "Failed to fork a new process");
-                        return -1;
-                    } else if (!rc) { /* child */
+                    rc= libxl_pipe(ctx, p);  if (rc==-1) return -1;
+                    rc= libxl_fork(ctx);  if (rc==-1) return -1;
+                    if (!rc) { /* child */
                         int null_r, null_w;
                         char *args[4];
                         args[0] = "tapdisk2";
tools/libxl/libxl_exec.c +5 -13
index e8cc74c..6d4a5c5 100644
@@ -30,19 +30,6 @@
 #include "libxl.h"
 #include "libxl_internal.h"
 
-static pid_t libxl_fork(struct libxl_ctx *ctx)
-{
-    pid_t pid;
-
-    pid = fork();
-    if (pid == -1) {
-        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "fork failed");
-        return -1;
-    }
-
-    return pid;
-}
-
 static int call_waitpid(pid_t (*waitpid_cb)(pid_t, int *, int), pid_t pid, int *status, int options)
 {
     return (waitpid_cb) ? waitpid_cb(pid, status, options) : waitpid(pid, status, options);
@@ -61,6 +48,11 @@ void libxl_exec(int stdinfd, int stdoutfd, int stderrfd, char *arg0, char **args
         dup2(stderrfd, STDERR_FILENO);
     for (i = 4; i < 256; i++)
         close(i);
+
+    signal(SIGPIPE, SIG_DFL);
+    /* in case our caller set it to IGN.  subprocesses are entitled
+     * to assume they got DFL. */
+
     execv(arg0, args);
     _exit(-1);
 }
tools/libxl/libxl_utils.c +23 -0
index 3a1f095..c6989ce 100644
@@ -280,3 +280,26 @@ int libxl_read_file_contents(struct libxl_ctx *ctx, const char *filename,
 
 READ_WRITE_EXACTLY(read, 1, /* */)
 READ_WRITE_EXACTLY(write, 0, const)
+
+
+pid_t libxl_fork(struct libxl_ctx *ctx)
+{
+    pid_t pid;
+
+    pid = fork();
+    if (pid == -1) {
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "fork failed");
+        return -1;
+    }
+
+    return pid;
+}
+
+int libxl_pipe(struct libxl_ctx *ctx, int pipes[2])
+{
+    if (pipe(pipes) < 0) {
+        XL_LOG(ctx, XL_LOG_ERROR, "Failed to create a pipe");
+        return -1;
+    }
+    return 0;
+}
tools/libxl/libxl_utils.h +4 -0
index 65e7e31..fe8b975 100644
@@ -44,5 +44,9 @@ int libxl_write_exactly(struct libxl_ctx *ctx, int fd, const void *data,
    * logged using filename (which is only used for logging) and what
    * (which may be 0). */
     
+pid_t libxl_fork(struct libxl_ctx *ctx);
+int libxl_pipe(struct libxl_ctx *ctx, int pipes[2]);
+  /* Just like fork(2), pipe(2), but log errors. */
+
 #endif
 
Signature · 1 line
1.5.6.5
#12 lore raw

PATCH 7/11 libxl: Expose libxl_report_exitstatus

In reply to Ian Jackson

xl would like to use libxl_report_exitstatus, so expose it in libxl_utils.h to avoid having to write it twice. Also, give it a "level" argument to set the loglevel of the resulting message.

---
 tools/libxl/libxl_exec.c     |   16 +++++++---------
 tools/libxl/libxl_internal.h |    5 -----
 tools/libxl/libxl_utils.h    |   11 +++++++++++
 3 files changed, 18 insertions(+), 14 deletions(-)
tools/libxl/libxl_exec.c +7 -9
index 6d4a5c5..52dda97 100644
@@ -57,34 +57,32 @@ void libxl_exec(int stdinfd, int stdoutfd, int stderrfd, char *arg0, char **args
     _exit(-1);
 }
 
-void libxl_report_child_exitstatus(struct libxl_ctx *ctx,
+void libxl_report_child_exitstatus(struct libxl_ctx *ctx, int level,
                                    const char *what, pid_t pid, int status)
 {
-    /* treats all exit statuses as errors; if that's not what you want,
-     * check status yourself first */
 
     if (WIFEXITED(status)) {
         int st = WEXITSTATUS(status);
         if (st)
-            XL_LOG(ctx, XL_LOG_ERROR, "%s [%ld] exited"
+            XL_LOG(ctx, level, "%s [%ld] exited"
                    " with error status %d", what, (unsigned long)pid, st);
         else
-            XL_LOG(ctx, XL_LOG_ERROR, "%s [%ld] unexpectedly"
+            XL_LOG(ctx, level, "%s [%ld] unexpectedly"
                    " exited status zero", what, (unsigned long)pid);
     } else if (WIFSIGNALED(status)) {
         int sig = WTERMSIG(status);
         const char *str = strsignal(sig);
         const char *coredump = WCOREDUMP(status) ? " (core dumped)" : "";
         if (str)
-            XL_LOG(ctx, XL_LOG_ERROR, "%s [%ld] died due to"
+            XL_LOG(ctx, level, "%s [%ld] died due to"
                    " fatal signal %s%s", what, (unsigned long)pid,
                    str, coredump);
         else
-            XL_LOG(ctx, XL_LOG_ERROR, "%s [%ld] died due to unknown"
+            XL_LOG(ctx, level, "%s [%ld] died due to unknown"
                    " fatal signal number %d%s", what, (unsigned long)pid,
                    sig, coredump);
     } else {
-        XL_LOG(ctx, XL_LOG_ERROR, "%s [%ld] gave unknown"
+        XL_LOG(ctx, level, "%s [%ld] gave unknown"
                " wait status 0x%x", what, (unsigned long)pid, status);
     }
 }
@@ -145,7 +143,7 @@ static void report_spawn_intermediate_status(struct libxl_ctx *ctx,
         char *intermediate_what = libxl_sprintf(ctx,
                           "%s intermediate process (startup monitor)",
                           for_spawn->what);
-        libxl_report_child_exitstatus(ctx, intermediate_what,
+        libxl_report_child_exitstatus(ctx, XL_LOG_ERROR, intermediate_what,
                                       for_spawn->intermediate, status);
     }
 }
tools/libxl/libxl_internal.h +0 -5
index 10e72e5..d1e355c 100644
@@ -49,11 +49,6 @@
 #endif
   /* all of these macros preserve errno (saving and restoring) */
 
-#define XL_LOG_DEBUG 3
-#define XL_LOG_INFO 2
-#define XL_LOG_WARNING 1
-#define XL_LOG_ERROR 0
-
 /* logging */
 void xl_logv(struct libxl_ctx *ctx, int errnoval, int loglevel, const char *file, int line, const char *func, char *fmt, va_list al);
 void xl_log(struct libxl_ctx *ctx, int errnoval, int loglevel, const char *file, int line, const char *func, char *fmt, ...);
tools/libxl/libxl_utils.h +11 -0
index fe8b975..4c04c46 100644
@@ -48,5 +48,16 @@ pid_t libxl_fork(struct libxl_ctx *ctx);
 int libxl_pipe(struct libxl_ctx *ctx, int pipes[2]);
   /* Just like fork(2), pipe(2), but log errors. */
 
+void libxl_report_child_exitstatus(struct libxl_ctx *ctx, int level,
+                                   const char *what, pid_t pid, int status);
+    /* treats all exit statuses as errors; if that's not what you want,
+     * check status yourself first */
+
+/* log levels: */
+#define XL_LOG_DEBUG 3
+#define XL_LOG_INFO 2
+#define XL_LOG_WARNING 1
+#define XL_LOG_ERROR 0
+
 #endif
 
Signature · 1 line
1.5.6.5
#13 lore raw

PATCH 8/11 libxl: Per-domain data storage for the convenience of the library user

In reply to Ian Jackson

We provide a mechanism whereby a user of the libxl library is able to store some information alongside the domain. The information stored is a block of bytes. Its lifetime is that of the domain - ie the userdata is garbage collected alongside the domain if the domain is destroyed. (This is why the feature needs to be in libxl and cannot be implemented in the user itself or in libxlutil.)

If a libxl caller does not need to use this feature it can ignore it.

The data is tagged with the (self-declared) name of the libxl user, so that different users cannot accidentally trip over each others' userdata. The data is not interpreted at all by libxl.

To assist developers and people debugging, there is a registry of the known userdata userids, and the corresponding data format as declared by that libxl user, in libxl.h next to these declarations:

int libxl_userdata_store(struct libxl_ctx *ctx, uint32_t domid,
const char *userdata_userid, const uint8_t *data, int datalen);
int libxl_userdata_retrieve(struct libxl_ctx *ctx, uint32_t domid,
const char *userdata_userid, uint8_t **data_r, int *datalen_r);

The next patch will introduce the data for the userid "xl".

---
 tools/libxl/libxl.c          |    2 +
 tools/libxl/libxl.h          |   31 +++++++++++
 tools/libxl/libxl_dom.c      |  115 ++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_internal.h |    1 +
 4 files changed, 149 insertions(+), 0 deletions(-)
tools/libxl/libxl.c +2 -0
index 711f0a0..4d069cd 100644
@@ -708,6 +708,8 @@ int libxl_domain_destroy(struct libxl_ctx *ctx, uint32_t domid, int force)
         if (!xs_rm(ctx->xsh, XBT_NULL, xapi_path))
             XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", xapi_path);
 
+    libxl__userdata_destroyall(ctx, domid);
+
     rc = xc_domain_destroy(ctx->xch, domid);
     if (rc < 0) {
         XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_destroy failed for %d", domid);
tools/libxl/libxl.h +31 -0
index 2d76087..d57dd10 100644
@@ -359,6 +359,37 @@ int libxl_device_pci_init(libxl_device_pci *pcidev, unsigned int domain,
                           unsigned int bus, unsigned int dev,
                           unsigned int func, unsigned int vdevfn);
 
+/*
+ * Functions for allowing users of libxl to store private data
+ * relating to a domain.  The data is an opaque sequence of bytes and
+ * is not interpreted or used by libxl.
+ *
+ * Data is indexed by the userdata userid, which is a short printable
+ * ASCII string.  The following list is a registry of userdata userids
+ * (the registry may be updated by posting a patch to xen-devel):
+ * 
+ *  userid      Data contents
+ *   "xl"        domain config file in xl format, Unix line endings
+ *
+ * libxl does not enforce the registration of userdata userids or the
+ * semantics of the data.  For specifications of the data formats
+ * see the code or documentation for the libxl caller in question.
+ */
+int libxl_userdata_store(struct libxl_ctx *ctx, uint32_t domid,
+                              const char *userdata_userid,
+                              const uint8_t *data, int datalen);
+  /* If datalen==0, data is not used and the user data for
+   * that domain and userdata_userid is deleted. */
+int libxl_userdata_retrieve(struct libxl_ctx *ctx, uint32_t domid,
+                                 const char *userdata_userid,
+                                 uint8_t **data_r, int *datalen_r);
+  /* On successful return, *data_r is from malloc.
+   * If there is no data for that domain and userdata_userid,
+   * *data_r and *datalen_r will be set to 0.
+   * data_r and datalen_r may be 0.
+   * On error return, *data_r and *datalen_r are undefined.
+   */
+
 typedef enum {
     POWER_BUTTON,
     SLEEP_BUTTON
tools/libxl/libxl_dom.c +115 -0

124 lines not on this page · raw message

tools/libxl/libxl_internal.h +1 -0
index d1e355c..b35cf0f 100644
@@ -130,6 +130,7 @@ int restore_common(struct libxl_ctx *ctx, uint32_t domid,
                    libxl_domain_build_info *info, libxl_domain_build_state *state, int fd);
 int core_suspend(struct libxl_ctx *ctx, uint32_t domid, int fd, int hvm, int live, int debug);
 int save_device_model(struct libxl_ctx *ctx, uint32_t domid, int fd);
+void libxl__userdata_destroyall(struct libxl_ctx *ctx, uint32_t domid);
 
 /* from xl_device */
 char *device_disk_backend_type_of_phystype(libxl_disk_phystype phystype);
Signature · 1 line
1.5.6.5
#14 lore raw

Re: [PATCH 08/11] libxl: Per-domain data storage for the convenience of the library user

Quoted 6 lines · Ian Jackson

We provide a mechanism whereby a user of the libxl library is able to store some information alongside the domain. The information stored is a block of bytes. Its lifetime is that of the domain - ie the userdata is garbage collected alongside the domain if the domain is destroyed. (This is why the feature needs to be in libxl and cannot be implemented in the user itself or in libxlutil.)

The domain is never garbage collected without having the toolstack knows about it (thinks crash gathering, other resources cleanups, policy choosing).

if the toolstack knows about it, it can do anything it want which include removing some extra userdata that it wants to store (and that doesn't have to have a special hardcoded path for it)

Signature · 1 line
Vincent
#15 lore raw

Re: [PATCH 08/11] libxl: Per-domain data storage for the convenience of the library user

Vincent Hanquez writes ("Re: [Xen-devel] [PATCH 08/11] libxl: Per-domain data storage for the convenience of the library user"):

Quoted 2 lines

The domain is never garbage collected without having the toolstack knows about it [...]

This is not true unless the libxl caller (which you are calling "the toolstack") has a never-dying daemon (or several), but it might not.

Ian.

#16 lore raw

Re: [PATCH 08/11] libxl: Per-domain data storage for the convenience of the library user

Quoted 6 lines · Ian Jackson

Vincent Hanquez writes ("Re: [Xen-devel] [PATCH 08/11] libxl: Per-domain data storage for the convenience of the library user"):

Quoted 2 lines

The domain is never garbage collected without having the toolstack knows about it [...]

This is not true unless the libxl caller (which you are calling "the toolstack") has a never-dying daemon (or several), but it might not.

All existing and working toolstacks are daemon (xend, XCP, XCI). There's just no ways to do without when you consider real world cases.

Signature · 1 line
Vincent
#17 lore raw

Re: [PATCH 08/11] libxl: Per-domain data storage for the convenience of the library user

Vincent Hanquez writes ("Re: [Xen-devel] [PATCH 08/11] libxl: Per-domain data storage for the convenience of the library user"):

Quoted 2 lines

All existing and working toolstacks are daemon (xend, XCP, XCI). There's just no ways to do without when you consider real world cases.

xl is a caller of libxl and is intended as the replacement for xm/xend. You may have noticed that this was part of the point of libxl ?

Ian.

#18 lore raw

Re: [PATCH 08/11] libxl: Per-domain data storage for the convenience of the library user

Quoted 3 lines · Ian Jackson

xl is a caller of libxl and is intended as the replacement for xm/xend. You may have noticed that this was part of the point of libxl ?

Do whatever suit you in xl, not in libxl.

libxl is a generic low level operation layer for toolstacks to use, the change your are proposing are making it harder|impossible to have one day XCP running on top of libxl.

XCP doesn't need a "locking" mechanism that use the name in xenstore for migration.

Signature · 1 line
Vincent
#19 lore raw

Re: [PATCH 08/11] libxl: Per-domain data storage for the convenience of the library user

Vincent Hanquez writes ("Re: [Xen-devel] [PATCH 08/11] libxl: Per-domain data storage for the convenience of the library user"):

Quoted 2 lines

XCP doesn't need a "locking" mechanism that use the name in xenstore for migration.

This is an unreasonable response, and I think it goes to the heart of the problem here.

xl is a first-class caller of libxl, not some second-rate unimportant thing. Every requirement of xl on libxl is a real requirement.

The real problem seems to me that you are only willing to agree to features and API properties which are useful for your "toolstacks", and not ones which are required for xl. This is not acceptable.

Ian.

#20 lore raw

Re: [PATCH 08/11] libxl: Per-domain data storage for the convenience of the library user

Quoted 7 lines · Ian Jackson

Vincent Hanquez writes ("Re: [Xen-devel] [PATCH 08/11] libxl: Per-domain data storage for the convenience of the library user"):

Quoted 2 lines

XCP doesn't need a "locking" mechanism that use the name in xenstore for migration.

This is an unreasonable response, and I think it goes to the heart of the problem here.

Quoted 6 lines

xl is a first-class caller of libxl, not some second-rate unimportant thing. Every requirement of xl on libxl is a real requirement.

The real problem seems to me that you are only willing to agree to features and API properties which are useful for your "toolstacks", and not ones which are required for xl. This is not acceptable.

libxl has been made to "make common the low level operations of actuals toolstacks", so that every toolstacks will share the same code base to avoid code duplication and also give user more flexibility to move between toolstacks.

Any others goals you have in mind, is a part of your own goals, not libxl's. Whilst i'm happy to see more assumptions removed to have even more generic code, adding assumptions on the other-hand, related to name in this case, is a non-starter.

Signature · 1 line
Vincent
#21 lore raw

Re: [PATCH 08/11] libxl: Per-domain data storage for the convenience of the library user

Vincent Hanquez writes ("Re: [Xen-devel] [PATCH 08/11] libxl: Per-domain data storage for the convenience of the library user"):

Quoted 4 lines

Any others goals you have in mind, is a part of your own goals, not libxl's. Whilst i'm happy to see more assumptions removed to have even more generic code, adding assumptions on the other-hand, related to name in this case, is a non-starter.

There is no assumption added. Callers who do not want the ability to atomically rename a domain (which is what libxl_domain_rename provides) can simply ignore the ability to do so.

Ian.

#22 lore raw

Re: [PATCH 08/11] libxl: Per-domain data storage for the convenience of the library user

In reply to Ian Jackson
Quoted 7 lines · Ian Jackson

Vincent Hanquez writes ("Re: [Xen-devel] [PATCH 08/11] libxl: Per-domain data storage for the convenience of the library user"):

Quoted 2 lines

The domain is never garbage collected without having the toolstack knows about it [...]

This is not true unless the libxl caller (which you are calling "the toolstack") has a never-dying daemon (or several), but it might not.

Actually I think Vincent is right in this case: there is no point in adding this feature to libxenlight because xl is perfectly capable of handling userdata by itself; for example xl could store the userdata when the domain is created, leave it there when the domain dies and remove\rename it when another domain with the same identifier is created.

For this reason I think you should move these two functions to libxlutil.

#23 lore raw

PATCH 9/11 xl: New savefile format. Save domain config when saving a domain.

In reply to Ian Jackson

We introduce a new format for saved domains. The new format, in contrast to the old:
* Has a magic number which can distinguish it from other kinds of file
* Is extensible
* Can contains the domain configuration file

On domain creation we remember the actual config file used (using the toolstack data feature of libxl, just introduced), and by default save it to the save file.

However, options are provided for the following:
* Reading and writing old-style xl saved domain files. (These will become deprecated at some point in the future.)
* When saving a domain, supplying an alternative config file to store in the savefile.
* When restoring a domain, supplying an alternative config file.

If a domain is restored with a different config file, it is the responsibility of the xl user to ensure that the two configs are "compatible". Changing the targets of virtual devices is supported; changing other features of the domain is not recommended. Bad changes may lead to undefined behaviour in the domain, and are in practice likely to cause resume failures or crashes.

---
 tools/libxl/libxlu_cfg.c |   37 ++++++
 tools/libxl/libxlutil.h  |    3 +-
 tools/libxl/xl.c         |  280 +++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 289 insertions(+), 31 deletions(-)
tools/libxl/libxlu_cfg.c +37 -0
index 632c371..0a5ccef 100644
@@ -53,6 +53,43 @@ int xlu_cfg_readfile(XLU_Config *cfg, const char *real_filename) {
     return ctx.err;
 }
 
+int xlu_cfg_readdata(XLU_Config *cfg, const char *data, int length) {
+    CfgParseContext ctx;
+    int e, r;
+    YY_BUFFER_STATE buf= 0;
+
+    ctx.scanner= 0;
+    ctx.cfg= cfg;
+    ctx.err= 0;
+    ctx.lexerrlineno= -1;
+    
+    e= xlu__cfg_yylex_init_extra(&ctx, &ctx.scanner);
+    if (e) {
+        fprintf(cfg->report,"%s: unable to create scanner: %s\n",
+                cfg->filename, strerror(e));
+        ctx.err= e;
+        ctx.scanner= 0;
+        goto xe;
+    }
+
+    buf = xlu__cfg_yy_scan_bytes(data, length, ctx.scanner);
+    if (!buf) {
+        fprintf(cfg->report,"%s: unable to allocate scanner buffer\n",
+                cfg->filename);
+        ctx.err= ENOMEM;
+        goto xe;
+    }
+
+    r= xlu__cfg_yyparse(&ctx);
+    if (r) assert(ctx.err);
+
+ xe:
+    if (buf) xlu__cfg_yy_delete_buffer(buf, ctx.scanner);
+    if (ctx.scanner) xlu__cfg_yylex_destroy(ctx.scanner);
+
+    return ctx.err;
+}
+
 void xlu__cfg_set_free(XLU_ConfigSetting *set) {
     free(set->name);
     free(set->values);
tools/libxl/libxlutil.h +2 -1
index 97b16aa..0262e55 100644
@@ -30,7 +30,8 @@ XLU_Config *xlu_cfg_init(FILE *report, const char *report_filename);
    *  until the Config is destroyed. */
 
 int xlu_cfg_readfile(XLU_Config*, const char *real_filename);
-  /* If this fails, then it is undefined behaviour to call xlu_cfg_get_...
+int xlu_cfg_readdata(XLU_Config*, const char *data, int length);
+  /* If these fail, then it is undefined behaviour to call xlu_cfg_get_...
    * functions.  You have to just xlu_cfg_destroy. */
  
 void xlu_cfg_destroy(XLU_Config*);
tools/libxl/xl.c +250 -30

454 lines not on this page · raw message

Signature · 1 line
1.5.6.5
#24 lore raw

Re: [PATCH 09/11] xl: New savefile format. Save domain config when saving a domain.

Quoted 17 lines · Ian Jackson

We introduce a new format for saved domains. The new format, in contrast to the old:
* Has a magic number which can distinguish it from other kinds of file
* Is extensible
* Can contains the domain configuration file

On domain creation we remember the actual config file used (using the toolstack data feature of libxl, just introduced), and by default save it to the save file.

However, options are provided for the following:
* Reading and writing old-style xl saved domain files. (These will become deprecated at some point in the future.)
* When saving a domain, supplying an alternative config file to store in the savefile.
* When restoring a domain, supplying an alternative config file.

Quoted 7 lines

If a domain is restored with a different config file, it is the responsibility of the xl user to ensure that the two configs are "compatible". Changing the targets of virtual devices is supported; changing other features of the domain is not recommended. Bad changes may lead to undefined behaviour in the domain, and are in practice likely to cause resume failures or crashes.

I think that providing compatibility with the old-style xl saved domain files is overkill and just adds complexity for no good reason. We should stick to the new format and forget about the old one altogether.

For the same reason I would remove the option to save\restore from an alternative config file.

Please keep it simple.

tools/libxl/xl.c line 56

Quoted 13 lines
tools/libxl/xl.c +13 -0
+
+static const char savefileheader_magic[32]=
+    "Xen saved domain, xl format\n \0 \r";
+
+typedef struct {
+    char magic[32]; /* savefileheader_magic */
+    /* All uint32_ts are in domain's byte order. */
+    uint32_t byteorder; /* SAVEFILE_BYTEORDER_VALUE */
+    uint32_t mandatory_flags; /* unknown flags => reject restore */
+    uint32_t optional_flags; /* unknown flags => reject restore */
+    uint32_t optional_data_len; /* skip, or skip tail, if not understood */
+} SaveFileHeader;
+

I don't think SaveFileHeader is consistent with the naming convention used so far; could you please call it save_file_header instead?

tools/libxl/xl.c static void printf_info(libxl_domain_create_info *c_info, · line 350

Quoted 7 lines
tools/libxl/xl.c +3 -1
-static void parse_config_file(const char *filename,
+static void parse_config_data(const char *configfile_filename_report,
+                              const char *configfile_data,
+                              int configfile_len,
                               libxl_domain_create_info *c_info,
                               libxl_domain_build_info *b_info,
                               libxl_device_disk **disks,

I think at this point we should remove const 'char *filename' altogether

Quoted 15 lines
tools/libxl/xl.c +9 -0
@@ -693,27 +746,124 @@ static void create_domain(int debug, int daemonize, const char *config_file, con
     int ret;
     libxl_device_model_starting *dm_starting = 0;
     libxl_waiter *w1 = NULL, *w2 = NULL;
+    void *config_data = 0;
+    int config_len = 0;
+    int restore_fd = -1;
+    SaveFileHeader hdr;
+
     memset(&dm_info, 0x00, sizeof(dm_info));
 
+    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
+        fprintf(stderr, "cannot init xl context\n");
+        exit(1);
+    }

tools/libxl/xl.c static void create_domain(int debug, int daemonize, const char *config_file, con · line 769

Quoted 10 lines
tools/libxl/xl.c +10 -0
+
+    if (restore_file) {
+        uint8_t *optdata_begin = 0;
+        const uint8_t *optdata_here = 0;
+        union { uint32_t u32; char b[4]; } u32buf;
+
+        restore_fd = open(restore_file, O_RDONLY);
+
+        if (suspend_old_xl_format) {
+            memset(&hdr,0,sizeof(hdr));

just remove this case

Quoted 69 lines
tools/libxl/xl.c +69 -0
+        } else {
+            uint32_t badflags;
+
+            CHK_ERRNO( libxl_read_exactly(&ctx, restore_fd, &hdr,
+                   sizeof(hdr), restore_file, "header") );
+            if (memcmp(hdr.magic, savefileheader_magic, sizeof(hdr.magic))) {
+                fprintf(stderr, "File has wrong magic number -"
+                        " corrupt or needs -O?\n");
+                exit(2);
+            }
+            if (hdr.byteorder != SAVEFILE_BYTEORDER_VALUE) {
+                fprintf(stderr, "File has wrong byte order\n");
+                exit(2);
+            }
+            fprintf(stderr, "Loading new save file %s"
+                    " (new xl fmt info"
+                    " 0x%"PRIx32"/0x%"PRIx32"/%"PRIu32")\n",
+                    restore_file, hdr.mandatory_flags, hdr.optional_flags,
+                    hdr.optional_data_len);
+
+            badflags = hdr.mandatory_flags & ~( 0 /* none understood yet */ );
+            if (badflags) {
+                fprintf(stderr, "Savefile has mandatory flag(s) 0x%"PRIx32" "
+                        "which are not supported; need newer xl\n",
+                        badflags);
+                exit(2);
+            }
+        }
+        if (hdr.optional_data_len) {
+            optdata_begin = xmalloc(hdr.optional_data_len);
+            CHK_ERRNO( libxl_read_exactly(&ctx, restore_fd, optdata_begin,
+                   hdr.optional_data_len, restore_file, "optdata") );
+        }
+
+#define OPTDATA_LEFT  (hdr.optional_data_len - (optdata_here - optdata_begin))
+#define WITH_OPTDATA(amt, body)                                         \
+            if (OPTDATA_LEFT < (amt)) {                                 \
+                fprintf(stderr, "Savefile truncated.\n"); exit(2);      \
+            } else {                                                    \
+                body;                                                   \
+                optdata_here += (amt);                                  \
+            }
+
+        optdata_here = optdata_begin;
+
+        if (OPTDATA_LEFT) {
+            fprintf(stderr, " Savefile contains xl domain config\n");
+            WITH_OPTDATA(4, {
+                memcpy(u32buf.b, optdata_here, 4);
+                config_len = u32buf.u32;
+            });
+            WITH_OPTDATA(config_len, {
+                config_data = xmalloc(config_len);
+                memcpy(config_data, optdata_here, config_len);
+            });
+        }
+
+    }
+
+    if (config_file) {
+        free(config_data);  config_data = 0;
+        ret = libxl_read_file_contents(&ctx, config_file,
+                                       &config_data, &config_len);
+        if (ret) { fprintf(stderr, "Failed to read config file: %s: %s\n",
+                           config_file, strerror(errno)); exit(1); }
+    } else {
+        if (!config_data) {
+            fprintf(stderr, "Config file not specified and"
+                    " none in save file\n");

tools/libxl/xl.c static void create_domain(int debug, int daemonize, const char *config_file, con · line 847

Quoted 10 lines
tools/libxl/xl.c +8 -1
+            exit(1);
+        }
+        config_file = "<saved>";
+    }
+
     printf("Parsing config file %s\n", config_file);
-    parse_config_file(config_file, &info1, &info2, &disks, &num_disks, &vifs, &num_vifs, &pcidevs, &num_pcidevs, &vfbs, &num_vfbs, &vkbs, &num_vkbs, &dm_info);
+
+    parse_config_data(config_file, config_data, config_len, &info1, &info2, &disks, &num_disks, &vifs, &num_vifs, &pcidevs, &num_pcidevs, &vfbs, &num_vfbs, &vkbs, &num_vkbs, &dm_info);
+

just remove config_file

Quoted 10 lines
tools/libxl/xl.c +2 -1
@@ -900,16 +1048,18 @@ static void help(char *command)
         printf("Usage: xl unpause <Domain>\n\n");
         printf("Unpause a paused domain.\n\n");
     } else if(!strcmp(command, "save")) {
-        printf("Usage: xl save [options] <Domain> <CheckpointFile>\n\n");
+        printf("Usage: xl save [options] <Domain> <CheckpointFile> [<ConfigFile>]\n\n");
         printf("Save a domain state to restore later.\n\n");
         printf("Options:\n\n");
         printf("-h                     Print this help.\n");
+        printf("-O                     Old (configless) xl save format.\n");

tools/libxl/xl.c static void help(char *command) · line 1064

Quoted 10 lines
tools/libxl/xl.c +2 -1
         printf("-c                     Leave domain running after creating the snapshot.\n");
     } else if(!strcmp(command, "restore")) {
-        printf("Usage: xl restore [options] <ConfigFile> <CheckpointFile>\n\n");
+        printf("Usage: xl restore [options] [<ConfigFile>] <CheckpointFile>\n\n");
         printf("Restore a domain from a saved state.\n\n");
         printf("Options:\n\n");
         printf("-h                     Print this help.\n");
+        printf("-O                     Old (configless) xl save format.\n");
         printf("-p                     Do not unpause domain after restoring it.\n");
         printf("-e                     Do not wait in the background for the death of the domain.\n");

do not add [<ConfigFile>], if you really want to provide this feature, use an option instead, so that we keep compatibility with xm:

xl save [options] <Domain> <CheckpointFile>
xl restore [options] <CheckpointFile>

#25 lore raw

PATCH 10/11 xl: Domain creation logging fixes

In reply to Ian Jackson

* Make create_domain always return to caller
* Have create_domain set its log callback sooner
* Actually write things to logfile, and some error checking

With some combinations of options, create_domain would never return to the caller, since it would have called daemon and will later exit. So we fork an additional time, so that we can call daemon in the child and also return to the caller in the parent. It's a shame that there's no version of daemon(3) that allows us to do this without the extra code and pointless extra fork.

daemon(0,0) closes all the fds. So we need to call daemon(0,1) and organise detaching our stdin/out/err ourselves. Doing this makes messages actually appear in the xl logfile in /var/log/xen.

Finally, make create_domain call libxl_ctx_set_log sooner. This makes some lost messages appear.

---
 tools/libxl/xl.c |   43 ++++++++++++++++++++++++++++++++++++-------
 1 files changed, 36 insertions(+), 7 deletions(-)
tools/libxl/xl.c +36 -7
index f3729f3..7d35db1 100644
@@ -743,7 +743,7 @@ static void create_domain(int debug, int daemonize, const char *config_file, con
     int num_disks = 0, num_vifs = 0, num_pcidevs = 0, num_vfbs = 0, num_vkbs = 0;
     int i, fd;
     int need_daemon = 1;
-    int ret;
+    int ret, rc;
     libxl_device_model_starting *dm_starting = 0;
     libxl_waiter *w1 = NULL, *w2 = NULL;
     void *config_data = 0;
@@ -757,6 +757,7 @@ static void create_domain(int debug, int daemonize, const char *config_file, con
         fprintf(stderr, "cannot init xl context\n");
         exit(1);
     }
+    libxl_ctx_set_log(&ctx, log_callback, NULL);
 
     if (restore_file) {
         uint8_t *optdata_begin = 0;
@@ -851,8 +852,6 @@ static void create_domain(int debug, int daemonize, const char *config_file, con
 start:
     domid = 0;
 
-    libxl_ctx_set_log(&ctx, log_callback, NULL);
-
     ret = libxl_domain_make(&ctx, &info1, &domid);
     if (ret) {
         fprintf(stderr, "cannot make domain: %d\n", ret);
@@ -931,17 +930,47 @@ start:
 
     if (need_daemon) {
         char *fullname, *name;
+        pid_t child1, got_child;
+        int nullfd;
+
+        child1 = libxl_fork(&ctx);
+        if (child1) {
+            int status;
+            for (;;) {
+                got_child = waitpid(child1, &status, 0);
+                if (got_child == child1) break;
+                assert(got_child == -1);
+                if (errno != EINTR) {
+                    perror("failed to wait for daemonizing child");
+                    return ERROR_FAIL;
+                }
+            }
+            if (status) {
+                libxl_report_child_exitstatus(&ctx, XL_LOG_ERROR,
+                           "daemonizing child", child1, status);
+                return ERROR_FAIL;
+            }
+            return 0; /* caller gets success in parent */
+        }
 
         asprintf(&name, "xl-%s", info1.name);
-        libxl_create_logfile(&ctx, name, &fullname);
-        logfile = open(fullname, O_WRONLY|O_CREAT, 0644);
+        rc = libxl_create_logfile(&ctx, name, &fullname);
+        if (rc) return rc;
+
+        CHK_ERRNO(( logfile = open(fullname, O_WRONLY|O_CREAT, 0644) )<0);
         free(fullname);
         free(name);
 
-        daemon(0, 0);
+        CHK_ERRNO(( nullfd = open("/dev/null", O_RDONLY) )<0);
+        dup2(nullfd, 0);
+        dup2(logfile, 1);
+        dup2(logfile, 2);
+
+        daemon(0, 1);
         need_daemon = 0;
     }
-    LOG("Waiting for domain %s (domid %d) to die", info1.name, domid);
+    LOG("Waiting for domain %s (domid %d) to die [pid %ld]",
+        info1.name, domid, (long)getpid());
     w1 = (libxl_waiter*) xmalloc(sizeof(libxl_waiter) * num_disks);
     w2 = (libxl_waiter*) xmalloc(sizeof(libxl_waiter));
     libxl_wait_for_disk_ejects(&ctx, domid, disks, num_disks, w1);
Signature · 1 line
1.5.6.5
#26 lore raw

PATCH 11/11 xl: Migration support

In reply to Ian Jackson

Implement "xl migrate".

ssh is used as the transport by default, although this can be overridden by specifying a different sshcommand. This is a very standard approach nowadays and avoids the need for daemons at the target host in the default configuration, while providing flexibility to admins. (In the future it might be nice to support plain unencrypted migration over TCP, which we do not rule out now, although it is not currently implemented.)

Properties of the migration protocol:
* The domain on the target machine is named "<domname>--incoming" while it is being transferred.
* The domain on the source machine is renamed "<domain>--migratedaway" before we give the target permission to rename and unpause.
* The locking in libxl_domain_rename ensures that of two simultaneous migration attempts no more than one will succeed.
* We go to some considerable effort to avoid leaving the domain in a bad state if something goes wrong with one of the ends or the network, although there is still (inevitably) a possibility of a unresolvable state (in case of very badly timed network failure) which is probably best resolved by destroying the domain at both ends.

Incidental changes:
create_domain now returns a libxl error code rather than exiting on error. domain_qualifier_to_domid takes an argument for saving the supplied name. Various common information (eg the domid we are operating on) is
now in static variables rather than locals.
New ERROR_BADFAIL error code for reporting unpleasant failures.

migration fixes
Migration fixup error reporting

migration - resume, not unpause

Error handling fixes; initialise common_ctx

migration fixes

---
 tools/libxl/libxl.h |    1 +
 tools/libxl/xl.c    |  675 ++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 590 insertions(+), 86 deletions(-)
tools/libxl/libxl.h +1 -0
index d57dd10..62b6cff 100644
@@ -241,6 +241,7 @@ enum {
     ERROR_NI = -3,
     ERROR_NOMEM = -4,
     ERROR_INVAL = -5,
+    ERROR_BADFAIL = -6,
 };
 
 #define LIBXL_VERSION 0
tools/libxl/xl.c +589 -86

956 lines not on this page · raw message

Signature · 1 line
1.5.6.5
#27 lore raw

Re: [PATCH 11/11] xl: Migration support

Quoted 14 lines · Ian Jackson
tools/libxl/xl.c +1 -0
index 7d35db1..53be99c 100644
@@ -17,6 +17,7 @@
 #include "libxl_osdeps.h"
 
 #include <stdio.h>
+#include <assert.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
@@ -42,9 +43,33 @@ int logfile = 2;
 

tools/libxl/xl.c int logfile = 2; · line 53

Quoted 10 lines
tools/libxl/xl.c +8 -0
 static int suspend_old_xl_format = 0;
 
+static struct libxl_ctx common_ctx;
+static uint32_t common_domid;
+static const char *common_domname;
+static uint8_t *common_config_data;
+static int common_config_len;
+
+static char *migration_domname;
+

I really don't like the usage of global variables to pass parameters between different functions.

In any case if you want to start using global variables you should do it consistently across all the file: please write a separate patch that does this clean up.

Quoted 34 lines
tools/libxl/xl.c +17 -1
 static const char savefileheader_magic[32]=
     "Xen saved domain, xl format\n \0 \r";
 
+static const char migrate_receiver_banner[]=
+    "xl migration receiver ready, send binary domain data.\n";
+static const char migrate_receiver_ready[]=
+    "domain received, ready to unpause";
+static const char migrate_permission_to_go[]=
+    "domain is yours, you are cleared to unpause";
+static const char migrate_report[]=
+    "my copy unpause results are as follows";
+  /* followed by one byte:
+   *     0: everything went well, domain is running
+   *            next thing is we all exit
+   * non-0: things went badly
+   *            next thing should be a migrate_permission_to_go
+   *            from target to source
+   */
+
 typedef struct {
     char magic[32]; /* savefileheader_magic */
     /* All uint32_ts are in domain's byte order. */
@@ -69,7 +94,7 @@ void log_callback(void *userdata, int loglevel, const char *file, int line, cons
     write(logfile, str, strlen(str));
 }
 
-static int domain_qualifier_to_domid(struct libxl_ctx *ctx, char *p, uint32_t *domid)
+static int domain_qualifier_to_domid(struct libxl_ctx *ctx, char *p, uint32_t *domid, const char **expected_name_r)
 {
     int i, alldigit;
 
@@ -83,10 +108,12 @@ static int domain_qualifier_to_domid(struct libxl_ctx *ctx, char *p, uint32_t *d
 
     if (i > 0 && alldigit) {

tools/libxl/xl.c static int domain_qualifier_to_domid(struct libxl_ctx *ctx, char *p, uint32_t *d · line 119

Quoted 10 lines
tools/libxl/xl.c +2 -0
         *domid = strtoul(p, NULL, 10);
+        if (expected_name_r) *expected_name_r = 0;
         return 0;
     } else {
         /* check here if it's a uuid and do proper conversion */
     }
+    if (expected_name_r) *expected_name_r = p;
     return libxl_name_to_domid(ctx, p, domid);
 }
 

can we avoid modifying domain_qualifier_to_domid and use libxl_domid_to_name instead?

Quoted 16 lines
tools/libxl/xl.c +12 -0
@@ -1091,6 +1126,25 @@ static void help(char *command)
         printf("-O                     Old (configless) xl save format.\n");
         printf("-p                     Do not unpause domain after restoring it.\n");
         printf("-e                     Do not wait in the background for the death of the domain.\n");
+        printf("-d                     Enable debug messages.\n");
+    } else if(!strcmp(command, "migrate")) {
+        printf("Usage: xl migrate [options] <Domain> <host>\n\n");
+        printf("Save a domain state to restore later.\n\n");
+        printf("Options:\n\n");
+        printf("-h                     Print this help.\n");
+        printf("-C <config>            Send <config> instead of config file from creation.\n");
+        printf("-s <sshcommand>        Use <sshcommand> instead of ssh.  String will be passed to sh.  If empty, run <host> instead of ssh <host> xl migrate-receive [-d -e]\n");
+        printf("-e                     Do not wait in the background (on <host>) for the death of the domain.\n");
+    } else if(!strcmp(command, "migrate-receive")) {
+        printf("Usage: xl migrate-receive  - for internal use only");
+    } else if(!strcmp(command, "restore")) {

tools/libxl/xl.c static void help(char *command) · line 1150

Quoted 10 lines
tools/libxl/xl.c +7 -0
+        printf("Usage: xl restore [options] [<ConfigFile>] <CheckpointFile>\n\n");
+        printf("Restore a domain from a saved state.\n\n");
+        printf("Options:\n\n");
+        printf("-h                     Print this help.\n");
+        printf("-O                     Old (configless) xl save format.\n");
+        printf("-p                     Do not unpause domain after restoring it.\n");
+        printf("-e                     Do not wait in the background for the death of the domain.\n");
     } else if(!strcmp(command, "destroy")) {
         printf("Usage: xl destroy <Domain>\n\n");
         printf("Terminate a domain immediately.\n\n");

just remove the old "configless" save format

Acked-by on the v1 series
#28 lore raw
In reply to Ian Jackson
Quoted 11 lines · Ian Jackson

This patch series provides support for "xl migrate".

There are a number of bugfixes and extensions to libxl, followed by a new savefile format (which can contain the domain config file and is more extensible), followed by actual migration support.

This should probably not go into 4.0 :-).

I have comments on patch 8, 9 and 11; all the rest: