libxl: introduce helper to initialise Dom0
authorWei Liu <wei.liu2@citrix.com>
Thu, 4 Sep 2014 22:43:14 +0000 (23:43 +0100)
committerIan Campbell <ian.campbell@citrix.com>
Tue, 9 Sep 2014 11:46:33 +0000 (12:46 +0100)
This small helper is responsible for generating Dom0 JSON config
stub and writing Dom0 xenstore entries. This helpers subsumes two calls
to xenstore-write in xencommons script.

Dom0 UUID is intentionally left untouched, so it is always all
zeros.  This makes sure that we don't leak Dom0 stubs across rebooting.

Signed-off-by: Wei Liu <wei.liu2@citrix.com>
Acked-by: Ian Campbell <ian.campbell@citrix.com>
[ ijc -- folded in incremental patch for *BSD ]

.gitignore
tools/hotplug/FreeBSD/rc.d/xencommons
tools/hotplug/Linux/init.d/xencommons.in.in
tools/hotplug/NetBSD/rc.d/xencommons
tools/libxl/Makefile
tools/libxl/xen-init-dom0.c [new file with mode: 0644]

index 6d725aa22561038af0ccd20d36dd4218956e11b1..8e34b851891357330f619f9ef1abb32accefadc8 100644 (file)
@@ -313,6 +313,7 @@ tools/libxl/testidl.c
 tools/libxl/*.pyc
 tools/libxl/libxl-save-helper
 tools/libxl/test_timedereg
+tools/libxl/xen-init-dom0
 tools/blktap2/control/tap-ctl
 tools/firmware/etherboot/eb-roms.h
 tools/firmware/etherboot/gpxe-git-snapshot.tar.gz
index a797016a0b184247837dcae9be08e274e30c9f36..2d9034ce254e2fdd28ef5a479bddf6cbb55fecca 100644 (file)
@@ -64,9 +64,8 @@ xen_startcmd()
 
        printf "\n"
 
-       printf "Setting domain 0 name and domid.\n"
-       ${BINDIR}/xenstore-write "/local/domain/0/name" "Domain-0"
-       ${BINDIR}/xenstore-write "/local/domain/0/domid" 0
+       printf "Setting domain 0 name, domid and JSON config...\n"
+       ${PRIVATE_BINDIR}/xen-init-dom0
 }
 
 xen_stop()
index b311bb8192c8246600548bbd395737fbfbb1717d..1d860d94e3b6ed6727101ed0b1ea0b0759e3a92d 100644 (file)
@@ -90,9 +90,8 @@ do_start () {
                    exit 1
                fi
 
-               echo Setting domain 0 name and domid...
-               ${BINDIR}/xenstore-write "/local/domain/0/name" "Domain-0"
-               ${BINDIR}/xenstore-write "/local/domain/0/domid" 0
+               echo Setting domain 0 name, domid and JSON config...
+               ${PRIVATE_BINDIR}/xen-init-dom0
        fi
 
        echo Starting xenconsoled...
index b1c353100dab49fcff0a693695865f030907029c..7ac87e2e89de19317a91487f0a9f609853f2c84d 100644 (file)
@@ -68,9 +68,8 @@ xen_startcmd()
 
        printf "\n"
 
-       printf "Setting domain 0 name and domid.\n"
-       ${BINDIR}/xenstore-write "/local/domain/0/name" "Domain-0"
-       ${BINDIR}/xenstore-write "/local/domain/0/domid" 0
+       printf "Setting domain 0 name, domid and JSON config...\n"
+       ${PRIVATE_BINDIR}/xen-init-dom0
 }
 
 xen_stop()
index bd0db3ba0bd0c516ea2a3d4a7f68f620c4b6e779..4bee4af7f2b353c84ad30b072a2500864518331d 100644 (file)
@@ -110,7 +110,7 @@ LIBXLU_OBJS = libxlu_cfg_y.o libxlu_cfg_l.o libxlu_cfg.o \
        libxlu_disk_l.o libxlu_disk.o libxlu_vif.o libxlu_pci.o
 $(LIBXLU_OBJS): CFLAGS += $(CFLAGS_libxenctrl) # For xentoollog.h
 
-CLIENTS = xl testidl libxl-save-helper
+CLIENTS = xl testidl libxl-save-helper xen-init-dom0
 
 CFLAGS_XL += $(CFLAGS_libxenlight)
 CFLAGS_XL += -Wshadow
@@ -121,6 +121,10 @@ $(XL_OBJS) $(TEST_PROG_OBJS) _libxl.api-for-check: \
 $(XL_OBJS): CFLAGS += $(CFLAGS_XL)
 $(XL_OBJS): CFLAGS += -include $(XEN_ROOT)/tools/config.h # libxl_json.h needs it.
 
+XEN_INIT_DOM0_OBJS = xen-init-dom0.o
+$(XEN_INIT_DOM0_OBJS): CFLAGS += $(CFLAGS_libxenctrl)
+$(XEN_INIT_DOM0_OBJS): CFLAGS += $(CFLAGS_libxenstore)
+
 SAVE_HELPER_OBJS = libxl_save_helper.o _libxl_save_msgs_helper.o
 $(SAVE_HELPER_OBJS): CFLAGS += $(CFLAGS_libxenctrl)
 
@@ -222,6 +226,9 @@ libxlutil.a: $(LIBXLU_OBJS)
 xl: $(XL_OBJS) libxlutil.so libxenlight.so
        $(CC) $(LDFLAGS) -o $@ $(XL_OBJS) libxlutil.so $(LDLIBS_libxenlight) $(LDLIBS_libxenctrl) -lyajl $(APPEND_LDFLAGS)
 
+xen-init-dom0: $(XEN_INIT_DOM0_OBJS) libxenlight.so
+       $(CC) $(LDFLAGS) -o $@ $(XEN_INIT_DOM0_OBJS) $(LDLIBS_libxenstore) $(LDLIBS_libxenlight) $(LDLIBS_libxenctrl) $(APPEND_LDFLAGS)
+
 test_%: test_%.o test_common.o libxlutil.so libxenlight_test.so
        $(CC) $(LDFLAGS) -o $@ $^ $(filter-out %libxenlight.so, $(LDLIBS_libxenlight)) $(LDLIBS_libxenctrl) -lyajl $(APPEND_LDFLAGS)
 
@@ -239,6 +246,7 @@ install: all
        $(INSTALL_DIR) $(DESTDIR)$(BASH_COMPLETION_DIR)
        $(INSTALL_DIR) $(DESTDIR)$(PRIVATE_BINDIR)
        $(INSTALL_PROG) xl $(DESTDIR)$(SBINDIR)
+       $(INSTALL_PROG) xen-init-dom0 $(DESTDIR)$(PRIVATE_BINDIR)
        $(INSTALL_PROG) libxl-save-helper $(DESTDIR)$(PRIVATE_BINDIR)
        $(INSTALL_SHLIB) libxenlight.so.$(MAJOR).$(MINOR) $(DESTDIR)$(LIBDIR)
        $(SYMLINK_SHLIB) libxenlight.so.$(MAJOR).$(MINOR) $(DESTDIR)$(LIBDIR)/libxenlight.so.$(MAJOR)
diff --git a/tools/libxl/xen-init-dom0.c b/tools/libxl/xen-init-dom0.c
new file mode 100644 (file)
index 0000000..7925d64
--- /dev/null
@@ -0,0 +1,120 @@
+#include <err.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <stdio.h>
+
+#include <xenctrl.h>
+#include <xenstore.h>
+#include <libxl.h>
+
+#define DOMNAME_PATH   "/local/domain/0/name"
+#define DOMID_PATH     "/local/domain/0/domid"
+
+static libxl_ctx *ctx;
+static xentoollog_logger_stdiostream *logger;
+static struct xs_handle *xsh;
+
+static void ctx_alloc(void)
+{
+    if (libxl_ctx_alloc(&ctx, LIBXL_VERSION, 0,
+                        (xentoollog_logger *)logger)) {
+        fprintf(stderr, "cannot init libxl context\n");
+        exit(1);
+    }
+    xsh = xs_open(0);
+    if (!xsh) {
+        fprintf(stderr, "cannot open xenstore connection\n");
+        exit(1);
+    }
+}
+
+static void ctx_free(void)
+{
+    if (ctx) {
+        libxl_ctx_free(ctx);
+        ctx = NULL;
+    }
+    if (logger) {
+        xtl_logger_destroy((xentoollog_logger *)logger);
+        logger = NULL;
+    }
+    if (xsh) {
+        xs_close(xsh);
+        xsh = NULL;
+    }
+}
+
+int main(int argc, char **argv)
+{
+    int rc;
+    libxl_domain_config dom0_config;
+    char *domname_string = NULL, *domid_string = NULL;
+    char *json = NULL;;
+
+    logger = xtl_createlogger_stdiostream(stderr, XTL_ERROR, 0);
+    if (!logger) exit(1);
+
+    atexit(ctx_free);
+
+    ctx_alloc();
+
+    libxl_domain_config_init(&dom0_config);
+
+    /* Sanity check: this program can only be run once. */
+    domid_string = xs_read(xsh, XBT_NULL, DOMID_PATH, NULL);
+    domname_string = xs_read(xsh, XBT_NULL, DOMNAME_PATH, NULL);
+    if (domid_string && domname_string) {
+        fprintf(stderr, "Dom0 is already set up\n");
+        rc = 0;
+        goto out;
+    }
+
+    /* Generate stub JSON config. */
+    dom0_config.c_info.type = LIBXL_DOMAIN_TYPE_PV;
+    libxl_domain_build_info_init_type(&dom0_config.b_info,
+                                      LIBXL_DOMAIN_TYPE_PV);
+
+    json = libxl_domain_config_to_json(ctx, &dom0_config);
+    /* libxl-json format requires the string ends with '\0'. Code
+     * snippet taken from libxl.
+     */
+    rc = libxl_userdata_store(ctx, 0, "libxl-json",
+                              (const uint8_t *)json,
+                              strlen(json) + 1 /* include '\0' */);
+    if (rc) {
+        fprintf(stderr, "cannot store stub json config for Dom0\n");
+        goto out;
+    }
+
+    /* Write xenstore entries. */
+    if (!xs_write(xsh, XBT_NULL, DOMID_PATH, "0", strlen("0"))) {
+        fprintf(stderr, "cannot set domid for Dom0\n");
+        rc = 1;
+        goto out;
+    }
+
+    if (!xs_write(xsh, XBT_NULL, DOMNAME_PATH, "Domain-0",
+                  strlen("Domain-0"))) {
+        fprintf(stderr, "cannot set domain name for Dom0\n");
+        rc = 1;
+        goto out;
+    }
+
+    printf("Done setting up Dom0\n");
+
+out:
+    libxl_domain_config_dispose(&dom0_config);
+    free(domid_string);
+    free(domname_string);
+    free(json);
+    return rc;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */