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