blob: 89f1093d818c2cb79263cc61184a9e01d7499885 [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"
Eric Blakec2a3d7d2017-07-21 08:50:47 -050025#include <getopt.h>
26
Fam Zheng67a1de02016-06-01 17:44:21 +080027#include "qemu-version.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010028#include "qapi/error.h"
BenoƮt Canetc054b3f2012-09-05 13:09:02 +020029#include "qapi-visit.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010030#include "qapi/qobject-output-visitor.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010031#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010032#include "qapi/qmp/qjson.h"
Fam Zheng335e9932017-05-03 00:35:39 +080033#include "qapi/qmp/qbool.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020034#include "qemu/cutils.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000035#include "qemu/config-file.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010036#include "qemu/option.h"
37#include "qemu/error-report.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030038#include "qemu/log.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000039#include "qom/object_interfaces.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010040#include "sysemu/sysemu.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020041#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010042#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020043#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080044#include "block/qapi.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010045#include "crypto/init.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030046#include "trace/control.h"
bellarde8445332006-06-14 15:32:10 +000047
Don Slutz61979a62015-01-09 10:17:35 -050048#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
Thomas Huth0781dd62016-10-05 11:54:44 +020049 "\n" QEMU_COPYRIGHT "\n"
Jeff Cody5f6979c2014-04-28 14:37:18 -040050
Anthony Liguoric227f092009-10-01 16:12:16 -050051typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010052 const char *name;
53 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050054} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010055
Federico Simoncelli8599ea42013-01-28 06:59:47 -050056enum {
57 OPTION_OUTPUT = 256,
58 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000059 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000060 OPTION_IMAGE_OPTS = 259,
Kevin Wolfb6495fa2015-07-10 18:09:18 +020061 OPTION_PATTERN = 260,
Kevin Wolf55d539c2016-06-03 13:59:41 +020062 OPTION_FLUSH_INTERVAL = 261,
63 OPTION_NO_DRAIN = 262,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +010064 OPTION_TARGET_IMAGE_OPTS = 263,
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +010065 OPTION_SIZE = 264,
Max Reitzdc5f6902017-06-13 22:20:55 +020066 OPTION_PREALLOCATION = 265,
Pavel Butsykin4ffca892017-09-18 15:42:27 +030067 OPTION_SHRINK = 266,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050068};
69
70typedef enum OutputFormat {
71 OFORMAT_JSON,
72 OFORMAT_HUMAN,
73} OutputFormat;
74
Kevin Wolfe6996142016-03-15 13:03:11 +010075/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040076#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000077
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010078static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000079{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010080 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000081}
82
Fam Zhengac1307a2014-04-22 13:36:11 +080083static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
84{
85 va_list ap;
86
87 error_printf("qemu-img: ");
88
89 va_start(ap, fmt);
90 error_vprintf(fmt, ap);
91 va_end(ap);
92
93 error_printf("\nTry 'qemu-img --help' for more information\n");
94 exit(EXIT_FAILURE);
95}
96
Stefan Hajnoczic9192972017-03-17 18:45:41 +080097static void QEMU_NORETURN missing_argument(const char *option)
98{
99 error_exit("missing argument for option '%s'", option);
100}
101
102static void QEMU_NORETURN unrecognized_option(const char *option)
103{
104 error_exit("unrecognized option '%s'", option);
105}
106
blueswir1d2c639d2009-01-24 18:19:25 +0000107/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +0800108static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +0000109{
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100110 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -0400111 QEMU_IMG_VERSION
Denis V. Lunev10985132016-06-17 17:44:13 +0300112 "usage: qemu-img [standard options] command [command options]\n"
malc3f020d72010-02-08 12:04:56 +0300113 "QEMU disk image utility\n"
114 "\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300115 " '-h', '--help' display this help and exit\n"
116 " '-V', '--version' output version information and exit\n"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +0300117 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
118 " specify tracing options\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300119 "\n"
malc3f020d72010-02-08 12:04:56 +0300120 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100121#define DEF(option, callback, arg_string) \
122 " " arg_string "\n"
123#include "qemu-img-cmds.h"
124#undef DEF
125#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +0300126 "\n"
127 "Command parameters:\n"
128 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000129 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
130 " manual page for a description of the object properties. The most common\n"
131 " object type is a 'secret', which is used to supply passwords and/or\n"
132 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300133 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400134 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800135 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
136 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100137 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
138 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300139 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200140 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
141 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
142 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300143 " 'output_filename' is the destination disk image filename\n"
144 " 'output_fmt' is the destination format\n"
145 " 'options' is a comma separated list of format specific options in a\n"
146 " name=value format. Use -o ? for an overview of the options supported by the\n"
147 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800148 " 'snapshot_param' is param used for internal snapshot, format\n"
149 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
150 " '[ID_OR_NAME]'\n"
151 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
152 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300153 " '-c' indicates that target image must be compressed (qcow format only)\n"
John Snow6e6e55f2017-07-17 20:34:22 -0400154 " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
155 " new backing file match exactly. The image doesn't need a working\n"
156 " backing file before rebasing in this case (useful for renaming the\n"
157 " backing file). For image creation, allow creating without attempting\n"
158 " to open the backing file.\n"
malc3f020d72010-02-08 12:04:56 +0300159 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200160 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100161 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200162 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
163 " contain only zeros for qemu-img to create a sparse image during\n"
164 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
165 " unallocated or zero sectors, and the destination image will always be\n"
166 " fully allocated\n"
BenoƮt Canetc054b3f2012-09-05 13:09:02 +0200167 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100168 " '-n' skips the target volume creation (useful if the volume is created\n"
169 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300170 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200171 "Parameters to check subcommand:\n"
172 " '-r' tries to repair any inconsistencies that are found during the check.\n"
173 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
174 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200175 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200176 "\n"
Peter Lieven2d9187b2017-02-28 13:40:07 +0100177 "Parameters to convert subcommand:\n"
178 " '-m' specifies how many coroutines work in parallel during the convert\n"
179 " process (defaults to 8)\n"
180 " '-W' allow to write to the target out of order rather than sequential\n"
181 "\n"
malc3f020d72010-02-08 12:04:56 +0300182 "Parameters to snapshot subcommand:\n"
183 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
184 " '-a' applies a snapshot (revert disk to saved state)\n"
185 " '-c' creates a snapshot\n"
186 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100187 " '-l' lists all snapshots in the given image\n"
188 "\n"
189 "Parameters to compare subcommand:\n"
190 " '-f' first image format\n"
191 " '-F' second image format\n"
Reda Sallahi86ce1f62016-08-10 04:43:12 +0200192 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
193 "\n"
194 "Parameters to dd subcommand:\n"
195 " 'bs=BYTES' read and write up to BYTES bytes at a time "
196 "(default: 512)\n"
197 " 'count=N' copy only N input blocks\n"
198 " 'if=FILE' read from FILE\n"
Reda Sallahif7c15532016-08-10 16:16:09 +0200199 " 'of=FILE' write to FILE\n"
200 " 'skip=N' skip N bs-sized blocks at the start of input\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100201
202 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100203 bdrv_iterate_format(format_print, NULL);
Eric Blakef5048cb2017-08-03 11:33:53 -0500204 printf("\n\n" QEMU_HELP_BOTTOM "\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800205 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000206}
207
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000208static QemuOptsList qemu_object_opts = {
209 .name = "object",
210 .implied_opt_name = "qom-type",
211 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
212 .desc = {
213 { }
214 },
215};
216
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000217static QemuOptsList qemu_source_opts = {
218 .name = "source",
219 .implied_opt_name = "file",
220 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
221 .desc = {
222 { }
223 },
224};
225
Stefan Weil7c30f652013-06-16 17:01:05 +0200226static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100227{
228 int ret = 0;
229 if (!quiet) {
230 va_list args;
231 va_start(args, fmt);
232 ret = vprintf(fmt, args);
233 va_end(args);
234 }
235 return ret;
236}
237
bellardea2384d2004-08-01 21:59:26 +0000238
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100239static int print_block_option_help(const char *filename, const char *fmt)
240{
241 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800242 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500243 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100244
245 /* Find driver and parse its options */
246 drv = bdrv_find_format(fmt);
247 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100248 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100249 return 1;
250 }
251
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800252 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100253 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500254 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100255 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100256 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800257 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100258 return 1;
259 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800260 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100261 }
262
Chunyan Liu83d05212014-06-05 17:20:51 +0800263 qemu_opts_print_help(create_opts);
264 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100265 return 0;
266}
267
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000268
Max Reitzefaa7c42016-03-16 19:54:38 +0100269static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100270 QemuOpts *opts, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800271 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000272{
273 QDict *options;
274 Error *local_err = NULL;
275 BlockBackend *blk;
276 options = qemu_opts_to_qdict(opts, NULL);
Fam Zheng335e9932017-05-03 00:35:39 +0800277 if (force_share) {
278 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
279 && !qdict_get_bool(options, BDRV_OPT_FORCE_SHARE)) {
280 error_report("--force-share/-U conflicts with image options");
Fam Zhengadb998c2017-05-15 22:10:14 +0800281 QDECREF(options);
Fam Zheng335e9932017-05-03 00:35:39 +0800282 return NULL;
283 }
Eric Blake579cf1d2017-05-15 14:54:39 -0500284 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800285 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100286 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000287 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100288 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000289 return NULL;
290 }
Kevin Wolfce099542016-03-15 13:01:04 +0100291 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000292
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000293 return blk;
294}
295
Max Reitzefaa7c42016-03-16 19:54:38 +0100296static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100297 QDict *options,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000298 const char *fmt, int flags,
Fam Zheng335e9932017-05-03 00:35:39 +0800299 bool writethrough, bool quiet,
300 bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000301{
302 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200303 Error *local_err = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100304
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100305 if (!options) {
306 options = qdict_new();
307 }
bellard75c23802004-08-27 21:28:58 +0000308 if (fmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500309 qdict_put_str(options, "driver", fmt);
bellard75c23802004-08-27 21:28:58 +0000310 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100311
Fam Zheng335e9932017-05-03 00:35:39 +0800312 if (force_share) {
Eric Blake579cf1d2017-05-15 14:54:39 -0500313 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800314 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100315 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500316 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100317 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000318 return NULL;
bellard75c23802004-08-27 21:28:58 +0000319 }
Kevin Wolfce099542016-03-15 13:01:04 +0100320 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100321
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200322 return blk;
bellard75c23802004-08-27 21:28:58 +0000323}
324
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000325
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100326static int img_add_key_secrets(void *opaque,
327 const char *name, const char *value,
328 Error **errp)
329{
330 QDict *options = opaque;
331
332 if (g_str_has_suffix(name, "key-secret")) {
Eric Blake187f47e2017-06-24 12:10:07 -0600333 qdict_put_str(options, name, value);
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100334 }
335
336 return 0;
337}
338
339static BlockBackend *img_open_new_file(const char *filename,
340 QemuOpts *create_opts,
341 const char *fmt, int flags,
342 bool writethrough, bool quiet,
343 bool force_share)
344{
345 QDict *options = NULL;
346
347 options = qdict_new();
348 qemu_opt_foreach(create_opts, img_add_key_secrets, options, &error_abort);
349
350 return img_open_file(filename, options, fmt, flags, writethrough, quiet,
351 force_share);
352}
353
354
Max Reitzefaa7c42016-03-16 19:54:38 +0100355static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000356 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100357 const char *fmt, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800358 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000359{
360 BlockBackend *blk;
361 if (image_opts) {
362 QemuOpts *opts;
363 if (fmt) {
364 error_report("--image-opts and --format are mutually exclusive");
365 return NULL;
366 }
367 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
368 filename, true);
369 if (!opts) {
370 return NULL;
371 }
Fam Zheng335e9932017-05-03 00:35:39 +0800372 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
373 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000374 } else {
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100375 blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
Fam Zheng335e9932017-05-03 00:35:39 +0800376 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000377 }
378 return blk;
379}
380
381
Chunyan Liu83d05212014-06-05 17:20:51 +0800382static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100383 const char *base_filename,
384 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200385{
Markus Armbruster6750e792015-02-12 17:43:08 +0100386 Error *err = NULL;
387
Kevin Wolfefa84d42009-05-18 16:42:12 +0200388 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100389 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100390 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100391 error_report("Backing file not supported for file format '%s'",
392 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100393 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900394 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200395 }
396 }
397 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100398 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100399 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100400 error_report("Backing file format not supported for file "
401 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100402 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900403 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200404 }
405 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900406 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200407}
408
Markus Armbruster606caa02017-02-21 21:14:04 +0100409static int64_t cvtnum(const char *s)
410{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100411 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100412 uint64_t value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100413
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100414 err = qemu_strtosz(s, NULL, &value);
415 if (err < 0) {
416 return err;
417 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100418 if (value > INT64_MAX) {
419 return -ERANGE;
420 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100421 return value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100422}
423
bellardea2384d2004-08-01 21:59:26 +0000424static int img_create(int argc, char **argv)
425{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200426 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100427 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000428 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000429 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000430 const char *filename;
431 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200432 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200433 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100434 bool quiet = false;
John Snow6e6e55f2017-07-17 20:34:22 -0400435 int flags = 0;
ths3b46e622007-09-17 08:09:54 +0000436
bellardea2384d2004-08-01 21:59:26 +0000437 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000438 static const struct option long_options[] = {
439 {"help", no_argument, 0, 'h'},
440 {"object", required_argument, 0, OPTION_OBJECT},
441 {0, 0, 0, 0}
442 };
John Snow6e6e55f2017-07-17 20:34:22 -0400443 c = getopt_long(argc, argv, ":F:b:f:ho:qu",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000444 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100445 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000446 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100447 }
bellardea2384d2004-08-01 21:59:26 +0000448 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800449 case ':':
450 missing_argument(argv[optind - 1]);
451 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100452 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800453 unrecognized_option(argv[optind - 1]);
454 break;
bellardea2384d2004-08-01 21:59:26 +0000455 case 'h':
456 help();
457 break;
aliguori9230eaf2009-03-28 17:55:19 +0000458 case 'F':
459 base_fmt = optarg;
460 break;
bellardea2384d2004-08-01 21:59:26 +0000461 case 'b':
462 base_filename = optarg;
463 break;
464 case 'f':
465 fmt = optarg;
466 break;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200467 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100468 if (!is_valid_option_list(optarg)) {
469 error_report("Invalid option list: %s", optarg);
470 goto fail;
471 }
472 if (!options) {
473 options = g_strdup(optarg);
474 } else {
475 char *old_options = options;
476 options = g_strdup_printf("%s,%s", options, optarg);
477 g_free(old_options);
478 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200479 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100480 case 'q':
481 quiet = true;
482 break;
John Snow6e6e55f2017-07-17 20:34:22 -0400483 case 'u':
484 flags |= BDRV_O_NO_BACKING;
485 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000486 case OPTION_OBJECT: {
487 QemuOpts *opts;
488 opts = qemu_opts_parse_noisily(&qemu_object_opts,
489 optarg, true);
490 if (!opts) {
491 goto fail;
492 }
493 } break;
bellardea2384d2004-08-01 21:59:26 +0000494 }
495 }
aliguori9230eaf2009-03-28 17:55:19 +0000496
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900497 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100498 filename = (optind < argc) ? argv[optind] : NULL;
499 if (options && has_help_option(options)) {
500 g_free(options);
501 return print_block_option_help(filename, fmt);
502 }
503
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100504 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800505 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100506 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100507 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900508
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000509 if (qemu_opts_foreach(&qemu_object_opts,
510 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200511 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000512 goto fail;
513 }
514
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100515 /* Get image size, if specified */
516 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100517 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100518
519 sval = cvtnum(argv[optind++]);
520 if (sval < 0) {
liguang79443392012-12-17 09:49:23 +0800521 if (sval == -ERANGE) {
522 error_report("Image size must be less than 8 EiB!");
523 } else {
524 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200525 "G, T, P or E suffixes for ");
526 error_report("kilobytes, megabytes, gigabytes, terabytes, "
527 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800528 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100529 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100530 }
531 img_size = (uint64_t)sval;
532 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200533 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800534 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200535 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100536
Luiz Capitulino9b375252012-11-30 10:52:05 -0200537 bdrv_img_create(filename, fmt, base_filename, base_fmt,
John Snow6e6e55f2017-07-17 20:34:22 -0400538 options, img_size, flags, quiet, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100539 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100540 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100541 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900542 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200543
Kevin Wolf77386bf2014-02-21 16:24:04 +0100544 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000545 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100546
547fail:
548 g_free(options);
549 return 1;
bellardea2384d2004-08-01 21:59:26 +0000550}
551
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100552static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500553{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500554 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500555 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100556 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600557
558 visit_type_ImageCheck(v, NULL, &check, &error_abort);
559 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500560 str = qobject_to_json_pretty(obj);
561 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100562 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500563 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600564 visit_free(v);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500565 QDECREF(str);
566}
567
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100568static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500569{
570 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100571 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500572 } else {
573 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100574 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
575 "Data may be corrupted, or further writes to the image "
576 "may corrupt it.\n",
577 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500578 }
579
580 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100581 qprintf(quiet,
582 "\n%" PRId64 " leaked clusters were found on the image.\n"
583 "This means waste of disk space, but no harm to data.\n",
584 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500585 }
586
587 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100588 qprintf(quiet,
589 "\n%" PRId64
590 " internal errors have occurred during the check.\n",
591 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500592 }
593 }
594
595 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100596 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
597 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
598 check->allocated_clusters, check->total_clusters,
599 check->allocated_clusters * 100.0 / check->total_clusters,
600 check->fragmented_clusters * 100.0 / check->allocated_clusters,
601 check->compressed_clusters * 100.0 /
602 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500603 }
604
605 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100606 qprintf(quiet,
607 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500608 }
609}
610
611static int collect_image_check(BlockDriverState *bs,
612 ImageCheck *check,
613 const char *filename,
614 const char *fmt,
615 int fix)
616{
617 int ret;
618 BdrvCheckResult result;
619
620 ret = bdrv_check(bs, &result, fix);
621 if (ret < 0) {
622 return ret;
623 }
624
625 check->filename = g_strdup(filename);
626 check->format = g_strdup(bdrv_get_format_name(bs));
627 check->check_errors = result.check_errors;
628 check->corruptions = result.corruptions;
629 check->has_corruptions = result.corruptions != 0;
630 check->leaks = result.leaks;
631 check->has_leaks = result.leaks != 0;
632 check->corruptions_fixed = result.corruptions_fixed;
633 check->has_corruptions_fixed = result.corruptions != 0;
634 check->leaks_fixed = result.leaks_fixed;
635 check->has_leaks_fixed = result.leaks != 0;
636 check->image_end_offset = result.image_end_offset;
637 check->has_image_end_offset = result.image_end_offset != 0;
638 check->total_clusters = result.bfi.total_clusters;
639 check->has_total_clusters = result.bfi.total_clusters != 0;
640 check->allocated_clusters = result.bfi.allocated_clusters;
641 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
642 check->fragmented_clusters = result.bfi.fragmented_clusters;
643 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100644 check->compressed_clusters = result.bfi.compressed_clusters;
645 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500646
647 return 0;
648}
649
Kevin Wolfe076f332010-06-29 11:43:13 +0200650/*
651 * Checks an image for consistency. Exit codes:
652 *
Max Reitzd6635c42014-06-02 22:15:21 +0200653 * 0 - Check completed, image is good
654 * 1 - Check not completed because of internal errors
655 * 2 - Check completed, image is corrupted
656 * 3 - Check completed, image has leaked clusters, but is good otherwise
657 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200658 */
aliguori15859692009-04-21 23:11:53 +0000659static int img_check(int argc, char **argv)
660{
661 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500662 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200663 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200664 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000665 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200666 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100667 int flags = BDRV_O_CHECK;
668 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200669 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100670 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000671 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +0800672 bool force_share = false;
aliguori15859692009-04-21 23:11:53 +0000673
674 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500675 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200676 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100677
aliguori15859692009-04-21 23:11:53 +0000678 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500679 int option_index = 0;
680 static const struct option long_options[] = {
681 {"help", no_argument, 0, 'h'},
682 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530683 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500684 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000685 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000686 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +0800687 {"force-share", no_argument, 0, 'U'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500688 {0, 0, 0, 0}
689 };
Fam Zheng335e9932017-05-03 00:35:39 +0800690 c = getopt_long(argc, argv, ":hf:r:T:qU",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500691 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100692 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000693 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100694 }
aliguori15859692009-04-21 23:11:53 +0000695 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800696 case ':':
697 missing_argument(argv[optind - 1]);
698 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100699 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800700 unrecognized_option(argv[optind - 1]);
701 break;
aliguori15859692009-04-21 23:11:53 +0000702 case 'h':
703 help();
704 break;
705 case 'f':
706 fmt = optarg;
707 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200708 case 'r':
709 flags |= BDRV_O_RDWR;
710
711 if (!strcmp(optarg, "leaks")) {
712 fix = BDRV_FIX_LEAKS;
713 } else if (!strcmp(optarg, "all")) {
714 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
715 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800716 error_exit("Unknown option value for -r "
717 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200718 }
719 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500720 case OPTION_OUTPUT:
721 output = optarg;
722 break;
Max Reitz40055952014-07-22 22:58:42 +0200723 case 'T':
724 cache = optarg;
725 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100726 case 'q':
727 quiet = true;
728 break;
Fam Zheng335e9932017-05-03 00:35:39 +0800729 case 'U':
730 force_share = true;
731 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000732 case OPTION_OBJECT: {
733 QemuOpts *opts;
734 opts = qemu_opts_parse_noisily(&qemu_object_opts,
735 optarg, true);
736 if (!opts) {
737 return 1;
738 }
739 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000740 case OPTION_IMAGE_OPTS:
741 image_opts = true;
742 break;
aliguori15859692009-04-21 23:11:53 +0000743 }
744 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200745 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800746 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100747 }
aliguori15859692009-04-21 23:11:53 +0000748 filename = argv[optind++];
749
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500750 if (output && !strcmp(output, "json")) {
751 output_format = OFORMAT_JSON;
752 } else if (output && !strcmp(output, "human")) {
753 output_format = OFORMAT_HUMAN;
754 } else if (output) {
755 error_report("--output must be used with human or json as argument.");
756 return 1;
757 }
758
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000759 if (qemu_opts_foreach(&qemu_object_opts,
760 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200761 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000762 return 1;
763 }
764
Kevin Wolfce099542016-03-15 13:01:04 +0100765 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200766 if (ret < 0) {
767 error_report("Invalid source cache option: %s", cache);
768 return 1;
769 }
770
Fam Zheng335e9932017-05-03 00:35:39 +0800771 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
772 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200773 if (!blk) {
774 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900775 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200776 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500777
778 check = g_new0(ImageCheck, 1);
779 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200780
781 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200782 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200783 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500784 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200785 }
786
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500787 if (check->corruptions_fixed || check->leaks_fixed) {
788 int corruptions_fixed, leaks_fixed;
789
790 leaks_fixed = check->leaks_fixed;
791 corruptions_fixed = check->corruptions_fixed;
792
793 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100794 qprintf(quiet,
795 "The following inconsistencies were found and repaired:\n\n"
796 " %" PRId64 " leaked clusters\n"
797 " %" PRId64 " corruptions\n\n"
798 "Double checking the fixed image now...\n",
799 check->leaks_fixed,
800 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500801 }
802
803 ret = collect_image_check(bs, check, filename, fmt, 0);
804
805 check->leaks_fixed = leaks_fixed;
806 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200807 }
808
Max Reitz832390a2014-10-23 15:29:12 +0200809 if (!ret) {
810 switch (output_format) {
811 case OFORMAT_HUMAN:
812 dump_human_image_check(check, quiet);
813 break;
814 case OFORMAT_JSON:
815 dump_json_image_check(check, quiet);
816 break;
817 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500818 }
819
820 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200821 if (ret) {
822 error_report("Check failed: %s", strerror(-ret));
823 } else {
824 error_report("Check failed");
825 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500826 ret = 1;
827 goto fail;
828 }
829
830 if (check->corruptions) {
831 ret = 2;
832 } else if (check->leaks) {
833 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200834 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500835 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000836 }
837
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500838fail:
839 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200840 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500841 return ret;
aliguori15859692009-04-21 23:11:53 +0000842}
843
Max Reitzd4a32382014-10-24 15:57:37 +0200844typedef struct CommonBlockJobCBInfo {
845 BlockDriverState *bs;
846 Error **errp;
847} CommonBlockJobCBInfo;
848
849static void common_block_job_cb(void *opaque, int ret)
850{
851 CommonBlockJobCBInfo *cbi = opaque;
852
853 if (ret < 0) {
854 error_setg_errno(cbi->errp, -ret, "Block job failed");
855 }
Max Reitzd4a32382014-10-24 15:57:37 +0200856}
857
858static void run_block_job(BlockJob *job, Error **errp)
859{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200860 AioContext *aio_context = blk_get_aio_context(job->blk);
sochin.jiang4172a002017-06-15 14:47:33 +0800861 int ret = 0;
Max Reitzd4a32382014-10-24 15:57:37 +0200862
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200863 aio_context_acquire(aio_context);
sochin.jiang4172a002017-06-15 14:47:33 +0800864 block_job_ref(job);
Max Reitzd4a32382014-10-24 15:57:37 +0200865 do {
866 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500867 qemu_progress_print(job->len ?
868 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
sochin.jiang4172a002017-06-15 14:47:33 +0800869 } while (!job->ready && !job->completed);
Max Reitzd4a32382014-10-24 15:57:37 +0200870
sochin.jiang4172a002017-06-15 14:47:33 +0800871 if (!job->completed) {
872 ret = block_job_complete_sync(job, errp);
873 } else {
874 ret = job->ret;
875 }
876 block_job_unref(job);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200877 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200878
sochin.jiang4172a002017-06-15 14:47:33 +0800879 /* publish completion progress only when success */
880 if (!ret) {
881 qemu_progress_print(100.f, 0);
882 }
Max Reitzd4a32382014-10-24 15:57:37 +0200883}
884
bellardea2384d2004-08-01 21:59:26 +0000885static int img_commit(int argc, char **argv)
886{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400887 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200888 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200889 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200890 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100891 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200892 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100893 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200894 Error *local_err = NULL;
895 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000896 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200897 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000898
899 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400900 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200901 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000902 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000903 static const struct option long_options[] = {
904 {"help", no_argument, 0, 'h'},
905 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000906 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000907 {0, 0, 0, 0}
908 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800909 c = getopt_long(argc, argv, ":f:ht:b:dpq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000910 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100911 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000912 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100913 }
bellardea2384d2004-08-01 21:59:26 +0000914 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800915 case ':':
916 missing_argument(argv[optind - 1]);
917 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100918 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800919 unrecognized_option(argv[optind - 1]);
920 break;
bellardea2384d2004-08-01 21:59:26 +0000921 case 'h':
922 help();
923 break;
924 case 'f':
925 fmt = optarg;
926 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400927 case 't':
928 cache = optarg;
929 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200930 case 'b':
931 base = optarg;
932 /* -b implies -d */
933 drop = true;
934 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200935 case 'd':
936 drop = true;
937 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200938 case 'p':
939 progress = true;
940 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100941 case 'q':
942 quiet = true;
943 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000944 case OPTION_OBJECT: {
945 QemuOpts *opts;
946 opts = qemu_opts_parse_noisily(&qemu_object_opts,
947 optarg, true);
948 if (!opts) {
949 return 1;
950 }
951 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000952 case OPTION_IMAGE_OPTS:
953 image_opts = true;
954 break;
bellardea2384d2004-08-01 21:59:26 +0000955 }
956 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200957
958 /* Progress is not shown in Quiet mode */
959 if (quiet) {
960 progress = false;
961 }
962
Kevin Wolffc11eb22013-08-05 10:53:04 +0200963 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800964 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100965 }
bellardea2384d2004-08-01 21:59:26 +0000966 filename = argv[optind++];
967
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000968 if (qemu_opts_foreach(&qemu_object_opts,
969 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200970 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000971 return 1;
972 }
973
Max Reitz9a86fe42014-10-24 15:57:38 +0200974 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100975 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400976 if (ret < 0) {
977 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100978 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400979 }
980
Fam Zheng335e9932017-05-03 00:35:39 +0800981 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
982 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200983 if (!blk) {
984 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900985 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200986 bs = blk_bs(blk);
987
Max Reitz687fa1d2014-10-24 15:57:39 +0200988 qemu_progress_init(progress, 1.f);
989 qemu_progress_print(0.f, 100);
990
Max Reitz1b22bff2014-10-24 15:57:40 +0200991 if (base) {
992 base_bs = bdrv_find_backing_image(bs, base);
993 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +0100994 error_setg(&local_err,
995 "Did not find '%s' in the backing chain of '%s'",
996 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +0200997 goto done;
998 }
999 } else {
1000 /* This is different from QMP, which by default uses the deepest file in
1001 * the backing chain (i.e., the very base); however, the traditional
1002 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +02001003 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +02001004 if (!base_bs) {
1005 error_setg(&local_err, "Image does not have a backing file");
1006 goto done;
1007 }
bellardea2384d2004-08-01 21:59:26 +00001008 }
1009
Max Reitzd4a32382014-10-24 15:57:37 +02001010 cbi = (CommonBlockJobCBInfo){
1011 .errp = &local_err,
1012 .bs = bs,
1013 };
1014
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001015 aio_context = bdrv_get_aio_context(bs);
1016 aio_context_acquire(aio_context);
John Snow47970df2016-10-27 12:06:57 -04001017 commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
Kevin Wolf0db832f2017-02-20 18:10:05 +01001018 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +08001019 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001020 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +02001021 if (local_err) {
1022 goto done;
1023 }
1024
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001025 /* When the block job completes, the BlockBackend reference will point to
1026 * the old backing file. In order to avoid that the top image is already
1027 * deleted, so we can still empty it afterwards, increment the reference
1028 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +02001029 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001030 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001031 }
1032
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001033 job = block_job_get("commit");
1034 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001035 if (local_err) {
1036 goto unref_backing;
1037 }
1038
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001039 if (!drop && bs->drv->bdrv_make_empty) {
1040 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001041 if (ret) {
1042 error_setg_errno(&local_err, -ret, "Could not empty %s",
1043 filename);
1044 goto unref_backing;
1045 }
1046 }
1047
1048unref_backing:
1049 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001050 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001051 }
Max Reitzd4a32382014-10-24 15:57:37 +02001052
1053done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001054 qemu_progress_end();
1055
Markus Armbruster26f54e92014-10-07 13:59:04 +02001056 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001057
1058 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001059 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001060 return 1;
1061 }
Max Reitzd4a32382014-10-24 15:57:37 +02001062
1063 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001064 return 0;
1065}
1066
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001067/*
Eric Blakedebb38a2017-10-11 22:47:11 -05001068 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1069 * of the first sector boundary within buf where the sector contains a
1070 * non-zero byte. This function is robust to a buffer that is not
1071 * sector-aligned.
1072 */
1073static int64_t find_nonzero(const uint8_t *buf, int64_t n)
1074{
1075 int64_t i;
1076 int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE);
1077
1078 for (i = 0; i < end; i += BDRV_SECTOR_SIZE) {
1079 if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) {
1080 return i;
1081 }
1082 }
1083 if (i < n && !buffer_is_zero(buf + i, n - end)) {
1084 return i;
1085 }
1086 return -1;
1087}
1088
1089/*
thsf58c7b32008-06-05 21:53:49 +00001090 * Returns true iff the first sector pointed to by 'buf' contains at least
1091 * a non-NUL byte.
1092 *
1093 * 'pnum' is set to the number of sectors (including and immediately following
1094 * the first one) that are known to be in the same allocated/unallocated state.
1095 */
bellardea2384d2004-08-01 21:59:26 +00001096static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1097{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001098 bool is_zero;
1099 int i;
bellardea2384d2004-08-01 21:59:26 +00001100
1101 if (n <= 0) {
1102 *pnum = 0;
1103 return 0;
1104 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001105 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001106 for(i = 1; i < n; i++) {
1107 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001108 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001109 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001110 }
bellardea2384d2004-08-01 21:59:26 +00001111 }
1112 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001113 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001114}
1115
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001116/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001117 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1118 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1119 * breaking up write requests for only small sparse areas.
1120 */
1121static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1122 int min)
1123{
1124 int ret;
1125 int num_checked, num_used;
1126
1127 if (n < min) {
1128 min = n;
1129 }
1130
1131 ret = is_allocated_sectors(buf, n, pnum);
1132 if (!ret) {
1133 return ret;
1134 }
1135
1136 num_used = *pnum;
1137 buf += BDRV_SECTOR_SIZE * *pnum;
1138 n -= *pnum;
1139 num_checked = num_used;
1140
1141 while (n > 0) {
1142 ret = is_allocated_sectors(buf, n, pnum);
1143
1144 buf += BDRV_SECTOR_SIZE * *pnum;
1145 n -= *pnum;
1146 num_checked += *pnum;
1147 if (ret) {
1148 num_used = num_checked;
1149 } else if (*pnum >= min) {
1150 break;
1151 }
1152 }
1153
1154 *pnum = num_used;
1155 return 1;
1156}
1157
1158/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001159 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1160 * buffers matches, non-zero otherwise.
1161 *
1162 * pnum is set to the number of sectors (including and immediately following
1163 * the first one) that are known to have the same comparison result
1164 */
1165static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1166 int *pnum)
1167{
Radim KrčmÔř8c1ac472015-02-20 17:06:15 +01001168 bool res;
1169 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001170
1171 if (n <= 0) {
1172 *pnum = 0;
1173 return 0;
1174 }
1175
1176 res = !!memcmp(buf1, buf2, 512);
1177 for(i = 1; i < n; i++) {
1178 buf1 += 512;
1179 buf2 += 512;
1180
1181 if (!!memcmp(buf1, buf2, 512) != res) {
1182 break;
1183 }
1184 }
1185
1186 *pnum = i;
1187 return res;
1188}
1189
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001190#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001191
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001192static int64_t sectors_to_bytes(int64_t sectors)
1193{
1194 return sectors << BDRV_SECTOR_BITS;
1195}
1196
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001197/*
1198 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1199 *
1200 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1201 * data and negative value on error.
1202 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001203 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001204 * @param sect_num: Number of first sector to check
1205 * @param sect_count: Number of sectors to check
1206 * @param filename: Name of disk file we are checking (logging purpose)
1207 * @param buffer: Allocated buffer for storing read data
1208 * @param quiet: Flag for quiet mode
1209 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001210static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001211 int sect_count, const char *filename,
1212 uint8_t *buffer, bool quiet)
1213{
Eric Blakedebb38a2017-10-11 22:47:11 -05001214 int ret = 0;
1215 int64_t idx;
1216
Eric Blake91669202016-05-06 10:26:43 -06001217 ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer,
1218 sect_count << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001219 if (ret < 0) {
1220 error_report("Error while reading offset %" PRId64 " of %s: %s",
1221 sectors_to_bytes(sect_num), filename, strerror(-ret));
1222 return ret;
1223 }
Eric Blakedebb38a2017-10-11 22:47:11 -05001224 idx = find_nonzero(buffer, sect_count * BDRV_SECTOR_SIZE);
1225 if (idx >= 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001226 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blakedebb38a2017-10-11 22:47:11 -05001227 sectors_to_bytes(sect_num) + idx);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001228 return 1;
1229 }
1230
1231 return 0;
1232}
1233
1234/*
1235 * Compares two images. Exit codes:
1236 *
1237 * 0 - Images are identical
1238 * 1 - Images differ
1239 * >1 - Error occurred
1240 */
1241static int img_compare(int argc, char **argv)
1242{
Max Reitz40055952014-07-22 22:58:42 +02001243 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001244 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001245 BlockDriverState *bs1, *bs2;
1246 int64_t total_sectors1, total_sectors2;
1247 uint8_t *buf1 = NULL, *buf2 = NULL;
Eric Blake31826642017-10-11 22:47:08 -05001248 int64_t pnum1, pnum2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001249 int allocated1, allocated2;
1250 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1251 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001252 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001253 bool writethrough;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001254 int64_t total_sectors;
1255 int64_t sector_num = 0;
1256 int64_t nb_sectors;
1257 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001258 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001259 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001260 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001261
Max Reitz40055952014-07-22 22:58:42 +02001262 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001263 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001264 static const struct option long_options[] = {
1265 {"help", no_argument, 0, 'h'},
1266 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001267 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001268 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001269 {0, 0, 0, 0}
1270 };
Fam Zheng335e9932017-05-03 00:35:39 +08001271 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001272 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001273 if (c == -1) {
1274 break;
1275 }
1276 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001277 case ':':
1278 missing_argument(argv[optind - 1]);
1279 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001280 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001281 unrecognized_option(argv[optind - 1]);
1282 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001283 case 'h':
1284 help();
1285 break;
1286 case 'f':
1287 fmt1 = optarg;
1288 break;
1289 case 'F':
1290 fmt2 = optarg;
1291 break;
Max Reitz40055952014-07-22 22:58:42 +02001292 case 'T':
1293 cache = optarg;
1294 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001295 case 'p':
1296 progress = true;
1297 break;
1298 case 'q':
1299 quiet = true;
1300 break;
1301 case 's':
1302 strict = true;
1303 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001304 case 'U':
1305 force_share = true;
1306 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001307 case OPTION_OBJECT: {
1308 QemuOpts *opts;
1309 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1310 optarg, true);
1311 if (!opts) {
1312 ret = 2;
1313 goto out4;
1314 }
1315 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001316 case OPTION_IMAGE_OPTS:
1317 image_opts = true;
1318 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001319 }
1320 }
1321
1322 /* Progress is not shown in Quiet mode */
1323 if (quiet) {
1324 progress = false;
1325 }
1326
1327
Kevin Wolffc11eb22013-08-05 10:53:04 +02001328 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001329 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001330 }
1331 filename1 = argv[optind++];
1332 filename2 = argv[optind++];
1333
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001334 if (qemu_opts_foreach(&qemu_object_opts,
1335 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001336 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001337 ret = 2;
1338 goto out4;
1339 }
1340
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001341 /* Initialize before goto out */
1342 qemu_progress_init(progress, 2.0);
1343
Kevin Wolfce099542016-03-15 13:01:04 +01001344 flags = 0;
1345 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001346 if (ret < 0) {
1347 error_report("Invalid source cache option: %s", cache);
1348 ret = 2;
1349 goto out3;
1350 }
1351
Fam Zheng335e9932017-05-03 00:35:39 +08001352 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1353 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001354 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001355 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001356 goto out3;
1357 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001358
Fam Zheng335e9932017-05-03 00:35:39 +08001359 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1360 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001361 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001362 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001363 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001364 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001365 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001366 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001367
Max Reitzf1d3cd72015-02-05 13:58:18 -05001368 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1369 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1370 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001371 if (total_sectors1 < 0) {
1372 error_report("Can't get size of %s: %s",
1373 filename1, strerror(-total_sectors1));
1374 ret = 4;
1375 goto out;
1376 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001377 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001378 if (total_sectors2 < 0) {
1379 error_report("Can't get size of %s: %s",
1380 filename2, strerror(-total_sectors2));
1381 ret = 4;
1382 goto out;
1383 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001384 total_sectors = MIN(total_sectors1, total_sectors2);
1385 progress_base = MAX(total_sectors1, total_sectors2);
1386
1387 qemu_progress_print(0, 100);
1388
1389 if (strict && total_sectors1 != total_sectors2) {
1390 ret = 1;
1391 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1392 goto out;
1393 }
1394
Eric Blake7daddc62017-10-11 22:47:09 -05001395 while (sector_num < total_sectors) {
Eric Blake31826642017-10-11 22:47:08 -05001396 int status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001397
Eric Blake31826642017-10-11 22:47:08 -05001398 status1 = bdrv_block_status_above(bs1, NULL,
1399 sector_num * BDRV_SECTOR_SIZE,
1400 (total_sectors1 - sector_num) *
1401 BDRV_SECTOR_SIZE,
1402 &pnum1, NULL, NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001403 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001404 ret = 3;
1405 error_report("Sector allocation test failed for %s", filename1);
1406 goto out;
1407 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001408 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001409
Eric Blake31826642017-10-11 22:47:08 -05001410 status2 = bdrv_block_status_above(bs2, NULL,
1411 sector_num * BDRV_SECTOR_SIZE,
1412 (total_sectors2 - sector_num) *
1413 BDRV_SECTOR_SIZE,
1414 &pnum2, NULL, NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001415 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001416 ret = 3;
1417 error_report("Sector allocation test failed for %s", filename2);
1418 goto out;
1419 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001420 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
Eric Blake31826642017-10-11 22:47:08 -05001421 /* TODO: Relax this once comparison is byte-based, and we no longer
1422 * have to worry about sector alignment */
1423 assert(QEMU_IS_ALIGNED(pnum1 | pnum2, BDRV_SECTOR_SIZE));
Eric Blake7daddc62017-10-11 22:47:09 -05001424
1425 assert(pnum1 && pnum2);
1426 nb_sectors = MIN(pnum1, pnum2) >> BDRV_SECTOR_BITS;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001427
Fam Zheng25ad8e62016-01-13 16:37:41 +08001428 if (strict) {
Eric Blake31826642017-10-11 22:47:08 -05001429 if (status1 != status2) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001430 ret = 1;
1431 qprintf(quiet, "Strict mode: Offset %" PRId64
1432 " block status mismatch!\n",
1433 sectors_to_bytes(sector_num));
1434 goto out;
1435 }
1436 }
1437 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
Eric Blake7daddc62017-10-11 22:47:09 -05001438 /* nothing to do */
Fam Zheng25ad8e62016-01-13 16:37:41 +08001439 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001440 if (allocated1) {
Eric Blake7daddc62017-10-11 22:47:09 -05001441 nb_sectors = MIN(nb_sectors, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
Eric Blake91669202016-05-06 10:26:43 -06001442 ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1,
1443 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001444 if (ret < 0) {
1445 error_report("Error while reading offset %" PRId64 " of %s:"
1446 " %s", sectors_to_bytes(sector_num), filename1,
1447 strerror(-ret));
1448 ret = 4;
1449 goto out;
1450 }
Eric Blake91669202016-05-06 10:26:43 -06001451 ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2,
1452 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001453 if (ret < 0) {
1454 error_report("Error while reading offset %" PRId64
1455 " of %s: %s", sectors_to_bytes(sector_num),
1456 filename2, strerror(-ret));
1457 ret = 4;
1458 goto out;
1459 }
1460 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1461 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001462 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1463 sectors_to_bytes(
1464 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001465 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001466 goto out;
1467 }
1468 }
1469 } else {
Eric Blake7daddc62017-10-11 22:47:09 -05001470 nb_sectors = MIN(nb_sectors, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001471 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001472 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001473 filename1, buf1, quiet);
1474 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001475 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001476 filename2, buf1, quiet);
1477 }
1478 if (ret) {
1479 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001480 error_report("Error while reading offset %" PRId64 ": %s",
1481 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001482 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001483 }
1484 goto out;
1485 }
1486 }
1487 sector_num += nb_sectors;
1488 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1489 }
1490
1491 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001492 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001493 const char *filename_over;
1494
1495 qprintf(quiet, "Warning: Image size mismatch!\n");
1496 if (total_sectors1 > total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001497 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001498 filename_over = filename1;
1499 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001500 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001501 filename_over = filename2;
1502 }
1503
Eric Blake7daddc62017-10-11 22:47:09 -05001504 while (sector_num < progress_base) {
Eric Blake51b0a482017-07-07 07:44:59 -05001505 int64_t count;
1506
Eric Blake391cb1a2017-10-11 22:47:10 -05001507 ret = bdrv_block_status_above(blk_bs(blk_over), NULL,
Eric Blake51b0a482017-07-07 07:44:59 -05001508 sector_num * BDRV_SECTOR_SIZE,
Eric Blake7daddc62017-10-11 22:47:09 -05001509 (progress_base - sector_num) *
1510 BDRV_SECTOR_SIZE,
Eric Blake391cb1a2017-10-11 22:47:10 -05001511 &count, NULL, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001512 if (ret < 0) {
1513 ret = 3;
1514 error_report("Sector allocation test failed for %s",
1515 filename_over);
1516 goto out;
1517
1518 }
Eric Blake391cb1a2017-10-11 22:47:10 -05001519 /* TODO relax this once bdrv_block_status_above does not enforce
Eric Blake51b0a482017-07-07 07:44:59 -05001520 * sector alignment */
1521 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
1522 nb_sectors = count >> BDRV_SECTOR_BITS;
Eric Blake391cb1a2017-10-11 22:47:10 -05001523 if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) {
Eric Blake7daddc62017-10-11 22:47:09 -05001524 nb_sectors = MIN(nb_sectors, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
Max Reitzf1d3cd72015-02-05 13:58:18 -05001525 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001526 filename_over, buf1, quiet);
1527 if (ret) {
1528 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001529 error_report("Error while reading offset %" PRId64
1530 " of %s: %s", sectors_to_bytes(sector_num),
1531 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001532 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001533 }
1534 goto out;
1535 }
1536 }
1537 sector_num += nb_sectors;
1538 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1539 }
1540 }
1541
1542 qprintf(quiet, "Images are identical.\n");
1543 ret = 0;
1544
1545out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001546 qemu_vfree(buf1);
1547 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001548 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001549out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001550 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001551out3:
1552 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001553out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001554 return ret;
1555}
1556
Kevin Wolf690c7302015-03-19 13:33:32 +01001557enum ImgConvertBlockStatus {
1558 BLK_DATA,
1559 BLK_ZERO,
1560 BLK_BACKING_FILE,
1561};
1562
Peter Lieven2d9187b2017-02-28 13:40:07 +01001563#define MAX_COROUTINES 16
1564
Kevin Wolf690c7302015-03-19 13:33:32 +01001565typedef struct ImgConvertState {
1566 BlockBackend **src;
1567 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001568 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001569 int64_t total_sectors;
1570 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001571 int64_t allocated_done;
1572 int64_t sector_num;
1573 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001574 enum ImgConvertBlockStatus status;
1575 int64_t sector_next_status;
1576 BlockBackend *target;
1577 bool has_zero_init;
1578 bool compressed;
1579 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001580 bool wr_in_order;
Kevin Wolf690c7302015-03-19 13:33:32 +01001581 int min_sparse;
1582 size_t cluster_sectors;
1583 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001584 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001585 int running_coroutines;
1586 Coroutine *co[MAX_COROUTINES];
1587 int64_t wait_sector_num[MAX_COROUTINES];
1588 CoMutex lock;
1589 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001590} ImgConvertState;
1591
Peter Lieven2d9187b2017-02-28 13:40:07 +01001592static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1593 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001594{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001595 *src_cur = 0;
1596 *src_cur_offset = 0;
1597 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1598 *src_cur_offset += s->src_sectors[*src_cur];
1599 (*src_cur)++;
1600 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001601 }
1602}
1603
1604static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1605{
Eric Blake31826642017-10-11 22:47:08 -05001606 int64_t src_cur_offset;
1607 int ret, n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001608
Peter Lieven2d9187b2017-02-28 13:40:07 +01001609 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001610
1611 assert(s->total_sectors > sector_num);
1612 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1613
1614 if (s->sector_next_status <= sector_num) {
Eric Blake31826642017-10-11 22:47:08 -05001615 int64_t count = n * BDRV_SECTOR_SIZE;
1616
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001617 if (s->target_has_backing) {
Eric Blake237d78f2017-10-11 22:47:03 -05001618
1619 ret = bdrv_block_status(blk_bs(s->src[src_cur]),
1620 (sector_num - src_cur_offset) *
1621 BDRV_SECTOR_SIZE,
1622 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001623 } else {
Eric Blake31826642017-10-11 22:47:08 -05001624 ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL,
1625 (sector_num - src_cur_offset) *
1626 BDRV_SECTOR_SIZE,
1627 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001628 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001629 if (ret < 0) {
1630 return ret;
1631 }
Eric Blake31826642017-10-11 22:47:08 -05001632 n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
Kevin Wolf690c7302015-03-19 13:33:32 +01001633
1634 if (ret & BDRV_BLOCK_ZERO) {
1635 s->status = BLK_ZERO;
1636 } else if (ret & BDRV_BLOCK_DATA) {
1637 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001638 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001639 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001640 }
1641
1642 s->sector_next_status = sector_num + n;
1643 }
1644
1645 n = MIN(n, s->sector_next_status - sector_num);
1646 if (s->status == BLK_DATA) {
1647 n = MIN(n, s->buf_sectors);
1648 }
1649
1650 /* We need to write complete clusters for compressed images, so if an
1651 * unallocated area is shorter than that, we must consider the whole
1652 * cluster allocated. */
1653 if (s->compressed) {
1654 if (n < s->cluster_sectors) {
1655 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1656 s->status = BLK_DATA;
1657 } else {
1658 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1659 }
1660 }
1661
1662 return n;
1663}
1664
Peter Lieven2d9187b2017-02-28 13:40:07 +01001665static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1666 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001667{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001668 int n, ret;
1669 QEMUIOVector qiov;
1670 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001671
Kevin Wolf690c7302015-03-19 13:33:32 +01001672 assert(nb_sectors <= s->buf_sectors);
1673 while (nb_sectors > 0) {
1674 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001675 int src_cur;
1676 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001677
1678 /* In the case of compression with multiple source files, we can get a
1679 * nb_sectors that spreads into the next part. So we must be able to
1680 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001681 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1682 blk = s->src[src_cur];
1683 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001684
Peter Lieven2d9187b2017-02-28 13:40:07 +01001685 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1686 iov.iov_base = buf;
1687 iov.iov_len = n << BDRV_SECTOR_BITS;
1688 qemu_iovec_init_external(&qiov, &iov, 1);
1689
1690 ret = blk_co_preadv(
1691 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1692 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001693 if (ret < 0) {
1694 return ret;
1695 }
1696
1697 sector_num += n;
1698 nb_sectors -= n;
1699 buf += n * BDRV_SECTOR_SIZE;
1700 }
1701
1702 return 0;
1703}
1704
Peter Lieven2d9187b2017-02-28 13:40:07 +01001705
1706static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1707 int nb_sectors, uint8_t *buf,
1708 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001709{
1710 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001711 QEMUIOVector qiov;
1712 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001713
1714 while (nb_sectors > 0) {
1715 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001716 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1717
Peter Lieven2d9187b2017-02-28 13:40:07 +01001718 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001719 case BLK_BACKING_FILE:
1720 /* If we have a backing file, leave clusters unallocated that are
1721 * unallocated in the source image, so that the backing file is
1722 * visible at the respective offset. */
1723 assert(s->target_has_backing);
1724 break;
1725
1726 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001727 /* If we're told to keep the target fully allocated (-S 0) or there
1728 * is real non-zero data, we must write it. Otherwise we can treat
1729 * it as zero sectors.
1730 * Compressed clusters need to be written as a whole, so in that
1731 * case we can only save the write if the buffer is completely
1732 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001733 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001734 (!s->compressed &&
1735 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1736 (s->compressed &&
1737 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001738 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001739 iov.iov_base = buf;
1740 iov.iov_len = n << BDRV_SECTOR_BITS;
1741 qemu_iovec_init_external(&qiov, &iov, 1);
1742
1743 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
Lidong Chendb933fb2017-04-27 10:58:27 +08001744 n << BDRV_SECTOR_BITS, &qiov, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001745 if (ret < 0) {
1746 return ret;
1747 }
1748 break;
1749 }
1750 /* fall-through */
1751
1752 case BLK_ZERO:
1753 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001754 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001755 break;
1756 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001757 ret = blk_co_pwrite_zeroes(s->target,
1758 sector_num << BDRV_SECTOR_BITS,
1759 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001760 if (ret < 0) {
1761 return ret;
1762 }
1763 break;
1764 }
1765
1766 sector_num += n;
1767 nb_sectors -= n;
1768 buf += n * BDRV_SECTOR_SIZE;
1769 }
1770
1771 return 0;
1772}
1773
Peter Lieven2d9187b2017-02-28 13:40:07 +01001774static void coroutine_fn convert_co_do_copy(void *opaque)
1775{
1776 ImgConvertState *s = opaque;
1777 uint8_t *buf = NULL;
1778 int ret, i;
1779 int index = -1;
1780
1781 for (i = 0; i < s->num_coroutines; i++) {
1782 if (s->co[i] == qemu_coroutine_self()) {
1783 index = i;
1784 break;
1785 }
1786 }
1787 assert(index >= 0);
1788
1789 s->running_coroutines++;
1790 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1791
1792 while (1) {
1793 int n;
1794 int64_t sector_num;
1795 enum ImgConvertBlockStatus status;
1796
1797 qemu_co_mutex_lock(&s->lock);
1798 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1799 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001800 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001801 }
1802 n = convert_iteration_sectors(s, s->sector_num);
1803 if (n < 0) {
1804 qemu_co_mutex_unlock(&s->lock);
1805 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001806 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001807 }
1808 /* save current sector and allocation status to local variables */
1809 sector_num = s->sector_num;
1810 status = s->status;
1811 if (!s->min_sparse && s->status == BLK_ZERO) {
1812 n = MIN(n, s->buf_sectors);
1813 }
1814 /* increment global sector counter so that other coroutines can
1815 * already continue reading beyond this request */
1816 s->sector_num += n;
1817 qemu_co_mutex_unlock(&s->lock);
1818
1819 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1820 s->allocated_done += n;
1821 qemu_progress_print(100.0 * s->allocated_done /
1822 s->allocated_sectors, 0);
1823 }
1824
1825 if (status == BLK_DATA) {
1826 ret = convert_co_read(s, sector_num, n, buf);
1827 if (ret < 0) {
1828 error_report("error while reading sector %" PRId64
1829 ": %s", sector_num, strerror(-ret));
1830 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001831 }
1832 } else if (!s->min_sparse && status == BLK_ZERO) {
1833 status = BLK_DATA;
1834 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1835 }
1836
1837 if (s->wr_in_order) {
1838 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03001839 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001840 s->wait_sector_num[index] = sector_num;
1841 qemu_coroutine_yield();
1842 }
1843 s->wait_sector_num[index] = -1;
1844 }
1845
Anton Nefedovb91127e2017-04-26 11:33:15 +03001846 if (s->ret == -EINPROGRESS) {
1847 ret = convert_co_write(s, sector_num, n, buf, status);
1848 if (ret < 0) {
1849 error_report("error while writing sector %" PRId64
1850 ": %s", sector_num, strerror(-ret));
1851 s->ret = ret;
1852 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001853 }
1854
1855 if (s->wr_in_order) {
1856 /* reenter the coroutine that might have waited
1857 * for this write to complete */
1858 s->wr_offs = sector_num + n;
1859 for (i = 0; i < s->num_coroutines; i++) {
1860 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1861 /*
1862 * A -> B -> A cannot occur because A has
1863 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1864 * B will never enter A during this time window.
1865 */
1866 qemu_coroutine_enter(s->co[i]);
1867 break;
1868 }
1869 }
1870 }
1871 }
1872
Peter Lieven2d9187b2017-02-28 13:40:07 +01001873 qemu_vfree(buf);
1874 s->co[index] = NULL;
1875 s->running_coroutines--;
1876 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1877 /* the convert job finished successfully */
1878 s->ret = 0;
1879 }
1880}
1881
Kevin Wolf690c7302015-03-19 13:33:32 +01001882static int convert_do_copy(ImgConvertState *s)
1883{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001884 int ret, i, n;
1885 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001886
1887 /* Check whether we have zero initialisation or can get it efficiently */
1888 s->has_zero_init = s->min_sparse && !s->target_has_backing
1889 ? bdrv_has_zero_init(blk_bs(s->target))
1890 : false;
1891
1892 if (!s->has_zero_init && !s->target_has_backing &&
1893 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1894 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001895 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001896 if (ret == 0) {
1897 s->has_zero_init = true;
1898 }
1899 }
1900
1901 /* Allocate buffer for copied data. For compressed images, only one cluster
1902 * can be copied at a time. */
1903 if (s->compressed) {
1904 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1905 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001906 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001907 }
1908 s->buf_sectors = s->cluster_sectors;
1909 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001910
Kevin Wolf690c7302015-03-19 13:33:32 +01001911 while (sector_num < s->total_sectors) {
1912 n = convert_iteration_sectors(s, sector_num);
1913 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001914 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001915 }
Max Reitzaad15de2016-03-24 23:33:57 +01001916 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1917 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001918 s->allocated_sectors += n;
1919 }
1920 sector_num += n;
1921 }
1922
1923 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001924 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001925 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001926
Peter Lieven2d9187b2017-02-28 13:40:07 +01001927 qemu_co_mutex_init(&s->lock);
1928 for (i = 0; i < s->num_coroutines; i++) {
1929 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1930 s->wait_sector_num[i] = -1;
1931 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001932 }
1933
Anton Nefedovb91127e2017-04-26 11:33:15 +03001934 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001935 main_loop_wait(false);
1936 }
1937
1938 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001939 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001940 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001941 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001942 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001943 }
1944 }
1945
Peter Lieven2d9187b2017-02-28 13:40:07 +01001946 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001947}
1948
bellardea2384d2004-08-01 21:59:26 +00001949static int img_convert(int argc, char **argv)
1950{
Peter Lieven9fd77f92017-04-21 11:11:55 +02001951 int c, bs_i, flags, src_flags = 0;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001952 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02001953 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1954 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001955 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00001956 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001957 BlockDriverState *out_bs;
1958 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08001959 QemuOptsList *create_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001960 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02001961 Error *local_err = NULL;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001962 bool writethrough, src_writethrough, quiet = false, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001963 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001964 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08001965 bool force_share = false;
bellardea2384d2004-08-01 21:59:26 +00001966
Peter Lieven9fd77f92017-04-21 11:11:55 +02001967 ImgConvertState s = (ImgConvertState) {
1968 /* Need at least 4k of zeros for sparse detection */
1969 .min_sparse = 8,
1970 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1971 .wr_in_order = true,
1972 .num_coroutines = 8,
1973 };
1974
bellardea2384d2004-08-01 21:59:26 +00001975 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001976 static const struct option long_options[] = {
1977 {"help", no_argument, 0, 'h'},
1978 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001979 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001980 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001981 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001982 {0, 0, 0, 0}
1983 };
Daniel P. Berrange6b4df542017-07-04 13:30:09 +01001984 c = getopt_long(argc, argv, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001985 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001986 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001987 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001988 }
bellardea2384d2004-08-01 21:59:26 +00001989 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001990 case ':':
1991 missing_argument(argv[optind - 1]);
1992 break;
Jes Sorensenef873942010-12-06 15:25:40 +01001993 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001994 unrecognized_option(argv[optind - 1]);
1995 break;
bellardea2384d2004-08-01 21:59:26 +00001996 case 'h':
1997 help();
1998 break;
1999 case 'f':
2000 fmt = optarg;
2001 break;
2002 case 'O':
2003 out_fmt = optarg;
2004 break;
thsf58c7b32008-06-05 21:53:49 +00002005 case 'B':
2006 out_baseimg = optarg;
2007 break;
bellardea2384d2004-08-01 21:59:26 +00002008 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002009 s.compressed = true;
bellardea2384d2004-08-01 21:59:26 +00002010 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002011 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01002012 if (!is_valid_option_list(optarg)) {
2013 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002014 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01002015 }
2016 if (!options) {
2017 options = g_strdup(optarg);
2018 } else {
2019 char *old_options = options;
2020 options = g_strdup_printf("%s,%s", options, optarg);
2021 g_free(old_options);
2022 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002023 break;
edison51ef6722010-09-21 19:58:41 -07002024 case 's':
2025 snapshot_name = optarg;
2026 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08002027 case 'l':
2028 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01002029 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2030 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08002031 if (!sn_opts) {
2032 error_report("Failed in parsing snapshot param '%s'",
2033 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002034 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002035 }
2036 } else {
2037 snapshot_name = optarg;
2038 }
2039 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002040 case 'S':
2041 {
2042 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002043
2044 sval = cvtnum(optarg);
2045 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02002046 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002047 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002048 }
2049
Peter Lieven9fd77f92017-04-21 11:11:55 +02002050 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002051 break;
2052 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002053 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002054 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002055 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002056 case 't':
2057 cache = optarg;
2058 break;
Max Reitz40055952014-07-22 22:58:42 +02002059 case 'T':
2060 src_cache = optarg;
2061 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002062 case 'q':
2063 quiet = true;
2064 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002065 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002066 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002067 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002068 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002069 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2070 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002071 error_report("Invalid number of coroutines. Allowed number of"
2072 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002073 goto fail_getopt;
2074 }
2075 break;
2076 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002077 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002078 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002079 case 'U':
2080 force_share = true;
2081 break;
Max Reitz3258b912017-04-26 15:46:47 +02002082 case OPTION_OBJECT: {
2083 QemuOpts *object_opts;
2084 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2085 optarg, true);
2086 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002087 goto fail_getopt;
2088 }
2089 break;
Max Reitz3258b912017-04-26 15:46:47 +02002090 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002091 case OPTION_IMAGE_OPTS:
2092 image_opts = true;
2093 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002094 case OPTION_TARGET_IMAGE_OPTS:
2095 tgt_image_opts = true;
2096 break;
bellardea2384d2004-08-01 21:59:26 +00002097 }
2098 }
ths3b46e622007-09-17 08:09:54 +00002099
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002100 if (!out_fmt && !tgt_image_opts) {
2101 out_fmt = "raw";
2102 }
2103
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002104 if (qemu_opts_foreach(&qemu_object_opts,
2105 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002106 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002107 goto fail_getopt;
2108 }
2109
Peter Lieven9fd77f92017-04-21 11:11:55 +02002110 if (!s.wr_in_order && s.compressed) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002111 error_report("Out of order write and compress are mutually exclusive");
Peter Lieven9fd77f92017-04-21 11:11:55 +02002112 goto fail_getopt;
2113 }
2114
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002115 if (tgt_image_opts && !skip_create) {
2116 error_report("--target-image-opts requires use of -n flag");
2117 goto fail_getopt;
2118 }
2119
Peter Lieven9fd77f92017-04-21 11:11:55 +02002120 s.src_num = argc - optind - 1;
2121 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2122
2123 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002124 if (out_fmt) {
2125 ret = print_block_option_help(out_filename, out_fmt);
2126 goto fail_getopt;
2127 } else {
2128 error_report("Option help requires a format be specified");
2129 goto fail_getopt;
2130 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002131 }
2132
2133 if (s.src_num < 1) {
2134 error_report("Must specify image file name");
2135 goto fail_getopt;
2136 }
2137
2138
Peter Lieven9fd77f92017-04-21 11:11:55 +02002139 /* ret is still -EINVAL until here */
2140 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2141 if (ret < 0) {
2142 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002143 goto fail_getopt;
2144 }
2145
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002146 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002147 if (quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002148 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002149 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002150 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002151 qemu_progress_print(0, 100);
2152
Peter Lieven9fd77f92017-04-21 11:11:55 +02002153 s.src = g_new0(BlockBackend *, s.src_num);
2154 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002155
Peter Lieven9fd77f92017-04-21 11:11:55 +02002156 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2157 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Fam Zheng335e9932017-05-03 00:35:39 +08002158 fmt, src_flags, src_writethrough, quiet,
2159 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002160 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002161 ret = -1;
2162 goto out;
2163 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002164 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2165 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002166 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002167 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002168 ret = -1;
2169 goto out;
2170 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002171 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002172 }
bellardea2384d2004-08-01 21:59:26 +00002173
Wenchao Xiaef806542013-12-04 17:10:57 +08002174 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002175 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002176 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2177 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2178 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002179 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002180 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002181 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002182 ret = -1;
2183 goto out;
2184 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002185
Peter Lieven9fd77f92017-04-21 11:11:55 +02002186 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2187 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002188 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002189 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002190 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002191 ret = -1;
2192 goto out;
edison51ef6722010-09-21 19:58:41 -07002193 }
2194
Max Reitz2e024cd2015-02-11 09:58:46 -05002195 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002196 /* Find driver and parse its options */
2197 drv = bdrv_find_format(out_fmt);
2198 if (!drv) {
2199 error_report("Unknown file format '%s'", out_fmt);
2200 ret = -1;
2201 goto out;
2202 }
2203
2204 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2205 if (!proto_drv) {
2206 error_report_err(local_err);
2207 ret = -1;
2208 goto out;
2209 }
2210
Max Reitz2e024cd2015-02-11 09:58:46 -05002211 if (!drv->create_opts) {
2212 error_report("Format driver '%s' does not support image creation",
2213 drv->format_name);
2214 ret = -1;
2215 goto out;
2216 }
Max Reitzf75613c2014-12-02 18:32:46 +01002217
Max Reitz2e024cd2015-02-11 09:58:46 -05002218 if (!proto_drv->create_opts) {
2219 error_report("Protocol driver '%s' does not support image creation",
2220 proto_drv->format_name);
2221 ret = -1;
2222 goto out;
2223 }
Max Reitzf75613c2014-12-02 18:32:46 +01002224
Max Reitz2e024cd2015-02-11 09:58:46 -05002225 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2226 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002227
Max Reitz2e024cd2015-02-11 09:58:46 -05002228 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002229 if (options) {
2230 qemu_opts_do_parse(opts, options, NULL, &local_err);
2231 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002232 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002233 ret = -1;
2234 goto out;
2235 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002236 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002237
Peter Lieven9fd77f92017-04-21 11:11:55 +02002238 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002239 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002240 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2241 if (ret < 0) {
2242 goto out;
2243 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002244 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002245
Kevin Wolfa18953f2010-10-14 15:46:04 +02002246 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002247 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002248 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002249 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002250 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002251 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002252
Max Reitz48758a82017-04-26 15:46:48 +02002253 if (s.src_num > 1 && out_baseimg) {
2254 error_report("Having a backing file for the target makes no sense when "
2255 "concatenating multiple input images");
2256 ret = -1;
2257 goto out;
2258 }
2259
Kevin Wolfefa84d42009-05-18 16:42:12 +02002260 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002261 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002262 bool encryption =
2263 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002264 const char *encryptfmt =
2265 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
Chunyan Liu83d05212014-06-05 17:20:51 +08002266 const char *preallocation =
2267 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002268
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002269 if (drv && !drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002270 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002271 ret = -1;
2272 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002273 }
2274
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002275 if (encryption || encryptfmt) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002276 error_report("Compression and encryption not supported at "
2277 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002278 ret = -1;
2279 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002280 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002281
Chunyan Liu83d05212014-06-05 17:20:51 +08002282 if (preallocation
2283 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002284 {
2285 error_report("Compression and preallocation not supported at "
2286 "the same time");
2287 ret = -1;
2288 goto out;
2289 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002290 }
2291
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002292 if (!skip_create) {
2293 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002294 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002295 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002296 error_reportf_err(local_err, "%s: error while converting %s: ",
2297 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002298 goto out;
bellardea2384d2004-08-01 21:59:26 +00002299 }
2300 }
ths3b46e622007-09-17 08:09:54 +00002301
Peter Lieven9fd77f92017-04-21 11:11:55 +02002302 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002303 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002304 if (ret < 0) {
2305 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002306 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002307 }
2308
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002309 if (skip_create) {
2310 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2311 flags, writethrough, quiet, false);
2312 } else {
2313 /* TODO ultimately we should allow --target-image-opts
2314 * to be used even when -n is not given.
2315 * That has to wait for bdrv_create to be improved
2316 * to allow filenames in option syntax
2317 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01002318 s.target = img_open_new_file(out_filename, opts, out_fmt,
2319 flags, writethrough, quiet, false);
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002320 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002321 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002322 ret = -1;
2323 goto out;
2324 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002325 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002326
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002327 if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2328 error_report("Compression not supported for this file format");
2329 ret = -1;
2330 goto out;
2331 }
2332
Eric Blake5def6b82016-06-23 16:37:19 -06002333 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002334 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2335 * as maximum. */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002336 s.buf_sectors = MIN(32768,
2337 MAX(s.buf_sectors,
2338 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2339 out_bs->bl.pdiscard_alignment >>
2340 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002341
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002342 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002343 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002344 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002345 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002346 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002347 ret = -1;
2348 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002349 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002350 error_report("output file is smaller than input file");
2351 ret = -1;
2352 goto out;
2353 }
2354 }
2355
Peter Lieven24f833c2013-11-27 11:07:07 +01002356 ret = bdrv_get_info(out_bs, &bdi);
2357 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002358 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002359 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002360 goto out;
2361 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002362 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002363 s.compressed = s.compressed || bdi.needs_compressed_writes;
2364 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Peter Lieven24f833c2013-11-27 11:07:07 +01002365 }
2366
Peter Lieven9fd77f92017-04-21 11:11:55 +02002367 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002368out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002369 if (!ret) {
2370 qemu_progress_print(100, 0);
2371 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002372 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002373 qemu_opts_del(opts);
2374 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002375 qemu_opts_del(sn_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002376 blk_unref(s.target);
2377 if (s.src) {
2378 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2379 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002380 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002381 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002382 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002383 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002384fail_getopt:
2385 g_free(options);
2386
Peter Lieven9fd77f92017-04-21 11:11:55 +02002387 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002388}
2389
bellard57d1a2b2004-08-03 21:15:11 +00002390
bellardfaea38e2006-08-05 21:31:00 +00002391static void dump_snapshots(BlockDriverState *bs)
2392{
2393 QEMUSnapshotInfo *sn_tab, *sn;
2394 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002395
2396 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2397 if (nb_sns <= 0)
2398 return;
2399 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002400 bdrv_snapshot_dump(fprintf, stdout, NULL);
2401 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002402 for(i = 0; i < nb_sns; i++) {
2403 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002404 bdrv_snapshot_dump(fprintf, stdout, sn);
2405 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002406 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002407 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002408}
2409
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002410static void dump_json_image_info_list(ImageInfoList *list)
2411{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002412 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002413 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002414 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002415
2416 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2417 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002418 str = qobject_to_json_pretty(obj);
2419 assert(str != NULL);
2420 printf("%s\n", qstring_get_str(str));
2421 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002422 visit_free(v);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002423 QDECREF(str);
2424}
2425
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002426static void dump_json_image_info(ImageInfo *info)
2427{
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002428 QString *str;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002429 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002430 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002431
2432 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2433 visit_complete(v, &obj);
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002434 str = qobject_to_json_pretty(obj);
2435 assert(str != NULL);
2436 printf("%s\n", qstring_get_str(str));
2437 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002438 visit_free(v);
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002439 QDECREF(str);
2440}
2441
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002442static void dump_human_image_info_list(ImageInfoList *list)
2443{
2444 ImageInfoList *elem;
2445 bool delim = false;
2446
2447 for (elem = list; elem; elem = elem->next) {
2448 if (delim) {
2449 printf("\n");
2450 }
2451 delim = true;
2452
Wenchao Xia5b917042013-05-25 11:09:45 +08002453 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002454 }
2455}
2456
2457static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2458{
2459 return strcmp(a, b) == 0;
2460}
2461
2462/**
2463 * Open an image file chain and return an ImageInfoList
2464 *
2465 * @filename: topmost image filename
2466 * @fmt: topmost image format (may be NULL to autodetect)
2467 * @chain: true - enumerate entire backing file chain
2468 * false - only topmost image file
2469 *
2470 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2471 * image file. If there was an error a message will have been printed to
2472 * stderr.
2473 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002474static ImageInfoList *collect_image_info_list(bool image_opts,
2475 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002476 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002477 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002478{
2479 ImageInfoList *head = NULL;
2480 ImageInfoList **last = &head;
2481 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002482 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002483
2484 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2485
2486 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002487 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002488 BlockDriverState *bs;
2489 ImageInfo *info;
2490 ImageInfoList *elem;
2491
2492 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2493 error_report("Backing file '%s' creates an infinite loop.",
2494 filename);
2495 goto err;
2496 }
2497 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2498
Max Reitzefaa7c42016-03-16 19:54:38 +01002499 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002500 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2501 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002502 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002503 goto err;
2504 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002505 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002506
Wenchao Xia43526ec2013-06-06 12:27:58 +08002507 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002508 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002509 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002510 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002511 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002512 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002513
2514 elem = g_new0(ImageInfoList, 1);
2515 elem->value = info;
2516 *last = elem;
2517 last = &elem->next;
2518
Markus Armbruster26f54e92014-10-07 13:59:04 +02002519 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002520
2521 filename = fmt = NULL;
2522 if (chain) {
2523 if (info->has_full_backing_filename) {
2524 filename = info->full_backing_filename;
2525 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002526 error_report("Could not determine absolute backing filename,"
2527 " but backing filename '%s' present",
2528 info->backing_filename);
2529 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002530 }
2531 if (info->has_backing_filename_format) {
2532 fmt = info->backing_filename_format;
2533 }
2534 }
2535 }
2536 g_hash_table_destroy(filenames);
2537 return head;
2538
2539err:
2540 qapi_free_ImageInfoList(head);
2541 g_hash_table_destroy(filenames);
2542 return NULL;
2543}
2544
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002545static int img_info(int argc, char **argv)
2546{
2547 int c;
2548 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002549 bool chain = false;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002550 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002551 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002552 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002553 bool force_share = false;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002554
bellardea2384d2004-08-01 21:59:26 +00002555 fmt = NULL;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002556 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002557 for(;;) {
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002558 int option_index = 0;
2559 static const struct option long_options[] = {
2560 {"help", no_argument, 0, 'h'},
2561 {"format", required_argument, 0, 'f'},
2562 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002563 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002564 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002565 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002566 {"force-share", no_argument, 0, 'U'},
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002567 {0, 0, 0, 0}
2568 };
Fam Zheng335e9932017-05-03 00:35:39 +08002569 c = getopt_long(argc, argv, ":f:hU",
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002570 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002571 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002572 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002573 }
bellardea2384d2004-08-01 21:59:26 +00002574 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002575 case ':':
2576 missing_argument(argv[optind - 1]);
2577 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002578 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002579 unrecognized_option(argv[optind - 1]);
2580 break;
bellardea2384d2004-08-01 21:59:26 +00002581 case 'h':
2582 help();
2583 break;
2584 case 'f':
2585 fmt = optarg;
2586 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002587 case 'U':
2588 force_share = true;
2589 break;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002590 case OPTION_OUTPUT:
2591 output = optarg;
2592 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002593 case OPTION_BACKING_CHAIN:
2594 chain = true;
2595 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002596 case OPTION_OBJECT: {
2597 QemuOpts *opts;
2598 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2599 optarg, true);
2600 if (!opts) {
2601 return 1;
2602 }
2603 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002604 case OPTION_IMAGE_OPTS:
2605 image_opts = true;
2606 break;
bellardea2384d2004-08-01 21:59:26 +00002607 }
2608 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002609 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002610 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002611 }
bellardea2384d2004-08-01 21:59:26 +00002612 filename = argv[optind++];
2613
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002614 if (output && !strcmp(output, "json")) {
2615 output_format = OFORMAT_JSON;
2616 } else if (output && !strcmp(output, "human")) {
2617 output_format = OFORMAT_HUMAN;
2618 } else if (output) {
2619 error_report("--output must be used with human or json as argument.");
2620 return 1;
2621 }
2622
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002623 if (qemu_opts_foreach(&qemu_object_opts,
2624 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002625 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002626 return 1;
2627 }
2628
Fam Zheng335e9932017-05-03 00:35:39 +08002629 list = collect_image_info_list(image_opts, filename, fmt, chain,
2630 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002631 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002632 return 1;
2633 }
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002634
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002635 switch (output_format) {
2636 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002637 dump_human_image_info_list(list);
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002638 break;
2639 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002640 if (chain) {
2641 dump_json_image_info_list(list);
2642 } else {
2643 dump_json_image_info(list->value);
2644 }
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002645 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002646 }
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002647
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002648 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002649 return 0;
2650}
2651
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002652static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2653 MapEntry *next)
2654{
2655 switch (output_format) {
2656 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002657 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002658 error_report("File contains external, encrypted or compressed clusters.");
2659 exit(1);
2660 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002661 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002662 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002663 e->start, e->length,
2664 e->has_offset ? e->offset : 0,
2665 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002666 }
2667 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2668 * Modify the flags here to allow more coalescing.
2669 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002670 if (next && (!next->data || next->zero)) {
2671 next->data = false;
2672 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002673 }
2674 break;
2675 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002676 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2677 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002678 (e->start == 0 ? "[" : ",\n"),
2679 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002680 e->zero ? "true" : "false",
2681 e->data ? "true" : "false");
2682 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002683 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002684 }
2685 putchar('}');
2686
2687 if (!next) {
2688 printf("]\n");
2689 }
2690 break;
2691 }
2692}
2693
Eric Blake5e344dd2017-10-11 22:47:02 -05002694static int get_block_status(BlockDriverState *bs, int64_t offset,
2695 int64_t bytes, MapEntry *e)
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002696{
Eric Blake237d78f2017-10-11 22:47:03 -05002697 int ret;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002698 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002699 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002700 bool has_offset;
Eric Blake237d78f2017-10-11 22:47:03 -05002701 int64_t map;
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 (;;) {
Eric Blake237d78f2017-10-11 22:47:03 -05002710 ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002711 if (ret < 0) {
2712 return ret;
2713 }
Eric Blake237d78f2017-10-11 22:47:03 -05002714 assert(bytes);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002715 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2716 break;
2717 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002718 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002719 if (bs == NULL) {
2720 ret = 0;
2721 break;
2722 }
2723
2724 depth++;
2725 }
2726
John Snow28756452016-02-05 13:12:33 -05002727 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2728
2729 *e = (MapEntry) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002730 .start = offset,
Eric Blake237d78f2017-10-11 22:47:03 -05002731 .length = bytes,
John Snow28756452016-02-05 13:12:33 -05002732 .data = !!(ret & BDRV_BLOCK_DATA),
2733 .zero = !!(ret & BDRV_BLOCK_ZERO),
Eric Blake237d78f2017-10-11 22:47:03 -05002734 .offset = map,
John Snow28756452016-02-05 13:12:33 -05002735 .has_offset = has_offset,
2736 .depth = depth,
2737 .has_filename = file && has_offset,
2738 .filename = file && has_offset ? file->filename : NULL,
2739 };
2740
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002741 return 0;
2742}
2743
Fam Zheng16b0d552016-01-26 11:59:02 +08002744static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2745{
2746 if (curr->length == 0) {
2747 return false;
2748 }
2749 if (curr->zero != next->zero ||
2750 curr->data != next->data ||
2751 curr->depth != next->depth ||
2752 curr->has_filename != next->has_filename ||
2753 curr->has_offset != next->has_offset) {
2754 return false;
2755 }
2756 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2757 return false;
2758 }
2759 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2760 return false;
2761 }
2762 return true;
2763}
2764
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002765static int img_map(int argc, char **argv)
2766{
2767 int c;
2768 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002769 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002770 BlockDriverState *bs;
2771 const char *filename, *fmt, *output;
2772 int64_t length;
2773 MapEntry curr = { .length = 0 }, next;
2774 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002775 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002776 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002777
2778 fmt = NULL;
2779 output = NULL;
2780 for (;;) {
2781 int option_index = 0;
2782 static const struct option long_options[] = {
2783 {"help", no_argument, 0, 'h'},
2784 {"format", required_argument, 0, 'f'},
2785 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002786 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002787 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002788 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002789 {0, 0, 0, 0}
2790 };
Fam Zheng335e9932017-05-03 00:35:39 +08002791 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002792 long_options, &option_index);
2793 if (c == -1) {
2794 break;
2795 }
2796 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002797 case ':':
2798 missing_argument(argv[optind - 1]);
2799 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002800 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002801 unrecognized_option(argv[optind - 1]);
2802 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002803 case 'h':
2804 help();
2805 break;
2806 case 'f':
2807 fmt = optarg;
2808 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002809 case 'U':
2810 force_share = true;
2811 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002812 case OPTION_OUTPUT:
2813 output = optarg;
2814 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002815 case OPTION_OBJECT: {
2816 QemuOpts *opts;
2817 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2818 optarg, true);
2819 if (!opts) {
2820 return 1;
2821 }
2822 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002823 case OPTION_IMAGE_OPTS:
2824 image_opts = true;
2825 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002826 }
2827 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002828 if (optind != argc - 1) {
2829 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002830 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002831 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002832
2833 if (output && !strcmp(output, "json")) {
2834 output_format = OFORMAT_JSON;
2835 } else if (output && !strcmp(output, "human")) {
2836 output_format = OFORMAT_HUMAN;
2837 } else if (output) {
2838 error_report("--output must be used with human or json as argument.");
2839 return 1;
2840 }
2841
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002842 if (qemu_opts_foreach(&qemu_object_opts,
2843 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002844 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002845 return 1;
2846 }
2847
Fam Zheng335e9932017-05-03 00:35:39 +08002848 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002849 if (!blk) {
2850 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002851 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002852 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002853
2854 if (output_format == OFORMAT_HUMAN) {
2855 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2856 }
2857
Max Reitzf1d3cd72015-02-05 13:58:18 -05002858 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002859 while (curr.start + curr.length < length) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002860 int64_t offset = curr.start + curr.length;
2861 int64_t n;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002862
2863 /* Probe up to 1 GiB at a time. */
Eric Blake5e344dd2017-10-11 22:47:02 -05002864 n = QEMU_ALIGN_DOWN(MIN(1 << 30, length - offset), BDRV_SECTOR_SIZE);
2865 ret = get_block_status(bs, offset, n, &next);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002866
2867 if (ret < 0) {
2868 error_report("Could not read file metadata: %s", strerror(-ret));
2869 goto out;
2870 }
2871
Fam Zheng16b0d552016-01-26 11:59:02 +08002872 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002873 curr.length += next.length;
2874 continue;
2875 }
2876
2877 if (curr.length > 0) {
2878 dump_map_entry(output_format, &curr, &next);
2879 }
2880 curr = next;
2881 }
2882
2883 dump_map_entry(output_format, &curr, NULL);
2884
2885out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002886 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002887 return ret < 0;
2888}
2889
aliguorif7b4a942009-01-07 17:40:15 +00002890#define SNAPSHOT_LIST 1
2891#define SNAPSHOT_CREATE 2
2892#define SNAPSHOT_APPLY 3
2893#define SNAPSHOT_DELETE 4
2894
Stuart Brady153859b2009-06-07 00:42:17 +01002895static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002896{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002897 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002898 BlockDriverState *bs;
2899 QEMUSnapshotInfo sn;
2900 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002901 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002902 int action = 0;
2903 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002904 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002905 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002906 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002907 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00002908
Kevin Wolfce099542016-03-15 13:01:04 +01002909 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002910 /* Parse commandline parameters */
2911 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002912 static const struct option long_options[] = {
2913 {"help", no_argument, 0, 'h'},
2914 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002915 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002916 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002917 {0, 0, 0, 0}
2918 };
Fam Zheng335e9932017-05-03 00:35:39 +08002919 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002920 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002921 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002922 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002923 }
aliguorif7b4a942009-01-07 17:40:15 +00002924 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002925 case ':':
2926 missing_argument(argv[optind - 1]);
2927 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002928 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002929 unrecognized_option(argv[optind - 1]);
2930 break;
aliguorif7b4a942009-01-07 17:40:15 +00002931 case 'h':
2932 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002933 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002934 case 'l':
2935 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002936 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002937 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002938 }
2939 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002940 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002941 break;
2942 case 'a':
2943 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002944 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002945 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002946 }
2947 action = SNAPSHOT_APPLY;
2948 snapshot_name = optarg;
2949 break;
2950 case 'c':
2951 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002952 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002953 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002954 }
2955 action = SNAPSHOT_CREATE;
2956 snapshot_name = optarg;
2957 break;
2958 case 'd':
2959 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002960 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002961 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002962 }
2963 action = SNAPSHOT_DELETE;
2964 snapshot_name = optarg;
2965 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002966 case 'q':
2967 quiet = true;
2968 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002969 case 'U':
2970 force_share = true;
2971 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002972 case OPTION_OBJECT: {
2973 QemuOpts *opts;
2974 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2975 optarg, true);
2976 if (!opts) {
2977 return 1;
2978 }
2979 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002980 case OPTION_IMAGE_OPTS:
2981 image_opts = true;
2982 break;
aliguorif7b4a942009-01-07 17:40:15 +00002983 }
2984 }
2985
Kevin Wolffc11eb22013-08-05 10:53:04 +02002986 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002987 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002988 }
aliguorif7b4a942009-01-07 17:40:15 +00002989 filename = argv[optind++];
2990
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002991 if (qemu_opts_foreach(&qemu_object_opts,
2992 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002993 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002994 return 1;
2995 }
2996
aliguorif7b4a942009-01-07 17:40:15 +00002997 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08002998 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
2999 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003000 if (!blk) {
3001 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003002 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003003 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00003004
3005 /* Perform the requested action */
3006 switch(action) {
3007 case SNAPSHOT_LIST:
3008 dump_snapshots(bs);
3009 break;
3010
3011 case SNAPSHOT_CREATE:
3012 memset(&sn, 0, sizeof(sn));
3013 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
3014
3015 qemu_gettimeofday(&tv);
3016 sn.date_sec = tv.tv_sec;
3017 sn.date_nsec = tv.tv_usec * 1000;
3018
3019 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003020 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003021 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003022 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003023 }
aliguorif7b4a942009-01-07 17:40:15 +00003024 break;
3025
3026 case SNAPSHOT_APPLY:
3027 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003028 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003029 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003030 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003031 }
aliguorif7b4a942009-01-07 17:40:15 +00003032 break;
3033
3034 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003035 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003036 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003037 error_reportf_err(err, "Could not delete snapshot '%s': ",
3038 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003039 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003040 }
aliguorif7b4a942009-01-07 17:40:15 +00003041 break;
3042 }
3043
3044 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003045 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003046 if (ret) {
3047 return 1;
3048 }
Stuart Brady153859b2009-06-07 00:42:17 +01003049 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003050}
3051
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003052static int img_rebase(int argc, char **argv)
3053{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003054 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003055 uint8_t *buf_old = NULL;
3056 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05003057 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003058 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003059 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3060 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003061 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003062 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003063 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003064 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003065 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003066 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003067 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003068
3069 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003070 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003071 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003072 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003073 out_baseimg = NULL;
3074 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003075 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003076 static const struct option long_options[] = {
3077 {"help", no_argument, 0, 'h'},
3078 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003079 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003080 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003081 {0, 0, 0, 0}
3082 };
Fam Zheng335e9932017-05-03 00:35:39 +08003083 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003084 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003085 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003086 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003087 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003088 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003089 case ':':
3090 missing_argument(argv[optind - 1]);
3091 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003092 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003093 unrecognized_option(argv[optind - 1]);
3094 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003095 case 'h':
3096 help();
3097 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003098 case 'f':
3099 fmt = optarg;
3100 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003101 case 'F':
3102 out_basefmt = optarg;
3103 break;
3104 case 'b':
3105 out_baseimg = optarg;
3106 break;
3107 case 'u':
3108 unsafe = 1;
3109 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003110 case 'p':
3111 progress = 1;
3112 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003113 case 't':
3114 cache = optarg;
3115 break;
Max Reitz40055952014-07-22 22:58:42 +02003116 case 'T':
3117 src_cache = optarg;
3118 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003119 case 'q':
3120 quiet = true;
3121 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003122 case OPTION_OBJECT: {
3123 QemuOpts *opts;
3124 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3125 optarg, true);
3126 if (!opts) {
3127 return 1;
3128 }
3129 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003130 case OPTION_IMAGE_OPTS:
3131 image_opts = true;
3132 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003133 case 'U':
3134 force_share = true;
3135 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003136 }
3137 }
3138
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003139 if (quiet) {
3140 progress = 0;
3141 }
3142
Fam Zhengac1307a2014-04-22 13:36:11 +08003143 if (optind != argc - 1) {
3144 error_exit("Expecting one image file name");
3145 }
3146 if (!unsafe && !out_baseimg) {
3147 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003148 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003149 filename = argv[optind++];
3150
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003151 if (qemu_opts_foreach(&qemu_object_opts,
3152 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003153 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003154 return 1;
3155 }
3156
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003157 qemu_progress_init(progress, 2.0);
3158 qemu_progress_print(0, 100);
3159
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003160 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003161 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003162 if (ret < 0) {
3163 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003164 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003165 }
3166
Kevin Wolfce099542016-03-15 13:01:04 +01003167 src_flags = 0;
3168 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003169 if (ret < 0) {
3170 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003171 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003172 }
3173
Kevin Wolfce099542016-03-15 13:01:04 +01003174 /* The source files are opened read-only, don't care about WCE */
3175 assert((src_flags & BDRV_O_RDWR) == 0);
3176 (void) src_writethrough;
3177
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003178 /*
3179 * Open the images.
3180 *
3181 * Ignore the old backing file for unsafe rebase in case we want to correct
3182 * the reference to a renamed or moved backing file.
3183 */
Fam Zheng335e9932017-05-03 00:35:39 +08003184 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3185 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003186 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003187 ret = -1;
3188 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003189 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003190 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003191
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003192 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003193 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003194 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003195 ret = -1;
3196 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003197 }
3198 }
3199
3200 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003201 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003202 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003203 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003204
Max Reitz644483d2015-02-05 13:58:17 -05003205 if (bs->backing_format[0] != '\0') {
3206 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003207 qdict_put_str(options, "driver", bs->backing_format);
Max Reitz644483d2015-02-05 13:58:17 -05003208 }
3209
Fam Zheng335e9932017-05-03 00:35:39 +08003210 if (force_share) {
3211 if (!options) {
3212 options = qdict_new();
3213 }
Eric Blake579cf1d2017-05-15 14:54:39 -05003214 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +08003215 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003216 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003217 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003218 options, src_flags, &local_err);
3219 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003220 error_reportf_err(local_err,
3221 "Could not open old backing file '%s': ",
3222 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003223 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003224 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003225 }
Max Reitz644483d2015-02-05 13:58:17 -05003226
Alex Bligha6166732012-10-16 13:46:18 +01003227 if (out_baseimg[0]) {
Fam Zheng335e9932017-05-03 00:35:39 +08003228 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003229 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003230 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003231 }
3232 if (force_share) {
3233 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003234 }
3235
Max Reitzefaa7c42016-03-16 19:54:38 +01003236 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003237 options, src_flags, &local_err);
3238 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003239 error_reportf_err(local_err,
3240 "Could not open new backing file '%s': ",
3241 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003242 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003243 goto out;
3244 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003245 }
3246 }
3247
3248 /*
3249 * Check each unallocated cluster in the COW file. If it is unallocated,
3250 * accesses go to the backing file. We must therefore compare this cluster
3251 * in the old and new backing file, and if they differ we need to copy it
3252 * from the old backing file into the COW file.
3253 *
3254 * If qemu-img crashes during this step, no harm is done. The content of
3255 * the image is the same as the original one at any time.
3256 */
3257 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003258 int64_t num_sectors;
3259 int64_t old_backing_num_sectors;
3260 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003261 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003262 int n;
Eric Blaked6a644b2017-07-07 07:44:57 -05003263 int64_t count;
Kevin Wolf1f710492012-10-12 14:29:18 +02003264 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003265
Max Reitzf1d3cd72015-02-05 13:58:18 -05003266 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3267 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003268
Max Reitzf1d3cd72015-02-05 13:58:18 -05003269 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003270 if (num_sectors < 0) {
3271 error_report("Could not get size of '%s': %s",
3272 filename, strerror(-num_sectors));
3273 ret = -1;
3274 goto out;
3275 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003276 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003277 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003278 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003279
3280 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3281 error_report("Could not get size of '%s': %s",
3282 backing_name, strerror(-old_backing_num_sectors));
3283 ret = -1;
3284 goto out;
3285 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003286 if (blk_new_backing) {
3287 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003288 if (new_backing_num_sectors < 0) {
3289 error_report("Could not get size of '%s': %s",
3290 out_baseimg, strerror(-new_backing_num_sectors));
3291 ret = -1;
3292 goto out;
3293 }
Alex Bligha6166732012-10-16 13:46:18 +01003294 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003295
Kevin Wolf1f710492012-10-12 14:29:18 +02003296 if (num_sectors != 0) {
3297 local_progress = (float)100 /
3298 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
3299 }
3300
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003301 for (sector = 0; sector < num_sectors; sector += n) {
3302
3303 /* How many sectors can we handle with the next read? */
3304 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
3305 n = (IO_BUF_SIZE / 512);
3306 } else {
3307 n = num_sectors - sector;
3308 }
3309
3310 /* If the cluster is allocated, we don't need to take action */
Eric Blaked6a644b2017-07-07 07:44:57 -05003311 ret = bdrv_is_allocated(bs, sector << BDRV_SECTOR_BITS,
3312 n << BDRV_SECTOR_BITS, &count);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003313 if (ret < 0) {
3314 error_report("error while reading image metadata: %s",
3315 strerror(-ret));
3316 goto out;
3317 }
Eric Blaked6a644b2017-07-07 07:44:57 -05003318 /* TODO relax this once bdrv_is_allocated does not enforce
3319 * sector alignment */
3320 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
3321 n = count >> BDRV_SECTOR_BITS;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003322 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003323 continue;
3324 }
3325
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003326 /*
3327 * Read old and new backing file and take into consideration that
3328 * backing files may be smaller than the COW image.
3329 */
3330 if (sector >= old_backing_num_sectors) {
3331 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3332 } else {
3333 if (sector + n > old_backing_num_sectors) {
3334 n = old_backing_num_sectors - sector;
3335 }
3336
Eric Blake91669202016-05-06 10:26:43 -06003337 ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS,
3338 buf_old, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003339 if (ret < 0) {
3340 error_report("error while reading from old backing file");
3341 goto out;
3342 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003343 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003344
Max Reitzf1d3cd72015-02-05 13:58:18 -05003345 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003346 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3347 } else {
3348 if (sector + n > new_backing_num_sectors) {
3349 n = new_backing_num_sectors - sector;
3350 }
3351
Eric Blake91669202016-05-06 10:26:43 -06003352 ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS,
3353 buf_new, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003354 if (ret < 0) {
3355 error_report("error while reading from new backing file");
3356 goto out;
3357 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003358 }
3359
3360 /* If they differ, we need to write to the COW file */
3361 uint64_t written = 0;
3362
3363 while (written < n) {
3364 int pnum;
3365
3366 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003367 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003368 {
Eric Blake91669202016-05-06 10:26:43 -06003369 ret = blk_pwrite(blk,
3370 (sector + written) << BDRV_SECTOR_BITS,
3371 buf_old + written * 512,
3372 pnum << BDRV_SECTOR_BITS, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003373 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003374 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003375 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003376 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003377 }
3378 }
3379
3380 written += pnum;
3381 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003382 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003383 }
3384 }
3385
3386 /*
3387 * Change the backing file. All clusters that are different from the old
3388 * backing file are overwritten in the COW file now, so the visible content
3389 * doesn't change when we switch the backing file.
3390 */
Alex Bligha6166732012-10-16 13:46:18 +01003391 if (out_baseimg && *out_baseimg) {
3392 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3393 } else {
3394 ret = bdrv_change_backing_file(bs, NULL, NULL);
3395 }
3396
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003397 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003398 error_report("Could not change the backing file to '%s': No "
3399 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003400 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003401 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003402 out_baseimg, strerror(-ret));
3403 }
3404
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003405 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003406 /*
3407 * TODO At this point it is possible to check if any clusters that are
3408 * allocated in the COW file are the same in the backing file. If so, they
3409 * could be dropped from the COW file. Don't do this before switching the
3410 * backing file, in case of a crash this would lead to corruption.
3411 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003412out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003413 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003414 /* Cleanup */
3415 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003416 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003417 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003418 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003419 qemu_vfree(buf_old);
3420 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003421
Markus Armbruster26f54e92014-10-07 13:59:04 +02003422 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003423 if (ret) {
3424 return 1;
3425 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003426 return 0;
3427}
3428
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003429static int img_resize(int argc, char **argv)
3430{
Markus Armbruster6750e792015-02-12 17:43:08 +01003431 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003432 int c, ret, relative;
3433 const char *filename, *fmt, *size;
Max Reitzdc5f6902017-06-13 22:20:55 +02003434 int64_t n, total_size, current_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003435 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003436 BlockBackend *blk = NULL;
Max Reitzdc5f6902017-06-13 22:20:55 +02003437 PreallocMode prealloc = PREALLOC_MODE_OFF;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003438 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003439
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003440 static QemuOptsList resize_options = {
3441 .name = "resize_options",
3442 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3443 .desc = {
3444 {
3445 .name = BLOCK_OPT_SIZE,
3446 .type = QEMU_OPT_SIZE,
3447 .help = "Virtual disk size"
3448 }, {
3449 /* end of list */
3450 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003451 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003452 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003453 bool image_opts = false;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003454 bool shrink = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003455
Kevin Wolfe80fec72011-04-29 10:58:12 +02003456 /* Remove size from argv manually so that negative numbers are not treated
3457 * as options by getopt. */
3458 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003459 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003460 return 1;
3461 }
3462
3463 size = argv[--argc];
3464
3465 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003466 fmt = NULL;
3467 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003468 static const struct option long_options[] = {
3469 {"help", no_argument, 0, 'h'},
3470 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003471 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Max Reitzdc5f6902017-06-13 22:20:55 +02003472 {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003473 {"shrink", no_argument, 0, OPTION_SHRINK},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003474 {0, 0, 0, 0}
3475 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003476 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003477 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003478 if (c == -1) {
3479 break;
3480 }
3481 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003482 case ':':
3483 missing_argument(argv[optind - 1]);
3484 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003485 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003486 unrecognized_option(argv[optind - 1]);
3487 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003488 case 'h':
3489 help();
3490 break;
3491 case 'f':
3492 fmt = optarg;
3493 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003494 case 'q':
3495 quiet = true;
3496 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003497 case OPTION_OBJECT: {
3498 QemuOpts *opts;
3499 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3500 optarg, true);
3501 if (!opts) {
3502 return 1;
3503 }
3504 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003505 case OPTION_IMAGE_OPTS:
3506 image_opts = true;
3507 break;
Max Reitzdc5f6902017-06-13 22:20:55 +02003508 case OPTION_PREALLOCATION:
Marc-AndrƩ Lureauf7abe0e2017-08-24 10:46:10 +02003509 prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
Markus Armbruster06c60b62017-08-24 10:45:57 +02003510 PREALLOC_MODE__MAX, NULL);
Max Reitzdc5f6902017-06-13 22:20:55 +02003511 if (prealloc == PREALLOC_MODE__MAX) {
3512 error_report("Invalid preallocation mode '%s'", optarg);
3513 return 1;
3514 }
3515 break;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003516 case OPTION_SHRINK:
3517 shrink = true;
3518 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003519 }
3520 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003521 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003522 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003523 }
3524 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003525
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003526 if (qemu_opts_foreach(&qemu_object_opts,
3527 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003528 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003529 return 1;
3530 }
3531
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003532 /* Choose grow, shrink, or absolute resize mode */
3533 switch (size[0]) {
3534 case '+':
3535 relative = 1;
3536 size++;
3537 break;
3538 case '-':
3539 relative = -1;
3540 size++;
3541 break;
3542 default:
3543 relative = 0;
3544 break;
3545 }
3546
3547 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003548 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003549 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003550 if (err) {
3551 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003552 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003553 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003554 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003555 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003556 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3557 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003558
Max Reitzefaa7c42016-03-16 19:54:38 +01003559 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003560 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3561 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003562 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003563 ret = -1;
3564 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003565 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003566
Max Reitzdc5f6902017-06-13 22:20:55 +02003567 current_size = blk_getlength(blk);
3568 if (current_size < 0) {
3569 error_report("Failed to inquire current image length: %s",
3570 strerror(-current_size));
3571 ret = -1;
3572 goto out;
3573 }
3574
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003575 if (relative) {
Max Reitzdc5f6902017-06-13 22:20:55 +02003576 total_size = current_size + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003577 } else {
3578 total_size = n;
3579 }
3580 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003581 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003582 ret = -1;
3583 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003584 }
3585
Max Reitzdc5f6902017-06-13 22:20:55 +02003586 if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
3587 error_report("Preallocation can only be used for growing images");
3588 ret = -1;
3589 goto out;
3590 }
3591
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003592 if (total_size < current_size && !shrink) {
3593 warn_report("Shrinking an image will delete all data beyond the "
3594 "shrunken image's end. Before performing such an "
3595 "operation, make sure there is no important data there.");
3596
3597 if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) {
3598 error_report(
3599 "Use the --shrink option to perform a shrink operation.");
3600 ret = -1;
3601 goto out;
3602 } else {
3603 warn_report("Using the --shrink option will suppress this message. "
3604 "Note that future versions of qemu-img may refuse to "
3605 "shrink images without this option.");
3606 }
3607 }
3608
Max Reitzdc5f6902017-06-13 22:20:55 +02003609 ret = blk_truncate(blk, total_size, prealloc, &err);
Max Reitzed3d2ec2017-03-28 22:51:27 +02003610 if (!ret) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003611 qprintf(quiet, "Image resized.\n");
Max Reitzed3d2ec2017-03-28 22:51:27 +02003612 } else {
3613 error_report_err(err);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003614 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003615out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003616 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003617 if (ret) {
3618 return 1;
3619 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003620 return 0;
3621}
3622
Max Reitz76a3a342014-10-27 11:12:51 +01003623static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003624 int64_t offset, int64_t total_work_size,
3625 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003626{
3627 qemu_progress_print(100.f * offset / total_work_size, 0);
3628}
3629
Max Reitz6f176b42013-09-03 10:09:50 +02003630static int img_amend(int argc, char **argv)
3631{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003632 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003633 int c, ret = 0;
3634 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003635 QemuOptsList *create_opts = NULL;
3636 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003637 const char *fmt = NULL, *filename, *cache;
3638 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003639 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003640 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003641 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003642 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003643 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003644
Max Reitzbd39e6e2014-07-22 22:58:43 +02003645 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003646 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003647 static const struct option long_options[] = {
3648 {"help", no_argument, 0, 'h'},
3649 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003650 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003651 {0, 0, 0, 0}
3652 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003653 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003654 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003655 if (c == -1) {
3656 break;
3657 }
3658
3659 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003660 case ':':
3661 missing_argument(argv[optind - 1]);
3662 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003663 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003664 unrecognized_option(argv[optind - 1]);
3665 break;
3666 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003667 help();
3668 break;
3669 case 'o':
3670 if (!is_valid_option_list(optarg)) {
3671 error_report("Invalid option list: %s", optarg);
3672 ret = -1;
3673 goto out_no_progress;
3674 }
3675 if (!options) {
3676 options = g_strdup(optarg);
3677 } else {
3678 char *old_options = options;
3679 options = g_strdup_printf("%s,%s", options, optarg);
3680 g_free(old_options);
3681 }
3682 break;
3683 case 'f':
3684 fmt = optarg;
3685 break;
3686 case 't':
3687 cache = optarg;
3688 break;
3689 case 'p':
3690 progress = true;
3691 break;
3692 case 'q':
3693 quiet = true;
3694 break;
3695 case OPTION_OBJECT:
3696 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3697 optarg, true);
3698 if (!opts) {
3699 ret = -1;
3700 goto out_no_progress;
3701 }
3702 break;
3703 case OPTION_IMAGE_OPTS:
3704 image_opts = true;
3705 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003706 }
3707 }
3708
Max Reitz6f176b42013-09-03 10:09:50 +02003709 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003710 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003711 }
3712
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003713 if (qemu_opts_foreach(&qemu_object_opts,
3714 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003715 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003716 ret = -1;
3717 goto out_no_progress;
3718 }
3719
Max Reitz76a3a342014-10-27 11:12:51 +01003720 if (quiet) {
3721 progress = false;
3722 }
3723 qemu_progress_init(progress, 1.0);
3724
Kevin Wolfa283cb62014-02-21 16:24:07 +01003725 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3726 if (fmt && has_help_option(options)) {
3727 /* If a format is explicitly specified (and possibly no filename is
3728 * given), print option help here */
3729 ret = print_block_option_help(filename, fmt);
3730 goto out;
3731 }
3732
3733 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003734 error_report("Expecting one image file name");
3735 ret = -1;
3736 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003737 }
Max Reitz6f176b42013-09-03 10:09:50 +02003738
Kevin Wolfce099542016-03-15 13:01:04 +01003739 flags = BDRV_O_RDWR;
3740 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003741 if (ret < 0) {
3742 error_report("Invalid cache option: %s", cache);
3743 goto out;
3744 }
3745
Fam Zheng335e9932017-05-03 00:35:39 +08003746 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3747 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003748 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003749 ret = -1;
3750 goto out;
3751 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003752 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003753
3754 fmt = bs->drv->format_name;
3755
Kevin Wolf626f84f2014-02-21 16:24:06 +01003756 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003757 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003758 ret = print_block_option_help(filename, fmt);
3759 goto out;
3760 }
3761
Max Reitzb2439d22014-12-02 18:32:47 +01003762 if (!bs->drv->create_opts) {
3763 error_report("Format driver '%s' does not support any options to amend",
3764 fmt);
3765 ret = -1;
3766 goto out;
3767 }
3768
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003769 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003770 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003771 qemu_opts_do_parse(opts, options, NULL, &err);
3772 if (err) {
3773 error_report_err(err);
3774 ret = -1;
3775 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003776 }
3777
Max Reitz76a3a342014-10-27 11:12:51 +01003778 /* In case the driver does not call amend_status_cb() */
3779 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003780 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003781 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003782 if (ret < 0) {
3783 error_report("Error while amending options: %s", strerror(-ret));
3784 goto out;
3785 }
3786
3787out:
Max Reitz76a3a342014-10-27 11:12:51 +01003788 qemu_progress_end();
3789
Max Reitze814dff2015-08-20 16:00:38 -07003790out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003791 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003792 qemu_opts_del(opts);
3793 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003794 g_free(options);
3795
Max Reitz6f176b42013-09-03 10:09:50 +02003796 if (ret) {
3797 return 1;
3798 }
3799 return 0;
3800}
3801
Kevin Wolfb6133b82014-08-05 14:17:13 +02003802typedef struct BenchData {
3803 BlockBackend *blk;
3804 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003805 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003806 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003807 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003808 int nrreq;
3809 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003810 int flush_interval;
3811 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003812 uint8_t *buf;
3813 QEMUIOVector *qiov;
3814
3815 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003816 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003817 uint64_t offset;
3818} BenchData;
3819
Kevin Wolf55d539c2016-06-03 13:59:41 +02003820static void bench_undrained_flush_cb(void *opaque, int ret)
3821{
3822 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003823 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003824 exit(EXIT_FAILURE);
3825 }
3826}
3827
Kevin Wolfb6133b82014-08-05 14:17:13 +02003828static void bench_cb(void *opaque, int ret)
3829{
3830 BenchData *b = opaque;
3831 BlockAIOCB *acb;
3832
3833 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003834 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003835 exit(EXIT_FAILURE);
3836 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003837
3838 if (b->in_flush) {
3839 /* Just finished a flush with drained queue: Start next requests */
3840 assert(b->in_flight == 0);
3841 b->in_flush = false;
3842 } else if (b->in_flight > 0) {
3843 int remaining = b->n - b->in_flight;
3844
Kevin Wolfb6133b82014-08-05 14:17:13 +02003845 b->n--;
3846 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003847
3848 /* Time for flush? Drain queue if requested, then flush */
3849 if (b->flush_interval && remaining % b->flush_interval == 0) {
3850 if (!b->in_flight || !b->drain_on_flush) {
3851 BlockCompletionFunc *cb;
3852
3853 if (b->drain_on_flush) {
3854 b->in_flush = true;
3855 cb = bench_cb;
3856 } else {
3857 cb = bench_undrained_flush_cb;
3858 }
3859
3860 acb = blk_aio_flush(b->blk, cb, b);
3861 if (!acb) {
3862 error_report("Failed to issue flush request");
3863 exit(EXIT_FAILURE);
3864 }
3865 }
3866 if (b->drain_on_flush) {
3867 return;
3868 }
3869 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003870 }
3871
3872 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003873 int64_t offset = b->offset;
3874 /* blk_aio_* might look for completed I/Os and kick bench_cb
3875 * again, so make sure this operation is counted by in_flight
3876 * and b->offset is ready for the next submission.
3877 */
3878 b->in_flight++;
3879 b->offset += b->step;
3880 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003881 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003882 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003883 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003884 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003885 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003886 if (!acb) {
3887 error_report("Failed to issue request");
3888 exit(EXIT_FAILURE);
3889 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003890 }
3891}
3892
3893static int img_bench(int argc, char **argv)
3894{
3895 int c, ret = 0;
3896 const char *fmt = NULL, *filename;
3897 bool quiet = false;
3898 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003899 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003900 int count = 75000;
3901 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003902 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003903 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003904 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003905 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003906 int flush_interval = 0;
3907 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003908 int64_t image_size;
3909 BlockBackend *blk = NULL;
3910 BenchData data = {};
3911 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003912 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003913 struct timeval t1, t2;
3914 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08003915 bool force_share = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003916
3917 for (;;) {
3918 static const struct option long_options[] = {
3919 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003920 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003921 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003922 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003923 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08003924 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003925 {0, 0, 0, 0}
3926 };
Fam Zheng335e9932017-05-03 00:35:39 +08003927 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003928 if (c == -1) {
3929 break;
3930 }
3931
3932 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003933 case ':':
3934 missing_argument(argv[optind - 1]);
3935 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003936 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003937 unrecognized_option(argv[optind - 1]);
3938 break;
3939 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02003940 help();
3941 break;
3942 case 'c':
3943 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003944 unsigned long res;
3945
3946 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003947 error_report("Invalid request count specified");
3948 return 1;
3949 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003950 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003951 break;
3952 }
3953 case 'd':
3954 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003955 unsigned long res;
3956
3957 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003958 error_report("Invalid queue depth specified");
3959 return 1;
3960 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003961 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003962 break;
3963 }
3964 case 'f':
3965 fmt = optarg;
3966 break;
3967 case 'n':
3968 flags |= BDRV_O_NATIVE_AIO;
3969 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003970 case 'o':
3971 {
Markus Armbruster606caa02017-02-21 21:14:04 +01003972 offset = cvtnum(optarg);
3973 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02003974 error_report("Invalid offset specified");
3975 return 1;
3976 }
3977 break;
3978 }
3979 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003980 case 'q':
3981 quiet = true;
3982 break;
3983 case 's':
3984 {
3985 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003986
Markus Armbruster606caa02017-02-21 21:14:04 +01003987 sval = cvtnum(optarg);
3988 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003989 error_report("Invalid buffer size specified");
3990 return 1;
3991 }
3992
3993 bufsize = sval;
3994 break;
3995 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003996 case 'S':
3997 {
3998 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003999
Markus Armbruster606caa02017-02-21 21:14:04 +01004000 sval = cvtnum(optarg);
4001 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02004002 error_report("Invalid step size specified");
4003 return 1;
4004 }
4005
4006 step = sval;
4007 break;
4008 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004009 case 't':
4010 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
4011 if (ret < 0) {
4012 error_report("Invalid cache mode");
4013 ret = -1;
4014 goto out;
4015 }
4016 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004017 case 'w':
4018 flags |= BDRV_O_RDWR;
4019 is_write = true;
4020 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004021 case 'U':
4022 force_share = true;
4023 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004024 case OPTION_PATTERN:
4025 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004026 unsigned long res;
4027
4028 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004029 error_report("Invalid pattern byte specified");
4030 return 1;
4031 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004032 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004033 break;
4034 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02004035 case OPTION_FLUSH_INTERVAL:
4036 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004037 unsigned long res;
4038
4039 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004040 error_report("Invalid flush interval specified");
4041 return 1;
4042 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004043 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004044 break;
4045 }
4046 case OPTION_NO_DRAIN:
4047 drain_on_flush = false;
4048 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004049 case OPTION_IMAGE_OPTS:
4050 image_opts = true;
4051 break;
4052 }
4053 }
4054
4055 if (optind != argc - 1) {
4056 error_exit("Expecting one image file name");
4057 }
4058 filename = argv[argc - 1];
4059
Kevin Wolf55d539c2016-06-03 13:59:41 +02004060 if (!is_write && flush_interval) {
4061 error_report("--flush-interval is only available in write tests");
4062 ret = -1;
4063 goto out;
4064 }
4065 if (flush_interval && flush_interval < depth) {
4066 error_report("Flush interval can't be smaller than depth");
4067 ret = -1;
4068 goto out;
4069 }
4070
Fam Zheng335e9932017-05-03 00:35:39 +08004071 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4072 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004073 if (!blk) {
4074 ret = -1;
4075 goto out;
4076 }
4077
4078 image_size = blk_getlength(blk);
4079 if (image_size < 0) {
4080 ret = image_size;
4081 goto out;
4082 }
4083
4084 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004085 .blk = blk,
4086 .image_size = image_size,
4087 .bufsize = bufsize,
4088 .step = step ?: bufsize,
4089 .nrreq = depth,
4090 .n = count,
4091 .offset = offset,
4092 .write = is_write,
4093 .flush_interval = flush_interval,
4094 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004095 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004096 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004097 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004098 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004099 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004100 if (flush_interval) {
4101 printf("Sending flush every %d requests\n", flush_interval);
4102 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004103
4104 data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004105 memset(data.buf, pattern, data.nrreq * data.bufsize);
4106
Kevin Wolfb6133b82014-08-05 14:17:13 +02004107 data.qiov = g_new(QEMUIOVector, data.nrreq);
4108 for (i = 0; i < data.nrreq; i++) {
4109 qemu_iovec_init(&data.qiov[i], 1);
4110 qemu_iovec_add(&data.qiov[i],
4111 data.buf + i * data.bufsize, data.bufsize);
4112 }
4113
4114 gettimeofday(&t1, NULL);
4115 bench_cb(&data, 0);
4116
4117 while (data.n > 0) {
4118 main_loop_wait(false);
4119 }
4120 gettimeofday(&t2, NULL);
4121
4122 printf("Run completed in %3.3f seconds.\n",
4123 (t2.tv_sec - t1.tv_sec)
4124 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4125
4126out:
4127 qemu_vfree(data.buf);
4128 blk_unref(blk);
4129
4130 if (ret) {
4131 return 1;
4132 }
4133 return 0;
4134}
4135
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004136#define C_BS 01
4137#define C_COUNT 02
4138#define C_IF 04
4139#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004140#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004141
4142struct DdInfo {
4143 unsigned int flags;
4144 int64_t count;
4145};
4146
4147struct DdIo {
4148 int bsz; /* Block size */
4149 char *filename;
4150 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004151 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004152};
4153
4154struct DdOpts {
4155 const char *name;
4156 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4157 unsigned int flag;
4158};
4159
4160static int img_dd_bs(const char *arg,
4161 struct DdIo *in, struct DdIo *out,
4162 struct DdInfo *dd)
4163{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004164 int64_t res;
4165
Markus Armbruster606caa02017-02-21 21:14:04 +01004166 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004167
Markus Armbruster606caa02017-02-21 21:14:04 +01004168 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004169 error_report("invalid number: '%s'", arg);
4170 return 1;
4171 }
4172 in->bsz = out->bsz = res;
4173
4174 return 0;
4175}
4176
4177static int img_dd_count(const char *arg,
4178 struct DdIo *in, struct DdIo *out,
4179 struct DdInfo *dd)
4180{
Markus Armbruster606caa02017-02-21 21:14:04 +01004181 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004182
Markus Armbruster606caa02017-02-21 21:14:04 +01004183 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004184 error_report("invalid number: '%s'", arg);
4185 return 1;
4186 }
4187
4188 return 0;
4189}
4190
4191static int img_dd_if(const char *arg,
4192 struct DdIo *in, struct DdIo *out,
4193 struct DdInfo *dd)
4194{
4195 in->filename = g_strdup(arg);
4196
4197 return 0;
4198}
4199
4200static int img_dd_of(const char *arg,
4201 struct DdIo *in, struct DdIo *out,
4202 struct DdInfo *dd)
4203{
4204 out->filename = g_strdup(arg);
4205
4206 return 0;
4207}
4208
Reda Sallahif7c15532016-08-10 16:16:09 +02004209static int img_dd_skip(const char *arg,
4210 struct DdIo *in, struct DdIo *out,
4211 struct DdInfo *dd)
4212{
Markus Armbruster606caa02017-02-21 21:14:04 +01004213 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004214
Markus Armbruster606caa02017-02-21 21:14:04 +01004215 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004216 error_report("invalid number: '%s'", arg);
4217 return 1;
4218 }
4219
4220 return 0;
4221}
4222
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004223static int img_dd(int argc, char **argv)
4224{
4225 int ret = 0;
4226 char *arg = NULL;
4227 char *tmp;
4228 BlockDriver *drv = NULL, *proto_drv = NULL;
4229 BlockBackend *blk1 = NULL, *blk2 = NULL;
4230 QemuOpts *opts = NULL;
4231 QemuOptsList *create_opts = NULL;
4232 Error *local_err = NULL;
4233 bool image_opts = false;
4234 int c, i;
4235 const char *out_fmt = "raw";
4236 const char *fmt = NULL;
4237 int64_t size = 0;
4238 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004239 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004240 struct DdInfo dd = {
4241 .flags = 0,
4242 .count = 0,
4243 };
4244 struct DdIo in = {
4245 .bsz = 512, /* Block size is by default 512 bytes */
4246 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004247 .buf = NULL,
4248 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004249 };
4250 struct DdIo out = {
4251 .bsz = 512,
4252 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004253 .buf = NULL,
4254 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004255 };
4256
4257 const struct DdOpts options[] = {
4258 { "bs", img_dd_bs, C_BS },
4259 { "count", img_dd_count, C_COUNT },
4260 { "if", img_dd_if, C_IF },
4261 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004262 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004263 { NULL, NULL, 0 }
4264 };
4265 const struct option long_options[] = {
4266 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004267 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004268 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004269 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004270 { 0, 0, 0, 0 }
4271 };
4272
Fam Zheng335e9932017-05-03 00:35:39 +08004273 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004274 if (c == EOF) {
4275 break;
4276 }
4277 switch (c) {
4278 case 'O':
4279 out_fmt = optarg;
4280 break;
4281 case 'f':
4282 fmt = optarg;
4283 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004284 case ':':
4285 missing_argument(argv[optind - 1]);
4286 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004287 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004288 unrecognized_option(argv[optind - 1]);
4289 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004290 case 'h':
4291 help();
4292 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004293 case 'U':
4294 force_share = true;
4295 break;
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004296 case OPTION_OBJECT:
4297 if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004298 ret = -1;
4299 goto out;
4300 }
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004301 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004302 case OPTION_IMAGE_OPTS:
4303 image_opts = true;
4304 break;
4305 }
4306 }
4307
4308 for (i = optind; i < argc; i++) {
4309 int j;
4310 arg = g_strdup(argv[i]);
4311
4312 tmp = strchr(arg, '=');
4313 if (tmp == NULL) {
4314 error_report("unrecognized operand %s", arg);
4315 ret = -1;
4316 goto out;
4317 }
4318
4319 *tmp++ = '\0';
4320
4321 for (j = 0; options[j].name != NULL; j++) {
4322 if (!strcmp(arg, options[j].name)) {
4323 break;
4324 }
4325 }
4326 if (options[j].name == NULL) {
4327 error_report("unrecognized operand %s", arg);
4328 ret = -1;
4329 goto out;
4330 }
4331
4332 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4333 ret = -1;
4334 goto out;
4335 }
4336 dd.flags |= options[j].flag;
4337 g_free(arg);
4338 arg = NULL;
4339 }
4340
4341 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4342 error_report("Must specify both input and output files");
4343 ret = -1;
4344 goto out;
4345 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004346
4347 if (qemu_opts_foreach(&qemu_object_opts,
4348 user_creatable_add_opts_foreach,
4349 NULL, NULL)) {
4350 ret = -1;
4351 goto out;
4352 }
4353
Fam Zheng335e9932017-05-03 00:35:39 +08004354 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4355 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004356
4357 if (!blk1) {
4358 ret = -1;
4359 goto out;
4360 }
4361
4362 drv = bdrv_find_format(out_fmt);
4363 if (!drv) {
4364 error_report("Unknown file format");
4365 ret = -1;
4366 goto out;
4367 }
4368 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4369
4370 if (!proto_drv) {
4371 error_report_err(local_err);
4372 ret = -1;
4373 goto out;
4374 }
4375 if (!drv->create_opts) {
4376 error_report("Format driver '%s' does not support image creation",
4377 drv->format_name);
4378 ret = -1;
4379 goto out;
4380 }
4381 if (!proto_drv->create_opts) {
4382 error_report("Protocol driver '%s' does not support image creation",
4383 proto_drv->format_name);
4384 ret = -1;
4385 goto out;
4386 }
4387 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4388 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4389
4390 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4391
4392 size = blk_getlength(blk1);
4393 if (size < 0) {
4394 error_report("Failed to get size for '%s'", in.filename);
4395 ret = -1;
4396 goto out;
4397 }
4398
4399 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4400 dd.count * in.bsz < size) {
4401 size = dd.count * in.bsz;
4402 }
4403
Reda Sallahif7c15532016-08-10 16:16:09 +02004404 /* Overflow means the specified offset is beyond input image's size */
4405 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4406 size < in.bsz * in.offset)) {
4407 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4408 } else {
4409 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4410 size - in.bsz * in.offset, &error_abort);
4411 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004412
4413 ret = bdrv_create(drv, out.filename, opts, &local_err);
4414 if (ret < 0) {
4415 error_reportf_err(local_err,
4416 "%s: error while creating output image: ",
4417 out.filename);
4418 ret = -1;
4419 goto out;
4420 }
4421
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004422 /* TODO, we can't honour --image-opts for the target,
4423 * since it needs to be given in a format compatible
4424 * with the bdrv_create() call above which does not
4425 * support image-opts style.
4426 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01004427 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004428 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004429
4430 if (!blk2) {
4431 ret = -1;
4432 goto out;
4433 }
4434
Reda Sallahif7c15532016-08-10 16:16:09 +02004435 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4436 size < in.offset * in.bsz)) {
4437 /* We give a warning if the skip option is bigger than the input
4438 * size and create an empty output disk image (i.e. like dd(1)).
4439 */
4440 error_report("%s: cannot skip to specified offset", in.filename);
4441 in_pos = size;
4442 } else {
4443 in_pos = in.offset * in.bsz;
4444 }
4445
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004446 in.buf = g_new(uint8_t, in.bsz);
4447
Reda Sallahif7c15532016-08-10 16:16:09 +02004448 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004449 int in_ret, out_ret;
4450
4451 if (in_pos + in.bsz > size) {
4452 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4453 } else {
4454 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4455 }
4456 if (in_ret < 0) {
4457 error_report("error while reading from input image file: %s",
4458 strerror(-in_ret));
4459 ret = -1;
4460 goto out;
4461 }
4462 in_pos += in_ret;
4463
4464 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4465
4466 if (out_ret < 0) {
4467 error_report("error while writing to output image file: %s",
4468 strerror(-out_ret));
4469 ret = -1;
4470 goto out;
4471 }
4472 out_pos += out_ret;
4473 }
4474
4475out:
4476 g_free(arg);
4477 qemu_opts_del(opts);
4478 qemu_opts_free(create_opts);
4479 blk_unref(blk1);
4480 blk_unref(blk2);
4481 g_free(in.filename);
4482 g_free(out.filename);
4483 g_free(in.buf);
4484 g_free(out.buf);
4485
4486 if (ret) {
4487 return 1;
4488 }
4489 return 0;
4490}
4491
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004492static void dump_json_block_measure_info(BlockMeasureInfo *info)
4493{
4494 QString *str;
4495 QObject *obj;
4496 Visitor *v = qobject_output_visitor_new(&obj);
4497
4498 visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
4499 visit_complete(v, &obj);
4500 str = qobject_to_json_pretty(obj);
4501 assert(str != NULL);
4502 printf("%s\n", qstring_get_str(str));
4503 qobject_decref(obj);
4504 visit_free(v);
4505 QDECREF(str);
4506}
4507
4508static int img_measure(int argc, char **argv)
4509{
4510 static const struct option long_options[] = {
4511 {"help", no_argument, 0, 'h'},
4512 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4513 {"object", required_argument, 0, OPTION_OBJECT},
4514 {"output", required_argument, 0, OPTION_OUTPUT},
4515 {"size", required_argument, 0, OPTION_SIZE},
4516 {"force-share", no_argument, 0, 'U'},
4517 {0, 0, 0, 0}
4518 };
4519 OutputFormat output_format = OFORMAT_HUMAN;
4520 BlockBackend *in_blk = NULL;
4521 BlockDriver *drv;
4522 const char *filename = NULL;
4523 const char *fmt = NULL;
4524 const char *out_fmt = "raw";
4525 char *options = NULL;
4526 char *snapshot_name = NULL;
4527 bool force_share = false;
4528 QemuOpts *opts = NULL;
4529 QemuOpts *object_opts = NULL;
4530 QemuOpts *sn_opts = NULL;
4531 QemuOptsList *create_opts = NULL;
4532 bool image_opts = false;
4533 uint64_t img_size = UINT64_MAX;
4534 BlockMeasureInfo *info = NULL;
4535 Error *local_err = NULL;
4536 int ret = 1;
4537 int c;
4538
4539 while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
4540 long_options, NULL)) != -1) {
4541 switch (c) {
4542 case '?':
4543 case 'h':
4544 help();
4545 break;
4546 case 'f':
4547 fmt = optarg;
4548 break;
4549 case 'O':
4550 out_fmt = optarg;
4551 break;
4552 case 'o':
4553 if (!is_valid_option_list(optarg)) {
4554 error_report("Invalid option list: %s", optarg);
4555 goto out;
4556 }
4557 if (!options) {
4558 options = g_strdup(optarg);
4559 } else {
4560 char *old_options = options;
4561 options = g_strdup_printf("%s,%s", options, optarg);
4562 g_free(old_options);
4563 }
4564 break;
4565 case 'l':
4566 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
4567 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
4568 optarg, false);
4569 if (!sn_opts) {
4570 error_report("Failed in parsing snapshot param '%s'",
4571 optarg);
4572 goto out;
4573 }
4574 } else {
4575 snapshot_name = optarg;
4576 }
4577 break;
4578 case 'U':
4579 force_share = true;
4580 break;
4581 case OPTION_OBJECT:
4582 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
4583 optarg, true);
4584 if (!object_opts) {
4585 goto out;
4586 }
4587 break;
4588 case OPTION_IMAGE_OPTS:
4589 image_opts = true;
4590 break;
4591 case OPTION_OUTPUT:
4592 if (!strcmp(optarg, "json")) {
4593 output_format = OFORMAT_JSON;
4594 } else if (!strcmp(optarg, "human")) {
4595 output_format = OFORMAT_HUMAN;
4596 } else {
4597 error_report("--output must be used with human or json "
4598 "as argument.");
4599 goto out;
4600 }
4601 break;
4602 case OPTION_SIZE:
4603 {
4604 int64_t sval;
4605
4606 sval = cvtnum(optarg);
4607 if (sval < 0) {
4608 if (sval == -ERANGE) {
4609 error_report("Image size must be less than 8 EiB!");
4610 } else {
4611 error_report("Invalid image size specified! You may use "
4612 "k, M, G, T, P or E suffixes for ");
4613 error_report("kilobytes, megabytes, gigabytes, terabytes, "
4614 "petabytes and exabytes.");
4615 }
4616 goto out;
4617 }
4618 img_size = (uint64_t)sval;
4619 }
4620 break;
4621 }
4622 }
4623
4624 if (qemu_opts_foreach(&qemu_object_opts,
4625 user_creatable_add_opts_foreach,
4626 NULL, NULL)) {
4627 goto out;
4628 }
4629
4630 if (argc - optind > 1) {
4631 error_report("At most one filename argument is allowed.");
4632 goto out;
4633 } else if (argc - optind == 1) {
4634 filename = argv[optind];
4635 }
4636
4637 if (!filename &&
4638 (object_opts || image_opts || fmt || snapshot_name || sn_opts)) {
4639 error_report("--object, --image-opts, -f, and -l "
4640 "require a filename argument.");
4641 goto out;
4642 }
4643 if (filename && img_size != UINT64_MAX) {
4644 error_report("--size N cannot be used together with a filename.");
4645 goto out;
4646 }
4647 if (!filename && img_size == UINT64_MAX) {
4648 error_report("Either --size N or one filename must be specified.");
4649 goto out;
4650 }
4651
4652 if (filename) {
4653 in_blk = img_open(image_opts, filename, fmt, 0,
4654 false, false, force_share);
4655 if (!in_blk) {
4656 goto out;
4657 }
4658
4659 if (sn_opts) {
4660 bdrv_snapshot_load_tmp(blk_bs(in_blk),
4661 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
4662 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
4663 &local_err);
4664 } else if (snapshot_name != NULL) {
4665 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
4666 snapshot_name, &local_err);
4667 }
4668 if (local_err) {
4669 error_reportf_err(local_err, "Failed to load snapshot: ");
4670 goto out;
4671 }
4672 }
4673
4674 drv = bdrv_find_format(out_fmt);
4675 if (!drv) {
4676 error_report("Unknown file format '%s'", out_fmt);
4677 goto out;
4678 }
4679 if (!drv->create_opts) {
4680 error_report("Format driver '%s' does not support image creation",
4681 drv->format_name);
4682 goto out;
4683 }
4684
4685 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4686 create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
4687 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4688 if (options) {
4689 qemu_opts_do_parse(opts, options, NULL, &local_err);
4690 if (local_err) {
4691 error_report_err(local_err);
4692 error_report("Invalid options for file format '%s'", out_fmt);
4693 goto out;
4694 }
4695 }
4696 if (img_size != UINT64_MAX) {
4697 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
4698 }
4699
4700 info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
4701 if (local_err) {
4702 error_report_err(local_err);
4703 goto out;
4704 }
4705
4706 if (output_format == OFORMAT_HUMAN) {
4707 printf("required size: %" PRIu64 "\n", info->required);
4708 printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
4709 } else {
4710 dump_json_block_measure_info(info);
4711 }
4712
4713 ret = 0;
4714
4715out:
4716 qapi_free_BlockMeasureInfo(info);
4717 qemu_opts_del(object_opts);
4718 qemu_opts_del(opts);
4719 qemu_opts_del(sn_opts);
4720 qemu_opts_free(create_opts);
4721 g_free(options);
4722 blk_unref(in_blk);
4723 return ret;
4724}
Kevin Wolfb6133b82014-08-05 14:17:13 +02004725
Anthony Liguoric227f092009-10-01 16:12:16 -05004726static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004727#define DEF(option, callback, arg_string) \
4728 { option, callback },
4729#include "qemu-img-cmds.h"
4730#undef DEF
4731#undef GEN_DOCS
4732 { NULL, NULL, },
4733};
4734
bellardea2384d2004-08-01 21:59:26 +00004735int main(int argc, char **argv)
4736{
Anthony Liguoric227f092009-10-01 16:12:16 -05004737 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004738 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004739 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004740 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004741 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004742 static const struct option long_options[] = {
4743 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004744 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004745 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004746 {0, 0, 0, 0}
4747 };
bellardea2384d2004-08-01 21:59:26 +00004748
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004749#ifdef CONFIG_POSIX
4750 signal(SIGPIPE, SIG_IGN);
4751#endif
4752
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004753 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004754 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004755 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004756
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004757 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004758 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004759 exit(EXIT_FAILURE);
4760 }
4761
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004762 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004763
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004764 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004765 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004766 if (argc < 2) {
4767 error_exit("Not enough arguments");
4768 }
Stuart Brady153859b2009-06-07 00:42:17 +01004769
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004770 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004771 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004772 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004773
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004774 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004775 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004776 case ':':
4777 missing_argument(argv[optind - 1]);
4778 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08004779 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004780 unrecognized_option(argv[optind - 1]);
4781 return 0;
4782 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03004783 help();
4784 return 0;
4785 case 'V':
4786 printf(QEMU_IMG_VERSION);
4787 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004788 case 'T':
4789 g_free(trace_file);
4790 trace_file = trace_opt_parse(optarg);
4791 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004792 }
bellardea2384d2004-08-01 21:59:26 +00004793 }
Stuart Brady153859b2009-06-07 00:42:17 +01004794
Denis V. Lunev10985132016-06-17 17:44:13 +03004795 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004796
Denis V. Lunev10985132016-06-17 17:44:13 +03004797 /* reset getopt_long scanning */
4798 argc -= optind;
4799 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004800 return 0;
4801 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004802 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004803 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004804
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004805 if (!trace_init_backends()) {
4806 exit(1);
4807 }
4808 trace_init_file(trace_file);
4809 qemu_set_log(LOG_TRACE);
4810
Denis V. Lunev10985132016-06-17 17:44:13 +03004811 /* find the command */
4812 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4813 if (!strcmp(cmdname, cmd->name)) {
4814 return cmd->handler(argc, argv);
4815 }
4816 }
Jeff Cody7db16892014-04-25 17:02:32 -04004817
Stuart Brady153859b2009-06-07 00:42:17 +01004818 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004819 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004820}