mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-08-04 00:03:54 -06:00
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>
This commit is contained in:
parent
69ff158b67
commit
4ffca8904a
5 changed files with 33 additions and 6 deletions
23
qemu-img.c
23
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");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue