blob: f7ffb79db6fcdc363fd5530fbf9c59d002c35357 [file] [log] [blame]
bellardea2384d2004-08-01 21:59:26 +00001/*
bellardfb43f4d2006-08-07 21:34:46 +00002 * QEMU disk image utility
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardea2384d2004-08-01 21:59:26 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Peter Maydell80c71a22016-01-18 18:01:42 +000024#include "qemu/osdep.h"
Fam Zheng67a1de02016-06-01 17:44:21 +080025#include "qemu-version.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010026#include "qapi/error.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020027#include "qapi-visit.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010028#include "qapi/qobject-output-visitor.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010029#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010030#include "qapi/qmp/qjson.h"
Fam Zheng335e9932017-05-03 00:35:39 +080031#include "qapi/qmp/qbool.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020032#include "qemu/cutils.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000033#include "qemu/config-file.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010034#include "qemu/option.h"
35#include "qemu/error-report.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030036#include "qemu/log.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000037#include "qom/object_interfaces.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/sysemu.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020039#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010040#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020041#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080042#include "block/qapi.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010043#include "crypto/init.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030044#include "trace/control.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020045#include <getopt.h>
bellarde8445332006-06-14 15:32:10 +000046
Don Slutz61979a62015-01-09 10:17:35 -050047#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
Thomas Huth0781dd62016-10-05 11:54:44 +020048 "\n" QEMU_COPYRIGHT "\n"
Jeff Cody5f6979c2014-04-28 14:37:18 -040049
Anthony Liguoric227f092009-10-01 16:12:16 -050050typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010051 const char *name;
52 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050053} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010054
Federico Simoncelli8599ea42013-01-28 06:59:47 -050055enum {
56 OPTION_OUTPUT = 256,
57 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000058 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000059 OPTION_IMAGE_OPTS = 259,
Kevin Wolfb6495fa2015-07-10 18:09:18 +020060 OPTION_PATTERN = 260,
Kevin Wolf55d539c2016-06-03 13:59:41 +020061 OPTION_FLUSH_INTERVAL = 261,
62 OPTION_NO_DRAIN = 262,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +010063 OPTION_TARGET_IMAGE_OPTS = 263,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050064};
65
66typedef enum OutputFormat {
67 OFORMAT_JSON,
68 OFORMAT_HUMAN,
69} OutputFormat;
70
Kevin Wolfe6996142016-03-15 13:03:11 +010071/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040072#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000073
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010074static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000075{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010076 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000077}
78
Fam Zhengac1307a2014-04-22 13:36:11 +080079static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
80{
81 va_list ap;
82
83 error_printf("qemu-img: ");
84
85 va_start(ap, fmt);
86 error_vprintf(fmt, ap);
87 va_end(ap);
88
89 error_printf("\nTry 'qemu-img --help' for more information\n");
90 exit(EXIT_FAILURE);
91}
92
Stefan Hajnoczic9192972017-03-17 18:45:41 +080093static void QEMU_NORETURN missing_argument(const char *option)
94{
95 error_exit("missing argument for option '%s'", option);
96}
97
98static void QEMU_NORETURN unrecognized_option(const char *option)
99{
100 error_exit("unrecognized option '%s'", option);
101}
102
blueswir1d2c639d2009-01-24 18:19:25 +0000103/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +0800104static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +0000105{
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100106 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -0400107 QEMU_IMG_VERSION
Denis V. Lunev10985132016-06-17 17:44:13 +0300108 "usage: qemu-img [standard options] command [command options]\n"
malc3f020d72010-02-08 12:04:56 +0300109 "QEMU disk image utility\n"
110 "\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300111 " '-h', '--help' display this help and exit\n"
112 " '-V', '--version' output version information and exit\n"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +0300113 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
114 " specify tracing options\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300115 "\n"
malc3f020d72010-02-08 12:04:56 +0300116 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100117#define DEF(option, callback, arg_string) \
118 " " arg_string "\n"
119#include "qemu-img-cmds.h"
120#undef DEF
121#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +0300122 "\n"
123 "Command parameters:\n"
124 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000125 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
126 " manual page for a description of the object properties. The most common\n"
127 " object type is a 'secret', which is used to supply passwords and/or\n"
128 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300129 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400130 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800131 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
132 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100133 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
134 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300135 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200136 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
137 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
138 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300139 " 'output_filename' is the destination disk image filename\n"
140 " 'output_fmt' is the destination format\n"
141 " 'options' is a comma separated list of format specific options in a\n"
142 " name=value format. Use -o ? for an overview of the options supported by the\n"
143 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800144 " 'snapshot_param' is param used for internal snapshot, format\n"
145 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
146 " '[ID_OR_NAME]'\n"
147 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
148 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300149 " '-c' indicates that target image must be compressed (qcow format only)\n"
150 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
151 " match exactly. The image doesn't need a working backing file before\n"
152 " rebasing in this case (useful for renaming the backing file)\n"
153 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200154 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100155 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200156 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
157 " contain only zeros for qemu-img to create a sparse image during\n"
158 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
159 " unallocated or zero sectors, and the destination image will always be\n"
160 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200161 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100162 " '-n' skips the target volume creation (useful if the volume is created\n"
163 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300164 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200165 "Parameters to check subcommand:\n"
166 " '-r' tries to repair any inconsistencies that are found during the check.\n"
167 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
168 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200169 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200170 "\n"
Peter Lieven2d9187b2017-02-28 13:40:07 +0100171 "Parameters to convert subcommand:\n"
172 " '-m' specifies how many coroutines work in parallel during the convert\n"
173 " process (defaults to 8)\n"
174 " '-W' allow to write to the target out of order rather than sequential\n"
175 "\n"
malc3f020d72010-02-08 12:04:56 +0300176 "Parameters to snapshot subcommand:\n"
177 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
178 " '-a' applies a snapshot (revert disk to saved state)\n"
179 " '-c' creates a snapshot\n"
180 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100181 " '-l' lists all snapshots in the given image\n"
182 "\n"
183 "Parameters to compare subcommand:\n"
184 " '-f' first image format\n"
185 " '-F' second image format\n"
Reda Sallahi86ce1f62016-08-10 04:43:12 +0200186 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
187 "\n"
188 "Parameters to dd subcommand:\n"
189 " 'bs=BYTES' read and write up to BYTES bytes at a time "
190 "(default: 512)\n"
191 " 'count=N' copy only N input blocks\n"
192 " 'if=FILE' read from FILE\n"
Reda Sallahif7c15532016-08-10 16:16:09 +0200193 " 'of=FILE' write to FILE\n"
194 " 'skip=N' skip N bs-sized blocks at the start of input\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100195
196 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100197 bdrv_iterate_format(format_print, NULL);
bellardea2384d2004-08-01 21:59:26 +0000198 printf("\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800199 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000200}
201
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000202static QemuOptsList qemu_object_opts = {
203 .name = "object",
204 .implied_opt_name = "qom-type",
205 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
206 .desc = {
207 { }
208 },
209};
210
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000211static QemuOptsList qemu_source_opts = {
212 .name = "source",
213 .implied_opt_name = "file",
214 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
215 .desc = {
216 { }
217 },
218};
219
Stefan Weil7c30f652013-06-16 17:01:05 +0200220static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100221{
222 int ret = 0;
223 if (!quiet) {
224 va_list args;
225 va_start(args, fmt);
226 ret = vprintf(fmt, args);
227 va_end(args);
228 }
229 return ret;
230}
231
bellardea2384d2004-08-01 21:59:26 +0000232
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100233static int print_block_option_help(const char *filename, const char *fmt)
234{
235 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800236 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500237 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100238
239 /* Find driver and parse its options */
240 drv = bdrv_find_format(fmt);
241 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100242 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100243 return 1;
244 }
245
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800246 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100247 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500248 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100249 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100250 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800251 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100252 return 1;
253 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800254 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100255 }
256
Chunyan Liu83d05212014-06-05 17:20:51 +0800257 qemu_opts_print_help(create_opts);
258 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100259 return 0;
260}
261
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000262
263static int img_open_password(BlockBackend *blk, const char *filename,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000264 int flags, bool quiet)
bellard75c23802004-08-27 21:28:58 +0000265{
266 BlockDriverState *bs;
bellard75c23802004-08-27 21:28:58 +0000267 char password[256];
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000268
269 bs = blk_bs(blk);
Daniel P. Berrange4ef130f2016-03-21 14:11:43 +0000270 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs) &&
271 !(flags & BDRV_O_NO_IO)) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000272 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
273 if (qemu_read_password(password, sizeof(password)) < 0) {
274 error_report("No password given");
275 return -1;
276 }
277 if (bdrv_set_key(bs, password) < 0) {
278 error_report("invalid password");
279 return -1;
280 }
281 }
282 return 0;
283}
284
285
Max Reitzefaa7c42016-03-16 19:54:38 +0100286static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100287 QemuOpts *opts, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800288 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000289{
290 QDict *options;
291 Error *local_err = NULL;
292 BlockBackend *blk;
293 options = qemu_opts_to_qdict(opts, NULL);
Fam Zheng335e9932017-05-03 00:35:39 +0800294 if (force_share) {
295 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
296 && !qdict_get_bool(options, BDRV_OPT_FORCE_SHARE)) {
297 error_report("--force-share/-U conflicts with image options");
Fam Zhengadb998c2017-05-15 22:10:14 +0800298 QDECREF(options);
Fam Zheng335e9932017-05-03 00:35:39 +0800299 return NULL;
300 }
Eric Blake579cf1d2017-05-15 14:54:39 -0500301 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800302 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100303 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000304 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100305 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000306 return NULL;
307 }
Kevin Wolfce099542016-03-15 13:01:04 +0100308 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000309
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000310 if (img_open_password(blk, optstr, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000311 blk_unref(blk);
312 return NULL;
313 }
314 return blk;
315}
316
Max Reitzefaa7c42016-03-16 19:54:38 +0100317static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100318 QDict *options,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000319 const char *fmt, int flags,
Fam Zheng335e9932017-05-03 00:35:39 +0800320 bool writethrough, bool quiet,
321 bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000322{
323 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200324 Error *local_err = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100325
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100326 if (!options) {
327 options = qdict_new();
328 }
bellard75c23802004-08-27 21:28:58 +0000329 if (fmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500330 qdict_put_str(options, "driver", fmt);
bellard75c23802004-08-27 21:28:58 +0000331 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100332
Fam Zheng335e9932017-05-03 00:35:39 +0800333 if (force_share) {
Eric Blake579cf1d2017-05-15 14:54:39 -0500334 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800335 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100336 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500337 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100338 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000339 return NULL;
bellard75c23802004-08-27 21:28:58 +0000340 }
Kevin Wolfce099542016-03-15 13:01:04 +0100341 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100342
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000343 if (img_open_password(blk, filename, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000344 blk_unref(blk);
345 return NULL;
bellard75c23802004-08-27 21:28:58 +0000346 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200347 return blk;
bellard75c23802004-08-27 21:28:58 +0000348}
349
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000350
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100351static int img_add_key_secrets(void *opaque,
352 const char *name, const char *value,
353 Error **errp)
354{
355 QDict *options = opaque;
356
357 if (g_str_has_suffix(name, "key-secret")) {
358 qdict_put(options, name, qstring_from_str(value));
359 }
360
361 return 0;
362}
363
364static BlockBackend *img_open_new_file(const char *filename,
365 QemuOpts *create_opts,
366 const char *fmt, int flags,
367 bool writethrough, bool quiet,
368 bool force_share)
369{
370 QDict *options = NULL;
371
372 options = qdict_new();
373 qemu_opt_foreach(create_opts, img_add_key_secrets, options, &error_abort);
374
375 return img_open_file(filename, options, fmt, flags, writethrough, quiet,
376 force_share);
377}
378
379
Max Reitzefaa7c42016-03-16 19:54:38 +0100380static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000381 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100382 const char *fmt, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800383 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000384{
385 BlockBackend *blk;
386 if (image_opts) {
387 QemuOpts *opts;
388 if (fmt) {
389 error_report("--image-opts and --format are mutually exclusive");
390 return NULL;
391 }
392 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
393 filename, true);
394 if (!opts) {
395 return NULL;
396 }
Fam Zheng335e9932017-05-03 00:35:39 +0800397 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
398 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000399 } else {
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100400 blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
Fam Zheng335e9932017-05-03 00:35:39 +0800401 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000402 }
403 return blk;
404}
405
406
Chunyan Liu83d05212014-06-05 17:20:51 +0800407static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100408 const char *base_filename,
409 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200410{
Markus Armbruster6750e792015-02-12 17:43:08 +0100411 Error *err = NULL;
412
Kevin Wolfefa84d42009-05-18 16:42:12 +0200413 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100414 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100415 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100416 error_report("Backing file not supported for file format '%s'",
417 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100418 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900419 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200420 }
421 }
422 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100423 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100424 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100425 error_report("Backing file format not supported for file "
426 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100427 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900428 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200429 }
430 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900431 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200432}
433
Markus Armbruster606caa02017-02-21 21:14:04 +0100434static int64_t cvtnum(const char *s)
435{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100436 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100437 uint64_t value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100438
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100439 err = qemu_strtosz(s, NULL, &value);
440 if (err < 0) {
441 return err;
442 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100443 if (value > INT64_MAX) {
444 return -ERANGE;
445 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100446 return value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100447}
448
bellardea2384d2004-08-01 21:59:26 +0000449static int img_create(int argc, char **argv)
450{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200451 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100452 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000453 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000454 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000455 const char *filename;
456 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200457 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200458 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100459 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000460
bellardea2384d2004-08-01 21:59:26 +0000461 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000462 static const struct option long_options[] = {
463 {"help", no_argument, 0, 'h'},
464 {"object", required_argument, 0, OPTION_OBJECT},
465 {0, 0, 0, 0}
466 };
Daniel P. Berrange6b4df542017-07-04 13:30:09 +0100467 c = getopt_long(argc, argv, ":F:b:f:ho:q",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000468 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100469 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000470 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100471 }
bellardea2384d2004-08-01 21:59:26 +0000472 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800473 case ':':
474 missing_argument(argv[optind - 1]);
475 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100476 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800477 unrecognized_option(argv[optind - 1]);
478 break;
bellardea2384d2004-08-01 21:59:26 +0000479 case 'h':
480 help();
481 break;
aliguori9230eaf2009-03-28 17:55:19 +0000482 case 'F':
483 base_fmt = optarg;
484 break;
bellardea2384d2004-08-01 21:59:26 +0000485 case 'b':
486 base_filename = optarg;
487 break;
488 case 'f':
489 fmt = optarg;
490 break;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200491 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100492 if (!is_valid_option_list(optarg)) {
493 error_report("Invalid option list: %s", optarg);
494 goto fail;
495 }
496 if (!options) {
497 options = g_strdup(optarg);
498 } else {
499 char *old_options = options;
500 options = g_strdup_printf("%s,%s", options, optarg);
501 g_free(old_options);
502 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200503 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100504 case 'q':
505 quiet = true;
506 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000507 case OPTION_OBJECT: {
508 QemuOpts *opts;
509 opts = qemu_opts_parse_noisily(&qemu_object_opts,
510 optarg, true);
511 if (!opts) {
512 goto fail;
513 }
514 } break;
bellardea2384d2004-08-01 21:59:26 +0000515 }
516 }
aliguori9230eaf2009-03-28 17:55:19 +0000517
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900518 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100519 filename = (optind < argc) ? argv[optind] : NULL;
520 if (options && has_help_option(options)) {
521 g_free(options);
522 return print_block_option_help(filename, fmt);
523 }
524
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100525 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800526 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100527 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100528 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900529
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000530 if (qemu_opts_foreach(&qemu_object_opts,
531 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200532 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000533 goto fail;
534 }
535
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100536 /* Get image size, if specified */
537 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100538 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100539
540 sval = cvtnum(argv[optind++]);
541 if (sval < 0) {
liguang79443392012-12-17 09:49:23 +0800542 if (sval == -ERANGE) {
543 error_report("Image size must be less than 8 EiB!");
544 } else {
545 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200546 "G, T, P or E suffixes for ");
547 error_report("kilobytes, megabytes, gigabytes, terabytes, "
548 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800549 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100550 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100551 }
552 img_size = (uint64_t)sval;
553 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200554 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800555 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200556 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100557
Luiz Capitulino9b375252012-11-30 10:52:05 -0200558 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Fam Zheng92172832017-04-21 20:27:01 +0800559 options, img_size, 0, quiet, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100560 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100561 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100562 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900563 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200564
Kevin Wolf77386bf2014-02-21 16:24:04 +0100565 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000566 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100567
568fail:
569 g_free(options);
570 return 1;
bellardea2384d2004-08-01 21:59:26 +0000571}
572
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100573static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500574{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500575 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500576 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100577 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600578
579 visit_type_ImageCheck(v, NULL, &check, &error_abort);
580 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500581 str = qobject_to_json_pretty(obj);
582 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100583 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500584 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600585 visit_free(v);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500586 QDECREF(str);
587}
588
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100589static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500590{
591 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100592 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500593 } else {
594 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100595 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
596 "Data may be corrupted, or further writes to the image "
597 "may corrupt it.\n",
598 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500599 }
600
601 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100602 qprintf(quiet,
603 "\n%" PRId64 " leaked clusters were found on the image.\n"
604 "This means waste of disk space, but no harm to data.\n",
605 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500606 }
607
608 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100609 qprintf(quiet,
610 "\n%" PRId64
611 " internal errors have occurred during the check.\n",
612 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500613 }
614 }
615
616 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100617 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
618 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
619 check->allocated_clusters, check->total_clusters,
620 check->allocated_clusters * 100.0 / check->total_clusters,
621 check->fragmented_clusters * 100.0 / check->allocated_clusters,
622 check->compressed_clusters * 100.0 /
623 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500624 }
625
626 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100627 qprintf(quiet,
628 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500629 }
630}
631
632static int collect_image_check(BlockDriverState *bs,
633 ImageCheck *check,
634 const char *filename,
635 const char *fmt,
636 int fix)
637{
638 int ret;
639 BdrvCheckResult result;
640
641 ret = bdrv_check(bs, &result, fix);
642 if (ret < 0) {
643 return ret;
644 }
645
646 check->filename = g_strdup(filename);
647 check->format = g_strdup(bdrv_get_format_name(bs));
648 check->check_errors = result.check_errors;
649 check->corruptions = result.corruptions;
650 check->has_corruptions = result.corruptions != 0;
651 check->leaks = result.leaks;
652 check->has_leaks = result.leaks != 0;
653 check->corruptions_fixed = result.corruptions_fixed;
654 check->has_corruptions_fixed = result.corruptions != 0;
655 check->leaks_fixed = result.leaks_fixed;
656 check->has_leaks_fixed = result.leaks != 0;
657 check->image_end_offset = result.image_end_offset;
658 check->has_image_end_offset = result.image_end_offset != 0;
659 check->total_clusters = result.bfi.total_clusters;
660 check->has_total_clusters = result.bfi.total_clusters != 0;
661 check->allocated_clusters = result.bfi.allocated_clusters;
662 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
663 check->fragmented_clusters = result.bfi.fragmented_clusters;
664 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100665 check->compressed_clusters = result.bfi.compressed_clusters;
666 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500667
668 return 0;
669}
670
Kevin Wolfe076f332010-06-29 11:43:13 +0200671/*
672 * Checks an image for consistency. Exit codes:
673 *
Max Reitzd6635c42014-06-02 22:15:21 +0200674 * 0 - Check completed, image is good
675 * 1 - Check not completed because of internal errors
676 * 2 - Check completed, image is corrupted
677 * 3 - Check completed, image has leaked clusters, but is good otherwise
678 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200679 */
aliguori15859692009-04-21 23:11:53 +0000680static int img_check(int argc, char **argv)
681{
682 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500683 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200684 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200685 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000686 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200687 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100688 int flags = BDRV_O_CHECK;
689 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200690 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100691 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000692 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +0800693 bool force_share = false;
aliguori15859692009-04-21 23:11:53 +0000694
695 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500696 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200697 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100698
aliguori15859692009-04-21 23:11:53 +0000699 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500700 int option_index = 0;
701 static const struct option long_options[] = {
702 {"help", no_argument, 0, 'h'},
703 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530704 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500705 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000706 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000707 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +0800708 {"force-share", no_argument, 0, 'U'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500709 {0, 0, 0, 0}
710 };
Fam Zheng335e9932017-05-03 00:35:39 +0800711 c = getopt_long(argc, argv, ":hf:r:T:qU",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500712 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100713 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000714 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100715 }
aliguori15859692009-04-21 23:11:53 +0000716 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800717 case ':':
718 missing_argument(argv[optind - 1]);
719 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100720 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800721 unrecognized_option(argv[optind - 1]);
722 break;
aliguori15859692009-04-21 23:11:53 +0000723 case 'h':
724 help();
725 break;
726 case 'f':
727 fmt = optarg;
728 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200729 case 'r':
730 flags |= BDRV_O_RDWR;
731
732 if (!strcmp(optarg, "leaks")) {
733 fix = BDRV_FIX_LEAKS;
734 } else if (!strcmp(optarg, "all")) {
735 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
736 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800737 error_exit("Unknown option value for -r "
738 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200739 }
740 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500741 case OPTION_OUTPUT:
742 output = optarg;
743 break;
Max Reitz40055952014-07-22 22:58:42 +0200744 case 'T':
745 cache = optarg;
746 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100747 case 'q':
748 quiet = true;
749 break;
Fam Zheng335e9932017-05-03 00:35:39 +0800750 case 'U':
751 force_share = true;
752 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000753 case OPTION_OBJECT: {
754 QemuOpts *opts;
755 opts = qemu_opts_parse_noisily(&qemu_object_opts,
756 optarg, true);
757 if (!opts) {
758 return 1;
759 }
760 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000761 case OPTION_IMAGE_OPTS:
762 image_opts = true;
763 break;
aliguori15859692009-04-21 23:11:53 +0000764 }
765 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200766 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800767 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100768 }
aliguori15859692009-04-21 23:11:53 +0000769 filename = argv[optind++];
770
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500771 if (output && !strcmp(output, "json")) {
772 output_format = OFORMAT_JSON;
773 } else if (output && !strcmp(output, "human")) {
774 output_format = OFORMAT_HUMAN;
775 } else if (output) {
776 error_report("--output must be used with human or json as argument.");
777 return 1;
778 }
779
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000780 if (qemu_opts_foreach(&qemu_object_opts,
781 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200782 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000783 return 1;
784 }
785
Kevin Wolfce099542016-03-15 13:01:04 +0100786 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200787 if (ret < 0) {
788 error_report("Invalid source cache option: %s", cache);
789 return 1;
790 }
791
Fam Zheng335e9932017-05-03 00:35:39 +0800792 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
793 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200794 if (!blk) {
795 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900796 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200797 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500798
799 check = g_new0(ImageCheck, 1);
800 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200801
802 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200803 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200804 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500805 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200806 }
807
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500808 if (check->corruptions_fixed || check->leaks_fixed) {
809 int corruptions_fixed, leaks_fixed;
810
811 leaks_fixed = check->leaks_fixed;
812 corruptions_fixed = check->corruptions_fixed;
813
814 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100815 qprintf(quiet,
816 "The following inconsistencies were found and repaired:\n\n"
817 " %" PRId64 " leaked clusters\n"
818 " %" PRId64 " corruptions\n\n"
819 "Double checking the fixed image now...\n",
820 check->leaks_fixed,
821 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500822 }
823
824 ret = collect_image_check(bs, check, filename, fmt, 0);
825
826 check->leaks_fixed = leaks_fixed;
827 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200828 }
829
Max Reitz832390a2014-10-23 15:29:12 +0200830 if (!ret) {
831 switch (output_format) {
832 case OFORMAT_HUMAN:
833 dump_human_image_check(check, quiet);
834 break;
835 case OFORMAT_JSON:
836 dump_json_image_check(check, quiet);
837 break;
838 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500839 }
840
841 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200842 if (ret) {
843 error_report("Check failed: %s", strerror(-ret));
844 } else {
845 error_report("Check failed");
846 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500847 ret = 1;
848 goto fail;
849 }
850
851 if (check->corruptions) {
852 ret = 2;
853 } else if (check->leaks) {
854 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200855 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500856 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000857 }
858
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500859fail:
860 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200861 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500862 return ret;
aliguori15859692009-04-21 23:11:53 +0000863}
864
Max Reitzd4a32382014-10-24 15:57:37 +0200865typedef struct CommonBlockJobCBInfo {
866 BlockDriverState *bs;
867 Error **errp;
868} CommonBlockJobCBInfo;
869
870static void common_block_job_cb(void *opaque, int ret)
871{
872 CommonBlockJobCBInfo *cbi = opaque;
873
874 if (ret < 0) {
875 error_setg_errno(cbi->errp, -ret, "Block job failed");
876 }
Max Reitzd4a32382014-10-24 15:57:37 +0200877}
878
879static void run_block_job(BlockJob *job, Error **errp)
880{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200881 AioContext *aio_context = blk_get_aio_context(job->blk);
sochin.jiang4172a002017-06-15 14:47:33 +0800882 int ret = 0;
Max Reitzd4a32382014-10-24 15:57:37 +0200883
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200884 aio_context_acquire(aio_context);
sochin.jiang4172a002017-06-15 14:47:33 +0800885 block_job_ref(job);
Max Reitzd4a32382014-10-24 15:57:37 +0200886 do {
887 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500888 qemu_progress_print(job->len ?
889 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
sochin.jiang4172a002017-06-15 14:47:33 +0800890 } while (!job->ready && !job->completed);
Max Reitzd4a32382014-10-24 15:57:37 +0200891
sochin.jiang4172a002017-06-15 14:47:33 +0800892 if (!job->completed) {
893 ret = block_job_complete_sync(job, errp);
894 } else {
895 ret = job->ret;
896 }
897 block_job_unref(job);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200898 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200899
sochin.jiang4172a002017-06-15 14:47:33 +0800900 /* publish completion progress only when success */
901 if (!ret) {
902 qemu_progress_print(100.f, 0);
903 }
Max Reitzd4a32382014-10-24 15:57:37 +0200904}
905
bellardea2384d2004-08-01 21:59:26 +0000906static int img_commit(int argc, char **argv)
907{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400908 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200909 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200910 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200911 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100912 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200913 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100914 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200915 Error *local_err = NULL;
916 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000917 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200918 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000919
920 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400921 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200922 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000923 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000924 static const struct option long_options[] = {
925 {"help", no_argument, 0, 'h'},
926 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000927 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000928 {0, 0, 0, 0}
929 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800930 c = getopt_long(argc, argv, ":f:ht:b:dpq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000931 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100932 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000933 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100934 }
bellardea2384d2004-08-01 21:59:26 +0000935 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800936 case ':':
937 missing_argument(argv[optind - 1]);
938 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100939 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800940 unrecognized_option(argv[optind - 1]);
941 break;
bellardea2384d2004-08-01 21:59:26 +0000942 case 'h':
943 help();
944 break;
945 case 'f':
946 fmt = optarg;
947 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400948 case 't':
949 cache = optarg;
950 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200951 case 'b':
952 base = optarg;
953 /* -b implies -d */
954 drop = true;
955 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200956 case 'd':
957 drop = true;
958 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200959 case 'p':
960 progress = true;
961 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100962 case 'q':
963 quiet = true;
964 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000965 case OPTION_OBJECT: {
966 QemuOpts *opts;
967 opts = qemu_opts_parse_noisily(&qemu_object_opts,
968 optarg, true);
969 if (!opts) {
970 return 1;
971 }
972 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000973 case OPTION_IMAGE_OPTS:
974 image_opts = true;
975 break;
bellardea2384d2004-08-01 21:59:26 +0000976 }
977 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200978
979 /* Progress is not shown in Quiet mode */
980 if (quiet) {
981 progress = false;
982 }
983
Kevin Wolffc11eb22013-08-05 10:53:04 +0200984 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800985 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100986 }
bellardea2384d2004-08-01 21:59:26 +0000987 filename = argv[optind++];
988
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000989 if (qemu_opts_foreach(&qemu_object_opts,
990 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200991 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000992 return 1;
993 }
994
Max Reitz9a86fe42014-10-24 15:57:38 +0200995 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100996 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400997 if (ret < 0) {
998 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100999 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001000 }
1001
Fam Zheng335e9932017-05-03 00:35:39 +08001002 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
1003 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001004 if (!blk) {
1005 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001006 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001007 bs = blk_bs(blk);
1008
Max Reitz687fa1d2014-10-24 15:57:39 +02001009 qemu_progress_init(progress, 1.f);
1010 qemu_progress_print(0.f, 100);
1011
Max Reitz1b22bff2014-10-24 15:57:40 +02001012 if (base) {
1013 base_bs = bdrv_find_backing_image(bs, base);
1014 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +01001015 error_setg(&local_err,
1016 "Did not find '%s' in the backing chain of '%s'",
1017 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +02001018 goto done;
1019 }
1020 } else {
1021 /* This is different from QMP, which by default uses the deepest file in
1022 * the backing chain (i.e., the very base); however, the traditional
1023 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +02001024 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +02001025 if (!base_bs) {
1026 error_setg(&local_err, "Image does not have a backing file");
1027 goto done;
1028 }
bellardea2384d2004-08-01 21:59:26 +00001029 }
1030
Max Reitzd4a32382014-10-24 15:57:37 +02001031 cbi = (CommonBlockJobCBInfo){
1032 .errp = &local_err,
1033 .bs = bs,
1034 };
1035
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001036 aio_context = bdrv_get_aio_context(bs);
1037 aio_context_acquire(aio_context);
John Snow47970df2016-10-27 12:06:57 -04001038 commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
Kevin Wolf0db832f2017-02-20 18:10:05 +01001039 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +08001040 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001041 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +02001042 if (local_err) {
1043 goto done;
1044 }
1045
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001046 /* When the block job completes, the BlockBackend reference will point to
1047 * the old backing file. In order to avoid that the top image is already
1048 * deleted, so we can still empty it afterwards, increment the reference
1049 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +02001050 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001051 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001052 }
1053
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001054 job = block_job_get("commit");
1055 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001056 if (local_err) {
1057 goto unref_backing;
1058 }
1059
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001060 if (!drop && bs->drv->bdrv_make_empty) {
1061 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001062 if (ret) {
1063 error_setg_errno(&local_err, -ret, "Could not empty %s",
1064 filename);
1065 goto unref_backing;
1066 }
1067 }
1068
1069unref_backing:
1070 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001071 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001072 }
Max Reitzd4a32382014-10-24 15:57:37 +02001073
1074done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001075 qemu_progress_end();
1076
Markus Armbruster26f54e92014-10-07 13:59:04 +02001077 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001078
1079 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001080 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001081 return 1;
1082 }
Max Reitzd4a32382014-10-24 15:57:37 +02001083
1084 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001085 return 0;
1086}
1087
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001088/*
thsf58c7b32008-06-05 21:53:49 +00001089 * Returns true iff the first sector pointed to by 'buf' contains at least
1090 * a non-NUL byte.
1091 *
1092 * 'pnum' is set to the number of sectors (including and immediately following
1093 * the first one) that are known to be in the same allocated/unallocated state.
1094 */
bellardea2384d2004-08-01 21:59:26 +00001095static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1096{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001097 bool is_zero;
1098 int i;
bellardea2384d2004-08-01 21:59:26 +00001099
1100 if (n <= 0) {
1101 *pnum = 0;
1102 return 0;
1103 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001104 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001105 for(i = 1; i < n; i++) {
1106 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001107 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001108 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001109 }
bellardea2384d2004-08-01 21:59:26 +00001110 }
1111 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001112 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001113}
1114
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001115/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001116 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1117 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1118 * breaking up write requests for only small sparse areas.
1119 */
1120static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1121 int min)
1122{
1123 int ret;
1124 int num_checked, num_used;
1125
1126 if (n < min) {
1127 min = n;
1128 }
1129
1130 ret = is_allocated_sectors(buf, n, pnum);
1131 if (!ret) {
1132 return ret;
1133 }
1134
1135 num_used = *pnum;
1136 buf += BDRV_SECTOR_SIZE * *pnum;
1137 n -= *pnum;
1138 num_checked = num_used;
1139
1140 while (n > 0) {
1141 ret = is_allocated_sectors(buf, n, pnum);
1142
1143 buf += BDRV_SECTOR_SIZE * *pnum;
1144 n -= *pnum;
1145 num_checked += *pnum;
1146 if (ret) {
1147 num_used = num_checked;
1148 } else if (*pnum >= min) {
1149 break;
1150 }
1151 }
1152
1153 *pnum = num_used;
1154 return 1;
1155}
1156
1157/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001158 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1159 * buffers matches, non-zero otherwise.
1160 *
1161 * pnum is set to the number of sectors (including and immediately following
1162 * the first one) that are known to have the same comparison result
1163 */
1164static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1165 int *pnum)
1166{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001167 bool res;
1168 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001169
1170 if (n <= 0) {
1171 *pnum = 0;
1172 return 0;
1173 }
1174
1175 res = !!memcmp(buf1, buf2, 512);
1176 for(i = 1; i < n; i++) {
1177 buf1 += 512;
1178 buf2 += 512;
1179
1180 if (!!memcmp(buf1, buf2, 512) != res) {
1181 break;
1182 }
1183 }
1184
1185 *pnum = i;
1186 return res;
1187}
1188
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001189#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001190
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001191static int64_t sectors_to_bytes(int64_t sectors)
1192{
1193 return sectors << BDRV_SECTOR_BITS;
1194}
1195
1196static int64_t sectors_to_process(int64_t total, int64_t from)
1197{
1198 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1199}
1200
1201/*
1202 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1203 *
1204 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1205 * data and negative value on error.
1206 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001207 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001208 * @param sect_num: Number of first sector to check
1209 * @param sect_count: Number of sectors to check
1210 * @param filename: Name of disk file we are checking (logging purpose)
1211 * @param buffer: Allocated buffer for storing read data
1212 * @param quiet: Flag for quiet mode
1213 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001214static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001215 int sect_count, const char *filename,
1216 uint8_t *buffer, bool quiet)
1217{
1218 int pnum, ret = 0;
Eric Blake91669202016-05-06 10:26:43 -06001219 ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer,
1220 sect_count << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001221 if (ret < 0) {
1222 error_report("Error while reading offset %" PRId64 " of %s: %s",
1223 sectors_to_bytes(sect_num), filename, strerror(-ret));
1224 return ret;
1225 }
1226 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1227 if (ret || pnum != sect_count) {
1228 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1229 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1230 return 1;
1231 }
1232
1233 return 0;
1234}
1235
1236/*
1237 * Compares two images. Exit codes:
1238 *
1239 * 0 - Images are identical
1240 * 1 - Images differ
1241 * >1 - Error occurred
1242 */
1243static int img_compare(int argc, char **argv)
1244{
Max Reitz40055952014-07-22 22:58:42 +02001245 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001246 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001247 BlockDriverState *bs1, *bs2;
1248 int64_t total_sectors1, total_sectors2;
1249 uint8_t *buf1 = NULL, *buf2 = NULL;
1250 int pnum1, pnum2;
1251 int allocated1, allocated2;
1252 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1253 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001254 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001255 bool writethrough;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001256 int64_t total_sectors;
1257 int64_t sector_num = 0;
1258 int64_t nb_sectors;
1259 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001260 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001261 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001262 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001263
Max Reitz40055952014-07-22 22:58:42 +02001264 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001265 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001266 static const struct option long_options[] = {
1267 {"help", no_argument, 0, 'h'},
1268 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001269 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001270 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001271 {0, 0, 0, 0}
1272 };
Fam Zheng335e9932017-05-03 00:35:39 +08001273 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001274 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001275 if (c == -1) {
1276 break;
1277 }
1278 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001279 case ':':
1280 missing_argument(argv[optind - 1]);
1281 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001282 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001283 unrecognized_option(argv[optind - 1]);
1284 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001285 case 'h':
1286 help();
1287 break;
1288 case 'f':
1289 fmt1 = optarg;
1290 break;
1291 case 'F':
1292 fmt2 = optarg;
1293 break;
Max Reitz40055952014-07-22 22:58:42 +02001294 case 'T':
1295 cache = optarg;
1296 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001297 case 'p':
1298 progress = true;
1299 break;
1300 case 'q':
1301 quiet = true;
1302 break;
1303 case 's':
1304 strict = true;
1305 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001306 case 'U':
1307 force_share = true;
1308 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001309 case OPTION_OBJECT: {
1310 QemuOpts *opts;
1311 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1312 optarg, true);
1313 if (!opts) {
1314 ret = 2;
1315 goto out4;
1316 }
1317 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001318 case OPTION_IMAGE_OPTS:
1319 image_opts = true;
1320 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001321 }
1322 }
1323
1324 /* Progress is not shown in Quiet mode */
1325 if (quiet) {
1326 progress = false;
1327 }
1328
1329
Kevin Wolffc11eb22013-08-05 10:53:04 +02001330 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001331 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001332 }
1333 filename1 = argv[optind++];
1334 filename2 = argv[optind++];
1335
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001336 if (qemu_opts_foreach(&qemu_object_opts,
1337 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001338 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001339 ret = 2;
1340 goto out4;
1341 }
1342
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001343 /* Initialize before goto out */
1344 qemu_progress_init(progress, 2.0);
1345
Kevin Wolfce099542016-03-15 13:01:04 +01001346 flags = 0;
1347 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001348 if (ret < 0) {
1349 error_report("Invalid source cache option: %s", cache);
1350 ret = 2;
1351 goto out3;
1352 }
1353
Fam Zheng335e9932017-05-03 00:35:39 +08001354 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1355 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001356 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001357 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001358 goto out3;
1359 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001360
Fam Zheng335e9932017-05-03 00:35:39 +08001361 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1362 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001363 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001364 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001365 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001366 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001367 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001368 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001369
Max Reitzf1d3cd72015-02-05 13:58:18 -05001370 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1371 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1372 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001373 if (total_sectors1 < 0) {
1374 error_report("Can't get size of %s: %s",
1375 filename1, strerror(-total_sectors1));
1376 ret = 4;
1377 goto out;
1378 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001379 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001380 if (total_sectors2 < 0) {
1381 error_report("Can't get size of %s: %s",
1382 filename2, strerror(-total_sectors2));
1383 ret = 4;
1384 goto out;
1385 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001386 total_sectors = MIN(total_sectors1, total_sectors2);
1387 progress_base = MAX(total_sectors1, total_sectors2);
1388
1389 qemu_progress_print(0, 100);
1390
1391 if (strict && total_sectors1 != total_sectors2) {
1392 ret = 1;
1393 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1394 goto out;
1395 }
1396
1397 for (;;) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001398 int64_t status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001399 BlockDriverState *file;
1400
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001401 nb_sectors = sectors_to_process(total_sectors, sector_num);
1402 if (nb_sectors <= 0) {
1403 break;
1404 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001405 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1406 total_sectors1 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001407 &pnum1, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001408 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001409 ret = 3;
1410 error_report("Sector allocation test failed for %s", filename1);
1411 goto out;
1412 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001413 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001414
Fam Zheng25ad8e62016-01-13 16:37:41 +08001415 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1416 total_sectors2 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001417 &pnum2, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001418 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001419 ret = 3;
1420 error_report("Sector allocation test failed for %s", filename2);
1421 goto out;
1422 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001423 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1424 if (pnum1) {
1425 nb_sectors = MIN(nb_sectors, pnum1);
1426 }
1427 if (pnum2) {
1428 nb_sectors = MIN(nb_sectors, pnum2);
1429 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001430
Fam Zheng25ad8e62016-01-13 16:37:41 +08001431 if (strict) {
1432 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1433 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1434 ret = 1;
1435 qprintf(quiet, "Strict mode: Offset %" PRId64
1436 " block status mismatch!\n",
1437 sectors_to_bytes(sector_num));
1438 goto out;
1439 }
1440 }
1441 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1442 nb_sectors = MIN(pnum1, pnum2);
1443 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001444 if (allocated1) {
Eric Blake91669202016-05-06 10:26:43 -06001445 ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1,
1446 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001447 if (ret < 0) {
1448 error_report("Error while reading offset %" PRId64 " of %s:"
1449 " %s", sectors_to_bytes(sector_num), filename1,
1450 strerror(-ret));
1451 ret = 4;
1452 goto out;
1453 }
Eric Blake91669202016-05-06 10:26:43 -06001454 ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2,
1455 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001456 if (ret < 0) {
1457 error_report("Error while reading offset %" PRId64
1458 " of %s: %s", sectors_to_bytes(sector_num),
1459 filename2, strerror(-ret));
1460 ret = 4;
1461 goto out;
1462 }
1463 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1464 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001465 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1466 sectors_to_bytes(
1467 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001468 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001469 goto out;
1470 }
1471 }
1472 } else {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001473
1474 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001475 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001476 filename1, buf1, quiet);
1477 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001478 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001479 filename2, buf1, quiet);
1480 }
1481 if (ret) {
1482 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001483 error_report("Error while reading offset %" PRId64 ": %s",
1484 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001485 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001486 }
1487 goto out;
1488 }
1489 }
1490 sector_num += nb_sectors;
1491 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1492 }
1493
1494 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001495 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001496 int64_t total_sectors_over;
1497 const char *filename_over;
1498
1499 qprintf(quiet, "Warning: Image size mismatch!\n");
1500 if (total_sectors1 > total_sectors2) {
1501 total_sectors_over = total_sectors1;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001502 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001503 filename_over = filename1;
1504 } else {
1505 total_sectors_over = total_sectors2;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001506 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001507 filename_over = filename2;
1508 }
1509
1510 for (;;) {
Eric Blake51b0a482017-07-07 07:44:59 -05001511 int64_t count;
1512
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001513 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1514 if (nb_sectors <= 0) {
1515 break;
1516 }
Eric Blake51b0a482017-07-07 07:44:59 -05001517 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL,
1518 sector_num * BDRV_SECTOR_SIZE,
1519 nb_sectors * BDRV_SECTOR_SIZE,
1520 &count);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001521 if (ret < 0) {
1522 ret = 3;
1523 error_report("Sector allocation test failed for %s",
1524 filename_over);
1525 goto out;
1526
1527 }
Eric Blake51b0a482017-07-07 07:44:59 -05001528 /* TODO relax this once bdrv_is_allocated_above does not enforce
1529 * sector alignment */
1530 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
1531 nb_sectors = count >> BDRV_SECTOR_BITS;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001532 if (ret) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001533 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001534 filename_over, buf1, quiet);
1535 if (ret) {
1536 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001537 error_report("Error while reading offset %" PRId64
1538 " of %s: %s", sectors_to_bytes(sector_num),
1539 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001540 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001541 }
1542 goto out;
1543 }
1544 }
1545 sector_num += nb_sectors;
1546 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1547 }
1548 }
1549
1550 qprintf(quiet, "Images are identical.\n");
1551 ret = 0;
1552
1553out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001554 qemu_vfree(buf1);
1555 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001556 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001557out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001558 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001559out3:
1560 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001561out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001562 return ret;
1563}
1564
Kevin Wolf690c7302015-03-19 13:33:32 +01001565enum ImgConvertBlockStatus {
1566 BLK_DATA,
1567 BLK_ZERO,
1568 BLK_BACKING_FILE,
1569};
1570
Peter Lieven2d9187b2017-02-28 13:40:07 +01001571#define MAX_COROUTINES 16
1572
Kevin Wolf690c7302015-03-19 13:33:32 +01001573typedef struct ImgConvertState {
1574 BlockBackend **src;
1575 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001576 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001577 int64_t total_sectors;
1578 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001579 int64_t allocated_done;
1580 int64_t sector_num;
1581 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001582 enum ImgConvertBlockStatus status;
1583 int64_t sector_next_status;
1584 BlockBackend *target;
1585 bool has_zero_init;
1586 bool compressed;
1587 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001588 bool wr_in_order;
Kevin Wolf690c7302015-03-19 13:33:32 +01001589 int min_sparse;
1590 size_t cluster_sectors;
1591 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001592 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001593 int running_coroutines;
1594 Coroutine *co[MAX_COROUTINES];
1595 int64_t wait_sector_num[MAX_COROUTINES];
1596 CoMutex lock;
1597 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001598} ImgConvertState;
1599
Peter Lieven2d9187b2017-02-28 13:40:07 +01001600static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1601 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001602{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001603 *src_cur = 0;
1604 *src_cur_offset = 0;
1605 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1606 *src_cur_offset += s->src_sectors[*src_cur];
1607 (*src_cur)++;
1608 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001609 }
1610}
1611
1612static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1613{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001614 int64_t ret, src_cur_offset;
1615 int n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001616
Peter Lieven2d9187b2017-02-28 13:40:07 +01001617 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001618
1619 assert(s->total_sectors > sector_num);
1620 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1621
1622 if (s->sector_next_status <= sector_num) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08001623 BlockDriverState *file;
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001624 if (s->target_has_backing) {
1625 ret = bdrv_get_block_status(blk_bs(s->src[src_cur]),
1626 sector_num - src_cur_offset,
1627 n, &n, &file);
1628 } else {
1629 ret = bdrv_get_block_status_above(blk_bs(s->src[src_cur]), NULL,
1630 sector_num - src_cur_offset,
1631 n, &n, &file);
1632 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001633 if (ret < 0) {
1634 return ret;
1635 }
1636
1637 if (ret & BDRV_BLOCK_ZERO) {
1638 s->status = BLK_ZERO;
1639 } else if (ret & BDRV_BLOCK_DATA) {
1640 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001641 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001642 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001643 }
1644
1645 s->sector_next_status = sector_num + n;
1646 }
1647
1648 n = MIN(n, s->sector_next_status - sector_num);
1649 if (s->status == BLK_DATA) {
1650 n = MIN(n, s->buf_sectors);
1651 }
1652
1653 /* We need to write complete clusters for compressed images, so if an
1654 * unallocated area is shorter than that, we must consider the whole
1655 * cluster allocated. */
1656 if (s->compressed) {
1657 if (n < s->cluster_sectors) {
1658 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1659 s->status = BLK_DATA;
1660 } else {
1661 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1662 }
1663 }
1664
1665 return n;
1666}
1667
Peter Lieven2d9187b2017-02-28 13:40:07 +01001668static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1669 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001670{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001671 int n, ret;
1672 QEMUIOVector qiov;
1673 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001674
Kevin Wolf690c7302015-03-19 13:33:32 +01001675 assert(nb_sectors <= s->buf_sectors);
1676 while (nb_sectors > 0) {
1677 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001678 int src_cur;
1679 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001680
1681 /* In the case of compression with multiple source files, we can get a
1682 * nb_sectors that spreads into the next part. So we must be able to
1683 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001684 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1685 blk = s->src[src_cur];
1686 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001687
Peter Lieven2d9187b2017-02-28 13:40:07 +01001688 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1689 iov.iov_base = buf;
1690 iov.iov_len = n << BDRV_SECTOR_BITS;
1691 qemu_iovec_init_external(&qiov, &iov, 1);
1692
1693 ret = blk_co_preadv(
1694 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1695 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001696 if (ret < 0) {
1697 return ret;
1698 }
1699
1700 sector_num += n;
1701 nb_sectors -= n;
1702 buf += n * BDRV_SECTOR_SIZE;
1703 }
1704
1705 return 0;
1706}
1707
Peter Lieven2d9187b2017-02-28 13:40:07 +01001708
1709static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1710 int nb_sectors, uint8_t *buf,
1711 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001712{
1713 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001714 QEMUIOVector qiov;
1715 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001716
1717 while (nb_sectors > 0) {
1718 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001719 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1720
Peter Lieven2d9187b2017-02-28 13:40:07 +01001721 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001722 case BLK_BACKING_FILE:
1723 /* If we have a backing file, leave clusters unallocated that are
1724 * unallocated in the source image, so that the backing file is
1725 * visible at the respective offset. */
1726 assert(s->target_has_backing);
1727 break;
1728
1729 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001730 /* If we're told to keep the target fully allocated (-S 0) or there
1731 * is real non-zero data, we must write it. Otherwise we can treat
1732 * it as zero sectors.
1733 * Compressed clusters need to be written as a whole, so in that
1734 * case we can only save the write if the buffer is completely
1735 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001736 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001737 (!s->compressed &&
1738 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1739 (s->compressed &&
1740 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001741 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001742 iov.iov_base = buf;
1743 iov.iov_len = n << BDRV_SECTOR_BITS;
1744 qemu_iovec_init_external(&qiov, &iov, 1);
1745
1746 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
Lidong Chendb933fb2017-04-27 10:58:27 +08001747 n << BDRV_SECTOR_BITS, &qiov, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001748 if (ret < 0) {
1749 return ret;
1750 }
1751 break;
1752 }
1753 /* fall-through */
1754
1755 case BLK_ZERO:
1756 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001757 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001758 break;
1759 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001760 ret = blk_co_pwrite_zeroes(s->target,
1761 sector_num << BDRV_SECTOR_BITS,
1762 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001763 if (ret < 0) {
1764 return ret;
1765 }
1766 break;
1767 }
1768
1769 sector_num += n;
1770 nb_sectors -= n;
1771 buf += n * BDRV_SECTOR_SIZE;
1772 }
1773
1774 return 0;
1775}
1776
Peter Lieven2d9187b2017-02-28 13:40:07 +01001777static void coroutine_fn convert_co_do_copy(void *opaque)
1778{
1779 ImgConvertState *s = opaque;
1780 uint8_t *buf = NULL;
1781 int ret, i;
1782 int index = -1;
1783
1784 for (i = 0; i < s->num_coroutines; i++) {
1785 if (s->co[i] == qemu_coroutine_self()) {
1786 index = i;
1787 break;
1788 }
1789 }
1790 assert(index >= 0);
1791
1792 s->running_coroutines++;
1793 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1794
1795 while (1) {
1796 int n;
1797 int64_t sector_num;
1798 enum ImgConvertBlockStatus status;
1799
1800 qemu_co_mutex_lock(&s->lock);
1801 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1802 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001803 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001804 }
1805 n = convert_iteration_sectors(s, s->sector_num);
1806 if (n < 0) {
1807 qemu_co_mutex_unlock(&s->lock);
1808 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001809 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001810 }
1811 /* save current sector and allocation status to local variables */
1812 sector_num = s->sector_num;
1813 status = s->status;
1814 if (!s->min_sparse && s->status == BLK_ZERO) {
1815 n = MIN(n, s->buf_sectors);
1816 }
1817 /* increment global sector counter so that other coroutines can
1818 * already continue reading beyond this request */
1819 s->sector_num += n;
1820 qemu_co_mutex_unlock(&s->lock);
1821
1822 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1823 s->allocated_done += n;
1824 qemu_progress_print(100.0 * s->allocated_done /
1825 s->allocated_sectors, 0);
1826 }
1827
1828 if (status == BLK_DATA) {
1829 ret = convert_co_read(s, sector_num, n, buf);
1830 if (ret < 0) {
1831 error_report("error while reading sector %" PRId64
1832 ": %s", sector_num, strerror(-ret));
1833 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001834 }
1835 } else if (!s->min_sparse && status == BLK_ZERO) {
1836 status = BLK_DATA;
1837 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1838 }
1839
1840 if (s->wr_in_order) {
1841 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03001842 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001843 s->wait_sector_num[index] = sector_num;
1844 qemu_coroutine_yield();
1845 }
1846 s->wait_sector_num[index] = -1;
1847 }
1848
Anton Nefedovb91127e2017-04-26 11:33:15 +03001849 if (s->ret == -EINPROGRESS) {
1850 ret = convert_co_write(s, sector_num, n, buf, status);
1851 if (ret < 0) {
1852 error_report("error while writing sector %" PRId64
1853 ": %s", sector_num, strerror(-ret));
1854 s->ret = ret;
1855 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001856 }
1857
1858 if (s->wr_in_order) {
1859 /* reenter the coroutine that might have waited
1860 * for this write to complete */
1861 s->wr_offs = sector_num + n;
1862 for (i = 0; i < s->num_coroutines; i++) {
1863 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1864 /*
1865 * A -> B -> A cannot occur because A has
1866 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1867 * B will never enter A during this time window.
1868 */
1869 qemu_coroutine_enter(s->co[i]);
1870 break;
1871 }
1872 }
1873 }
1874 }
1875
Peter Lieven2d9187b2017-02-28 13:40:07 +01001876 qemu_vfree(buf);
1877 s->co[index] = NULL;
1878 s->running_coroutines--;
1879 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1880 /* the convert job finished successfully */
1881 s->ret = 0;
1882 }
1883}
1884
Kevin Wolf690c7302015-03-19 13:33:32 +01001885static int convert_do_copy(ImgConvertState *s)
1886{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001887 int ret, i, n;
1888 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001889
1890 /* Check whether we have zero initialisation or can get it efficiently */
1891 s->has_zero_init = s->min_sparse && !s->target_has_backing
1892 ? bdrv_has_zero_init(blk_bs(s->target))
1893 : false;
1894
1895 if (!s->has_zero_init && !s->target_has_backing &&
1896 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1897 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001898 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001899 if (ret == 0) {
1900 s->has_zero_init = true;
1901 }
1902 }
1903
1904 /* Allocate buffer for copied data. For compressed images, only one cluster
1905 * can be copied at a time. */
1906 if (s->compressed) {
1907 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1908 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001909 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001910 }
1911 s->buf_sectors = s->cluster_sectors;
1912 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001913
Kevin Wolf690c7302015-03-19 13:33:32 +01001914 while (sector_num < s->total_sectors) {
1915 n = convert_iteration_sectors(s, sector_num);
1916 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001917 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001918 }
Max Reitzaad15de2016-03-24 23:33:57 +01001919 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1920 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001921 s->allocated_sectors += n;
1922 }
1923 sector_num += n;
1924 }
1925
1926 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001927 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001928 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001929
Peter Lieven2d9187b2017-02-28 13:40:07 +01001930 qemu_co_mutex_init(&s->lock);
1931 for (i = 0; i < s->num_coroutines; i++) {
1932 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1933 s->wait_sector_num[i] = -1;
1934 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001935 }
1936
Anton Nefedovb91127e2017-04-26 11:33:15 +03001937 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001938 main_loop_wait(false);
1939 }
1940
1941 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001942 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001943 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001944 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001945 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001946 }
1947 }
1948
Peter Lieven2d9187b2017-02-28 13:40:07 +01001949 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001950}
1951
bellardea2384d2004-08-01 21:59:26 +00001952static int img_convert(int argc, char **argv)
1953{
Peter Lieven9fd77f92017-04-21 11:11:55 +02001954 int c, bs_i, flags, src_flags = 0;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001955 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02001956 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1957 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001958 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00001959 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001960 BlockDriverState *out_bs;
1961 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08001962 QemuOptsList *create_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001963 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02001964 Error *local_err = NULL;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001965 bool writethrough, src_writethrough, quiet = false, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001966 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001967 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08001968 bool force_share = false;
bellardea2384d2004-08-01 21:59:26 +00001969
Peter Lieven9fd77f92017-04-21 11:11:55 +02001970 ImgConvertState s = (ImgConvertState) {
1971 /* Need at least 4k of zeros for sparse detection */
1972 .min_sparse = 8,
1973 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1974 .wr_in_order = true,
1975 .num_coroutines = 8,
1976 };
1977
bellardea2384d2004-08-01 21:59:26 +00001978 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001979 static const struct option long_options[] = {
1980 {"help", no_argument, 0, 'h'},
1981 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001982 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001983 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001984 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001985 {0, 0, 0, 0}
1986 };
Daniel P. Berrange6b4df542017-07-04 13:30:09 +01001987 c = getopt_long(argc, argv, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001988 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001989 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001990 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001991 }
bellardea2384d2004-08-01 21:59:26 +00001992 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001993 case ':':
1994 missing_argument(argv[optind - 1]);
1995 break;
Jes Sorensenef873942010-12-06 15:25:40 +01001996 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001997 unrecognized_option(argv[optind - 1]);
1998 break;
bellardea2384d2004-08-01 21:59:26 +00001999 case 'h':
2000 help();
2001 break;
2002 case 'f':
2003 fmt = optarg;
2004 break;
2005 case 'O':
2006 out_fmt = optarg;
2007 break;
thsf58c7b32008-06-05 21:53:49 +00002008 case 'B':
2009 out_baseimg = optarg;
2010 break;
bellardea2384d2004-08-01 21:59:26 +00002011 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002012 s.compressed = true;
bellardea2384d2004-08-01 21:59:26 +00002013 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002014 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01002015 if (!is_valid_option_list(optarg)) {
2016 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002017 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01002018 }
2019 if (!options) {
2020 options = g_strdup(optarg);
2021 } else {
2022 char *old_options = options;
2023 options = g_strdup_printf("%s,%s", options, optarg);
2024 g_free(old_options);
2025 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002026 break;
edison51ef6722010-09-21 19:58:41 -07002027 case 's':
2028 snapshot_name = optarg;
2029 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08002030 case 'l':
2031 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01002032 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2033 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08002034 if (!sn_opts) {
2035 error_report("Failed in parsing snapshot param '%s'",
2036 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002037 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002038 }
2039 } else {
2040 snapshot_name = optarg;
2041 }
2042 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002043 case 'S':
2044 {
2045 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002046
2047 sval = cvtnum(optarg);
2048 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02002049 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002050 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002051 }
2052
Peter Lieven9fd77f92017-04-21 11:11:55 +02002053 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002054 break;
2055 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002056 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002057 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002058 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002059 case 't':
2060 cache = optarg;
2061 break;
Max Reitz40055952014-07-22 22:58:42 +02002062 case 'T':
2063 src_cache = optarg;
2064 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002065 case 'q':
2066 quiet = true;
2067 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002068 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002069 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002070 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002071 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002072 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2073 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002074 error_report("Invalid number of coroutines. Allowed number of"
2075 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002076 goto fail_getopt;
2077 }
2078 break;
2079 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002080 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002081 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002082 case 'U':
2083 force_share = true;
2084 break;
Max Reitz3258b912017-04-26 15:46:47 +02002085 case OPTION_OBJECT: {
2086 QemuOpts *object_opts;
2087 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2088 optarg, true);
2089 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002090 goto fail_getopt;
2091 }
2092 break;
Max Reitz3258b912017-04-26 15:46:47 +02002093 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002094 case OPTION_IMAGE_OPTS:
2095 image_opts = true;
2096 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002097 case OPTION_TARGET_IMAGE_OPTS:
2098 tgt_image_opts = true;
2099 break;
bellardea2384d2004-08-01 21:59:26 +00002100 }
2101 }
ths3b46e622007-09-17 08:09:54 +00002102
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002103 if (!out_fmt && !tgt_image_opts) {
2104 out_fmt = "raw";
2105 }
2106
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002107 if (qemu_opts_foreach(&qemu_object_opts,
2108 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002109 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002110 goto fail_getopt;
2111 }
2112
Peter Lieven9fd77f92017-04-21 11:11:55 +02002113 if (!s.wr_in_order && s.compressed) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002114 error_report("Out of order write and compress are mutually exclusive");
Peter Lieven9fd77f92017-04-21 11:11:55 +02002115 goto fail_getopt;
2116 }
2117
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002118 if (tgt_image_opts && !skip_create) {
2119 error_report("--target-image-opts requires use of -n flag");
2120 goto fail_getopt;
2121 }
2122
Peter Lieven9fd77f92017-04-21 11:11:55 +02002123 s.src_num = argc - optind - 1;
2124 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2125
2126 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002127 if (out_fmt) {
2128 ret = print_block_option_help(out_filename, out_fmt);
2129 goto fail_getopt;
2130 } else {
2131 error_report("Option help requires a format be specified");
2132 goto fail_getopt;
2133 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002134 }
2135
2136 if (s.src_num < 1) {
2137 error_report("Must specify image file name");
2138 goto fail_getopt;
2139 }
2140
2141
Peter Lieven9fd77f92017-04-21 11:11:55 +02002142 /* ret is still -EINVAL until here */
2143 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2144 if (ret < 0) {
2145 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002146 goto fail_getopt;
2147 }
2148
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002149 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002150 if (quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002151 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002152 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002153 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002154 qemu_progress_print(0, 100);
2155
Peter Lieven9fd77f92017-04-21 11:11:55 +02002156 s.src = g_new0(BlockBackend *, s.src_num);
2157 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002158
Peter Lieven9fd77f92017-04-21 11:11:55 +02002159 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2160 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Fam Zheng335e9932017-05-03 00:35:39 +08002161 fmt, src_flags, src_writethrough, quiet,
2162 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002163 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002164 ret = -1;
2165 goto out;
2166 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002167 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2168 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002169 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002170 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002171 ret = -1;
2172 goto out;
2173 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002174 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002175 }
bellardea2384d2004-08-01 21:59:26 +00002176
Wenchao Xiaef806542013-12-04 17:10:57 +08002177 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002178 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002179 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2180 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2181 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002182 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002183 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002184 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002185 ret = -1;
2186 goto out;
2187 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002188
Peter Lieven9fd77f92017-04-21 11:11:55 +02002189 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2190 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002191 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002192 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002193 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002194 ret = -1;
2195 goto out;
edison51ef6722010-09-21 19:58:41 -07002196 }
2197
Max Reitz2e024cd2015-02-11 09:58:46 -05002198 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002199 /* Find driver and parse its options */
2200 drv = bdrv_find_format(out_fmt);
2201 if (!drv) {
2202 error_report("Unknown file format '%s'", out_fmt);
2203 ret = -1;
2204 goto out;
2205 }
2206
2207 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2208 if (!proto_drv) {
2209 error_report_err(local_err);
2210 ret = -1;
2211 goto out;
2212 }
2213
Max Reitz2e024cd2015-02-11 09:58:46 -05002214 if (!drv->create_opts) {
2215 error_report("Format driver '%s' does not support image creation",
2216 drv->format_name);
2217 ret = -1;
2218 goto out;
2219 }
Max Reitzf75613c2014-12-02 18:32:46 +01002220
Max Reitz2e024cd2015-02-11 09:58:46 -05002221 if (!proto_drv->create_opts) {
2222 error_report("Protocol driver '%s' does not support image creation",
2223 proto_drv->format_name);
2224 ret = -1;
2225 goto out;
2226 }
Max Reitzf75613c2014-12-02 18:32:46 +01002227
Max Reitz2e024cd2015-02-11 09:58:46 -05002228 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2229 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002230
Max Reitz2e024cd2015-02-11 09:58:46 -05002231 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002232 if (options) {
2233 qemu_opts_do_parse(opts, options, NULL, &local_err);
2234 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002235 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002236 ret = -1;
2237 goto out;
2238 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002239 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002240
Peter Lieven9fd77f92017-04-21 11:11:55 +02002241 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002242 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002243 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2244 if (ret < 0) {
2245 goto out;
2246 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002247 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002248
Kevin Wolfa18953f2010-10-14 15:46:04 +02002249 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002250 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002251 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002252 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002253 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002254 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002255
Max Reitz48758a82017-04-26 15:46:48 +02002256 if (s.src_num > 1 && out_baseimg) {
2257 error_report("Having a backing file for the target makes no sense when "
2258 "concatenating multiple input images");
2259 ret = -1;
2260 goto out;
2261 }
2262
Kevin Wolfefa84d42009-05-18 16:42:12 +02002263 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002264 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002265 bool encryption =
2266 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
2267 const char *preallocation =
2268 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002269
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002270 if (drv && !drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002271 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002272 ret = -1;
2273 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002274 }
2275
Chunyan Liu83d05212014-06-05 17:20:51 +08002276 if (encryption) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002277 error_report("Compression and encryption not supported at "
2278 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002279 ret = -1;
2280 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002281 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002282
Chunyan Liu83d05212014-06-05 17:20:51 +08002283 if (preallocation
2284 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002285 {
2286 error_report("Compression and preallocation not supported at "
2287 "the same time");
2288 ret = -1;
2289 goto out;
2290 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002291 }
2292
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002293 if (!skip_create) {
2294 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002295 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002296 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002297 error_reportf_err(local_err, "%s: error while converting %s: ",
2298 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002299 goto out;
bellardea2384d2004-08-01 21:59:26 +00002300 }
2301 }
ths3b46e622007-09-17 08:09:54 +00002302
Peter Lieven9fd77f92017-04-21 11:11:55 +02002303 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002304 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002305 if (ret < 0) {
2306 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002307 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002308 }
2309
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002310 if (skip_create) {
2311 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2312 flags, writethrough, quiet, false);
2313 } else {
2314 /* TODO ultimately we should allow --target-image-opts
2315 * to be used even when -n is not given.
2316 * That has to wait for bdrv_create to be improved
2317 * to allow filenames in option syntax
2318 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01002319 s.target = img_open_new_file(out_filename, opts, out_fmt,
2320 flags, writethrough, quiet, false);
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002321 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002322 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002323 ret = -1;
2324 goto out;
2325 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002326 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002327
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002328 if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2329 error_report("Compression not supported for this file format");
2330 ret = -1;
2331 goto out;
2332 }
2333
Eric Blake5def6b82016-06-23 16:37:19 -06002334 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002335 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2336 * as maximum. */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002337 s.buf_sectors = MIN(32768,
2338 MAX(s.buf_sectors,
2339 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2340 out_bs->bl.pdiscard_alignment >>
2341 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002342
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002343 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002344 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002345 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002346 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002347 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002348 ret = -1;
2349 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002350 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002351 error_report("output file is smaller than input file");
2352 ret = -1;
2353 goto out;
2354 }
2355 }
2356
Peter Lieven24f833c2013-11-27 11:07:07 +01002357 ret = bdrv_get_info(out_bs, &bdi);
2358 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002359 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002360 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002361 goto out;
2362 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002363 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002364 s.compressed = s.compressed || bdi.needs_compressed_writes;
2365 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Peter Lieven24f833c2013-11-27 11:07:07 +01002366 }
2367
Peter Lieven9fd77f92017-04-21 11:11:55 +02002368 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002369out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002370 if (!ret) {
2371 qemu_progress_print(100, 0);
2372 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002373 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002374 qemu_opts_del(opts);
2375 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002376 qemu_opts_del(sn_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002377 blk_unref(s.target);
2378 if (s.src) {
2379 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2380 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002381 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002382 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002383 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002384 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002385fail_getopt:
2386 g_free(options);
2387
Peter Lieven9fd77f92017-04-21 11:11:55 +02002388 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002389}
2390
bellard57d1a2b2004-08-03 21:15:11 +00002391
bellardfaea38e2006-08-05 21:31:00 +00002392static void dump_snapshots(BlockDriverState *bs)
2393{
2394 QEMUSnapshotInfo *sn_tab, *sn;
2395 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002396
2397 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2398 if (nb_sns <= 0)
2399 return;
2400 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002401 bdrv_snapshot_dump(fprintf, stdout, NULL);
2402 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002403 for(i = 0; i < nb_sns; i++) {
2404 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002405 bdrv_snapshot_dump(fprintf, stdout, sn);
2406 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002407 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002408 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002409}
2410
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002411static void dump_json_image_info_list(ImageInfoList *list)
2412{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002413 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002414 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002415 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002416
2417 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2418 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002419 str = qobject_to_json_pretty(obj);
2420 assert(str != NULL);
2421 printf("%s\n", qstring_get_str(str));
2422 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002423 visit_free(v);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002424 QDECREF(str);
2425}
2426
Benoît Canetc054b3f2012-09-05 13:09:02 +02002427static void dump_json_image_info(ImageInfo *info)
2428{
Benoît Canetc054b3f2012-09-05 13:09:02 +02002429 QString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002430 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002431 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002432
2433 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2434 visit_complete(v, &obj);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002435 str = qobject_to_json_pretty(obj);
2436 assert(str != NULL);
2437 printf("%s\n", qstring_get_str(str));
2438 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002439 visit_free(v);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002440 QDECREF(str);
2441}
2442
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002443static void dump_human_image_info_list(ImageInfoList *list)
2444{
2445 ImageInfoList *elem;
2446 bool delim = false;
2447
2448 for (elem = list; elem; elem = elem->next) {
2449 if (delim) {
2450 printf("\n");
2451 }
2452 delim = true;
2453
Wenchao Xia5b917042013-05-25 11:09:45 +08002454 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002455 }
2456}
2457
2458static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2459{
2460 return strcmp(a, b) == 0;
2461}
2462
2463/**
2464 * Open an image file chain and return an ImageInfoList
2465 *
2466 * @filename: topmost image filename
2467 * @fmt: topmost image format (may be NULL to autodetect)
2468 * @chain: true - enumerate entire backing file chain
2469 * false - only topmost image file
2470 *
2471 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2472 * image file. If there was an error a message will have been printed to
2473 * stderr.
2474 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002475static ImageInfoList *collect_image_info_list(bool image_opts,
2476 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002477 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002478 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002479{
2480 ImageInfoList *head = NULL;
2481 ImageInfoList **last = &head;
2482 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002483 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002484
2485 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2486
2487 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002488 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002489 BlockDriverState *bs;
2490 ImageInfo *info;
2491 ImageInfoList *elem;
2492
2493 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2494 error_report("Backing file '%s' creates an infinite loop.",
2495 filename);
2496 goto err;
2497 }
2498 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2499
Max Reitzefaa7c42016-03-16 19:54:38 +01002500 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002501 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2502 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002503 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002504 goto err;
2505 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002506 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002507
Wenchao Xia43526ec2013-06-06 12:27:58 +08002508 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002509 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002510 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002511 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002512 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002513 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002514
2515 elem = g_new0(ImageInfoList, 1);
2516 elem->value = info;
2517 *last = elem;
2518 last = &elem->next;
2519
Markus Armbruster26f54e92014-10-07 13:59:04 +02002520 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002521
2522 filename = fmt = NULL;
2523 if (chain) {
2524 if (info->has_full_backing_filename) {
2525 filename = info->full_backing_filename;
2526 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002527 error_report("Could not determine absolute backing filename,"
2528 " but backing filename '%s' present",
2529 info->backing_filename);
2530 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002531 }
2532 if (info->has_backing_filename_format) {
2533 fmt = info->backing_filename_format;
2534 }
2535 }
2536 }
2537 g_hash_table_destroy(filenames);
2538 return head;
2539
2540err:
2541 qapi_free_ImageInfoList(head);
2542 g_hash_table_destroy(filenames);
2543 return NULL;
2544}
2545
Benoît Canetc054b3f2012-09-05 13:09:02 +02002546static int img_info(int argc, char **argv)
2547{
2548 int c;
2549 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002550 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002551 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002552 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002553 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002554 bool force_share = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002555
bellardea2384d2004-08-01 21:59:26 +00002556 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002557 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002558 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002559 int option_index = 0;
2560 static const struct option long_options[] = {
2561 {"help", no_argument, 0, 'h'},
2562 {"format", required_argument, 0, 'f'},
2563 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002564 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002565 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002566 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002567 {"force-share", no_argument, 0, 'U'},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002568 {0, 0, 0, 0}
2569 };
Fam Zheng335e9932017-05-03 00:35:39 +08002570 c = getopt_long(argc, argv, ":f:hU",
Benoît Canetc054b3f2012-09-05 13:09:02 +02002571 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002572 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002573 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002574 }
bellardea2384d2004-08-01 21:59:26 +00002575 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002576 case ':':
2577 missing_argument(argv[optind - 1]);
2578 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002579 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002580 unrecognized_option(argv[optind - 1]);
2581 break;
bellardea2384d2004-08-01 21:59:26 +00002582 case 'h':
2583 help();
2584 break;
2585 case 'f':
2586 fmt = optarg;
2587 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002588 case 'U':
2589 force_share = true;
2590 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002591 case OPTION_OUTPUT:
2592 output = optarg;
2593 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002594 case OPTION_BACKING_CHAIN:
2595 chain = true;
2596 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002597 case OPTION_OBJECT: {
2598 QemuOpts *opts;
2599 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2600 optarg, true);
2601 if (!opts) {
2602 return 1;
2603 }
2604 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002605 case OPTION_IMAGE_OPTS:
2606 image_opts = true;
2607 break;
bellardea2384d2004-08-01 21:59:26 +00002608 }
2609 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002610 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002611 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002612 }
bellardea2384d2004-08-01 21:59:26 +00002613 filename = argv[optind++];
2614
Benoît Canetc054b3f2012-09-05 13:09:02 +02002615 if (output && !strcmp(output, "json")) {
2616 output_format = OFORMAT_JSON;
2617 } else if (output && !strcmp(output, "human")) {
2618 output_format = OFORMAT_HUMAN;
2619 } else if (output) {
2620 error_report("--output must be used with human or json as argument.");
2621 return 1;
2622 }
2623
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002624 if (qemu_opts_foreach(&qemu_object_opts,
2625 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002626 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002627 return 1;
2628 }
2629
Fam Zheng335e9932017-05-03 00:35:39 +08002630 list = collect_image_info_list(image_opts, filename, fmt, chain,
2631 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002632 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002633 return 1;
2634 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002635
Benoît Canetc054b3f2012-09-05 13:09:02 +02002636 switch (output_format) {
2637 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002638 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002639 break;
2640 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002641 if (chain) {
2642 dump_json_image_info_list(list);
2643 } else {
2644 dump_json_image_info(list->value);
2645 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002646 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002647 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002648
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002649 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002650 return 0;
2651}
2652
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002653static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2654 MapEntry *next)
2655{
2656 switch (output_format) {
2657 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002658 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002659 error_report("File contains external, encrypted or compressed clusters.");
2660 exit(1);
2661 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002662 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002663 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002664 e->start, e->length,
2665 e->has_offset ? e->offset : 0,
2666 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002667 }
2668 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2669 * Modify the flags here to allow more coalescing.
2670 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002671 if (next && (!next->data || next->zero)) {
2672 next->data = false;
2673 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002674 }
2675 break;
2676 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002677 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2678 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002679 (e->start == 0 ? "[" : ",\n"),
2680 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002681 e->zero ? "true" : "false",
2682 e->data ? "true" : "false");
2683 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002684 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002685 }
2686 putchar('}');
2687
2688 if (!next) {
2689 printf("]\n");
2690 }
2691 break;
2692 }
2693}
2694
2695static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2696 int nb_sectors, MapEntry *e)
2697{
2698 int64_t ret;
2699 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002700 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002701 bool has_offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002702
2703 /* As an optimization, we could cache the current range of unallocated
2704 * clusters in each file of the chain, and avoid querying the same
2705 * range repeatedly.
2706 */
2707
2708 depth = 0;
2709 for (;;) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08002710 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2711 &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002712 if (ret < 0) {
2713 return ret;
2714 }
2715 assert(nb_sectors);
2716 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2717 break;
2718 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002719 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002720 if (bs == NULL) {
2721 ret = 0;
2722 break;
2723 }
2724
2725 depth++;
2726 }
2727
John Snow28756452016-02-05 13:12:33 -05002728 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2729
2730 *e = (MapEntry) {
2731 .start = sector_num * BDRV_SECTOR_SIZE,
2732 .length = nb_sectors * BDRV_SECTOR_SIZE,
2733 .data = !!(ret & BDRV_BLOCK_DATA),
2734 .zero = !!(ret & BDRV_BLOCK_ZERO),
2735 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2736 .has_offset = has_offset,
2737 .depth = depth,
2738 .has_filename = file && has_offset,
2739 .filename = file && has_offset ? file->filename : NULL,
2740 };
2741
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002742 return 0;
2743}
2744
Fam Zheng16b0d552016-01-26 11:59:02 +08002745static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2746{
2747 if (curr->length == 0) {
2748 return false;
2749 }
2750 if (curr->zero != next->zero ||
2751 curr->data != next->data ||
2752 curr->depth != next->depth ||
2753 curr->has_filename != next->has_filename ||
2754 curr->has_offset != next->has_offset) {
2755 return false;
2756 }
2757 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2758 return false;
2759 }
2760 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2761 return false;
2762 }
2763 return true;
2764}
2765
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002766static int img_map(int argc, char **argv)
2767{
2768 int c;
2769 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002770 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002771 BlockDriverState *bs;
2772 const char *filename, *fmt, *output;
2773 int64_t length;
2774 MapEntry curr = { .length = 0 }, next;
2775 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002776 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002777 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002778
2779 fmt = NULL;
2780 output = NULL;
2781 for (;;) {
2782 int option_index = 0;
2783 static const struct option long_options[] = {
2784 {"help", no_argument, 0, 'h'},
2785 {"format", required_argument, 0, 'f'},
2786 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002787 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002788 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002789 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002790 {0, 0, 0, 0}
2791 };
Fam Zheng335e9932017-05-03 00:35:39 +08002792 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002793 long_options, &option_index);
2794 if (c == -1) {
2795 break;
2796 }
2797 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002798 case ':':
2799 missing_argument(argv[optind - 1]);
2800 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002801 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002802 unrecognized_option(argv[optind - 1]);
2803 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002804 case 'h':
2805 help();
2806 break;
2807 case 'f':
2808 fmt = optarg;
2809 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002810 case 'U':
2811 force_share = true;
2812 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002813 case OPTION_OUTPUT:
2814 output = optarg;
2815 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002816 case OPTION_OBJECT: {
2817 QemuOpts *opts;
2818 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2819 optarg, true);
2820 if (!opts) {
2821 return 1;
2822 }
2823 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002824 case OPTION_IMAGE_OPTS:
2825 image_opts = true;
2826 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002827 }
2828 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002829 if (optind != argc - 1) {
2830 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002831 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002832 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002833
2834 if (output && !strcmp(output, "json")) {
2835 output_format = OFORMAT_JSON;
2836 } else if (output && !strcmp(output, "human")) {
2837 output_format = OFORMAT_HUMAN;
2838 } else if (output) {
2839 error_report("--output must be used with human or json as argument.");
2840 return 1;
2841 }
2842
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002843 if (qemu_opts_foreach(&qemu_object_opts,
2844 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002845 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002846 return 1;
2847 }
2848
Fam Zheng335e9932017-05-03 00:35:39 +08002849 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002850 if (!blk) {
2851 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002852 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002853 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002854
2855 if (output_format == OFORMAT_HUMAN) {
2856 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2857 }
2858
Max Reitzf1d3cd72015-02-05 13:58:18 -05002859 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002860 while (curr.start + curr.length < length) {
2861 int64_t nsectors_left;
2862 int64_t sector_num;
2863 int n;
2864
2865 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2866
2867 /* Probe up to 1 GiB at a time. */
2868 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2869 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2870 ret = get_block_status(bs, sector_num, n, &next);
2871
2872 if (ret < 0) {
2873 error_report("Could not read file metadata: %s", strerror(-ret));
2874 goto out;
2875 }
2876
Fam Zheng16b0d552016-01-26 11:59:02 +08002877 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002878 curr.length += next.length;
2879 continue;
2880 }
2881
2882 if (curr.length > 0) {
2883 dump_map_entry(output_format, &curr, &next);
2884 }
2885 curr = next;
2886 }
2887
2888 dump_map_entry(output_format, &curr, NULL);
2889
2890out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002891 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002892 return ret < 0;
2893}
2894
aliguorif7b4a942009-01-07 17:40:15 +00002895#define SNAPSHOT_LIST 1
2896#define SNAPSHOT_CREATE 2
2897#define SNAPSHOT_APPLY 3
2898#define SNAPSHOT_DELETE 4
2899
Stuart Brady153859b2009-06-07 00:42:17 +01002900static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002901{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002902 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002903 BlockDriverState *bs;
2904 QEMUSnapshotInfo sn;
2905 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002906 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002907 int action = 0;
2908 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002909 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002910 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002911 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002912 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00002913
Kevin Wolfce099542016-03-15 13:01:04 +01002914 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002915 /* Parse commandline parameters */
2916 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002917 static const struct option long_options[] = {
2918 {"help", no_argument, 0, 'h'},
2919 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002920 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002921 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002922 {0, 0, 0, 0}
2923 };
Fam Zheng335e9932017-05-03 00:35:39 +08002924 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002925 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002926 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002927 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002928 }
aliguorif7b4a942009-01-07 17:40:15 +00002929 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002930 case ':':
2931 missing_argument(argv[optind - 1]);
2932 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002933 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002934 unrecognized_option(argv[optind - 1]);
2935 break;
aliguorif7b4a942009-01-07 17:40:15 +00002936 case 'h':
2937 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002938 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002939 case 'l':
2940 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002941 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002942 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002943 }
2944 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002945 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002946 break;
2947 case 'a':
2948 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002949 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002950 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002951 }
2952 action = SNAPSHOT_APPLY;
2953 snapshot_name = optarg;
2954 break;
2955 case 'c':
2956 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002957 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002958 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002959 }
2960 action = SNAPSHOT_CREATE;
2961 snapshot_name = optarg;
2962 break;
2963 case 'd':
2964 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002965 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002966 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002967 }
2968 action = SNAPSHOT_DELETE;
2969 snapshot_name = optarg;
2970 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002971 case 'q':
2972 quiet = true;
2973 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002974 case 'U':
2975 force_share = true;
2976 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002977 case OPTION_OBJECT: {
2978 QemuOpts *opts;
2979 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2980 optarg, true);
2981 if (!opts) {
2982 return 1;
2983 }
2984 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002985 case OPTION_IMAGE_OPTS:
2986 image_opts = true;
2987 break;
aliguorif7b4a942009-01-07 17:40:15 +00002988 }
2989 }
2990
Kevin Wolffc11eb22013-08-05 10:53:04 +02002991 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002992 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002993 }
aliguorif7b4a942009-01-07 17:40:15 +00002994 filename = argv[optind++];
2995
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002996 if (qemu_opts_foreach(&qemu_object_opts,
2997 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002998 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002999 return 1;
3000 }
3001
aliguorif7b4a942009-01-07 17:40:15 +00003002 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08003003 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
3004 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003005 if (!blk) {
3006 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003007 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003008 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00003009
3010 /* Perform the requested action */
3011 switch(action) {
3012 case SNAPSHOT_LIST:
3013 dump_snapshots(bs);
3014 break;
3015
3016 case SNAPSHOT_CREATE:
3017 memset(&sn, 0, sizeof(sn));
3018 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
3019
3020 qemu_gettimeofday(&tv);
3021 sn.date_sec = tv.tv_sec;
3022 sn.date_nsec = tv.tv_usec * 1000;
3023
3024 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003025 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003026 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003027 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003028 }
aliguorif7b4a942009-01-07 17:40:15 +00003029 break;
3030
3031 case SNAPSHOT_APPLY:
3032 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003033 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003034 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003035 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003036 }
aliguorif7b4a942009-01-07 17:40:15 +00003037 break;
3038
3039 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003040 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003041 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003042 error_reportf_err(err, "Could not delete snapshot '%s': ",
3043 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003044 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003045 }
aliguorif7b4a942009-01-07 17:40:15 +00003046 break;
3047 }
3048
3049 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003050 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003051 if (ret) {
3052 return 1;
3053 }
Stuart Brady153859b2009-06-07 00:42:17 +01003054 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003055}
3056
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003057static int img_rebase(int argc, char **argv)
3058{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003059 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003060 uint8_t *buf_old = NULL;
3061 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05003062 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003063 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003064 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3065 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003066 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003067 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003068 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003069 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003070 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003071 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003072 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003073
3074 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003075 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003076 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003077 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003078 out_baseimg = NULL;
3079 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003080 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003081 static const struct option long_options[] = {
3082 {"help", no_argument, 0, 'h'},
3083 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003084 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003085 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003086 {0, 0, 0, 0}
3087 };
Fam Zheng335e9932017-05-03 00:35:39 +08003088 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003089 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003090 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003091 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003092 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003093 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003094 case ':':
3095 missing_argument(argv[optind - 1]);
3096 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003097 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003098 unrecognized_option(argv[optind - 1]);
3099 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003100 case 'h':
3101 help();
3102 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003103 case 'f':
3104 fmt = optarg;
3105 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003106 case 'F':
3107 out_basefmt = optarg;
3108 break;
3109 case 'b':
3110 out_baseimg = optarg;
3111 break;
3112 case 'u':
3113 unsafe = 1;
3114 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003115 case 'p':
3116 progress = 1;
3117 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003118 case 't':
3119 cache = optarg;
3120 break;
Max Reitz40055952014-07-22 22:58:42 +02003121 case 'T':
3122 src_cache = optarg;
3123 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003124 case 'q':
3125 quiet = true;
3126 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003127 case OPTION_OBJECT: {
3128 QemuOpts *opts;
3129 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3130 optarg, true);
3131 if (!opts) {
3132 return 1;
3133 }
3134 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003135 case OPTION_IMAGE_OPTS:
3136 image_opts = true;
3137 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003138 case 'U':
3139 force_share = true;
3140 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003141 }
3142 }
3143
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003144 if (quiet) {
3145 progress = 0;
3146 }
3147
Fam Zhengac1307a2014-04-22 13:36:11 +08003148 if (optind != argc - 1) {
3149 error_exit("Expecting one image file name");
3150 }
3151 if (!unsafe && !out_baseimg) {
3152 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003153 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003154 filename = argv[optind++];
3155
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003156 if (qemu_opts_foreach(&qemu_object_opts,
3157 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003158 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003159 return 1;
3160 }
3161
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003162 qemu_progress_init(progress, 2.0);
3163 qemu_progress_print(0, 100);
3164
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003165 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003166 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003167 if (ret < 0) {
3168 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003169 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003170 }
3171
Kevin Wolfce099542016-03-15 13:01:04 +01003172 src_flags = 0;
3173 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003174 if (ret < 0) {
3175 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003176 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003177 }
3178
Kevin Wolfce099542016-03-15 13:01:04 +01003179 /* The source files are opened read-only, don't care about WCE */
3180 assert((src_flags & BDRV_O_RDWR) == 0);
3181 (void) src_writethrough;
3182
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003183 /*
3184 * Open the images.
3185 *
3186 * Ignore the old backing file for unsafe rebase in case we want to correct
3187 * the reference to a renamed or moved backing file.
3188 */
Fam Zheng335e9932017-05-03 00:35:39 +08003189 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3190 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003191 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003192 ret = -1;
3193 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003194 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003195 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003196
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003197 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003198 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003199 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003200 ret = -1;
3201 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003202 }
3203 }
3204
3205 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003206 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003207 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003208 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003209
Max Reitz644483d2015-02-05 13:58:17 -05003210 if (bs->backing_format[0] != '\0') {
3211 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003212 qdict_put_str(options, "driver", bs->backing_format);
Max Reitz644483d2015-02-05 13:58:17 -05003213 }
3214
Fam Zheng335e9932017-05-03 00:35:39 +08003215 if (force_share) {
3216 if (!options) {
3217 options = qdict_new();
3218 }
Eric Blake579cf1d2017-05-15 14:54:39 -05003219 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +08003220 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003221 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003222 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003223 options, src_flags, &local_err);
3224 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003225 error_reportf_err(local_err,
3226 "Could not open old backing file '%s': ",
3227 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003228 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003229 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003230 }
Max Reitz644483d2015-02-05 13:58:17 -05003231
Alex Bligha6166732012-10-16 13:46:18 +01003232 if (out_baseimg[0]) {
Fam Zheng335e9932017-05-03 00:35:39 +08003233 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003234 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003235 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003236 }
3237 if (force_share) {
3238 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003239 }
3240
Max Reitzefaa7c42016-03-16 19:54:38 +01003241 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003242 options, src_flags, &local_err);
3243 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003244 error_reportf_err(local_err,
3245 "Could not open new backing file '%s': ",
3246 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003247 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003248 goto out;
3249 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003250 }
3251 }
3252
3253 /*
3254 * Check each unallocated cluster in the COW file. If it is unallocated,
3255 * accesses go to the backing file. We must therefore compare this cluster
3256 * in the old and new backing file, and if they differ we need to copy it
3257 * from the old backing file into the COW file.
3258 *
3259 * If qemu-img crashes during this step, no harm is done. The content of
3260 * the image is the same as the original one at any time.
3261 */
3262 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003263 int64_t num_sectors;
3264 int64_t old_backing_num_sectors;
3265 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003266 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003267 int n;
Eric Blaked6a644b2017-07-07 07:44:57 -05003268 int64_t count;
Kevin Wolf1f710492012-10-12 14:29:18 +02003269 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003270
Max Reitzf1d3cd72015-02-05 13:58:18 -05003271 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3272 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003273
Max Reitzf1d3cd72015-02-05 13:58:18 -05003274 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003275 if (num_sectors < 0) {
3276 error_report("Could not get size of '%s': %s",
3277 filename, strerror(-num_sectors));
3278 ret = -1;
3279 goto out;
3280 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003281 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003282 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003283 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003284
3285 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3286 error_report("Could not get size of '%s': %s",
3287 backing_name, strerror(-old_backing_num_sectors));
3288 ret = -1;
3289 goto out;
3290 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003291 if (blk_new_backing) {
3292 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003293 if (new_backing_num_sectors < 0) {
3294 error_report("Could not get size of '%s': %s",
3295 out_baseimg, strerror(-new_backing_num_sectors));
3296 ret = -1;
3297 goto out;
3298 }
Alex Bligha6166732012-10-16 13:46:18 +01003299 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003300
Kevin Wolf1f710492012-10-12 14:29:18 +02003301 if (num_sectors != 0) {
3302 local_progress = (float)100 /
3303 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
3304 }
3305
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003306 for (sector = 0; sector < num_sectors; sector += n) {
3307
3308 /* How many sectors can we handle with the next read? */
3309 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
3310 n = (IO_BUF_SIZE / 512);
3311 } else {
3312 n = num_sectors - sector;
3313 }
3314
3315 /* If the cluster is allocated, we don't need to take action */
Eric Blaked6a644b2017-07-07 07:44:57 -05003316 ret = bdrv_is_allocated(bs, sector << BDRV_SECTOR_BITS,
3317 n << BDRV_SECTOR_BITS, &count);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003318 if (ret < 0) {
3319 error_report("error while reading image metadata: %s",
3320 strerror(-ret));
3321 goto out;
3322 }
Eric Blaked6a644b2017-07-07 07:44:57 -05003323 /* TODO relax this once bdrv_is_allocated does not enforce
3324 * sector alignment */
3325 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
3326 n = count >> BDRV_SECTOR_BITS;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003327 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003328 continue;
3329 }
3330
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003331 /*
3332 * Read old and new backing file and take into consideration that
3333 * backing files may be smaller than the COW image.
3334 */
3335 if (sector >= old_backing_num_sectors) {
3336 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3337 } else {
3338 if (sector + n > old_backing_num_sectors) {
3339 n = old_backing_num_sectors - sector;
3340 }
3341
Eric Blake91669202016-05-06 10:26:43 -06003342 ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS,
3343 buf_old, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003344 if (ret < 0) {
3345 error_report("error while reading from old backing file");
3346 goto out;
3347 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003348 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003349
Max Reitzf1d3cd72015-02-05 13:58:18 -05003350 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003351 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3352 } else {
3353 if (sector + n > new_backing_num_sectors) {
3354 n = new_backing_num_sectors - sector;
3355 }
3356
Eric Blake91669202016-05-06 10:26:43 -06003357 ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS,
3358 buf_new, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003359 if (ret < 0) {
3360 error_report("error while reading from new backing file");
3361 goto out;
3362 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003363 }
3364
3365 /* If they differ, we need to write to the COW file */
3366 uint64_t written = 0;
3367
3368 while (written < n) {
3369 int pnum;
3370
3371 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003372 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003373 {
Eric Blake91669202016-05-06 10:26:43 -06003374 ret = blk_pwrite(blk,
3375 (sector + written) << BDRV_SECTOR_BITS,
3376 buf_old + written * 512,
3377 pnum << BDRV_SECTOR_BITS, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003378 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003379 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003380 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003381 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003382 }
3383 }
3384
3385 written += pnum;
3386 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003387 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003388 }
3389 }
3390
3391 /*
3392 * Change the backing file. All clusters that are different from the old
3393 * backing file are overwritten in the COW file now, so the visible content
3394 * doesn't change when we switch the backing file.
3395 */
Alex Bligha6166732012-10-16 13:46:18 +01003396 if (out_baseimg && *out_baseimg) {
3397 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3398 } else {
3399 ret = bdrv_change_backing_file(bs, NULL, NULL);
3400 }
3401
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003402 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003403 error_report("Could not change the backing file to '%s': No "
3404 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003405 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003406 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003407 out_baseimg, strerror(-ret));
3408 }
3409
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003410 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003411 /*
3412 * TODO At this point it is possible to check if any clusters that are
3413 * allocated in the COW file are the same in the backing file. If so, they
3414 * could be dropped from the COW file. Don't do this before switching the
3415 * backing file, in case of a crash this would lead to corruption.
3416 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003417out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003418 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003419 /* Cleanup */
3420 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003421 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003422 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003423 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003424 qemu_vfree(buf_old);
3425 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003426
Markus Armbruster26f54e92014-10-07 13:59:04 +02003427 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003428 if (ret) {
3429 return 1;
3430 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003431 return 0;
3432}
3433
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003434static int img_resize(int argc, char **argv)
3435{
Markus Armbruster6750e792015-02-12 17:43:08 +01003436 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003437 int c, ret, relative;
3438 const char *filename, *fmt, *size;
3439 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003440 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003441 BlockBackend *blk = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003442 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003443
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003444 static QemuOptsList resize_options = {
3445 .name = "resize_options",
3446 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3447 .desc = {
3448 {
3449 .name = BLOCK_OPT_SIZE,
3450 .type = QEMU_OPT_SIZE,
3451 .help = "Virtual disk size"
3452 }, {
3453 /* end of list */
3454 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003455 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003456 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003457 bool image_opts = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003458
Kevin Wolfe80fec72011-04-29 10:58:12 +02003459 /* Remove size from argv manually so that negative numbers are not treated
3460 * as options by getopt. */
3461 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003462 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003463 return 1;
3464 }
3465
3466 size = argv[--argc];
3467
3468 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003469 fmt = NULL;
3470 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003471 static const struct option long_options[] = {
3472 {"help", no_argument, 0, 'h'},
3473 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003474 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003475 {0, 0, 0, 0}
3476 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003477 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003478 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003479 if (c == -1) {
3480 break;
3481 }
3482 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003483 case ':':
3484 missing_argument(argv[optind - 1]);
3485 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003486 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003487 unrecognized_option(argv[optind - 1]);
3488 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003489 case 'h':
3490 help();
3491 break;
3492 case 'f':
3493 fmt = optarg;
3494 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003495 case 'q':
3496 quiet = true;
3497 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003498 case OPTION_OBJECT: {
3499 QemuOpts *opts;
3500 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3501 optarg, true);
3502 if (!opts) {
3503 return 1;
3504 }
3505 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003506 case OPTION_IMAGE_OPTS:
3507 image_opts = true;
3508 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003509 }
3510 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003511 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003512 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003513 }
3514 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003515
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003516 if (qemu_opts_foreach(&qemu_object_opts,
3517 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003518 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003519 return 1;
3520 }
3521
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003522 /* Choose grow, shrink, or absolute resize mode */
3523 switch (size[0]) {
3524 case '+':
3525 relative = 1;
3526 size++;
3527 break;
3528 case '-':
3529 relative = -1;
3530 size++;
3531 break;
3532 default:
3533 relative = 0;
3534 break;
3535 }
3536
3537 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003538 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003539 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003540 if (err) {
3541 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003542 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003543 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003544 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003545 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003546 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3547 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003548
Max Reitzefaa7c42016-03-16 19:54:38 +01003549 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003550 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3551 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003552 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003553 ret = -1;
3554 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003555 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003556
3557 if (relative) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003558 total_size = blk_getlength(blk) + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003559 } else {
3560 total_size = n;
3561 }
3562 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003563 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003564 ret = -1;
3565 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003566 }
3567
Max Reitzed3d2ec2017-03-28 22:51:27 +02003568 ret = blk_truncate(blk, total_size, &err);
3569 if (!ret) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003570 qprintf(quiet, "Image resized.\n");
Max Reitzed3d2ec2017-03-28 22:51:27 +02003571 } else {
3572 error_report_err(err);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003573 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003574out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003575 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003576 if (ret) {
3577 return 1;
3578 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003579 return 0;
3580}
3581
Max Reitz76a3a342014-10-27 11:12:51 +01003582static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003583 int64_t offset, int64_t total_work_size,
3584 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003585{
3586 qemu_progress_print(100.f * offset / total_work_size, 0);
3587}
3588
Max Reitz6f176b42013-09-03 10:09:50 +02003589static int img_amend(int argc, char **argv)
3590{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003591 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003592 int c, ret = 0;
3593 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003594 QemuOptsList *create_opts = NULL;
3595 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003596 const char *fmt = NULL, *filename, *cache;
3597 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003598 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003599 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003600 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003601 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003602 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003603
Max Reitzbd39e6e2014-07-22 22:58:43 +02003604 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003605 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003606 static const struct option long_options[] = {
3607 {"help", no_argument, 0, 'h'},
3608 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003609 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003610 {0, 0, 0, 0}
3611 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003612 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003613 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003614 if (c == -1) {
3615 break;
3616 }
3617
3618 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003619 case ':':
3620 missing_argument(argv[optind - 1]);
3621 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003622 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003623 unrecognized_option(argv[optind - 1]);
3624 break;
3625 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003626 help();
3627 break;
3628 case 'o':
3629 if (!is_valid_option_list(optarg)) {
3630 error_report("Invalid option list: %s", optarg);
3631 ret = -1;
3632 goto out_no_progress;
3633 }
3634 if (!options) {
3635 options = g_strdup(optarg);
3636 } else {
3637 char *old_options = options;
3638 options = g_strdup_printf("%s,%s", options, optarg);
3639 g_free(old_options);
3640 }
3641 break;
3642 case 'f':
3643 fmt = optarg;
3644 break;
3645 case 't':
3646 cache = optarg;
3647 break;
3648 case 'p':
3649 progress = true;
3650 break;
3651 case 'q':
3652 quiet = true;
3653 break;
3654 case OPTION_OBJECT:
3655 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3656 optarg, true);
3657 if (!opts) {
3658 ret = -1;
3659 goto out_no_progress;
3660 }
3661 break;
3662 case OPTION_IMAGE_OPTS:
3663 image_opts = true;
3664 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003665 }
3666 }
3667
Max Reitz6f176b42013-09-03 10:09:50 +02003668 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003669 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003670 }
3671
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003672 if (qemu_opts_foreach(&qemu_object_opts,
3673 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003674 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003675 ret = -1;
3676 goto out_no_progress;
3677 }
3678
Max Reitz76a3a342014-10-27 11:12:51 +01003679 if (quiet) {
3680 progress = false;
3681 }
3682 qemu_progress_init(progress, 1.0);
3683
Kevin Wolfa283cb62014-02-21 16:24:07 +01003684 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3685 if (fmt && has_help_option(options)) {
3686 /* If a format is explicitly specified (and possibly no filename is
3687 * given), print option help here */
3688 ret = print_block_option_help(filename, fmt);
3689 goto out;
3690 }
3691
3692 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003693 error_report("Expecting one image file name");
3694 ret = -1;
3695 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003696 }
Max Reitz6f176b42013-09-03 10:09:50 +02003697
Kevin Wolfce099542016-03-15 13:01:04 +01003698 flags = BDRV_O_RDWR;
3699 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003700 if (ret < 0) {
3701 error_report("Invalid cache option: %s", cache);
3702 goto out;
3703 }
3704
Fam Zheng335e9932017-05-03 00:35:39 +08003705 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3706 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003707 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003708 ret = -1;
3709 goto out;
3710 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003711 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003712
3713 fmt = bs->drv->format_name;
3714
Kevin Wolf626f84f2014-02-21 16:24:06 +01003715 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003716 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003717 ret = print_block_option_help(filename, fmt);
3718 goto out;
3719 }
3720
Max Reitzb2439d22014-12-02 18:32:47 +01003721 if (!bs->drv->create_opts) {
3722 error_report("Format driver '%s' does not support any options to amend",
3723 fmt);
3724 ret = -1;
3725 goto out;
3726 }
3727
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003728 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003729 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003730 qemu_opts_do_parse(opts, options, NULL, &err);
3731 if (err) {
3732 error_report_err(err);
3733 ret = -1;
3734 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003735 }
3736
Max Reitz76a3a342014-10-27 11:12:51 +01003737 /* In case the driver does not call amend_status_cb() */
3738 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003739 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003740 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003741 if (ret < 0) {
3742 error_report("Error while amending options: %s", strerror(-ret));
3743 goto out;
3744 }
3745
3746out:
Max Reitz76a3a342014-10-27 11:12:51 +01003747 qemu_progress_end();
3748
Max Reitze814dff2015-08-20 16:00:38 -07003749out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003750 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003751 qemu_opts_del(opts);
3752 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003753 g_free(options);
3754
Max Reitz6f176b42013-09-03 10:09:50 +02003755 if (ret) {
3756 return 1;
3757 }
3758 return 0;
3759}
3760
Kevin Wolfb6133b82014-08-05 14:17:13 +02003761typedef struct BenchData {
3762 BlockBackend *blk;
3763 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003764 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003765 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003766 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003767 int nrreq;
3768 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003769 int flush_interval;
3770 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003771 uint8_t *buf;
3772 QEMUIOVector *qiov;
3773
3774 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003775 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003776 uint64_t offset;
3777} BenchData;
3778
Kevin Wolf55d539c2016-06-03 13:59:41 +02003779static void bench_undrained_flush_cb(void *opaque, int ret)
3780{
3781 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003782 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003783 exit(EXIT_FAILURE);
3784 }
3785}
3786
Kevin Wolfb6133b82014-08-05 14:17:13 +02003787static void bench_cb(void *opaque, int ret)
3788{
3789 BenchData *b = opaque;
3790 BlockAIOCB *acb;
3791
3792 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003793 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003794 exit(EXIT_FAILURE);
3795 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003796
3797 if (b->in_flush) {
3798 /* Just finished a flush with drained queue: Start next requests */
3799 assert(b->in_flight == 0);
3800 b->in_flush = false;
3801 } else if (b->in_flight > 0) {
3802 int remaining = b->n - b->in_flight;
3803
Kevin Wolfb6133b82014-08-05 14:17:13 +02003804 b->n--;
3805 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003806
3807 /* Time for flush? Drain queue if requested, then flush */
3808 if (b->flush_interval && remaining % b->flush_interval == 0) {
3809 if (!b->in_flight || !b->drain_on_flush) {
3810 BlockCompletionFunc *cb;
3811
3812 if (b->drain_on_flush) {
3813 b->in_flush = true;
3814 cb = bench_cb;
3815 } else {
3816 cb = bench_undrained_flush_cb;
3817 }
3818
3819 acb = blk_aio_flush(b->blk, cb, b);
3820 if (!acb) {
3821 error_report("Failed to issue flush request");
3822 exit(EXIT_FAILURE);
3823 }
3824 }
3825 if (b->drain_on_flush) {
3826 return;
3827 }
3828 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003829 }
3830
3831 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003832 int64_t offset = b->offset;
3833 /* blk_aio_* might look for completed I/Os and kick bench_cb
3834 * again, so make sure this operation is counted by in_flight
3835 * and b->offset is ready for the next submission.
3836 */
3837 b->in_flight++;
3838 b->offset += b->step;
3839 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003840 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003841 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003842 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003843 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003844 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003845 if (!acb) {
3846 error_report("Failed to issue request");
3847 exit(EXIT_FAILURE);
3848 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003849 }
3850}
3851
3852static int img_bench(int argc, char **argv)
3853{
3854 int c, ret = 0;
3855 const char *fmt = NULL, *filename;
3856 bool quiet = false;
3857 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003858 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003859 int count = 75000;
3860 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003861 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003862 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003863 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003864 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003865 int flush_interval = 0;
3866 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003867 int64_t image_size;
3868 BlockBackend *blk = NULL;
3869 BenchData data = {};
3870 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003871 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003872 struct timeval t1, t2;
3873 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08003874 bool force_share = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003875
3876 for (;;) {
3877 static const struct option long_options[] = {
3878 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003879 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003880 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003881 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003882 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08003883 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003884 {0, 0, 0, 0}
3885 };
Fam Zheng335e9932017-05-03 00:35:39 +08003886 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003887 if (c == -1) {
3888 break;
3889 }
3890
3891 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003892 case ':':
3893 missing_argument(argv[optind - 1]);
3894 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003895 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003896 unrecognized_option(argv[optind - 1]);
3897 break;
3898 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02003899 help();
3900 break;
3901 case 'c':
3902 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003903 unsigned long res;
3904
3905 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003906 error_report("Invalid request count specified");
3907 return 1;
3908 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003909 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003910 break;
3911 }
3912 case 'd':
3913 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003914 unsigned long res;
3915
3916 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003917 error_report("Invalid queue depth specified");
3918 return 1;
3919 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003920 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003921 break;
3922 }
3923 case 'f':
3924 fmt = optarg;
3925 break;
3926 case 'n':
3927 flags |= BDRV_O_NATIVE_AIO;
3928 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003929 case 'o':
3930 {
Markus Armbruster606caa02017-02-21 21:14:04 +01003931 offset = cvtnum(optarg);
3932 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02003933 error_report("Invalid offset specified");
3934 return 1;
3935 }
3936 break;
3937 }
3938 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003939 case 'q':
3940 quiet = true;
3941 break;
3942 case 's':
3943 {
3944 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003945
Markus Armbruster606caa02017-02-21 21:14:04 +01003946 sval = cvtnum(optarg);
3947 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003948 error_report("Invalid buffer size specified");
3949 return 1;
3950 }
3951
3952 bufsize = sval;
3953 break;
3954 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003955 case 'S':
3956 {
3957 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003958
Markus Armbruster606caa02017-02-21 21:14:04 +01003959 sval = cvtnum(optarg);
3960 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02003961 error_report("Invalid step size specified");
3962 return 1;
3963 }
3964
3965 step = sval;
3966 break;
3967 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003968 case 't':
3969 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3970 if (ret < 0) {
3971 error_report("Invalid cache mode");
3972 ret = -1;
3973 goto out;
3974 }
3975 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003976 case 'w':
3977 flags |= BDRV_O_RDWR;
3978 is_write = true;
3979 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003980 case 'U':
3981 force_share = true;
3982 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003983 case OPTION_PATTERN:
3984 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003985 unsigned long res;
3986
3987 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003988 error_report("Invalid pattern byte specified");
3989 return 1;
3990 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003991 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003992 break;
3993 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003994 case OPTION_FLUSH_INTERVAL:
3995 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003996 unsigned long res;
3997
3998 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02003999 error_report("Invalid flush interval specified");
4000 return 1;
4001 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004002 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004003 break;
4004 }
4005 case OPTION_NO_DRAIN:
4006 drain_on_flush = false;
4007 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004008 case OPTION_IMAGE_OPTS:
4009 image_opts = true;
4010 break;
4011 }
4012 }
4013
4014 if (optind != argc - 1) {
4015 error_exit("Expecting one image file name");
4016 }
4017 filename = argv[argc - 1];
4018
Kevin Wolf55d539c2016-06-03 13:59:41 +02004019 if (!is_write && flush_interval) {
4020 error_report("--flush-interval is only available in write tests");
4021 ret = -1;
4022 goto out;
4023 }
4024 if (flush_interval && flush_interval < depth) {
4025 error_report("Flush interval can't be smaller than depth");
4026 ret = -1;
4027 goto out;
4028 }
4029
Fam Zheng335e9932017-05-03 00:35:39 +08004030 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4031 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004032 if (!blk) {
4033 ret = -1;
4034 goto out;
4035 }
4036
4037 image_size = blk_getlength(blk);
4038 if (image_size < 0) {
4039 ret = image_size;
4040 goto out;
4041 }
4042
4043 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004044 .blk = blk,
4045 .image_size = image_size,
4046 .bufsize = bufsize,
4047 .step = step ?: bufsize,
4048 .nrreq = depth,
4049 .n = count,
4050 .offset = offset,
4051 .write = is_write,
4052 .flush_interval = flush_interval,
4053 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004054 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004055 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004056 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004057 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004058 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004059 if (flush_interval) {
4060 printf("Sending flush every %d requests\n", flush_interval);
4061 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004062
4063 data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004064 memset(data.buf, pattern, data.nrreq * data.bufsize);
4065
Kevin Wolfb6133b82014-08-05 14:17:13 +02004066 data.qiov = g_new(QEMUIOVector, data.nrreq);
4067 for (i = 0; i < data.nrreq; i++) {
4068 qemu_iovec_init(&data.qiov[i], 1);
4069 qemu_iovec_add(&data.qiov[i],
4070 data.buf + i * data.bufsize, data.bufsize);
4071 }
4072
4073 gettimeofday(&t1, NULL);
4074 bench_cb(&data, 0);
4075
4076 while (data.n > 0) {
4077 main_loop_wait(false);
4078 }
4079 gettimeofday(&t2, NULL);
4080
4081 printf("Run completed in %3.3f seconds.\n",
4082 (t2.tv_sec - t1.tv_sec)
4083 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4084
4085out:
4086 qemu_vfree(data.buf);
4087 blk_unref(blk);
4088
4089 if (ret) {
4090 return 1;
4091 }
4092 return 0;
4093}
4094
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004095#define C_BS 01
4096#define C_COUNT 02
4097#define C_IF 04
4098#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004099#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004100
4101struct DdInfo {
4102 unsigned int flags;
4103 int64_t count;
4104};
4105
4106struct DdIo {
4107 int bsz; /* Block size */
4108 char *filename;
4109 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004110 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004111};
4112
4113struct DdOpts {
4114 const char *name;
4115 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4116 unsigned int flag;
4117};
4118
4119static int img_dd_bs(const char *arg,
4120 struct DdIo *in, struct DdIo *out,
4121 struct DdInfo *dd)
4122{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004123 int64_t res;
4124
Markus Armbruster606caa02017-02-21 21:14:04 +01004125 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004126
Markus Armbruster606caa02017-02-21 21:14:04 +01004127 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004128 error_report("invalid number: '%s'", arg);
4129 return 1;
4130 }
4131 in->bsz = out->bsz = res;
4132
4133 return 0;
4134}
4135
4136static int img_dd_count(const char *arg,
4137 struct DdIo *in, struct DdIo *out,
4138 struct DdInfo *dd)
4139{
Markus Armbruster606caa02017-02-21 21:14:04 +01004140 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004141
Markus Armbruster606caa02017-02-21 21:14:04 +01004142 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004143 error_report("invalid number: '%s'", arg);
4144 return 1;
4145 }
4146
4147 return 0;
4148}
4149
4150static int img_dd_if(const char *arg,
4151 struct DdIo *in, struct DdIo *out,
4152 struct DdInfo *dd)
4153{
4154 in->filename = g_strdup(arg);
4155
4156 return 0;
4157}
4158
4159static int img_dd_of(const char *arg,
4160 struct DdIo *in, struct DdIo *out,
4161 struct DdInfo *dd)
4162{
4163 out->filename = g_strdup(arg);
4164
4165 return 0;
4166}
4167
Reda Sallahif7c15532016-08-10 16:16:09 +02004168static int img_dd_skip(const char *arg,
4169 struct DdIo *in, struct DdIo *out,
4170 struct DdInfo *dd)
4171{
Markus Armbruster606caa02017-02-21 21:14:04 +01004172 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004173
Markus Armbruster606caa02017-02-21 21:14:04 +01004174 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004175 error_report("invalid number: '%s'", arg);
4176 return 1;
4177 }
4178
4179 return 0;
4180}
4181
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004182static int img_dd(int argc, char **argv)
4183{
4184 int ret = 0;
4185 char *arg = NULL;
4186 char *tmp;
4187 BlockDriver *drv = NULL, *proto_drv = NULL;
4188 BlockBackend *blk1 = NULL, *blk2 = NULL;
4189 QemuOpts *opts = NULL;
4190 QemuOptsList *create_opts = NULL;
4191 Error *local_err = NULL;
4192 bool image_opts = false;
4193 int c, i;
4194 const char *out_fmt = "raw";
4195 const char *fmt = NULL;
4196 int64_t size = 0;
4197 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004198 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004199 struct DdInfo dd = {
4200 .flags = 0,
4201 .count = 0,
4202 };
4203 struct DdIo in = {
4204 .bsz = 512, /* Block size is by default 512 bytes */
4205 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004206 .buf = NULL,
4207 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004208 };
4209 struct DdIo out = {
4210 .bsz = 512,
4211 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004212 .buf = NULL,
4213 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004214 };
4215
4216 const struct DdOpts options[] = {
4217 { "bs", img_dd_bs, C_BS },
4218 { "count", img_dd_count, C_COUNT },
4219 { "if", img_dd_if, C_IF },
4220 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004221 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004222 { NULL, NULL, 0 }
4223 };
4224 const struct option long_options[] = {
4225 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004226 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004227 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004228 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004229 { 0, 0, 0, 0 }
4230 };
4231
Fam Zheng335e9932017-05-03 00:35:39 +08004232 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004233 if (c == EOF) {
4234 break;
4235 }
4236 switch (c) {
4237 case 'O':
4238 out_fmt = optarg;
4239 break;
4240 case 'f':
4241 fmt = optarg;
4242 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004243 case ':':
4244 missing_argument(argv[optind - 1]);
4245 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004246 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004247 unrecognized_option(argv[optind - 1]);
4248 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004249 case 'h':
4250 help();
4251 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004252 case 'U':
4253 force_share = true;
4254 break;
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004255 case OPTION_OBJECT:
4256 if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004257 ret = -1;
4258 goto out;
4259 }
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004260 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004261 case OPTION_IMAGE_OPTS:
4262 image_opts = true;
4263 break;
4264 }
4265 }
4266
4267 for (i = optind; i < argc; i++) {
4268 int j;
4269 arg = g_strdup(argv[i]);
4270
4271 tmp = strchr(arg, '=');
4272 if (tmp == NULL) {
4273 error_report("unrecognized operand %s", arg);
4274 ret = -1;
4275 goto out;
4276 }
4277
4278 *tmp++ = '\0';
4279
4280 for (j = 0; options[j].name != NULL; j++) {
4281 if (!strcmp(arg, options[j].name)) {
4282 break;
4283 }
4284 }
4285 if (options[j].name == NULL) {
4286 error_report("unrecognized operand %s", arg);
4287 ret = -1;
4288 goto out;
4289 }
4290
4291 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4292 ret = -1;
4293 goto out;
4294 }
4295 dd.flags |= options[j].flag;
4296 g_free(arg);
4297 arg = NULL;
4298 }
4299
4300 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4301 error_report("Must specify both input and output files");
4302 ret = -1;
4303 goto out;
4304 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004305
4306 if (qemu_opts_foreach(&qemu_object_opts,
4307 user_creatable_add_opts_foreach,
4308 NULL, NULL)) {
4309 ret = -1;
4310 goto out;
4311 }
4312
Fam Zheng335e9932017-05-03 00:35:39 +08004313 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4314 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004315
4316 if (!blk1) {
4317 ret = -1;
4318 goto out;
4319 }
4320
4321 drv = bdrv_find_format(out_fmt);
4322 if (!drv) {
4323 error_report("Unknown file format");
4324 ret = -1;
4325 goto out;
4326 }
4327 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4328
4329 if (!proto_drv) {
4330 error_report_err(local_err);
4331 ret = -1;
4332 goto out;
4333 }
4334 if (!drv->create_opts) {
4335 error_report("Format driver '%s' does not support image creation",
4336 drv->format_name);
4337 ret = -1;
4338 goto out;
4339 }
4340 if (!proto_drv->create_opts) {
4341 error_report("Protocol driver '%s' does not support image creation",
4342 proto_drv->format_name);
4343 ret = -1;
4344 goto out;
4345 }
4346 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4347 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4348
4349 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4350
4351 size = blk_getlength(blk1);
4352 if (size < 0) {
4353 error_report("Failed to get size for '%s'", in.filename);
4354 ret = -1;
4355 goto out;
4356 }
4357
4358 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4359 dd.count * in.bsz < size) {
4360 size = dd.count * in.bsz;
4361 }
4362
Reda Sallahif7c15532016-08-10 16:16:09 +02004363 /* Overflow means the specified offset is beyond input image's size */
4364 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4365 size < in.bsz * in.offset)) {
4366 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4367 } else {
4368 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4369 size - in.bsz * in.offset, &error_abort);
4370 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004371
4372 ret = bdrv_create(drv, out.filename, opts, &local_err);
4373 if (ret < 0) {
4374 error_reportf_err(local_err,
4375 "%s: error while creating output image: ",
4376 out.filename);
4377 ret = -1;
4378 goto out;
4379 }
4380
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004381 /* TODO, we can't honour --image-opts for the target,
4382 * since it needs to be given in a format compatible
4383 * with the bdrv_create() call above which does not
4384 * support image-opts style.
4385 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01004386 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004387 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004388
4389 if (!blk2) {
4390 ret = -1;
4391 goto out;
4392 }
4393
Reda Sallahif7c15532016-08-10 16:16:09 +02004394 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4395 size < in.offset * in.bsz)) {
4396 /* We give a warning if the skip option is bigger than the input
4397 * size and create an empty output disk image (i.e. like dd(1)).
4398 */
4399 error_report("%s: cannot skip to specified offset", in.filename);
4400 in_pos = size;
4401 } else {
4402 in_pos = in.offset * in.bsz;
4403 }
4404
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004405 in.buf = g_new(uint8_t, in.bsz);
4406
Reda Sallahif7c15532016-08-10 16:16:09 +02004407 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004408 int in_ret, out_ret;
4409
4410 if (in_pos + in.bsz > size) {
4411 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4412 } else {
4413 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4414 }
4415 if (in_ret < 0) {
4416 error_report("error while reading from input image file: %s",
4417 strerror(-in_ret));
4418 ret = -1;
4419 goto out;
4420 }
4421 in_pos += in_ret;
4422
4423 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4424
4425 if (out_ret < 0) {
4426 error_report("error while writing to output image file: %s",
4427 strerror(-out_ret));
4428 ret = -1;
4429 goto out;
4430 }
4431 out_pos += out_ret;
4432 }
4433
4434out:
4435 g_free(arg);
4436 qemu_opts_del(opts);
4437 qemu_opts_free(create_opts);
4438 blk_unref(blk1);
4439 blk_unref(blk2);
4440 g_free(in.filename);
4441 g_free(out.filename);
4442 g_free(in.buf);
4443 g_free(out.buf);
4444
4445 if (ret) {
4446 return 1;
4447 }
4448 return 0;
4449}
4450
Kevin Wolfb6133b82014-08-05 14:17:13 +02004451
Anthony Liguoric227f092009-10-01 16:12:16 -05004452static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004453#define DEF(option, callback, arg_string) \
4454 { option, callback },
4455#include "qemu-img-cmds.h"
4456#undef DEF
4457#undef GEN_DOCS
4458 { NULL, NULL, },
4459};
4460
bellardea2384d2004-08-01 21:59:26 +00004461int main(int argc, char **argv)
4462{
Anthony Liguoric227f092009-10-01 16:12:16 -05004463 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004464 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004465 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004466 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004467 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004468 static const struct option long_options[] = {
4469 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004470 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004471 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004472 {0, 0, 0, 0}
4473 };
bellardea2384d2004-08-01 21:59:26 +00004474
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004475#ifdef CONFIG_POSIX
4476 signal(SIGPIPE, SIG_IGN);
4477#endif
4478
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004479 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004480 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004481 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004482
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004483 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004484 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004485 exit(EXIT_FAILURE);
4486 }
4487
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004488 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004489
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004490 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004491 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004492 if (argc < 2) {
4493 error_exit("Not enough arguments");
4494 }
Stuart Brady153859b2009-06-07 00:42:17 +01004495
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004496 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004497 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004498 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004499
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004500 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004501 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004502 case ':':
4503 missing_argument(argv[optind - 1]);
4504 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08004505 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004506 unrecognized_option(argv[optind - 1]);
4507 return 0;
4508 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03004509 help();
4510 return 0;
4511 case 'V':
4512 printf(QEMU_IMG_VERSION);
4513 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004514 case 'T':
4515 g_free(trace_file);
4516 trace_file = trace_opt_parse(optarg);
4517 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004518 }
bellardea2384d2004-08-01 21:59:26 +00004519 }
Stuart Brady153859b2009-06-07 00:42:17 +01004520
Denis V. Lunev10985132016-06-17 17:44:13 +03004521 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004522
Denis V. Lunev10985132016-06-17 17:44:13 +03004523 /* reset getopt_long scanning */
4524 argc -= optind;
4525 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004526 return 0;
4527 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004528 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004529 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004530
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004531 if (!trace_init_backends()) {
4532 exit(1);
4533 }
4534 trace_init_file(trace_file);
4535 qemu_set_log(LOG_TRACE);
4536
Denis V. Lunev10985132016-06-17 17:44:13 +03004537 /* find the command */
4538 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4539 if (!strcmp(cmdname, cmd->name)) {
4540 return cmd->handler(argc, argv);
4541 }
4542 }
Jeff Cody7db16892014-04-25 17:02:32 -04004543
Stuart Brady153859b2009-06-07 00:42:17 +01004544 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004545 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004546}