summaryrefslogtreecommitdiff
path: root/qemu-img.c
diff options
context:
space:
mode:
authorStefan Hajnoczi <stefanha@linux.vnet.ibm.com>2010-04-24 09:12:12 +0100
committerKevin Wolf <kwolf@redhat.com>2010-05-03 10:07:32 +0200
commitae6b0ed6d4d4a41e77584f44d2888303645fa845 (patch)
treeb3a9d851a0a738db40e4e91137cf5a0c91bdff78 /qemu-img.c
parent51762288b41f59d027073c404a29256fc4d4540e (diff)
downloadqemu-ae6b0ed6d4d4a41e77584f44d2888303645fa845.tar.gz
qemu-img: Add 'resize' command to grow/shrink disk images
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 <stefanha@linux.vnet.ibm.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Diffstat (limited to 'qemu-img.c')
-rw-r--r--qemu-img.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/qemu-img.c b/qemu-img.c
index 74311a5215..c21d9990e0 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 },