[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-devel] [PATCH v3 08/15] libxl: introduce helper to initialise Dom0
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@xxxxxxxxxx> --- 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 _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxx http://lists.xen.org/xen-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |