From patchwork Sat Apr 24 08:12:12 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 50883 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id B36A6B7D4E for ; Sat, 24 Apr 2010 18:17:33 +1000 (EST) Received: from localhost ([127.0.0.1]:59895 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O5aW5-0000Z1-0h for incoming@patchwork.ozlabs.org; Sat, 24 Apr 2010 04:15:09 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1O5aSu-0000Bo-Kq for qemu-devel@nongnu.org; Sat, 24 Apr 2010 04:11:52 -0400 Received: from [140.186.70.92] (port=36031 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O5aSr-0000BT-Gc for qemu-devel@nongnu.org; Sat, 24 Apr 2010 04:11:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1O5aSp-0001vf-Kd for qemu-devel@nongnu.org; Sat, 24 Apr 2010 04:11:49 -0400 Received: from mtagate2.uk.ibm.com ([194.196.100.162]:60614) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1O5aSp-0001vN-D6 for qemu-devel@nongnu.org; Sat, 24 Apr 2010 04:11:47 -0400 Received: from d06nrmr1806.portsmouth.uk.ibm.com (d06nrmr1806.portsmouth.uk.ibm.com [9.149.39.193]) by mtagate2.uk.ibm.com (8.13.1/8.13.1) with ESMTP id o3O8BiWZ011154 for ; Sat, 24 Apr 2010 08:11:44 GMT Received: from d06av03.portsmouth.uk.ibm.com (d06av03.portsmouth.uk.ibm.com [9.149.37.213]) by d06nrmr1806.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o3O8BiLN1400950 for ; Sat, 24 Apr 2010 09:11:44 +0100 Received: from d06av03.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av03.portsmouth.uk.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id o3O8BiK3010577 for ; Sat, 24 Apr 2010 09:11:44 +0100 Received: from localhost.localdomain (sig-9-146-165-214.uk.ibm.com [9.146.165.214]) by d06av03.portsmouth.uk.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id o3O8BhQ0010569; Sat, 24 Apr 2010 09:11:43 +0100 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Date: Sat, 24 Apr 2010 09:12:12 +0100 Message-Id: <1272096733-6070-1-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: Kevin Wolf , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH 1/2] qemu-img: Add 'resize' command to grow/shrink disk images X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This patch adds a 'resize' command to grow/shrink disk images. This allows changing the size of disk images without copying to a new image file. Currently only raw files support resize. Signed-off-by: Stefan Hajnoczi --- This applies to kevin/block. I am also sending a qemu-iotests patch to test bdrv_truncate(). qemu-img-cmds.hx | 6 +++ qemu-img.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-img.texi | 12 +++++++ 3 files changed, 110 insertions(+), 0 deletions(-) diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx index f96876a..c079019 100644 --- a/qemu-img-cmds.hx +++ b/qemu-img-cmds.hx @@ -49,5 +49,11 @@ DEF("rebase", img_rebase, "rebase [-f fmt] [-u] -b backing_file [-F backing_fmt] filename") STEXI @item rebase [-f @var{fmt}] [-u] -b @var{backing_file} [-F @var{backing_fmt}] @var{filename} +ETEXI + +DEF("resize", img_resize, + "resize filename [+ | -]size") +STEXI +@item rebase @var{filename} [+ | -]@var{size} @end table ETEXI diff --git a/qemu-img.c b/qemu-img.c index 74311a5..c21d999 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1225,6 +1225,98 @@ static int img_rebase(int argc, char **argv) return 0; } +static int img_resize(int argc, char **argv) +{ + int c, ret, relative; + const char *filename, *fmt, *size; + int64_t n, total_size; + BlockDriverState *bs; + QEMUOptionParameter *param; + QEMUOptionParameter resize_options[] = { + { + .name = BLOCK_OPT_SIZE, + .type = OPT_SIZE, + .help = "Virtual disk size" + }, + { NULL } + }; + + fmt = NULL; + for(;;) { + c = getopt(argc, argv, "f:h"); + if (c == -1) { + break; + } + switch(c) { + case 'h': + help(); + break; + case 'f': + fmt = optarg; + break; + } + } + if (optind + 1 >= argc) { + help(); + } + filename = argv[optind++]; + size = argv[optind++]; + + /* Choose grow, shrink, or absolute resize mode */ + switch (size[0]) { + case '+': + relative = 1; + size++; + break; + case '-': + relative = -1; + size++; + break; + default: + relative = 0; + break; + } + + /* Parse size */ + param = parse_option_parameters("", resize_options, NULL); + if (set_option_parameter(param, BLOCK_OPT_SIZE, size)) { + /* Error message already printed when size parsing fails */ + exit(1); + } + n = get_option_parameter(param, BLOCK_OPT_SIZE)->value.n; + free_option_parameters(param); + + bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR); + + if (relative) { + total_size = bdrv_getlength(bs) + n * relative; + } else { + total_size = n; + } + if (total_size <= 0) { + error("New image size must be positive"); + } + + ret = bdrv_truncate(bs, total_size); + switch (ret) { + case 0: + printf("Image resized.\n"); + break; + case -ENOTSUP: + error("This image format does not support resize"); + break; + case -EACCES: + error("Image is read-only"); + break; + default: + error("Error resizing image (%d)", -ret); + break; + } + + bdrv_delete(bs); + return 0; +} + static const img_cmd_t img_cmds[] = { #define DEF(option, callback, arg_string) \ { option, callback }, diff --git a/qemu-img.texi b/qemu-img.texi index ac97854..c1b1f27 100644 --- a/qemu-img.texi +++ b/qemu-img.texi @@ -106,6 +106,18 @@ they are displayed too. @item snapshot [-l | -a @var{snapshot} | -c @var{snapshot} | -d @var{snapshot} ] @var{filename} List, apply, create or delete snapshots in image @var{filename}. + +@item resize @var{filename} [+ | -]@var{size} + +Change the disk image as if it had been created with @var{size}. + +Before using this command to shrink a disk image, you MUST use file system and +partitioning tools inside the VM to reduce allocated file systems and partition +sizes accordingly. Failure to do so will result in data loss! + +After using this command to grow a disk image, you must use file system and +partitioning tools inside the VM to actually begin using the new space on the +device. @end table Supported image file formats: