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