xref: /kvmtool/builtin-run.c (revision 49a8afd1b9a4e503bdafb2bbc04549e03d514836)
1 #include "kvm/builtin-run.h"
2 
3 #include "kvm/builtin-setup.h"
4 #include "kvm/virtio-balloon.h"
5 #include "kvm/virtio-console.h"
6 #include "kvm/parse-options.h"
7 #include "kvm/8250-serial.h"
8 #include "kvm/framebuffer.h"
9 #include "kvm/disk-image.h"
10 #include "kvm/threadpool.h"
11 #include "kvm/virtio-scsi.h"
12 #include "kvm/virtio-blk.h"
13 #include "kvm/virtio-net.h"
14 #include "kvm/virtio-rng.h"
15 #include "kvm/ioeventfd.h"
16 #include "kvm/virtio-9p.h"
17 #include "kvm/barrier.h"
18 #include "kvm/kvm-cpu.h"
19 #include "kvm/ioport.h"
20 #include "kvm/symbol.h"
21 #include "kvm/i8042.h"
22 #include "kvm/mutex.h"
23 #include "kvm/term.h"
24 #include "kvm/util.h"
25 #include "kvm/strbuf.h"
26 #include "kvm/vesa.h"
27 #include "kvm/irq.h"
28 #include "kvm/kvm.h"
29 #include "kvm/pci.h"
30 #include "kvm/rtc.h"
31 #include "kvm/sdl.h"
32 #include "kvm/vnc.h"
33 #include "kvm/guest_compat.h"
34 #include "kvm/pci-shmem.h"
35 #include "kvm/kvm-ipc.h"
36 #include "kvm/builtin-debug.h"
37 
38 #include <linux/types.h>
39 #include <linux/err.h>
40 
41 #include <sys/utsname.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <termios.h>
45 #include <signal.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <ctype.h>
50 #include <stdio.h>
51 
52 #define MB_SHIFT		(20)
53 #define KB_SHIFT		(10)
54 #define GB_SHIFT		(30)
55 
56 struct kvm *kvm;
57 __thread struct kvm_cpu *current_kvm_cpu;
58 
59 static int  kvm_run_wrapper;
60 
61 bool do_debug_print = false;
62 
63 extern char _binary_guest_init_start;
64 extern char _binary_guest_init_size;
65 
66 static const char * const run_usage[] = {
67 	"lkvm run [<options>] [<kernel image>]",
68 	NULL
69 };
70 
71 enum {
72 	KVM_RUN_DEFAULT,
73 	KVM_RUN_SANDBOX,
74 };
75 
76 static int img_name_parser(const struct option *opt, const char *arg, int unset)
77 {
78 	char path[PATH_MAX];
79 	struct stat st;
80 
81 	snprintf(path, PATH_MAX, "%s%s", kvm__get_dir(), arg);
82 
83 	if ((stat(arg, &st) == 0 && S_ISDIR(st.st_mode)) ||
84 	   (stat(path, &st) == 0 && S_ISDIR(st.st_mode)))
85 		return virtio_9p_img_name_parser(opt, arg, unset);
86 	return disk_img_name_parser(opt, arg, unset);
87 }
88 
89 void kvm_run_set_wrapper_sandbox(void)
90 {
91 	kvm_run_wrapper = KVM_RUN_SANDBOX;
92 }
93 
94 #define BUILD_OPTIONS(name, cfg, kvm)					\
95 	struct option name[] = {					\
96 	OPT_GROUP("Basic options:"),					\
97 	OPT_STRING('\0', "name", &(cfg)->guest_name, "guest name",	\
98 			"A name for the guest"),			\
99 	OPT_INTEGER('c', "cpus", &(cfg)->nrcpus, "Number of CPUs"),	\
100 	OPT_U64('m', "mem", &(cfg)->ram_size, "Virtual machine memory size\
101 		in MiB."),						\
102 	OPT_CALLBACK('\0', "shmem", NULL,				\
103 		     "[pci:]<addr>:<size>[:handle=<handle>][:create]",	\
104 		     "Share host shmem with guest via pci device",	\
105 		     shmem_parser, NULL),				\
106 	OPT_CALLBACK('d', "disk", kvm, "image or rootfs_dir", "Disk 	\
107 			image or rootfs directory", img_name_parser,	\
108 			kvm),						\
109 	OPT_BOOLEAN('\0', "balloon", &(cfg)->balloon, "Enable virtio	\
110 			balloon"),					\
111 	OPT_BOOLEAN('\0', "vnc", &(cfg)->vnc, "Enable VNC framebuffer"),\
112 	OPT_BOOLEAN('\0', "sdl", &(cfg)->sdl, "Enable SDL framebuffer"),\
113 	OPT_BOOLEAN('\0', "rng", &(cfg)->virtio_rng, "Enable virtio Random\
114 			Number Generator"),				\
115 	OPT_CALLBACK('\0', "9p", NULL, "dir_to_share,tag_name",		\
116 		     "Enable virtio 9p to share files between host and	\
117 		     guest", virtio_9p_rootdir_parser, kvm),		\
118 	OPT_STRING('\0', "console", &(cfg)->console, "serial, virtio or	\
119 			hv", "Console to use"),				\
120 	OPT_STRING('\0', "dev", &(cfg)->dev, "device_file",		\
121 			"KVM device file"),				\
122 	OPT_CALLBACK('\0', "tty", NULL, "tty id",			\
123 		     "Remap guest TTY into a pty on the host",		\
124 		     tty_parser, NULL),					\
125 	OPT_STRING('\0', "sandbox", &(cfg)->sandbox, "script",		\
126 			"Run this script when booting into custom	\
127 			rootfs"),					\
128 	OPT_STRING('\0', "hugetlbfs", &(cfg)->hugetlbfs_path, "path",	\
129 			"Hugetlbfs path"),				\
130 									\
131 	OPT_GROUP("Kernel options:"),					\
132 	OPT_STRING('k', "kernel", &(cfg)->kernel_filename, "kernel",	\
133 			"Kernel to boot in virtual machine"),		\
134 	OPT_STRING('i', "initrd", &(cfg)->initrd_filename, "initrd",	\
135 			"Initial RAM disk image"),			\
136 	OPT_STRING('p', "params", &(cfg)->kernel_cmdline, "params",	\
137 			"Kernel command line arguments"),		\
138 	OPT_STRING('f', "firmware", &(cfg)->firmware_filename, "firmware",\
139 			"Firmware image to boot in virtual machine"),	\
140 									\
141 	OPT_GROUP("Networking options:"),				\
142 	OPT_CALLBACK_DEFAULT('n', "network", NULL, "network params",	\
143 		     "Create a new guest NIC",				\
144 		     netdev_parser, NULL, kvm),				\
145 	OPT_BOOLEAN('\0', "no-dhcp", &(cfg)->no_dhcp, "Disable kernel DHCP\
146 			in rootfs mode"),				\
147 									\
148 	OPT_GROUP("BIOS options:"),					\
149 	OPT_INTEGER('\0', "vidmode", &(cfg)->vidmode,			\
150 		    "Video mode"),					\
151 									\
152 	OPT_GROUP("Debug options:"),					\
153 	OPT_BOOLEAN('\0', "debug", &do_debug_print,			\
154 			"Enable debug messages"),			\
155 	OPT_BOOLEAN('\0', "debug-single-step", &(cfg)->single_step,	\
156 			"Enable single stepping"),			\
157 	OPT_BOOLEAN('\0', "debug-ioport", &(cfg)->ioport_debug,		\
158 			"Enable ioport debugging"),			\
159 	OPT_BOOLEAN('\0', "debug-mmio", &(cfg)->mmio_debug,		\
160 			"Enable MMIO debugging"),			\
161 	OPT_INTEGER('\0', "debug-iodelay", &(cfg)->debug_iodelay,	\
162 			"Delay IO by millisecond"),			\
163 	OPT_END()							\
164 	};
165 
166 static void handle_sigalrm(int sig)
167 {
168 	kvm__arch_periodic_poll(kvm);
169 }
170 
171 static void *kvm_cpu_thread(void *arg)
172 {
173 	current_kvm_cpu		= arg;
174 
175 	if (kvm_cpu__start(current_kvm_cpu))
176 		goto panic_kvm;
177 
178 	return (void *) (intptr_t) 0;
179 
180 panic_kvm:
181 	fprintf(stderr, "KVM exit reason: %u (\"%s\")\n",
182 		current_kvm_cpu->kvm_run->exit_reason,
183 		kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]);
184 	if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN)
185 		fprintf(stderr, "KVM exit code: 0x%Lu\n",
186 			current_kvm_cpu->kvm_run->hw.hardware_exit_reason);
187 
188 	kvm_cpu__set_debug_fd(STDOUT_FILENO);
189 	kvm_cpu__show_registers(current_kvm_cpu);
190 	kvm_cpu__show_code(current_kvm_cpu);
191 	kvm_cpu__show_page_tables(current_kvm_cpu);
192 
193 	return (void *) (intptr_t) 1;
194 }
195 
196 static char kernel[PATH_MAX];
197 
198 static const char *host_kernels[] = {
199 	"/boot/vmlinuz",
200 	"/boot/bzImage",
201 	NULL
202 };
203 
204 static const char *default_kernels[] = {
205 	"./bzImage",
206 	"arch/" BUILD_ARCH "/boot/bzImage",
207 	"../../arch/" BUILD_ARCH "/boot/bzImage",
208 	NULL
209 };
210 
211 static const char *default_vmlinux[] = {
212 	"vmlinux",
213 	"../../../vmlinux",
214 	"../../vmlinux",
215 	NULL
216 };
217 
218 static void kernel_usage_with_options(void)
219 {
220 	const char **k;
221 	struct utsname uts;
222 
223 	fprintf(stderr, "Fatal: could not find default kernel image in:\n");
224 	k = &default_kernels[0];
225 	while (*k) {
226 		fprintf(stderr, "\t%s\n", *k);
227 		k++;
228 	}
229 
230 	if (uname(&uts) < 0)
231 		return;
232 
233 	k = &host_kernels[0];
234 	while (*k) {
235 		if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
236 			return;
237 		fprintf(stderr, "\t%s\n", kernel);
238 		k++;
239 	}
240 	fprintf(stderr, "\nPlease see '%s run --help' for more options.\n\n",
241 		KVM_BINARY_NAME);
242 }
243 
244 static u64 host_ram_size(void)
245 {
246 	long page_size;
247 	long nr_pages;
248 
249 	nr_pages	= sysconf(_SC_PHYS_PAGES);
250 	if (nr_pages < 0) {
251 		pr_warning("sysconf(_SC_PHYS_PAGES) failed");
252 		return 0;
253 	}
254 
255 	page_size	= sysconf(_SC_PAGE_SIZE);
256 	if (page_size < 0) {
257 		pr_warning("sysconf(_SC_PAGE_SIZE) failed");
258 		return 0;
259 	}
260 
261 	return (nr_pages * page_size) >> MB_SHIFT;
262 }
263 
264 /*
265  * If user didn't specify how much memory it wants to allocate for the guest,
266  * avoid filling the whole host RAM.
267  */
268 #define RAM_SIZE_RATIO		0.8
269 
270 static u64 get_ram_size(int nr_cpus)
271 {
272 	u64 available;
273 	u64 ram_size;
274 
275 	ram_size	= 64 * (nr_cpus + 3);
276 
277 	available	= host_ram_size() * RAM_SIZE_RATIO;
278 	if (!available)
279 		available = MIN_RAM_SIZE_MB;
280 
281 	if (ram_size > available)
282 		ram_size	= available;
283 
284 	return ram_size;
285 }
286 
287 static const char *find_kernel(void)
288 {
289 	const char **k;
290 	struct stat st;
291 	struct utsname uts;
292 
293 	k = &default_kernels[0];
294 	while (*k) {
295 		if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) {
296 			k++;
297 			continue;
298 		}
299 		strncpy(kernel, *k, PATH_MAX);
300 		return kernel;
301 	}
302 
303 	if (uname(&uts) < 0)
304 		return NULL;
305 
306 	k = &host_kernels[0];
307 	while (*k) {
308 		if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
309 			return NULL;
310 
311 		if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) {
312 			k++;
313 			continue;
314 		}
315 		return kernel;
316 
317 	}
318 	return NULL;
319 }
320 
321 static const char *find_vmlinux(void)
322 {
323 	const char **vmlinux;
324 
325 	vmlinux = &default_vmlinux[0];
326 	while (*vmlinux) {
327 		struct stat st;
328 
329 		if (stat(*vmlinux, &st) < 0 || !S_ISREG(st.st_mode)) {
330 			vmlinux++;
331 			continue;
332 		}
333 		return *vmlinux;
334 	}
335 	return NULL;
336 }
337 
338 void kvm_run_help(void)
339 {
340 	BUILD_OPTIONS(options, &kvm->cfg, kvm);
341 	usage_with_options(run_usage, options);
342 }
343 
344 static int kvm_setup_guest_init(void)
345 {
346 	const char *rootfs = kvm->cfg.custom_rootfs_name;
347 	char tmp[PATH_MAX];
348 	size_t size;
349 	int fd, ret;
350 	char *data;
351 
352 	/* Setup /virt/init */
353 	size = (size_t)&_binary_guest_init_size;
354 	data = (char *)&_binary_guest_init_start;
355 	snprintf(tmp, PATH_MAX, "%s%s/virt/init", kvm__get_dir(), rootfs);
356 	remove(tmp);
357 	fd = open(tmp, O_CREAT | O_WRONLY, 0755);
358 	if (fd < 0)
359 		die("Fail to setup %s", tmp);
360 	ret = xwrite(fd, data, size);
361 	if (ret < 0)
362 		die("Fail to setup %s", tmp);
363 	close(fd);
364 
365 	return 0;
366 }
367 
368 static int kvm_run_set_sandbox(void)
369 {
370 	const char *guestfs_name = kvm->cfg.custom_rootfs_name;
371 	char path[PATH_MAX], script[PATH_MAX], *tmp;
372 
373 	snprintf(path, PATH_MAX, "%s%s/virt/sandbox.sh", kvm__get_dir(), guestfs_name);
374 
375 	remove(path);
376 
377 	if (kvm->cfg.sandbox == NULL)
378 		return 0;
379 
380 	tmp = realpath(kvm->cfg.sandbox, NULL);
381 	if (tmp == NULL)
382 		return -ENOMEM;
383 
384 	snprintf(script, PATH_MAX, "/host/%s", tmp);
385 	free(tmp);
386 
387 	return symlink(script, path);
388 }
389 
390 static void kvm_write_sandbox_cmd_exactly(int fd, const char *arg)
391 {
392 	const char *single_quote;
393 
394 	if (!*arg) { /* zero length string */
395 		if (write(fd, "''", 2) <= 0)
396 			die("Failed writing sandbox script");
397 		return;
398 	}
399 
400 	while (*arg) {
401 		single_quote = strchrnul(arg, '\'');
402 
403 		/* write non-single-quote string as #('string') */
404 		if (arg != single_quote) {
405 			if (write(fd, "'", 1) <= 0 ||
406 			    write(fd, arg, single_quote - arg) <= 0 ||
407 			    write(fd, "'", 1) <= 0)
408 				die("Failed writing sandbox script");
409 		}
410 
411 		/* write single quote as #("'") */
412 		if (*single_quote) {
413 			if (write(fd, "\"'\"", 3) <= 0)
414 				die("Failed writing sandbox script");
415 		} else
416 			break;
417 
418 		arg = single_quote + 1;
419 	}
420 }
421 
422 static void resolve_program(const char *src, char *dst, size_t len)
423 {
424 	struct stat st;
425 	int err;
426 
427 	err = stat(src, &st);
428 
429 	if (!err && S_ISREG(st.st_mode)) {
430 		char resolved_path[PATH_MAX];
431 
432 		if (!realpath(src, resolved_path))
433 			die("Unable to resolve program %s: %s\n", src, strerror(errno));
434 
435 		snprintf(dst, len, "/host%s", resolved_path);
436 	} else
437 		strncpy(dst, src, len);
438 }
439 
440 static void kvm_run_write_sandbox_cmd(const char **argv, int argc)
441 {
442 	const char script_hdr[] = "#! /bin/bash\n\n";
443 	char program[PATH_MAX];
444 	int fd;
445 
446 	remove(kvm->cfg.sandbox);
447 
448 	fd = open(kvm->cfg.sandbox, O_RDWR | O_CREAT, 0777);
449 	if (fd < 0)
450 		die("Failed creating sandbox script");
451 
452 	if (write(fd, script_hdr, sizeof(script_hdr) - 1) <= 0)
453 		die("Failed writing sandbox script");
454 
455 	resolve_program(argv[0], program, PATH_MAX);
456 	kvm_write_sandbox_cmd_exactly(fd, program);
457 
458 	argv++;
459 	argc--;
460 
461 	while (argc) {
462 		if (write(fd, " ", 1) <= 0)
463 			die("Failed writing sandbox script");
464 
465 		kvm_write_sandbox_cmd_exactly(fd, argv[0]);
466 		argv++;
467 		argc--;
468 	}
469 	if (write(fd, "\n", 1) <= 0)
470 		die("Failed writing sandbox script");
471 
472 	close(fd);
473 }
474 
475 static int kvm_cmd_run_init(int argc, const char **argv)
476 {
477 	static char real_cmdline[2048], default_name[20];
478 	unsigned int nr_online_cpus;
479 
480 	kvm = kvm__new();
481 	if (IS_ERR(kvm))
482 		return PTR_ERR(kvm);
483 
484 	signal(SIGALRM, handle_sigalrm);
485 
486 	nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
487 	kvm->cfg.custom_rootfs_name = "default";
488 
489 	while (argc != 0) {
490 		BUILD_OPTIONS(options, &kvm->cfg, kvm);
491 		argc = parse_options(argc, argv, options, run_usage,
492 				PARSE_OPT_STOP_AT_NON_OPTION |
493 				PARSE_OPT_KEEP_DASHDASH);
494 		if (argc != 0) {
495 			/* Cusrom options, should have been handled elsewhere */
496 			if (strcmp(argv[0], "--") == 0) {
497 				if (kvm_run_wrapper == KVM_RUN_SANDBOX) {
498 					kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME;
499 					kvm_run_write_sandbox_cmd(argv+1, argc-1);
500 					break;
501 				}
502 			}
503 
504 			if ((kvm_run_wrapper == KVM_RUN_DEFAULT && kvm->cfg.kernel_filename) ||
505 				(kvm_run_wrapper == KVM_RUN_SANDBOX && kvm->cfg.sandbox)) {
506 				fprintf(stderr, "Cannot handle parameter: "
507 						"%s\n", argv[0]);
508 				usage_with_options(run_usage, options);
509 				free(kvm);
510 				return -EINVAL;
511 			}
512 			if (kvm_run_wrapper == KVM_RUN_SANDBOX) {
513 				/*
514 				 * first unhandled parameter is treated as
515 				 * sandbox command
516 				 */
517 				kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME;
518 				kvm_run_write_sandbox_cmd(argv, argc);
519 			} else {
520 				/*
521 				 * first unhandled parameter is treated as a kernel
522 				 * image
523 				 */
524 				kvm->cfg.kernel_filename = argv[0];
525 			}
526 			argv++;
527 			argc--;
528 		}
529 
530 	}
531 
532 	kvm->nr_disks = kvm->cfg.image_count;
533 
534 	if (!kvm->cfg.kernel_filename)
535 		kvm->cfg.kernel_filename = find_kernel();
536 
537 	if (!kvm->cfg.kernel_filename) {
538 		kernel_usage_with_options();
539 		return -EINVAL;
540 	}
541 
542 	kvm->cfg.vmlinux_filename = find_vmlinux();
543 	kvm->vmlinux = kvm->cfg.vmlinux_filename;
544 
545 	if (kvm->cfg.nrcpus == 0)
546 		kvm->cfg.nrcpus = nr_online_cpus;
547 
548 	if (!kvm->cfg.ram_size)
549 		kvm->cfg.ram_size = get_ram_size(kvm->cfg.nrcpus);
550 
551 	if (kvm->cfg.ram_size < MIN_RAM_SIZE_MB)
552 		die("Not enough memory specified: %lluMB (min %lluMB)", kvm->cfg.ram_size, MIN_RAM_SIZE_MB);
553 
554 	if (kvm->cfg.ram_size > host_ram_size())
555 		pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", kvm->cfg.ram_size, host_ram_size());
556 
557 	kvm->cfg.ram_size <<= MB_SHIFT;
558 
559 	if (!kvm->cfg.dev)
560 		kvm->cfg.dev = DEFAULT_KVM_DEV;
561 
562 	if (!kvm->cfg.console)
563 		kvm->cfg.console = DEFAULT_CONSOLE;
564 
565 	if (!strncmp(kvm->cfg.console, "virtio", 6))
566 		kvm->cfg.active_console  = CONSOLE_VIRTIO;
567 	else if (!strncmp(kvm->cfg.console, "serial", 6))
568 		kvm->cfg.active_console  = CONSOLE_8250;
569 	else if (!strncmp(kvm->cfg.console, "hv", 2))
570 		kvm->cfg.active_console = CONSOLE_HV;
571 	else
572 		pr_warning("No console!");
573 
574 	if (!kvm->cfg.host_ip)
575 		kvm->cfg.host_ip = DEFAULT_HOST_ADDR;
576 
577 	if (!kvm->cfg.guest_ip)
578 		kvm->cfg.guest_ip = DEFAULT_GUEST_ADDR;
579 
580 	if (!kvm->cfg.guest_mac)
581 		kvm->cfg.guest_mac = DEFAULT_GUEST_MAC;
582 
583 	if (!kvm->cfg.host_mac)
584 		kvm->cfg.host_mac = DEFAULT_HOST_MAC;
585 
586 	if (!kvm->cfg.script)
587 		kvm->cfg.script = DEFAULT_SCRIPT;
588 
589 	if (!kvm->cfg.vnc && !kvm->cfg.sdl)
590 		kvm->cfg.vidmode = -1;
591 
592 	if (!kvm->cfg.network)
593                 kvm->cfg.network = DEFAULT_NETWORK;
594 
595 	memset(real_cmdline, 0, sizeof(real_cmdline));
596 	kvm__arch_set_cmdline(real_cmdline, kvm->cfg.vnc || kvm->cfg.sdl);
597 
598 	if (strlen(real_cmdline) > 0)
599 		strcat(real_cmdline, " ");
600 
601 	if (kvm->cfg.kernel_cmdline)
602 		strlcat(real_cmdline, kvm->cfg.kernel_cmdline, sizeof(real_cmdline));
603 
604 	if (!kvm->cfg.guest_name) {
605 		if (kvm->cfg.custom_rootfs) {
606 			kvm->cfg.guest_name = kvm->cfg.custom_rootfs_name;
607 		} else {
608 			sprintf(default_name, "guest-%u", getpid());
609 			kvm->cfg.guest_name = default_name;
610 		}
611 	}
612 
613 	if (!kvm->cfg.using_rootfs && !kvm->cfg.disk_image[0].filename && !kvm->cfg.initrd_filename) {
614 		char tmp[PATH_MAX];
615 
616 		kvm_setup_create_new(kvm->cfg.custom_rootfs_name);
617 		kvm_setup_resolv(kvm->cfg.custom_rootfs_name);
618 
619 		snprintf(tmp, PATH_MAX, "%s%s", kvm__get_dir(), "default");
620 		if (virtio_9p__register(kvm, tmp, "/dev/root") < 0)
621 			die("Unable to initialize virtio 9p");
622 		if (virtio_9p__register(kvm, "/", "hostfs") < 0)
623 			die("Unable to initialize virtio 9p");
624 		kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1;
625 	}
626 
627 	if (kvm->cfg.using_rootfs) {
628 		strcat(real_cmdline, " root=/dev/root rw rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p");
629 		if (kvm->cfg.custom_rootfs) {
630 			kvm_run_set_sandbox();
631 
632 			strcat(real_cmdline, " init=/virt/init");
633 
634 			if (!kvm->cfg.no_dhcp)
635 				strcat(real_cmdline, "  ip=dhcp");
636 			if (kvm_setup_guest_init())
637 				die("Failed to setup init for guest.");
638 		}
639 	} else if (!strstr(real_cmdline, "root=")) {
640 		strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
641 	}
642 
643 	kvm->cfg.real_cmdline = real_cmdline;
644 
645 	printf("  # %s run -k %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME,
646 		kvm->cfg.kernel_filename, kvm->cfg.ram_size / 1024 / 1024, kvm->cfg.nrcpus, kvm->cfg.guest_name);
647 
648 	return init_list__init(kvm);
649 }
650 
651 static int kvm_cmd_run_work(void)
652 {
653 	int i;
654 	void *ret = NULL;
655 
656 	for (i = 0; i < kvm->nrcpus; i++) {
657 		if (pthread_create(&kvm->cpus[i]->thread, NULL, kvm_cpu_thread, kvm->cpus[i]) != 0)
658 			die("unable to create KVM VCPU thread");
659 	}
660 
661 	/* Only VCPU #0 is going to exit by itself when shutting down */
662 	return pthread_join(kvm->cpus[0]->thread, &ret);
663 }
664 
665 static void kvm_cmd_run_exit(int guest_ret)
666 {
667 	compat__print_all_messages();
668 
669 	init_list__exit(kvm);
670 
671 	if (guest_ret == 0)
672 		printf("\n  # KVM session ended normally.\n");
673 }
674 
675 int kvm_cmd_run(int argc, const char **argv, const char *prefix)
676 {
677 	int r, ret = -EFAULT;
678 
679 	r = kvm_cmd_run_init(argc, argv);
680 	if (r < 0)
681 		return r;
682 
683 	ret = kvm_cmd_run_work();
684 	kvm_cmd_run_exit(ret);
685 
686 	return ret;
687 }
688