do {
aio_poll(aio_context, true);
+ qemu_progress_print((float)job->offset / job->len * 100.f, 0);
} while (!job->ready);
block_job_complete_sync(job, errp);
+
+ /* A block job may finish instantaneously without publishing any progress,
+ * so just signal completion here */
+ qemu_progress_print(100.f, 0);
}
static int img_commit(int argc, char **argv)
{
int c, ret, flags;
- const char *filename, *fmt, *cache;
+ const char *filename, *fmt, *cache, *base;
BlockBackend *blk;
BlockDriverState *bs, *base_bs;
- bool quiet = false;
+ bool progress = false, quiet = false, drop = false;
Error *local_err = NULL;
CommonBlockJobCBInfo cbi;
fmt = NULL;
cache = BDRV_DEFAULT_CACHE;
+ base = NULL;
for(;;) {
- c = getopt(argc, argv, "f:ht:q");
+ c = getopt(argc, argv, "f:ht:b:dpq");
if (c == -1) {
break;
}
case 't':
cache = optarg;
break;
+ case 'b':
+ base = optarg;
+ /* -b implies -d */
+ drop = true;
+ break;
+ case 'd':
+ drop = true;
+ break;
+ case 'p':
+ progress = true;
+ break;
case 'q':
quiet = true;
break;
}
}
+
+ /* Progress is not shown in Quiet mode */
+ if (quiet) {
+ progress = false;
+ }
+
if (optind != argc - 1) {
error_exit("Expecting one image file name");
}
filename = argv[optind++];
- flags = BDRV_O_RDWR;
+ flags = BDRV_O_RDWR | BDRV_O_UNMAP;
ret = bdrv_parse_cache_flags(cache, &flags);
if (ret < 0) {
error_report("Invalid cache option: %s", cache);
}
bs = blk_bs(blk);
- /* This is different from QMP, which by default uses the deepest file in the
- * backing chain (i.e., the very base); however, the traditional behavior of
- * qemu-img commit is using the immediate backing file. */
- base_bs = bs->backing_hd;
- if (!base_bs) {
- error_setg(&local_err, "Image does not have a backing file");
- goto done;
+ qemu_progress_init(progress, 1.f);
+ qemu_progress_print(0.f, 100);
+
+ if (base) {
+ base_bs = bdrv_find_backing_image(bs, base);
+ if (!base_bs) {
+ error_set(&local_err, QERR_BASE_NOT_FOUND, base);
+ goto done;
+ }
+ } else {
+ /* This is different from QMP, which by default uses the deepest file in
+ * the backing chain (i.e., the very base); however, the traditional
+ * behavior of qemu-img commit is using the immediate backing file. */
+ base_bs = bs->backing_hd;
+ if (!base_bs) {
+ error_setg(&local_err, "Image does not have a backing file");
+ goto done;
+ }
}
cbi = (CommonBlockJobCBInfo){
goto done;
}
+ /* The block job will swap base_bs and bs (which is not what we really want
+ * here, but okay) and unref base_bs (after the swap, i.e., the old top
+ * image). In order to still be able to empty that top image afterwards,
+ * increment the reference counter here preemptively. */
+ if (!drop) {
+ bdrv_ref(base_bs);
+ }
+
run_block_job(bs->job, &local_err);
+ if (local_err) {
+ goto unref_backing;
+ }
+
+ if (!drop && base_bs->drv->bdrv_make_empty) {
+ ret = base_bs->drv->bdrv_make_empty(base_bs);
+ if (ret) {
+ error_setg_errno(&local_err, -ret, "Could not empty %s",
+ filename);
+ goto unref_backing;
+ }
+ }
+
+unref_backing:
+ if (!drop) {
+ bdrv_unref(base_bs);
+ }
done:
+ qemu_progress_end();
+
blk_unref(blk);
if (local_err) {
blk1 = img_open("image_1", filename1, fmt1, flags, true, quiet);
if (!blk1) {
- error_report("Can't open file %s", filename1);
ret = 2;
goto out3;
}
blk2 = img_open("image_2", filename2, fmt2, flags, true, quiet);
if (!blk2) {
- error_report("Can't open file %s", filename2);
ret = 2;
goto out2;
}
true, quiet);
g_free(id);
if (!blk[bs_i]) {
- error_report("Could not open '%s'", argv[optind + bs_i]);
ret = -1;
goto out;
}
goto out;
}
+ if (!drv->create_opts) {
+ error_report("Format driver '%s' does not support image creation",
+ drv->format_name);
+ ret = -1;
+ goto out;
+ }
+
+ if (!proto_drv->create_opts) {
+ error_report("Protocol driver '%s' does not support image creation",
+ proto_drv->format_name);
+ ret = -1;
+ goto out;
+ }
+
create_opts = qemu_opts_append(create_opts, drv->create_opts);
create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
return 0;
}
+static void amend_status_cb(BlockDriverState *bs,
+ int64_t offset, int64_t total_work_size)
+{
+ qemu_progress_print(100.f * offset / total_work_size, 0);
+}
+
static int img_amend(int argc, char **argv)
{
int c, ret = 0;
QemuOpts *opts = NULL;
const char *fmt = NULL, *filename, *cache;
int flags;
- bool quiet = false;
+ bool quiet = false, progress = false;
BlockBackend *blk = NULL;
BlockDriverState *bs = NULL;
cache = BDRV_DEFAULT_CACHE;
for (;;) {
- c = getopt(argc, argv, "ho:f:t:q");
+ c = getopt(argc, argv, "ho:f:t:pq");
if (c == -1) {
break;
}
case 't':
cache = optarg;
break;
+ case 'p':
+ progress = true;
+ break;
case 'q':
quiet = true;
break;
error_exit("Must specify options (-o)");
}
+ if (quiet) {
+ progress = false;
+ }
+ qemu_progress_init(progress, 1.0);
+
filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
if (fmt && has_help_option(options)) {
/* If a format is explicitly specified (and possibly no filename is
}
if (optind != argc - 1) {
- error_exit("Expecting one image file name");
+ error_report("Expecting one image file name");
+ ret = -1;
+ goto out;
}
flags = BDRV_O_FLAGS | BDRV_O_RDWR;
blk = img_open("image", filename, fmt, flags, true, quiet);
if (!blk) {
- error_report("Could not open image '%s'", filename);
ret = -1;
goto out;
}
goto out;
}
+ if (!bs->drv->create_opts) {
+ error_report("Format driver '%s' does not support any options to amend",
+ fmt);
+ ret = -1;
+ goto out;
+ }
+
create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
if (options && qemu_opts_do_parse(opts, options, NULL)) {
goto out;
}
- ret = bdrv_amend_options(bs, opts);
+ /* In case the driver does not call amend_status_cb() */
+ qemu_progress_print(0.f, 0);
+ ret = bdrv_amend_options(bs, opts, &amend_status_cb);
+ qemu_progress_print(100.f, 0);
if (ret < 0) {
error_report("Error while amending options: %s", strerror(-ret));
goto out;
}
out:
+ qemu_progress_end();
+
blk_unref(blk);
qemu_opts_del(opts);
qemu_opts_free(create_opts);