From patchwork Fri May 20 18:41:46 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prasad Joshi X-Patchwork-Id: 804492 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p4KIfvLw031810 for ; Fri, 20 May 2011 18:41:58 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934976Ab1ETSlz (ORCPT ); Fri, 20 May 2011 14:41:55 -0400 Received: from mail-ww0-f42.google.com ([74.125.82.42]:37755 "EHLO mail-ww0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934043Ab1ETSly (ORCPT ); Fri, 20 May 2011 14:41:54 -0400 Received: by wwk4 with SMTP id 4so755694wwk.1 for ; Fri, 20 May 2011 11:41:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:date:message-id:x-mailer; bh=0K56WIayyD6zEgdssAX5ZoG03eSHvUlPsZ9XHaQeShM=; b=vXOSHMpHMAPESM3SoY4AatB4GavGNaFzrpCpyf1oEETK4wWlN7yeoq1vrdpi8hbj6Z sEK1IXhWT+V4Mmj6ihTG7+G7afpFO+e2e2iBADySqiD+rfkXYq+KI1+69f/2uPc0fBNZ FLxRFpC9Y6fq76FraDfj49HdUAXM12xH3xfU0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; b=is1f3YyhZK2ZUtEwMUhRkl0XSNtyQP6UfHu8iDsbqfAZPzgqT8eoQb9JkpGzOVz6Vu EUH9IbN8jlf8BtbAy/Hi7X7nQYR95pY4I0Y/MqPZuQGrZuec7X0PMpih+mNCiT43rY7C KRwr1rA8tkfmsPGQU4hv4gDofPvHq6dlLeIu4= Received: by 10.227.164.199 with SMTP id f7mr4600016wby.70.1305916912702; Fri, 20 May 2011 11:41:52 -0700 (PDT) Received: from prasad-kvm.localdomain (pineapple.rdg.ac.uk [134.225.206.123]) by mx.google.com with ESMTPS id k2sm2461954wby.17.2011.05.20.11.41.50 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 20 May 2011 11:41:51 -0700 (PDT) Received: by prasad-kvm.localdomain (Postfix, from userid 1000) id 5CE8826E004E; Fri, 20 May 2011 19:41:50 +0100 (BST) From: Prasad Joshi To: prasadjoshi124@gmail.com Cc: mingo@elte.hu, kvm@vger.kernel.org, penberg@kernel.org, asias.hejun@gmail.com, gorcunov@gmail.com, levinsasha928@gmail.com, chaitanyakulkarni15@gmail.com, ashwini.kulkarni@gmail.com Subject: [PATCH v3 1/4] kvm tools: Add a wrapper function to open disk images Date: Fri, 20 May 2011 19:41:46 +0100 Message-Id: <1305916909-21734-1-git-send-email-prasadjoshi124@gmail.com> X-Mailer: git-send-email 1.7.4.1 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Fri, 20 May 2011 18:41:58 +0000 (UTC) The patch was suggested by Ingo to move the disk image subsystem code from the kvm-run.c file. The code to open all of the specified disk images is now moved to a wrapper function in disk/core.c. Suggested-by: Ingo Molnar Signed-off-by: Prasad Joshi --- tools/kvm/disk/core.c | 34 ++++++++++++++++++++++++++++++++++ tools/kvm/include/kvm/disk-image.h | 2 ++ tools/kvm/include/kvm/kvm.h | 1 + tools/kvm/kvm-run.c | 15 ++++++--------- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/tools/kvm/disk/core.c b/tools/kvm/disk/core.c index 34852a2..fa603cc 100644 --- a/tools/kvm/disk/core.c +++ b/tools/kvm/disk/core.c @@ -59,6 +59,40 @@ struct disk_image *disk_image__open(const char *filename, bool readonly) return NULL; } +struct disk_image **disk_image__open_all(const char **filenames, bool *readonly, int count) +{ + struct disk_image **disks; + int i; + + if (count > MAX_DISK_IMAGES) + return NULL; + + disks = malloc(sizeof(*disks) * count); + if (!disks) + return NULL; + + for (i = 0; i < count; i++) { + if (!filenames[i]) { + disks[i] = NULL; + continue; + } + + disks[i] = disk_image__open(filenames[i], readonly[i]); + if (!disks[i]) { + pr_error("Loading disk image '%s' failed", filenames[i]); + goto error; + } + } + return disks; +error: + if (i > 0) { + while (--i) + disk_image__close(disks[i]); + } + + return NULL; +} + int disk_image__flush(struct disk_image *disk) { if (disk->ops->flush) diff --git a/tools/kvm/include/kvm/disk-image.h b/tools/kvm/include/kvm/disk-image.h index ac394e8..bf2bfa9 100644 --- a/tools/kvm/include/kvm/disk-image.h +++ b/tools/kvm/include/kvm/disk-image.h @@ -23,6 +23,7 @@ #define DISK_IMAGE_MMAP 0 #define DISK_IMAGE_NOMMAP 1 +#define MAX_DISK_IMAGES 4 struct disk_image; @@ -51,6 +52,7 @@ struct disk_image { }; struct disk_image *disk_image__open(const char *filename, bool readonly); +struct disk_image **disk_image__open_all(const char **filenames, bool *readonly, int count); struct disk_image *disk_image__new(int fd, u64 size, struct disk_image_operations *ops, int mmap); int disk_image__close(struct disk_image *disk); int disk_image__flush(struct disk_image *disk); diff --git a/tools/kvm/include/kvm/kvm.h b/tools/kvm/include/kvm/kvm.h index 3cf6e6c..f419634 100644 --- a/tools/kvm/include/kvm/kvm.h +++ b/tools/kvm/include/kvm/kvm.h @@ -32,6 +32,7 @@ struct kvm { struct interrupt_table interrupt_table; const char *vmlinux; + struct disk_image **disks; }; struct kvm *kvm__init(const char *kvm_dev, unsigned long ram_size); diff --git a/tools/kvm/kvm-run.c b/tools/kvm/kvm-run.c index f7de0fb..28e3ed6 100644 --- a/tools/kvm/kvm-run.c +++ b/tools/kvm/kvm-run.c @@ -44,7 +44,6 @@ #define MB_SHIFT (20) #define MIN_RAM_SIZE_MB (64ULL) #define MIN_RAM_SIZE_BYTE (MIN_RAM_SIZE_MB << MB_SHIFT) -#define MAX_DISK_IMAGES 4 static struct kvm *kvm; static struct kvm_cpu *kvm_cpus[KVM_NR_CPUS]; @@ -530,17 +529,15 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix) if (!strstr(real_cmdline, "root=")) strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline)); - for (i = 0; i < image_count; i++) { - if (image_filename[i]) { - struct disk_image *disk = disk_image__open(image_filename[i], readonly_image[i]); - if (!disk) - die("unable to load disk image %s", image_filename[i]); + kvm->disks = disk_image__open_all(image_filename, readonly_image, image_count); + if (!kvm->disks) + die("Unable to load all disk images."); - virtio_blk__init(kvm, disk); - } - } free(hi); + for (i = 0; i < image_count; i++) + virtio_blk__init(kvm, kvm->disks[i]); + printf(" # kvm run -k %s -m %Lu -c %d\n", kernel_filename, ram_size / 1024 / 1024, nrcpus); if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename,