xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Wei Liu <wei.liu2@citrix.com>
To: xen-devel@lists.xen.org
Cc: Wei Liu <wei.liu2@citrix.com>,
	ian.jackson@eu.citrix.com, ian.campbell@citrix.com
Subject: [PATCH v3 08/15] libxl: introduce helper to initialise Dom0
Date: Thu, 4 Sep 2014 23:43:14 +0100	[thread overview]
Message-ID: <1409870601-7538-9-git-send-email-wei.liu2@citrix.com> (raw)
In-Reply-To: <1409870601-7538-1-git-send-email-wei.liu2@citrix.com>

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>

---
change in v3:
move to libxl directory
---
 .gitignore                                  |    1 +
 tools/hotplug/Linux/init.d/xencommons.in.in |    5 +-
 tools/libxl/Makefile                        |   10 ++-
 tools/libxl/xen-init-dom0.c                 |  120 +++++++++++++++++++++++++++
 4 files changed, 132 insertions(+), 4 deletions(-)
 create mode 100644 tools/libxl/xen-init-dom0.c

diff --git a/.gitignore b/.gitignore
index 6d725aa..8e34b85 100644
--- a/.gitignore
+++ b/.gitignore
@@ -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
diff --git a/tools/hotplug/Linux/init.d/xencommons.in.in b/tools/hotplug/Linux/init.d/xencommons.in.in
index b311bb8..1d860d9 100644
--- a/tools/hotplug/Linux/init.d/xencommons.in.in
+++ b/tools/hotplug/Linux/init.d/xencommons.in.in
@@ -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...
diff --git a/tools/libxl/Makefile b/tools/libxl/Makefile
index bd0db3b..4bee4af 100644
--- a/tools/libxl/Makefile
+++ b/tools/libxl/Makefile
@@ -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
index 0000000..7925d64
--- /dev/null
+++ b/tools/libxl/xen-init-dom0.c
@@ -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:
+ */
-- 
1.7.10.4

  parent reply	other threads:[~2014-09-04 22:43 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-04 22:43 [PATCH v3 00/15] libxl: synchronise domain configuration Wei Liu
2014-09-04 22:43 ` [PATCH v3 01/15] libxl: make userdata_path libxl internal function Wei Liu
2014-09-04 22:43 ` [PATCH v3 02/15] libxl: functions to lock / unlock libxl userdata store Wei Liu
2014-09-04 22:43 ` [PATCH v3 03/15] libxl: properly lock " Wei Liu
2014-09-09 10:52   ` Ian Campbell
2014-09-04 22:43 ` [PATCH v3 04/15] libxl: libxl-json format and internal functions to get / set it Wei Liu
2014-09-04 22:43 ` [PATCH v3 05/15] libxl: store a copy of configuration when creating domain Wei Liu
2014-09-04 22:43 ` [PATCH v3 06/15] libxl: introduce libxl__device_from_pcidev Wei Liu
2014-09-04 22:43 ` [PATCH v3 07/15] libxl: disallow attaching the same device more than once Wei Liu
2014-09-09 10:56   ` Ian Campbell
2014-09-04 22:43 ` Wei Liu [this message]
2014-09-05 13:22   ` [PATCH v3 08/15] libxl: introduce helper to initialise Dom0 Wei Liu
2014-09-09 11:03     ` Wei Liu
2014-09-09 11:16   ` Ian Campbell
2014-09-09 12:16     ` Ian Campbell
2014-09-04 22:43 ` [PATCH v3 09/15] libxl: synchronise configuration when we hotplug a device Wei Liu
2014-09-09 11:11   ` Ian Campbell
2014-09-09 11:23     ` Ian Campbell
2014-09-09 13:37     ` Wei Liu
2014-09-09 13:41       ` Ian Campbell
2014-09-04 22:43 ` [PATCH v3 10/15] libxl: make libxl_cd_insert "eject" + "insert" Wei Liu
2014-09-09 11:30   ` Ian Campbell
2014-09-09 13:38     ` Wei Liu
2014-09-15 14:38     ` Wei Liu
2014-09-04 22:43 ` [PATCH v3 11/15] libxl: refactor libxl_get_memory_target Wei Liu
2014-09-09 11:36   ` Ian Campbell
2014-09-09 11:39     ` Ian Campbell
2014-09-09 13:39       ` Wei Liu
2014-09-04 22:43 ` [PATCH v3 12/15] libxl: introduce libxl_retrieve_domain_configuration Wei Liu
2014-09-09 11:41   ` Ian Campbell
2014-09-04 22:43 ` [PATCH v3 13/15] libxl: introduce libxl_userdata_unlink Wei Liu
2014-09-09 11:42   ` Ian Campbell
2014-09-04 22:43 ` [PATCH v3 14/15] xl: use libxl_retrieve_domain_configuration and JSON format Wei Liu
2014-09-09 11:44   ` Ian Campbell
2014-09-04 22:43 ` [PATCH v3 15/15] xl: long output of "list" command now contains Dom0 information Wei Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1409870601-7538-9-git-send-email-wei.liu2@citrix.com \
    --to=wei.liu2@citrix.com \
    --cc=ian.campbell@citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).