blob: b1c1e484d215463ac4585b81873ce3ca0b3ecfed [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 */
Markus Armbruster452fcdb2018-02-01 12:18:39 +010024
Peter Maydell80c71a22016-01-18 18:01:42 +000025#include "qemu/osdep.h"
Eric Blakec2a3d7d2017-07-21 08:50:47 -050026#include <getopt.h>
27
Fam Zheng67a1de02016-06-01 17:44:21 +080028#include "qemu-version.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010029#include "qapi/error.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010030#include "qapi/qapi-visit-block-core.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010031#include "qapi/qobject-output-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010032#include "qapi/qmp/qjson.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010033#include "qapi/qmp/qdict.h"
Markus Armbrusterfc81fa12018-02-01 12:18:40 +010034#include "qapi/qmp/qstring.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020035#include "qemu/cutils.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000036#include "qemu/config-file.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010037#include "qemu/option.h"
38#include "qemu/error-report.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030039#include "qemu/log.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000040#include "qom/object_interfaces.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010041#include "sysemu/sysemu.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020042#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010043#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020044#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080045#include "block/qapi.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010046#include "crypto/init.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030047#include "trace/control.h"
bellarde8445332006-06-14 15:32:10 +000048
Thomas Huth7e563bf2018-02-15 12:06:47 +010049#define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \
Thomas Huth0781dd62016-10-05 11:54:44 +020050 "\n" QEMU_COPYRIGHT "\n"
Jeff Cody5f6979c2014-04-28 14:37:18 -040051
Anthony Liguoric227f092009-10-01 16:12:16 -050052typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010053 const char *name;
54 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050055} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010056
Federico Simoncelli8599ea42013-01-28 06:59:47 -050057enum {
58 OPTION_OUTPUT = 256,
59 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000060 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000061 OPTION_IMAGE_OPTS = 259,
Kevin Wolfb6495fa2015-07-10 18:09:18 +020062 OPTION_PATTERN = 260,
Kevin Wolf55d539c2016-06-03 13:59:41 +020063 OPTION_FLUSH_INTERVAL = 261,
64 OPTION_NO_DRAIN = 262,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +010065 OPTION_TARGET_IMAGE_OPTS = 263,
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +010066 OPTION_SIZE = 264,
Max Reitzdc5f6902017-06-13 22:20:55 +020067 OPTION_PREALLOCATION = 265,
Pavel Butsykin4ffca892017-09-18 15:42:27 +030068 OPTION_SHRINK = 266,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050069};
70
71typedef enum OutputFormat {
72 OFORMAT_JSON,
73 OFORMAT_HUMAN,
74} OutputFormat;
75
Kevin Wolfe6996142016-03-15 13:03:11 +010076/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040077#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000078
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010079static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000080{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010081 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000082}
83
Fam Zhengac1307a2014-04-22 13:36:11 +080084static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
85{
86 va_list ap;
87
88 error_printf("qemu-img: ");
89
90 va_start(ap, fmt);
91 error_vprintf(fmt, ap);
92 va_end(ap);
93
94 error_printf("\nTry 'qemu-img --help' for more information\n");
95 exit(EXIT_FAILURE);
96}
97
Stefan Hajnoczic9192972017-03-17 18:45:41 +080098static void QEMU_NORETURN missing_argument(const char *option)
99{
100 error_exit("missing argument for option '%s'", option);
101}
102
103static void QEMU_NORETURN unrecognized_option(const char *option)
104{
105 error_exit("unrecognized option '%s'", option);
106}
107
blueswir1d2c639d2009-01-24 18:19:25 +0000108/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +0800109static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +0000110{
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100111 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -0400112 QEMU_IMG_VERSION
Denis V. Lunev10985132016-06-17 17:44:13 +0300113 "usage: qemu-img [standard options] command [command options]\n"
malc3f020d72010-02-08 12:04:56 +0300114 "QEMU disk image utility\n"
115 "\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300116 " '-h', '--help' display this help and exit\n"
117 " '-V', '--version' output version information and exit\n"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +0300118 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
119 " specify tracing options\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300120 "\n"
malc3f020d72010-02-08 12:04:56 +0300121 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100122#define DEF(option, callback, arg_string) \
123 " " arg_string "\n"
124#include "qemu-img-cmds.h"
125#undef DEF
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
Max Reitzd402b6a2018-05-09 23:00:21 +0200252 if (!drv->create_opts) {
253 error_report("Format driver '%s' does not support image creation", fmt);
254 return 1;
255 }
256
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800257 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100258 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500259 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100260 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100261 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800262 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100263 return 1;
264 }
Max Reitzd402b6a2018-05-09 23:00:21 +0200265 if (!proto_drv->create_opts) {
266 error_report("Protocal driver '%s' does not support image creation",
267 proto_drv->format_name);
268 return 1;
269 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800270 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100271 }
272
Max Reitz7f3fb002018-05-09 23:00:19 +0200273 printf("Supported options:\n");
Chunyan Liu83d05212014-06-05 17:20:51 +0800274 qemu_opts_print_help(create_opts);
275 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100276 return 0;
277}
278
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000279
Max Reitzefaa7c42016-03-16 19:54:38 +0100280static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100281 QemuOpts *opts, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800282 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000283{
284 QDict *options;
285 Error *local_err = NULL;
286 BlockBackend *blk;
287 options = qemu_opts_to_qdict(opts, NULL);
Fam Zheng335e9932017-05-03 00:35:39 +0800288 if (force_share) {
289 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
Max Reitz4615f872018-05-02 22:20:50 +0200290 && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) {
Fam Zheng335e9932017-05-03 00:35:39 +0800291 error_report("--force-share/-U conflicts with image options");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200292 qobject_unref(options);
Fam Zheng335e9932017-05-03 00:35:39 +0800293 return NULL;
294 }
Max Reitz4615f872018-05-02 22:20:50 +0200295 qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on");
Fam Zheng335e9932017-05-03 00:35:39 +0800296 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100297 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000298 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100299 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000300 return NULL;
301 }
Kevin Wolfce099542016-03-15 13:01:04 +0100302 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000303
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000304 return blk;
305}
306
Max Reitzefaa7c42016-03-16 19:54:38 +0100307static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100308 QDict *options,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000309 const char *fmt, int flags,
Fam Zheng335e9932017-05-03 00:35:39 +0800310 bool writethrough, bool quiet,
311 bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000312{
313 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200314 Error *local_err = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100315
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100316 if (!options) {
317 options = qdict_new();
318 }
bellard75c23802004-08-27 21:28:58 +0000319 if (fmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500320 qdict_put_str(options, "driver", fmt);
bellard75c23802004-08-27 21:28:58 +0000321 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100322
Fam Zheng335e9932017-05-03 00:35:39 +0800323 if (force_share) {
Eric Blake579cf1d2017-05-15 14:54:39 -0500324 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800325 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100326 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500327 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100328 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000329 return NULL;
bellard75c23802004-08-27 21:28:58 +0000330 }
Kevin Wolfce099542016-03-15 13:01:04 +0100331 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100332
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200333 return blk;
bellard75c23802004-08-27 21:28:58 +0000334}
335
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000336
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100337static int img_add_key_secrets(void *opaque,
338 const char *name, const char *value,
339 Error **errp)
340{
341 QDict *options = opaque;
342
343 if (g_str_has_suffix(name, "key-secret")) {
Eric Blake187f47e2017-06-24 12:10:07 -0600344 qdict_put_str(options, name, value);
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100345 }
346
347 return 0;
348}
349
350static BlockBackend *img_open_new_file(const char *filename,
351 QemuOpts *create_opts,
352 const char *fmt, int flags,
353 bool writethrough, bool quiet,
354 bool force_share)
355{
356 QDict *options = NULL;
357
358 options = qdict_new();
359 qemu_opt_foreach(create_opts, img_add_key_secrets, options, &error_abort);
360
361 return img_open_file(filename, options, fmt, flags, writethrough, quiet,
362 force_share);
363}
364
365
Max Reitzefaa7c42016-03-16 19:54:38 +0100366static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000367 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100368 const char *fmt, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800369 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000370{
371 BlockBackend *blk;
372 if (image_opts) {
373 QemuOpts *opts;
374 if (fmt) {
375 error_report("--image-opts and --format are mutually exclusive");
376 return NULL;
377 }
378 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
379 filename, true);
380 if (!opts) {
381 return NULL;
382 }
Fam Zheng335e9932017-05-03 00:35:39 +0800383 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
384 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000385 } else {
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100386 blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
Fam Zheng335e9932017-05-03 00:35:39 +0800387 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000388 }
389 return blk;
390}
391
392
Chunyan Liu83d05212014-06-05 17:20:51 +0800393static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100394 const char *base_filename,
395 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200396{
Markus Armbruster6750e792015-02-12 17:43:08 +0100397 Error *err = NULL;
398
Kevin Wolfefa84d42009-05-18 16:42:12 +0200399 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100400 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100401 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100402 error_report("Backing file not supported for file format '%s'",
403 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100404 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900405 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200406 }
407 }
408 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100409 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100410 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100411 error_report("Backing file format not supported for file "
412 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100413 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900414 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200415 }
416 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900417 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200418}
419
Markus Armbruster606caa02017-02-21 21:14:04 +0100420static int64_t cvtnum(const char *s)
421{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100422 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100423 uint64_t value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100424
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100425 err = qemu_strtosz(s, NULL, &value);
426 if (err < 0) {
427 return err;
428 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100429 if (value > INT64_MAX) {
430 return -ERANGE;
431 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100432 return value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100433}
434
bellardea2384d2004-08-01 21:59:26 +0000435static int img_create(int argc, char **argv)
436{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200437 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100438 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000439 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000440 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000441 const char *filename;
442 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200443 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200444 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100445 bool quiet = false;
John Snow6e6e55f2017-07-17 20:34:22 -0400446 int flags = 0;
ths3b46e622007-09-17 08:09:54 +0000447
bellardea2384d2004-08-01 21:59:26 +0000448 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000449 static const struct option long_options[] = {
450 {"help", no_argument, 0, 'h'},
451 {"object", required_argument, 0, OPTION_OBJECT},
452 {0, 0, 0, 0}
453 };
John Snow6e6e55f2017-07-17 20:34:22 -0400454 c = getopt_long(argc, argv, ":F:b:f:ho:qu",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000455 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100456 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000457 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100458 }
bellardea2384d2004-08-01 21:59:26 +0000459 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800460 case ':':
461 missing_argument(argv[optind - 1]);
462 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100463 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800464 unrecognized_option(argv[optind - 1]);
465 break;
bellardea2384d2004-08-01 21:59:26 +0000466 case 'h':
467 help();
468 break;
aliguori9230eaf2009-03-28 17:55:19 +0000469 case 'F':
470 base_fmt = optarg;
471 break;
bellardea2384d2004-08-01 21:59:26 +0000472 case 'b':
473 base_filename = optarg;
474 break;
475 case 'f':
476 fmt = optarg;
477 break;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200478 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100479 if (!is_valid_option_list(optarg)) {
480 error_report("Invalid option list: %s", optarg);
481 goto fail;
482 }
483 if (!options) {
484 options = g_strdup(optarg);
485 } else {
486 char *old_options = options;
487 options = g_strdup_printf("%s,%s", options, optarg);
488 g_free(old_options);
489 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200490 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100491 case 'q':
492 quiet = true;
493 break;
John Snow6e6e55f2017-07-17 20:34:22 -0400494 case 'u':
495 flags |= BDRV_O_NO_BACKING;
496 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000497 case OPTION_OBJECT: {
498 QemuOpts *opts;
499 opts = qemu_opts_parse_noisily(&qemu_object_opts,
500 optarg, true);
501 if (!opts) {
502 goto fail;
503 }
504 } break;
bellardea2384d2004-08-01 21:59:26 +0000505 }
506 }
aliguori9230eaf2009-03-28 17:55:19 +0000507
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900508 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100509 filename = (optind < argc) ? argv[optind] : NULL;
510 if (options && has_help_option(options)) {
511 g_free(options);
512 return print_block_option_help(filename, fmt);
513 }
514
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100515 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800516 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100517 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100518 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900519
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000520 if (qemu_opts_foreach(&qemu_object_opts,
521 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200522 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000523 goto fail;
524 }
525
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100526 /* Get image size, if specified */
527 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100528 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100529
530 sval = cvtnum(argv[optind++]);
531 if (sval < 0) {
liguang79443392012-12-17 09:49:23 +0800532 if (sval == -ERANGE) {
533 error_report("Image size must be less than 8 EiB!");
534 } else {
535 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200536 "G, T, P or E suffixes for ");
537 error_report("kilobytes, megabytes, gigabytes, terabytes, "
538 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800539 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100540 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100541 }
542 img_size = (uint64_t)sval;
543 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200544 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800545 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200546 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100547
Luiz Capitulino9b375252012-11-30 10:52:05 -0200548 bdrv_img_create(filename, fmt, base_filename, base_fmt,
John Snow6e6e55f2017-07-17 20:34:22 -0400549 options, img_size, flags, quiet, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100550 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100551 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100552 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900553 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200554
Kevin Wolf77386bf2014-02-21 16:24:04 +0100555 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000556 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100557
558fail:
559 g_free(options);
560 return 1;
bellardea2384d2004-08-01 21:59:26 +0000561}
562
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100563static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500564{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500565 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500566 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100567 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600568
569 visit_type_ImageCheck(v, NULL, &check, &error_abort);
570 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500571 str = qobject_to_json_pretty(obj);
572 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100573 qprintf(quiet, "%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200574 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600575 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200576 qobject_unref(str);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500577}
578
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100579static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500580{
581 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100582 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500583 } else {
584 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100585 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
586 "Data may be corrupted, or further writes to the image "
587 "may corrupt it.\n",
588 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500589 }
590
591 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100592 qprintf(quiet,
593 "\n%" PRId64 " leaked clusters were found on the image.\n"
594 "This means waste of disk space, but no harm to data.\n",
595 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500596 }
597
598 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100599 qprintf(quiet,
600 "\n%" PRId64
601 " internal errors have occurred during the check.\n",
602 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500603 }
604 }
605
606 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100607 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
608 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
609 check->allocated_clusters, check->total_clusters,
610 check->allocated_clusters * 100.0 / check->total_clusters,
611 check->fragmented_clusters * 100.0 / check->allocated_clusters,
612 check->compressed_clusters * 100.0 /
613 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500614 }
615
616 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100617 qprintf(quiet,
618 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500619 }
620}
621
622static int collect_image_check(BlockDriverState *bs,
623 ImageCheck *check,
624 const char *filename,
625 const char *fmt,
626 int fix)
627{
628 int ret;
629 BdrvCheckResult result;
630
631 ret = bdrv_check(bs, &result, fix);
632 if (ret < 0) {
633 return ret;
634 }
635
636 check->filename = g_strdup(filename);
637 check->format = g_strdup(bdrv_get_format_name(bs));
638 check->check_errors = result.check_errors;
639 check->corruptions = result.corruptions;
640 check->has_corruptions = result.corruptions != 0;
641 check->leaks = result.leaks;
642 check->has_leaks = result.leaks != 0;
643 check->corruptions_fixed = result.corruptions_fixed;
644 check->has_corruptions_fixed = result.corruptions != 0;
645 check->leaks_fixed = result.leaks_fixed;
646 check->has_leaks_fixed = result.leaks != 0;
647 check->image_end_offset = result.image_end_offset;
648 check->has_image_end_offset = result.image_end_offset != 0;
649 check->total_clusters = result.bfi.total_clusters;
650 check->has_total_clusters = result.bfi.total_clusters != 0;
651 check->allocated_clusters = result.bfi.allocated_clusters;
652 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
653 check->fragmented_clusters = result.bfi.fragmented_clusters;
654 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100655 check->compressed_clusters = result.bfi.compressed_clusters;
656 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500657
658 return 0;
659}
660
Kevin Wolfe076f332010-06-29 11:43:13 +0200661/*
662 * Checks an image for consistency. Exit codes:
663 *
Max Reitzd6635c42014-06-02 22:15:21 +0200664 * 0 - Check completed, image is good
665 * 1 - Check not completed because of internal errors
666 * 2 - Check completed, image is corrupted
667 * 3 - Check completed, image has leaked clusters, but is good otherwise
668 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200669 */
aliguori15859692009-04-21 23:11:53 +0000670static int img_check(int argc, char **argv)
671{
672 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500673 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200674 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200675 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000676 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200677 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100678 int flags = BDRV_O_CHECK;
679 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200680 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100681 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000682 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +0800683 bool force_share = false;
aliguori15859692009-04-21 23:11:53 +0000684
685 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500686 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200687 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100688
aliguori15859692009-04-21 23:11:53 +0000689 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500690 int option_index = 0;
691 static const struct option long_options[] = {
692 {"help", no_argument, 0, 'h'},
693 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530694 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500695 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000696 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000697 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +0800698 {"force-share", no_argument, 0, 'U'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500699 {0, 0, 0, 0}
700 };
Fam Zheng335e9932017-05-03 00:35:39 +0800701 c = getopt_long(argc, argv, ":hf:r:T:qU",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500702 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100703 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000704 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100705 }
aliguori15859692009-04-21 23:11:53 +0000706 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800707 case ':':
708 missing_argument(argv[optind - 1]);
709 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100710 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800711 unrecognized_option(argv[optind - 1]);
712 break;
aliguori15859692009-04-21 23:11:53 +0000713 case 'h':
714 help();
715 break;
716 case 'f':
717 fmt = optarg;
718 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200719 case 'r':
720 flags |= BDRV_O_RDWR;
721
722 if (!strcmp(optarg, "leaks")) {
723 fix = BDRV_FIX_LEAKS;
724 } else if (!strcmp(optarg, "all")) {
725 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
726 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800727 error_exit("Unknown option value for -r "
728 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200729 }
730 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500731 case OPTION_OUTPUT:
732 output = optarg;
733 break;
Max Reitz40055952014-07-22 22:58:42 +0200734 case 'T':
735 cache = optarg;
736 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100737 case 'q':
738 quiet = true;
739 break;
Fam Zheng335e9932017-05-03 00:35:39 +0800740 case 'U':
741 force_share = true;
742 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000743 case OPTION_OBJECT: {
744 QemuOpts *opts;
745 opts = qemu_opts_parse_noisily(&qemu_object_opts,
746 optarg, true);
747 if (!opts) {
748 return 1;
749 }
750 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000751 case OPTION_IMAGE_OPTS:
752 image_opts = true;
753 break;
aliguori15859692009-04-21 23:11:53 +0000754 }
755 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200756 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800757 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100758 }
aliguori15859692009-04-21 23:11:53 +0000759 filename = argv[optind++];
760
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500761 if (output && !strcmp(output, "json")) {
762 output_format = OFORMAT_JSON;
763 } else if (output && !strcmp(output, "human")) {
764 output_format = OFORMAT_HUMAN;
765 } else if (output) {
766 error_report("--output must be used with human or json as argument.");
767 return 1;
768 }
769
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000770 if (qemu_opts_foreach(&qemu_object_opts,
771 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200772 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000773 return 1;
774 }
775
Kevin Wolfce099542016-03-15 13:01:04 +0100776 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200777 if (ret < 0) {
778 error_report("Invalid source cache option: %s", cache);
779 return 1;
780 }
781
Fam Zheng335e9932017-05-03 00:35:39 +0800782 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
783 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200784 if (!blk) {
785 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900786 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200787 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500788
789 check = g_new0(ImageCheck, 1);
790 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200791
792 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200793 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200794 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500795 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200796 }
797
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500798 if (check->corruptions_fixed || check->leaks_fixed) {
799 int corruptions_fixed, leaks_fixed;
800
801 leaks_fixed = check->leaks_fixed;
802 corruptions_fixed = check->corruptions_fixed;
803
804 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100805 qprintf(quiet,
806 "The following inconsistencies were found and repaired:\n\n"
807 " %" PRId64 " leaked clusters\n"
808 " %" PRId64 " corruptions\n\n"
809 "Double checking the fixed image now...\n",
810 check->leaks_fixed,
811 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500812 }
813
814 ret = collect_image_check(bs, check, filename, fmt, 0);
815
816 check->leaks_fixed = leaks_fixed;
817 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200818 }
819
Max Reitz832390a2014-10-23 15:29:12 +0200820 if (!ret) {
821 switch (output_format) {
822 case OFORMAT_HUMAN:
823 dump_human_image_check(check, quiet);
824 break;
825 case OFORMAT_JSON:
826 dump_json_image_check(check, quiet);
827 break;
828 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500829 }
830
831 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200832 if (ret) {
833 error_report("Check failed: %s", strerror(-ret));
834 } else {
835 error_report("Check failed");
836 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500837 ret = 1;
838 goto fail;
839 }
840
841 if (check->corruptions) {
842 ret = 2;
843 } else if (check->leaks) {
844 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200845 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500846 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000847 }
848
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500849fail:
850 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200851 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500852 return ret;
aliguori15859692009-04-21 23:11:53 +0000853}
854
Max Reitzd4a32382014-10-24 15:57:37 +0200855typedef struct CommonBlockJobCBInfo {
856 BlockDriverState *bs;
857 Error **errp;
858} CommonBlockJobCBInfo;
859
860static void common_block_job_cb(void *opaque, int ret)
861{
862 CommonBlockJobCBInfo *cbi = opaque;
863
864 if (ret < 0) {
865 error_setg_errno(cbi->errp, -ret, "Block job failed");
866 }
Max Reitzd4a32382014-10-24 15:57:37 +0200867}
868
869static void run_block_job(BlockJob *job, Error **errp)
870{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200871 AioContext *aio_context = blk_get_aio_context(job->blk);
sochin.jiang4172a002017-06-15 14:47:33 +0800872 int ret = 0;
Max Reitzd4a32382014-10-24 15:57:37 +0200873
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200874 aio_context_acquire(aio_context);
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200875 job_ref(&job->job);
Max Reitzd4a32382014-10-24 15:57:37 +0200876 do {
Kevin Wolf30a5c882018-05-04 12:17:20 +0200877 float progress = 0.0f;
Max Reitzd4a32382014-10-24 15:57:37 +0200878 aio_poll(aio_context, true);
Kevin Wolf30a5c882018-05-04 12:17:20 +0200879 if (job->job.progress_total) {
880 progress = (float)job->job.progress_current /
881 job->job.progress_total * 100.f;
882 }
883 qemu_progress_print(progress, 0);
Kevin Wolfdf956ae2018-04-25 15:09:58 +0200884 } while (!job_is_ready(&job->job) && !job_is_completed(&job->job));
Max Reitzd4a32382014-10-24 15:57:37 +0200885
Kevin Wolfdbe5e6c2018-04-19 13:04:01 +0200886 if (!job_is_completed(&job->job)) {
Kevin Wolf3d70ff52018-04-24 16:13:52 +0200887 ret = job_complete_sync(&job->job, errp);
sochin.jiang4172a002017-06-15 14:47:33 +0800888 } else {
Kevin Wolf4ad35182018-04-19 17:30:16 +0200889 ret = job->job.ret;
sochin.jiang4172a002017-06-15 14:47:33 +0800890 }
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200891 job_unref(&job->job);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200892 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200893
sochin.jiang4172a002017-06-15 14:47:33 +0800894 /* publish completion progress only when success */
895 if (!ret) {
896 qemu_progress_print(100.f, 0);
897 }
Max Reitzd4a32382014-10-24 15:57:37 +0200898}
899
bellardea2384d2004-08-01 21:59:26 +0000900static int img_commit(int argc, char **argv)
901{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400902 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200903 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200904 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200905 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100906 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200907 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100908 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200909 Error *local_err = NULL;
910 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000911 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200912 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000913
914 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400915 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200916 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000917 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000918 static const struct option long_options[] = {
919 {"help", no_argument, 0, 'h'},
920 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000921 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000922 {0, 0, 0, 0}
923 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800924 c = getopt_long(argc, argv, ":f:ht:b:dpq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000925 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100926 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000927 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100928 }
bellardea2384d2004-08-01 21:59:26 +0000929 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800930 case ':':
931 missing_argument(argv[optind - 1]);
932 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100933 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800934 unrecognized_option(argv[optind - 1]);
935 break;
bellardea2384d2004-08-01 21:59:26 +0000936 case 'h':
937 help();
938 break;
939 case 'f':
940 fmt = optarg;
941 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400942 case 't':
943 cache = optarg;
944 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200945 case 'b':
946 base = optarg;
947 /* -b implies -d */
948 drop = true;
949 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200950 case 'd':
951 drop = true;
952 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200953 case 'p':
954 progress = true;
955 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100956 case 'q':
957 quiet = true;
958 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000959 case OPTION_OBJECT: {
960 QemuOpts *opts;
961 opts = qemu_opts_parse_noisily(&qemu_object_opts,
962 optarg, true);
963 if (!opts) {
964 return 1;
965 }
966 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000967 case OPTION_IMAGE_OPTS:
968 image_opts = true;
969 break;
bellardea2384d2004-08-01 21:59:26 +0000970 }
971 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200972
973 /* Progress is not shown in Quiet mode */
974 if (quiet) {
975 progress = false;
976 }
977
Kevin Wolffc11eb22013-08-05 10:53:04 +0200978 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800979 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100980 }
bellardea2384d2004-08-01 21:59:26 +0000981 filename = argv[optind++];
982
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000983 if (qemu_opts_foreach(&qemu_object_opts,
984 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200985 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000986 return 1;
987 }
988
Max Reitz9a86fe42014-10-24 15:57:38 +0200989 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100990 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400991 if (ret < 0) {
992 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100993 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400994 }
995
Fam Zheng335e9932017-05-03 00:35:39 +0800996 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
997 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200998 if (!blk) {
999 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001000 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001001 bs = blk_bs(blk);
1002
Max Reitz687fa1d2014-10-24 15:57:39 +02001003 qemu_progress_init(progress, 1.f);
1004 qemu_progress_print(0.f, 100);
1005
Max Reitz1b22bff2014-10-24 15:57:40 +02001006 if (base) {
1007 base_bs = bdrv_find_backing_image(bs, base);
1008 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +01001009 error_setg(&local_err,
1010 "Did not find '%s' in the backing chain of '%s'",
1011 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +02001012 goto done;
1013 }
1014 } else {
1015 /* This is different from QMP, which by default uses the deepest file in
1016 * the backing chain (i.e., the very base); however, the traditional
1017 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +02001018 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +02001019 if (!base_bs) {
1020 error_setg(&local_err, "Image does not have a backing file");
1021 goto done;
1022 }
bellardea2384d2004-08-01 21:59:26 +00001023 }
1024
Max Reitzd4a32382014-10-24 15:57:37 +02001025 cbi = (CommonBlockJobCBInfo){
1026 .errp = &local_err,
1027 .bs = bs,
1028 };
1029
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001030 aio_context = bdrv_get_aio_context(bs);
1031 aio_context_acquire(aio_context);
Kevin Wolfbb02b652018-04-19 17:54:56 +02001032 commit_active_start("commit", bs, base_bs, JOB_DEFAULT, 0,
Kevin Wolf0db832f2017-02-20 18:10:05 +01001033 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +08001034 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001035 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +02001036 if (local_err) {
1037 goto done;
1038 }
1039
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001040 /* When the block job completes, the BlockBackend reference will point to
1041 * the old backing file. In order to avoid that the top image is already
1042 * deleted, so we can still empty it afterwards, increment the reference
1043 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +02001044 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001045 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001046 }
1047
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001048 job = block_job_get("commit");
1049 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001050 if (local_err) {
1051 goto unref_backing;
1052 }
1053
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001054 if (!drop && bs->drv->bdrv_make_empty) {
1055 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001056 if (ret) {
1057 error_setg_errno(&local_err, -ret, "Could not empty %s",
1058 filename);
1059 goto unref_backing;
1060 }
1061 }
1062
1063unref_backing:
1064 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001065 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001066 }
Max Reitzd4a32382014-10-24 15:57:37 +02001067
1068done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001069 qemu_progress_end();
1070
Markus Armbruster26f54e92014-10-07 13:59:04 +02001071 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001072
1073 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001074 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001075 return 1;
1076 }
Max Reitzd4a32382014-10-24 15:57:37 +02001077
1078 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001079 return 0;
1080}
1081
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001082/*
Eric Blakedebb38a2017-10-11 22:47:11 -05001083 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1084 * of the first sector boundary within buf where the sector contains a
1085 * non-zero byte. This function is robust to a buffer that is not
1086 * sector-aligned.
1087 */
1088static int64_t find_nonzero(const uint8_t *buf, int64_t n)
1089{
1090 int64_t i;
1091 int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE);
1092
1093 for (i = 0; i < end; i += BDRV_SECTOR_SIZE) {
1094 if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) {
1095 return i;
1096 }
1097 }
1098 if (i < n && !buffer_is_zero(buf + i, n - end)) {
1099 return i;
1100 }
1101 return -1;
1102}
1103
1104/*
thsf58c7b32008-06-05 21:53:49 +00001105 * Returns true iff the first sector pointed to by 'buf' contains at least
1106 * a non-NUL byte.
1107 *
1108 * 'pnum' is set to the number of sectors (including and immediately following
1109 * the first one) that are known to be in the same allocated/unallocated state.
1110 */
bellardea2384d2004-08-01 21:59:26 +00001111static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1112{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001113 bool is_zero;
1114 int i;
bellardea2384d2004-08-01 21:59:26 +00001115
1116 if (n <= 0) {
1117 *pnum = 0;
1118 return 0;
1119 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001120 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001121 for(i = 1; i < n; i++) {
1122 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001123 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001124 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001125 }
bellardea2384d2004-08-01 21:59:26 +00001126 }
1127 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001128 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001129}
1130
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001131/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001132 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1133 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1134 * breaking up write requests for only small sparse areas.
1135 */
1136static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1137 int min)
1138{
1139 int ret;
1140 int num_checked, num_used;
1141
1142 if (n < min) {
1143 min = n;
1144 }
1145
1146 ret = is_allocated_sectors(buf, n, pnum);
1147 if (!ret) {
1148 return ret;
1149 }
1150
1151 num_used = *pnum;
1152 buf += BDRV_SECTOR_SIZE * *pnum;
1153 n -= *pnum;
1154 num_checked = num_used;
1155
1156 while (n > 0) {
1157 ret = is_allocated_sectors(buf, n, pnum);
1158
1159 buf += BDRV_SECTOR_SIZE * *pnum;
1160 n -= *pnum;
1161 num_checked += *pnum;
1162 if (ret) {
1163 num_used = num_checked;
1164 } else if (*pnum >= min) {
1165 break;
1166 }
1167 }
1168
1169 *pnum = num_used;
1170 return 1;
1171}
1172
1173/*
Eric Blakedc61cd32017-10-11 22:47:14 -05001174 * Compares two buffers sector by sector. Returns 0 if the first
1175 * sector of each buffer matches, non-zero otherwise.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001176 *
Eric Blakedc61cd32017-10-11 22:47:14 -05001177 * pnum is set to the sector-aligned size of the buffer prefix that
1178 * has the same matching status as the first sector.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001179 */
Eric Blakedc61cd32017-10-11 22:47:14 -05001180static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2,
1181 int64_t bytes, int64_t *pnum)
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001182{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001183 bool res;
Eric Blakedc61cd32017-10-11 22:47:14 -05001184 int64_t i = MIN(bytes, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001185
Eric Blakedc61cd32017-10-11 22:47:14 -05001186 assert(bytes > 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001187
Eric Blakedc61cd32017-10-11 22:47:14 -05001188 res = !!memcmp(buf1, buf2, i);
1189 while (i < bytes) {
1190 int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001191
Eric Blakedc61cd32017-10-11 22:47:14 -05001192 if (!!memcmp(buf1 + i, buf2 + i, len) != res) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001193 break;
1194 }
Eric Blakedc61cd32017-10-11 22:47:14 -05001195 i += len;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001196 }
1197
1198 *pnum = i;
1199 return res;
1200}
1201
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001202#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001203
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001204/*
1205 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1206 *
Eric Blake0608e402017-10-11 22:47:12 -05001207 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1208 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1209 * failure), and 4 on error (the exit status for read errors), after emitting
1210 * an error message.
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001211 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001212 * @param blk: BlockBackend for the image
Eric Blakec41508e2017-10-11 22:47:13 -05001213 * @param offset: Starting offset to check
1214 * @param bytes: Number of bytes to check
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001215 * @param filename: Name of disk file we are checking (logging purpose)
1216 * @param buffer: Allocated buffer for storing read data
1217 * @param quiet: Flag for quiet mode
1218 */
Eric Blakec41508e2017-10-11 22:47:13 -05001219static int check_empty_sectors(BlockBackend *blk, int64_t offset,
1220 int64_t bytes, const char *filename,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001221 uint8_t *buffer, bool quiet)
1222{
Eric Blakedebb38a2017-10-11 22:47:11 -05001223 int ret = 0;
1224 int64_t idx;
1225
Eric Blakec41508e2017-10-11 22:47:13 -05001226 ret = blk_pread(blk, offset, buffer, bytes);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001227 if (ret < 0) {
1228 error_report("Error while reading offset %" PRId64 " of %s: %s",
Eric Blakec41508e2017-10-11 22:47:13 -05001229 offset, filename, strerror(-ret));
Eric Blake0608e402017-10-11 22:47:12 -05001230 return 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001231 }
Eric Blakec41508e2017-10-11 22:47:13 -05001232 idx = find_nonzero(buffer, bytes);
Eric Blakedebb38a2017-10-11 22:47:11 -05001233 if (idx >= 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001234 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blakec41508e2017-10-11 22:47:13 -05001235 offset + idx);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001236 return 1;
1237 }
1238
1239 return 0;
1240}
1241
1242/*
1243 * Compares two images. Exit codes:
1244 *
1245 * 0 - Images are identical
1246 * 1 - Images differ
1247 * >1 - Error occurred
1248 */
1249static int img_compare(int argc, char **argv)
1250{
Max Reitz40055952014-07-22 22:58:42 +02001251 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001252 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001253 BlockDriverState *bs1, *bs2;
Eric Blake033d9fc2017-10-11 22:47:16 -05001254 int64_t total_size1, total_size2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001255 uint8_t *buf1 = NULL, *buf2 = NULL;
Eric Blake31826642017-10-11 22:47:08 -05001256 int64_t pnum1, pnum2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001257 int allocated1, allocated2;
1258 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1259 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001260 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001261 bool writethrough;
Eric Blake033d9fc2017-10-11 22:47:16 -05001262 int64_t total_size;
1263 int64_t offset = 0;
1264 int64_t chunk;
Eric Blakedc61cd32017-10-11 22:47:14 -05001265 int c;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001266 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001267 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001268 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001269
Max Reitz40055952014-07-22 22:58:42 +02001270 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001271 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001272 static const struct option long_options[] = {
1273 {"help", no_argument, 0, 'h'},
1274 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001275 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001276 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001277 {0, 0, 0, 0}
1278 };
Fam Zheng335e9932017-05-03 00:35:39 +08001279 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001280 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001281 if (c == -1) {
1282 break;
1283 }
1284 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001285 case ':':
1286 missing_argument(argv[optind - 1]);
1287 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001288 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001289 unrecognized_option(argv[optind - 1]);
1290 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001291 case 'h':
1292 help();
1293 break;
1294 case 'f':
1295 fmt1 = optarg;
1296 break;
1297 case 'F':
1298 fmt2 = optarg;
1299 break;
Max Reitz40055952014-07-22 22:58:42 +02001300 case 'T':
1301 cache = optarg;
1302 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001303 case 'p':
1304 progress = true;
1305 break;
1306 case 'q':
1307 quiet = true;
1308 break;
1309 case 's':
1310 strict = true;
1311 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001312 case 'U':
1313 force_share = true;
1314 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001315 case OPTION_OBJECT: {
1316 QemuOpts *opts;
1317 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1318 optarg, true);
1319 if (!opts) {
1320 ret = 2;
1321 goto out4;
1322 }
1323 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001324 case OPTION_IMAGE_OPTS:
1325 image_opts = true;
1326 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001327 }
1328 }
1329
1330 /* Progress is not shown in Quiet mode */
1331 if (quiet) {
1332 progress = false;
1333 }
1334
1335
Kevin Wolffc11eb22013-08-05 10:53:04 +02001336 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001337 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001338 }
1339 filename1 = argv[optind++];
1340 filename2 = argv[optind++];
1341
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001342 if (qemu_opts_foreach(&qemu_object_opts,
1343 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001344 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001345 ret = 2;
1346 goto out4;
1347 }
1348
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001349 /* Initialize before goto out */
1350 qemu_progress_init(progress, 2.0);
1351
Kevin Wolfce099542016-03-15 13:01:04 +01001352 flags = 0;
1353 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001354 if (ret < 0) {
1355 error_report("Invalid source cache option: %s", cache);
1356 ret = 2;
1357 goto out3;
1358 }
1359
Fam Zheng335e9932017-05-03 00:35:39 +08001360 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1361 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001362 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001363 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001364 goto out3;
1365 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001366
Fam Zheng335e9932017-05-03 00:35:39 +08001367 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1368 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001369 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001370 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001371 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001372 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001373 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001374 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001375
Max Reitzf1d3cd72015-02-05 13:58:18 -05001376 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1377 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
Eric Blake033d9fc2017-10-11 22:47:16 -05001378 total_size1 = blk_getlength(blk1);
1379 if (total_size1 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001380 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001381 filename1, strerror(-total_size1));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001382 ret = 4;
1383 goto out;
1384 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001385 total_size2 = blk_getlength(blk2);
1386 if (total_size2 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001387 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001388 filename2, strerror(-total_size2));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001389 ret = 4;
1390 goto out;
1391 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001392 total_size = MIN(total_size1, total_size2);
1393 progress_base = MAX(total_size1, total_size2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001394
1395 qemu_progress_print(0, 100);
1396
Eric Blake033d9fc2017-10-11 22:47:16 -05001397 if (strict && total_size1 != total_size2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001398 ret = 1;
1399 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1400 goto out;
1401 }
1402
Eric Blake033d9fc2017-10-11 22:47:16 -05001403 while (offset < total_size) {
Eric Blake31826642017-10-11 22:47:08 -05001404 int status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001405
Eric Blake033d9fc2017-10-11 22:47:16 -05001406 status1 = bdrv_block_status_above(bs1, NULL, offset,
1407 total_size1 - offset, &pnum1, NULL,
1408 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001409 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001410 ret = 3;
1411 error_report("Sector allocation test failed for %s", filename1);
1412 goto out;
1413 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001414 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001415
Eric Blake033d9fc2017-10-11 22:47:16 -05001416 status2 = bdrv_block_status_above(bs2, NULL, offset,
1417 total_size2 - offset, &pnum2, NULL,
1418 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001419 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001420 ret = 3;
1421 error_report("Sector allocation test failed for %s", filename2);
1422 goto out;
1423 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001424 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
Eric Blake7daddc62017-10-11 22:47:09 -05001425
1426 assert(pnum1 && pnum2);
Eric Blake033d9fc2017-10-11 22:47:16 -05001427 chunk = MIN(pnum1, pnum2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001428
Fam Zheng25ad8e62016-01-13 16:37:41 +08001429 if (strict) {
Eric Blake31826642017-10-11 22:47:08 -05001430 if (status1 != status2) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001431 ret = 1;
1432 qprintf(quiet, "Strict mode: Offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001433 " block status mismatch!\n", offset);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001434 goto out;
1435 }
1436 }
1437 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
Eric Blake7daddc62017-10-11 22:47:09 -05001438 /* nothing to do */
Fam Zheng25ad8e62016-01-13 16:37:41 +08001439 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001440 if (allocated1) {
Eric Blakedc61cd32017-10-11 22:47:14 -05001441 int64_t pnum;
1442
Eric Blake033d9fc2017-10-11 22:47:16 -05001443 chunk = MIN(chunk, IO_BUF_SIZE);
1444 ret = blk_pread(blk1, offset, buf1, chunk);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001445 if (ret < 0) {
1446 error_report("Error while reading offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001447 " of %s: %s",
1448 offset, filename1, strerror(-ret));
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001449 ret = 4;
1450 goto out;
1451 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001452 ret = blk_pread(blk2, offset, buf2, chunk);
1453 if (ret < 0) {
1454 error_report("Error while reading offset %" PRId64
1455 " of %s: %s",
1456 offset, filename2, strerror(-ret));
1457 ret = 4;
1458 goto out;
1459 }
1460 ret = compare_buffers(buf1, buf2, chunk, &pnum);
1461 if (ret || pnum != chunk) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001462 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blake033d9fc2017-10-11 22:47:16 -05001463 offset + (ret ? 0 : pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001464 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001465 goto out;
1466 }
1467 }
1468 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001469 chunk = MIN(chunk, IO_BUF_SIZE);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001470 if (allocated1) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001471 ret = check_empty_sectors(blk1, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001472 filename1, buf1, quiet);
1473 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001474 ret = check_empty_sectors(blk2, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001475 filename2, buf1, quiet);
1476 }
1477 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001478 goto out;
1479 }
1480 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001481 offset += chunk;
1482 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001483 }
1484
Eric Blake033d9fc2017-10-11 22:47:16 -05001485 if (total_size1 != total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001486 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001487 const char *filename_over;
1488
1489 qprintf(quiet, "Warning: Image size mismatch!\n");
Eric Blake033d9fc2017-10-11 22:47:16 -05001490 if (total_size1 > total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001491 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001492 filename_over = filename1;
1493 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001494 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001495 filename_over = filename2;
1496 }
1497
Eric Blake033d9fc2017-10-11 22:47:16 -05001498 while (offset < progress_base) {
1499 ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset,
1500 progress_base - offset, &chunk,
1501 NULL, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001502 if (ret < 0) {
1503 ret = 3;
1504 error_report("Sector allocation test failed for %s",
1505 filename_over);
1506 goto out;
1507
1508 }
Eric Blake391cb1a2017-10-11 22:47:10 -05001509 if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001510 chunk = MIN(chunk, IO_BUF_SIZE);
1511 ret = check_empty_sectors(blk_over, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001512 filename_over, buf1, quiet);
1513 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001514 goto out;
1515 }
1516 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001517 offset += chunk;
1518 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001519 }
1520 }
1521
1522 qprintf(quiet, "Images are identical.\n");
1523 ret = 0;
1524
1525out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001526 qemu_vfree(buf1);
1527 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001528 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001529out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001530 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001531out3:
1532 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001533out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001534 return ret;
1535}
1536
Kevin Wolf690c7302015-03-19 13:33:32 +01001537enum ImgConvertBlockStatus {
1538 BLK_DATA,
1539 BLK_ZERO,
1540 BLK_BACKING_FILE,
1541};
1542
Peter Lieven2d9187b2017-02-28 13:40:07 +01001543#define MAX_COROUTINES 16
1544
Kevin Wolf690c7302015-03-19 13:33:32 +01001545typedef struct ImgConvertState {
1546 BlockBackend **src;
1547 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001548 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001549 int64_t total_sectors;
1550 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001551 int64_t allocated_done;
1552 int64_t sector_num;
1553 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001554 enum ImgConvertBlockStatus status;
1555 int64_t sector_next_status;
1556 BlockBackend *target;
1557 bool has_zero_init;
1558 bool compressed;
1559 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001560 bool wr_in_order;
Fam Zhengee5306d2018-06-01 17:26:48 +08001561 bool copy_range;
Kevin Wolf690c7302015-03-19 13:33:32 +01001562 int min_sparse;
1563 size_t cluster_sectors;
1564 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001565 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001566 int running_coroutines;
1567 Coroutine *co[MAX_COROUTINES];
1568 int64_t wait_sector_num[MAX_COROUTINES];
1569 CoMutex lock;
1570 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001571} ImgConvertState;
1572
Peter Lieven2d9187b2017-02-28 13:40:07 +01001573static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1574 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001575{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001576 *src_cur = 0;
1577 *src_cur_offset = 0;
1578 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1579 *src_cur_offset += s->src_sectors[*src_cur];
1580 (*src_cur)++;
1581 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001582 }
1583}
1584
1585static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1586{
Eric Blake31826642017-10-11 22:47:08 -05001587 int64_t src_cur_offset;
1588 int ret, n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001589
Peter Lieven2d9187b2017-02-28 13:40:07 +01001590 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001591
1592 assert(s->total_sectors > sector_num);
1593 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1594
1595 if (s->sector_next_status <= sector_num) {
Eric Blake31826642017-10-11 22:47:08 -05001596 int64_t count = n * BDRV_SECTOR_SIZE;
1597
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001598 if (s->target_has_backing) {
Eric Blake237d78f2017-10-11 22:47:03 -05001599
1600 ret = bdrv_block_status(blk_bs(s->src[src_cur]),
1601 (sector_num - src_cur_offset) *
1602 BDRV_SECTOR_SIZE,
1603 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001604 } else {
Eric Blake31826642017-10-11 22:47:08 -05001605 ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL,
1606 (sector_num - src_cur_offset) *
1607 BDRV_SECTOR_SIZE,
1608 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001609 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001610 if (ret < 0) {
1611 return ret;
1612 }
Eric Blake31826642017-10-11 22:47:08 -05001613 n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
Kevin Wolf690c7302015-03-19 13:33:32 +01001614
1615 if (ret & BDRV_BLOCK_ZERO) {
1616 s->status = BLK_ZERO;
1617 } else if (ret & BDRV_BLOCK_DATA) {
1618 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001619 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001620 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001621 }
1622
1623 s->sector_next_status = sector_num + n;
1624 }
1625
1626 n = MIN(n, s->sector_next_status - sector_num);
1627 if (s->status == BLK_DATA) {
1628 n = MIN(n, s->buf_sectors);
1629 }
1630
1631 /* We need to write complete clusters for compressed images, so if an
1632 * unallocated area is shorter than that, we must consider the whole
1633 * cluster allocated. */
1634 if (s->compressed) {
1635 if (n < s->cluster_sectors) {
1636 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1637 s->status = BLK_DATA;
1638 } else {
1639 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1640 }
1641 }
1642
1643 return n;
1644}
1645
Peter Lieven2d9187b2017-02-28 13:40:07 +01001646static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1647 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001648{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001649 int n, ret;
1650 QEMUIOVector qiov;
1651 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001652
Kevin Wolf690c7302015-03-19 13:33:32 +01001653 assert(nb_sectors <= s->buf_sectors);
1654 while (nb_sectors > 0) {
1655 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001656 int src_cur;
1657 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001658
1659 /* In the case of compression with multiple source files, we can get a
1660 * nb_sectors that spreads into the next part. So we must be able to
1661 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001662 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1663 blk = s->src[src_cur];
1664 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001665
Peter Lieven2d9187b2017-02-28 13:40:07 +01001666 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1667 iov.iov_base = buf;
1668 iov.iov_len = n << BDRV_SECTOR_BITS;
1669 qemu_iovec_init_external(&qiov, &iov, 1);
1670
1671 ret = blk_co_preadv(
1672 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1673 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001674 if (ret < 0) {
1675 return ret;
1676 }
1677
1678 sector_num += n;
1679 nb_sectors -= n;
1680 buf += n * BDRV_SECTOR_SIZE;
1681 }
1682
1683 return 0;
1684}
1685
Peter Lieven2d9187b2017-02-28 13:40:07 +01001686
1687static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1688 int nb_sectors, uint8_t *buf,
1689 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001690{
1691 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001692 QEMUIOVector qiov;
1693 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001694
1695 while (nb_sectors > 0) {
1696 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001697 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1698
Peter Lieven2d9187b2017-02-28 13:40:07 +01001699 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001700 case BLK_BACKING_FILE:
1701 /* If we have a backing file, leave clusters unallocated that are
1702 * unallocated in the source image, so that the backing file is
1703 * visible at the respective offset. */
1704 assert(s->target_has_backing);
1705 break;
1706
1707 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001708 /* If we're told to keep the target fully allocated (-S 0) or there
1709 * is real non-zero data, we must write it. Otherwise we can treat
1710 * it as zero sectors.
1711 * Compressed clusters need to be written as a whole, so in that
1712 * case we can only save the write if the buffer is completely
1713 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001714 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001715 (!s->compressed &&
1716 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1717 (s->compressed &&
1718 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001719 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001720 iov.iov_base = buf;
1721 iov.iov_len = n << BDRV_SECTOR_BITS;
1722 qemu_iovec_init_external(&qiov, &iov, 1);
1723
1724 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
Lidong Chendb933fb2017-04-27 10:58:27 +08001725 n << BDRV_SECTOR_BITS, &qiov, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001726 if (ret < 0) {
1727 return ret;
1728 }
1729 break;
1730 }
1731 /* fall-through */
1732
1733 case BLK_ZERO:
1734 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001735 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001736 break;
1737 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001738 ret = blk_co_pwrite_zeroes(s->target,
1739 sector_num << BDRV_SECTOR_BITS,
1740 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001741 if (ret < 0) {
1742 return ret;
1743 }
1744 break;
1745 }
1746
1747 sector_num += n;
1748 nb_sectors -= n;
1749 buf += n * BDRV_SECTOR_SIZE;
1750 }
1751
1752 return 0;
1753}
1754
Fam Zhengee5306d2018-06-01 17:26:48 +08001755static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num,
1756 int nb_sectors)
1757{
1758 int n, ret;
1759
1760 while (nb_sectors > 0) {
1761 BlockBackend *blk;
1762 int src_cur;
1763 int64_t bs_sectors, src_cur_offset;
1764 int64_t offset;
1765
1766 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1767 offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS;
1768 blk = s->src[src_cur];
1769 bs_sectors = s->src_sectors[src_cur];
1770
1771 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1772
1773 ret = blk_co_copy_range(blk, offset, s->target,
1774 sector_num << BDRV_SECTOR_BITS,
1775 n << BDRV_SECTOR_BITS, 0);
1776 if (ret < 0) {
1777 return ret;
1778 }
1779
1780 sector_num += n;
1781 nb_sectors -= n;
1782 }
1783 return 0;
1784}
1785
Peter Lieven2d9187b2017-02-28 13:40:07 +01001786static void coroutine_fn convert_co_do_copy(void *opaque)
1787{
1788 ImgConvertState *s = opaque;
1789 uint8_t *buf = NULL;
1790 int ret, i;
1791 int index = -1;
1792
1793 for (i = 0; i < s->num_coroutines; i++) {
1794 if (s->co[i] == qemu_coroutine_self()) {
1795 index = i;
1796 break;
1797 }
1798 }
1799 assert(index >= 0);
1800
1801 s->running_coroutines++;
1802 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1803
1804 while (1) {
1805 int n;
1806 int64_t sector_num;
1807 enum ImgConvertBlockStatus status;
Fam Zhengee5306d2018-06-01 17:26:48 +08001808 bool copy_range;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001809
1810 qemu_co_mutex_lock(&s->lock);
1811 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1812 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001813 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001814 }
1815 n = convert_iteration_sectors(s, s->sector_num);
1816 if (n < 0) {
1817 qemu_co_mutex_unlock(&s->lock);
1818 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001819 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001820 }
1821 /* save current sector and allocation status to local variables */
1822 sector_num = s->sector_num;
1823 status = s->status;
1824 if (!s->min_sparse && s->status == BLK_ZERO) {
1825 n = MIN(n, s->buf_sectors);
1826 }
1827 /* increment global sector counter so that other coroutines can
1828 * already continue reading beyond this request */
1829 s->sector_num += n;
1830 qemu_co_mutex_unlock(&s->lock);
1831
1832 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1833 s->allocated_done += n;
1834 qemu_progress_print(100.0 * s->allocated_done /
1835 s->allocated_sectors, 0);
1836 }
1837
Fam Zhengee5306d2018-06-01 17:26:48 +08001838retry:
1839 copy_range = s->copy_range && s->status == BLK_DATA;
1840 if (status == BLK_DATA && !copy_range) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001841 ret = convert_co_read(s, sector_num, n, buf);
1842 if (ret < 0) {
1843 error_report("error while reading sector %" PRId64
1844 ": %s", sector_num, strerror(-ret));
1845 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001846 }
1847 } else if (!s->min_sparse && status == BLK_ZERO) {
1848 status = BLK_DATA;
1849 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1850 }
1851
1852 if (s->wr_in_order) {
1853 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03001854 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001855 s->wait_sector_num[index] = sector_num;
1856 qemu_coroutine_yield();
1857 }
1858 s->wait_sector_num[index] = -1;
1859 }
1860
Anton Nefedovb91127e2017-04-26 11:33:15 +03001861 if (s->ret == -EINPROGRESS) {
Fam Zhengee5306d2018-06-01 17:26:48 +08001862 if (copy_range) {
1863 ret = convert_co_copy_range(s, sector_num, n);
1864 if (ret) {
1865 s->copy_range = false;
1866 goto retry;
1867 }
1868 } else {
1869 ret = convert_co_write(s, sector_num, n, buf, status);
1870 }
Anton Nefedovb91127e2017-04-26 11:33:15 +03001871 if (ret < 0) {
1872 error_report("error while writing sector %" PRId64
1873 ": %s", sector_num, strerror(-ret));
1874 s->ret = ret;
1875 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001876 }
1877
1878 if (s->wr_in_order) {
1879 /* reenter the coroutine that might have waited
1880 * for this write to complete */
1881 s->wr_offs = sector_num + n;
1882 for (i = 0; i < s->num_coroutines; i++) {
1883 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1884 /*
1885 * A -> B -> A cannot occur because A has
1886 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1887 * B will never enter A during this time window.
1888 */
1889 qemu_coroutine_enter(s->co[i]);
1890 break;
1891 }
1892 }
1893 }
1894 }
1895
Peter Lieven2d9187b2017-02-28 13:40:07 +01001896 qemu_vfree(buf);
1897 s->co[index] = NULL;
1898 s->running_coroutines--;
1899 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1900 /* the convert job finished successfully */
1901 s->ret = 0;
1902 }
1903}
1904
Kevin Wolf690c7302015-03-19 13:33:32 +01001905static int convert_do_copy(ImgConvertState *s)
1906{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001907 int ret, i, n;
1908 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001909
1910 /* Check whether we have zero initialisation or can get it efficiently */
1911 s->has_zero_init = s->min_sparse && !s->target_has_backing
1912 ? bdrv_has_zero_init(blk_bs(s->target))
1913 : false;
1914
1915 if (!s->has_zero_init && !s->target_has_backing &&
1916 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1917 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001918 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001919 if (ret == 0) {
1920 s->has_zero_init = true;
1921 }
1922 }
1923
1924 /* Allocate buffer for copied data. For compressed images, only one cluster
1925 * can be copied at a time. */
1926 if (s->compressed) {
1927 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1928 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001929 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001930 }
1931 s->buf_sectors = s->cluster_sectors;
1932 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001933
Kevin Wolf690c7302015-03-19 13:33:32 +01001934 while (sector_num < s->total_sectors) {
1935 n = convert_iteration_sectors(s, sector_num);
1936 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001937 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001938 }
Max Reitzaad15de2016-03-24 23:33:57 +01001939 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1940 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001941 s->allocated_sectors += n;
1942 }
1943 sector_num += n;
1944 }
1945
1946 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001947 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001948 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001949
Peter Lieven2d9187b2017-02-28 13:40:07 +01001950 qemu_co_mutex_init(&s->lock);
1951 for (i = 0; i < s->num_coroutines; i++) {
1952 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1953 s->wait_sector_num[i] = -1;
1954 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001955 }
1956
Anton Nefedovb91127e2017-04-26 11:33:15 +03001957 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001958 main_loop_wait(false);
1959 }
1960
1961 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001962 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001963 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001964 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001965 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001966 }
1967 }
1968
Peter Lieven2d9187b2017-02-28 13:40:07 +01001969 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001970}
1971
bellardea2384d2004-08-01 21:59:26 +00001972static int img_convert(int argc, char **argv)
1973{
Peter Lieven9fd77f92017-04-21 11:11:55 +02001974 int c, bs_i, flags, src_flags = 0;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001975 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02001976 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1977 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001978 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00001979 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001980 BlockDriverState *out_bs;
1981 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08001982 QemuOptsList *create_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001983 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02001984 Error *local_err = NULL;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001985 bool writethrough, src_writethrough, quiet = false, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001986 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001987 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08001988 bool force_share = false;
bellardea2384d2004-08-01 21:59:26 +00001989
Peter Lieven9fd77f92017-04-21 11:11:55 +02001990 ImgConvertState s = (ImgConvertState) {
1991 /* Need at least 4k of zeros for sparse detection */
1992 .min_sparse = 8,
Fam Zhengee5306d2018-06-01 17:26:48 +08001993 .copy_range = true,
Peter Lieven9fd77f92017-04-21 11:11:55 +02001994 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1995 .wr_in_order = true,
1996 .num_coroutines = 8,
1997 };
1998
bellardea2384d2004-08-01 21:59:26 +00001999 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002000 static const struct option long_options[] = {
2001 {"help", no_argument, 0, 'h'},
2002 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002003 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002004 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002005 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002006 {0, 0, 0, 0}
2007 };
Daniel P. Berrange6b4df542017-07-04 13:30:09 +01002008 c = getopt_long(argc, argv, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002009 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002010 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002011 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002012 }
bellardea2384d2004-08-01 21:59:26 +00002013 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002014 case ':':
2015 missing_argument(argv[optind - 1]);
2016 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002017 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002018 unrecognized_option(argv[optind - 1]);
2019 break;
bellardea2384d2004-08-01 21:59:26 +00002020 case 'h':
2021 help();
2022 break;
2023 case 'f':
2024 fmt = optarg;
2025 break;
2026 case 'O':
2027 out_fmt = optarg;
2028 break;
thsf58c7b32008-06-05 21:53:49 +00002029 case 'B':
2030 out_baseimg = optarg;
2031 break;
bellardea2384d2004-08-01 21:59:26 +00002032 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002033 s.compressed = true;
Fam Zhengee5306d2018-06-01 17:26:48 +08002034 s.copy_range = false;
bellardea2384d2004-08-01 21:59:26 +00002035 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002036 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01002037 if (!is_valid_option_list(optarg)) {
2038 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002039 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01002040 }
2041 if (!options) {
2042 options = g_strdup(optarg);
2043 } else {
2044 char *old_options = options;
2045 options = g_strdup_printf("%s,%s", options, optarg);
2046 g_free(old_options);
2047 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002048 break;
edison51ef6722010-09-21 19:58:41 -07002049 case 's':
2050 snapshot_name = optarg;
2051 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08002052 case 'l':
2053 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01002054 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2055 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08002056 if (!sn_opts) {
2057 error_report("Failed in parsing snapshot param '%s'",
2058 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002059 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002060 }
2061 } else {
2062 snapshot_name = optarg;
2063 }
2064 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002065 case 'S':
2066 {
2067 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002068
2069 sval = cvtnum(optarg);
2070 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02002071 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002072 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002073 }
2074
Peter Lieven9fd77f92017-04-21 11:11:55 +02002075 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Fam Zhengee5306d2018-06-01 17:26:48 +08002076 s.copy_range = false;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002077 break;
2078 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002079 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002080 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002081 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002082 case 't':
2083 cache = optarg;
2084 break;
Max Reitz40055952014-07-22 22:58:42 +02002085 case 'T':
2086 src_cache = optarg;
2087 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002088 case 'q':
2089 quiet = true;
2090 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002091 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002092 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002093 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002094 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002095 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2096 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002097 error_report("Invalid number of coroutines. Allowed number of"
2098 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002099 goto fail_getopt;
2100 }
2101 break;
2102 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002103 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002104 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002105 case 'U':
2106 force_share = true;
2107 break;
Max Reitz3258b912017-04-26 15:46:47 +02002108 case OPTION_OBJECT: {
2109 QemuOpts *object_opts;
2110 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2111 optarg, true);
2112 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002113 goto fail_getopt;
2114 }
2115 break;
Max Reitz3258b912017-04-26 15:46:47 +02002116 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002117 case OPTION_IMAGE_OPTS:
2118 image_opts = true;
2119 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002120 case OPTION_TARGET_IMAGE_OPTS:
2121 tgt_image_opts = true;
2122 break;
bellardea2384d2004-08-01 21:59:26 +00002123 }
2124 }
ths3b46e622007-09-17 08:09:54 +00002125
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002126 if (!out_fmt && !tgt_image_opts) {
2127 out_fmt = "raw";
2128 }
2129
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002130 if (qemu_opts_foreach(&qemu_object_opts,
2131 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002132 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002133 goto fail_getopt;
2134 }
2135
Peter Lieven9fd77f92017-04-21 11:11:55 +02002136 if (!s.wr_in_order && s.compressed) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002137 error_report("Out of order write and compress are mutually exclusive");
Peter Lieven9fd77f92017-04-21 11:11:55 +02002138 goto fail_getopt;
2139 }
2140
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002141 if (tgt_image_opts && !skip_create) {
2142 error_report("--target-image-opts requires use of -n flag");
2143 goto fail_getopt;
2144 }
2145
Peter Lieven9fd77f92017-04-21 11:11:55 +02002146 s.src_num = argc - optind - 1;
2147 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2148
2149 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002150 if (out_fmt) {
2151 ret = print_block_option_help(out_filename, out_fmt);
2152 goto fail_getopt;
2153 } else {
2154 error_report("Option help requires a format be specified");
2155 goto fail_getopt;
2156 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002157 }
2158
2159 if (s.src_num < 1) {
2160 error_report("Must specify image file name");
2161 goto fail_getopt;
2162 }
2163
2164
Peter Lieven9fd77f92017-04-21 11:11:55 +02002165 /* ret is still -EINVAL until here */
2166 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2167 if (ret < 0) {
2168 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002169 goto fail_getopt;
2170 }
2171
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002172 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002173 if (quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002174 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002175 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002176 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002177 qemu_progress_print(0, 100);
2178
Peter Lieven9fd77f92017-04-21 11:11:55 +02002179 s.src = g_new0(BlockBackend *, s.src_num);
2180 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002181
Peter Lieven9fd77f92017-04-21 11:11:55 +02002182 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2183 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Fam Zheng335e9932017-05-03 00:35:39 +08002184 fmt, src_flags, src_writethrough, quiet,
2185 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002186 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002187 ret = -1;
2188 goto out;
2189 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002190 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2191 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002192 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002193 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002194 ret = -1;
2195 goto out;
2196 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002197 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002198 }
bellardea2384d2004-08-01 21:59:26 +00002199
Wenchao Xiaef806542013-12-04 17:10:57 +08002200 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002201 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002202 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2203 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2204 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002205 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002206 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002207 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002208 ret = -1;
2209 goto out;
2210 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002211
Peter Lieven9fd77f92017-04-21 11:11:55 +02002212 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2213 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002214 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002215 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002216 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002217 ret = -1;
2218 goto out;
edison51ef6722010-09-21 19:58:41 -07002219 }
2220
Max Reitz2e024cd2015-02-11 09:58:46 -05002221 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002222 /* Find driver and parse its options */
2223 drv = bdrv_find_format(out_fmt);
2224 if (!drv) {
2225 error_report("Unknown file format '%s'", out_fmt);
2226 ret = -1;
2227 goto out;
2228 }
2229
2230 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2231 if (!proto_drv) {
2232 error_report_err(local_err);
2233 ret = -1;
2234 goto out;
2235 }
2236
Max Reitz2e024cd2015-02-11 09:58:46 -05002237 if (!drv->create_opts) {
2238 error_report("Format driver '%s' does not support image creation",
2239 drv->format_name);
2240 ret = -1;
2241 goto out;
2242 }
Max Reitzf75613c2014-12-02 18:32:46 +01002243
Max Reitz2e024cd2015-02-11 09:58:46 -05002244 if (!proto_drv->create_opts) {
2245 error_report("Protocol driver '%s' does not support image creation",
2246 proto_drv->format_name);
2247 ret = -1;
2248 goto out;
2249 }
Max Reitzf75613c2014-12-02 18:32:46 +01002250
Max Reitz2e024cd2015-02-11 09:58:46 -05002251 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2252 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002253
Max Reitz2e024cd2015-02-11 09:58:46 -05002254 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002255 if (options) {
2256 qemu_opts_do_parse(opts, options, NULL, &local_err);
2257 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002258 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002259 ret = -1;
2260 goto out;
2261 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002262 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002263
Peter Lieven9fd77f92017-04-21 11:11:55 +02002264 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002265 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002266 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2267 if (ret < 0) {
2268 goto out;
2269 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002270 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002271
Kevin Wolfa18953f2010-10-14 15:46:04 +02002272 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002273 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002274 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002275 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002276 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002277 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002278
Max Reitz48758a82017-04-26 15:46:48 +02002279 if (s.src_num > 1 && out_baseimg) {
2280 error_report("Having a backing file for the target makes no sense when "
2281 "concatenating multiple input images");
2282 ret = -1;
2283 goto out;
2284 }
2285
Kevin Wolfefa84d42009-05-18 16:42:12 +02002286 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002287 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002288 bool encryption =
2289 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002290 const char *encryptfmt =
2291 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
Chunyan Liu83d05212014-06-05 17:20:51 +08002292 const char *preallocation =
2293 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002294
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002295 if (drv && !drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002296 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002297 ret = -1;
2298 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002299 }
2300
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002301 if (encryption || encryptfmt) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002302 error_report("Compression and encryption not supported at "
2303 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002304 ret = -1;
2305 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002306 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002307
Chunyan Liu83d05212014-06-05 17:20:51 +08002308 if (preallocation
2309 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002310 {
2311 error_report("Compression and preallocation not supported at "
2312 "the same time");
2313 ret = -1;
2314 goto out;
2315 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002316 }
2317
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002318 if (!skip_create) {
2319 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002320 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002321 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002322 error_reportf_err(local_err, "%s: error while converting %s: ",
2323 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002324 goto out;
bellardea2384d2004-08-01 21:59:26 +00002325 }
2326 }
ths3b46e622007-09-17 08:09:54 +00002327
Peter Lieven9fd77f92017-04-21 11:11:55 +02002328 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002329 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002330 if (ret < 0) {
2331 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002332 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002333 }
2334
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002335 if (skip_create) {
2336 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2337 flags, writethrough, quiet, false);
2338 } else {
2339 /* TODO ultimately we should allow --target-image-opts
2340 * to be used even when -n is not given.
2341 * That has to wait for bdrv_create to be improved
2342 * to allow filenames in option syntax
2343 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01002344 s.target = img_open_new_file(out_filename, opts, out_fmt,
2345 flags, writethrough, quiet, false);
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002346 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002347 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002348 ret = -1;
2349 goto out;
2350 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002351 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002352
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002353 if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2354 error_report("Compression not supported for this file format");
2355 ret = -1;
2356 goto out;
2357 }
2358
Eric Blake5def6b82016-06-23 16:37:19 -06002359 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002360 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2361 * as maximum. */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002362 s.buf_sectors = MIN(32768,
2363 MAX(s.buf_sectors,
2364 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2365 out_bs->bl.pdiscard_alignment >>
2366 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002367
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002368 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002369 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002370 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002371 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002372 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002373 ret = -1;
2374 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002375 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002376 error_report("output file is smaller than input file");
2377 ret = -1;
2378 goto out;
2379 }
2380 }
2381
Peter Lieven24f833c2013-11-27 11:07:07 +01002382 ret = bdrv_get_info(out_bs, &bdi);
2383 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002384 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002385 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002386 goto out;
2387 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002388 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002389 s.compressed = s.compressed || bdi.needs_compressed_writes;
2390 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Peter Lieven24f833c2013-11-27 11:07:07 +01002391 }
2392
Peter Lieven9fd77f92017-04-21 11:11:55 +02002393 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002394out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002395 if (!ret) {
2396 qemu_progress_print(100, 0);
2397 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002398 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002399 qemu_opts_del(opts);
2400 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002401 qemu_opts_del(sn_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002402 blk_unref(s.target);
2403 if (s.src) {
2404 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2405 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002406 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002407 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002408 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002409 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002410fail_getopt:
2411 g_free(options);
2412
Peter Lieven9fd77f92017-04-21 11:11:55 +02002413 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002414}
2415
bellard57d1a2b2004-08-03 21:15:11 +00002416
bellardfaea38e2006-08-05 21:31:00 +00002417static void dump_snapshots(BlockDriverState *bs)
2418{
2419 QEMUSnapshotInfo *sn_tab, *sn;
2420 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002421
2422 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2423 if (nb_sns <= 0)
2424 return;
2425 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002426 bdrv_snapshot_dump(fprintf, stdout, NULL);
2427 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002428 for(i = 0; i < nb_sns; i++) {
2429 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002430 bdrv_snapshot_dump(fprintf, stdout, sn);
2431 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002432 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002433 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002434}
2435
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002436static void dump_json_image_info_list(ImageInfoList *list)
2437{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002438 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002439 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002440 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002441
2442 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2443 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002444 str = qobject_to_json_pretty(obj);
2445 assert(str != NULL);
2446 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002447 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002448 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002449 qobject_unref(str);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002450}
2451
Benoît Canetc054b3f2012-09-05 13:09:02 +02002452static void dump_json_image_info(ImageInfo *info)
2453{
Benoît Canetc054b3f2012-09-05 13:09:02 +02002454 QString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002455 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002456 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002457
2458 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2459 visit_complete(v, &obj);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002460 str = qobject_to_json_pretty(obj);
2461 assert(str != NULL);
2462 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002463 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002464 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002465 qobject_unref(str);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002466}
2467
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002468static void dump_human_image_info_list(ImageInfoList *list)
2469{
2470 ImageInfoList *elem;
2471 bool delim = false;
2472
2473 for (elem = list; elem; elem = elem->next) {
2474 if (delim) {
2475 printf("\n");
2476 }
2477 delim = true;
2478
Wenchao Xia5b917042013-05-25 11:09:45 +08002479 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002480 }
2481}
2482
2483static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2484{
2485 return strcmp(a, b) == 0;
2486}
2487
2488/**
2489 * Open an image file chain and return an ImageInfoList
2490 *
2491 * @filename: topmost image filename
2492 * @fmt: topmost image format (may be NULL to autodetect)
2493 * @chain: true - enumerate entire backing file chain
2494 * false - only topmost image file
2495 *
2496 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2497 * image file. If there was an error a message will have been printed to
2498 * stderr.
2499 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002500static ImageInfoList *collect_image_info_list(bool image_opts,
2501 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002502 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002503 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002504{
2505 ImageInfoList *head = NULL;
2506 ImageInfoList **last = &head;
2507 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002508 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002509
2510 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2511
2512 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002513 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002514 BlockDriverState *bs;
2515 ImageInfo *info;
2516 ImageInfoList *elem;
2517
2518 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2519 error_report("Backing file '%s' creates an infinite loop.",
2520 filename);
2521 goto err;
2522 }
2523 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2524
Max Reitzefaa7c42016-03-16 19:54:38 +01002525 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002526 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2527 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002528 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002529 goto err;
2530 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002531 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002532
Wenchao Xia43526ec2013-06-06 12:27:58 +08002533 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002534 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002535 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002536 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002537 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002538 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002539
2540 elem = g_new0(ImageInfoList, 1);
2541 elem->value = info;
2542 *last = elem;
2543 last = &elem->next;
2544
Markus Armbruster26f54e92014-10-07 13:59:04 +02002545 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002546
2547 filename = fmt = NULL;
2548 if (chain) {
2549 if (info->has_full_backing_filename) {
2550 filename = info->full_backing_filename;
2551 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002552 error_report("Could not determine absolute backing filename,"
2553 " but backing filename '%s' present",
2554 info->backing_filename);
2555 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002556 }
2557 if (info->has_backing_filename_format) {
2558 fmt = info->backing_filename_format;
2559 }
2560 }
2561 }
2562 g_hash_table_destroy(filenames);
2563 return head;
2564
2565err:
2566 qapi_free_ImageInfoList(head);
2567 g_hash_table_destroy(filenames);
2568 return NULL;
2569}
2570
Benoît Canetc054b3f2012-09-05 13:09:02 +02002571static int img_info(int argc, char **argv)
2572{
2573 int c;
2574 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002575 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002576 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002577 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002578 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002579 bool force_share = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002580
bellardea2384d2004-08-01 21:59:26 +00002581 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002582 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002583 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002584 int option_index = 0;
2585 static const struct option long_options[] = {
2586 {"help", no_argument, 0, 'h'},
2587 {"format", required_argument, 0, 'f'},
2588 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002589 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002590 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002591 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002592 {"force-share", no_argument, 0, 'U'},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002593 {0, 0, 0, 0}
2594 };
Fam Zheng335e9932017-05-03 00:35:39 +08002595 c = getopt_long(argc, argv, ":f:hU",
Benoît Canetc054b3f2012-09-05 13:09:02 +02002596 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002597 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002598 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002599 }
bellardea2384d2004-08-01 21:59:26 +00002600 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002601 case ':':
2602 missing_argument(argv[optind - 1]);
2603 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002604 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002605 unrecognized_option(argv[optind - 1]);
2606 break;
bellardea2384d2004-08-01 21:59:26 +00002607 case 'h':
2608 help();
2609 break;
2610 case 'f':
2611 fmt = optarg;
2612 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002613 case 'U':
2614 force_share = true;
2615 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002616 case OPTION_OUTPUT:
2617 output = optarg;
2618 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002619 case OPTION_BACKING_CHAIN:
2620 chain = true;
2621 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002622 case OPTION_OBJECT: {
2623 QemuOpts *opts;
2624 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2625 optarg, true);
2626 if (!opts) {
2627 return 1;
2628 }
2629 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002630 case OPTION_IMAGE_OPTS:
2631 image_opts = true;
2632 break;
bellardea2384d2004-08-01 21:59:26 +00002633 }
2634 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002635 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002636 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002637 }
bellardea2384d2004-08-01 21:59:26 +00002638 filename = argv[optind++];
2639
Benoît Canetc054b3f2012-09-05 13:09:02 +02002640 if (output && !strcmp(output, "json")) {
2641 output_format = OFORMAT_JSON;
2642 } else if (output && !strcmp(output, "human")) {
2643 output_format = OFORMAT_HUMAN;
2644 } else if (output) {
2645 error_report("--output must be used with human or json as argument.");
2646 return 1;
2647 }
2648
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002649 if (qemu_opts_foreach(&qemu_object_opts,
2650 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002651 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002652 return 1;
2653 }
2654
Fam Zheng335e9932017-05-03 00:35:39 +08002655 list = collect_image_info_list(image_opts, filename, fmt, chain,
2656 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002657 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002658 return 1;
2659 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002660
Benoît Canetc054b3f2012-09-05 13:09:02 +02002661 switch (output_format) {
2662 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002663 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002664 break;
2665 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002666 if (chain) {
2667 dump_json_image_info_list(list);
2668 } else {
2669 dump_json_image_info(list->value);
2670 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002671 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002672 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002673
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002674 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002675 return 0;
2676}
2677
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002678static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2679 MapEntry *next)
2680{
2681 switch (output_format) {
2682 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002683 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002684 error_report("File contains external, encrypted or compressed clusters.");
2685 exit(1);
2686 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002687 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002688 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002689 e->start, e->length,
2690 e->has_offset ? e->offset : 0,
2691 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002692 }
2693 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2694 * Modify the flags here to allow more coalescing.
2695 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002696 if (next && (!next->data || next->zero)) {
2697 next->data = false;
2698 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002699 }
2700 break;
2701 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002702 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2703 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002704 (e->start == 0 ? "[" : ",\n"),
2705 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002706 e->zero ? "true" : "false",
2707 e->data ? "true" : "false");
2708 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002709 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002710 }
2711 putchar('}');
2712
2713 if (!next) {
2714 printf("]\n");
2715 }
2716 break;
2717 }
2718}
2719
Eric Blake5e344dd2017-10-11 22:47:02 -05002720static int get_block_status(BlockDriverState *bs, int64_t offset,
2721 int64_t bytes, MapEntry *e)
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002722{
Eric Blake237d78f2017-10-11 22:47:03 -05002723 int ret;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002724 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002725 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002726 bool has_offset;
Eric Blake237d78f2017-10-11 22:47:03 -05002727 int64_t map;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002728
2729 /* As an optimization, we could cache the current range of unallocated
2730 * clusters in each file of the chain, and avoid querying the same
2731 * range repeatedly.
2732 */
2733
2734 depth = 0;
2735 for (;;) {
Eric Blake237d78f2017-10-11 22:47:03 -05002736 ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002737 if (ret < 0) {
2738 return ret;
2739 }
Eric Blake237d78f2017-10-11 22:47:03 -05002740 assert(bytes);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002741 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2742 break;
2743 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002744 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002745 if (bs == NULL) {
2746 ret = 0;
2747 break;
2748 }
2749
2750 depth++;
2751 }
2752
John Snow28756452016-02-05 13:12:33 -05002753 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2754
2755 *e = (MapEntry) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002756 .start = offset,
Eric Blake237d78f2017-10-11 22:47:03 -05002757 .length = bytes,
John Snow28756452016-02-05 13:12:33 -05002758 .data = !!(ret & BDRV_BLOCK_DATA),
2759 .zero = !!(ret & BDRV_BLOCK_ZERO),
Eric Blake237d78f2017-10-11 22:47:03 -05002760 .offset = map,
John Snow28756452016-02-05 13:12:33 -05002761 .has_offset = has_offset,
2762 .depth = depth,
2763 .has_filename = file && has_offset,
2764 .filename = file && has_offset ? file->filename : NULL,
2765 };
2766
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002767 return 0;
2768}
2769
Fam Zheng16b0d552016-01-26 11:59:02 +08002770static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2771{
2772 if (curr->length == 0) {
2773 return false;
2774 }
2775 if (curr->zero != next->zero ||
2776 curr->data != next->data ||
2777 curr->depth != next->depth ||
2778 curr->has_filename != next->has_filename ||
2779 curr->has_offset != next->has_offset) {
2780 return false;
2781 }
2782 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2783 return false;
2784 }
2785 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2786 return false;
2787 }
2788 return true;
2789}
2790
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002791static int img_map(int argc, char **argv)
2792{
2793 int c;
2794 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002795 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002796 BlockDriverState *bs;
2797 const char *filename, *fmt, *output;
2798 int64_t length;
2799 MapEntry curr = { .length = 0 }, next;
2800 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002801 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002802 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002803
2804 fmt = NULL;
2805 output = NULL;
2806 for (;;) {
2807 int option_index = 0;
2808 static const struct option long_options[] = {
2809 {"help", no_argument, 0, 'h'},
2810 {"format", required_argument, 0, 'f'},
2811 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002812 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002813 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002814 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002815 {0, 0, 0, 0}
2816 };
Fam Zheng335e9932017-05-03 00:35:39 +08002817 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002818 long_options, &option_index);
2819 if (c == -1) {
2820 break;
2821 }
2822 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002823 case ':':
2824 missing_argument(argv[optind - 1]);
2825 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002826 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002827 unrecognized_option(argv[optind - 1]);
2828 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002829 case 'h':
2830 help();
2831 break;
2832 case 'f':
2833 fmt = optarg;
2834 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002835 case 'U':
2836 force_share = true;
2837 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002838 case OPTION_OUTPUT:
2839 output = optarg;
2840 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002841 case OPTION_OBJECT: {
2842 QemuOpts *opts;
2843 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2844 optarg, true);
2845 if (!opts) {
2846 return 1;
2847 }
2848 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002849 case OPTION_IMAGE_OPTS:
2850 image_opts = true;
2851 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002852 }
2853 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002854 if (optind != argc - 1) {
2855 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002856 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002857 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002858
2859 if (output && !strcmp(output, "json")) {
2860 output_format = OFORMAT_JSON;
2861 } else if (output && !strcmp(output, "human")) {
2862 output_format = OFORMAT_HUMAN;
2863 } else if (output) {
2864 error_report("--output must be used with human or json as argument.");
2865 return 1;
2866 }
2867
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002868 if (qemu_opts_foreach(&qemu_object_opts,
2869 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002870 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002871 return 1;
2872 }
2873
Fam Zheng335e9932017-05-03 00:35:39 +08002874 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002875 if (!blk) {
2876 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002877 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002878 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002879
2880 if (output_format == OFORMAT_HUMAN) {
2881 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2882 }
2883
Max Reitzf1d3cd72015-02-05 13:58:18 -05002884 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002885 while (curr.start + curr.length < length) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002886 int64_t offset = curr.start + curr.length;
2887 int64_t n;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002888
2889 /* Probe up to 1 GiB at a time. */
Eric Blake5e344dd2017-10-11 22:47:02 -05002890 n = QEMU_ALIGN_DOWN(MIN(1 << 30, length - offset), BDRV_SECTOR_SIZE);
2891 ret = get_block_status(bs, offset, n, &next);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002892
2893 if (ret < 0) {
2894 error_report("Could not read file metadata: %s", strerror(-ret));
2895 goto out;
2896 }
2897
Fam Zheng16b0d552016-01-26 11:59:02 +08002898 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002899 curr.length += next.length;
2900 continue;
2901 }
2902
2903 if (curr.length > 0) {
2904 dump_map_entry(output_format, &curr, &next);
2905 }
2906 curr = next;
2907 }
2908
2909 dump_map_entry(output_format, &curr, NULL);
2910
2911out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002912 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002913 return ret < 0;
2914}
2915
aliguorif7b4a942009-01-07 17:40:15 +00002916#define SNAPSHOT_LIST 1
2917#define SNAPSHOT_CREATE 2
2918#define SNAPSHOT_APPLY 3
2919#define SNAPSHOT_DELETE 4
2920
Stuart Brady153859b2009-06-07 00:42:17 +01002921static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002922{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002923 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002924 BlockDriverState *bs;
2925 QEMUSnapshotInfo sn;
2926 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002927 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002928 int action = 0;
2929 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002930 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002931 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002932 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002933 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00002934
Kevin Wolfce099542016-03-15 13:01:04 +01002935 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002936 /* Parse commandline parameters */
2937 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002938 static const struct option long_options[] = {
2939 {"help", no_argument, 0, 'h'},
2940 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002941 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002942 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002943 {0, 0, 0, 0}
2944 };
Fam Zheng335e9932017-05-03 00:35:39 +08002945 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002946 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002947 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002948 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002949 }
aliguorif7b4a942009-01-07 17:40:15 +00002950 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002951 case ':':
2952 missing_argument(argv[optind - 1]);
2953 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002954 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002955 unrecognized_option(argv[optind - 1]);
2956 break;
aliguorif7b4a942009-01-07 17:40:15 +00002957 case 'h':
2958 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002959 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002960 case 'l':
2961 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002962 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002963 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002964 }
2965 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002966 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002967 break;
2968 case 'a':
2969 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002970 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002971 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002972 }
2973 action = SNAPSHOT_APPLY;
2974 snapshot_name = optarg;
2975 break;
2976 case 'c':
2977 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002978 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002979 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002980 }
2981 action = SNAPSHOT_CREATE;
2982 snapshot_name = optarg;
2983 break;
2984 case 'd':
2985 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002986 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002987 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002988 }
2989 action = SNAPSHOT_DELETE;
2990 snapshot_name = optarg;
2991 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002992 case 'q':
2993 quiet = true;
2994 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002995 case 'U':
2996 force_share = true;
2997 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002998 case OPTION_OBJECT: {
2999 QemuOpts *opts;
3000 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3001 optarg, true);
3002 if (!opts) {
3003 return 1;
3004 }
3005 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003006 case OPTION_IMAGE_OPTS:
3007 image_opts = true;
3008 break;
aliguorif7b4a942009-01-07 17:40:15 +00003009 }
3010 }
3011
Kevin Wolffc11eb22013-08-05 10:53:04 +02003012 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003013 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003014 }
aliguorif7b4a942009-01-07 17:40:15 +00003015 filename = argv[optind++];
3016
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003017 if (qemu_opts_foreach(&qemu_object_opts,
3018 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003019 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003020 return 1;
3021 }
3022
aliguorif7b4a942009-01-07 17:40:15 +00003023 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08003024 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
3025 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003026 if (!blk) {
3027 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003028 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003029 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00003030
3031 /* Perform the requested action */
3032 switch(action) {
3033 case SNAPSHOT_LIST:
3034 dump_snapshots(bs);
3035 break;
3036
3037 case SNAPSHOT_CREATE:
3038 memset(&sn, 0, sizeof(sn));
3039 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
3040
3041 qemu_gettimeofday(&tv);
3042 sn.date_sec = tv.tv_sec;
3043 sn.date_nsec = tv.tv_usec * 1000;
3044
3045 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003046 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003047 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003048 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003049 }
aliguorif7b4a942009-01-07 17:40:15 +00003050 break;
3051
3052 case SNAPSHOT_APPLY:
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01003053 ret = bdrv_snapshot_goto(bs, snapshot_name, &err);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003054 if (ret) {
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01003055 error_reportf_err(err, "Could not apply snapshot '%s': ",
3056 snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003057 }
aliguorif7b4a942009-01-07 17:40:15 +00003058 break;
3059
3060 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003061 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003062 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003063 error_reportf_err(err, "Could not delete snapshot '%s': ",
3064 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003065 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003066 }
aliguorif7b4a942009-01-07 17:40:15 +00003067 break;
3068 }
3069
3070 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003071 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003072 if (ret) {
3073 return 1;
3074 }
Stuart Brady153859b2009-06-07 00:42:17 +01003075 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003076}
3077
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003078static int img_rebase(int argc, char **argv)
3079{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003080 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003081 uint8_t *buf_old = NULL;
3082 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05003083 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003084 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003085 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3086 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003087 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003088 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003089 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003090 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003091 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003092 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003093 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003094
3095 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003096 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003097 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003098 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003099 out_baseimg = NULL;
3100 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003101 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003102 static const struct option long_options[] = {
3103 {"help", no_argument, 0, 'h'},
3104 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003105 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003106 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003107 {0, 0, 0, 0}
3108 };
Fam Zheng335e9932017-05-03 00:35:39 +08003109 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003110 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003111 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003112 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003113 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003114 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003115 case ':':
3116 missing_argument(argv[optind - 1]);
3117 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003118 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003119 unrecognized_option(argv[optind - 1]);
3120 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003121 case 'h':
3122 help();
3123 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003124 case 'f':
3125 fmt = optarg;
3126 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003127 case 'F':
3128 out_basefmt = optarg;
3129 break;
3130 case 'b':
3131 out_baseimg = optarg;
3132 break;
3133 case 'u':
3134 unsafe = 1;
3135 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003136 case 'p':
3137 progress = 1;
3138 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003139 case 't':
3140 cache = optarg;
3141 break;
Max Reitz40055952014-07-22 22:58:42 +02003142 case 'T':
3143 src_cache = optarg;
3144 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003145 case 'q':
3146 quiet = true;
3147 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003148 case OPTION_OBJECT: {
3149 QemuOpts *opts;
3150 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3151 optarg, true);
3152 if (!opts) {
3153 return 1;
3154 }
3155 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003156 case OPTION_IMAGE_OPTS:
3157 image_opts = true;
3158 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003159 case 'U':
3160 force_share = true;
3161 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003162 }
3163 }
3164
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003165 if (quiet) {
3166 progress = 0;
3167 }
3168
Fam Zhengac1307a2014-04-22 13:36:11 +08003169 if (optind != argc - 1) {
3170 error_exit("Expecting one image file name");
3171 }
3172 if (!unsafe && !out_baseimg) {
3173 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003174 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003175 filename = argv[optind++];
3176
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003177 if (qemu_opts_foreach(&qemu_object_opts,
3178 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003179 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003180 return 1;
3181 }
3182
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003183 qemu_progress_init(progress, 2.0);
3184 qemu_progress_print(0, 100);
3185
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003186 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003187 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003188 if (ret < 0) {
3189 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003190 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003191 }
3192
Kevin Wolfce099542016-03-15 13:01:04 +01003193 src_flags = 0;
3194 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003195 if (ret < 0) {
3196 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003197 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003198 }
3199
Kevin Wolfce099542016-03-15 13:01:04 +01003200 /* The source files are opened read-only, don't care about WCE */
3201 assert((src_flags & BDRV_O_RDWR) == 0);
3202 (void) src_writethrough;
3203
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003204 /*
3205 * Open the images.
3206 *
3207 * Ignore the old backing file for unsafe rebase in case we want to correct
3208 * the reference to a renamed or moved backing file.
3209 */
Fam Zheng335e9932017-05-03 00:35:39 +08003210 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3211 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003212 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003213 ret = -1;
3214 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003215 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003216 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003217
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003218 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003219 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003220 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003221 ret = -1;
3222 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003223 }
3224 }
3225
3226 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003227 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003228 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003229 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003230
Max Reitz644483d2015-02-05 13:58:17 -05003231 if (bs->backing_format[0] != '\0') {
3232 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003233 qdict_put_str(options, "driver", bs->backing_format);
Max Reitz644483d2015-02-05 13:58:17 -05003234 }
3235
Fam Zheng335e9932017-05-03 00:35:39 +08003236 if (force_share) {
3237 if (!options) {
3238 options = qdict_new();
3239 }
Eric Blake579cf1d2017-05-15 14:54:39 -05003240 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +08003241 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003242 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003243 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003244 options, src_flags, &local_err);
3245 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003246 error_reportf_err(local_err,
3247 "Could not open old backing file '%s': ",
3248 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003249 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003250 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003251 }
Max Reitz644483d2015-02-05 13:58:17 -05003252
Alex Bligha6166732012-10-16 13:46:18 +01003253 if (out_baseimg[0]) {
Fam Zheng335e9932017-05-03 00:35:39 +08003254 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003255 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003256 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003257 }
3258 if (force_share) {
3259 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003260 }
3261
Max Reitzefaa7c42016-03-16 19:54:38 +01003262 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003263 options, src_flags, &local_err);
3264 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003265 error_reportf_err(local_err,
3266 "Could not open new backing file '%s': ",
3267 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003268 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003269 goto out;
3270 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003271 }
3272 }
3273
3274 /*
3275 * Check each unallocated cluster in the COW file. If it is unallocated,
3276 * accesses go to the backing file. We must therefore compare this cluster
3277 * in the old and new backing file, and if they differ we need to copy it
3278 * from the old backing file into the COW file.
3279 *
3280 * If qemu-img crashes during this step, no harm is done. The content of
3281 * the image is the same as the original one at any time.
3282 */
3283 if (!unsafe) {
Eric Blake41536282017-10-11 22:47:15 -05003284 int64_t size;
3285 int64_t old_backing_size;
3286 int64_t new_backing_size = 0;
3287 uint64_t offset;
3288 int64_t n;
Kevin Wolf1f710492012-10-12 14:29:18 +02003289 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003290
Max Reitzf1d3cd72015-02-05 13:58:18 -05003291 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3292 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003293
Eric Blake41536282017-10-11 22:47:15 -05003294 size = blk_getlength(blk);
3295 if (size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003296 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003297 filename, strerror(-size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003298 ret = -1;
3299 goto out;
3300 }
Eric Blake41536282017-10-11 22:47:15 -05003301 old_backing_size = blk_getlength(blk_old_backing);
3302 if (old_backing_size < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003303 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003304
3305 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3306 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003307 backing_name, strerror(-old_backing_size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003308 ret = -1;
3309 goto out;
3310 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003311 if (blk_new_backing) {
Eric Blake41536282017-10-11 22:47:15 -05003312 new_backing_size = blk_getlength(blk_new_backing);
3313 if (new_backing_size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003314 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003315 out_baseimg, strerror(-new_backing_size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003316 ret = -1;
3317 goto out;
3318 }
Alex Bligha6166732012-10-16 13:46:18 +01003319 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003320
Eric Blake41536282017-10-11 22:47:15 -05003321 if (size != 0) {
3322 local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE));
Kevin Wolf1f710492012-10-12 14:29:18 +02003323 }
3324
Eric Blake41536282017-10-11 22:47:15 -05003325 for (offset = 0; offset < size; offset += n) {
3326 /* How many bytes can we handle with the next read? */
3327 n = MIN(IO_BUF_SIZE, size - offset);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003328
3329 /* If the cluster is allocated, we don't need to take action */
Eric Blake41536282017-10-11 22:47:15 -05003330 ret = bdrv_is_allocated(bs, offset, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003331 if (ret < 0) {
3332 error_report("error while reading image metadata: %s",
3333 strerror(-ret));
3334 goto out;
3335 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003336 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003337 continue;
3338 }
3339
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003340 /*
3341 * Read old and new backing file and take into consideration that
3342 * backing files may be smaller than the COW image.
3343 */
Eric Blake41536282017-10-11 22:47:15 -05003344 if (offset >= old_backing_size) {
3345 memset(buf_old, 0, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003346 } else {
Eric Blake41536282017-10-11 22:47:15 -05003347 if (offset + n > old_backing_size) {
3348 n = old_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003349 }
3350
Eric Blake41536282017-10-11 22:47:15 -05003351 ret = blk_pread(blk_old_backing, offset, buf_old, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003352 if (ret < 0) {
3353 error_report("error while reading from old backing file");
3354 goto out;
3355 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003356 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003357
Eric Blake41536282017-10-11 22:47:15 -05003358 if (offset >= new_backing_size || !blk_new_backing) {
3359 memset(buf_new, 0, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003360 } else {
Eric Blake41536282017-10-11 22:47:15 -05003361 if (offset + n > new_backing_size) {
3362 n = new_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003363 }
3364
Eric Blake41536282017-10-11 22:47:15 -05003365 ret = blk_pread(blk_new_backing, offset, buf_new, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003366 if (ret < 0) {
3367 error_report("error while reading from new backing file");
3368 goto out;
3369 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003370 }
3371
3372 /* If they differ, we need to write to the COW file */
3373 uint64_t written = 0;
3374
Eric Blake41536282017-10-11 22:47:15 -05003375 while (written < n) {
Eric Blakedc61cd32017-10-11 22:47:14 -05003376 int64_t pnum;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003377
Eric Blake41536282017-10-11 22:47:15 -05003378 if (compare_buffers(buf_old + written, buf_new + written,
3379 n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003380 {
Eric Blake41536282017-10-11 22:47:15 -05003381 ret = blk_pwrite(blk, offset + written,
Eric Blakedc61cd32017-10-11 22:47:14 -05003382 buf_old + written, pnum, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003383 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003384 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003385 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003386 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003387 }
3388 }
3389
3390 written += pnum;
3391 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003392 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003393 }
3394 }
3395
3396 /*
3397 * Change the backing file. All clusters that are different from the old
3398 * backing file are overwritten in the COW file now, so the visible content
3399 * doesn't change when we switch the backing file.
3400 */
Alex Bligha6166732012-10-16 13:46:18 +01003401 if (out_baseimg && *out_baseimg) {
3402 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3403 } else {
3404 ret = bdrv_change_backing_file(bs, NULL, NULL);
3405 }
3406
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003407 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003408 error_report("Could not change the backing file to '%s': No "
3409 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003410 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003411 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003412 out_baseimg, strerror(-ret));
3413 }
3414
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003415 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003416 /*
3417 * TODO At this point it is possible to check if any clusters that are
3418 * allocated in the COW file are the same in the backing file. If so, they
3419 * could be dropped from the COW file. Don't do this before switching the
3420 * backing file, in case of a crash this would lead to corruption.
3421 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003422out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003423 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003424 /* Cleanup */
3425 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003426 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003427 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003428 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003429 qemu_vfree(buf_old);
3430 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003431
Markus Armbruster26f54e92014-10-07 13:59:04 +02003432 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003433 if (ret) {
3434 return 1;
3435 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003436 return 0;
3437}
3438
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003439static int img_resize(int argc, char **argv)
3440{
Markus Armbruster6750e792015-02-12 17:43:08 +01003441 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003442 int c, ret, relative;
3443 const char *filename, *fmt, *size;
Max Reitz5279b302018-04-21 18:39:57 +02003444 int64_t n, total_size, current_size, new_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003445 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003446 BlockBackend *blk = NULL;
Max Reitzdc5f6902017-06-13 22:20:55 +02003447 PreallocMode prealloc = PREALLOC_MODE_OFF;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003448 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003449
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003450 static QemuOptsList resize_options = {
3451 .name = "resize_options",
3452 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3453 .desc = {
3454 {
3455 .name = BLOCK_OPT_SIZE,
3456 .type = QEMU_OPT_SIZE,
3457 .help = "Virtual disk size"
3458 }, {
3459 /* end of list */
3460 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003461 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003462 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003463 bool image_opts = false;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003464 bool shrink = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003465
Kevin Wolfe80fec72011-04-29 10:58:12 +02003466 /* Remove size from argv manually so that negative numbers are not treated
3467 * as options by getopt. */
3468 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003469 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003470 return 1;
3471 }
3472
3473 size = argv[--argc];
3474
3475 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003476 fmt = NULL;
3477 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003478 static const struct option long_options[] = {
3479 {"help", no_argument, 0, 'h'},
3480 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003481 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Max Reitzdc5f6902017-06-13 22:20:55 +02003482 {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003483 {"shrink", no_argument, 0, OPTION_SHRINK},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003484 {0, 0, 0, 0}
3485 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003486 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003487 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003488 if (c == -1) {
3489 break;
3490 }
3491 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003492 case ':':
3493 missing_argument(argv[optind - 1]);
3494 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003495 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003496 unrecognized_option(argv[optind - 1]);
3497 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003498 case 'h':
3499 help();
3500 break;
3501 case 'f':
3502 fmt = optarg;
3503 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003504 case 'q':
3505 quiet = true;
3506 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003507 case OPTION_OBJECT: {
3508 QemuOpts *opts;
3509 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3510 optarg, true);
3511 if (!opts) {
3512 return 1;
3513 }
3514 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003515 case OPTION_IMAGE_OPTS:
3516 image_opts = true;
3517 break;
Max Reitzdc5f6902017-06-13 22:20:55 +02003518 case OPTION_PREALLOCATION:
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02003519 prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
Markus Armbruster06c60b62017-08-24 10:45:57 +02003520 PREALLOC_MODE__MAX, NULL);
Max Reitzdc5f6902017-06-13 22:20:55 +02003521 if (prealloc == PREALLOC_MODE__MAX) {
3522 error_report("Invalid preallocation mode '%s'", optarg);
3523 return 1;
3524 }
3525 break;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003526 case OPTION_SHRINK:
3527 shrink = true;
3528 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003529 }
3530 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003531 if (optind != argc - 1) {
Max Reitzbe8fbd42018-02-05 17:27:45 +01003532 error_exit("Expecting image file name and size");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003533 }
3534 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003535
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003536 if (qemu_opts_foreach(&qemu_object_opts,
3537 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003538 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003539 return 1;
3540 }
3541
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003542 /* Choose grow, shrink, or absolute resize mode */
3543 switch (size[0]) {
3544 case '+':
3545 relative = 1;
3546 size++;
3547 break;
3548 case '-':
3549 relative = -1;
3550 size++;
3551 break;
3552 default:
3553 relative = 0;
3554 break;
3555 }
3556
3557 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003558 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003559 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003560 if (err) {
3561 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003562 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003563 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003564 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003565 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003566 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3567 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003568
Max Reitzefaa7c42016-03-16 19:54:38 +01003569 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003570 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3571 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003572 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003573 ret = -1;
3574 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003575 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003576
Max Reitzdc5f6902017-06-13 22:20:55 +02003577 current_size = blk_getlength(blk);
3578 if (current_size < 0) {
3579 error_report("Failed to inquire current image length: %s",
3580 strerror(-current_size));
3581 ret = -1;
3582 goto out;
3583 }
3584
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003585 if (relative) {
Max Reitzdc5f6902017-06-13 22:20:55 +02003586 total_size = current_size + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003587 } else {
3588 total_size = n;
3589 }
3590 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003591 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003592 ret = -1;
3593 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003594 }
3595
Max Reitzdc5f6902017-06-13 22:20:55 +02003596 if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
3597 error_report("Preallocation can only be used for growing images");
3598 ret = -1;
3599 goto out;
3600 }
3601
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003602 if (total_size < current_size && !shrink) {
3603 warn_report("Shrinking an image will delete all data beyond the "
3604 "shrunken image's end. Before performing such an "
3605 "operation, make sure there is no important data there.");
3606
3607 if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) {
3608 error_report(
3609 "Use the --shrink option to perform a shrink operation.");
3610 ret = -1;
3611 goto out;
3612 } else {
3613 warn_report("Using the --shrink option will suppress this message. "
3614 "Note that future versions of qemu-img may refuse to "
3615 "shrink images without this option.");
3616 }
3617 }
3618
Max Reitzdc5f6902017-06-13 22:20:55 +02003619 ret = blk_truncate(blk, total_size, prealloc, &err);
Max Reitz5279b302018-04-21 18:39:57 +02003620 if (ret < 0) {
Max Reitzed3d2ec2017-03-28 22:51:27 +02003621 error_report_err(err);
Max Reitz5279b302018-04-21 18:39:57 +02003622 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003623 }
Max Reitz5279b302018-04-21 18:39:57 +02003624
3625 new_size = blk_getlength(blk);
3626 if (new_size < 0) {
3627 error_report("Failed to verify truncated image length: %s",
3628 strerror(-new_size));
3629 ret = -1;
3630 goto out;
3631 }
3632
3633 /* Some block drivers implement a truncation method, but only so
3634 * the user can cause qemu to refresh the image's size from disk.
3635 * The idea is that the user resizes the image outside of qemu and
3636 * then invokes block_resize to inform qemu about it.
3637 * (This includes iscsi and file-posix for device files.)
3638 * Of course, that is not the behavior someone invoking
3639 * qemu-img resize would find useful, so we catch that behavior
3640 * here and tell the user. */
3641 if (new_size != total_size && new_size == current_size) {
3642 error_report("Image was not resized; resizing may not be supported "
3643 "for this image");
3644 ret = -1;
3645 goto out;
3646 }
3647
3648 if (new_size != total_size) {
3649 warn_report("Image should have been resized to %" PRIi64
3650 " bytes, but was resized to %" PRIi64 " bytes",
3651 total_size, new_size);
3652 }
3653
3654 qprintf(quiet, "Image resized.\n");
3655
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003656out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003657 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003658 if (ret) {
3659 return 1;
3660 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003661 return 0;
3662}
3663
Max Reitz76a3a342014-10-27 11:12:51 +01003664static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003665 int64_t offset, int64_t total_work_size,
3666 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003667{
3668 qemu_progress_print(100.f * offset / total_work_size, 0);
3669}
3670
Max Reitz51641352018-05-09 23:00:20 +02003671static int print_amend_option_help(const char *format)
3672{
3673 BlockDriver *drv;
3674
3675 /* Find driver and parse its options */
3676 drv = bdrv_find_format(format);
3677 if (!drv) {
3678 error_report("Unknown file format '%s'", format);
3679 return 1;
3680 }
3681
3682 if (!drv->bdrv_amend_options) {
3683 error_report("Format driver '%s' does not support option amendment",
3684 format);
3685 return 1;
3686 }
3687
3688 /* Every driver supporting amendment must have create_opts */
3689 assert(drv->create_opts);
3690
3691 printf("Creation options for '%s':\n", format);
3692 qemu_opts_print_help(drv->create_opts);
3693 printf("\nNote that not all of these options may be amendable.\n");
3694 return 0;
3695}
3696
Max Reitz6f176b42013-09-03 10:09:50 +02003697static int img_amend(int argc, char **argv)
3698{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003699 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003700 int c, ret = 0;
3701 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003702 QemuOptsList *create_opts = NULL;
3703 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003704 const char *fmt = NULL, *filename, *cache;
3705 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003706 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003707 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003708 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003709 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003710 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003711
Max Reitzbd39e6e2014-07-22 22:58:43 +02003712 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003713 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003714 static const struct option long_options[] = {
3715 {"help", no_argument, 0, 'h'},
3716 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003717 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003718 {0, 0, 0, 0}
3719 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003720 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003721 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003722 if (c == -1) {
3723 break;
3724 }
3725
3726 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003727 case ':':
3728 missing_argument(argv[optind - 1]);
3729 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003730 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003731 unrecognized_option(argv[optind - 1]);
3732 break;
3733 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003734 help();
3735 break;
3736 case 'o':
3737 if (!is_valid_option_list(optarg)) {
3738 error_report("Invalid option list: %s", optarg);
3739 ret = -1;
3740 goto out_no_progress;
3741 }
3742 if (!options) {
3743 options = g_strdup(optarg);
3744 } else {
3745 char *old_options = options;
3746 options = g_strdup_printf("%s,%s", options, optarg);
3747 g_free(old_options);
3748 }
3749 break;
3750 case 'f':
3751 fmt = optarg;
3752 break;
3753 case 't':
3754 cache = optarg;
3755 break;
3756 case 'p':
3757 progress = true;
3758 break;
3759 case 'q':
3760 quiet = true;
3761 break;
3762 case OPTION_OBJECT:
3763 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3764 optarg, true);
3765 if (!opts) {
3766 ret = -1;
3767 goto out_no_progress;
3768 }
3769 break;
3770 case OPTION_IMAGE_OPTS:
3771 image_opts = true;
3772 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003773 }
3774 }
3775
Max Reitz6f176b42013-09-03 10:09:50 +02003776 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003777 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003778 }
3779
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003780 if (qemu_opts_foreach(&qemu_object_opts,
3781 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003782 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003783 ret = -1;
3784 goto out_no_progress;
3785 }
3786
Max Reitz76a3a342014-10-27 11:12:51 +01003787 if (quiet) {
3788 progress = false;
3789 }
3790 qemu_progress_init(progress, 1.0);
3791
Kevin Wolfa283cb62014-02-21 16:24:07 +01003792 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3793 if (fmt && has_help_option(options)) {
3794 /* If a format is explicitly specified (and possibly no filename is
3795 * given), print option help here */
Max Reitz51641352018-05-09 23:00:20 +02003796 ret = print_amend_option_help(fmt);
Kevin Wolfa283cb62014-02-21 16:24:07 +01003797 goto out;
3798 }
3799
3800 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003801 error_report("Expecting one image file name");
3802 ret = -1;
3803 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003804 }
Max Reitz6f176b42013-09-03 10:09:50 +02003805
Kevin Wolfce099542016-03-15 13:01:04 +01003806 flags = BDRV_O_RDWR;
3807 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003808 if (ret < 0) {
3809 error_report("Invalid cache option: %s", cache);
3810 goto out;
3811 }
3812
Fam Zheng335e9932017-05-03 00:35:39 +08003813 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3814 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003815 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003816 ret = -1;
3817 goto out;
3818 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003819 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003820
3821 fmt = bs->drv->format_name;
3822
Kevin Wolf626f84f2014-02-21 16:24:06 +01003823 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003824 /* If the format was auto-detected, print option help here */
Max Reitz51641352018-05-09 23:00:20 +02003825 ret = print_amend_option_help(fmt);
Max Reitz6f176b42013-09-03 10:09:50 +02003826 goto out;
3827 }
3828
Max Reitz1f996682018-05-09 23:00:17 +02003829 if (!bs->drv->bdrv_amend_options) {
3830 error_report("Format driver '%s' does not support option amendment",
Max Reitzb2439d22014-12-02 18:32:47 +01003831 fmt);
3832 ret = -1;
3833 goto out;
3834 }
3835
Max Reitz1f996682018-05-09 23:00:17 +02003836 /* Every driver supporting amendment must have create_opts */
3837 assert(bs->drv->create_opts);
3838
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003839 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003840 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003841 qemu_opts_do_parse(opts, options, NULL, &err);
3842 if (err) {
3843 error_report_err(err);
3844 ret = -1;
3845 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003846 }
3847
Max Reitz76a3a342014-10-27 11:12:51 +01003848 /* In case the driver does not call amend_status_cb() */
3849 qemu_progress_print(0.f, 0);
Max Reitzd1402b52018-05-09 23:00:18 +02003850 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, &err);
Max Reitz76a3a342014-10-27 11:12:51 +01003851 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003852 if (ret < 0) {
Max Reitzd1402b52018-05-09 23:00:18 +02003853 error_report_err(err);
Max Reitz6f176b42013-09-03 10:09:50 +02003854 goto out;
3855 }
3856
3857out:
Max Reitz76a3a342014-10-27 11:12:51 +01003858 qemu_progress_end();
3859
Max Reitze814dff2015-08-20 16:00:38 -07003860out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003861 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003862 qemu_opts_del(opts);
3863 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003864 g_free(options);
3865
Max Reitz6f176b42013-09-03 10:09:50 +02003866 if (ret) {
3867 return 1;
3868 }
3869 return 0;
3870}
3871
Kevin Wolfb6133b82014-08-05 14:17:13 +02003872typedef struct BenchData {
3873 BlockBackend *blk;
3874 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003875 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003876 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003877 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003878 int nrreq;
3879 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003880 int flush_interval;
3881 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003882 uint8_t *buf;
3883 QEMUIOVector *qiov;
3884
3885 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003886 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003887 uint64_t offset;
3888} BenchData;
3889
Kevin Wolf55d539c2016-06-03 13:59:41 +02003890static void bench_undrained_flush_cb(void *opaque, int ret)
3891{
3892 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003893 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003894 exit(EXIT_FAILURE);
3895 }
3896}
3897
Kevin Wolfb6133b82014-08-05 14:17:13 +02003898static void bench_cb(void *opaque, int ret)
3899{
3900 BenchData *b = opaque;
3901 BlockAIOCB *acb;
3902
3903 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003904 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003905 exit(EXIT_FAILURE);
3906 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003907
3908 if (b->in_flush) {
3909 /* Just finished a flush with drained queue: Start next requests */
3910 assert(b->in_flight == 0);
3911 b->in_flush = false;
3912 } else if (b->in_flight > 0) {
3913 int remaining = b->n - b->in_flight;
3914
Kevin Wolfb6133b82014-08-05 14:17:13 +02003915 b->n--;
3916 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003917
3918 /* Time for flush? Drain queue if requested, then flush */
3919 if (b->flush_interval && remaining % b->flush_interval == 0) {
3920 if (!b->in_flight || !b->drain_on_flush) {
3921 BlockCompletionFunc *cb;
3922
3923 if (b->drain_on_flush) {
3924 b->in_flush = true;
3925 cb = bench_cb;
3926 } else {
3927 cb = bench_undrained_flush_cb;
3928 }
3929
3930 acb = blk_aio_flush(b->blk, cb, b);
3931 if (!acb) {
3932 error_report("Failed to issue flush request");
3933 exit(EXIT_FAILURE);
3934 }
3935 }
3936 if (b->drain_on_flush) {
3937 return;
3938 }
3939 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003940 }
3941
3942 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003943 int64_t offset = b->offset;
3944 /* blk_aio_* might look for completed I/Os and kick bench_cb
3945 * again, so make sure this operation is counted by in_flight
3946 * and b->offset is ready for the next submission.
3947 */
3948 b->in_flight++;
3949 b->offset += b->step;
3950 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003951 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003952 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003953 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003954 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003955 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003956 if (!acb) {
3957 error_report("Failed to issue request");
3958 exit(EXIT_FAILURE);
3959 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003960 }
3961}
3962
3963static int img_bench(int argc, char **argv)
3964{
3965 int c, ret = 0;
3966 const char *fmt = NULL, *filename;
3967 bool quiet = false;
3968 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003969 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003970 int count = 75000;
3971 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003972 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003973 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003974 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003975 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003976 int flush_interval = 0;
3977 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003978 int64_t image_size;
3979 BlockBackend *blk = NULL;
3980 BenchData data = {};
3981 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003982 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003983 struct timeval t1, t2;
3984 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08003985 bool force_share = false;
Fam Zheng79d46582018-01-16 14:08:58 +08003986 size_t buf_size;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003987
3988 for (;;) {
3989 static const struct option long_options[] = {
3990 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003991 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003992 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003993 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003994 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08003995 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003996 {0, 0, 0, 0}
3997 };
Fam Zheng335e9932017-05-03 00:35:39 +08003998 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003999 if (c == -1) {
4000 break;
4001 }
4002
4003 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004004 case ':':
4005 missing_argument(argv[optind - 1]);
4006 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004007 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004008 unrecognized_option(argv[optind - 1]);
4009 break;
4010 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02004011 help();
4012 break;
4013 case 'c':
4014 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004015 unsigned long res;
4016
4017 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02004018 error_report("Invalid request count specified");
4019 return 1;
4020 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004021 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004022 break;
4023 }
4024 case 'd':
4025 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004026 unsigned long res;
4027
4028 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02004029 error_report("Invalid queue depth specified");
4030 return 1;
4031 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004032 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004033 break;
4034 }
4035 case 'f':
4036 fmt = optarg;
4037 break;
4038 case 'n':
4039 flags |= BDRV_O_NATIVE_AIO;
4040 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02004041 case 'o':
4042 {
Markus Armbruster606caa02017-02-21 21:14:04 +01004043 offset = cvtnum(optarg);
4044 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02004045 error_report("Invalid offset specified");
4046 return 1;
4047 }
4048 break;
4049 }
4050 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004051 case 'q':
4052 quiet = true;
4053 break;
4054 case 's':
4055 {
4056 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004057
Markus Armbruster606caa02017-02-21 21:14:04 +01004058 sval = cvtnum(optarg);
4059 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02004060 error_report("Invalid buffer size specified");
4061 return 1;
4062 }
4063
4064 bufsize = sval;
4065 break;
4066 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02004067 case 'S':
4068 {
4069 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02004070
Markus Armbruster606caa02017-02-21 21:14:04 +01004071 sval = cvtnum(optarg);
4072 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02004073 error_report("Invalid step size specified");
4074 return 1;
4075 }
4076
4077 step = sval;
4078 break;
4079 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004080 case 't':
4081 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
4082 if (ret < 0) {
4083 error_report("Invalid cache mode");
4084 ret = -1;
4085 goto out;
4086 }
4087 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004088 case 'w':
4089 flags |= BDRV_O_RDWR;
4090 is_write = true;
4091 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004092 case 'U':
4093 force_share = true;
4094 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004095 case OPTION_PATTERN:
4096 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004097 unsigned long res;
4098
4099 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004100 error_report("Invalid pattern byte specified");
4101 return 1;
4102 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004103 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004104 break;
4105 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02004106 case OPTION_FLUSH_INTERVAL:
4107 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004108 unsigned long res;
4109
4110 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004111 error_report("Invalid flush interval specified");
4112 return 1;
4113 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004114 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004115 break;
4116 }
4117 case OPTION_NO_DRAIN:
4118 drain_on_flush = false;
4119 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004120 case OPTION_IMAGE_OPTS:
4121 image_opts = true;
4122 break;
4123 }
4124 }
4125
4126 if (optind != argc - 1) {
4127 error_exit("Expecting one image file name");
4128 }
4129 filename = argv[argc - 1];
4130
Kevin Wolf55d539c2016-06-03 13:59:41 +02004131 if (!is_write && flush_interval) {
4132 error_report("--flush-interval is only available in write tests");
4133 ret = -1;
4134 goto out;
4135 }
4136 if (flush_interval && flush_interval < depth) {
4137 error_report("Flush interval can't be smaller than depth");
4138 ret = -1;
4139 goto out;
4140 }
4141
Fam Zheng335e9932017-05-03 00:35:39 +08004142 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4143 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004144 if (!blk) {
4145 ret = -1;
4146 goto out;
4147 }
4148
4149 image_size = blk_getlength(blk);
4150 if (image_size < 0) {
4151 ret = image_size;
4152 goto out;
4153 }
4154
4155 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004156 .blk = blk,
4157 .image_size = image_size,
4158 .bufsize = bufsize,
4159 .step = step ?: bufsize,
4160 .nrreq = depth,
4161 .n = count,
4162 .offset = offset,
4163 .write = is_write,
4164 .flush_interval = flush_interval,
4165 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004166 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004167 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004168 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004169 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004170 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004171 if (flush_interval) {
4172 printf("Sending flush every %d requests\n", flush_interval);
4173 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004174
Fam Zheng79d46582018-01-16 14:08:58 +08004175 buf_size = data.nrreq * data.bufsize;
4176 data.buf = blk_blockalign(blk, buf_size);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004177 memset(data.buf, pattern, data.nrreq * data.bufsize);
4178
Fam Zheng79d46582018-01-16 14:08:58 +08004179 blk_register_buf(blk, data.buf, buf_size);
4180
Kevin Wolfb6133b82014-08-05 14:17:13 +02004181 data.qiov = g_new(QEMUIOVector, data.nrreq);
4182 for (i = 0; i < data.nrreq; i++) {
4183 qemu_iovec_init(&data.qiov[i], 1);
4184 qemu_iovec_add(&data.qiov[i],
4185 data.buf + i * data.bufsize, data.bufsize);
4186 }
4187
4188 gettimeofday(&t1, NULL);
4189 bench_cb(&data, 0);
4190
4191 while (data.n > 0) {
4192 main_loop_wait(false);
4193 }
4194 gettimeofday(&t2, NULL);
4195
4196 printf("Run completed in %3.3f seconds.\n",
4197 (t2.tv_sec - t1.tv_sec)
4198 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4199
4200out:
Fam Zheng79d46582018-01-16 14:08:58 +08004201 if (data.buf) {
4202 blk_unregister_buf(blk, data.buf);
4203 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004204 qemu_vfree(data.buf);
4205 blk_unref(blk);
4206
4207 if (ret) {
4208 return 1;
4209 }
4210 return 0;
4211}
4212
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004213#define C_BS 01
4214#define C_COUNT 02
4215#define C_IF 04
4216#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004217#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004218
4219struct DdInfo {
4220 unsigned int flags;
4221 int64_t count;
4222};
4223
4224struct DdIo {
4225 int bsz; /* Block size */
4226 char *filename;
4227 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004228 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004229};
4230
4231struct DdOpts {
4232 const char *name;
4233 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4234 unsigned int flag;
4235};
4236
4237static int img_dd_bs(const char *arg,
4238 struct DdIo *in, struct DdIo *out,
4239 struct DdInfo *dd)
4240{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004241 int64_t res;
4242
Markus Armbruster606caa02017-02-21 21:14:04 +01004243 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004244
Markus Armbruster606caa02017-02-21 21:14:04 +01004245 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004246 error_report("invalid number: '%s'", arg);
4247 return 1;
4248 }
4249 in->bsz = out->bsz = res;
4250
4251 return 0;
4252}
4253
4254static int img_dd_count(const char *arg,
4255 struct DdIo *in, struct DdIo *out,
4256 struct DdInfo *dd)
4257{
Markus Armbruster606caa02017-02-21 21:14:04 +01004258 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004259
Markus Armbruster606caa02017-02-21 21:14:04 +01004260 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004261 error_report("invalid number: '%s'", arg);
4262 return 1;
4263 }
4264
4265 return 0;
4266}
4267
4268static int img_dd_if(const char *arg,
4269 struct DdIo *in, struct DdIo *out,
4270 struct DdInfo *dd)
4271{
4272 in->filename = g_strdup(arg);
4273
4274 return 0;
4275}
4276
4277static int img_dd_of(const char *arg,
4278 struct DdIo *in, struct DdIo *out,
4279 struct DdInfo *dd)
4280{
4281 out->filename = g_strdup(arg);
4282
4283 return 0;
4284}
4285
Reda Sallahif7c15532016-08-10 16:16:09 +02004286static int img_dd_skip(const char *arg,
4287 struct DdIo *in, struct DdIo *out,
4288 struct DdInfo *dd)
4289{
Markus Armbruster606caa02017-02-21 21:14:04 +01004290 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004291
Markus Armbruster606caa02017-02-21 21:14:04 +01004292 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004293 error_report("invalid number: '%s'", arg);
4294 return 1;
4295 }
4296
4297 return 0;
4298}
4299
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004300static int img_dd(int argc, char **argv)
4301{
4302 int ret = 0;
4303 char *arg = NULL;
4304 char *tmp;
4305 BlockDriver *drv = NULL, *proto_drv = NULL;
4306 BlockBackend *blk1 = NULL, *blk2 = NULL;
4307 QemuOpts *opts = NULL;
4308 QemuOptsList *create_opts = NULL;
4309 Error *local_err = NULL;
4310 bool image_opts = false;
4311 int c, i;
4312 const char *out_fmt = "raw";
4313 const char *fmt = NULL;
4314 int64_t size = 0;
4315 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004316 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004317 struct DdInfo dd = {
4318 .flags = 0,
4319 .count = 0,
4320 };
4321 struct DdIo in = {
4322 .bsz = 512, /* Block size is by default 512 bytes */
4323 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004324 .buf = NULL,
4325 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004326 };
4327 struct DdIo out = {
4328 .bsz = 512,
4329 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004330 .buf = NULL,
4331 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004332 };
4333
4334 const struct DdOpts options[] = {
4335 { "bs", img_dd_bs, C_BS },
4336 { "count", img_dd_count, C_COUNT },
4337 { "if", img_dd_if, C_IF },
4338 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004339 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004340 { NULL, NULL, 0 }
4341 };
4342 const struct option long_options[] = {
4343 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004344 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004345 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004346 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004347 { 0, 0, 0, 0 }
4348 };
4349
Fam Zheng335e9932017-05-03 00:35:39 +08004350 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004351 if (c == EOF) {
4352 break;
4353 }
4354 switch (c) {
4355 case 'O':
4356 out_fmt = optarg;
4357 break;
4358 case 'f':
4359 fmt = optarg;
4360 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004361 case ':':
4362 missing_argument(argv[optind - 1]);
4363 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004364 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004365 unrecognized_option(argv[optind - 1]);
4366 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004367 case 'h':
4368 help();
4369 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004370 case 'U':
4371 force_share = true;
4372 break;
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004373 case OPTION_OBJECT:
4374 if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004375 ret = -1;
4376 goto out;
4377 }
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004378 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004379 case OPTION_IMAGE_OPTS:
4380 image_opts = true;
4381 break;
4382 }
4383 }
4384
4385 for (i = optind; i < argc; i++) {
4386 int j;
4387 arg = g_strdup(argv[i]);
4388
4389 tmp = strchr(arg, '=');
4390 if (tmp == NULL) {
4391 error_report("unrecognized operand %s", arg);
4392 ret = -1;
4393 goto out;
4394 }
4395
4396 *tmp++ = '\0';
4397
4398 for (j = 0; options[j].name != NULL; j++) {
4399 if (!strcmp(arg, options[j].name)) {
4400 break;
4401 }
4402 }
4403 if (options[j].name == NULL) {
4404 error_report("unrecognized operand %s", arg);
4405 ret = -1;
4406 goto out;
4407 }
4408
4409 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4410 ret = -1;
4411 goto out;
4412 }
4413 dd.flags |= options[j].flag;
4414 g_free(arg);
4415 arg = NULL;
4416 }
4417
4418 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4419 error_report("Must specify both input and output files");
4420 ret = -1;
4421 goto out;
4422 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004423
4424 if (qemu_opts_foreach(&qemu_object_opts,
4425 user_creatable_add_opts_foreach,
4426 NULL, NULL)) {
4427 ret = -1;
4428 goto out;
4429 }
4430
Fam Zheng335e9932017-05-03 00:35:39 +08004431 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4432 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004433
4434 if (!blk1) {
4435 ret = -1;
4436 goto out;
4437 }
4438
4439 drv = bdrv_find_format(out_fmt);
4440 if (!drv) {
4441 error_report("Unknown file format");
4442 ret = -1;
4443 goto out;
4444 }
4445 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4446
4447 if (!proto_drv) {
4448 error_report_err(local_err);
4449 ret = -1;
4450 goto out;
4451 }
4452 if (!drv->create_opts) {
4453 error_report("Format driver '%s' does not support image creation",
4454 drv->format_name);
4455 ret = -1;
4456 goto out;
4457 }
4458 if (!proto_drv->create_opts) {
4459 error_report("Protocol driver '%s' does not support image creation",
4460 proto_drv->format_name);
4461 ret = -1;
4462 goto out;
4463 }
4464 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4465 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4466
4467 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4468
4469 size = blk_getlength(blk1);
4470 if (size < 0) {
4471 error_report("Failed to get size for '%s'", in.filename);
4472 ret = -1;
4473 goto out;
4474 }
4475
4476 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4477 dd.count * in.bsz < size) {
4478 size = dd.count * in.bsz;
4479 }
4480
Reda Sallahif7c15532016-08-10 16:16:09 +02004481 /* Overflow means the specified offset is beyond input image's size */
4482 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4483 size < in.bsz * in.offset)) {
4484 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4485 } else {
4486 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4487 size - in.bsz * in.offset, &error_abort);
4488 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004489
4490 ret = bdrv_create(drv, out.filename, opts, &local_err);
4491 if (ret < 0) {
4492 error_reportf_err(local_err,
4493 "%s: error while creating output image: ",
4494 out.filename);
4495 ret = -1;
4496 goto out;
4497 }
4498
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004499 /* TODO, we can't honour --image-opts for the target,
4500 * since it needs to be given in a format compatible
4501 * with the bdrv_create() call above which does not
4502 * support image-opts style.
4503 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01004504 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004505 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004506
4507 if (!blk2) {
4508 ret = -1;
4509 goto out;
4510 }
4511
Reda Sallahif7c15532016-08-10 16:16:09 +02004512 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4513 size < in.offset * in.bsz)) {
4514 /* We give a warning if the skip option is bigger than the input
4515 * size and create an empty output disk image (i.e. like dd(1)).
4516 */
4517 error_report("%s: cannot skip to specified offset", in.filename);
4518 in_pos = size;
4519 } else {
4520 in_pos = in.offset * in.bsz;
4521 }
4522
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004523 in.buf = g_new(uint8_t, in.bsz);
4524
Reda Sallahif7c15532016-08-10 16:16:09 +02004525 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004526 int in_ret, out_ret;
4527
4528 if (in_pos + in.bsz > size) {
4529 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4530 } else {
4531 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4532 }
4533 if (in_ret < 0) {
4534 error_report("error while reading from input image file: %s",
4535 strerror(-in_ret));
4536 ret = -1;
4537 goto out;
4538 }
4539 in_pos += in_ret;
4540
4541 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4542
4543 if (out_ret < 0) {
4544 error_report("error while writing to output image file: %s",
4545 strerror(-out_ret));
4546 ret = -1;
4547 goto out;
4548 }
4549 out_pos += out_ret;
4550 }
4551
4552out:
4553 g_free(arg);
4554 qemu_opts_del(opts);
4555 qemu_opts_free(create_opts);
4556 blk_unref(blk1);
4557 blk_unref(blk2);
4558 g_free(in.filename);
4559 g_free(out.filename);
4560 g_free(in.buf);
4561 g_free(out.buf);
4562
4563 if (ret) {
4564 return 1;
4565 }
4566 return 0;
4567}
4568
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004569static void dump_json_block_measure_info(BlockMeasureInfo *info)
4570{
4571 QString *str;
4572 QObject *obj;
4573 Visitor *v = qobject_output_visitor_new(&obj);
4574
4575 visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
4576 visit_complete(v, &obj);
4577 str = qobject_to_json_pretty(obj);
4578 assert(str != NULL);
4579 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004580 qobject_unref(obj);
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004581 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004582 qobject_unref(str);
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004583}
4584
4585static int img_measure(int argc, char **argv)
4586{
4587 static const struct option long_options[] = {
4588 {"help", no_argument, 0, 'h'},
4589 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4590 {"object", required_argument, 0, OPTION_OBJECT},
4591 {"output", required_argument, 0, OPTION_OUTPUT},
4592 {"size", required_argument, 0, OPTION_SIZE},
4593 {"force-share", no_argument, 0, 'U'},
4594 {0, 0, 0, 0}
4595 };
4596 OutputFormat output_format = OFORMAT_HUMAN;
4597 BlockBackend *in_blk = NULL;
4598 BlockDriver *drv;
4599 const char *filename = NULL;
4600 const char *fmt = NULL;
4601 const char *out_fmt = "raw";
4602 char *options = NULL;
4603 char *snapshot_name = NULL;
4604 bool force_share = false;
4605 QemuOpts *opts = NULL;
4606 QemuOpts *object_opts = NULL;
4607 QemuOpts *sn_opts = NULL;
4608 QemuOptsList *create_opts = NULL;
4609 bool image_opts = false;
4610 uint64_t img_size = UINT64_MAX;
4611 BlockMeasureInfo *info = NULL;
4612 Error *local_err = NULL;
4613 int ret = 1;
4614 int c;
4615
4616 while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
4617 long_options, NULL)) != -1) {
4618 switch (c) {
4619 case '?':
4620 case 'h':
4621 help();
4622 break;
4623 case 'f':
4624 fmt = optarg;
4625 break;
4626 case 'O':
4627 out_fmt = optarg;
4628 break;
4629 case 'o':
4630 if (!is_valid_option_list(optarg)) {
4631 error_report("Invalid option list: %s", optarg);
4632 goto out;
4633 }
4634 if (!options) {
4635 options = g_strdup(optarg);
4636 } else {
4637 char *old_options = options;
4638 options = g_strdup_printf("%s,%s", options, optarg);
4639 g_free(old_options);
4640 }
4641 break;
4642 case 'l':
4643 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
4644 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
4645 optarg, false);
4646 if (!sn_opts) {
4647 error_report("Failed in parsing snapshot param '%s'",
4648 optarg);
4649 goto out;
4650 }
4651 } else {
4652 snapshot_name = optarg;
4653 }
4654 break;
4655 case 'U':
4656 force_share = true;
4657 break;
4658 case OPTION_OBJECT:
4659 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
4660 optarg, true);
4661 if (!object_opts) {
4662 goto out;
4663 }
4664 break;
4665 case OPTION_IMAGE_OPTS:
4666 image_opts = true;
4667 break;
4668 case OPTION_OUTPUT:
4669 if (!strcmp(optarg, "json")) {
4670 output_format = OFORMAT_JSON;
4671 } else if (!strcmp(optarg, "human")) {
4672 output_format = OFORMAT_HUMAN;
4673 } else {
4674 error_report("--output must be used with human or json "
4675 "as argument.");
4676 goto out;
4677 }
4678 break;
4679 case OPTION_SIZE:
4680 {
4681 int64_t sval;
4682
4683 sval = cvtnum(optarg);
4684 if (sval < 0) {
4685 if (sval == -ERANGE) {
4686 error_report("Image size must be less than 8 EiB!");
4687 } else {
4688 error_report("Invalid image size specified! You may use "
4689 "k, M, G, T, P or E suffixes for ");
4690 error_report("kilobytes, megabytes, gigabytes, terabytes, "
4691 "petabytes and exabytes.");
4692 }
4693 goto out;
4694 }
4695 img_size = (uint64_t)sval;
4696 }
4697 break;
4698 }
4699 }
4700
4701 if (qemu_opts_foreach(&qemu_object_opts,
4702 user_creatable_add_opts_foreach,
4703 NULL, NULL)) {
4704 goto out;
4705 }
4706
4707 if (argc - optind > 1) {
4708 error_report("At most one filename argument is allowed.");
4709 goto out;
4710 } else if (argc - optind == 1) {
4711 filename = argv[optind];
4712 }
4713
4714 if (!filename &&
4715 (object_opts || image_opts || fmt || snapshot_name || sn_opts)) {
4716 error_report("--object, --image-opts, -f, and -l "
4717 "require a filename argument.");
4718 goto out;
4719 }
4720 if (filename && img_size != UINT64_MAX) {
4721 error_report("--size N cannot be used together with a filename.");
4722 goto out;
4723 }
4724 if (!filename && img_size == UINT64_MAX) {
4725 error_report("Either --size N or one filename must be specified.");
4726 goto out;
4727 }
4728
4729 if (filename) {
4730 in_blk = img_open(image_opts, filename, fmt, 0,
4731 false, false, force_share);
4732 if (!in_blk) {
4733 goto out;
4734 }
4735
4736 if (sn_opts) {
4737 bdrv_snapshot_load_tmp(blk_bs(in_blk),
4738 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
4739 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
4740 &local_err);
4741 } else if (snapshot_name != NULL) {
4742 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
4743 snapshot_name, &local_err);
4744 }
4745 if (local_err) {
4746 error_reportf_err(local_err, "Failed to load snapshot: ");
4747 goto out;
4748 }
4749 }
4750
4751 drv = bdrv_find_format(out_fmt);
4752 if (!drv) {
4753 error_report("Unknown file format '%s'", out_fmt);
4754 goto out;
4755 }
4756 if (!drv->create_opts) {
4757 error_report("Format driver '%s' does not support image creation",
4758 drv->format_name);
4759 goto out;
4760 }
4761
4762 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4763 create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
4764 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4765 if (options) {
4766 qemu_opts_do_parse(opts, options, NULL, &local_err);
4767 if (local_err) {
4768 error_report_err(local_err);
4769 error_report("Invalid options for file format '%s'", out_fmt);
4770 goto out;
4771 }
4772 }
4773 if (img_size != UINT64_MAX) {
4774 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
4775 }
4776
4777 info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
4778 if (local_err) {
4779 error_report_err(local_err);
4780 goto out;
4781 }
4782
4783 if (output_format == OFORMAT_HUMAN) {
4784 printf("required size: %" PRIu64 "\n", info->required);
4785 printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
4786 } else {
4787 dump_json_block_measure_info(info);
4788 }
4789
4790 ret = 0;
4791
4792out:
4793 qapi_free_BlockMeasureInfo(info);
4794 qemu_opts_del(object_opts);
4795 qemu_opts_del(opts);
4796 qemu_opts_del(sn_opts);
4797 qemu_opts_free(create_opts);
4798 g_free(options);
4799 blk_unref(in_blk);
4800 return ret;
4801}
Kevin Wolfb6133b82014-08-05 14:17:13 +02004802
Anthony Liguoric227f092009-10-01 16:12:16 -05004803static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004804#define DEF(option, callback, arg_string) \
4805 { option, callback },
4806#include "qemu-img-cmds.h"
4807#undef DEF
Stuart Brady153859b2009-06-07 00:42:17 +01004808 { NULL, NULL, },
4809};
4810
bellardea2384d2004-08-01 21:59:26 +00004811int main(int argc, char **argv)
4812{
Anthony Liguoric227f092009-10-01 16:12:16 -05004813 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004814 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004815 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004816 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004817 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004818 static const struct option long_options[] = {
4819 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004820 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004821 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004822 {0, 0, 0, 0}
4823 };
bellardea2384d2004-08-01 21:59:26 +00004824
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004825#ifdef CONFIG_POSIX
4826 signal(SIGPIPE, SIG_IGN);
4827#endif
4828
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004829 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004830 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004831 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004832
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004833 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004834 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004835 exit(EXIT_FAILURE);
4836 }
4837
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004838 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004839
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004840 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004841 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004842 if (argc < 2) {
4843 error_exit("Not enough arguments");
4844 }
Stuart Brady153859b2009-06-07 00:42:17 +01004845
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004846 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004847 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004848 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004849
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004850 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004851 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004852 case ':':
4853 missing_argument(argv[optind - 1]);
4854 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08004855 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004856 unrecognized_option(argv[optind - 1]);
4857 return 0;
4858 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03004859 help();
4860 return 0;
4861 case 'V':
4862 printf(QEMU_IMG_VERSION);
4863 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004864 case 'T':
4865 g_free(trace_file);
4866 trace_file = trace_opt_parse(optarg);
4867 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004868 }
bellardea2384d2004-08-01 21:59:26 +00004869 }
Stuart Brady153859b2009-06-07 00:42:17 +01004870
Denis V. Lunev10985132016-06-17 17:44:13 +03004871 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004872
Denis V. Lunev10985132016-06-17 17:44:13 +03004873 /* reset getopt_long scanning */
4874 argc -= optind;
4875 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004876 return 0;
4877 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004878 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004879 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004880
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004881 if (!trace_init_backends()) {
4882 exit(1);
4883 }
4884 trace_init_file(trace_file);
4885 qemu_set_log(LOG_TRACE);
4886
Denis V. Lunev10985132016-06-17 17:44:13 +03004887 /* find the command */
4888 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4889 if (!strcmp(cmdname, cmd->name)) {
4890 return cmd->handler(argc, argv);
4891 }
4892 }
Jeff Cody7db16892014-04-25 17:02:32 -04004893
Stuart Brady153859b2009-06-07 00:42:17 +01004894 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004895 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004896}