xen_domainbuild.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #include <signal.h>
  2. #include "xen_backend.h"
  3. #include "xen_domainbuild.h"
  4. #include "qemu/timer.h"
  5. #include "qemu/log.h"
  6. #include <xenguest.h>
  7. static int xenstore_domain_mkdir(char *path)
  8. {
  9. struct xs_permissions perms_ro[] = {{
  10. .id = 0, /* set owner: dom0 */
  11. },{
  12. .id = xen_domid,
  13. .perms = XS_PERM_READ,
  14. }};
  15. struct xs_permissions perms_rw[] = {{
  16. .id = 0, /* set owner: dom0 */
  17. },{
  18. .id = xen_domid,
  19. .perms = XS_PERM_READ | XS_PERM_WRITE,
  20. }};
  21. const char *writable[] = { "device", "control", "error", NULL };
  22. char subpath[256];
  23. int i;
  24. if (!xs_mkdir(xenstore, 0, path)) {
  25. fprintf(stderr, "%s: xs_mkdir %s: failed\n", __FUNCTION__, path);
  26. return -1;
  27. }
  28. if (!xs_set_permissions(xenstore, 0, path, perms_ro, 2)) {
  29. fprintf(stderr, "%s: xs_set_permissions failed\n", __FUNCTION__);
  30. return -1;
  31. }
  32. for (i = 0; writable[i]; i++) {
  33. snprintf(subpath, sizeof(subpath), "%s/%s", path, writable[i]);
  34. if (!xs_mkdir(xenstore, 0, subpath)) {
  35. fprintf(stderr, "%s: xs_mkdir %s: failed\n", __FUNCTION__, subpath);
  36. return -1;
  37. }
  38. if (!xs_set_permissions(xenstore, 0, subpath, perms_rw, 2)) {
  39. fprintf(stderr, "%s: xs_set_permissions failed\n", __FUNCTION__);
  40. return -1;
  41. }
  42. }
  43. return 0;
  44. }
  45. int xenstore_domain_init1(const char *kernel, const char *ramdisk,
  46. const char *cmdline)
  47. {
  48. char *dom, uuid_string[42], vm[256], path[256];
  49. int i;
  50. snprintf(uuid_string, sizeof(uuid_string), UUID_FMT,
  51. qemu_uuid[0], qemu_uuid[1], qemu_uuid[2], qemu_uuid[3],
  52. qemu_uuid[4], qemu_uuid[5], qemu_uuid[6], qemu_uuid[7],
  53. qemu_uuid[8], qemu_uuid[9], qemu_uuid[10], qemu_uuid[11],
  54. qemu_uuid[12], qemu_uuid[13], qemu_uuid[14], qemu_uuid[15]);
  55. dom = xs_get_domain_path(xenstore, xen_domid);
  56. snprintf(vm, sizeof(vm), "/vm/%s", uuid_string);
  57. xenstore_domain_mkdir(dom);
  58. xenstore_write_str(vm, "image/ostype", "linux");
  59. if (kernel)
  60. xenstore_write_str(vm, "image/kernel", kernel);
  61. if (ramdisk)
  62. xenstore_write_str(vm, "image/ramdisk", ramdisk);
  63. if (cmdline)
  64. xenstore_write_str(vm, "image/cmdline", cmdline);
  65. /* name + id */
  66. xenstore_write_str(vm, "name", qemu_name ? qemu_name : "no-name");
  67. xenstore_write_str(vm, "uuid", uuid_string);
  68. xenstore_write_str(dom, "name", qemu_name ? qemu_name : "no-name");
  69. xenstore_write_int(dom, "domid", xen_domid);
  70. xenstore_write_str(dom, "vm", vm);
  71. /* memory */
  72. xenstore_write_int(dom, "memory/target", ram_size >> 10); // kB
  73. xenstore_write_int(vm, "memory", ram_size >> 20); // MB
  74. xenstore_write_int(vm, "maxmem", ram_size >> 20); // MB
  75. /* cpus */
  76. for (i = 0; i < smp_cpus; i++) {
  77. snprintf(path, sizeof(path), "cpu/%d/availability",i);
  78. xenstore_write_str(dom, path, "online");
  79. }
  80. xenstore_write_int(vm, "vcpu_avail", smp_cpus);
  81. xenstore_write_int(vm, "vcpus", smp_cpus);
  82. /* vnc password */
  83. xenstore_write_str(vm, "vncpassword", "" /* FIXME */);
  84. free(dom);
  85. return 0;
  86. }
  87. int xenstore_domain_init2(int xenstore_port, int xenstore_mfn,
  88. int console_port, int console_mfn)
  89. {
  90. char *dom;
  91. dom = xs_get_domain_path(xenstore, xen_domid);
  92. /* signal new domain */
  93. xs_introduce_domain(xenstore,
  94. xen_domid,
  95. xenstore_mfn,
  96. xenstore_port);
  97. /* xenstore */
  98. xenstore_write_int(dom, "store/ring-ref", xenstore_mfn);
  99. xenstore_write_int(dom, "store/port", xenstore_port);
  100. /* console */
  101. xenstore_write_str(dom, "console/type", "ioemu");
  102. xenstore_write_int(dom, "console/limit", 128 * 1024);
  103. xenstore_write_int(dom, "console/ring-ref", console_mfn);
  104. xenstore_write_int(dom, "console/port", console_port);
  105. xen_config_dev_console(0);
  106. free(dom);
  107. return 0;
  108. }
  109. /* ------------------------------------------------------------- */
  110. static QEMUTimer *xen_poll;
  111. /* check domain state once per second */
  112. static void xen_domain_poll(void *opaque)
  113. {
  114. struct xc_dominfo info;
  115. int rc;
  116. rc = xc_domain_getinfo(xen_xc, xen_domid, 1, &info);
  117. if ((rc != 1) || (info.domid != xen_domid)) {
  118. qemu_log("xen: domain %d is gone\n", xen_domid);
  119. goto quit;
  120. }
  121. if (info.dying) {
  122. qemu_log("xen: domain %d is dying (%s%s)\n", xen_domid,
  123. info.crashed ? "crashed" : "",
  124. info.shutdown ? "shutdown" : "");
  125. goto quit;
  126. }
  127. qemu_mod_timer(xen_poll, qemu_get_clock_ms(rt_clock) + 1000);
  128. return;
  129. quit:
  130. qemu_system_shutdown_request();
  131. }
  132. static int xen_domain_watcher(void)
  133. {
  134. int qemu_running = 1;
  135. int fd[2], i, n, rc;
  136. char byte;
  137. if (pipe(fd) != 0) {
  138. qemu_log("%s: Huh? pipe error: %s\n", __FUNCTION__, strerror(errno));
  139. return -1;
  140. }
  141. if (fork() != 0)
  142. return 0; /* not child */
  143. /* close all file handles, except stdio/out/err,
  144. * our watch pipe and the xen interface handle */
  145. n = getdtablesize();
  146. for (i = 3; i < n; i++) {
  147. if (i == fd[0])
  148. continue;
  149. if (i == xc_fd(xen_xc)) {
  150. continue;
  151. }
  152. close(i);
  153. }
  154. /* ignore term signals */
  155. signal(SIGINT, SIG_IGN);
  156. signal(SIGTERM, SIG_IGN);
  157. /* wait for qemu exiting */
  158. while (qemu_running) {
  159. rc = read(fd[0], &byte, 1);
  160. switch (rc) {
  161. case -1:
  162. if (errno == EINTR)
  163. continue;
  164. qemu_log("%s: Huh? read error: %s\n", __FUNCTION__, strerror(errno));
  165. qemu_running = 0;
  166. break;
  167. case 0:
  168. /* EOF -> qemu exited */
  169. qemu_running = 0;
  170. break;
  171. default:
  172. qemu_log("%s: Huh? data on the watch pipe?\n", __FUNCTION__);
  173. break;
  174. }
  175. }
  176. /* cleanup */
  177. qemu_log("%s: destroy domain %d\n", __FUNCTION__, xen_domid);
  178. xc_domain_destroy(xen_xc, xen_domid);
  179. _exit(0);
  180. }
  181. /* normal cleanup */
  182. static void xen_domain_cleanup(void)
  183. {
  184. char *dom;
  185. dom = xs_get_domain_path(xenstore, xen_domid);
  186. if (dom) {
  187. xs_rm(xenstore, 0, dom);
  188. free(dom);
  189. }
  190. xs_release_domain(xenstore, xen_domid);
  191. }
  192. int xen_domain_build_pv(const char *kernel, const char *ramdisk,
  193. const char *cmdline)
  194. {
  195. uint32_t ssidref = 0;
  196. uint32_t flags = 0;
  197. xen_domain_handle_t uuid;
  198. unsigned int xenstore_port = 0, console_port = 0;
  199. unsigned long xenstore_mfn = 0, console_mfn = 0;
  200. int rc;
  201. memcpy(uuid, qemu_uuid, sizeof(uuid));
  202. rc = xc_domain_create(xen_xc, ssidref, uuid, flags, &xen_domid);
  203. if (rc < 0) {
  204. fprintf(stderr, "xen: xc_domain_create() failed\n");
  205. goto err;
  206. }
  207. qemu_log("xen: created domain %d\n", xen_domid);
  208. atexit(xen_domain_cleanup);
  209. if (xen_domain_watcher() == -1) {
  210. goto err;
  211. }
  212. xenstore_domain_init1(kernel, ramdisk, cmdline);
  213. rc = xc_domain_max_vcpus(xen_xc, xen_domid, smp_cpus);
  214. if (rc < 0) {
  215. fprintf(stderr, "xen: xc_domain_max_vcpus() failed\n");
  216. goto err;
  217. }
  218. #if 0
  219. rc = xc_domain_setcpuweight(xen_xc, xen_domid, 256);
  220. if (rc < 0) {
  221. fprintf(stderr, "xen: xc_domain_setcpuweight() failed\n");
  222. goto err;
  223. }
  224. #endif
  225. rc = xc_domain_setmaxmem(xen_xc, xen_domid, ram_size >> 10);
  226. if (rc < 0) {
  227. fprintf(stderr, "xen: xc_domain_setmaxmem() failed\n");
  228. goto err;
  229. }
  230. xenstore_port = xc_evtchn_alloc_unbound(xen_xc, xen_domid, 0);
  231. console_port = xc_evtchn_alloc_unbound(xen_xc, xen_domid, 0);
  232. rc = xc_linux_build(xen_xc, xen_domid, ram_size >> 20,
  233. kernel, ramdisk, cmdline,
  234. 0, flags,
  235. xenstore_port, &xenstore_mfn,
  236. console_port, &console_mfn);
  237. if (rc < 0) {
  238. fprintf(stderr, "xen: xc_linux_build() failed\n");
  239. goto err;
  240. }
  241. xenstore_domain_init2(xenstore_port, xenstore_mfn,
  242. console_port, console_mfn);
  243. qemu_log("xen: unpausing domain %d\n", xen_domid);
  244. rc = xc_domain_unpause(xen_xc, xen_domid);
  245. if (rc < 0) {
  246. fprintf(stderr, "xen: xc_domain_unpause() failed\n");
  247. goto err;
  248. }
  249. xen_poll = qemu_new_timer_ms(rt_clock, xen_domain_poll, NULL);
  250. qemu_mod_timer(xen_poll, qemu_get_clock_ms(rt_clock) + 1000);
  251. return 0;
  252. err:
  253. return -1;
  254. }