1 #include <stdlib.h>
2 #include <stdint.h>
3 #include <string.h>
4 #include <stdio.h>
5 
6 #include <xenctrl.h>
7 #include <xenstore.h>
8 #include <libxl.h>
9 
10 #include "init-dom-json.h"
11 
12 #define DOMNAME_PATH   "/local/domain/0/name"
13 #define DOMID_PATH     "/local/domain/0/domid"
14 
clear_domid_history(void)15 static int clear_domid_history(void)
16 {
17     int rc = 1;
18     xentoollog_logger_stdiostream *logger;
19     libxl_ctx *ctx;
20 
21     logger = xtl_createlogger_stdiostream(stderr, XTL_ERROR, 0);
22     if (!logger)
23         return 1;
24 
25     if (libxl_ctx_alloc(&ctx, LIBXL_VERSION, 0,
26                         (xentoollog_logger *)logger)) {
27         fprintf(stderr, "cannot init libxl context\n");
28         goto outlog;
29     }
30 
31     if (!libxl_clear_domid_history(ctx))
32         rc = 0;
33 
34     libxl_ctx_free(ctx);
35 
36 outlog:
37     xtl_logger_destroy((xentoollog_logger *)logger);
38     return rc;
39 }
40 
main(int argc,char ** argv)41 int main(int argc, char **argv)
42 {
43     int rc;
44     struct xs_handle *xsh = NULL;
45     xc_interface *xch = NULL;
46     char *domname_string = NULL, *domid_string = NULL;
47     libxl_uuid uuid;
48 
49     /* Accept 0 or 1 argument */
50     if (argc > 2) {
51         fprintf(stderr, "too many arguments\n");
52         rc = 1;
53         goto out;
54     }
55 
56     xsh = xs_open(0);
57     if (!xsh) {
58         perror("cannot open xenstore connection");
59         rc = 1;
60         goto out;
61     }
62 
63     xch = xc_interface_open(NULL, NULL, 0);
64     if (!xch) {
65         perror("xc_interface_open() failed");
66         rc = 1;
67         goto out;
68     }
69 
70     /* Sanity check: this program can only be run once. */
71     domid_string = xs_read(xsh, XBT_NULL, DOMID_PATH, NULL);
72     domname_string = xs_read(xsh, XBT_NULL, DOMNAME_PATH, NULL);
73     if (domid_string && domname_string) {
74         fprintf(stderr, "Dom0 is already set up\n");
75         rc = 0;
76         goto out;
77     }
78 
79     libxl_uuid_clear(&uuid);
80 
81     /* If UUID is supplied, parse it. */
82     if (argc == 2 && libxl_uuid_from_string(&uuid, argv[1])) {
83         fprintf(stderr, "failed to parse UUID %s\n", argv[1]);
84         rc = 1;
85         goto out;
86     }
87 
88     if (!libxl_uuid_is_nil(&uuid) &&
89         xc_domain_sethandle(xch, 0, libxl_uuid_bytearray(&uuid))) {
90         perror("failed to set Dom0 UUID");
91         rc = 1;
92         goto out;
93     }
94 
95     rc = gen_stub_json_config(0, &uuid);
96     if (rc)
97         goto out;
98 
99     rc = clear_domid_history();
100     if (rc)
101         goto out;
102 
103     /* Write xenstore entries. */
104     if (!xs_write(xsh, XBT_NULL, DOMID_PATH, "0", strlen("0"))) {
105         fprintf(stderr, "cannot set domid for Dom0\n");
106         rc = 1;
107         goto out;
108     }
109 
110     if (!xs_write(xsh, XBT_NULL, DOMNAME_PATH, "Domain-0",
111                   strlen("Domain-0"))) {
112         fprintf(stderr, "cannot set domain name for Dom0\n");
113         rc = 1;
114         goto out;
115     }
116 
117     printf("Done setting up Dom0\n");
118 
119 out:
120     free(domid_string);
121     free(domname_string);
122     xs_close(xsh);
123     xc_interface_close(xch);
124     return rc;
125 }
126 
127 /*
128  * Local variables:
129  * mode: C
130  * c-basic-offset: 4
131  * indent-tabs-mode: nil
132  * End:
133  */
134