summaryrefslogtreecommitdiff
path: root/qemu-img.c
diff options
context:
space:
mode:
authorPavel Butsykin <pbutsykin@virtuozzo.com>2017-09-18 15:42:27 +0300
committerMax Reitz <mreitz@redhat.com>2017-09-26 15:00:32 +0200
commit4ffca8904a350460cdaa6304ea8c9b9c693d2d91 (patch)
tree3d7c0d764d18334222c11e84dc8a081361ada1b6 /qemu-img.c
parent69ff158b677df0862b2f2639740c8fda5eb91599 (diff)
downloadqemu-4ffca8904a350460cdaa6304ea8c9b9c693d2d91.tar.gz
qemu-img: add --shrink flag for resize
The flag is additional precaution against data loss. Perhaps in the future the operation shrink without this flag will be blocked for all formats, but for now we need to maintain compatibility with raw. Signed-off-by: Pavel Butsykin <pbutsykin@virtuozzo.com> Reviewed-by: Max Reitz <mreitz@redhat.com> Reviewed-by: John Snow <jsnow@redhat.com> Message-id: 20170918124230.8152-2-pbutsykin@virtuozzo.com [mreitz: Added a missing space to a warning] Signed-off-by: Max Reitz <mreitz@redhat.com>
Diffstat (limited to 'qemu-img.c')
-rw-r--r--qemu-img.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/qemu-img.c b/qemu-img.c
index df984b11b9..d6007b2a6d 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -64,6 +64,7 @@ enum {
OPTION_TARGET_IMAGE_OPTS = 263,
OPTION_SIZE = 264,
OPTION_PREALLOCATION = 265,
+ OPTION_SHRINK = 266,
};
typedef enum OutputFormat {
@@ -3436,6 +3437,7 @@ static int img_resize(int argc, char **argv)
},
};
bool image_opts = false;
+ bool shrink = false;
/* Remove size from argv manually so that negative numbers are not treated
* as options by getopt. */
@@ -3454,6 +3456,7 @@ static int img_resize(int argc, char **argv)
{"object", required_argument, 0, OPTION_OBJECT},
{"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
{"preallocation", required_argument, 0, OPTION_PREALLOCATION},
+ {"shrink", no_argument, 0, OPTION_SHRINK},
{0, 0, 0, 0}
};
c = getopt_long(argc, argv, ":f:hq",
@@ -3496,6 +3499,9 @@ static int img_resize(int argc, char **argv)
return 1;
}
break;
+ case OPTION_SHRINK:
+ shrink = true;
+ break;
}
}
if (optind != argc - 1) {
@@ -3569,6 +3575,23 @@ static int img_resize(int argc, char **argv)
goto out;
}
+ if (total_size < current_size && !shrink) {
+ warn_report("Shrinking an image will delete all data beyond the "
+ "shrunken image's end. Before performing such an "
+ "operation, make sure there is no important data there.");
+
+ if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) {
+ error_report(
+ "Use the --shrink option to perform a shrink operation.");
+ ret = -1;
+ goto out;
+ } else {
+ warn_report("Using the --shrink option will suppress this message. "
+ "Note that future versions of qemu-img may refuse to "
+ "shrink images without this option.");
+ }
+ }
+
ret = blk_truncate(blk, total_size, prealloc, &err);
if (!ret) {
qprintf(quiet, "Image resized.\n");