[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH V4 20/24] xl: introduce load/save_domain_config



They are used to serialize / deserialize domain configuration to / from
user data store.

Signed-off-by: Wei Liu <wei.liu2@xxxxxxxxxx>
---
 tools/libxl/xl_cmdimpl.c |  119 ++++++++++++++++++++++++++++++----------------
 1 file changed, 77 insertions(+), 42 deletions(-)

diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c
index 7443d86..f891ab5 100644
--- a/tools/libxl/xl_cmdimpl.c
+++ b/tools/libxl/xl_cmdimpl.c
@@ -160,6 +160,73 @@ struct domain_create {
     char **migration_domname_r; /* from malloc */
 };
 
+static int _parse_config_data_json(char *config_data, int config_len,
+                                   libxl_domain_config *d_config)
+{
+    int ret;
+
+    /* Make sure this string ends with \0 -- the parser expects a NULL
+     * terminated string.
+     */
+    if (config_data[config_len-1] != '\0') {
+        config_data = realloc(config_data, config_len + 1);
+        if (!config_data) {
+            fprintf(stderr, "Failed to realloc config_data\n");
+            ret = ERROR_NOMEM;
+            goto out;
+        }
+        config_data[config_len] = '\0';
+    }
+
+    ret = libxl_domain_config_from_json(ctx, d_config, config_data);
+
+out:
+    return ret;
+}
+/* Load domain configuration from userdata store */
+static int load_domain_config(uint32_t domid, libxl_domain_config *d_config)
+{
+    uint8_t *data;
+    int ret, len;
+
+    ret = libxl_userdata_retrieve(ctx, domid, "xl-json", &data, &len);
+    if (ret) {
+        fprintf(stderr, "failed to retrieve guest configuration (rc=%d).", 
ret);
+        ret = ERROR_FAIL;
+        goto out;
+    }
+
+    ret = _parse_config_data_json((char *)data, len, d_config);
+    if (ret)
+        goto out;
+
+    free(data);
+
+out:
+    return ret;
+}
+
+/* Store domain configuration to userdata store */
+static int store_domain_config(uint32_t domid, libxl_domain_config *d_config)
+{
+    char *d_config_json;
+    int ret;
+
+    d_config_json = libxl_domain_config_to_json(ctx, d_config);
+
+    ret = libxl_userdata_store(ctx, domid, "xl-json",
+                               (const uint8_t *)d_config_json,
+                               strlen(d_config_json));
+
+    free(d_config_json);
+
+    if (ret) {
+        fprintf(stderr, "failed to store guest configuration (rc=%d). ", ret);
+        return ERROR_FAIL;
+    }
+
+    return 0;
+}
 
 static uint32_t find_domain(const char *p) __attribute__((warn_unused_result));
 static uint32_t find_domain(const char *p)
@@ -737,19 +804,7 @@ static void parse_config_data_json(char *config_data,
         exit(1);
     }
 
-    /* Make sure this string ends with \0 -- the parser expects a NULL
-     * terminated string.
-     */
-    if (config_data[config_len-1] != '\0') {
-        config_data = realloc(config_data, config_len + 1);
-        if (!config_data) {
-            fprintf(stderr, "Failed to realloc config_data\n");
-            exit(1);
-        }
-        config_data[config_len] = '\0';
-    }
-
-    ret = libxl_domain_config_from_json(ctx, d_config, config_data);
+    ret = _parse_config_data_json(config_data, config_len, d_config);
     if (ret) {
         fprintf(stderr, "Failed to parse config\n");
         exit(1);
@@ -2060,7 +2115,6 @@ static uint32_t create_domain(struct domain_create 
*dom_info)
     uint32_t domid = INVALID_DOMID;
 
     libxl_domain_config d_config, d_config_saved;
-    char *d_config_json = NULL;
 
     int debug = dom_info->debug;
     int daemonize = dom_info->daemonize;
@@ -2318,15 +2372,8 @@ start:
     }
 
     update_domain_config(&d_config_saved, &d_config);
-    d_config_json = libxl_domain_config_to_json(ctx, &d_config_saved);
-    if (d_config_json == NULL) {
-        perror("cannot generate JSON object of domain configuration");
-        ret = ERROR_FAIL;
-        goto error_out;
-    }
-    ret = libxl_userdata_store(ctx, domid, "xl-json",
-                               (const uint8_t *)d_config_json,
-                               strlen(d_config_json));
+
+    ret = store_domain_config(domid, &d_config_saved);
     if (ret) {
         perror("cannot save config file");
         ret = ERROR_FAIL;
@@ -2492,8 +2539,6 @@ out:
 
     free(config_data);
 
-    free(d_config_json);
-
     console_child_report(child_console);
 
     if (deathw)
@@ -3218,9 +3263,7 @@ static void list_domains_details(const libxl_dominfo 
*info, int nb_domain)
 {
     libxl_domain_config d_config;
 
-    char *config_source;
-    uint8_t *data;
-    int i, len, rc;
+    int i, rc;
 
     yajl_gen hand = NULL;
     yajl_gen_status s;
@@ -3244,20 +3287,17 @@ static void list_domains_details(const libxl_dominfo 
*info, int nb_domain)
         /* no detailed info available on dom0 */
         if (info[i].domid == 0)
             continue;
-        rc = libxl_userdata_retrieve(ctx, info[i].domid, "xl-json",
-                                     &data, &len);
-        if (rc)
-            continue;
-        CHK_SYSCALL(asprintf(&config_source, "<domid %d data>", 
info[i].domid));
         libxl_domain_config_init(&d_config);
-        parse_config_data_json((char *)data, len, &d_config);
+        rc = load_domain_config(info[i].domid, &d_config);
+        if (rc) {
+            libxl_domain_config_dispose(&d_config);
+            continue;
+        }
         if (default_output_format == OUTPUT_FORMAT_JSON)
             s = printf_info_one_json(hand, info[i].domid, &d_config);
         else
             printf_info_sexp(info[i].domid, &d_config);
         libxl_domain_config_dispose(&d_config);
-        free(data);
-        free(config_source);
         if (s != yajl_gen_status_ok)
             goto out;
     }
@@ -4622,17 +4662,12 @@ int main_config_update(int argc, char **argv)
         printf_info(default_output_format, -1, &d_config);
 
     if (!dryrun_only) {
-        char *d_config_json = NULL;
         fprintf(stderr, "setting dom%d configuration\n", domid);
-        d_config_json = libxl_domain_config_to_json(ctx, &d_config);
-        rc = libxl_userdata_store(ctx, domid, "xl-json",
-                                  (const uint8_t*)d_config_json,
-                                  strlen(d_config_json));
+        rc = store_domain_config(domid, &d_config);
         if (rc) {
             fprintf(stderr, "failed to update configuration\n");
             exit(1);
         }
-        free(d_config_json);
     }
 
     libxl_domain_config_dispose(&d_config);
-- 
1.7.10.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.