* THE SOFTWARE.
*/
#include "qemu/osdep.h"
+#include <getopt.h>
+
#include "qemu-version.h"
#include "qapi/error.h"
#include "qapi-visit.h"
#include "block/qapi.h"
#include "crypto/init.h"
#include "trace/control.h"
-#include <getopt.h>
#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
"\n" QEMU_COPYRIGHT "\n"
OPTION_NO_DRAIN = 262,
OPTION_TARGET_IMAGE_OPTS = 263,
OPTION_SIZE = 264,
+ OPTION_PREALLOCATION = 265,
};
typedef enum OutputFormat {
" 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
" instead\n"
" '-c' indicates that target image must be compressed (qcow format only)\n"
- " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
- " match exactly. The image doesn't need a working backing file before\n"
- " rebasing in this case (useful for renaming the backing file)\n"
+ " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
+ " new backing file match exactly. The image doesn't need a working\n"
+ " backing file before rebasing in this case (useful for renaming the\n"
+ " backing file). For image creation, allow creating without attempting\n"
+ " to open the backing file.\n"
" '-h' with or without a command shows this help and lists the supported formats\n"
" '-p' show progress of command (only certain commands)\n"
" '-q' use Quiet mode - do not print any output (except errors)\n"
printf("%s\nSupported formats:", help_msg);
bdrv_iterate_format(format_print, NULL);
- printf("\n");
+ printf("\n\n" QEMU_HELP_BOTTOM "\n");
exit(EXIT_SUCCESS);
}
QDict *options = opaque;
if (g_str_has_suffix(name, "key-secret")) {
- qdict_put(options, name, qstring_from_str(value));
+ qdict_put_str(options, name, value);
}
return 0;
char *options = NULL;
Error *local_err = NULL;
bool quiet = false;
+ int flags = 0;
for(;;) {
static const struct option long_options[] = {
{"object", required_argument, 0, OPTION_OBJECT},
{0, 0, 0, 0}
};
- c = getopt_long(argc, argv, ":F:b:f:ho:q",
+ c = getopt_long(argc, argv, ":F:b:f:ho:qu",
long_options, NULL);
if (c == -1) {
break;
case 'q':
quiet = true;
break;
+ case 'u':
+ flags |= BDRV_O_NO_BACKING;
+ break;
case OPTION_OBJECT: {
QemuOpts *opts;
opts = qemu_opts_parse_noisily(&qemu_object_opts,
}
bdrv_img_create(filename, fmt, base_filename, base_fmt,
- options, img_size, 0, quiet, &local_err);
+ options, img_size, flags, quiet, &local_err);
if (local_err) {
error_reportf_err(local_err, "%s: ", filename);
goto fail;
Error *err = NULL;
int c, ret, relative;
const char *filename, *fmt, *size;
- int64_t n, total_size;
+ int64_t n, total_size, current_size;
bool quiet = false;
BlockBackend *blk = NULL;
+ PreallocMode prealloc = PREALLOC_MODE_OFF;
QemuOpts *param;
static QemuOptsList resize_options = {
{"help", no_argument, 0, 'h'},
{"object", required_argument, 0, OPTION_OBJECT},
{"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
+ {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
{0, 0, 0, 0}
};
c = getopt_long(argc, argv, ":f:hq",
case OPTION_IMAGE_OPTS:
image_opts = true;
break;
+ case OPTION_PREALLOCATION:
+ prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
+ PREALLOC_MODE__MAX, NULL);
+ if (prealloc == PREALLOC_MODE__MAX) {
+ error_report("Invalid preallocation mode '%s'", optarg);
+ return 1;
+ }
+ break;
}
}
if (optind != argc - 1) {
goto out;
}
+ current_size = blk_getlength(blk);
+ if (current_size < 0) {
+ error_report("Failed to inquire current image length: %s",
+ strerror(-current_size));
+ ret = -1;
+ goto out;
+ }
+
if (relative) {
- total_size = blk_getlength(blk) + n * relative;
+ total_size = current_size + n * relative;
} else {
total_size = n;
}
goto out;
}
- ret = blk_truncate(blk, total_size, PREALLOC_MODE_OFF, &err);
+ if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
+ error_report("Preallocation can only be used for growing images");
+ ret = -1;
+ goto out;
+ }
+
+ ret = blk_truncate(blk, total_size, prealloc, &err);
if (!ret) {
qprintf(quiet, "Image resized.\n");
} else {