blob: c6b62638530b5ba4290c92195e2ed61699e63750 [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/*
Eric Blakedc61cd32017-10-11 22:47:14 -05001159 * Compares two buffers sector by sector. Returns 0 if the first
1160 * sector of each buffer matches, non-zero otherwise.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001161 *
Eric Blakedc61cd32017-10-11 22:47:14 -05001162 * pnum is set to the sector-aligned size of the buffer prefix that
1163 * has the same matching status as the first sector.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001164 */
Eric Blakedc61cd32017-10-11 22:47:14 -05001165static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2,
1166 int64_t bytes, int64_t *pnum)
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001167{
Radim KrčmÔř8c1ac472015-02-20 17:06:15 +01001168 bool res;
Eric Blakedc61cd32017-10-11 22:47:14 -05001169 int64_t i = MIN(bytes, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001170
Eric Blakedc61cd32017-10-11 22:47:14 -05001171 assert(bytes > 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001172
Eric Blakedc61cd32017-10-11 22:47:14 -05001173 res = !!memcmp(buf1, buf2, i);
1174 while (i < bytes) {
1175 int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001176
Eric Blakedc61cd32017-10-11 22:47:14 -05001177 if (!!memcmp(buf1 + i, buf2 + i, len) != res) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001178 break;
1179 }
Eric Blakedc61cd32017-10-11 22:47:14 -05001180 i += len;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001181 }
1182
1183 *pnum = i;
1184 return res;
1185}
1186
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001187#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001188
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001189static int64_t sectors_to_bytes(int64_t sectors)
1190{
1191 return sectors << BDRV_SECTOR_BITS;
1192}
1193
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001194/*
1195 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1196 *
Eric Blake0608e402017-10-11 22:47:12 -05001197 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1198 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1199 * failure), and 4 on error (the exit status for read errors), after emitting
1200 * an error message.
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001201 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001202 * @param blk: BlockBackend for the image
Eric Blakec41508e2017-10-11 22:47:13 -05001203 * @param offset: Starting offset to check
1204 * @param bytes: Number of bytes to check
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001205 * @param filename: Name of disk file we are checking (logging purpose)
1206 * @param buffer: Allocated buffer for storing read data
1207 * @param quiet: Flag for quiet mode
1208 */
Eric Blakec41508e2017-10-11 22:47:13 -05001209static int check_empty_sectors(BlockBackend *blk, int64_t offset,
1210 int64_t bytes, const char *filename,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001211 uint8_t *buffer, bool quiet)
1212{
Eric Blakedebb38a2017-10-11 22:47:11 -05001213 int ret = 0;
1214 int64_t idx;
1215
Eric Blakec41508e2017-10-11 22:47:13 -05001216 ret = blk_pread(blk, offset, buffer, bytes);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001217 if (ret < 0) {
1218 error_report("Error while reading offset %" PRId64 " of %s: %s",
Eric Blakec41508e2017-10-11 22:47:13 -05001219 offset, filename, strerror(-ret));
Eric Blake0608e402017-10-11 22:47:12 -05001220 return 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001221 }
Eric Blakec41508e2017-10-11 22:47:13 -05001222 idx = find_nonzero(buffer, bytes);
Eric Blakedebb38a2017-10-11 22:47:11 -05001223 if (idx >= 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001224 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blakec41508e2017-10-11 22:47:13 -05001225 offset + idx);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001226 return 1;
1227 }
1228
1229 return 0;
1230}
1231
1232/*
1233 * Compares two images. Exit codes:
1234 *
1235 * 0 - Images are identical
1236 * 1 - Images differ
1237 * >1 - Error occurred
1238 */
1239static int img_compare(int argc, char **argv)
1240{
Max Reitz40055952014-07-22 22:58:42 +02001241 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001242 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001243 BlockDriverState *bs1, *bs2;
1244 int64_t total_sectors1, total_sectors2;
1245 uint8_t *buf1 = NULL, *buf2 = NULL;
Eric Blake31826642017-10-11 22:47:08 -05001246 int64_t pnum1, pnum2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001247 int allocated1, allocated2;
1248 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1249 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001250 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001251 bool writethrough;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001252 int64_t total_sectors;
1253 int64_t sector_num = 0;
1254 int64_t nb_sectors;
Eric Blakedc61cd32017-10-11 22:47:14 -05001255 int c;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001256 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001257 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001258 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001259
Max Reitz40055952014-07-22 22:58:42 +02001260 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001261 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001262 static const struct option long_options[] = {
1263 {"help", no_argument, 0, 'h'},
1264 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001265 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001266 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001267 {0, 0, 0, 0}
1268 };
Fam Zheng335e9932017-05-03 00:35:39 +08001269 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001270 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001271 if (c == -1) {
1272 break;
1273 }
1274 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001275 case ':':
1276 missing_argument(argv[optind - 1]);
1277 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001278 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001279 unrecognized_option(argv[optind - 1]);
1280 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001281 case 'h':
1282 help();
1283 break;
1284 case 'f':
1285 fmt1 = optarg;
1286 break;
1287 case 'F':
1288 fmt2 = optarg;
1289 break;
Max Reitz40055952014-07-22 22:58:42 +02001290 case 'T':
1291 cache = optarg;
1292 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001293 case 'p':
1294 progress = true;
1295 break;
1296 case 'q':
1297 quiet = true;
1298 break;
1299 case 's':
1300 strict = true;
1301 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001302 case 'U':
1303 force_share = true;
1304 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001305 case OPTION_OBJECT: {
1306 QemuOpts *opts;
1307 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1308 optarg, true);
1309 if (!opts) {
1310 ret = 2;
1311 goto out4;
1312 }
1313 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001314 case OPTION_IMAGE_OPTS:
1315 image_opts = true;
1316 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001317 }
1318 }
1319
1320 /* Progress is not shown in Quiet mode */
1321 if (quiet) {
1322 progress = false;
1323 }
1324
1325
Kevin Wolffc11eb22013-08-05 10:53:04 +02001326 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001327 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001328 }
1329 filename1 = argv[optind++];
1330 filename2 = argv[optind++];
1331
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001332 if (qemu_opts_foreach(&qemu_object_opts,
1333 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001334 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001335 ret = 2;
1336 goto out4;
1337 }
1338
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001339 /* Initialize before goto out */
1340 qemu_progress_init(progress, 2.0);
1341
Kevin Wolfce099542016-03-15 13:01:04 +01001342 flags = 0;
1343 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001344 if (ret < 0) {
1345 error_report("Invalid source cache option: %s", cache);
1346 ret = 2;
1347 goto out3;
1348 }
1349
Fam Zheng335e9932017-05-03 00:35:39 +08001350 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1351 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001352 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001353 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001354 goto out3;
1355 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001356
Fam Zheng335e9932017-05-03 00:35:39 +08001357 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1358 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001359 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001360 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001361 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001362 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001363 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001364 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001365
Max Reitzf1d3cd72015-02-05 13:58:18 -05001366 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1367 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1368 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001369 if (total_sectors1 < 0) {
1370 error_report("Can't get size of %s: %s",
1371 filename1, strerror(-total_sectors1));
1372 ret = 4;
1373 goto out;
1374 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001375 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001376 if (total_sectors2 < 0) {
1377 error_report("Can't get size of %s: %s",
1378 filename2, strerror(-total_sectors2));
1379 ret = 4;
1380 goto out;
1381 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001382 total_sectors = MIN(total_sectors1, total_sectors2);
1383 progress_base = MAX(total_sectors1, total_sectors2);
1384
1385 qemu_progress_print(0, 100);
1386
1387 if (strict && total_sectors1 != total_sectors2) {
1388 ret = 1;
1389 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1390 goto out;
1391 }
1392
Eric Blake7daddc62017-10-11 22:47:09 -05001393 while (sector_num < total_sectors) {
Eric Blake31826642017-10-11 22:47:08 -05001394 int status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001395
Eric Blake31826642017-10-11 22:47:08 -05001396 status1 = bdrv_block_status_above(bs1, NULL,
1397 sector_num * BDRV_SECTOR_SIZE,
1398 (total_sectors1 - sector_num) *
1399 BDRV_SECTOR_SIZE,
1400 &pnum1, NULL, NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001401 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001402 ret = 3;
1403 error_report("Sector allocation test failed for %s", filename1);
1404 goto out;
1405 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001406 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001407
Eric Blake31826642017-10-11 22:47:08 -05001408 status2 = bdrv_block_status_above(bs2, NULL,
1409 sector_num * BDRV_SECTOR_SIZE,
1410 (total_sectors2 - sector_num) *
1411 BDRV_SECTOR_SIZE,
1412 &pnum2, NULL, NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001413 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001414 ret = 3;
1415 error_report("Sector allocation test failed for %s", filename2);
1416 goto out;
1417 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001418 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
Eric Blake31826642017-10-11 22:47:08 -05001419 /* TODO: Relax this once comparison is byte-based, and we no longer
1420 * have to worry about sector alignment */
1421 assert(QEMU_IS_ALIGNED(pnum1 | pnum2, BDRV_SECTOR_SIZE));
Eric Blake7daddc62017-10-11 22:47:09 -05001422
1423 assert(pnum1 && pnum2);
1424 nb_sectors = MIN(pnum1, pnum2) >> BDRV_SECTOR_BITS;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001425
Fam Zheng25ad8e62016-01-13 16:37:41 +08001426 if (strict) {
Eric Blake31826642017-10-11 22:47:08 -05001427 if (status1 != status2) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001428 ret = 1;
1429 qprintf(quiet, "Strict mode: Offset %" PRId64
1430 " block status mismatch!\n",
1431 sectors_to_bytes(sector_num));
1432 goto out;
1433 }
1434 }
1435 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
Eric Blake7daddc62017-10-11 22:47:09 -05001436 /* nothing to do */
Fam Zheng25ad8e62016-01-13 16:37:41 +08001437 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001438 if (allocated1) {
Eric Blakedc61cd32017-10-11 22:47:14 -05001439 int64_t pnum;
1440
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 }
Eric Blakedc61cd32017-10-11 22:47:14 -05001460 ret = compare_buffers(buf1, buf2,
1461 nb_sectors * BDRV_SECTOR_SIZE, &pnum);
1462 if (ret || pnum != nb_sectors * BDRV_SECTOR_SIZE) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001463 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blakedc61cd32017-10-11 22:47:14 -05001464 sectors_to_bytes(sector_num) + (ret ? 0 : 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) {
Eric Blakec41508e2017-10-11 22:47:13 -05001472 ret = check_empty_sectors(blk1, sector_num * BDRV_SECTOR_SIZE,
1473 nb_sectors * BDRV_SECTOR_SIZE,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001474 filename1, buf1, quiet);
1475 } else {
Eric Blakec41508e2017-10-11 22:47:13 -05001476 ret = check_empty_sectors(blk2, sector_num * BDRV_SECTOR_SIZE,
1477 nb_sectors * BDRV_SECTOR_SIZE,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001478 filename2, buf1, quiet);
1479 }
1480 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001481 goto out;
1482 }
1483 }
1484 sector_num += nb_sectors;
1485 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1486 }
1487
1488 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001489 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001490 const char *filename_over;
1491
1492 qprintf(quiet, "Warning: Image size mismatch!\n");
1493 if (total_sectors1 > total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001494 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001495 filename_over = filename1;
1496 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001497 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001498 filename_over = filename2;
1499 }
1500
Eric Blake7daddc62017-10-11 22:47:09 -05001501 while (sector_num < progress_base) {
Eric Blake51b0a482017-07-07 07:44:59 -05001502 int64_t count;
1503
Eric Blake391cb1a2017-10-11 22:47:10 -05001504 ret = bdrv_block_status_above(blk_bs(blk_over), NULL,
Eric Blake51b0a482017-07-07 07:44:59 -05001505 sector_num * BDRV_SECTOR_SIZE,
Eric Blake7daddc62017-10-11 22:47:09 -05001506 (progress_base - sector_num) *
1507 BDRV_SECTOR_SIZE,
Eric Blake391cb1a2017-10-11 22:47:10 -05001508 &count, NULL, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001509 if (ret < 0) {
1510 ret = 3;
1511 error_report("Sector allocation test failed for %s",
1512 filename_over);
1513 goto out;
1514
1515 }
Eric Blake391cb1a2017-10-11 22:47:10 -05001516 /* TODO relax this once bdrv_block_status_above does not enforce
Eric Blake51b0a482017-07-07 07:44:59 -05001517 * sector alignment */
1518 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
1519 nb_sectors = count >> BDRV_SECTOR_BITS;
Eric Blake391cb1a2017-10-11 22:47:10 -05001520 if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) {
Eric Blake7daddc62017-10-11 22:47:09 -05001521 nb_sectors = MIN(nb_sectors, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
Eric Blakec41508e2017-10-11 22:47:13 -05001522 ret = check_empty_sectors(blk_over,
1523 sector_num * BDRV_SECTOR_SIZE,
1524 nb_sectors * BDRV_SECTOR_SIZE,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001525 filename_over, buf1, quiet);
1526 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001527 goto out;
1528 }
1529 }
1530 sector_num += nb_sectors;
1531 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1532 }
1533 }
1534
1535 qprintf(quiet, "Images are identical.\n");
1536 ret = 0;
1537
1538out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001539 qemu_vfree(buf1);
1540 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001541 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001542out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001543 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001544out3:
1545 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001546out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001547 return ret;
1548}
1549
Kevin Wolf690c7302015-03-19 13:33:32 +01001550enum ImgConvertBlockStatus {
1551 BLK_DATA,
1552 BLK_ZERO,
1553 BLK_BACKING_FILE,
1554};
1555
Peter Lieven2d9187b2017-02-28 13:40:07 +01001556#define MAX_COROUTINES 16
1557
Kevin Wolf690c7302015-03-19 13:33:32 +01001558typedef struct ImgConvertState {
1559 BlockBackend **src;
1560 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001561 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001562 int64_t total_sectors;
1563 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001564 int64_t allocated_done;
1565 int64_t sector_num;
1566 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001567 enum ImgConvertBlockStatus status;
1568 int64_t sector_next_status;
1569 BlockBackend *target;
1570 bool has_zero_init;
1571 bool compressed;
1572 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001573 bool wr_in_order;
Kevin Wolf690c7302015-03-19 13:33:32 +01001574 int min_sparse;
1575 size_t cluster_sectors;
1576 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001577 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001578 int running_coroutines;
1579 Coroutine *co[MAX_COROUTINES];
1580 int64_t wait_sector_num[MAX_COROUTINES];
1581 CoMutex lock;
1582 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001583} ImgConvertState;
1584
Peter Lieven2d9187b2017-02-28 13:40:07 +01001585static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1586 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001587{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001588 *src_cur = 0;
1589 *src_cur_offset = 0;
1590 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1591 *src_cur_offset += s->src_sectors[*src_cur];
1592 (*src_cur)++;
1593 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001594 }
1595}
1596
1597static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1598{
Eric Blake31826642017-10-11 22:47:08 -05001599 int64_t src_cur_offset;
1600 int ret, n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001601
Peter Lieven2d9187b2017-02-28 13:40:07 +01001602 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001603
1604 assert(s->total_sectors > sector_num);
1605 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1606
1607 if (s->sector_next_status <= sector_num) {
Eric Blake31826642017-10-11 22:47:08 -05001608 int64_t count = n * BDRV_SECTOR_SIZE;
1609
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001610 if (s->target_has_backing) {
Eric Blake237d78f2017-10-11 22:47:03 -05001611
1612 ret = bdrv_block_status(blk_bs(s->src[src_cur]),
1613 (sector_num - src_cur_offset) *
1614 BDRV_SECTOR_SIZE,
1615 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001616 } else {
Eric Blake31826642017-10-11 22:47:08 -05001617 ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL,
1618 (sector_num - src_cur_offset) *
1619 BDRV_SECTOR_SIZE,
1620 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001621 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001622 if (ret < 0) {
1623 return ret;
1624 }
Eric Blake31826642017-10-11 22:47:08 -05001625 n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
Kevin Wolf690c7302015-03-19 13:33:32 +01001626
1627 if (ret & BDRV_BLOCK_ZERO) {
1628 s->status = BLK_ZERO;
1629 } else if (ret & BDRV_BLOCK_DATA) {
1630 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001631 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001632 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001633 }
1634
1635 s->sector_next_status = sector_num + n;
1636 }
1637
1638 n = MIN(n, s->sector_next_status - sector_num);
1639 if (s->status == BLK_DATA) {
1640 n = MIN(n, s->buf_sectors);
1641 }
1642
1643 /* We need to write complete clusters for compressed images, so if an
1644 * unallocated area is shorter than that, we must consider the whole
1645 * cluster allocated. */
1646 if (s->compressed) {
1647 if (n < s->cluster_sectors) {
1648 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1649 s->status = BLK_DATA;
1650 } else {
1651 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1652 }
1653 }
1654
1655 return n;
1656}
1657
Peter Lieven2d9187b2017-02-28 13:40:07 +01001658static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1659 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001660{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001661 int n, ret;
1662 QEMUIOVector qiov;
1663 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001664
Kevin Wolf690c7302015-03-19 13:33:32 +01001665 assert(nb_sectors <= s->buf_sectors);
1666 while (nb_sectors > 0) {
1667 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001668 int src_cur;
1669 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001670
1671 /* In the case of compression with multiple source files, we can get a
1672 * nb_sectors that spreads into the next part. So we must be able to
1673 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001674 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1675 blk = s->src[src_cur];
1676 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001677
Peter Lieven2d9187b2017-02-28 13:40:07 +01001678 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1679 iov.iov_base = buf;
1680 iov.iov_len = n << BDRV_SECTOR_BITS;
1681 qemu_iovec_init_external(&qiov, &iov, 1);
1682
1683 ret = blk_co_preadv(
1684 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1685 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001686 if (ret < 0) {
1687 return ret;
1688 }
1689
1690 sector_num += n;
1691 nb_sectors -= n;
1692 buf += n * BDRV_SECTOR_SIZE;
1693 }
1694
1695 return 0;
1696}
1697
Peter Lieven2d9187b2017-02-28 13:40:07 +01001698
1699static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1700 int nb_sectors, uint8_t *buf,
1701 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001702{
1703 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001704 QEMUIOVector qiov;
1705 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001706
1707 while (nb_sectors > 0) {
1708 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001709 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1710
Peter Lieven2d9187b2017-02-28 13:40:07 +01001711 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001712 case BLK_BACKING_FILE:
1713 /* If we have a backing file, leave clusters unallocated that are
1714 * unallocated in the source image, so that the backing file is
1715 * visible at the respective offset. */
1716 assert(s->target_has_backing);
1717 break;
1718
1719 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001720 /* If we're told to keep the target fully allocated (-S 0) or there
1721 * is real non-zero data, we must write it. Otherwise we can treat
1722 * it as zero sectors.
1723 * Compressed clusters need to be written as a whole, so in that
1724 * case we can only save the write if the buffer is completely
1725 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001726 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001727 (!s->compressed &&
1728 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1729 (s->compressed &&
1730 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001731 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001732 iov.iov_base = buf;
1733 iov.iov_len = n << BDRV_SECTOR_BITS;
1734 qemu_iovec_init_external(&qiov, &iov, 1);
1735
1736 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
Lidong Chendb933fb2017-04-27 10:58:27 +08001737 n << BDRV_SECTOR_BITS, &qiov, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001738 if (ret < 0) {
1739 return ret;
1740 }
1741 break;
1742 }
1743 /* fall-through */
1744
1745 case BLK_ZERO:
1746 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001747 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001748 break;
1749 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001750 ret = blk_co_pwrite_zeroes(s->target,
1751 sector_num << BDRV_SECTOR_BITS,
1752 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001753 if (ret < 0) {
1754 return ret;
1755 }
1756 break;
1757 }
1758
1759 sector_num += n;
1760 nb_sectors -= n;
1761 buf += n * BDRV_SECTOR_SIZE;
1762 }
1763
1764 return 0;
1765}
1766
Peter Lieven2d9187b2017-02-28 13:40:07 +01001767static void coroutine_fn convert_co_do_copy(void *opaque)
1768{
1769 ImgConvertState *s = opaque;
1770 uint8_t *buf = NULL;
1771 int ret, i;
1772 int index = -1;
1773
1774 for (i = 0; i < s->num_coroutines; i++) {
1775 if (s->co[i] == qemu_coroutine_self()) {
1776 index = i;
1777 break;
1778 }
1779 }
1780 assert(index >= 0);
1781
1782 s->running_coroutines++;
1783 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1784
1785 while (1) {
1786 int n;
1787 int64_t sector_num;
1788 enum ImgConvertBlockStatus status;
1789
1790 qemu_co_mutex_lock(&s->lock);
1791 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1792 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001793 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001794 }
1795 n = convert_iteration_sectors(s, s->sector_num);
1796 if (n < 0) {
1797 qemu_co_mutex_unlock(&s->lock);
1798 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001799 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001800 }
1801 /* save current sector and allocation status to local variables */
1802 sector_num = s->sector_num;
1803 status = s->status;
1804 if (!s->min_sparse && s->status == BLK_ZERO) {
1805 n = MIN(n, s->buf_sectors);
1806 }
1807 /* increment global sector counter so that other coroutines can
1808 * already continue reading beyond this request */
1809 s->sector_num += n;
1810 qemu_co_mutex_unlock(&s->lock);
1811
1812 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1813 s->allocated_done += n;
1814 qemu_progress_print(100.0 * s->allocated_done /
1815 s->allocated_sectors, 0);
1816 }
1817
1818 if (status == BLK_DATA) {
1819 ret = convert_co_read(s, sector_num, n, buf);
1820 if (ret < 0) {
1821 error_report("error while reading sector %" PRId64
1822 ": %s", sector_num, strerror(-ret));
1823 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001824 }
1825 } else if (!s->min_sparse && status == BLK_ZERO) {
1826 status = BLK_DATA;
1827 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1828 }
1829
1830 if (s->wr_in_order) {
1831 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03001832 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001833 s->wait_sector_num[index] = sector_num;
1834 qemu_coroutine_yield();
1835 }
1836 s->wait_sector_num[index] = -1;
1837 }
1838
Anton Nefedovb91127e2017-04-26 11:33:15 +03001839 if (s->ret == -EINPROGRESS) {
1840 ret = convert_co_write(s, sector_num, n, buf, status);
1841 if (ret < 0) {
1842 error_report("error while writing sector %" PRId64
1843 ": %s", sector_num, strerror(-ret));
1844 s->ret = ret;
1845 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001846 }
1847
1848 if (s->wr_in_order) {
1849 /* reenter the coroutine that might have waited
1850 * for this write to complete */
1851 s->wr_offs = sector_num + n;
1852 for (i = 0; i < s->num_coroutines; i++) {
1853 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1854 /*
1855 * A -> B -> A cannot occur because A has
1856 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1857 * B will never enter A during this time window.
1858 */
1859 qemu_coroutine_enter(s->co[i]);
1860 break;
1861 }
1862 }
1863 }
1864 }
1865
Peter Lieven2d9187b2017-02-28 13:40:07 +01001866 qemu_vfree(buf);
1867 s->co[index] = NULL;
1868 s->running_coroutines--;
1869 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1870 /* the convert job finished successfully */
1871 s->ret = 0;
1872 }
1873}
1874
Kevin Wolf690c7302015-03-19 13:33:32 +01001875static int convert_do_copy(ImgConvertState *s)
1876{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001877 int ret, i, n;
1878 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001879
1880 /* Check whether we have zero initialisation or can get it efficiently */
1881 s->has_zero_init = s->min_sparse && !s->target_has_backing
1882 ? bdrv_has_zero_init(blk_bs(s->target))
1883 : false;
1884
1885 if (!s->has_zero_init && !s->target_has_backing &&
1886 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1887 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001888 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001889 if (ret == 0) {
1890 s->has_zero_init = true;
1891 }
1892 }
1893
1894 /* Allocate buffer for copied data. For compressed images, only one cluster
1895 * can be copied at a time. */
1896 if (s->compressed) {
1897 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1898 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001899 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001900 }
1901 s->buf_sectors = s->cluster_sectors;
1902 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001903
Kevin Wolf690c7302015-03-19 13:33:32 +01001904 while (sector_num < s->total_sectors) {
1905 n = convert_iteration_sectors(s, sector_num);
1906 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001907 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001908 }
Max Reitzaad15de2016-03-24 23:33:57 +01001909 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1910 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001911 s->allocated_sectors += n;
1912 }
1913 sector_num += n;
1914 }
1915
1916 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001917 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001918 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001919
Peter Lieven2d9187b2017-02-28 13:40:07 +01001920 qemu_co_mutex_init(&s->lock);
1921 for (i = 0; i < s->num_coroutines; i++) {
1922 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1923 s->wait_sector_num[i] = -1;
1924 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001925 }
1926
Anton Nefedovb91127e2017-04-26 11:33:15 +03001927 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001928 main_loop_wait(false);
1929 }
1930
1931 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001932 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001933 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001934 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001935 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001936 }
1937 }
1938
Peter Lieven2d9187b2017-02-28 13:40:07 +01001939 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001940}
1941
bellardea2384d2004-08-01 21:59:26 +00001942static int img_convert(int argc, char **argv)
1943{
Peter Lieven9fd77f92017-04-21 11:11:55 +02001944 int c, bs_i, flags, src_flags = 0;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001945 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02001946 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1947 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001948 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00001949 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001950 BlockDriverState *out_bs;
1951 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08001952 QemuOptsList *create_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001953 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02001954 Error *local_err = NULL;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001955 bool writethrough, src_writethrough, quiet = false, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001956 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001957 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08001958 bool force_share = false;
bellardea2384d2004-08-01 21:59:26 +00001959
Peter Lieven9fd77f92017-04-21 11:11:55 +02001960 ImgConvertState s = (ImgConvertState) {
1961 /* Need at least 4k of zeros for sparse detection */
1962 .min_sparse = 8,
1963 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1964 .wr_in_order = true,
1965 .num_coroutines = 8,
1966 };
1967
bellardea2384d2004-08-01 21:59:26 +00001968 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001969 static const struct option long_options[] = {
1970 {"help", no_argument, 0, 'h'},
1971 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001972 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001973 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001974 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001975 {0, 0, 0, 0}
1976 };
Daniel P. Berrange6b4df542017-07-04 13:30:09 +01001977 c = getopt_long(argc, argv, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001978 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001979 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001980 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001981 }
bellardea2384d2004-08-01 21:59:26 +00001982 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001983 case ':':
1984 missing_argument(argv[optind - 1]);
1985 break;
Jes Sorensenef873942010-12-06 15:25:40 +01001986 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001987 unrecognized_option(argv[optind - 1]);
1988 break;
bellardea2384d2004-08-01 21:59:26 +00001989 case 'h':
1990 help();
1991 break;
1992 case 'f':
1993 fmt = optarg;
1994 break;
1995 case 'O':
1996 out_fmt = optarg;
1997 break;
thsf58c7b32008-06-05 21:53:49 +00001998 case 'B':
1999 out_baseimg = optarg;
2000 break;
bellardea2384d2004-08-01 21:59:26 +00002001 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002002 s.compressed = true;
bellardea2384d2004-08-01 21:59:26 +00002003 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002004 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01002005 if (!is_valid_option_list(optarg)) {
2006 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002007 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01002008 }
2009 if (!options) {
2010 options = g_strdup(optarg);
2011 } else {
2012 char *old_options = options;
2013 options = g_strdup_printf("%s,%s", options, optarg);
2014 g_free(old_options);
2015 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002016 break;
edison51ef6722010-09-21 19:58:41 -07002017 case 's':
2018 snapshot_name = optarg;
2019 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08002020 case 'l':
2021 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01002022 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2023 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08002024 if (!sn_opts) {
2025 error_report("Failed in parsing snapshot param '%s'",
2026 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002027 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002028 }
2029 } else {
2030 snapshot_name = optarg;
2031 }
2032 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002033 case 'S':
2034 {
2035 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002036
2037 sval = cvtnum(optarg);
2038 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02002039 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002040 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002041 }
2042
Peter Lieven9fd77f92017-04-21 11:11:55 +02002043 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002044 break;
2045 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002046 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002047 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002048 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002049 case 't':
2050 cache = optarg;
2051 break;
Max Reitz40055952014-07-22 22:58:42 +02002052 case 'T':
2053 src_cache = optarg;
2054 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002055 case 'q':
2056 quiet = true;
2057 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002058 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002059 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002060 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002061 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002062 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2063 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002064 error_report("Invalid number of coroutines. Allowed number of"
2065 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002066 goto fail_getopt;
2067 }
2068 break;
2069 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002070 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002071 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002072 case 'U':
2073 force_share = true;
2074 break;
Max Reitz3258b912017-04-26 15:46:47 +02002075 case OPTION_OBJECT: {
2076 QemuOpts *object_opts;
2077 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2078 optarg, true);
2079 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002080 goto fail_getopt;
2081 }
2082 break;
Max Reitz3258b912017-04-26 15:46:47 +02002083 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002084 case OPTION_IMAGE_OPTS:
2085 image_opts = true;
2086 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002087 case OPTION_TARGET_IMAGE_OPTS:
2088 tgt_image_opts = true;
2089 break;
bellardea2384d2004-08-01 21:59:26 +00002090 }
2091 }
ths3b46e622007-09-17 08:09:54 +00002092
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002093 if (!out_fmt && !tgt_image_opts) {
2094 out_fmt = "raw";
2095 }
2096
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002097 if (qemu_opts_foreach(&qemu_object_opts,
2098 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002099 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002100 goto fail_getopt;
2101 }
2102
Peter Lieven9fd77f92017-04-21 11:11:55 +02002103 if (!s.wr_in_order && s.compressed) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002104 error_report("Out of order write and compress are mutually exclusive");
Peter Lieven9fd77f92017-04-21 11:11:55 +02002105 goto fail_getopt;
2106 }
2107
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002108 if (tgt_image_opts && !skip_create) {
2109 error_report("--target-image-opts requires use of -n flag");
2110 goto fail_getopt;
2111 }
2112
Peter Lieven9fd77f92017-04-21 11:11:55 +02002113 s.src_num = argc - optind - 1;
2114 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2115
2116 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002117 if (out_fmt) {
2118 ret = print_block_option_help(out_filename, out_fmt);
2119 goto fail_getopt;
2120 } else {
2121 error_report("Option help requires a format be specified");
2122 goto fail_getopt;
2123 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002124 }
2125
2126 if (s.src_num < 1) {
2127 error_report("Must specify image file name");
2128 goto fail_getopt;
2129 }
2130
2131
Peter Lieven9fd77f92017-04-21 11:11:55 +02002132 /* ret is still -EINVAL until here */
2133 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2134 if (ret < 0) {
2135 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002136 goto fail_getopt;
2137 }
2138
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002139 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002140 if (quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002141 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002142 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002143 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002144 qemu_progress_print(0, 100);
2145
Peter Lieven9fd77f92017-04-21 11:11:55 +02002146 s.src = g_new0(BlockBackend *, s.src_num);
2147 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002148
Peter Lieven9fd77f92017-04-21 11:11:55 +02002149 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2150 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Fam Zheng335e9932017-05-03 00:35:39 +08002151 fmt, src_flags, src_writethrough, quiet,
2152 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002153 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002154 ret = -1;
2155 goto out;
2156 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002157 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2158 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002159 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002160 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002161 ret = -1;
2162 goto out;
2163 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002164 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002165 }
bellardea2384d2004-08-01 21:59:26 +00002166
Wenchao Xiaef806542013-12-04 17:10:57 +08002167 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002168 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002169 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2170 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2171 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002172 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002173 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002174 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002175 ret = -1;
2176 goto out;
2177 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002178
Peter Lieven9fd77f92017-04-21 11:11:55 +02002179 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2180 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002181 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002182 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002183 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002184 ret = -1;
2185 goto out;
edison51ef6722010-09-21 19:58:41 -07002186 }
2187
Max Reitz2e024cd2015-02-11 09:58:46 -05002188 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002189 /* Find driver and parse its options */
2190 drv = bdrv_find_format(out_fmt);
2191 if (!drv) {
2192 error_report("Unknown file format '%s'", out_fmt);
2193 ret = -1;
2194 goto out;
2195 }
2196
2197 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2198 if (!proto_drv) {
2199 error_report_err(local_err);
2200 ret = -1;
2201 goto out;
2202 }
2203
Max Reitz2e024cd2015-02-11 09:58:46 -05002204 if (!drv->create_opts) {
2205 error_report("Format driver '%s' does not support image creation",
2206 drv->format_name);
2207 ret = -1;
2208 goto out;
2209 }
Max Reitzf75613c2014-12-02 18:32:46 +01002210
Max Reitz2e024cd2015-02-11 09:58:46 -05002211 if (!proto_drv->create_opts) {
2212 error_report("Protocol driver '%s' does not support image creation",
2213 proto_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 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2219 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002220
Max Reitz2e024cd2015-02-11 09:58:46 -05002221 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002222 if (options) {
2223 qemu_opts_do_parse(opts, options, NULL, &local_err);
2224 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002225 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002226 ret = -1;
2227 goto out;
2228 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002229 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002230
Peter Lieven9fd77f92017-04-21 11:11:55 +02002231 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002232 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002233 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2234 if (ret < 0) {
2235 goto out;
2236 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002237 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002238
Kevin Wolfa18953f2010-10-14 15:46:04 +02002239 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002240 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002241 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002242 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002243 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002244 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002245
Max Reitz48758a82017-04-26 15:46:48 +02002246 if (s.src_num > 1 && out_baseimg) {
2247 error_report("Having a backing file for the target makes no sense when "
2248 "concatenating multiple input images");
2249 ret = -1;
2250 goto out;
2251 }
2252
Kevin Wolfefa84d42009-05-18 16:42:12 +02002253 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002254 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002255 bool encryption =
2256 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002257 const char *encryptfmt =
2258 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
Chunyan Liu83d05212014-06-05 17:20:51 +08002259 const char *preallocation =
2260 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002261
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002262 if (drv && !drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002263 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002264 ret = -1;
2265 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002266 }
2267
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002268 if (encryption || encryptfmt) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002269 error_report("Compression and encryption not supported at "
2270 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002271 ret = -1;
2272 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002273 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002274
Chunyan Liu83d05212014-06-05 17:20:51 +08002275 if (preallocation
2276 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002277 {
2278 error_report("Compression and preallocation not supported at "
2279 "the same time");
2280 ret = -1;
2281 goto out;
2282 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002283 }
2284
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002285 if (!skip_create) {
2286 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002287 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002288 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002289 error_reportf_err(local_err, "%s: error while converting %s: ",
2290 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002291 goto out;
bellardea2384d2004-08-01 21:59:26 +00002292 }
2293 }
ths3b46e622007-09-17 08:09:54 +00002294
Peter Lieven9fd77f92017-04-21 11:11:55 +02002295 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002296 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002297 if (ret < 0) {
2298 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002299 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002300 }
2301
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002302 if (skip_create) {
2303 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2304 flags, writethrough, quiet, false);
2305 } else {
2306 /* TODO ultimately we should allow --target-image-opts
2307 * to be used even when -n is not given.
2308 * That has to wait for bdrv_create to be improved
2309 * to allow filenames in option syntax
2310 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01002311 s.target = img_open_new_file(out_filename, opts, out_fmt,
2312 flags, writethrough, quiet, false);
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002313 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002314 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002315 ret = -1;
2316 goto out;
2317 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002318 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002319
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002320 if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2321 error_report("Compression not supported for this file format");
2322 ret = -1;
2323 goto out;
2324 }
2325
Eric Blake5def6b82016-06-23 16:37:19 -06002326 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002327 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2328 * as maximum. */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002329 s.buf_sectors = MIN(32768,
2330 MAX(s.buf_sectors,
2331 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2332 out_bs->bl.pdiscard_alignment >>
2333 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002334
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002335 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002336 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002337 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002338 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002339 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002340 ret = -1;
2341 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002342 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002343 error_report("output file is smaller than input file");
2344 ret = -1;
2345 goto out;
2346 }
2347 }
2348
Peter Lieven24f833c2013-11-27 11:07:07 +01002349 ret = bdrv_get_info(out_bs, &bdi);
2350 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002351 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002352 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002353 goto out;
2354 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002355 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002356 s.compressed = s.compressed || bdi.needs_compressed_writes;
2357 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Peter Lieven24f833c2013-11-27 11:07:07 +01002358 }
2359
Peter Lieven9fd77f92017-04-21 11:11:55 +02002360 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002361out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002362 if (!ret) {
2363 qemu_progress_print(100, 0);
2364 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002365 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002366 qemu_opts_del(opts);
2367 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002368 qemu_opts_del(sn_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002369 blk_unref(s.target);
2370 if (s.src) {
2371 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2372 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002373 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002374 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002375 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002376 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002377fail_getopt:
2378 g_free(options);
2379
Peter Lieven9fd77f92017-04-21 11:11:55 +02002380 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002381}
2382
bellard57d1a2b2004-08-03 21:15:11 +00002383
bellardfaea38e2006-08-05 21:31:00 +00002384static void dump_snapshots(BlockDriverState *bs)
2385{
2386 QEMUSnapshotInfo *sn_tab, *sn;
2387 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002388
2389 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2390 if (nb_sns <= 0)
2391 return;
2392 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002393 bdrv_snapshot_dump(fprintf, stdout, NULL);
2394 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002395 for(i = 0; i < nb_sns; i++) {
2396 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002397 bdrv_snapshot_dump(fprintf, stdout, sn);
2398 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002399 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002400 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002401}
2402
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002403static void dump_json_image_info_list(ImageInfoList *list)
2404{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002405 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002406 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002407 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002408
2409 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2410 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002411 str = qobject_to_json_pretty(obj);
2412 assert(str != NULL);
2413 printf("%s\n", qstring_get_str(str));
2414 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002415 visit_free(v);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002416 QDECREF(str);
2417}
2418
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002419static void dump_json_image_info(ImageInfo *info)
2420{
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002421 QString *str;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002422 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002423 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002424
2425 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2426 visit_complete(v, &obj);
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002427 str = qobject_to_json_pretty(obj);
2428 assert(str != NULL);
2429 printf("%s\n", qstring_get_str(str));
2430 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002431 visit_free(v);
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002432 QDECREF(str);
2433}
2434
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002435static void dump_human_image_info_list(ImageInfoList *list)
2436{
2437 ImageInfoList *elem;
2438 bool delim = false;
2439
2440 for (elem = list; elem; elem = elem->next) {
2441 if (delim) {
2442 printf("\n");
2443 }
2444 delim = true;
2445
Wenchao Xia5b917042013-05-25 11:09:45 +08002446 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002447 }
2448}
2449
2450static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2451{
2452 return strcmp(a, b) == 0;
2453}
2454
2455/**
2456 * Open an image file chain and return an ImageInfoList
2457 *
2458 * @filename: topmost image filename
2459 * @fmt: topmost image format (may be NULL to autodetect)
2460 * @chain: true - enumerate entire backing file chain
2461 * false - only topmost image file
2462 *
2463 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2464 * image file. If there was an error a message will have been printed to
2465 * stderr.
2466 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002467static ImageInfoList *collect_image_info_list(bool image_opts,
2468 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002469 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002470 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002471{
2472 ImageInfoList *head = NULL;
2473 ImageInfoList **last = &head;
2474 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002475 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002476
2477 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2478
2479 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002480 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002481 BlockDriverState *bs;
2482 ImageInfo *info;
2483 ImageInfoList *elem;
2484
2485 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2486 error_report("Backing file '%s' creates an infinite loop.",
2487 filename);
2488 goto err;
2489 }
2490 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2491
Max Reitzefaa7c42016-03-16 19:54:38 +01002492 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002493 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2494 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002495 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002496 goto err;
2497 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002498 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002499
Wenchao Xia43526ec2013-06-06 12:27:58 +08002500 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002501 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002502 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002503 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002504 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002505 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002506
2507 elem = g_new0(ImageInfoList, 1);
2508 elem->value = info;
2509 *last = elem;
2510 last = &elem->next;
2511
Markus Armbruster26f54e92014-10-07 13:59:04 +02002512 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002513
2514 filename = fmt = NULL;
2515 if (chain) {
2516 if (info->has_full_backing_filename) {
2517 filename = info->full_backing_filename;
2518 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002519 error_report("Could not determine absolute backing filename,"
2520 " but backing filename '%s' present",
2521 info->backing_filename);
2522 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002523 }
2524 if (info->has_backing_filename_format) {
2525 fmt = info->backing_filename_format;
2526 }
2527 }
2528 }
2529 g_hash_table_destroy(filenames);
2530 return head;
2531
2532err:
2533 qapi_free_ImageInfoList(head);
2534 g_hash_table_destroy(filenames);
2535 return NULL;
2536}
2537
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002538static int img_info(int argc, char **argv)
2539{
2540 int c;
2541 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002542 bool chain = false;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002543 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002544 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002545 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002546 bool force_share = false;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002547
bellardea2384d2004-08-01 21:59:26 +00002548 fmt = NULL;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002549 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002550 for(;;) {
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002551 int option_index = 0;
2552 static const struct option long_options[] = {
2553 {"help", no_argument, 0, 'h'},
2554 {"format", required_argument, 0, 'f'},
2555 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002556 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002557 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002558 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002559 {"force-share", no_argument, 0, 'U'},
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002560 {0, 0, 0, 0}
2561 };
Fam Zheng335e9932017-05-03 00:35:39 +08002562 c = getopt_long(argc, argv, ":f:hU",
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002563 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002564 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002565 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002566 }
bellardea2384d2004-08-01 21:59:26 +00002567 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002568 case ':':
2569 missing_argument(argv[optind - 1]);
2570 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002571 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002572 unrecognized_option(argv[optind - 1]);
2573 break;
bellardea2384d2004-08-01 21:59:26 +00002574 case 'h':
2575 help();
2576 break;
2577 case 'f':
2578 fmt = optarg;
2579 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002580 case 'U':
2581 force_share = true;
2582 break;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002583 case OPTION_OUTPUT:
2584 output = optarg;
2585 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002586 case OPTION_BACKING_CHAIN:
2587 chain = true;
2588 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002589 case OPTION_OBJECT: {
2590 QemuOpts *opts;
2591 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2592 optarg, true);
2593 if (!opts) {
2594 return 1;
2595 }
2596 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002597 case OPTION_IMAGE_OPTS:
2598 image_opts = true;
2599 break;
bellardea2384d2004-08-01 21:59:26 +00002600 }
2601 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002602 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002603 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002604 }
bellardea2384d2004-08-01 21:59:26 +00002605 filename = argv[optind++];
2606
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002607 if (output && !strcmp(output, "json")) {
2608 output_format = OFORMAT_JSON;
2609 } else if (output && !strcmp(output, "human")) {
2610 output_format = OFORMAT_HUMAN;
2611 } else if (output) {
2612 error_report("--output must be used with human or json as argument.");
2613 return 1;
2614 }
2615
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002616 if (qemu_opts_foreach(&qemu_object_opts,
2617 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002618 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002619 return 1;
2620 }
2621
Fam Zheng335e9932017-05-03 00:35:39 +08002622 list = collect_image_info_list(image_opts, filename, fmt, chain,
2623 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002624 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002625 return 1;
2626 }
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002627
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002628 switch (output_format) {
2629 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002630 dump_human_image_info_list(list);
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002631 break;
2632 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002633 if (chain) {
2634 dump_json_image_info_list(list);
2635 } else {
2636 dump_json_image_info(list->value);
2637 }
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002638 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002639 }
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002640
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002641 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002642 return 0;
2643}
2644
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002645static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2646 MapEntry *next)
2647{
2648 switch (output_format) {
2649 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002650 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002651 error_report("File contains external, encrypted or compressed clusters.");
2652 exit(1);
2653 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002654 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002655 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002656 e->start, e->length,
2657 e->has_offset ? e->offset : 0,
2658 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002659 }
2660 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2661 * Modify the flags here to allow more coalescing.
2662 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002663 if (next && (!next->data || next->zero)) {
2664 next->data = false;
2665 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002666 }
2667 break;
2668 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002669 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2670 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002671 (e->start == 0 ? "[" : ",\n"),
2672 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002673 e->zero ? "true" : "false",
2674 e->data ? "true" : "false");
2675 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002676 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002677 }
2678 putchar('}');
2679
2680 if (!next) {
2681 printf("]\n");
2682 }
2683 break;
2684 }
2685}
2686
Eric Blake5e344dd2017-10-11 22:47:02 -05002687static int get_block_status(BlockDriverState *bs, int64_t offset,
2688 int64_t bytes, MapEntry *e)
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002689{
Eric Blake237d78f2017-10-11 22:47:03 -05002690 int ret;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002691 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002692 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002693 bool has_offset;
Eric Blake237d78f2017-10-11 22:47:03 -05002694 int64_t map;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002695
2696 /* As an optimization, we could cache the current range of unallocated
2697 * clusters in each file of the chain, and avoid querying the same
2698 * range repeatedly.
2699 */
2700
2701 depth = 0;
2702 for (;;) {
Eric Blake237d78f2017-10-11 22:47:03 -05002703 ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002704 if (ret < 0) {
2705 return ret;
2706 }
Eric Blake237d78f2017-10-11 22:47:03 -05002707 assert(bytes);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002708 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2709 break;
2710 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002711 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002712 if (bs == NULL) {
2713 ret = 0;
2714 break;
2715 }
2716
2717 depth++;
2718 }
2719
John Snow28756452016-02-05 13:12:33 -05002720 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2721
2722 *e = (MapEntry) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002723 .start = offset,
Eric Blake237d78f2017-10-11 22:47:03 -05002724 .length = bytes,
John Snow28756452016-02-05 13:12:33 -05002725 .data = !!(ret & BDRV_BLOCK_DATA),
2726 .zero = !!(ret & BDRV_BLOCK_ZERO),
Eric Blake237d78f2017-10-11 22:47:03 -05002727 .offset = map,
John Snow28756452016-02-05 13:12:33 -05002728 .has_offset = has_offset,
2729 .depth = depth,
2730 .has_filename = file && has_offset,
2731 .filename = file && has_offset ? file->filename : NULL,
2732 };
2733
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002734 return 0;
2735}
2736
Fam Zheng16b0d552016-01-26 11:59:02 +08002737static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2738{
2739 if (curr->length == 0) {
2740 return false;
2741 }
2742 if (curr->zero != next->zero ||
2743 curr->data != next->data ||
2744 curr->depth != next->depth ||
2745 curr->has_filename != next->has_filename ||
2746 curr->has_offset != next->has_offset) {
2747 return false;
2748 }
2749 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2750 return false;
2751 }
2752 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2753 return false;
2754 }
2755 return true;
2756}
2757
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002758static int img_map(int argc, char **argv)
2759{
2760 int c;
2761 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002762 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002763 BlockDriverState *bs;
2764 const char *filename, *fmt, *output;
2765 int64_t length;
2766 MapEntry curr = { .length = 0 }, next;
2767 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002768 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002769 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002770
2771 fmt = NULL;
2772 output = NULL;
2773 for (;;) {
2774 int option_index = 0;
2775 static const struct option long_options[] = {
2776 {"help", no_argument, 0, 'h'},
2777 {"format", required_argument, 0, 'f'},
2778 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002779 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002780 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002781 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002782 {0, 0, 0, 0}
2783 };
Fam Zheng335e9932017-05-03 00:35:39 +08002784 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002785 long_options, &option_index);
2786 if (c == -1) {
2787 break;
2788 }
2789 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002790 case ':':
2791 missing_argument(argv[optind - 1]);
2792 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002793 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002794 unrecognized_option(argv[optind - 1]);
2795 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002796 case 'h':
2797 help();
2798 break;
2799 case 'f':
2800 fmt = optarg;
2801 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002802 case 'U':
2803 force_share = true;
2804 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002805 case OPTION_OUTPUT:
2806 output = optarg;
2807 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002808 case OPTION_OBJECT: {
2809 QemuOpts *opts;
2810 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2811 optarg, true);
2812 if (!opts) {
2813 return 1;
2814 }
2815 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002816 case OPTION_IMAGE_OPTS:
2817 image_opts = true;
2818 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002819 }
2820 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002821 if (optind != argc - 1) {
2822 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002823 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002824 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002825
2826 if (output && !strcmp(output, "json")) {
2827 output_format = OFORMAT_JSON;
2828 } else if (output && !strcmp(output, "human")) {
2829 output_format = OFORMAT_HUMAN;
2830 } else if (output) {
2831 error_report("--output must be used with human or json as argument.");
2832 return 1;
2833 }
2834
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002835 if (qemu_opts_foreach(&qemu_object_opts,
2836 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002837 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002838 return 1;
2839 }
2840
Fam Zheng335e9932017-05-03 00:35:39 +08002841 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002842 if (!blk) {
2843 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002844 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002845 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002846
2847 if (output_format == OFORMAT_HUMAN) {
2848 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2849 }
2850
Max Reitzf1d3cd72015-02-05 13:58:18 -05002851 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002852 while (curr.start + curr.length < length) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002853 int64_t offset = curr.start + curr.length;
2854 int64_t n;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002855
2856 /* Probe up to 1 GiB at a time. */
Eric Blake5e344dd2017-10-11 22:47:02 -05002857 n = QEMU_ALIGN_DOWN(MIN(1 << 30, length - offset), BDRV_SECTOR_SIZE);
2858 ret = get_block_status(bs, offset, n, &next);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002859
2860 if (ret < 0) {
2861 error_report("Could not read file metadata: %s", strerror(-ret));
2862 goto out;
2863 }
2864
Fam Zheng16b0d552016-01-26 11:59:02 +08002865 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002866 curr.length += next.length;
2867 continue;
2868 }
2869
2870 if (curr.length > 0) {
2871 dump_map_entry(output_format, &curr, &next);
2872 }
2873 curr = next;
2874 }
2875
2876 dump_map_entry(output_format, &curr, NULL);
2877
2878out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002879 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002880 return ret < 0;
2881}
2882
aliguorif7b4a942009-01-07 17:40:15 +00002883#define SNAPSHOT_LIST 1
2884#define SNAPSHOT_CREATE 2
2885#define SNAPSHOT_APPLY 3
2886#define SNAPSHOT_DELETE 4
2887
Stuart Brady153859b2009-06-07 00:42:17 +01002888static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002889{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002890 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002891 BlockDriverState *bs;
2892 QEMUSnapshotInfo sn;
2893 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002894 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002895 int action = 0;
2896 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002897 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002898 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002899 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002900 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00002901
Kevin Wolfce099542016-03-15 13:01:04 +01002902 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002903 /* Parse commandline parameters */
2904 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002905 static const struct option long_options[] = {
2906 {"help", no_argument, 0, 'h'},
2907 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002908 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002909 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002910 {0, 0, 0, 0}
2911 };
Fam Zheng335e9932017-05-03 00:35:39 +08002912 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002913 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002914 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002915 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002916 }
aliguorif7b4a942009-01-07 17:40:15 +00002917 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002918 case ':':
2919 missing_argument(argv[optind - 1]);
2920 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002921 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002922 unrecognized_option(argv[optind - 1]);
2923 break;
aliguorif7b4a942009-01-07 17:40:15 +00002924 case 'h':
2925 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002926 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002927 case 'l':
2928 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002929 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002930 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002931 }
2932 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002933 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002934 break;
2935 case 'a':
2936 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002937 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002938 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002939 }
2940 action = SNAPSHOT_APPLY;
2941 snapshot_name = optarg;
2942 break;
2943 case 'c':
2944 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002945 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002946 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002947 }
2948 action = SNAPSHOT_CREATE;
2949 snapshot_name = optarg;
2950 break;
2951 case 'd':
2952 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002953 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002954 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002955 }
2956 action = SNAPSHOT_DELETE;
2957 snapshot_name = optarg;
2958 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002959 case 'q':
2960 quiet = true;
2961 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002962 case 'U':
2963 force_share = true;
2964 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002965 case OPTION_OBJECT: {
2966 QemuOpts *opts;
2967 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2968 optarg, true);
2969 if (!opts) {
2970 return 1;
2971 }
2972 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002973 case OPTION_IMAGE_OPTS:
2974 image_opts = true;
2975 break;
aliguorif7b4a942009-01-07 17:40:15 +00002976 }
2977 }
2978
Kevin Wolffc11eb22013-08-05 10:53:04 +02002979 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002980 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002981 }
aliguorif7b4a942009-01-07 17:40:15 +00002982 filename = argv[optind++];
2983
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002984 if (qemu_opts_foreach(&qemu_object_opts,
2985 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002986 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002987 return 1;
2988 }
2989
aliguorif7b4a942009-01-07 17:40:15 +00002990 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08002991 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
2992 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002993 if (!blk) {
2994 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002995 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002996 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002997
2998 /* Perform the requested action */
2999 switch(action) {
3000 case SNAPSHOT_LIST:
3001 dump_snapshots(bs);
3002 break;
3003
3004 case SNAPSHOT_CREATE:
3005 memset(&sn, 0, sizeof(sn));
3006 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
3007
3008 qemu_gettimeofday(&tv);
3009 sn.date_sec = tv.tv_sec;
3010 sn.date_nsec = tv.tv_usec * 1000;
3011
3012 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003013 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003014 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003015 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003016 }
aliguorif7b4a942009-01-07 17:40:15 +00003017 break;
3018
3019 case SNAPSHOT_APPLY:
3020 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003021 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003022 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003023 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003024 }
aliguorif7b4a942009-01-07 17:40:15 +00003025 break;
3026
3027 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003028 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003029 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003030 error_reportf_err(err, "Could not delete snapshot '%s': ",
3031 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003032 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003033 }
aliguorif7b4a942009-01-07 17:40:15 +00003034 break;
3035 }
3036
3037 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003038 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003039 if (ret) {
3040 return 1;
3041 }
Stuart Brady153859b2009-06-07 00:42:17 +01003042 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003043}
3044
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003045static int img_rebase(int argc, char **argv)
3046{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003047 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003048 uint8_t *buf_old = NULL;
3049 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05003050 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003051 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003052 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3053 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003054 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003055 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003056 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003057 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003058 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003059 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003060 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003061
3062 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003063 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003064 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003065 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003066 out_baseimg = NULL;
3067 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003068 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003069 static const struct option long_options[] = {
3070 {"help", no_argument, 0, 'h'},
3071 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003072 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003073 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003074 {0, 0, 0, 0}
3075 };
Fam Zheng335e9932017-05-03 00:35:39 +08003076 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003077 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003078 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003079 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003080 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003081 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003082 case ':':
3083 missing_argument(argv[optind - 1]);
3084 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003085 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003086 unrecognized_option(argv[optind - 1]);
3087 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003088 case 'h':
3089 help();
3090 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003091 case 'f':
3092 fmt = optarg;
3093 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003094 case 'F':
3095 out_basefmt = optarg;
3096 break;
3097 case 'b':
3098 out_baseimg = optarg;
3099 break;
3100 case 'u':
3101 unsafe = 1;
3102 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003103 case 'p':
3104 progress = 1;
3105 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003106 case 't':
3107 cache = optarg;
3108 break;
Max Reitz40055952014-07-22 22:58:42 +02003109 case 'T':
3110 src_cache = optarg;
3111 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003112 case 'q':
3113 quiet = true;
3114 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003115 case OPTION_OBJECT: {
3116 QemuOpts *opts;
3117 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3118 optarg, true);
3119 if (!opts) {
3120 return 1;
3121 }
3122 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003123 case OPTION_IMAGE_OPTS:
3124 image_opts = true;
3125 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003126 case 'U':
3127 force_share = true;
3128 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003129 }
3130 }
3131
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003132 if (quiet) {
3133 progress = 0;
3134 }
3135
Fam Zhengac1307a2014-04-22 13:36:11 +08003136 if (optind != argc - 1) {
3137 error_exit("Expecting one image file name");
3138 }
3139 if (!unsafe && !out_baseimg) {
3140 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003141 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003142 filename = argv[optind++];
3143
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003144 if (qemu_opts_foreach(&qemu_object_opts,
3145 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003146 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003147 return 1;
3148 }
3149
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003150 qemu_progress_init(progress, 2.0);
3151 qemu_progress_print(0, 100);
3152
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003153 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003154 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003155 if (ret < 0) {
3156 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003157 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003158 }
3159
Kevin Wolfce099542016-03-15 13:01:04 +01003160 src_flags = 0;
3161 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003162 if (ret < 0) {
3163 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003164 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003165 }
3166
Kevin Wolfce099542016-03-15 13:01:04 +01003167 /* The source files are opened read-only, don't care about WCE */
3168 assert((src_flags & BDRV_O_RDWR) == 0);
3169 (void) src_writethrough;
3170
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003171 /*
3172 * Open the images.
3173 *
3174 * Ignore the old backing file for unsafe rebase in case we want to correct
3175 * the reference to a renamed or moved backing file.
3176 */
Fam Zheng335e9932017-05-03 00:35:39 +08003177 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3178 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003179 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003180 ret = -1;
3181 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003182 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003183 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003184
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003185 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003186 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003187 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003188 ret = -1;
3189 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003190 }
3191 }
3192
3193 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003194 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003195 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003196 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003197
Max Reitz644483d2015-02-05 13:58:17 -05003198 if (bs->backing_format[0] != '\0') {
3199 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003200 qdict_put_str(options, "driver", bs->backing_format);
Max Reitz644483d2015-02-05 13:58:17 -05003201 }
3202
Fam Zheng335e9932017-05-03 00:35:39 +08003203 if (force_share) {
3204 if (!options) {
3205 options = qdict_new();
3206 }
Eric Blake579cf1d2017-05-15 14:54:39 -05003207 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +08003208 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003209 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003210 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003211 options, src_flags, &local_err);
3212 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003213 error_reportf_err(local_err,
3214 "Could not open old backing file '%s': ",
3215 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003216 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003217 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003218 }
Max Reitz644483d2015-02-05 13:58:17 -05003219
Alex Bligha6166732012-10-16 13:46:18 +01003220 if (out_baseimg[0]) {
Fam Zheng335e9932017-05-03 00:35:39 +08003221 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003222 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003223 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003224 }
3225 if (force_share) {
3226 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003227 }
3228
Max Reitzefaa7c42016-03-16 19:54:38 +01003229 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003230 options, src_flags, &local_err);
3231 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003232 error_reportf_err(local_err,
3233 "Could not open new backing file '%s': ",
3234 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003235 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003236 goto out;
3237 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003238 }
3239 }
3240
3241 /*
3242 * Check each unallocated cluster in the COW file. If it is unallocated,
3243 * accesses go to the backing file. We must therefore compare this cluster
3244 * in the old and new backing file, and if they differ we need to copy it
3245 * from the old backing file into the COW file.
3246 *
3247 * If qemu-img crashes during this step, no harm is done. The content of
3248 * the image is the same as the original one at any time.
3249 */
3250 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003251 int64_t num_sectors;
3252 int64_t old_backing_num_sectors;
3253 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003254 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003255 int n;
Eric Blaked6a644b2017-07-07 07:44:57 -05003256 int64_t count;
Kevin Wolf1f710492012-10-12 14:29:18 +02003257 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003258
Max Reitzf1d3cd72015-02-05 13:58:18 -05003259 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3260 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003261
Max Reitzf1d3cd72015-02-05 13:58:18 -05003262 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003263 if (num_sectors < 0) {
3264 error_report("Could not get size of '%s': %s",
3265 filename, strerror(-num_sectors));
3266 ret = -1;
3267 goto out;
3268 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003269 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003270 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003271 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003272
3273 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3274 error_report("Could not get size of '%s': %s",
3275 backing_name, strerror(-old_backing_num_sectors));
3276 ret = -1;
3277 goto out;
3278 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003279 if (blk_new_backing) {
3280 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003281 if (new_backing_num_sectors < 0) {
3282 error_report("Could not get size of '%s': %s",
3283 out_baseimg, strerror(-new_backing_num_sectors));
3284 ret = -1;
3285 goto out;
3286 }
Alex Bligha6166732012-10-16 13:46:18 +01003287 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003288
Kevin Wolf1f710492012-10-12 14:29:18 +02003289 if (num_sectors != 0) {
3290 local_progress = (float)100 /
3291 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
3292 }
3293
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003294 for (sector = 0; sector < num_sectors; sector += n) {
3295
3296 /* How many sectors can we handle with the next read? */
3297 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
3298 n = (IO_BUF_SIZE / 512);
3299 } else {
3300 n = num_sectors - sector;
3301 }
3302
3303 /* If the cluster is allocated, we don't need to take action */
Eric Blaked6a644b2017-07-07 07:44:57 -05003304 ret = bdrv_is_allocated(bs, sector << BDRV_SECTOR_BITS,
3305 n << BDRV_SECTOR_BITS, &count);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003306 if (ret < 0) {
3307 error_report("error while reading image metadata: %s",
3308 strerror(-ret));
3309 goto out;
3310 }
Eric Blaked6a644b2017-07-07 07:44:57 -05003311 /* TODO relax this once bdrv_is_allocated does not enforce
3312 * sector alignment */
3313 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
3314 n = count >> BDRV_SECTOR_BITS;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003315 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003316 continue;
3317 }
3318
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003319 /*
3320 * Read old and new backing file and take into consideration that
3321 * backing files may be smaller than the COW image.
3322 */
3323 if (sector >= old_backing_num_sectors) {
3324 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3325 } else {
3326 if (sector + n > old_backing_num_sectors) {
3327 n = old_backing_num_sectors - sector;
3328 }
3329
Eric Blake91669202016-05-06 10:26:43 -06003330 ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS,
3331 buf_old, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003332 if (ret < 0) {
3333 error_report("error while reading from old backing file");
3334 goto out;
3335 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003336 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003337
Max Reitzf1d3cd72015-02-05 13:58:18 -05003338 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003339 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3340 } else {
3341 if (sector + n > new_backing_num_sectors) {
3342 n = new_backing_num_sectors - sector;
3343 }
3344
Eric Blake91669202016-05-06 10:26:43 -06003345 ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS,
3346 buf_new, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003347 if (ret < 0) {
3348 error_report("error while reading from new backing file");
3349 goto out;
3350 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003351 }
3352
3353 /* If they differ, we need to write to the COW file */
3354 uint64_t written = 0;
3355
Eric Blakedc61cd32017-10-11 22:47:14 -05003356 while (written < n * BDRV_SECTOR_SIZE) {
3357 int64_t pnum;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003358
Eric Blakedc61cd32017-10-11 22:47:14 -05003359 if (compare_buffers(buf_old + written,
3360 buf_new + written,
3361 n * BDRV_SECTOR_SIZE - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003362 {
Eric Blake91669202016-05-06 10:26:43 -06003363 ret = blk_pwrite(blk,
Eric Blakedc61cd32017-10-11 22:47:14 -05003364 (sector << BDRV_SECTOR_BITS) + written,
3365 buf_old + written, pnum, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003366 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003367 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003368 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003369 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003370 }
3371 }
3372
3373 written += pnum;
3374 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003375 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003376 }
3377 }
3378
3379 /*
3380 * Change the backing file. All clusters that are different from the old
3381 * backing file are overwritten in the COW file now, so the visible content
3382 * doesn't change when we switch the backing file.
3383 */
Alex Bligha6166732012-10-16 13:46:18 +01003384 if (out_baseimg && *out_baseimg) {
3385 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3386 } else {
3387 ret = bdrv_change_backing_file(bs, NULL, NULL);
3388 }
3389
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003390 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003391 error_report("Could not change the backing file to '%s': No "
3392 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003393 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003394 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003395 out_baseimg, strerror(-ret));
3396 }
3397
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003398 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003399 /*
3400 * TODO At this point it is possible to check if any clusters that are
3401 * allocated in the COW file are the same in the backing file. If so, they
3402 * could be dropped from the COW file. Don't do this before switching the
3403 * backing file, in case of a crash this would lead to corruption.
3404 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003405out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003406 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003407 /* Cleanup */
3408 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003409 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003410 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003411 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003412 qemu_vfree(buf_old);
3413 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003414
Markus Armbruster26f54e92014-10-07 13:59:04 +02003415 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003416 if (ret) {
3417 return 1;
3418 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003419 return 0;
3420}
3421
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003422static int img_resize(int argc, char **argv)
3423{
Markus Armbruster6750e792015-02-12 17:43:08 +01003424 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003425 int c, ret, relative;
3426 const char *filename, *fmt, *size;
Max Reitzdc5f6902017-06-13 22:20:55 +02003427 int64_t n, total_size, current_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003428 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003429 BlockBackend *blk = NULL;
Max Reitzdc5f6902017-06-13 22:20:55 +02003430 PreallocMode prealloc = PREALLOC_MODE_OFF;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003431 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003432
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003433 static QemuOptsList resize_options = {
3434 .name = "resize_options",
3435 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3436 .desc = {
3437 {
3438 .name = BLOCK_OPT_SIZE,
3439 .type = QEMU_OPT_SIZE,
3440 .help = "Virtual disk size"
3441 }, {
3442 /* end of list */
3443 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003444 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003445 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003446 bool image_opts = false;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003447 bool shrink = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003448
Kevin Wolfe80fec72011-04-29 10:58:12 +02003449 /* Remove size from argv manually so that negative numbers are not treated
3450 * as options by getopt. */
3451 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003452 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003453 return 1;
3454 }
3455
3456 size = argv[--argc];
3457
3458 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003459 fmt = NULL;
3460 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003461 static const struct option long_options[] = {
3462 {"help", no_argument, 0, 'h'},
3463 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003464 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Max Reitzdc5f6902017-06-13 22:20:55 +02003465 {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003466 {"shrink", no_argument, 0, OPTION_SHRINK},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003467 {0, 0, 0, 0}
3468 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003469 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003470 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003471 if (c == -1) {
3472 break;
3473 }
3474 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003475 case ':':
3476 missing_argument(argv[optind - 1]);
3477 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003478 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003479 unrecognized_option(argv[optind - 1]);
3480 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003481 case 'h':
3482 help();
3483 break;
3484 case 'f':
3485 fmt = optarg;
3486 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003487 case 'q':
3488 quiet = true;
3489 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003490 case OPTION_OBJECT: {
3491 QemuOpts *opts;
3492 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3493 optarg, true);
3494 if (!opts) {
3495 return 1;
3496 }
3497 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003498 case OPTION_IMAGE_OPTS:
3499 image_opts = true;
3500 break;
Max Reitzdc5f6902017-06-13 22:20:55 +02003501 case OPTION_PREALLOCATION:
Marc-AndrƩ Lureauf7abe0e2017-08-24 10:46:10 +02003502 prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
Markus Armbruster06c60b62017-08-24 10:45:57 +02003503 PREALLOC_MODE__MAX, NULL);
Max Reitzdc5f6902017-06-13 22:20:55 +02003504 if (prealloc == PREALLOC_MODE__MAX) {
3505 error_report("Invalid preallocation mode '%s'", optarg);
3506 return 1;
3507 }
3508 break;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003509 case OPTION_SHRINK:
3510 shrink = true;
3511 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003512 }
3513 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003514 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003515 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003516 }
3517 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003518
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003519 if (qemu_opts_foreach(&qemu_object_opts,
3520 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003521 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003522 return 1;
3523 }
3524
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003525 /* Choose grow, shrink, or absolute resize mode */
3526 switch (size[0]) {
3527 case '+':
3528 relative = 1;
3529 size++;
3530 break;
3531 case '-':
3532 relative = -1;
3533 size++;
3534 break;
3535 default:
3536 relative = 0;
3537 break;
3538 }
3539
3540 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003541 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003542 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003543 if (err) {
3544 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003545 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003546 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003547 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003548 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003549 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3550 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003551
Max Reitzefaa7c42016-03-16 19:54:38 +01003552 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003553 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3554 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003555 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003556 ret = -1;
3557 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003558 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003559
Max Reitzdc5f6902017-06-13 22:20:55 +02003560 current_size = blk_getlength(blk);
3561 if (current_size < 0) {
3562 error_report("Failed to inquire current image length: %s",
3563 strerror(-current_size));
3564 ret = -1;
3565 goto out;
3566 }
3567
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003568 if (relative) {
Max Reitzdc5f6902017-06-13 22:20:55 +02003569 total_size = current_size + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003570 } else {
3571 total_size = n;
3572 }
3573 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003574 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003575 ret = -1;
3576 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003577 }
3578
Max Reitzdc5f6902017-06-13 22:20:55 +02003579 if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
3580 error_report("Preallocation can only be used for growing images");
3581 ret = -1;
3582 goto out;
3583 }
3584
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003585 if (total_size < current_size && !shrink) {
3586 warn_report("Shrinking an image will delete all data beyond the "
3587 "shrunken image's end. Before performing such an "
3588 "operation, make sure there is no important data there.");
3589
3590 if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) {
3591 error_report(
3592 "Use the --shrink option to perform a shrink operation.");
3593 ret = -1;
3594 goto out;
3595 } else {
3596 warn_report("Using the --shrink option will suppress this message. "
3597 "Note that future versions of qemu-img may refuse to "
3598 "shrink images without this option.");
3599 }
3600 }
3601
Max Reitzdc5f6902017-06-13 22:20:55 +02003602 ret = blk_truncate(blk, total_size, prealloc, &err);
Max Reitzed3d2ec2017-03-28 22:51:27 +02003603 if (!ret) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003604 qprintf(quiet, "Image resized.\n");
Max Reitzed3d2ec2017-03-28 22:51:27 +02003605 } else {
3606 error_report_err(err);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003607 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003608out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003609 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003610 if (ret) {
3611 return 1;
3612 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003613 return 0;
3614}
3615
Max Reitz76a3a342014-10-27 11:12:51 +01003616static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003617 int64_t offset, int64_t total_work_size,
3618 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003619{
3620 qemu_progress_print(100.f * offset / total_work_size, 0);
3621}
3622
Max Reitz6f176b42013-09-03 10:09:50 +02003623static int img_amend(int argc, char **argv)
3624{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003625 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003626 int c, ret = 0;
3627 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003628 QemuOptsList *create_opts = NULL;
3629 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003630 const char *fmt = NULL, *filename, *cache;
3631 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003632 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003633 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003634 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003635 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003636 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003637
Max Reitzbd39e6e2014-07-22 22:58:43 +02003638 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003639 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003640 static const struct option long_options[] = {
3641 {"help", no_argument, 0, 'h'},
3642 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003643 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003644 {0, 0, 0, 0}
3645 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003646 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003647 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003648 if (c == -1) {
3649 break;
3650 }
3651
3652 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003653 case ':':
3654 missing_argument(argv[optind - 1]);
3655 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003656 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003657 unrecognized_option(argv[optind - 1]);
3658 break;
3659 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003660 help();
3661 break;
3662 case 'o':
3663 if (!is_valid_option_list(optarg)) {
3664 error_report("Invalid option list: %s", optarg);
3665 ret = -1;
3666 goto out_no_progress;
3667 }
3668 if (!options) {
3669 options = g_strdup(optarg);
3670 } else {
3671 char *old_options = options;
3672 options = g_strdup_printf("%s,%s", options, optarg);
3673 g_free(old_options);
3674 }
3675 break;
3676 case 'f':
3677 fmt = optarg;
3678 break;
3679 case 't':
3680 cache = optarg;
3681 break;
3682 case 'p':
3683 progress = true;
3684 break;
3685 case 'q':
3686 quiet = true;
3687 break;
3688 case OPTION_OBJECT:
3689 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3690 optarg, true);
3691 if (!opts) {
3692 ret = -1;
3693 goto out_no_progress;
3694 }
3695 break;
3696 case OPTION_IMAGE_OPTS:
3697 image_opts = true;
3698 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003699 }
3700 }
3701
Max Reitz6f176b42013-09-03 10:09:50 +02003702 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003703 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003704 }
3705
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003706 if (qemu_opts_foreach(&qemu_object_opts,
3707 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003708 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003709 ret = -1;
3710 goto out_no_progress;
3711 }
3712
Max Reitz76a3a342014-10-27 11:12:51 +01003713 if (quiet) {
3714 progress = false;
3715 }
3716 qemu_progress_init(progress, 1.0);
3717
Kevin Wolfa283cb62014-02-21 16:24:07 +01003718 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3719 if (fmt && has_help_option(options)) {
3720 /* If a format is explicitly specified (and possibly no filename is
3721 * given), print option help here */
3722 ret = print_block_option_help(filename, fmt);
3723 goto out;
3724 }
3725
3726 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003727 error_report("Expecting one image file name");
3728 ret = -1;
3729 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003730 }
Max Reitz6f176b42013-09-03 10:09:50 +02003731
Kevin Wolfce099542016-03-15 13:01:04 +01003732 flags = BDRV_O_RDWR;
3733 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003734 if (ret < 0) {
3735 error_report("Invalid cache option: %s", cache);
3736 goto out;
3737 }
3738
Fam Zheng335e9932017-05-03 00:35:39 +08003739 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3740 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003741 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003742 ret = -1;
3743 goto out;
3744 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003745 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003746
3747 fmt = bs->drv->format_name;
3748
Kevin Wolf626f84f2014-02-21 16:24:06 +01003749 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003750 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003751 ret = print_block_option_help(filename, fmt);
3752 goto out;
3753 }
3754
Max Reitzb2439d22014-12-02 18:32:47 +01003755 if (!bs->drv->create_opts) {
3756 error_report("Format driver '%s' does not support any options to amend",
3757 fmt);
3758 ret = -1;
3759 goto out;
3760 }
3761
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003762 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003763 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003764 qemu_opts_do_parse(opts, options, NULL, &err);
3765 if (err) {
3766 error_report_err(err);
3767 ret = -1;
3768 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003769 }
3770
Max Reitz76a3a342014-10-27 11:12:51 +01003771 /* In case the driver does not call amend_status_cb() */
3772 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003773 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003774 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003775 if (ret < 0) {
3776 error_report("Error while amending options: %s", strerror(-ret));
3777 goto out;
3778 }
3779
3780out:
Max Reitz76a3a342014-10-27 11:12:51 +01003781 qemu_progress_end();
3782
Max Reitze814dff2015-08-20 16:00:38 -07003783out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003784 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003785 qemu_opts_del(opts);
3786 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003787 g_free(options);
3788
Max Reitz6f176b42013-09-03 10:09:50 +02003789 if (ret) {
3790 return 1;
3791 }
3792 return 0;
3793}
3794
Kevin Wolfb6133b82014-08-05 14:17:13 +02003795typedef struct BenchData {
3796 BlockBackend *blk;
3797 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003798 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003799 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003800 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003801 int nrreq;
3802 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003803 int flush_interval;
3804 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003805 uint8_t *buf;
3806 QEMUIOVector *qiov;
3807
3808 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003809 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003810 uint64_t offset;
3811} BenchData;
3812
Kevin Wolf55d539c2016-06-03 13:59:41 +02003813static void bench_undrained_flush_cb(void *opaque, int ret)
3814{
3815 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003816 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003817 exit(EXIT_FAILURE);
3818 }
3819}
3820
Kevin Wolfb6133b82014-08-05 14:17:13 +02003821static void bench_cb(void *opaque, int ret)
3822{
3823 BenchData *b = opaque;
3824 BlockAIOCB *acb;
3825
3826 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003827 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003828 exit(EXIT_FAILURE);
3829 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003830
3831 if (b->in_flush) {
3832 /* Just finished a flush with drained queue: Start next requests */
3833 assert(b->in_flight == 0);
3834 b->in_flush = false;
3835 } else if (b->in_flight > 0) {
3836 int remaining = b->n - b->in_flight;
3837
Kevin Wolfb6133b82014-08-05 14:17:13 +02003838 b->n--;
3839 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003840
3841 /* Time for flush? Drain queue if requested, then flush */
3842 if (b->flush_interval && remaining % b->flush_interval == 0) {
3843 if (!b->in_flight || !b->drain_on_flush) {
3844 BlockCompletionFunc *cb;
3845
3846 if (b->drain_on_flush) {
3847 b->in_flush = true;
3848 cb = bench_cb;
3849 } else {
3850 cb = bench_undrained_flush_cb;
3851 }
3852
3853 acb = blk_aio_flush(b->blk, cb, b);
3854 if (!acb) {
3855 error_report("Failed to issue flush request");
3856 exit(EXIT_FAILURE);
3857 }
3858 }
3859 if (b->drain_on_flush) {
3860 return;
3861 }
3862 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003863 }
3864
3865 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003866 int64_t offset = b->offset;
3867 /* blk_aio_* might look for completed I/Os and kick bench_cb
3868 * again, so make sure this operation is counted by in_flight
3869 * and b->offset is ready for the next submission.
3870 */
3871 b->in_flight++;
3872 b->offset += b->step;
3873 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003874 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003875 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003876 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003877 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003878 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003879 if (!acb) {
3880 error_report("Failed to issue request");
3881 exit(EXIT_FAILURE);
3882 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003883 }
3884}
3885
3886static int img_bench(int argc, char **argv)
3887{
3888 int c, ret = 0;
3889 const char *fmt = NULL, *filename;
3890 bool quiet = false;
3891 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003892 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003893 int count = 75000;
3894 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003895 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003896 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003897 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003898 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003899 int flush_interval = 0;
3900 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003901 int64_t image_size;
3902 BlockBackend *blk = NULL;
3903 BenchData data = {};
3904 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003905 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003906 struct timeval t1, t2;
3907 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08003908 bool force_share = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003909
3910 for (;;) {
3911 static const struct option long_options[] = {
3912 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003913 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003914 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003915 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003916 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08003917 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003918 {0, 0, 0, 0}
3919 };
Fam Zheng335e9932017-05-03 00:35:39 +08003920 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003921 if (c == -1) {
3922 break;
3923 }
3924
3925 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003926 case ':':
3927 missing_argument(argv[optind - 1]);
3928 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003929 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003930 unrecognized_option(argv[optind - 1]);
3931 break;
3932 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02003933 help();
3934 break;
3935 case 'c':
3936 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003937 unsigned long res;
3938
3939 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003940 error_report("Invalid request count specified");
3941 return 1;
3942 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003943 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003944 break;
3945 }
3946 case 'd':
3947 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003948 unsigned long res;
3949
3950 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003951 error_report("Invalid queue depth specified");
3952 return 1;
3953 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003954 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003955 break;
3956 }
3957 case 'f':
3958 fmt = optarg;
3959 break;
3960 case 'n':
3961 flags |= BDRV_O_NATIVE_AIO;
3962 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003963 case 'o':
3964 {
Markus Armbruster606caa02017-02-21 21:14:04 +01003965 offset = cvtnum(optarg);
3966 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02003967 error_report("Invalid offset specified");
3968 return 1;
3969 }
3970 break;
3971 }
3972 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003973 case 'q':
3974 quiet = true;
3975 break;
3976 case 's':
3977 {
3978 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003979
Markus Armbruster606caa02017-02-21 21:14:04 +01003980 sval = cvtnum(optarg);
3981 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003982 error_report("Invalid buffer size specified");
3983 return 1;
3984 }
3985
3986 bufsize = sval;
3987 break;
3988 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003989 case 'S':
3990 {
3991 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003992
Markus Armbruster606caa02017-02-21 21:14:04 +01003993 sval = cvtnum(optarg);
3994 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02003995 error_report("Invalid step size specified");
3996 return 1;
3997 }
3998
3999 step = sval;
4000 break;
4001 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004002 case 't':
4003 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
4004 if (ret < 0) {
4005 error_report("Invalid cache mode");
4006 ret = -1;
4007 goto out;
4008 }
4009 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004010 case 'w':
4011 flags |= BDRV_O_RDWR;
4012 is_write = true;
4013 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004014 case 'U':
4015 force_share = true;
4016 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004017 case OPTION_PATTERN:
4018 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004019 unsigned long res;
4020
4021 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004022 error_report("Invalid pattern byte specified");
4023 return 1;
4024 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004025 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004026 break;
4027 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02004028 case OPTION_FLUSH_INTERVAL:
4029 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004030 unsigned long res;
4031
4032 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004033 error_report("Invalid flush interval specified");
4034 return 1;
4035 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004036 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004037 break;
4038 }
4039 case OPTION_NO_DRAIN:
4040 drain_on_flush = false;
4041 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004042 case OPTION_IMAGE_OPTS:
4043 image_opts = true;
4044 break;
4045 }
4046 }
4047
4048 if (optind != argc - 1) {
4049 error_exit("Expecting one image file name");
4050 }
4051 filename = argv[argc - 1];
4052
Kevin Wolf55d539c2016-06-03 13:59:41 +02004053 if (!is_write && flush_interval) {
4054 error_report("--flush-interval is only available in write tests");
4055 ret = -1;
4056 goto out;
4057 }
4058 if (flush_interval && flush_interval < depth) {
4059 error_report("Flush interval can't be smaller than depth");
4060 ret = -1;
4061 goto out;
4062 }
4063
Fam Zheng335e9932017-05-03 00:35:39 +08004064 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4065 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004066 if (!blk) {
4067 ret = -1;
4068 goto out;
4069 }
4070
4071 image_size = blk_getlength(blk);
4072 if (image_size < 0) {
4073 ret = image_size;
4074 goto out;
4075 }
4076
4077 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004078 .blk = blk,
4079 .image_size = image_size,
4080 .bufsize = bufsize,
4081 .step = step ?: bufsize,
4082 .nrreq = depth,
4083 .n = count,
4084 .offset = offset,
4085 .write = is_write,
4086 .flush_interval = flush_interval,
4087 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004088 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004089 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004090 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004091 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004092 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004093 if (flush_interval) {
4094 printf("Sending flush every %d requests\n", flush_interval);
4095 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004096
4097 data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004098 memset(data.buf, pattern, data.nrreq * data.bufsize);
4099
Kevin Wolfb6133b82014-08-05 14:17:13 +02004100 data.qiov = g_new(QEMUIOVector, data.nrreq);
4101 for (i = 0; i < data.nrreq; i++) {
4102 qemu_iovec_init(&data.qiov[i], 1);
4103 qemu_iovec_add(&data.qiov[i],
4104 data.buf + i * data.bufsize, data.bufsize);
4105 }
4106
4107 gettimeofday(&t1, NULL);
4108 bench_cb(&data, 0);
4109
4110 while (data.n > 0) {
4111 main_loop_wait(false);
4112 }
4113 gettimeofday(&t2, NULL);
4114
4115 printf("Run completed in %3.3f seconds.\n",
4116 (t2.tv_sec - t1.tv_sec)
4117 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4118
4119out:
4120 qemu_vfree(data.buf);
4121 blk_unref(blk);
4122
4123 if (ret) {
4124 return 1;
4125 }
4126 return 0;
4127}
4128
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004129#define C_BS 01
4130#define C_COUNT 02
4131#define C_IF 04
4132#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004133#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004134
4135struct DdInfo {
4136 unsigned int flags;
4137 int64_t count;
4138};
4139
4140struct DdIo {
4141 int bsz; /* Block size */
4142 char *filename;
4143 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004144 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004145};
4146
4147struct DdOpts {
4148 const char *name;
4149 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4150 unsigned int flag;
4151};
4152
4153static int img_dd_bs(const char *arg,
4154 struct DdIo *in, struct DdIo *out,
4155 struct DdInfo *dd)
4156{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004157 int64_t res;
4158
Markus Armbruster606caa02017-02-21 21:14:04 +01004159 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004160
Markus Armbruster606caa02017-02-21 21:14:04 +01004161 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004162 error_report("invalid number: '%s'", arg);
4163 return 1;
4164 }
4165 in->bsz = out->bsz = res;
4166
4167 return 0;
4168}
4169
4170static int img_dd_count(const char *arg,
4171 struct DdIo *in, struct DdIo *out,
4172 struct DdInfo *dd)
4173{
Markus Armbruster606caa02017-02-21 21:14:04 +01004174 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004175
Markus Armbruster606caa02017-02-21 21:14:04 +01004176 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004177 error_report("invalid number: '%s'", arg);
4178 return 1;
4179 }
4180
4181 return 0;
4182}
4183
4184static int img_dd_if(const char *arg,
4185 struct DdIo *in, struct DdIo *out,
4186 struct DdInfo *dd)
4187{
4188 in->filename = g_strdup(arg);
4189
4190 return 0;
4191}
4192
4193static int img_dd_of(const char *arg,
4194 struct DdIo *in, struct DdIo *out,
4195 struct DdInfo *dd)
4196{
4197 out->filename = g_strdup(arg);
4198
4199 return 0;
4200}
4201
Reda Sallahif7c15532016-08-10 16:16:09 +02004202static int img_dd_skip(const char *arg,
4203 struct DdIo *in, struct DdIo *out,
4204 struct DdInfo *dd)
4205{
Markus Armbruster606caa02017-02-21 21:14:04 +01004206 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004207
Markus Armbruster606caa02017-02-21 21:14:04 +01004208 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004209 error_report("invalid number: '%s'", arg);
4210 return 1;
4211 }
4212
4213 return 0;
4214}
4215
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004216static int img_dd(int argc, char **argv)
4217{
4218 int ret = 0;
4219 char *arg = NULL;
4220 char *tmp;
4221 BlockDriver *drv = NULL, *proto_drv = NULL;
4222 BlockBackend *blk1 = NULL, *blk2 = NULL;
4223 QemuOpts *opts = NULL;
4224 QemuOptsList *create_opts = NULL;
4225 Error *local_err = NULL;
4226 bool image_opts = false;
4227 int c, i;
4228 const char *out_fmt = "raw";
4229 const char *fmt = NULL;
4230 int64_t size = 0;
4231 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004232 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004233 struct DdInfo dd = {
4234 .flags = 0,
4235 .count = 0,
4236 };
4237 struct DdIo in = {
4238 .bsz = 512, /* Block size is by default 512 bytes */
4239 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004240 .buf = NULL,
4241 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004242 };
4243 struct DdIo out = {
4244 .bsz = 512,
4245 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004246 .buf = NULL,
4247 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004248 };
4249
4250 const struct DdOpts options[] = {
4251 { "bs", img_dd_bs, C_BS },
4252 { "count", img_dd_count, C_COUNT },
4253 { "if", img_dd_if, C_IF },
4254 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004255 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004256 { NULL, NULL, 0 }
4257 };
4258 const struct option long_options[] = {
4259 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004260 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004261 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004262 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004263 { 0, 0, 0, 0 }
4264 };
4265
Fam Zheng335e9932017-05-03 00:35:39 +08004266 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004267 if (c == EOF) {
4268 break;
4269 }
4270 switch (c) {
4271 case 'O':
4272 out_fmt = optarg;
4273 break;
4274 case 'f':
4275 fmt = optarg;
4276 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004277 case ':':
4278 missing_argument(argv[optind - 1]);
4279 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004280 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004281 unrecognized_option(argv[optind - 1]);
4282 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004283 case 'h':
4284 help();
4285 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004286 case 'U':
4287 force_share = true;
4288 break;
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004289 case OPTION_OBJECT:
4290 if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004291 ret = -1;
4292 goto out;
4293 }
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004294 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004295 case OPTION_IMAGE_OPTS:
4296 image_opts = true;
4297 break;
4298 }
4299 }
4300
4301 for (i = optind; i < argc; i++) {
4302 int j;
4303 arg = g_strdup(argv[i]);
4304
4305 tmp = strchr(arg, '=');
4306 if (tmp == NULL) {
4307 error_report("unrecognized operand %s", arg);
4308 ret = -1;
4309 goto out;
4310 }
4311
4312 *tmp++ = '\0';
4313
4314 for (j = 0; options[j].name != NULL; j++) {
4315 if (!strcmp(arg, options[j].name)) {
4316 break;
4317 }
4318 }
4319 if (options[j].name == NULL) {
4320 error_report("unrecognized operand %s", arg);
4321 ret = -1;
4322 goto out;
4323 }
4324
4325 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4326 ret = -1;
4327 goto out;
4328 }
4329 dd.flags |= options[j].flag;
4330 g_free(arg);
4331 arg = NULL;
4332 }
4333
4334 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4335 error_report("Must specify both input and output files");
4336 ret = -1;
4337 goto out;
4338 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004339
4340 if (qemu_opts_foreach(&qemu_object_opts,
4341 user_creatable_add_opts_foreach,
4342 NULL, NULL)) {
4343 ret = -1;
4344 goto out;
4345 }
4346
Fam Zheng335e9932017-05-03 00:35:39 +08004347 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4348 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004349
4350 if (!blk1) {
4351 ret = -1;
4352 goto out;
4353 }
4354
4355 drv = bdrv_find_format(out_fmt);
4356 if (!drv) {
4357 error_report("Unknown file format");
4358 ret = -1;
4359 goto out;
4360 }
4361 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4362
4363 if (!proto_drv) {
4364 error_report_err(local_err);
4365 ret = -1;
4366 goto out;
4367 }
4368 if (!drv->create_opts) {
4369 error_report("Format driver '%s' does not support image creation",
4370 drv->format_name);
4371 ret = -1;
4372 goto out;
4373 }
4374 if (!proto_drv->create_opts) {
4375 error_report("Protocol driver '%s' does not support image creation",
4376 proto_drv->format_name);
4377 ret = -1;
4378 goto out;
4379 }
4380 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4381 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4382
4383 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4384
4385 size = blk_getlength(blk1);
4386 if (size < 0) {
4387 error_report("Failed to get size for '%s'", in.filename);
4388 ret = -1;
4389 goto out;
4390 }
4391
4392 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4393 dd.count * in.bsz < size) {
4394 size = dd.count * in.bsz;
4395 }
4396
Reda Sallahif7c15532016-08-10 16:16:09 +02004397 /* Overflow means the specified offset is beyond input image's size */
4398 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4399 size < in.bsz * in.offset)) {
4400 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4401 } else {
4402 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4403 size - in.bsz * in.offset, &error_abort);
4404 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004405
4406 ret = bdrv_create(drv, out.filename, opts, &local_err);
4407 if (ret < 0) {
4408 error_reportf_err(local_err,
4409 "%s: error while creating output image: ",
4410 out.filename);
4411 ret = -1;
4412 goto out;
4413 }
4414
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004415 /* TODO, we can't honour --image-opts for the target,
4416 * since it needs to be given in a format compatible
4417 * with the bdrv_create() call above which does not
4418 * support image-opts style.
4419 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01004420 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004421 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004422
4423 if (!blk2) {
4424 ret = -1;
4425 goto out;
4426 }
4427
Reda Sallahif7c15532016-08-10 16:16:09 +02004428 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4429 size < in.offset * in.bsz)) {
4430 /* We give a warning if the skip option is bigger than the input
4431 * size and create an empty output disk image (i.e. like dd(1)).
4432 */
4433 error_report("%s: cannot skip to specified offset", in.filename);
4434 in_pos = size;
4435 } else {
4436 in_pos = in.offset * in.bsz;
4437 }
4438
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004439 in.buf = g_new(uint8_t, in.bsz);
4440
Reda Sallahif7c15532016-08-10 16:16:09 +02004441 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004442 int in_ret, out_ret;
4443
4444 if (in_pos + in.bsz > size) {
4445 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4446 } else {
4447 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4448 }
4449 if (in_ret < 0) {
4450 error_report("error while reading from input image file: %s",
4451 strerror(-in_ret));
4452 ret = -1;
4453 goto out;
4454 }
4455 in_pos += in_ret;
4456
4457 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4458
4459 if (out_ret < 0) {
4460 error_report("error while writing to output image file: %s",
4461 strerror(-out_ret));
4462 ret = -1;
4463 goto out;
4464 }
4465 out_pos += out_ret;
4466 }
4467
4468out:
4469 g_free(arg);
4470 qemu_opts_del(opts);
4471 qemu_opts_free(create_opts);
4472 blk_unref(blk1);
4473 blk_unref(blk2);
4474 g_free(in.filename);
4475 g_free(out.filename);
4476 g_free(in.buf);
4477 g_free(out.buf);
4478
4479 if (ret) {
4480 return 1;
4481 }
4482 return 0;
4483}
4484
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004485static void dump_json_block_measure_info(BlockMeasureInfo *info)
4486{
4487 QString *str;
4488 QObject *obj;
4489 Visitor *v = qobject_output_visitor_new(&obj);
4490
4491 visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
4492 visit_complete(v, &obj);
4493 str = qobject_to_json_pretty(obj);
4494 assert(str != NULL);
4495 printf("%s\n", qstring_get_str(str));
4496 qobject_decref(obj);
4497 visit_free(v);
4498 QDECREF(str);
4499}
4500
4501static int img_measure(int argc, char **argv)
4502{
4503 static const struct option long_options[] = {
4504 {"help", no_argument, 0, 'h'},
4505 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4506 {"object", required_argument, 0, OPTION_OBJECT},
4507 {"output", required_argument, 0, OPTION_OUTPUT},
4508 {"size", required_argument, 0, OPTION_SIZE},
4509 {"force-share", no_argument, 0, 'U'},
4510 {0, 0, 0, 0}
4511 };
4512 OutputFormat output_format = OFORMAT_HUMAN;
4513 BlockBackend *in_blk = NULL;
4514 BlockDriver *drv;
4515 const char *filename = NULL;
4516 const char *fmt = NULL;
4517 const char *out_fmt = "raw";
4518 char *options = NULL;
4519 char *snapshot_name = NULL;
4520 bool force_share = false;
4521 QemuOpts *opts = NULL;
4522 QemuOpts *object_opts = NULL;
4523 QemuOpts *sn_opts = NULL;
4524 QemuOptsList *create_opts = NULL;
4525 bool image_opts = false;
4526 uint64_t img_size = UINT64_MAX;
4527 BlockMeasureInfo *info = NULL;
4528 Error *local_err = NULL;
4529 int ret = 1;
4530 int c;
4531
4532 while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
4533 long_options, NULL)) != -1) {
4534 switch (c) {
4535 case '?':
4536 case 'h':
4537 help();
4538 break;
4539 case 'f':
4540 fmt = optarg;
4541 break;
4542 case 'O':
4543 out_fmt = optarg;
4544 break;
4545 case 'o':
4546 if (!is_valid_option_list(optarg)) {
4547 error_report("Invalid option list: %s", optarg);
4548 goto out;
4549 }
4550 if (!options) {
4551 options = g_strdup(optarg);
4552 } else {
4553 char *old_options = options;
4554 options = g_strdup_printf("%s,%s", options, optarg);
4555 g_free(old_options);
4556 }
4557 break;
4558 case 'l':
4559 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
4560 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
4561 optarg, false);
4562 if (!sn_opts) {
4563 error_report("Failed in parsing snapshot param '%s'",
4564 optarg);
4565 goto out;
4566 }
4567 } else {
4568 snapshot_name = optarg;
4569 }
4570 break;
4571 case 'U':
4572 force_share = true;
4573 break;
4574 case OPTION_OBJECT:
4575 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
4576 optarg, true);
4577 if (!object_opts) {
4578 goto out;
4579 }
4580 break;
4581 case OPTION_IMAGE_OPTS:
4582 image_opts = true;
4583 break;
4584 case OPTION_OUTPUT:
4585 if (!strcmp(optarg, "json")) {
4586 output_format = OFORMAT_JSON;
4587 } else if (!strcmp(optarg, "human")) {
4588 output_format = OFORMAT_HUMAN;
4589 } else {
4590 error_report("--output must be used with human or json "
4591 "as argument.");
4592 goto out;
4593 }
4594 break;
4595 case OPTION_SIZE:
4596 {
4597 int64_t sval;
4598
4599 sval = cvtnum(optarg);
4600 if (sval < 0) {
4601 if (sval == -ERANGE) {
4602 error_report("Image size must be less than 8 EiB!");
4603 } else {
4604 error_report("Invalid image size specified! You may use "
4605 "k, M, G, T, P or E suffixes for ");
4606 error_report("kilobytes, megabytes, gigabytes, terabytes, "
4607 "petabytes and exabytes.");
4608 }
4609 goto out;
4610 }
4611 img_size = (uint64_t)sval;
4612 }
4613 break;
4614 }
4615 }
4616
4617 if (qemu_opts_foreach(&qemu_object_opts,
4618 user_creatable_add_opts_foreach,
4619 NULL, NULL)) {
4620 goto out;
4621 }
4622
4623 if (argc - optind > 1) {
4624 error_report("At most one filename argument is allowed.");
4625 goto out;
4626 } else if (argc - optind == 1) {
4627 filename = argv[optind];
4628 }
4629
4630 if (!filename &&
4631 (object_opts || image_opts || fmt || snapshot_name || sn_opts)) {
4632 error_report("--object, --image-opts, -f, and -l "
4633 "require a filename argument.");
4634 goto out;
4635 }
4636 if (filename && img_size != UINT64_MAX) {
4637 error_report("--size N cannot be used together with a filename.");
4638 goto out;
4639 }
4640 if (!filename && img_size == UINT64_MAX) {
4641 error_report("Either --size N or one filename must be specified.");
4642 goto out;
4643 }
4644
4645 if (filename) {
4646 in_blk = img_open(image_opts, filename, fmt, 0,
4647 false, false, force_share);
4648 if (!in_blk) {
4649 goto out;
4650 }
4651
4652 if (sn_opts) {
4653 bdrv_snapshot_load_tmp(blk_bs(in_blk),
4654 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
4655 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
4656 &local_err);
4657 } else if (snapshot_name != NULL) {
4658 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
4659 snapshot_name, &local_err);
4660 }
4661 if (local_err) {
4662 error_reportf_err(local_err, "Failed to load snapshot: ");
4663 goto out;
4664 }
4665 }
4666
4667 drv = bdrv_find_format(out_fmt);
4668 if (!drv) {
4669 error_report("Unknown file format '%s'", out_fmt);
4670 goto out;
4671 }
4672 if (!drv->create_opts) {
4673 error_report("Format driver '%s' does not support image creation",
4674 drv->format_name);
4675 goto out;
4676 }
4677
4678 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4679 create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
4680 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4681 if (options) {
4682 qemu_opts_do_parse(opts, options, NULL, &local_err);
4683 if (local_err) {
4684 error_report_err(local_err);
4685 error_report("Invalid options for file format '%s'", out_fmt);
4686 goto out;
4687 }
4688 }
4689 if (img_size != UINT64_MAX) {
4690 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
4691 }
4692
4693 info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
4694 if (local_err) {
4695 error_report_err(local_err);
4696 goto out;
4697 }
4698
4699 if (output_format == OFORMAT_HUMAN) {
4700 printf("required size: %" PRIu64 "\n", info->required);
4701 printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
4702 } else {
4703 dump_json_block_measure_info(info);
4704 }
4705
4706 ret = 0;
4707
4708out:
4709 qapi_free_BlockMeasureInfo(info);
4710 qemu_opts_del(object_opts);
4711 qemu_opts_del(opts);
4712 qemu_opts_del(sn_opts);
4713 qemu_opts_free(create_opts);
4714 g_free(options);
4715 blk_unref(in_blk);
4716 return ret;
4717}
Kevin Wolfb6133b82014-08-05 14:17:13 +02004718
Anthony Liguoric227f092009-10-01 16:12:16 -05004719static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004720#define DEF(option, callback, arg_string) \
4721 { option, callback },
4722#include "qemu-img-cmds.h"
4723#undef DEF
4724#undef GEN_DOCS
4725 { NULL, NULL, },
4726};
4727
bellardea2384d2004-08-01 21:59:26 +00004728int main(int argc, char **argv)
4729{
Anthony Liguoric227f092009-10-01 16:12:16 -05004730 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004731 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004732 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004733 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004734 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004735 static const struct option long_options[] = {
4736 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004737 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004738 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004739 {0, 0, 0, 0}
4740 };
bellardea2384d2004-08-01 21:59:26 +00004741
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004742#ifdef CONFIG_POSIX
4743 signal(SIGPIPE, SIG_IGN);
4744#endif
4745
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004746 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004747 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004748 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004749
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004750 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004751 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004752 exit(EXIT_FAILURE);
4753 }
4754
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004755 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004756
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004757 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004758 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004759 if (argc < 2) {
4760 error_exit("Not enough arguments");
4761 }
Stuart Brady153859b2009-06-07 00:42:17 +01004762
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004763 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004764 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004765 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004766
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004767 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004768 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004769 case ':':
4770 missing_argument(argv[optind - 1]);
4771 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08004772 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004773 unrecognized_option(argv[optind - 1]);
4774 return 0;
4775 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03004776 help();
4777 return 0;
4778 case 'V':
4779 printf(QEMU_IMG_VERSION);
4780 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004781 case 'T':
4782 g_free(trace_file);
4783 trace_file = trace_opt_parse(optarg);
4784 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004785 }
bellardea2384d2004-08-01 21:59:26 +00004786 }
Stuart Brady153859b2009-06-07 00:42:17 +01004787
Denis V. Lunev10985132016-06-17 17:44:13 +03004788 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004789
Denis V. Lunev10985132016-06-17 17:44:13 +03004790 /* reset getopt_long scanning */
4791 argc -= optind;
4792 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004793 return 0;
4794 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004795 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004796 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004797
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004798 if (!trace_init_backends()) {
4799 exit(1);
4800 }
4801 trace_init_file(trace_file);
4802 qemu_set_log(LOG_TRACE);
4803
Denis V. Lunev10985132016-06-17 17:44:13 +03004804 /* find the command */
4805 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4806 if (!strcmp(cmdname, cmd->name)) {
4807 return cmd->handler(argc, argv);
4808 }
4809 }
Jeff Cody7db16892014-04-25 17:02:32 -04004810
Stuart Brady153859b2009-06-07 00:42:17 +01004811 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004812 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004813}