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