]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
kvm tools: kernel load/firmware cleanup
authorSasha Levin <levinsasha928@gmail.com>
Wed, 5 Sep 2012 08:31:59 +0000 (10:31 +0200)
committerPekka Enberg <penberg@kernel.org>
Wed, 5 Sep 2012 13:22:38 +0000 (16:22 +0300)
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 <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
tools/kvm/builtin-run.c
tools/kvm/include/kvm/kvm-config.h
tools/kvm/kvm.c

index 1ad574d750300493d7d8265e3e21593bd3e6bd53..ec783f60e331170b5c049920d4b6ad2f9f239552 100644 (file)
@@ -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);
index 5e7dc282455da94bdc6cb9e5f797dc3b6ff29047..df36a76d2edc77c5c10c2562311ff46a2646e33c 100644 (file)
@@ -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;
index b950c0854df7a3c4b0a436aeaeedce19a0542c79..7a6e15e58b9d27d2a2d1dfbd0cd5f7a4b41f92a1 100644 (file)
@@ -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: