From patchwork Thu May 5 10:24:31 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 756432 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter2.kernel.org (8.14.4/8.14.3) with ESMTP id p45AP9Bo009360 for ; Thu, 5 May 2011 10:25:10 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753739Ab1EEKZF (ORCPT ); Thu, 5 May 2011 06:25:05 -0400 Received: from mail-ww0-f44.google.com ([74.125.82.44]:41831 "EHLO mail-ww0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753783Ab1EEKZD (ORCPT ); Thu, 5 May 2011 06:25:03 -0400 Received: by wwa36 with SMTP id 36so2190065wwa.1 for ; Thu, 05 May 2011 03:25:00 -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 :in-reply-to:references; bh=Bm/lm+DLjfP4VeV6wTqernNKME3eHWtruzVbS3jTOa8=; b=Wrm0VuxRq4K85tcsVajPltfaj7xb9C9c6n0YzciAX/A5r6D3wWVuOo6lgFzv3Br7YF 3opAN4OqjURHLMOfH3bJSxzck219TXzXZ18PYO0lLrpbur7l+5Hkb2ngbFO/EwXBOLIr 1n5CqJ7tEMuBNEezSqqYTlt6vXXPT2K+5zVZY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; b=CYQRhbNLYmB+qMTYyrbNFC2SMax/7AxG4MwWGVqbJ2HkkcD6lDHCy4YWhSf5l7bPdY Auvga7guzPNPyvxholfna7E8jYOVqWLexfVtE/stA+fV+bja2Lh5OHJ6rQ6NFomAWzCE tAEaP+PA0GGB3SptFE+eAnEX9zP8pWNDWWBbE= Received: by 10.216.174.65 with SMTP id w43mr5945750wel.95.1304591100622; Thu, 05 May 2011 03:25:00 -0700 (PDT) Received: from localhost.localdomain (bzq-79-183-199-149.red.bezeqint.net [79.183.199.149]) by mx.google.com with ESMTPS id n2sm1009192wej.22.2011.05.05.03.24.58 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 05 May 2011 03:25:00 -0700 (PDT) From: Sasha Levin To: penberg@kernel.org Cc: mingo@elte.hu, asias.hejun@gmail.com, gorcunov@gmail.com, prasadjoshi124@gmail.com, kvm@vger.kernel.org, Sasha Levin Subject: [PATCH 3/3 V3] kvm tools: Add cmdline options for loading multiple images Date: Thu, 5 May 2011 13:24:31 +0300 Message-Id: <1304591071-27074-3-git-send-email-levinsasha928@gmail.com> X-Mailer: git-send-email 1.7.5.rc3 In-Reply-To: <1304591071-27074-1-git-send-email-levinsasha928@gmail.com> References: <1304591071-27074-1-git-send-email-levinsasha928@gmail.com> 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 (demeter2.kernel.org [140.211.167.43]); Thu, 05 May 2011 10:25:10 +0000 (UTC) Introduced new syntax for loading disk images. Example: ./kvm run --image image1.img,ro --image image2.img Will load image1.img with read only, and image2.img as read/write. Signed-off-by: Sasha Levin --- tools/kvm/include/kvm/parse-options.h | 35 ++++++++++++++++++++++++ tools/kvm/kvm-run.c | 48 ++++++++++++++++++++++++--------- 2 files changed, 70 insertions(+), 13 deletions(-) diff --git a/tools/kvm/include/kvm/parse-options.h b/tools/kvm/include/kvm/parse-options.h index 5d8c618..2d5c99e 100644 --- a/tools/kvm/include/kvm/parse-options.h +++ b/tools/kvm/include/kvm/parse-options.h @@ -138,6 +138,41 @@ intptr_t defval; .help = (h) \ } +#define OPT_CALLBACK(s, l, v, a, h, f) \ +{ \ + .type = OPTION_CALLBACK, \ + .short_name = (s), \ + .long_name = (l), \ + .value = (v), \ + (a), \ + .help = (h), \ + .callback = (f) \ +} + +#define OPT_CALLBACK_NOOPT(s, l, v, a, h, f) \ +{ \ + .type = OPTION_CALLBACK, \ + .short_name = (s), \ + .long_name = (l), \ + .value = (v), \ + (a), \ + .help = (h), \ + .callback = (f), \ + .flags = PARSE_OPT_NOARG \ +} + +#define OPT_CALLBACK_DEFAULT(s, l, v, a, h, f, d) \ +{ \ + .type = OPTION_CALLBACK, \ + .short_name = (s), \ + .long_name = (l), \ + .value = (v), (a), \ + .help = (h), \ + .callback = (f), \ + .defval = (intptr_t)d, \ + .flags = PARSE_OPT_LASTARG_DEFAULT \ +} + #define OPT_END() { .type = OPTION_END } enum { diff --git a/tools/kvm/kvm-run.c b/tools/kvm/kvm-run.c index b92232d..2182dc5 100644 --- a/tools/kvm/kvm-run.c +++ b/tools/kvm/kvm-run.c @@ -42,16 +42,18 @@ #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]; static __thread struct kvm_cpu *current_kvm_cpu; static u64 ram_size = MIN_RAM_SIZE_MB; +static u8 image_count; static const char *kernel_cmdline; static const char *kernel_filename; static const char *initrd_filename; -static const char *image_filename; +static const char *image_filename[MAX_DISK_IMAGES]; static const char *console; static const char *kvm_dev; static const char *network; @@ -59,7 +61,7 @@ static const char *host_ip_addr; static const char *guest_mac; static const char *script; static bool single_step; -static bool readonly_image; +static bool readonly_image[MAX_DISK_IMAGES]; static bool virtio_rng; extern bool ioport_debug; extern int active_console; @@ -71,13 +73,31 @@ static const char * const run_usage[] = { NULL }; +static int img_name_parser(const struct option *opt, const char *arg, int unset) +{ + char *sep; + + if (image_count >= MAX_DISK_IMAGES) + die("Currently only 4 images are supported"); + + image_filename[image_count] = arg; + sep = strstr(arg, ","); + if (sep) { + if (strcmp(sep + 1, "ro") == 0) + readonly_image[image_count] = 1; + *sep = 0; + } + + image_count++; + + return 0; +} + static const struct option options[] = { OPT_GROUP("Basic options:"), OPT_INTEGER('\0', "cpus", &nrcpus, "Number of CPUs"), OPT_U64('m', "mem", &ram_size, "Virtual machine memory size in MiB."), - OPT_STRING('i', "image", &image_filename, "image", "Disk image"), - OPT_BOOLEAN('\0', "readonly", &readonly_image, - "Don't write changes back to disk image"), + OPT_CALLBACK('i', "image", NULL, "image", "Disk image", img_name_parser), OPT_STRING('c', "console", &console, "serial or virtio", "Console to use"), OPT_BOOLEAN('\0', "virtio-rng", &virtio_rng, @@ -397,23 +417,25 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix) strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline)); hi = NULL; - if (!image_filename) { + if (!image_filename[0]) { hi = host_image(real_cmdline, sizeof(real_cmdline)); if (hi) { - image_filename = hi; - readonly_image = true; + image_filename[0] = hi; + readonly_image[0] = true; } } if (!strstr(real_cmdline, "root=")) strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline)); - if (image_filename) { - struct disk_image *disk = disk_image__open(image_filename, readonly_image); - if (!disk) - die("unable to load disk image %s", image_filename); + 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]); - virtio_blk__init(kvm, disk); + virtio_blk__init(kvm, disk); + } } free(hi);