From e3d3ced98505f1305250af86edd79c5ac895f1af Mon Sep 17 00:00:00 2001 From: Sasha Levin Date: Wed, 5 Sep 2012 10:31:59 +0200 Subject: [PATCH] kvm tools: kernel load/firmware cleanup Sort out the config initialization order so that configuration is fully initialized before init functions start running, and move the firmware initialization code into kvm.c. Signed-off-by: Sasha Levin Signed-off-by: Pekka Enberg --- tools/kvm/builtin-run.c | 122 +++++++++++++---------------- tools/kvm/include/kvm/kvm-config.h | 1 + tools/kvm/kvm.c | 15 ++++ 3 files changed, 70 insertions(+), 68 deletions(-) diff --git a/tools/kvm/builtin-run.c b/tools/kvm/builtin-run.c index 1ad574d75030..ec783f60e331 100644 --- a/tools/kvm/builtin-run.c +++ b/tools/kvm/builtin-run.c @@ -594,6 +594,7 @@ static int kvm_cmd_run_init(int argc, const char **argv) } kvm->cfg.vmlinux_filename = find_vmlinux(); + kvm->vmlinux = kvm->cfg.vmlinux_filename; if (kvm->cfg.nrcpus == 0) kvm->cfg.nrcpus = nr_online_cpus; @@ -642,11 +643,14 @@ static int kvm_cmd_run_init(int argc, const char **argv) if (!kvm->cfg.vnc && !kvm->cfg.sdl) kvm->cfg.vidmode = -1; - r = term_init(kvm); - if (r < 0) { - pr_err("term_init() failed with error %d\n", r); - goto fail; - } + memset(real_cmdline, 0, sizeof(real_cmdline)); + kvm__arch_set_cmdline(real_cmdline, kvm->cfg.vnc || kvm->cfg.sdl); + + if (strlen(real_cmdline) > 0) + strcat(real_cmdline, " "); + + if (kvm->cfg.kernel_cmdline) + strlcat(real_cmdline, kvm->cfg.kernel_cmdline, sizeof(real_cmdline)); if (!kvm->cfg.guest_name) { if (kvm->cfg.custom_rootfs) { @@ -657,10 +661,52 @@ static int kvm_cmd_run_init(int argc, const char **argv) } } + if (!kvm->cfg.using_rootfs && !kvm->cfg.disk_image[0].filename && !kvm->cfg.initrd_filename) { + char tmp[PATH_MAX]; + + kvm_setup_create_new(kvm->cfg.custom_rootfs_name); + kvm_setup_resolv(kvm->cfg.custom_rootfs_name); + + snprintf(tmp, PATH_MAX, "%s%s", kvm__get_dir(), "default"); + if (virtio_9p__register(kvm, tmp, "/dev/root") < 0) + die("Unable to initialize virtio 9p"); + if (virtio_9p__register(kvm, "/", "hostfs") < 0) + die("Unable to initialize virtio 9p"); + kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1; + } + + if (kvm->cfg.using_rootfs) { + strcat(real_cmdline, " root=/dev/root rw rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p"); + if (kvm->cfg.custom_rootfs) { + kvm_run_set_sandbox(); + + strcat(real_cmdline, " init=/virt/init"); + + if (!kvm->cfg.no_dhcp) + strcat(real_cmdline, " ip=dhcp"); + if (kvm_setup_guest_init()) + die("Failed to setup init for guest."); + } + } else if (!strstr(real_cmdline, "root=")) { + strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline)); + } + + kvm->cfg.real_cmdline = real_cmdline; + + printf(" # %s run -k %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME, + kvm->cfg.kernel_filename, kvm->cfg.ram_size / 1024 / 1024, kvm->cfg.nrcpus, kvm->cfg.guest_name); + r = kvm__init(kvm); if (r) goto fail; + r = term_init(kvm); + if (r < 0) { + pr_err("term_init() failed with error %d\n", r); + goto fail; + } + + r = ioeventfd__init(kvm); if (r < 0) { pr_err("ioeventfd__init() failed with error %d\n", r); @@ -691,64 +737,15 @@ static int kvm_cmd_run_init(int argc, const char **argv) goto fail; } - memset(real_cmdline, 0, sizeof(real_cmdline)); - kvm__arch_set_cmdline(real_cmdline, kvm->cfg.vnc || kvm->cfg.sdl); - - if (strlen(real_cmdline) > 0) - strcat(real_cmdline, " "); - - if (kvm->cfg.kernel_cmdline) - strlcat(real_cmdline, kvm->cfg.kernel_cmdline, sizeof(real_cmdline)); - - if (!kvm->cfg.using_rootfs && !kvm->cfg.disk_image[0].filename && !kvm->cfg.initrd_filename) { - char tmp[PATH_MAX]; - - kvm_setup_create_new(kvm->cfg.custom_rootfs_name); - kvm_setup_resolv(kvm->cfg.custom_rootfs_name); - - snprintf(tmp, PATH_MAX, "%s%s", kvm__get_dir(), "default"); - if (virtio_9p__register(kvm, tmp, "/dev/root") < 0) - die("Unable to initialize virtio 9p"); - if (virtio_9p__register(kvm, "/", "hostfs") < 0) - die("Unable to initialize virtio 9p"); - kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1; - } - - if (kvm->cfg.using_rootfs) { - strcat(real_cmdline, " root=/dev/root rw rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p"); - if (kvm->cfg.custom_rootfs) { - kvm_run_set_sandbox(); - - strcat(real_cmdline, " init=/virt/init"); - - if (!kvm->cfg.no_dhcp) - strcat(real_cmdline, " ip=dhcp"); - if (kvm_setup_guest_init()) - die("Failed to setup init for guest."); - } - } else if (!strstr(real_cmdline, "root=")) { - strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline)); - } - r = disk_image__init(kvm); if (r < 0) { pr_err("disk_image__init() failed with error %d\n", r); goto fail; } - printf(" # %s run -k %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME, - kvm->cfg.kernel_filename, kvm->cfg.ram_size / 1024 / 1024, kvm->cfg.nrcpus, kvm->cfg.guest_name); - - if (!kvm->cfg.firmware_filename) { - if (!kvm__load_kernel(kvm, kvm->cfg.kernel_filename, - kvm->cfg.initrd_filename, real_cmdline, kvm->cfg.vidmode)) - die("unable to load kernel %s", kvm->cfg.kernel_filename); - - kvm->vmlinux = kvm->cfg.vmlinux_filename; - r = symbol_init(kvm); - if (r < 0) - pr_debug("symbol_init() failed with error %d\n", r); - } + r = symbol_init(kvm); + if (r < 0) + pr_debug("symbol_init() failed with error %d\n", r); ioport__setup_arch(); @@ -848,17 +845,6 @@ static int kvm_cmd_run_init(int argc, const char **argv) goto fail; } - if (kvm->cfg.firmware_filename) { - if (!kvm__load_firmware(kvm, kvm->cfg.firmware_filename)) - die("unable to load firmware image %s: %s", kvm->cfg.firmware_filename, strerror(errno)); - } else { - kvm__arch_setup_firmware(kvm); - if (r < 0) { - pr_err("kvm__arch_setup_firmware() failed with error %d\n", r); - goto fail; - } - } - r = thread_pool__init(kvm); if (r < 0) { pr_err("thread_pool__init() failed with error %d\n", r); diff --git a/tools/kvm/include/kvm/kvm-config.h b/tools/kvm/include/kvm/kvm-config.h index 5e7dc282455d..df36a76d2edc 100644 --- a/tools/kvm/include/kvm/kvm-config.h +++ b/tools/kvm/include/kvm/kvm-config.h @@ -43,6 +43,7 @@ struct kvm_config { const char *sandbox; const char *hugetlbfs_path; const char *custom_rootfs_name; + const char *real_cmdline; struct virtio_net_params *net_params; bool single_step; bool vnc; diff --git a/tools/kvm/kvm.c b/tools/kvm/kvm.c index b950c0854df7..7a6e15e58b9d 100644 --- a/tools/kvm/kvm.c +++ b/tools/kvm/kvm.c @@ -246,6 +246,21 @@ int kvm__init(struct kvm *kvm) kvm__arch_init(kvm, kvm->cfg.hugetlbfs_path, kvm->cfg.ram_size); + if (!kvm->cfg.firmware_filename) { + if (!kvm__load_kernel(kvm, kvm->cfg.kernel_filename, + kvm->cfg.initrd_filename, kvm->cfg.real_cmdline, kvm->cfg.vidmode)) + die("unable to load kernel %s", kvm->cfg.kernel_filename); + } + + if (kvm->cfg.firmware_filename) { + if (!kvm__load_firmware(kvm, kvm->cfg.firmware_filename)) + die("unable to load firmware image %s: %s", kvm->cfg.firmware_filename, strerror(errno)); + } else { + ret = kvm__arch_setup_firmware(kvm); + if (ret < 0) + die("kvm__arch_setup_firmware() failed with error %d\n", ret); + } + return 0; err_vm_fd: -- 2.39.5