xen_domainbuild.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. return;
  132. }
  133. static int xen_domain_watcher(void)
  134. {
  135. int qemu_running = 1;
  136. int fd[2], i, n, rc;
  137. char byte;
  138. if (pipe(fd) != 0) {
  139. qemu_log("%s: Huh? pipe error: %s\n", __FUNCTION__, strerror(errno));
  140. return -1;
  141. }
  142. if (fork() != 0)
  143. return 0; /* not child */
  144. /* close all file handles, except stdio/out/err,
  145. * our watch pipe and the xen interface handle */
  146. n = getdtablesize();
  147. for (i = 3; i < n; i++) {
  148. if (i == fd[0])
  149. continue;
  150. if (i == xc_fd(xen_xc)) {
  151. continue;
  152. }
  153. close(i);
  154. }
  155. /* ignore term signals */
  156. signal(SIGINT, SIG_IGN);
  157. signal(SIGTERM, SIG_IGN);
  158. /* wait for qemu exiting */
  159. while (qemu_running) {
  160. rc = read(fd[0], &byte, 1);
  161. switch (rc) {
  162. case -1:
  163. if (errno == EINTR)
  164. continue;
  165. qemu_log("%s: Huh? read error: %s\n", __FUNCTION__, strerror(errno));
  166. qemu_running = 0;
  167. break;
  168. case 0:
  169. /* EOF -> qemu exited */
  170. qemu_running = 0;
  171. break;
  172. default:
  173. qemu_log("%s: Huh? data on the watch pipe?\n", __FUNCTION__);
  174. break;
  175. }
  176. }
  177. /* cleanup */
  178. qemu_log("%s: destroy domain %d\n", __FUNCTION__, xen_domid);
  179. xc_domain_destroy(xen_xc, xen_domid);
  180. _exit(0);
  181. }
  182. /* normal cleanup */
  183. static void xen_domain_cleanup(void)
  184. {
  185. char *dom;
  186. dom = xs_get_domain_path(xenstore, xen_domid);
  187. if (dom) {
  188. xs_rm(xenstore, 0, dom);
  189. free(dom);
  190. }
  191. xs_release_domain(xenstore, xen_domid);
  192. }
  193. int xen_domain_build_pv(const char *kernel, const char *ramdisk,
  194. const char *cmdline)
  195. {
  196. uint32_t ssidref = 0;
  197. uint32_t flags = 0;
  198. xen_domain_handle_t uuid;
  199. unsigned int xenstore_port = 0, console_port = 0;
  200. unsigned long xenstore_mfn = 0, console_mfn = 0;
  201. int rc;
  202. memcpy(uuid, qemu_uuid, sizeof(uuid));
  203. rc = xc_domain_create(xen_xc, ssidref, uuid, flags, &xen_domid);
  204. if (rc < 0) {
  205. fprintf(stderr, "xen: xc_domain_create() failed\n");
  206. goto err;
  207. }
  208. qemu_log("xen: created domain %d\n", xen_domid);
  209. atexit(xen_domain_cleanup);
  210. if (xen_domain_watcher() == -1) {
  211. goto err;
  212. }
  213. xenstore_domain_init1(kernel, ramdisk, cmdline);
  214. rc = xc_domain_max_vcpus(xen_xc, xen_domid, smp_cpus);
  215. if (rc < 0) {
  216. fprintf(stderr, "xen: xc_domain_max_vcpus() failed\n");
  217. goto err;
  218. }
  219. #if 0
  220. rc = xc_domain_setcpuweight(xen_xc, xen_domid, 256);
  221. if (rc < 0) {
  222. fprintf(stderr, "xen: xc_domain_setcpuweight() failed\n");
  223. goto err;
  224. }
  225. #endif
  226. rc = xc_domain_setmaxmem(xen_xc, xen_domid, ram_size >> 10);
  227. if (rc < 0) {
  228. fprintf(stderr, "xen: xc_domain_setmaxmem() failed\n");
  229. goto err;
  230. }
  231. xenstore_port = xc_evtchn_alloc_unbound(xen_xc, xen_domid, 0);
  232. console_port = xc_evtchn_alloc_unbound(xen_xc, xen_domid, 0);
  233. rc = xc_linux_build(xen_xc, xen_domid, ram_size >> 20,
  234. kernel, ramdisk, cmdline,
  235. 0, flags,
  236. xenstore_port, &xenstore_mfn,
  237. console_port, &console_mfn);
  238. if (rc < 0) {
  239. fprintf(stderr, "xen: xc_linux_build() failed\n");
  240. goto err;
  241. }
  242. xenstore_domain_init2(xenstore_port, xenstore_mfn,
  243. console_port, console_mfn);
  244. qemu_log("xen: unpausing domain %d\n", xen_domid);
  245. rc = xc_domain_unpause(xen_xc, xen_domid);
  246. if (rc < 0) {
  247. fprintf(stderr, "xen: xc_domain_unpause() failed\n");
  248. goto err;
  249. }
  250. xen_poll = qemu_new_timer_ms(rt_clock, xen_domain_poll, NULL);
  251. qemu_mod_timer(xen_poll, qemu_get_clock_ms(rt_clock) + 1000);
  252. return 0;
  253. err:
  254. return -1;
  255. }