From patchwork Wed Sep 5 08:31:59 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 1406631 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id 35683402E1 for ; Wed, 5 Sep 2012 08:36:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758188Ab2IEIg2 (ORCPT ); Wed, 5 Sep 2012 04:36:28 -0400 Received: from mail-we0-f174.google.com ([74.125.82.174]:40454 "EHLO mail-we0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758177Ab2IEIgY (ORCPT ); Wed, 5 Sep 2012 04:36:24 -0400 Received: by mail-we0-f174.google.com with SMTP id x8so223930wey.19 for ; Wed, 05 Sep 2012 01:36:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=ccaoemy3/vDMWwALnVtIoukju/mKIorjcJFOVnxS69A=; b=MCoRSTmqRbX6IihiDjdTPgDCsSHXCCZ+0Nd8j7lYhs1/n0SKfXlltyHmauBNsXAels Wds68gKwT1Ayc5hoFq7cskHeVHvQdBLkOX2J9nayE0+RDBhgUdwsHCGvZPR9S9//4yAQ lHrLiZiwjDG+2GCNMckxwPqt8GnUVXfqzWY4MAf6Q6aP93RMN/uucT050wZRJQuGwFmc A03nyXFLQG19Q1+ptRDA8f7Z9R01ymbWex8ppSDGJE4bZrFtgCE5b7VBAZm5Uxqu9j1z V52ysASG4rsEbaYHSeScerUHe4HbwUo88PH1AHKAzt/NPIMpKWkK8V2aougoAvma61WN i+6w== Received: by 10.180.106.130 with SMTP id gu2mr337366wib.20.1346834184152; Wed, 05 Sep 2012 01:36:24 -0700 (PDT) Received: from lappy.capriciverd.com (20.Red-80-59-140.staticIP.rima-tde.net. [80.59.140.20]) by mx.google.com with ESMTPS id q4sm27971068wix.9.2012.09.05.01.36.22 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 05 Sep 2012 01:36:23 -0700 (PDT) From: Sasha Levin To: penberg@kernel.org Cc: asias.hejun@gmail.com, mingo@elte.hu, gorcunov@openvz.org, kvm@vger.kernel.org, Sasha Levin Subject: [PATCH 25/33] kvm tools: kernel load/firmware cleanup Date: Wed, 5 Sep 2012 10:31:59 +0200 Message-Id: <1346833927-15740-26-git-send-email-levinsasha928@gmail.com> X-Mailer: git-send-email 1.7.12 In-Reply-To: <1346833927-15740-1-git-send-email-levinsasha928@gmail.com> References: <1346833927-15740-1-git-send-email-levinsasha928@gmail.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 --- 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 c59f100..ba1cf41 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 5e7dc28..df36a76 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 b950c08..7a6e15e 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: