blob: 60e45ec103653b2d89ac8130b5986fc16f7a9484 [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
126#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +0300127 "\n"
128 "Command parameters:\n"
129 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000130 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
131 " manual page for a description of the object properties. The most common\n"
132 " object type is a 'secret', which is used to supply passwords and/or\n"
133 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300134 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400135 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800136 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
137 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100138 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
139 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300140 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200141 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
142 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
143 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300144 " 'output_filename' is the destination disk image filename\n"
145 " 'output_fmt' is the destination format\n"
146 " 'options' is a comma separated list of format specific options in a\n"
147 " name=value format. Use -o ? for an overview of the options supported by the\n"
148 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800149 " 'snapshot_param' is param used for internal snapshot, format\n"
150 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
151 " '[ID_OR_NAME]'\n"
152 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
153 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300154 " '-c' indicates that target image must be compressed (qcow format only)\n"
John Snow6e6e55f2017-07-17 20:34:22 -0400155 " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
156 " new backing file match exactly. The image doesn't need a working\n"
157 " backing file before rebasing in this case (useful for renaming the\n"
158 " backing file). For image creation, allow creating without attempting\n"
159 " to open the backing file.\n"
malc3f020d72010-02-08 12:04:56 +0300160 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200161 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100162 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200163 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
164 " contain only zeros for qemu-img to create a sparse image during\n"
165 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
166 " unallocated or zero sectors, and the destination image will always be\n"
167 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200168 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100169 " '-n' skips the target volume creation (useful if the volume is created\n"
170 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300171 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200172 "Parameters to check subcommand:\n"
173 " '-r' tries to repair any inconsistencies that are found during the check.\n"
174 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
175 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200176 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200177 "\n"
Peter Lieven2d9187b2017-02-28 13:40:07 +0100178 "Parameters to convert subcommand:\n"
179 " '-m' specifies how many coroutines work in parallel during the convert\n"
180 " process (defaults to 8)\n"
181 " '-W' allow to write to the target out of order rather than sequential\n"
182 "\n"
malc3f020d72010-02-08 12:04:56 +0300183 "Parameters to snapshot subcommand:\n"
184 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
185 " '-a' applies a snapshot (revert disk to saved state)\n"
186 " '-c' creates a snapshot\n"
187 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100188 " '-l' lists all snapshots in the given image\n"
189 "\n"
190 "Parameters to compare subcommand:\n"
191 " '-f' first image format\n"
192 " '-F' second image format\n"
Reda Sallahi86ce1f62016-08-10 04:43:12 +0200193 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
194 "\n"
195 "Parameters to dd subcommand:\n"
196 " 'bs=BYTES' read and write up to BYTES bytes at a time "
197 "(default: 512)\n"
198 " 'count=N' copy only N input blocks\n"
199 " 'if=FILE' read from FILE\n"
Reda Sallahif7c15532016-08-10 16:16:09 +0200200 " 'of=FILE' write to FILE\n"
201 " 'skip=N' skip N bs-sized blocks at the start of input\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100202
203 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100204 bdrv_iterate_format(format_print, NULL);
Eric Blakef5048cb2017-08-03 11:33:53 -0500205 printf("\n\n" QEMU_HELP_BOTTOM "\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800206 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000207}
208
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000209static QemuOptsList qemu_object_opts = {
210 .name = "object",
211 .implied_opt_name = "qom-type",
212 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
213 .desc = {
214 { }
215 },
216};
217
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000218static QemuOptsList qemu_source_opts = {
219 .name = "source",
220 .implied_opt_name = "file",
221 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
222 .desc = {
223 { }
224 },
225};
226
Stefan Weil7c30f652013-06-16 17:01:05 +0200227static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100228{
229 int ret = 0;
230 if (!quiet) {
231 va_list args;
232 va_start(args, fmt);
233 ret = vprintf(fmt, args);
234 va_end(args);
235 }
236 return ret;
237}
238
bellardea2384d2004-08-01 21:59:26 +0000239
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100240static int print_block_option_help(const char *filename, const char *fmt)
241{
242 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800243 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500244 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100245
246 /* Find driver and parse its options */
247 drv = bdrv_find_format(fmt);
248 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100249 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100250 return 1;
251 }
252
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800253 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100254 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500255 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100256 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100257 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800258 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100259 return 1;
260 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800261 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100262 }
263
Chunyan Liu83d05212014-06-05 17:20:51 +0800264 qemu_opts_print_help(create_opts);
265 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100266 return 0;
267}
268
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000269
Max Reitzefaa7c42016-03-16 19:54:38 +0100270static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100271 QemuOpts *opts, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800272 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000273{
274 QDict *options;
275 Error *local_err = NULL;
276 BlockBackend *blk;
277 options = qemu_opts_to_qdict(opts, NULL);
Fam Zheng335e9932017-05-03 00:35:39 +0800278 if (force_share) {
279 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
Max Reitz4615f872018-05-02 22:20:50 +0200280 && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) {
Fam Zheng335e9932017-05-03 00:35:39 +0800281 error_report("--force-share/-U conflicts with image options");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200282 qobject_unref(options);
Fam Zheng335e9932017-05-03 00:35:39 +0800283 return NULL;
284 }
Max Reitz4615f872018-05-02 22:20:50 +0200285 qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on");
Fam Zheng335e9932017-05-03 00:35:39 +0800286 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100287 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000288 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100289 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000290 return NULL;
291 }
Kevin Wolfce099542016-03-15 13:01:04 +0100292 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000293
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000294 return blk;
295}
296
Max Reitzefaa7c42016-03-16 19:54:38 +0100297static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100298 QDict *options,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000299 const char *fmt, int flags,
Fam Zheng335e9932017-05-03 00:35:39 +0800300 bool writethrough, bool quiet,
301 bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000302{
303 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200304 Error *local_err = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100305
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100306 if (!options) {
307 options = qdict_new();
308 }
bellard75c23802004-08-27 21:28:58 +0000309 if (fmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500310 qdict_put_str(options, "driver", fmt);
bellard75c23802004-08-27 21:28:58 +0000311 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100312
Fam Zheng335e9932017-05-03 00:35:39 +0800313 if (force_share) {
Eric Blake579cf1d2017-05-15 14:54:39 -0500314 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800315 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100316 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500317 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100318 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000319 return NULL;
bellard75c23802004-08-27 21:28:58 +0000320 }
Kevin Wolfce099542016-03-15 13:01:04 +0100321 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100322
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200323 return blk;
bellard75c23802004-08-27 21:28:58 +0000324}
325
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000326
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100327static int img_add_key_secrets(void *opaque,
328 const char *name, const char *value,
329 Error **errp)
330{
331 QDict *options = opaque;
332
333 if (g_str_has_suffix(name, "key-secret")) {
Eric Blake187f47e2017-06-24 12:10:07 -0600334 qdict_put_str(options, name, value);
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100335 }
336
337 return 0;
338}
339
340static BlockBackend *img_open_new_file(const char *filename,
341 QemuOpts *create_opts,
342 const char *fmt, int flags,
343 bool writethrough, bool quiet,
344 bool force_share)
345{
346 QDict *options = NULL;
347
348 options = qdict_new();
349 qemu_opt_foreach(create_opts, img_add_key_secrets, options, &error_abort);
350
351 return img_open_file(filename, options, fmt, flags, writethrough, quiet,
352 force_share);
353}
354
355
Max Reitzefaa7c42016-03-16 19:54:38 +0100356static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000357 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100358 const char *fmt, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800359 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000360{
361 BlockBackend *blk;
362 if (image_opts) {
363 QemuOpts *opts;
364 if (fmt) {
365 error_report("--image-opts and --format are mutually exclusive");
366 return NULL;
367 }
368 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
369 filename, true);
370 if (!opts) {
371 return NULL;
372 }
Fam Zheng335e9932017-05-03 00:35:39 +0800373 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
374 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000375 } else {
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100376 blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
Fam Zheng335e9932017-05-03 00:35:39 +0800377 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000378 }
379 return blk;
380}
381
382
Chunyan Liu83d05212014-06-05 17:20:51 +0800383static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100384 const char *base_filename,
385 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200386{
Markus Armbruster6750e792015-02-12 17:43:08 +0100387 Error *err = NULL;
388
Kevin Wolfefa84d42009-05-18 16:42:12 +0200389 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100390 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100391 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100392 error_report("Backing file not supported for file format '%s'",
393 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100394 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900395 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200396 }
397 }
398 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100399 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100400 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100401 error_report("Backing file format not supported for file "
402 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100403 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900404 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200405 }
406 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900407 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200408}
409
Markus Armbruster606caa02017-02-21 21:14:04 +0100410static int64_t cvtnum(const char *s)
411{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100412 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100413 uint64_t value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100414
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100415 err = qemu_strtosz(s, NULL, &value);
416 if (err < 0) {
417 return err;
418 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100419 if (value > INT64_MAX) {
420 return -ERANGE;
421 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100422 return value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100423}
424
bellardea2384d2004-08-01 21:59:26 +0000425static int img_create(int argc, char **argv)
426{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200427 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100428 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000429 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000430 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000431 const char *filename;
432 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200433 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200434 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100435 bool quiet = false;
John Snow6e6e55f2017-07-17 20:34:22 -0400436 int flags = 0;
ths3b46e622007-09-17 08:09:54 +0000437
bellardea2384d2004-08-01 21:59:26 +0000438 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000439 static const struct option long_options[] = {
440 {"help", no_argument, 0, 'h'},
441 {"object", required_argument, 0, OPTION_OBJECT},
442 {0, 0, 0, 0}
443 };
John Snow6e6e55f2017-07-17 20:34:22 -0400444 c = getopt_long(argc, argv, ":F:b:f:ho:qu",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000445 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100446 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000447 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100448 }
bellardea2384d2004-08-01 21:59:26 +0000449 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800450 case ':':
451 missing_argument(argv[optind - 1]);
452 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100453 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800454 unrecognized_option(argv[optind - 1]);
455 break;
bellardea2384d2004-08-01 21:59:26 +0000456 case 'h':
457 help();
458 break;
aliguori9230eaf2009-03-28 17:55:19 +0000459 case 'F':
460 base_fmt = optarg;
461 break;
bellardea2384d2004-08-01 21:59:26 +0000462 case 'b':
463 base_filename = optarg;
464 break;
465 case 'f':
466 fmt = optarg;
467 break;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200468 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100469 if (!is_valid_option_list(optarg)) {
470 error_report("Invalid option list: %s", optarg);
471 goto fail;
472 }
473 if (!options) {
474 options = g_strdup(optarg);
475 } else {
476 char *old_options = options;
477 options = g_strdup_printf("%s,%s", options, optarg);
478 g_free(old_options);
479 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200480 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100481 case 'q':
482 quiet = true;
483 break;
John Snow6e6e55f2017-07-17 20:34:22 -0400484 case 'u':
485 flags |= BDRV_O_NO_BACKING;
486 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000487 case OPTION_OBJECT: {
488 QemuOpts *opts;
489 opts = qemu_opts_parse_noisily(&qemu_object_opts,
490 optarg, true);
491 if (!opts) {
492 goto fail;
493 }
494 } break;
bellardea2384d2004-08-01 21:59:26 +0000495 }
496 }
aliguori9230eaf2009-03-28 17:55:19 +0000497
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900498 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100499 filename = (optind < argc) ? argv[optind] : NULL;
500 if (options && has_help_option(options)) {
501 g_free(options);
502 return print_block_option_help(filename, fmt);
503 }
504
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100505 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800506 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100507 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100508 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900509
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000510 if (qemu_opts_foreach(&qemu_object_opts,
511 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200512 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000513 goto fail;
514 }
515
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100516 /* Get image size, if specified */
517 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100518 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100519
520 sval = cvtnum(argv[optind++]);
521 if (sval < 0) {
liguang79443392012-12-17 09:49:23 +0800522 if (sval == -ERANGE) {
523 error_report("Image size must be less than 8 EiB!");
524 } else {
525 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200526 "G, T, P or E suffixes for ");
527 error_report("kilobytes, megabytes, gigabytes, terabytes, "
528 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800529 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100530 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100531 }
532 img_size = (uint64_t)sval;
533 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200534 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800535 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200536 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100537
Luiz Capitulino9b375252012-11-30 10:52:05 -0200538 bdrv_img_create(filename, fmt, base_filename, base_fmt,
John Snow6e6e55f2017-07-17 20:34:22 -0400539 options, img_size, flags, quiet, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100540 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100541 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100542 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900543 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200544
Kevin Wolf77386bf2014-02-21 16:24:04 +0100545 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000546 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100547
548fail:
549 g_free(options);
550 return 1;
bellardea2384d2004-08-01 21:59:26 +0000551}
552
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100553static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500554{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500555 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500556 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100557 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600558
559 visit_type_ImageCheck(v, NULL, &check, &error_abort);
560 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500561 str = qobject_to_json_pretty(obj);
562 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100563 qprintf(quiet, "%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200564 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600565 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200566 qobject_unref(str);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500567}
568
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100569static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500570{
571 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100572 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500573 } else {
574 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100575 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
576 "Data may be corrupted, or further writes to the image "
577 "may corrupt it.\n",
578 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500579 }
580
581 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100582 qprintf(quiet,
583 "\n%" PRId64 " leaked clusters were found on the image.\n"
584 "This means waste of disk space, but no harm to data.\n",
585 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500586 }
587
588 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100589 qprintf(quiet,
590 "\n%" PRId64
591 " internal errors have occurred during the check.\n",
592 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500593 }
594 }
595
596 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100597 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
598 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
599 check->allocated_clusters, check->total_clusters,
600 check->allocated_clusters * 100.0 / check->total_clusters,
601 check->fragmented_clusters * 100.0 / check->allocated_clusters,
602 check->compressed_clusters * 100.0 /
603 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500604 }
605
606 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100607 qprintf(quiet,
608 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500609 }
610}
611
612static int collect_image_check(BlockDriverState *bs,
613 ImageCheck *check,
614 const char *filename,
615 const char *fmt,
616 int fix)
617{
618 int ret;
619 BdrvCheckResult result;
620
621 ret = bdrv_check(bs, &result, fix);
622 if (ret < 0) {
623 return ret;
624 }
625
626 check->filename = g_strdup(filename);
627 check->format = g_strdup(bdrv_get_format_name(bs));
628 check->check_errors = result.check_errors;
629 check->corruptions = result.corruptions;
630 check->has_corruptions = result.corruptions != 0;
631 check->leaks = result.leaks;
632 check->has_leaks = result.leaks != 0;
633 check->corruptions_fixed = result.corruptions_fixed;
634 check->has_corruptions_fixed = result.corruptions != 0;
635 check->leaks_fixed = result.leaks_fixed;
636 check->has_leaks_fixed = result.leaks != 0;
637 check->image_end_offset = result.image_end_offset;
638 check->has_image_end_offset = result.image_end_offset != 0;
639 check->total_clusters = result.bfi.total_clusters;
640 check->has_total_clusters = result.bfi.total_clusters != 0;
641 check->allocated_clusters = result.bfi.allocated_clusters;
642 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
643 check->fragmented_clusters = result.bfi.fragmented_clusters;
644 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100645 check->compressed_clusters = result.bfi.compressed_clusters;
646 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500647
648 return 0;
649}
650
Kevin Wolfe076f332010-06-29 11:43:13 +0200651/*
652 * Checks an image for consistency. Exit codes:
653 *
Max Reitzd6635c42014-06-02 22:15:21 +0200654 * 0 - Check completed, image is good
655 * 1 - Check not completed because of internal errors
656 * 2 - Check completed, image is corrupted
657 * 3 - Check completed, image has leaked clusters, but is good otherwise
658 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200659 */
aliguori15859692009-04-21 23:11:53 +0000660static int img_check(int argc, char **argv)
661{
662 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500663 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200664 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200665 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000666 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200667 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100668 int flags = BDRV_O_CHECK;
669 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200670 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100671 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000672 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +0800673 bool force_share = false;
aliguori15859692009-04-21 23:11:53 +0000674
675 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500676 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200677 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100678
aliguori15859692009-04-21 23:11:53 +0000679 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500680 int option_index = 0;
681 static const struct option long_options[] = {
682 {"help", no_argument, 0, 'h'},
683 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530684 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500685 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000686 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000687 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +0800688 {"force-share", no_argument, 0, 'U'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500689 {0, 0, 0, 0}
690 };
Fam Zheng335e9932017-05-03 00:35:39 +0800691 c = getopt_long(argc, argv, ":hf:r:T:qU",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500692 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100693 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000694 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100695 }
aliguori15859692009-04-21 23:11:53 +0000696 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800697 case ':':
698 missing_argument(argv[optind - 1]);
699 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100700 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800701 unrecognized_option(argv[optind - 1]);
702 break;
aliguori15859692009-04-21 23:11:53 +0000703 case 'h':
704 help();
705 break;
706 case 'f':
707 fmt = optarg;
708 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200709 case 'r':
710 flags |= BDRV_O_RDWR;
711
712 if (!strcmp(optarg, "leaks")) {
713 fix = BDRV_FIX_LEAKS;
714 } else if (!strcmp(optarg, "all")) {
715 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
716 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800717 error_exit("Unknown option value for -r "
718 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200719 }
720 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500721 case OPTION_OUTPUT:
722 output = optarg;
723 break;
Max Reitz40055952014-07-22 22:58:42 +0200724 case 'T':
725 cache = optarg;
726 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100727 case 'q':
728 quiet = true;
729 break;
Fam Zheng335e9932017-05-03 00:35:39 +0800730 case 'U':
731 force_share = true;
732 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000733 case OPTION_OBJECT: {
734 QemuOpts *opts;
735 opts = qemu_opts_parse_noisily(&qemu_object_opts,
736 optarg, true);
737 if (!opts) {
738 return 1;
739 }
740 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000741 case OPTION_IMAGE_OPTS:
742 image_opts = true;
743 break;
aliguori15859692009-04-21 23:11:53 +0000744 }
745 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200746 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800747 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100748 }
aliguori15859692009-04-21 23:11:53 +0000749 filename = argv[optind++];
750
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500751 if (output && !strcmp(output, "json")) {
752 output_format = OFORMAT_JSON;
753 } else if (output && !strcmp(output, "human")) {
754 output_format = OFORMAT_HUMAN;
755 } else if (output) {
756 error_report("--output must be used with human or json as argument.");
757 return 1;
758 }
759
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000760 if (qemu_opts_foreach(&qemu_object_opts,
761 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200762 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000763 return 1;
764 }
765
Kevin Wolfce099542016-03-15 13:01:04 +0100766 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200767 if (ret < 0) {
768 error_report("Invalid source cache option: %s", cache);
769 return 1;
770 }
771
Fam Zheng335e9932017-05-03 00:35:39 +0800772 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
773 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200774 if (!blk) {
775 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900776 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200777 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500778
779 check = g_new0(ImageCheck, 1);
780 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200781
782 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200783 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200784 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500785 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200786 }
787
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500788 if (check->corruptions_fixed || check->leaks_fixed) {
789 int corruptions_fixed, leaks_fixed;
790
791 leaks_fixed = check->leaks_fixed;
792 corruptions_fixed = check->corruptions_fixed;
793
794 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100795 qprintf(quiet,
796 "The following inconsistencies were found and repaired:\n\n"
797 " %" PRId64 " leaked clusters\n"
798 " %" PRId64 " corruptions\n\n"
799 "Double checking the fixed image now...\n",
800 check->leaks_fixed,
801 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500802 }
803
804 ret = collect_image_check(bs, check, filename, fmt, 0);
805
806 check->leaks_fixed = leaks_fixed;
807 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200808 }
809
Max Reitz832390a2014-10-23 15:29:12 +0200810 if (!ret) {
811 switch (output_format) {
812 case OFORMAT_HUMAN:
813 dump_human_image_check(check, quiet);
814 break;
815 case OFORMAT_JSON:
816 dump_json_image_check(check, quiet);
817 break;
818 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500819 }
820
821 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200822 if (ret) {
823 error_report("Check failed: %s", strerror(-ret));
824 } else {
825 error_report("Check failed");
826 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500827 ret = 1;
828 goto fail;
829 }
830
831 if (check->corruptions) {
832 ret = 2;
833 } else if (check->leaks) {
834 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200835 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500836 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000837 }
838
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500839fail:
840 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200841 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500842 return ret;
aliguori15859692009-04-21 23:11:53 +0000843}
844
Max Reitzd4a32382014-10-24 15:57:37 +0200845typedef struct CommonBlockJobCBInfo {
846 BlockDriverState *bs;
847 Error **errp;
848} CommonBlockJobCBInfo;
849
850static void common_block_job_cb(void *opaque, int ret)
851{
852 CommonBlockJobCBInfo *cbi = opaque;
853
854 if (ret < 0) {
855 error_setg_errno(cbi->errp, -ret, "Block job failed");
856 }
Max Reitzd4a32382014-10-24 15:57:37 +0200857}
858
859static void run_block_job(BlockJob *job, Error **errp)
860{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200861 AioContext *aio_context = blk_get_aio_context(job->blk);
sochin.jiang4172a002017-06-15 14:47:33 +0800862 int ret = 0;
Max Reitzd4a32382014-10-24 15:57:37 +0200863
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200864 aio_context_acquire(aio_context);
sochin.jiang4172a002017-06-15 14:47:33 +0800865 block_job_ref(job);
Max Reitzd4a32382014-10-24 15:57:37 +0200866 do {
867 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500868 qemu_progress_print(job->len ?
869 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
sochin.jiang4172a002017-06-15 14:47:33 +0800870 } while (!job->ready && !job->completed);
Max Reitzd4a32382014-10-24 15:57:37 +0200871
sochin.jiang4172a002017-06-15 14:47:33 +0800872 if (!job->completed) {
873 ret = block_job_complete_sync(job, errp);
874 } else {
875 ret = job->ret;
876 }
877 block_job_unref(job);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200878 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200879
sochin.jiang4172a002017-06-15 14:47:33 +0800880 /* publish completion progress only when success */
881 if (!ret) {
882 qemu_progress_print(100.f, 0);
883 }
Max Reitzd4a32382014-10-24 15:57:37 +0200884}
885
bellardea2384d2004-08-01 21:59:26 +0000886static int img_commit(int argc, char **argv)
887{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400888 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200889 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200890 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200891 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100892 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200893 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100894 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200895 Error *local_err = NULL;
896 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000897 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200898 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000899
900 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400901 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200902 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000903 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000904 static const struct option long_options[] = {
905 {"help", no_argument, 0, 'h'},
906 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000907 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000908 {0, 0, 0, 0}
909 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800910 c = getopt_long(argc, argv, ":f:ht:b:dpq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000911 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100912 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000913 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100914 }
bellardea2384d2004-08-01 21:59:26 +0000915 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800916 case ':':
917 missing_argument(argv[optind - 1]);
918 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100919 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800920 unrecognized_option(argv[optind - 1]);
921 break;
bellardea2384d2004-08-01 21:59:26 +0000922 case 'h':
923 help();
924 break;
925 case 'f':
926 fmt = optarg;
927 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400928 case 't':
929 cache = optarg;
930 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200931 case 'b':
932 base = optarg;
933 /* -b implies -d */
934 drop = true;
935 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200936 case 'd':
937 drop = true;
938 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200939 case 'p':
940 progress = true;
941 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100942 case 'q':
943 quiet = true;
944 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000945 case OPTION_OBJECT: {
946 QemuOpts *opts;
947 opts = qemu_opts_parse_noisily(&qemu_object_opts,
948 optarg, true);
949 if (!opts) {
950 return 1;
951 }
952 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000953 case OPTION_IMAGE_OPTS:
954 image_opts = true;
955 break;
bellardea2384d2004-08-01 21:59:26 +0000956 }
957 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200958
959 /* Progress is not shown in Quiet mode */
960 if (quiet) {
961 progress = false;
962 }
963
Kevin Wolffc11eb22013-08-05 10:53:04 +0200964 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800965 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100966 }
bellardea2384d2004-08-01 21:59:26 +0000967 filename = argv[optind++];
968
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000969 if (qemu_opts_foreach(&qemu_object_opts,
970 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200971 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000972 return 1;
973 }
974
Max Reitz9a86fe42014-10-24 15:57:38 +0200975 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100976 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400977 if (ret < 0) {
978 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100979 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400980 }
981
Fam Zheng335e9932017-05-03 00:35:39 +0800982 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
983 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200984 if (!blk) {
985 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900986 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200987 bs = blk_bs(blk);
988
Max Reitz687fa1d2014-10-24 15:57:39 +0200989 qemu_progress_init(progress, 1.f);
990 qemu_progress_print(0.f, 100);
991
Max Reitz1b22bff2014-10-24 15:57:40 +0200992 if (base) {
993 base_bs = bdrv_find_backing_image(bs, base);
994 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +0100995 error_setg(&local_err,
996 "Did not find '%s' in the backing chain of '%s'",
997 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +0200998 goto done;
999 }
1000 } else {
1001 /* This is different from QMP, which by default uses the deepest file in
1002 * the backing chain (i.e., the very base); however, the traditional
1003 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +02001004 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +02001005 if (!base_bs) {
1006 error_setg(&local_err, "Image does not have a backing file");
1007 goto done;
1008 }
bellardea2384d2004-08-01 21:59:26 +00001009 }
1010
Max Reitzd4a32382014-10-24 15:57:37 +02001011 cbi = (CommonBlockJobCBInfo){
1012 .errp = &local_err,
1013 .bs = bs,
1014 };
1015
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001016 aio_context = bdrv_get_aio_context(bs);
1017 aio_context_acquire(aio_context);
John Snow47970df2016-10-27 12:06:57 -04001018 commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
Kevin Wolf0db832f2017-02-20 18:10:05 +01001019 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +08001020 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001021 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +02001022 if (local_err) {
1023 goto done;
1024 }
1025
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001026 /* When the block job completes, the BlockBackend reference will point to
1027 * the old backing file. In order to avoid that the top image is already
1028 * deleted, so we can still empty it afterwards, increment the reference
1029 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +02001030 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001031 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001032 }
1033
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001034 job = block_job_get("commit");
1035 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001036 if (local_err) {
1037 goto unref_backing;
1038 }
1039
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001040 if (!drop && bs->drv->bdrv_make_empty) {
1041 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001042 if (ret) {
1043 error_setg_errno(&local_err, -ret, "Could not empty %s",
1044 filename);
1045 goto unref_backing;
1046 }
1047 }
1048
1049unref_backing:
1050 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001051 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001052 }
Max Reitzd4a32382014-10-24 15:57:37 +02001053
1054done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001055 qemu_progress_end();
1056
Markus Armbruster26f54e92014-10-07 13:59:04 +02001057 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001058
1059 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001060 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001061 return 1;
1062 }
Max Reitzd4a32382014-10-24 15:57:37 +02001063
1064 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001065 return 0;
1066}
1067
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001068/*
Eric Blakedebb38a2017-10-11 22:47:11 -05001069 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1070 * of the first sector boundary within buf where the sector contains a
1071 * non-zero byte. This function is robust to a buffer that is not
1072 * sector-aligned.
1073 */
1074static int64_t find_nonzero(const uint8_t *buf, int64_t n)
1075{
1076 int64_t i;
1077 int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE);
1078
1079 for (i = 0; i < end; i += BDRV_SECTOR_SIZE) {
1080 if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) {
1081 return i;
1082 }
1083 }
1084 if (i < n && !buffer_is_zero(buf + i, n - end)) {
1085 return i;
1086 }
1087 return -1;
1088}
1089
1090/*
thsf58c7b32008-06-05 21:53:49 +00001091 * Returns true iff the first sector pointed to by 'buf' contains at least
1092 * a non-NUL byte.
1093 *
1094 * 'pnum' is set to the number of sectors (including and immediately following
1095 * the first one) that are known to be in the same allocated/unallocated state.
1096 */
bellardea2384d2004-08-01 21:59:26 +00001097static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1098{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001099 bool is_zero;
1100 int i;
bellardea2384d2004-08-01 21:59:26 +00001101
1102 if (n <= 0) {
1103 *pnum = 0;
1104 return 0;
1105 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001106 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001107 for(i = 1; i < n; i++) {
1108 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001109 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001110 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001111 }
bellardea2384d2004-08-01 21:59:26 +00001112 }
1113 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001114 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001115}
1116
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001117/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001118 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1119 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1120 * breaking up write requests for only small sparse areas.
1121 */
1122static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1123 int min)
1124{
1125 int ret;
1126 int num_checked, num_used;
1127
1128 if (n < min) {
1129 min = n;
1130 }
1131
1132 ret = is_allocated_sectors(buf, n, pnum);
1133 if (!ret) {
1134 return ret;
1135 }
1136
1137 num_used = *pnum;
1138 buf += BDRV_SECTOR_SIZE * *pnum;
1139 n -= *pnum;
1140 num_checked = num_used;
1141
1142 while (n > 0) {
1143 ret = is_allocated_sectors(buf, n, pnum);
1144
1145 buf += BDRV_SECTOR_SIZE * *pnum;
1146 n -= *pnum;
1147 num_checked += *pnum;
1148 if (ret) {
1149 num_used = num_checked;
1150 } else if (*pnum >= min) {
1151 break;
1152 }
1153 }
1154
1155 *pnum = num_used;
1156 return 1;
1157}
1158
1159/*
Eric Blakedc61cd32017-10-11 22:47:14 -05001160 * Compares two buffers sector by sector. Returns 0 if the first
1161 * sector of each buffer matches, non-zero otherwise.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001162 *
Eric Blakedc61cd32017-10-11 22:47:14 -05001163 * pnum is set to the sector-aligned size of the buffer prefix that
1164 * has the same matching status as the first sector.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001165 */
Eric Blakedc61cd32017-10-11 22:47:14 -05001166static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2,
1167 int64_t bytes, int64_t *pnum)
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001168{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001169 bool res;
Eric Blakedc61cd32017-10-11 22:47:14 -05001170 int64_t i = MIN(bytes, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001171
Eric Blakedc61cd32017-10-11 22:47:14 -05001172 assert(bytes > 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001173
Eric Blakedc61cd32017-10-11 22:47:14 -05001174 res = !!memcmp(buf1, buf2, i);
1175 while (i < bytes) {
1176 int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001177
Eric Blakedc61cd32017-10-11 22:47:14 -05001178 if (!!memcmp(buf1 + i, buf2 + i, len) != res) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001179 break;
1180 }
Eric Blakedc61cd32017-10-11 22:47:14 -05001181 i += len;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001182 }
1183
1184 *pnum = i;
1185 return res;
1186}
1187
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001188#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001189
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001190/*
1191 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1192 *
Eric Blake0608e402017-10-11 22:47:12 -05001193 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1194 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1195 * failure), and 4 on error (the exit status for read errors), after emitting
1196 * an error message.
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001197 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001198 * @param blk: BlockBackend for the image
Eric Blakec41508e2017-10-11 22:47:13 -05001199 * @param offset: Starting offset to check
1200 * @param bytes: Number of bytes to check
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001201 * @param filename: Name of disk file we are checking (logging purpose)
1202 * @param buffer: Allocated buffer for storing read data
1203 * @param quiet: Flag for quiet mode
1204 */
Eric Blakec41508e2017-10-11 22:47:13 -05001205static int check_empty_sectors(BlockBackend *blk, int64_t offset,
1206 int64_t bytes, const char *filename,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001207 uint8_t *buffer, bool quiet)
1208{
Eric Blakedebb38a2017-10-11 22:47:11 -05001209 int ret = 0;
1210 int64_t idx;
1211
Eric Blakec41508e2017-10-11 22:47:13 -05001212 ret = blk_pread(blk, offset, buffer, bytes);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001213 if (ret < 0) {
1214 error_report("Error while reading offset %" PRId64 " of %s: %s",
Eric Blakec41508e2017-10-11 22:47:13 -05001215 offset, filename, strerror(-ret));
Eric Blake0608e402017-10-11 22:47:12 -05001216 return 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001217 }
Eric Blakec41508e2017-10-11 22:47:13 -05001218 idx = find_nonzero(buffer, bytes);
Eric Blakedebb38a2017-10-11 22:47:11 -05001219 if (idx >= 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001220 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blakec41508e2017-10-11 22:47:13 -05001221 offset + idx);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001222 return 1;
1223 }
1224
1225 return 0;
1226}
1227
1228/*
1229 * Compares two images. Exit codes:
1230 *
1231 * 0 - Images are identical
1232 * 1 - Images differ
1233 * >1 - Error occurred
1234 */
1235static int img_compare(int argc, char **argv)
1236{
Max Reitz40055952014-07-22 22:58:42 +02001237 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001238 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001239 BlockDriverState *bs1, *bs2;
Eric Blake033d9fc2017-10-11 22:47:16 -05001240 int64_t total_size1, total_size2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001241 uint8_t *buf1 = NULL, *buf2 = NULL;
Eric Blake31826642017-10-11 22:47:08 -05001242 int64_t pnum1, pnum2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001243 int allocated1, allocated2;
1244 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1245 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001246 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001247 bool writethrough;
Eric Blake033d9fc2017-10-11 22:47:16 -05001248 int64_t total_size;
1249 int64_t offset = 0;
1250 int64_t chunk;
Eric Blakedc61cd32017-10-11 22:47:14 -05001251 int c;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001252 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001253 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001254 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001255
Max Reitz40055952014-07-22 22:58:42 +02001256 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001257 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001258 static const struct option long_options[] = {
1259 {"help", no_argument, 0, 'h'},
1260 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001261 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001262 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001263 {0, 0, 0, 0}
1264 };
Fam Zheng335e9932017-05-03 00:35:39 +08001265 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001266 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001267 if (c == -1) {
1268 break;
1269 }
1270 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001271 case ':':
1272 missing_argument(argv[optind - 1]);
1273 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001274 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001275 unrecognized_option(argv[optind - 1]);
1276 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001277 case 'h':
1278 help();
1279 break;
1280 case 'f':
1281 fmt1 = optarg;
1282 break;
1283 case 'F':
1284 fmt2 = optarg;
1285 break;
Max Reitz40055952014-07-22 22:58:42 +02001286 case 'T':
1287 cache = optarg;
1288 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001289 case 'p':
1290 progress = true;
1291 break;
1292 case 'q':
1293 quiet = true;
1294 break;
1295 case 's':
1296 strict = true;
1297 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001298 case 'U':
1299 force_share = true;
1300 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001301 case OPTION_OBJECT: {
1302 QemuOpts *opts;
1303 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1304 optarg, true);
1305 if (!opts) {
1306 ret = 2;
1307 goto out4;
1308 }
1309 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001310 case OPTION_IMAGE_OPTS:
1311 image_opts = true;
1312 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001313 }
1314 }
1315
1316 /* Progress is not shown in Quiet mode */
1317 if (quiet) {
1318 progress = false;
1319 }
1320
1321
Kevin Wolffc11eb22013-08-05 10:53:04 +02001322 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001323 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001324 }
1325 filename1 = argv[optind++];
1326 filename2 = argv[optind++];
1327
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001328 if (qemu_opts_foreach(&qemu_object_opts,
1329 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001330 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001331 ret = 2;
1332 goto out4;
1333 }
1334
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001335 /* Initialize before goto out */
1336 qemu_progress_init(progress, 2.0);
1337
Kevin Wolfce099542016-03-15 13:01:04 +01001338 flags = 0;
1339 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001340 if (ret < 0) {
1341 error_report("Invalid source cache option: %s", cache);
1342 ret = 2;
1343 goto out3;
1344 }
1345
Fam Zheng335e9932017-05-03 00:35:39 +08001346 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1347 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001348 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001349 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001350 goto out3;
1351 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001352
Fam Zheng335e9932017-05-03 00:35:39 +08001353 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1354 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001355 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001356 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001357 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001358 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001359 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001360 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001361
Max Reitzf1d3cd72015-02-05 13:58:18 -05001362 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1363 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
Eric Blake033d9fc2017-10-11 22:47:16 -05001364 total_size1 = blk_getlength(blk1);
1365 if (total_size1 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001366 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001367 filename1, strerror(-total_size1));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001368 ret = 4;
1369 goto out;
1370 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001371 total_size2 = blk_getlength(blk2);
1372 if (total_size2 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001373 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001374 filename2, strerror(-total_size2));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001375 ret = 4;
1376 goto out;
1377 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001378 total_size = MIN(total_size1, total_size2);
1379 progress_base = MAX(total_size1, total_size2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001380
1381 qemu_progress_print(0, 100);
1382
Eric Blake033d9fc2017-10-11 22:47:16 -05001383 if (strict && total_size1 != total_size2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001384 ret = 1;
1385 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1386 goto out;
1387 }
1388
Eric Blake033d9fc2017-10-11 22:47:16 -05001389 while (offset < total_size) {
Eric Blake31826642017-10-11 22:47:08 -05001390 int status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001391
Eric Blake033d9fc2017-10-11 22:47:16 -05001392 status1 = bdrv_block_status_above(bs1, NULL, offset,
1393 total_size1 - offset, &pnum1, NULL,
1394 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001395 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001396 ret = 3;
1397 error_report("Sector allocation test failed for %s", filename1);
1398 goto out;
1399 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001400 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001401
Eric Blake033d9fc2017-10-11 22:47:16 -05001402 status2 = bdrv_block_status_above(bs2, NULL, offset,
1403 total_size2 - offset, &pnum2, NULL,
1404 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001405 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001406 ret = 3;
1407 error_report("Sector allocation test failed for %s", filename2);
1408 goto out;
1409 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001410 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
Eric Blake7daddc62017-10-11 22:47:09 -05001411
1412 assert(pnum1 && pnum2);
Eric Blake033d9fc2017-10-11 22:47:16 -05001413 chunk = MIN(pnum1, pnum2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001414
Fam Zheng25ad8e62016-01-13 16:37:41 +08001415 if (strict) {
Eric Blake31826642017-10-11 22:47:08 -05001416 if (status1 != status2) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001417 ret = 1;
1418 qprintf(quiet, "Strict mode: Offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001419 " block status mismatch!\n", offset);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001420 goto out;
1421 }
1422 }
1423 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
Eric Blake7daddc62017-10-11 22:47:09 -05001424 /* nothing to do */
Fam Zheng25ad8e62016-01-13 16:37:41 +08001425 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001426 if (allocated1) {
Eric Blakedc61cd32017-10-11 22:47:14 -05001427 int64_t pnum;
1428
Eric Blake033d9fc2017-10-11 22:47:16 -05001429 chunk = MIN(chunk, IO_BUF_SIZE);
1430 ret = blk_pread(blk1, offset, buf1, chunk);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001431 if (ret < 0) {
1432 error_report("Error while reading offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001433 " of %s: %s",
1434 offset, filename1, strerror(-ret));
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001435 ret = 4;
1436 goto out;
1437 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001438 ret = blk_pread(blk2, offset, buf2, chunk);
1439 if (ret < 0) {
1440 error_report("Error while reading offset %" PRId64
1441 " of %s: %s",
1442 offset, filename2, strerror(-ret));
1443 ret = 4;
1444 goto out;
1445 }
1446 ret = compare_buffers(buf1, buf2, chunk, &pnum);
1447 if (ret || pnum != chunk) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001448 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blake033d9fc2017-10-11 22:47:16 -05001449 offset + (ret ? 0 : pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001450 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001451 goto out;
1452 }
1453 }
1454 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001455 chunk = MIN(chunk, IO_BUF_SIZE);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001456 if (allocated1) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001457 ret = check_empty_sectors(blk1, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001458 filename1, buf1, quiet);
1459 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001460 ret = check_empty_sectors(blk2, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001461 filename2, buf1, quiet);
1462 }
1463 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001464 goto out;
1465 }
1466 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001467 offset += chunk;
1468 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001469 }
1470
Eric Blake033d9fc2017-10-11 22:47:16 -05001471 if (total_size1 != total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001472 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001473 const char *filename_over;
1474
1475 qprintf(quiet, "Warning: Image size mismatch!\n");
Eric Blake033d9fc2017-10-11 22:47:16 -05001476 if (total_size1 > total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001477 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001478 filename_over = filename1;
1479 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001480 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001481 filename_over = filename2;
1482 }
1483
Eric Blake033d9fc2017-10-11 22:47:16 -05001484 while (offset < progress_base) {
1485 ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset,
1486 progress_base - offset, &chunk,
1487 NULL, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001488 if (ret < 0) {
1489 ret = 3;
1490 error_report("Sector allocation test failed for %s",
1491 filename_over);
1492 goto out;
1493
1494 }
Eric Blake391cb1a2017-10-11 22:47:10 -05001495 if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001496 chunk = MIN(chunk, IO_BUF_SIZE);
1497 ret = check_empty_sectors(blk_over, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001498 filename_over, buf1, quiet);
1499 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001500 goto out;
1501 }
1502 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001503 offset += chunk;
1504 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001505 }
1506 }
1507
1508 qprintf(quiet, "Images are identical.\n");
1509 ret = 0;
1510
1511out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001512 qemu_vfree(buf1);
1513 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001514 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001515out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001516 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001517out3:
1518 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001519out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001520 return ret;
1521}
1522
Kevin Wolf690c7302015-03-19 13:33:32 +01001523enum ImgConvertBlockStatus {
1524 BLK_DATA,
1525 BLK_ZERO,
1526 BLK_BACKING_FILE,
1527};
1528
Peter Lieven2d9187b2017-02-28 13:40:07 +01001529#define MAX_COROUTINES 16
1530
Kevin Wolf690c7302015-03-19 13:33:32 +01001531typedef struct ImgConvertState {
1532 BlockBackend **src;
1533 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001534 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001535 int64_t total_sectors;
1536 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001537 int64_t allocated_done;
1538 int64_t sector_num;
1539 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001540 enum ImgConvertBlockStatus status;
1541 int64_t sector_next_status;
1542 BlockBackend *target;
1543 bool has_zero_init;
1544 bool compressed;
1545 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001546 bool wr_in_order;
Kevin Wolf690c7302015-03-19 13:33:32 +01001547 int min_sparse;
1548 size_t cluster_sectors;
1549 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001550 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001551 int running_coroutines;
1552 Coroutine *co[MAX_COROUTINES];
1553 int64_t wait_sector_num[MAX_COROUTINES];
1554 CoMutex lock;
1555 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001556} ImgConvertState;
1557
Peter Lieven2d9187b2017-02-28 13:40:07 +01001558static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1559 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001560{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001561 *src_cur = 0;
1562 *src_cur_offset = 0;
1563 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1564 *src_cur_offset += s->src_sectors[*src_cur];
1565 (*src_cur)++;
1566 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001567 }
1568}
1569
1570static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1571{
Eric Blake31826642017-10-11 22:47:08 -05001572 int64_t src_cur_offset;
1573 int ret, n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001574
Peter Lieven2d9187b2017-02-28 13:40:07 +01001575 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001576
1577 assert(s->total_sectors > sector_num);
1578 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1579
1580 if (s->sector_next_status <= sector_num) {
Eric Blake31826642017-10-11 22:47:08 -05001581 int64_t count = n * BDRV_SECTOR_SIZE;
1582
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001583 if (s->target_has_backing) {
Eric Blake237d78f2017-10-11 22:47:03 -05001584
1585 ret = bdrv_block_status(blk_bs(s->src[src_cur]),
1586 (sector_num - src_cur_offset) *
1587 BDRV_SECTOR_SIZE,
1588 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001589 } else {
Eric Blake31826642017-10-11 22:47:08 -05001590 ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL,
1591 (sector_num - src_cur_offset) *
1592 BDRV_SECTOR_SIZE,
1593 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001594 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001595 if (ret < 0) {
1596 return ret;
1597 }
Eric Blake31826642017-10-11 22:47:08 -05001598 n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
Kevin Wolf690c7302015-03-19 13:33:32 +01001599
1600 if (ret & BDRV_BLOCK_ZERO) {
1601 s->status = BLK_ZERO;
1602 } else if (ret & BDRV_BLOCK_DATA) {
1603 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001604 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001605 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001606 }
1607
1608 s->sector_next_status = sector_num + n;
1609 }
1610
1611 n = MIN(n, s->sector_next_status - sector_num);
1612 if (s->status == BLK_DATA) {
1613 n = MIN(n, s->buf_sectors);
1614 }
1615
1616 /* We need to write complete clusters for compressed images, so if an
1617 * unallocated area is shorter than that, we must consider the whole
1618 * cluster allocated. */
1619 if (s->compressed) {
1620 if (n < s->cluster_sectors) {
1621 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1622 s->status = BLK_DATA;
1623 } else {
1624 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1625 }
1626 }
1627
1628 return n;
1629}
1630
Peter Lieven2d9187b2017-02-28 13:40:07 +01001631static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1632 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001633{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001634 int n, ret;
1635 QEMUIOVector qiov;
1636 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001637
Kevin Wolf690c7302015-03-19 13:33:32 +01001638 assert(nb_sectors <= s->buf_sectors);
1639 while (nb_sectors > 0) {
1640 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001641 int src_cur;
1642 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001643
1644 /* In the case of compression with multiple source files, we can get a
1645 * nb_sectors that spreads into the next part. So we must be able to
1646 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001647 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1648 blk = s->src[src_cur];
1649 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001650
Peter Lieven2d9187b2017-02-28 13:40:07 +01001651 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1652 iov.iov_base = buf;
1653 iov.iov_len = n << BDRV_SECTOR_BITS;
1654 qemu_iovec_init_external(&qiov, &iov, 1);
1655
1656 ret = blk_co_preadv(
1657 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1658 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001659 if (ret < 0) {
1660 return ret;
1661 }
1662
1663 sector_num += n;
1664 nb_sectors -= n;
1665 buf += n * BDRV_SECTOR_SIZE;
1666 }
1667
1668 return 0;
1669}
1670
Peter Lieven2d9187b2017-02-28 13:40:07 +01001671
1672static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1673 int nb_sectors, uint8_t *buf,
1674 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001675{
1676 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001677 QEMUIOVector qiov;
1678 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001679
1680 while (nb_sectors > 0) {
1681 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001682 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1683
Peter Lieven2d9187b2017-02-28 13:40:07 +01001684 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001685 case BLK_BACKING_FILE:
1686 /* If we have a backing file, leave clusters unallocated that are
1687 * unallocated in the source image, so that the backing file is
1688 * visible at the respective offset. */
1689 assert(s->target_has_backing);
1690 break;
1691
1692 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001693 /* If we're told to keep the target fully allocated (-S 0) or there
1694 * is real non-zero data, we must write it. Otherwise we can treat
1695 * it as zero sectors.
1696 * Compressed clusters need to be written as a whole, so in that
1697 * case we can only save the write if the buffer is completely
1698 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001699 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001700 (!s->compressed &&
1701 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1702 (s->compressed &&
1703 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001704 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001705 iov.iov_base = buf;
1706 iov.iov_len = n << BDRV_SECTOR_BITS;
1707 qemu_iovec_init_external(&qiov, &iov, 1);
1708
1709 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
Lidong Chendb933fb2017-04-27 10:58:27 +08001710 n << BDRV_SECTOR_BITS, &qiov, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001711 if (ret < 0) {
1712 return ret;
1713 }
1714 break;
1715 }
1716 /* fall-through */
1717
1718 case BLK_ZERO:
1719 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001720 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001721 break;
1722 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001723 ret = blk_co_pwrite_zeroes(s->target,
1724 sector_num << BDRV_SECTOR_BITS,
1725 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001726 if (ret < 0) {
1727 return ret;
1728 }
1729 break;
1730 }
1731
1732 sector_num += n;
1733 nb_sectors -= n;
1734 buf += n * BDRV_SECTOR_SIZE;
1735 }
1736
1737 return 0;
1738}
1739
Peter Lieven2d9187b2017-02-28 13:40:07 +01001740static void coroutine_fn convert_co_do_copy(void *opaque)
1741{
1742 ImgConvertState *s = opaque;
1743 uint8_t *buf = NULL;
1744 int ret, i;
1745 int index = -1;
1746
1747 for (i = 0; i < s->num_coroutines; i++) {
1748 if (s->co[i] == qemu_coroutine_self()) {
1749 index = i;
1750 break;
1751 }
1752 }
1753 assert(index >= 0);
1754
1755 s->running_coroutines++;
1756 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1757
1758 while (1) {
1759 int n;
1760 int64_t sector_num;
1761 enum ImgConvertBlockStatus status;
1762
1763 qemu_co_mutex_lock(&s->lock);
1764 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1765 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001766 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001767 }
1768 n = convert_iteration_sectors(s, s->sector_num);
1769 if (n < 0) {
1770 qemu_co_mutex_unlock(&s->lock);
1771 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001772 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001773 }
1774 /* save current sector and allocation status to local variables */
1775 sector_num = s->sector_num;
1776 status = s->status;
1777 if (!s->min_sparse && s->status == BLK_ZERO) {
1778 n = MIN(n, s->buf_sectors);
1779 }
1780 /* increment global sector counter so that other coroutines can
1781 * already continue reading beyond this request */
1782 s->sector_num += n;
1783 qemu_co_mutex_unlock(&s->lock);
1784
1785 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1786 s->allocated_done += n;
1787 qemu_progress_print(100.0 * s->allocated_done /
1788 s->allocated_sectors, 0);
1789 }
1790
1791 if (status == BLK_DATA) {
1792 ret = convert_co_read(s, sector_num, n, buf);
1793 if (ret < 0) {
1794 error_report("error while reading sector %" PRId64
1795 ": %s", sector_num, strerror(-ret));
1796 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001797 }
1798 } else if (!s->min_sparse && status == BLK_ZERO) {
1799 status = BLK_DATA;
1800 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1801 }
1802
1803 if (s->wr_in_order) {
1804 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03001805 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001806 s->wait_sector_num[index] = sector_num;
1807 qemu_coroutine_yield();
1808 }
1809 s->wait_sector_num[index] = -1;
1810 }
1811
Anton Nefedovb91127e2017-04-26 11:33:15 +03001812 if (s->ret == -EINPROGRESS) {
1813 ret = convert_co_write(s, sector_num, n, buf, status);
1814 if (ret < 0) {
1815 error_report("error while writing sector %" PRId64
1816 ": %s", sector_num, strerror(-ret));
1817 s->ret = ret;
1818 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001819 }
1820
1821 if (s->wr_in_order) {
1822 /* reenter the coroutine that might have waited
1823 * for this write to complete */
1824 s->wr_offs = sector_num + n;
1825 for (i = 0; i < s->num_coroutines; i++) {
1826 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1827 /*
1828 * A -> B -> A cannot occur because A has
1829 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1830 * B will never enter A during this time window.
1831 */
1832 qemu_coroutine_enter(s->co[i]);
1833 break;
1834 }
1835 }
1836 }
1837 }
1838
Peter Lieven2d9187b2017-02-28 13:40:07 +01001839 qemu_vfree(buf);
1840 s->co[index] = NULL;
1841 s->running_coroutines--;
1842 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1843 /* the convert job finished successfully */
1844 s->ret = 0;
1845 }
1846}
1847
Kevin Wolf690c7302015-03-19 13:33:32 +01001848static int convert_do_copy(ImgConvertState *s)
1849{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001850 int ret, i, n;
1851 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001852
1853 /* Check whether we have zero initialisation or can get it efficiently */
1854 s->has_zero_init = s->min_sparse && !s->target_has_backing
1855 ? bdrv_has_zero_init(blk_bs(s->target))
1856 : false;
1857
1858 if (!s->has_zero_init && !s->target_has_backing &&
1859 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1860 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001861 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001862 if (ret == 0) {
1863 s->has_zero_init = true;
1864 }
1865 }
1866
1867 /* Allocate buffer for copied data. For compressed images, only one cluster
1868 * can be copied at a time. */
1869 if (s->compressed) {
1870 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1871 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001872 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001873 }
1874 s->buf_sectors = s->cluster_sectors;
1875 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001876
Kevin Wolf690c7302015-03-19 13:33:32 +01001877 while (sector_num < s->total_sectors) {
1878 n = convert_iteration_sectors(s, sector_num);
1879 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001880 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001881 }
Max Reitzaad15de2016-03-24 23:33:57 +01001882 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1883 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001884 s->allocated_sectors += n;
1885 }
1886 sector_num += n;
1887 }
1888
1889 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001890 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001891 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001892
Peter Lieven2d9187b2017-02-28 13:40:07 +01001893 qemu_co_mutex_init(&s->lock);
1894 for (i = 0; i < s->num_coroutines; i++) {
1895 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1896 s->wait_sector_num[i] = -1;
1897 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001898 }
1899
Anton Nefedovb91127e2017-04-26 11:33:15 +03001900 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001901 main_loop_wait(false);
1902 }
1903
1904 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001905 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001906 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001907 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001908 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001909 }
1910 }
1911
Peter Lieven2d9187b2017-02-28 13:40:07 +01001912 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001913}
1914
bellardea2384d2004-08-01 21:59:26 +00001915static int img_convert(int argc, char **argv)
1916{
Peter Lieven9fd77f92017-04-21 11:11:55 +02001917 int c, bs_i, flags, src_flags = 0;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001918 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02001919 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1920 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001921 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00001922 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001923 BlockDriverState *out_bs;
1924 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08001925 QemuOptsList *create_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001926 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02001927 Error *local_err = NULL;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001928 bool writethrough, src_writethrough, quiet = false, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001929 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001930 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08001931 bool force_share = false;
bellardea2384d2004-08-01 21:59:26 +00001932
Peter Lieven9fd77f92017-04-21 11:11:55 +02001933 ImgConvertState s = (ImgConvertState) {
1934 /* Need at least 4k of zeros for sparse detection */
1935 .min_sparse = 8,
1936 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1937 .wr_in_order = true,
1938 .num_coroutines = 8,
1939 };
1940
bellardea2384d2004-08-01 21:59:26 +00001941 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001942 static const struct option long_options[] = {
1943 {"help", no_argument, 0, 'h'},
1944 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001945 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001946 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001947 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001948 {0, 0, 0, 0}
1949 };
Daniel P. Berrange6b4df542017-07-04 13:30:09 +01001950 c = getopt_long(argc, argv, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001951 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001952 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001953 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001954 }
bellardea2384d2004-08-01 21:59:26 +00001955 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001956 case ':':
1957 missing_argument(argv[optind - 1]);
1958 break;
Jes Sorensenef873942010-12-06 15:25:40 +01001959 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001960 unrecognized_option(argv[optind - 1]);
1961 break;
bellardea2384d2004-08-01 21:59:26 +00001962 case 'h':
1963 help();
1964 break;
1965 case 'f':
1966 fmt = optarg;
1967 break;
1968 case 'O':
1969 out_fmt = optarg;
1970 break;
thsf58c7b32008-06-05 21:53:49 +00001971 case 'B':
1972 out_baseimg = optarg;
1973 break;
bellardea2384d2004-08-01 21:59:26 +00001974 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02001975 s.compressed = true;
bellardea2384d2004-08-01 21:59:26 +00001976 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001977 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001978 if (!is_valid_option_list(optarg)) {
1979 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001980 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001981 }
1982 if (!options) {
1983 options = g_strdup(optarg);
1984 } else {
1985 char *old_options = options;
1986 options = g_strdup_printf("%s,%s", options, optarg);
1987 g_free(old_options);
1988 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001989 break;
edison51ef6722010-09-21 19:58:41 -07001990 case 's':
1991 snapshot_name = optarg;
1992 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001993 case 'l':
1994 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01001995 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
1996 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08001997 if (!sn_opts) {
1998 error_report("Failed in parsing snapshot param '%s'",
1999 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002000 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002001 }
2002 } else {
2003 snapshot_name = optarg;
2004 }
2005 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002006 case 'S':
2007 {
2008 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002009
2010 sval = cvtnum(optarg);
2011 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02002012 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002013 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002014 }
2015
Peter Lieven9fd77f92017-04-21 11:11:55 +02002016 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002017 break;
2018 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002019 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002020 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002021 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002022 case 't':
2023 cache = optarg;
2024 break;
Max Reitz40055952014-07-22 22:58:42 +02002025 case 'T':
2026 src_cache = optarg;
2027 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002028 case 'q':
2029 quiet = true;
2030 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002031 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002032 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002033 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002034 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002035 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2036 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002037 error_report("Invalid number of coroutines. Allowed number of"
2038 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002039 goto fail_getopt;
2040 }
2041 break;
2042 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002043 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002044 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002045 case 'U':
2046 force_share = true;
2047 break;
Max Reitz3258b912017-04-26 15:46:47 +02002048 case OPTION_OBJECT: {
2049 QemuOpts *object_opts;
2050 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2051 optarg, true);
2052 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002053 goto fail_getopt;
2054 }
2055 break;
Max Reitz3258b912017-04-26 15:46:47 +02002056 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002057 case OPTION_IMAGE_OPTS:
2058 image_opts = true;
2059 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002060 case OPTION_TARGET_IMAGE_OPTS:
2061 tgt_image_opts = true;
2062 break;
bellardea2384d2004-08-01 21:59:26 +00002063 }
2064 }
ths3b46e622007-09-17 08:09:54 +00002065
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002066 if (!out_fmt && !tgt_image_opts) {
2067 out_fmt = "raw";
2068 }
2069
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002070 if (qemu_opts_foreach(&qemu_object_opts,
2071 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002072 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002073 goto fail_getopt;
2074 }
2075
Peter Lieven9fd77f92017-04-21 11:11:55 +02002076 if (!s.wr_in_order && s.compressed) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002077 error_report("Out of order write and compress are mutually exclusive");
Peter Lieven9fd77f92017-04-21 11:11:55 +02002078 goto fail_getopt;
2079 }
2080
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002081 if (tgt_image_opts && !skip_create) {
2082 error_report("--target-image-opts requires use of -n flag");
2083 goto fail_getopt;
2084 }
2085
Peter Lieven9fd77f92017-04-21 11:11:55 +02002086 s.src_num = argc - optind - 1;
2087 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2088
2089 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002090 if (out_fmt) {
2091 ret = print_block_option_help(out_filename, out_fmt);
2092 goto fail_getopt;
2093 } else {
2094 error_report("Option help requires a format be specified");
2095 goto fail_getopt;
2096 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002097 }
2098
2099 if (s.src_num < 1) {
2100 error_report("Must specify image file name");
2101 goto fail_getopt;
2102 }
2103
2104
Peter Lieven9fd77f92017-04-21 11:11:55 +02002105 /* ret is still -EINVAL until here */
2106 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2107 if (ret < 0) {
2108 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002109 goto fail_getopt;
2110 }
2111
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002112 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002113 if (quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002114 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002115 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002116 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002117 qemu_progress_print(0, 100);
2118
Peter Lieven9fd77f92017-04-21 11:11:55 +02002119 s.src = g_new0(BlockBackend *, s.src_num);
2120 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002121
Peter Lieven9fd77f92017-04-21 11:11:55 +02002122 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2123 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Fam Zheng335e9932017-05-03 00:35:39 +08002124 fmt, src_flags, src_writethrough, quiet,
2125 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002126 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002127 ret = -1;
2128 goto out;
2129 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002130 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2131 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002132 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002133 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002134 ret = -1;
2135 goto out;
2136 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002137 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002138 }
bellardea2384d2004-08-01 21:59:26 +00002139
Wenchao Xiaef806542013-12-04 17:10:57 +08002140 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002141 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002142 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2143 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2144 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002145 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002146 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002147 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002148 ret = -1;
2149 goto out;
2150 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002151
Peter Lieven9fd77f92017-04-21 11:11:55 +02002152 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2153 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002154 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002155 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002156 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002157 ret = -1;
2158 goto out;
edison51ef6722010-09-21 19:58:41 -07002159 }
2160
Max Reitz2e024cd2015-02-11 09:58:46 -05002161 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002162 /* Find driver and parse its options */
2163 drv = bdrv_find_format(out_fmt);
2164 if (!drv) {
2165 error_report("Unknown file format '%s'", out_fmt);
2166 ret = -1;
2167 goto out;
2168 }
2169
2170 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2171 if (!proto_drv) {
2172 error_report_err(local_err);
2173 ret = -1;
2174 goto out;
2175 }
2176
Max Reitz2e024cd2015-02-11 09:58:46 -05002177 if (!drv->create_opts) {
2178 error_report("Format driver '%s' does not support image creation",
2179 drv->format_name);
2180 ret = -1;
2181 goto out;
2182 }
Max Reitzf75613c2014-12-02 18:32:46 +01002183
Max Reitz2e024cd2015-02-11 09:58:46 -05002184 if (!proto_drv->create_opts) {
2185 error_report("Protocol driver '%s' does not support image creation",
2186 proto_drv->format_name);
2187 ret = -1;
2188 goto out;
2189 }
Max Reitzf75613c2014-12-02 18:32:46 +01002190
Max Reitz2e024cd2015-02-11 09:58:46 -05002191 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2192 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002193
Max Reitz2e024cd2015-02-11 09:58:46 -05002194 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002195 if (options) {
2196 qemu_opts_do_parse(opts, options, NULL, &local_err);
2197 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002198 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002199 ret = -1;
2200 goto out;
2201 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002202 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002203
Peter Lieven9fd77f92017-04-21 11:11:55 +02002204 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002205 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002206 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2207 if (ret < 0) {
2208 goto out;
2209 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002210 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002211
Kevin Wolfa18953f2010-10-14 15:46:04 +02002212 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002213 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002214 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002215 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002216 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002217 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002218
Max Reitz48758a82017-04-26 15:46:48 +02002219 if (s.src_num > 1 && out_baseimg) {
2220 error_report("Having a backing file for the target makes no sense when "
2221 "concatenating multiple input images");
2222 ret = -1;
2223 goto out;
2224 }
2225
Kevin Wolfefa84d42009-05-18 16:42:12 +02002226 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002227 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002228 bool encryption =
2229 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002230 const char *encryptfmt =
2231 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
Chunyan Liu83d05212014-06-05 17:20:51 +08002232 const char *preallocation =
2233 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002234
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002235 if (drv && !drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002236 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002237 ret = -1;
2238 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002239 }
2240
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002241 if (encryption || encryptfmt) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002242 error_report("Compression and encryption not supported at "
2243 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002244 ret = -1;
2245 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002246 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002247
Chunyan Liu83d05212014-06-05 17:20:51 +08002248 if (preallocation
2249 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002250 {
2251 error_report("Compression and preallocation not supported at "
2252 "the same time");
2253 ret = -1;
2254 goto out;
2255 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002256 }
2257
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002258 if (!skip_create) {
2259 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002260 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002261 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002262 error_reportf_err(local_err, "%s: error while converting %s: ",
2263 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002264 goto out;
bellardea2384d2004-08-01 21:59:26 +00002265 }
2266 }
ths3b46e622007-09-17 08:09:54 +00002267
Peter Lieven9fd77f92017-04-21 11:11:55 +02002268 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002269 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002270 if (ret < 0) {
2271 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002272 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002273 }
2274
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002275 if (skip_create) {
2276 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2277 flags, writethrough, quiet, false);
2278 } else {
2279 /* TODO ultimately we should allow --target-image-opts
2280 * to be used even when -n is not given.
2281 * That has to wait for bdrv_create to be improved
2282 * to allow filenames in option syntax
2283 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01002284 s.target = img_open_new_file(out_filename, opts, out_fmt,
2285 flags, writethrough, quiet, false);
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002286 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002287 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002288 ret = -1;
2289 goto out;
2290 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002291 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002292
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002293 if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2294 error_report("Compression not supported for this file format");
2295 ret = -1;
2296 goto out;
2297 }
2298
Eric Blake5def6b82016-06-23 16:37:19 -06002299 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002300 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2301 * as maximum. */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002302 s.buf_sectors = MIN(32768,
2303 MAX(s.buf_sectors,
2304 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2305 out_bs->bl.pdiscard_alignment >>
2306 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002307
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002308 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002309 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002310 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002311 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002312 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002313 ret = -1;
2314 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002315 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002316 error_report("output file is smaller than input file");
2317 ret = -1;
2318 goto out;
2319 }
2320 }
2321
Peter Lieven24f833c2013-11-27 11:07:07 +01002322 ret = bdrv_get_info(out_bs, &bdi);
2323 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002324 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002325 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002326 goto out;
2327 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002328 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002329 s.compressed = s.compressed || bdi.needs_compressed_writes;
2330 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Peter Lieven24f833c2013-11-27 11:07:07 +01002331 }
2332
Peter Lieven9fd77f92017-04-21 11:11:55 +02002333 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002334out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002335 if (!ret) {
2336 qemu_progress_print(100, 0);
2337 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002338 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002339 qemu_opts_del(opts);
2340 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002341 qemu_opts_del(sn_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002342 blk_unref(s.target);
2343 if (s.src) {
2344 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2345 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002346 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002347 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002348 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002349 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002350fail_getopt:
2351 g_free(options);
2352
Peter Lieven9fd77f92017-04-21 11:11:55 +02002353 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002354}
2355
bellard57d1a2b2004-08-03 21:15:11 +00002356
bellardfaea38e2006-08-05 21:31:00 +00002357static void dump_snapshots(BlockDriverState *bs)
2358{
2359 QEMUSnapshotInfo *sn_tab, *sn;
2360 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002361
2362 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2363 if (nb_sns <= 0)
2364 return;
2365 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002366 bdrv_snapshot_dump(fprintf, stdout, NULL);
2367 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002368 for(i = 0; i < nb_sns; i++) {
2369 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002370 bdrv_snapshot_dump(fprintf, stdout, sn);
2371 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002372 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002373 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002374}
2375
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002376static void dump_json_image_info_list(ImageInfoList *list)
2377{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002378 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002379 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002380 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002381
2382 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2383 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002384 str = qobject_to_json_pretty(obj);
2385 assert(str != NULL);
2386 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002387 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002388 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002389 qobject_unref(str);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002390}
2391
Benoît Canetc054b3f2012-09-05 13:09:02 +02002392static void dump_json_image_info(ImageInfo *info)
2393{
Benoît Canetc054b3f2012-09-05 13:09:02 +02002394 QString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002395 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002396 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002397
2398 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2399 visit_complete(v, &obj);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002400 str = qobject_to_json_pretty(obj);
2401 assert(str != NULL);
2402 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002403 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002404 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002405 qobject_unref(str);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002406}
2407
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002408static void dump_human_image_info_list(ImageInfoList *list)
2409{
2410 ImageInfoList *elem;
2411 bool delim = false;
2412
2413 for (elem = list; elem; elem = elem->next) {
2414 if (delim) {
2415 printf("\n");
2416 }
2417 delim = true;
2418
Wenchao Xia5b917042013-05-25 11:09:45 +08002419 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002420 }
2421}
2422
2423static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2424{
2425 return strcmp(a, b) == 0;
2426}
2427
2428/**
2429 * Open an image file chain and return an ImageInfoList
2430 *
2431 * @filename: topmost image filename
2432 * @fmt: topmost image format (may be NULL to autodetect)
2433 * @chain: true - enumerate entire backing file chain
2434 * false - only topmost image file
2435 *
2436 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2437 * image file. If there was an error a message will have been printed to
2438 * stderr.
2439 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002440static ImageInfoList *collect_image_info_list(bool image_opts,
2441 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002442 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002443 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002444{
2445 ImageInfoList *head = NULL;
2446 ImageInfoList **last = &head;
2447 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002448 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002449
2450 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2451
2452 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002453 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002454 BlockDriverState *bs;
2455 ImageInfo *info;
2456 ImageInfoList *elem;
2457
2458 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2459 error_report("Backing file '%s' creates an infinite loop.",
2460 filename);
2461 goto err;
2462 }
2463 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2464
Max Reitzefaa7c42016-03-16 19:54:38 +01002465 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002466 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2467 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002468 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002469 goto err;
2470 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002471 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002472
Wenchao Xia43526ec2013-06-06 12:27:58 +08002473 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002474 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002475 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002476 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002477 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002478 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002479
2480 elem = g_new0(ImageInfoList, 1);
2481 elem->value = info;
2482 *last = elem;
2483 last = &elem->next;
2484
Markus Armbruster26f54e92014-10-07 13:59:04 +02002485 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002486
2487 filename = fmt = NULL;
2488 if (chain) {
2489 if (info->has_full_backing_filename) {
2490 filename = info->full_backing_filename;
2491 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002492 error_report("Could not determine absolute backing filename,"
2493 " but backing filename '%s' present",
2494 info->backing_filename);
2495 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002496 }
2497 if (info->has_backing_filename_format) {
2498 fmt = info->backing_filename_format;
2499 }
2500 }
2501 }
2502 g_hash_table_destroy(filenames);
2503 return head;
2504
2505err:
2506 qapi_free_ImageInfoList(head);
2507 g_hash_table_destroy(filenames);
2508 return NULL;
2509}
2510
Benoît Canetc054b3f2012-09-05 13:09:02 +02002511static int img_info(int argc, char **argv)
2512{
2513 int c;
2514 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002515 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002516 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002517 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002518 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002519 bool force_share = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002520
bellardea2384d2004-08-01 21:59:26 +00002521 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002522 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002523 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002524 int option_index = 0;
2525 static const struct option long_options[] = {
2526 {"help", no_argument, 0, 'h'},
2527 {"format", required_argument, 0, 'f'},
2528 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002529 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002530 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002531 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002532 {"force-share", no_argument, 0, 'U'},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002533 {0, 0, 0, 0}
2534 };
Fam Zheng335e9932017-05-03 00:35:39 +08002535 c = getopt_long(argc, argv, ":f:hU",
Benoît Canetc054b3f2012-09-05 13:09:02 +02002536 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002537 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002538 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002539 }
bellardea2384d2004-08-01 21:59:26 +00002540 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002541 case ':':
2542 missing_argument(argv[optind - 1]);
2543 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002544 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002545 unrecognized_option(argv[optind - 1]);
2546 break;
bellardea2384d2004-08-01 21:59:26 +00002547 case 'h':
2548 help();
2549 break;
2550 case 'f':
2551 fmt = optarg;
2552 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002553 case 'U':
2554 force_share = true;
2555 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002556 case OPTION_OUTPUT:
2557 output = optarg;
2558 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002559 case OPTION_BACKING_CHAIN:
2560 chain = true;
2561 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002562 case OPTION_OBJECT: {
2563 QemuOpts *opts;
2564 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2565 optarg, true);
2566 if (!opts) {
2567 return 1;
2568 }
2569 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002570 case OPTION_IMAGE_OPTS:
2571 image_opts = true;
2572 break;
bellardea2384d2004-08-01 21:59:26 +00002573 }
2574 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002575 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002576 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002577 }
bellardea2384d2004-08-01 21:59:26 +00002578 filename = argv[optind++];
2579
Benoît Canetc054b3f2012-09-05 13:09:02 +02002580 if (output && !strcmp(output, "json")) {
2581 output_format = OFORMAT_JSON;
2582 } else if (output && !strcmp(output, "human")) {
2583 output_format = OFORMAT_HUMAN;
2584 } else if (output) {
2585 error_report("--output must be used with human or json as argument.");
2586 return 1;
2587 }
2588
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002589 if (qemu_opts_foreach(&qemu_object_opts,
2590 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002591 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002592 return 1;
2593 }
2594
Fam Zheng335e9932017-05-03 00:35:39 +08002595 list = collect_image_info_list(image_opts, filename, fmt, chain,
2596 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002597 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002598 return 1;
2599 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002600
Benoît Canetc054b3f2012-09-05 13:09:02 +02002601 switch (output_format) {
2602 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002603 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002604 break;
2605 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002606 if (chain) {
2607 dump_json_image_info_list(list);
2608 } else {
2609 dump_json_image_info(list->value);
2610 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002611 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002612 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002613
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002614 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002615 return 0;
2616}
2617
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002618static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2619 MapEntry *next)
2620{
2621 switch (output_format) {
2622 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002623 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002624 error_report("File contains external, encrypted or compressed clusters.");
2625 exit(1);
2626 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002627 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002628 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002629 e->start, e->length,
2630 e->has_offset ? e->offset : 0,
2631 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002632 }
2633 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2634 * Modify the flags here to allow more coalescing.
2635 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002636 if (next && (!next->data || next->zero)) {
2637 next->data = false;
2638 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002639 }
2640 break;
2641 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002642 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2643 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002644 (e->start == 0 ? "[" : ",\n"),
2645 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002646 e->zero ? "true" : "false",
2647 e->data ? "true" : "false");
2648 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002649 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002650 }
2651 putchar('}');
2652
2653 if (!next) {
2654 printf("]\n");
2655 }
2656 break;
2657 }
2658}
2659
Eric Blake5e344dd2017-10-11 22:47:02 -05002660static int get_block_status(BlockDriverState *bs, int64_t offset,
2661 int64_t bytes, MapEntry *e)
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002662{
Eric Blake237d78f2017-10-11 22:47:03 -05002663 int ret;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002664 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002665 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002666 bool has_offset;
Eric Blake237d78f2017-10-11 22:47:03 -05002667 int64_t map;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002668
2669 /* As an optimization, we could cache the current range of unallocated
2670 * clusters in each file of the chain, and avoid querying the same
2671 * range repeatedly.
2672 */
2673
2674 depth = 0;
2675 for (;;) {
Eric Blake237d78f2017-10-11 22:47:03 -05002676 ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002677 if (ret < 0) {
2678 return ret;
2679 }
Eric Blake237d78f2017-10-11 22:47:03 -05002680 assert(bytes);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002681 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2682 break;
2683 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002684 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002685 if (bs == NULL) {
2686 ret = 0;
2687 break;
2688 }
2689
2690 depth++;
2691 }
2692
John Snow28756452016-02-05 13:12:33 -05002693 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2694
2695 *e = (MapEntry) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002696 .start = offset,
Eric Blake237d78f2017-10-11 22:47:03 -05002697 .length = bytes,
John Snow28756452016-02-05 13:12:33 -05002698 .data = !!(ret & BDRV_BLOCK_DATA),
2699 .zero = !!(ret & BDRV_BLOCK_ZERO),
Eric Blake237d78f2017-10-11 22:47:03 -05002700 .offset = map,
John Snow28756452016-02-05 13:12:33 -05002701 .has_offset = has_offset,
2702 .depth = depth,
2703 .has_filename = file && has_offset,
2704 .filename = file && has_offset ? file->filename : NULL,
2705 };
2706
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002707 return 0;
2708}
2709
Fam Zheng16b0d552016-01-26 11:59:02 +08002710static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2711{
2712 if (curr->length == 0) {
2713 return false;
2714 }
2715 if (curr->zero != next->zero ||
2716 curr->data != next->data ||
2717 curr->depth != next->depth ||
2718 curr->has_filename != next->has_filename ||
2719 curr->has_offset != next->has_offset) {
2720 return false;
2721 }
2722 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2723 return false;
2724 }
2725 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2726 return false;
2727 }
2728 return true;
2729}
2730
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002731static int img_map(int argc, char **argv)
2732{
2733 int c;
2734 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002735 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002736 BlockDriverState *bs;
2737 const char *filename, *fmt, *output;
2738 int64_t length;
2739 MapEntry curr = { .length = 0 }, next;
2740 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002741 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002742 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002743
2744 fmt = NULL;
2745 output = NULL;
2746 for (;;) {
2747 int option_index = 0;
2748 static const struct option long_options[] = {
2749 {"help", no_argument, 0, 'h'},
2750 {"format", required_argument, 0, 'f'},
2751 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002752 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002753 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002754 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002755 {0, 0, 0, 0}
2756 };
Fam Zheng335e9932017-05-03 00:35:39 +08002757 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002758 long_options, &option_index);
2759 if (c == -1) {
2760 break;
2761 }
2762 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002763 case ':':
2764 missing_argument(argv[optind - 1]);
2765 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002766 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002767 unrecognized_option(argv[optind - 1]);
2768 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002769 case 'h':
2770 help();
2771 break;
2772 case 'f':
2773 fmt = optarg;
2774 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002775 case 'U':
2776 force_share = true;
2777 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002778 case OPTION_OUTPUT:
2779 output = optarg;
2780 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002781 case OPTION_OBJECT: {
2782 QemuOpts *opts;
2783 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2784 optarg, true);
2785 if (!opts) {
2786 return 1;
2787 }
2788 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002789 case OPTION_IMAGE_OPTS:
2790 image_opts = true;
2791 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002792 }
2793 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002794 if (optind != argc - 1) {
2795 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002796 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002797 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002798
2799 if (output && !strcmp(output, "json")) {
2800 output_format = OFORMAT_JSON;
2801 } else if (output && !strcmp(output, "human")) {
2802 output_format = OFORMAT_HUMAN;
2803 } else if (output) {
2804 error_report("--output must be used with human or json as argument.");
2805 return 1;
2806 }
2807
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002808 if (qemu_opts_foreach(&qemu_object_opts,
2809 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002810 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002811 return 1;
2812 }
2813
Fam Zheng335e9932017-05-03 00:35:39 +08002814 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002815 if (!blk) {
2816 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002817 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002818 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002819
2820 if (output_format == OFORMAT_HUMAN) {
2821 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2822 }
2823
Max Reitzf1d3cd72015-02-05 13:58:18 -05002824 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002825 while (curr.start + curr.length < length) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002826 int64_t offset = curr.start + curr.length;
2827 int64_t n;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002828
2829 /* Probe up to 1 GiB at a time. */
Eric Blake5e344dd2017-10-11 22:47:02 -05002830 n = QEMU_ALIGN_DOWN(MIN(1 << 30, length - offset), BDRV_SECTOR_SIZE);
2831 ret = get_block_status(bs, offset, n, &next);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002832
2833 if (ret < 0) {
2834 error_report("Could not read file metadata: %s", strerror(-ret));
2835 goto out;
2836 }
2837
Fam Zheng16b0d552016-01-26 11:59:02 +08002838 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002839 curr.length += next.length;
2840 continue;
2841 }
2842
2843 if (curr.length > 0) {
2844 dump_map_entry(output_format, &curr, &next);
2845 }
2846 curr = next;
2847 }
2848
2849 dump_map_entry(output_format, &curr, NULL);
2850
2851out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002852 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002853 return ret < 0;
2854}
2855
aliguorif7b4a942009-01-07 17:40:15 +00002856#define SNAPSHOT_LIST 1
2857#define SNAPSHOT_CREATE 2
2858#define SNAPSHOT_APPLY 3
2859#define SNAPSHOT_DELETE 4
2860
Stuart Brady153859b2009-06-07 00:42:17 +01002861static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002862{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002863 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002864 BlockDriverState *bs;
2865 QEMUSnapshotInfo sn;
2866 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002867 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002868 int action = 0;
2869 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002870 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002871 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002872 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002873 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00002874
Kevin Wolfce099542016-03-15 13:01:04 +01002875 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002876 /* Parse commandline parameters */
2877 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002878 static const struct option long_options[] = {
2879 {"help", no_argument, 0, 'h'},
2880 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002881 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002882 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002883 {0, 0, 0, 0}
2884 };
Fam Zheng335e9932017-05-03 00:35:39 +08002885 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002886 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002887 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002888 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002889 }
aliguorif7b4a942009-01-07 17:40:15 +00002890 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002891 case ':':
2892 missing_argument(argv[optind - 1]);
2893 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002894 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002895 unrecognized_option(argv[optind - 1]);
2896 break;
aliguorif7b4a942009-01-07 17:40:15 +00002897 case 'h':
2898 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002899 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002900 case 'l':
2901 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002902 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002903 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002904 }
2905 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002906 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002907 break;
2908 case 'a':
2909 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002910 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002911 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002912 }
2913 action = SNAPSHOT_APPLY;
2914 snapshot_name = optarg;
2915 break;
2916 case 'c':
2917 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002918 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002919 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002920 }
2921 action = SNAPSHOT_CREATE;
2922 snapshot_name = optarg;
2923 break;
2924 case 'd':
2925 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002926 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002927 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002928 }
2929 action = SNAPSHOT_DELETE;
2930 snapshot_name = optarg;
2931 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002932 case 'q':
2933 quiet = true;
2934 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002935 case 'U':
2936 force_share = true;
2937 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002938 case OPTION_OBJECT: {
2939 QemuOpts *opts;
2940 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2941 optarg, true);
2942 if (!opts) {
2943 return 1;
2944 }
2945 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002946 case OPTION_IMAGE_OPTS:
2947 image_opts = true;
2948 break;
aliguorif7b4a942009-01-07 17:40:15 +00002949 }
2950 }
2951
Kevin Wolffc11eb22013-08-05 10:53:04 +02002952 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002953 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002954 }
aliguorif7b4a942009-01-07 17:40:15 +00002955 filename = argv[optind++];
2956
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002957 if (qemu_opts_foreach(&qemu_object_opts,
2958 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002959 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002960 return 1;
2961 }
2962
aliguorif7b4a942009-01-07 17:40:15 +00002963 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08002964 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
2965 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002966 if (!blk) {
2967 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002968 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002969 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002970
2971 /* Perform the requested action */
2972 switch(action) {
2973 case SNAPSHOT_LIST:
2974 dump_snapshots(bs);
2975 break;
2976
2977 case SNAPSHOT_CREATE:
2978 memset(&sn, 0, sizeof(sn));
2979 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2980
2981 qemu_gettimeofday(&tv);
2982 sn.date_sec = tv.tv_sec;
2983 sn.date_nsec = tv.tv_usec * 1000;
2984
2985 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002986 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002987 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002988 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002989 }
aliguorif7b4a942009-01-07 17:40:15 +00002990 break;
2991
2992 case SNAPSHOT_APPLY:
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01002993 ret = bdrv_snapshot_goto(bs, snapshot_name, &err);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002994 if (ret) {
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01002995 error_reportf_err(err, "Could not apply snapshot '%s': ",
2996 snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002997 }
aliguorif7b4a942009-01-07 17:40:15 +00002998 break;
2999
3000 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003001 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003002 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003003 error_reportf_err(err, "Could not delete snapshot '%s': ",
3004 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003005 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003006 }
aliguorif7b4a942009-01-07 17:40:15 +00003007 break;
3008 }
3009
3010 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003011 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003012 if (ret) {
3013 return 1;
3014 }
Stuart Brady153859b2009-06-07 00:42:17 +01003015 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003016}
3017
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003018static int img_rebase(int argc, char **argv)
3019{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003020 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003021 uint8_t *buf_old = NULL;
3022 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05003023 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003024 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003025 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3026 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003027 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003028 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003029 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003030 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003031 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003032 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003033 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003034
3035 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003036 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003037 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003038 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003039 out_baseimg = NULL;
3040 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003041 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003042 static const struct option long_options[] = {
3043 {"help", no_argument, 0, 'h'},
3044 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003045 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003046 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003047 {0, 0, 0, 0}
3048 };
Fam Zheng335e9932017-05-03 00:35:39 +08003049 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003050 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003051 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003052 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003053 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003054 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003055 case ':':
3056 missing_argument(argv[optind - 1]);
3057 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003058 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003059 unrecognized_option(argv[optind - 1]);
3060 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003061 case 'h':
3062 help();
3063 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003064 case 'f':
3065 fmt = optarg;
3066 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003067 case 'F':
3068 out_basefmt = optarg;
3069 break;
3070 case 'b':
3071 out_baseimg = optarg;
3072 break;
3073 case 'u':
3074 unsafe = 1;
3075 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003076 case 'p':
3077 progress = 1;
3078 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003079 case 't':
3080 cache = optarg;
3081 break;
Max Reitz40055952014-07-22 22:58:42 +02003082 case 'T':
3083 src_cache = optarg;
3084 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003085 case 'q':
3086 quiet = true;
3087 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003088 case OPTION_OBJECT: {
3089 QemuOpts *opts;
3090 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3091 optarg, true);
3092 if (!opts) {
3093 return 1;
3094 }
3095 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003096 case OPTION_IMAGE_OPTS:
3097 image_opts = true;
3098 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003099 case 'U':
3100 force_share = true;
3101 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003102 }
3103 }
3104
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003105 if (quiet) {
3106 progress = 0;
3107 }
3108
Fam Zhengac1307a2014-04-22 13:36:11 +08003109 if (optind != argc - 1) {
3110 error_exit("Expecting one image file name");
3111 }
3112 if (!unsafe && !out_baseimg) {
3113 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003114 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003115 filename = argv[optind++];
3116
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003117 if (qemu_opts_foreach(&qemu_object_opts,
3118 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003119 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003120 return 1;
3121 }
3122
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003123 qemu_progress_init(progress, 2.0);
3124 qemu_progress_print(0, 100);
3125
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003126 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003127 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003128 if (ret < 0) {
3129 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003130 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003131 }
3132
Kevin Wolfce099542016-03-15 13:01:04 +01003133 src_flags = 0;
3134 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003135 if (ret < 0) {
3136 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003137 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003138 }
3139
Kevin Wolfce099542016-03-15 13:01:04 +01003140 /* The source files are opened read-only, don't care about WCE */
3141 assert((src_flags & BDRV_O_RDWR) == 0);
3142 (void) src_writethrough;
3143
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003144 /*
3145 * Open the images.
3146 *
3147 * Ignore the old backing file for unsafe rebase in case we want to correct
3148 * the reference to a renamed or moved backing file.
3149 */
Fam Zheng335e9932017-05-03 00:35:39 +08003150 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3151 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003152 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003153 ret = -1;
3154 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003155 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003156 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003157
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003158 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003159 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003160 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003161 ret = -1;
3162 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003163 }
3164 }
3165
3166 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003167 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003168 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003169 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003170
Max Reitz644483d2015-02-05 13:58:17 -05003171 if (bs->backing_format[0] != '\0') {
3172 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003173 qdict_put_str(options, "driver", bs->backing_format);
Max Reitz644483d2015-02-05 13:58:17 -05003174 }
3175
Fam Zheng335e9932017-05-03 00:35:39 +08003176 if (force_share) {
3177 if (!options) {
3178 options = qdict_new();
3179 }
Eric Blake579cf1d2017-05-15 14:54:39 -05003180 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +08003181 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003182 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003183 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003184 options, src_flags, &local_err);
3185 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003186 error_reportf_err(local_err,
3187 "Could not open old backing file '%s': ",
3188 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003189 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003190 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003191 }
Max Reitz644483d2015-02-05 13:58:17 -05003192
Alex Bligha6166732012-10-16 13:46:18 +01003193 if (out_baseimg[0]) {
Fam Zheng335e9932017-05-03 00:35:39 +08003194 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003195 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003196 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003197 }
3198 if (force_share) {
3199 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003200 }
3201
Max Reitzefaa7c42016-03-16 19:54:38 +01003202 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003203 options, src_flags, &local_err);
3204 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003205 error_reportf_err(local_err,
3206 "Could not open new backing file '%s': ",
3207 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003208 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003209 goto out;
3210 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003211 }
3212 }
3213
3214 /*
3215 * Check each unallocated cluster in the COW file. If it is unallocated,
3216 * accesses go to the backing file. We must therefore compare this cluster
3217 * in the old and new backing file, and if they differ we need to copy it
3218 * from the old backing file into the COW file.
3219 *
3220 * If qemu-img crashes during this step, no harm is done. The content of
3221 * the image is the same as the original one at any time.
3222 */
3223 if (!unsafe) {
Eric Blake41536282017-10-11 22:47:15 -05003224 int64_t size;
3225 int64_t old_backing_size;
3226 int64_t new_backing_size = 0;
3227 uint64_t offset;
3228 int64_t n;
Kevin Wolf1f710492012-10-12 14:29:18 +02003229 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003230
Max Reitzf1d3cd72015-02-05 13:58:18 -05003231 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3232 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003233
Eric Blake41536282017-10-11 22:47:15 -05003234 size = blk_getlength(blk);
3235 if (size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003236 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003237 filename, strerror(-size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003238 ret = -1;
3239 goto out;
3240 }
Eric Blake41536282017-10-11 22:47:15 -05003241 old_backing_size = blk_getlength(blk_old_backing);
3242 if (old_backing_size < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003243 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003244
3245 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3246 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003247 backing_name, strerror(-old_backing_size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003248 ret = -1;
3249 goto out;
3250 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003251 if (blk_new_backing) {
Eric Blake41536282017-10-11 22:47:15 -05003252 new_backing_size = blk_getlength(blk_new_backing);
3253 if (new_backing_size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003254 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003255 out_baseimg, strerror(-new_backing_size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003256 ret = -1;
3257 goto out;
3258 }
Alex Bligha6166732012-10-16 13:46:18 +01003259 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003260
Eric Blake41536282017-10-11 22:47:15 -05003261 if (size != 0) {
3262 local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE));
Kevin Wolf1f710492012-10-12 14:29:18 +02003263 }
3264
Eric Blake41536282017-10-11 22:47:15 -05003265 for (offset = 0; offset < size; offset += n) {
3266 /* How many bytes can we handle with the next read? */
3267 n = MIN(IO_BUF_SIZE, size - offset);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003268
3269 /* If the cluster is allocated, we don't need to take action */
Eric Blake41536282017-10-11 22:47:15 -05003270 ret = bdrv_is_allocated(bs, offset, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003271 if (ret < 0) {
3272 error_report("error while reading image metadata: %s",
3273 strerror(-ret));
3274 goto out;
3275 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003276 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003277 continue;
3278 }
3279
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003280 /*
3281 * Read old and new backing file and take into consideration that
3282 * backing files may be smaller than the COW image.
3283 */
Eric Blake41536282017-10-11 22:47:15 -05003284 if (offset >= old_backing_size) {
3285 memset(buf_old, 0, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003286 } else {
Eric Blake41536282017-10-11 22:47:15 -05003287 if (offset + n > old_backing_size) {
3288 n = old_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003289 }
3290
Eric Blake41536282017-10-11 22:47:15 -05003291 ret = blk_pread(blk_old_backing, offset, buf_old, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003292 if (ret < 0) {
3293 error_report("error while reading from old backing file");
3294 goto out;
3295 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003296 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003297
Eric Blake41536282017-10-11 22:47:15 -05003298 if (offset >= new_backing_size || !blk_new_backing) {
3299 memset(buf_new, 0, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003300 } else {
Eric Blake41536282017-10-11 22:47:15 -05003301 if (offset + n > new_backing_size) {
3302 n = new_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003303 }
3304
Eric Blake41536282017-10-11 22:47:15 -05003305 ret = blk_pread(blk_new_backing, offset, buf_new, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003306 if (ret < 0) {
3307 error_report("error while reading from new backing file");
3308 goto out;
3309 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003310 }
3311
3312 /* If they differ, we need to write to the COW file */
3313 uint64_t written = 0;
3314
Eric Blake41536282017-10-11 22:47:15 -05003315 while (written < n) {
Eric Blakedc61cd32017-10-11 22:47:14 -05003316 int64_t pnum;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003317
Eric Blake41536282017-10-11 22:47:15 -05003318 if (compare_buffers(buf_old + written, buf_new + written,
3319 n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003320 {
Eric Blake41536282017-10-11 22:47:15 -05003321 ret = blk_pwrite(blk, offset + written,
Eric Blakedc61cd32017-10-11 22:47:14 -05003322 buf_old + written, pnum, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003323 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003324 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003325 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003326 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003327 }
3328 }
3329
3330 written += pnum;
3331 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003332 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003333 }
3334 }
3335
3336 /*
3337 * Change the backing file. All clusters that are different from the old
3338 * backing file are overwritten in the COW file now, so the visible content
3339 * doesn't change when we switch the backing file.
3340 */
Alex Bligha6166732012-10-16 13:46:18 +01003341 if (out_baseimg && *out_baseimg) {
3342 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3343 } else {
3344 ret = bdrv_change_backing_file(bs, NULL, NULL);
3345 }
3346
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003347 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003348 error_report("Could not change the backing file to '%s': No "
3349 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003350 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003351 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003352 out_baseimg, strerror(-ret));
3353 }
3354
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003355 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003356 /*
3357 * TODO At this point it is possible to check if any clusters that are
3358 * allocated in the COW file are the same in the backing file. If so, they
3359 * could be dropped from the COW file. Don't do this before switching the
3360 * backing file, in case of a crash this would lead to corruption.
3361 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003362out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003363 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003364 /* Cleanup */
3365 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003366 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003367 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003368 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003369 qemu_vfree(buf_old);
3370 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003371
Markus Armbruster26f54e92014-10-07 13:59:04 +02003372 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003373 if (ret) {
3374 return 1;
3375 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003376 return 0;
3377}
3378
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003379static int img_resize(int argc, char **argv)
3380{
Markus Armbruster6750e792015-02-12 17:43:08 +01003381 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003382 int c, ret, relative;
3383 const char *filename, *fmt, *size;
Max Reitz5279b302018-04-21 18:39:57 +02003384 int64_t n, total_size, current_size, new_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003385 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003386 BlockBackend *blk = NULL;
Max Reitzdc5f6902017-06-13 22:20:55 +02003387 PreallocMode prealloc = PREALLOC_MODE_OFF;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003388 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003389
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003390 static QemuOptsList resize_options = {
3391 .name = "resize_options",
3392 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3393 .desc = {
3394 {
3395 .name = BLOCK_OPT_SIZE,
3396 .type = QEMU_OPT_SIZE,
3397 .help = "Virtual disk size"
3398 }, {
3399 /* end of list */
3400 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003401 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003402 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003403 bool image_opts = false;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003404 bool shrink = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003405
Kevin Wolfe80fec72011-04-29 10:58:12 +02003406 /* Remove size from argv manually so that negative numbers are not treated
3407 * as options by getopt. */
3408 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003409 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003410 return 1;
3411 }
3412
3413 size = argv[--argc];
3414
3415 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003416 fmt = NULL;
3417 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003418 static const struct option long_options[] = {
3419 {"help", no_argument, 0, 'h'},
3420 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003421 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Max Reitzdc5f6902017-06-13 22:20:55 +02003422 {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003423 {"shrink", no_argument, 0, OPTION_SHRINK},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003424 {0, 0, 0, 0}
3425 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003426 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003427 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003428 if (c == -1) {
3429 break;
3430 }
3431 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003432 case ':':
3433 missing_argument(argv[optind - 1]);
3434 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003435 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003436 unrecognized_option(argv[optind - 1]);
3437 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003438 case 'h':
3439 help();
3440 break;
3441 case 'f':
3442 fmt = optarg;
3443 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003444 case 'q':
3445 quiet = true;
3446 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003447 case OPTION_OBJECT: {
3448 QemuOpts *opts;
3449 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3450 optarg, true);
3451 if (!opts) {
3452 return 1;
3453 }
3454 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003455 case OPTION_IMAGE_OPTS:
3456 image_opts = true;
3457 break;
Max Reitzdc5f6902017-06-13 22:20:55 +02003458 case OPTION_PREALLOCATION:
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02003459 prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
Markus Armbruster06c60b62017-08-24 10:45:57 +02003460 PREALLOC_MODE__MAX, NULL);
Max Reitzdc5f6902017-06-13 22:20:55 +02003461 if (prealloc == PREALLOC_MODE__MAX) {
3462 error_report("Invalid preallocation mode '%s'", optarg);
3463 return 1;
3464 }
3465 break;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003466 case OPTION_SHRINK:
3467 shrink = true;
3468 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003469 }
3470 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003471 if (optind != argc - 1) {
Max Reitzbe8fbd42018-02-05 17:27:45 +01003472 error_exit("Expecting image file name and size");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003473 }
3474 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003475
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003476 if (qemu_opts_foreach(&qemu_object_opts,
3477 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003478 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003479 return 1;
3480 }
3481
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003482 /* Choose grow, shrink, or absolute resize mode */
3483 switch (size[0]) {
3484 case '+':
3485 relative = 1;
3486 size++;
3487 break;
3488 case '-':
3489 relative = -1;
3490 size++;
3491 break;
3492 default:
3493 relative = 0;
3494 break;
3495 }
3496
3497 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003498 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003499 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003500 if (err) {
3501 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003502 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003503 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003504 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003505 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003506 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3507 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003508
Max Reitzefaa7c42016-03-16 19:54:38 +01003509 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003510 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3511 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003512 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003513 ret = -1;
3514 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003515 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003516
Max Reitzdc5f6902017-06-13 22:20:55 +02003517 current_size = blk_getlength(blk);
3518 if (current_size < 0) {
3519 error_report("Failed to inquire current image length: %s",
3520 strerror(-current_size));
3521 ret = -1;
3522 goto out;
3523 }
3524
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003525 if (relative) {
Max Reitzdc5f6902017-06-13 22:20:55 +02003526 total_size = current_size + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003527 } else {
3528 total_size = n;
3529 }
3530 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003531 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003532 ret = -1;
3533 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003534 }
3535
Max Reitzdc5f6902017-06-13 22:20:55 +02003536 if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
3537 error_report("Preallocation can only be used for growing images");
3538 ret = -1;
3539 goto out;
3540 }
3541
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003542 if (total_size < current_size && !shrink) {
3543 warn_report("Shrinking an image will delete all data beyond the "
3544 "shrunken image's end. Before performing such an "
3545 "operation, make sure there is no important data there.");
3546
3547 if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) {
3548 error_report(
3549 "Use the --shrink option to perform a shrink operation.");
3550 ret = -1;
3551 goto out;
3552 } else {
3553 warn_report("Using the --shrink option will suppress this message. "
3554 "Note that future versions of qemu-img may refuse to "
3555 "shrink images without this option.");
3556 }
3557 }
3558
Max Reitzdc5f6902017-06-13 22:20:55 +02003559 ret = blk_truncate(blk, total_size, prealloc, &err);
Max Reitz5279b302018-04-21 18:39:57 +02003560 if (ret < 0) {
Max Reitzed3d2ec2017-03-28 22:51:27 +02003561 error_report_err(err);
Max Reitz5279b302018-04-21 18:39:57 +02003562 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003563 }
Max Reitz5279b302018-04-21 18:39:57 +02003564
3565 new_size = blk_getlength(blk);
3566 if (new_size < 0) {
3567 error_report("Failed to verify truncated image length: %s",
3568 strerror(-new_size));
3569 ret = -1;
3570 goto out;
3571 }
3572
3573 /* Some block drivers implement a truncation method, but only so
3574 * the user can cause qemu to refresh the image's size from disk.
3575 * The idea is that the user resizes the image outside of qemu and
3576 * then invokes block_resize to inform qemu about it.
3577 * (This includes iscsi and file-posix for device files.)
3578 * Of course, that is not the behavior someone invoking
3579 * qemu-img resize would find useful, so we catch that behavior
3580 * here and tell the user. */
3581 if (new_size != total_size && new_size == current_size) {
3582 error_report("Image was not resized; resizing may not be supported "
3583 "for this image");
3584 ret = -1;
3585 goto out;
3586 }
3587
3588 if (new_size != total_size) {
3589 warn_report("Image should have been resized to %" PRIi64
3590 " bytes, but was resized to %" PRIi64 " bytes",
3591 total_size, new_size);
3592 }
3593
3594 qprintf(quiet, "Image resized.\n");
3595
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003596out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003597 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003598 if (ret) {
3599 return 1;
3600 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003601 return 0;
3602}
3603
Max Reitz76a3a342014-10-27 11:12:51 +01003604static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003605 int64_t offset, int64_t total_work_size,
3606 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003607{
3608 qemu_progress_print(100.f * offset / total_work_size, 0);
3609}
3610
Max Reitz6f176b42013-09-03 10:09:50 +02003611static int img_amend(int argc, char **argv)
3612{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003613 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003614 int c, ret = 0;
3615 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003616 QemuOptsList *create_opts = NULL;
3617 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003618 const char *fmt = NULL, *filename, *cache;
3619 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003620 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003621 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003622 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003623 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003624 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003625
Max Reitzbd39e6e2014-07-22 22:58:43 +02003626 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003627 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003628 static const struct option long_options[] = {
3629 {"help", no_argument, 0, 'h'},
3630 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003631 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003632 {0, 0, 0, 0}
3633 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003634 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003635 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003636 if (c == -1) {
3637 break;
3638 }
3639
3640 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003641 case ':':
3642 missing_argument(argv[optind - 1]);
3643 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003644 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003645 unrecognized_option(argv[optind - 1]);
3646 break;
3647 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003648 help();
3649 break;
3650 case 'o':
3651 if (!is_valid_option_list(optarg)) {
3652 error_report("Invalid option list: %s", optarg);
3653 ret = -1;
3654 goto out_no_progress;
3655 }
3656 if (!options) {
3657 options = g_strdup(optarg);
3658 } else {
3659 char *old_options = options;
3660 options = g_strdup_printf("%s,%s", options, optarg);
3661 g_free(old_options);
3662 }
3663 break;
3664 case 'f':
3665 fmt = optarg;
3666 break;
3667 case 't':
3668 cache = optarg;
3669 break;
3670 case 'p':
3671 progress = true;
3672 break;
3673 case 'q':
3674 quiet = true;
3675 break;
3676 case OPTION_OBJECT:
3677 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3678 optarg, true);
3679 if (!opts) {
3680 ret = -1;
3681 goto out_no_progress;
3682 }
3683 break;
3684 case OPTION_IMAGE_OPTS:
3685 image_opts = true;
3686 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003687 }
3688 }
3689
Max Reitz6f176b42013-09-03 10:09:50 +02003690 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003691 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003692 }
3693
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003694 if (qemu_opts_foreach(&qemu_object_opts,
3695 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003696 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003697 ret = -1;
3698 goto out_no_progress;
3699 }
3700
Max Reitz76a3a342014-10-27 11:12:51 +01003701 if (quiet) {
3702 progress = false;
3703 }
3704 qemu_progress_init(progress, 1.0);
3705
Kevin Wolfa283cb62014-02-21 16:24:07 +01003706 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3707 if (fmt && has_help_option(options)) {
3708 /* If a format is explicitly specified (and possibly no filename is
3709 * given), print option help here */
3710 ret = print_block_option_help(filename, fmt);
3711 goto out;
3712 }
3713
3714 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003715 error_report("Expecting one image file name");
3716 ret = -1;
3717 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003718 }
Max Reitz6f176b42013-09-03 10:09:50 +02003719
Kevin Wolfce099542016-03-15 13:01:04 +01003720 flags = BDRV_O_RDWR;
3721 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003722 if (ret < 0) {
3723 error_report("Invalid cache option: %s", cache);
3724 goto out;
3725 }
3726
Fam Zheng335e9932017-05-03 00:35:39 +08003727 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3728 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003729 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003730 ret = -1;
3731 goto out;
3732 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003733 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003734
3735 fmt = bs->drv->format_name;
3736
Kevin Wolf626f84f2014-02-21 16:24:06 +01003737 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003738 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003739 ret = print_block_option_help(filename, fmt);
3740 goto out;
3741 }
3742
Max Reitzb2439d22014-12-02 18:32:47 +01003743 if (!bs->drv->create_opts) {
3744 error_report("Format driver '%s' does not support any options to amend",
3745 fmt);
3746 ret = -1;
3747 goto out;
3748 }
3749
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003750 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003751 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003752 qemu_opts_do_parse(opts, options, NULL, &err);
3753 if (err) {
3754 error_report_err(err);
3755 ret = -1;
3756 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003757 }
3758
Max Reitz76a3a342014-10-27 11:12:51 +01003759 /* In case the driver does not call amend_status_cb() */
3760 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003761 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003762 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003763 if (ret < 0) {
3764 error_report("Error while amending options: %s", strerror(-ret));
3765 goto out;
3766 }
3767
3768out:
Max Reitz76a3a342014-10-27 11:12:51 +01003769 qemu_progress_end();
3770
Max Reitze814dff2015-08-20 16:00:38 -07003771out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003772 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003773 qemu_opts_del(opts);
3774 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003775 g_free(options);
3776
Max Reitz6f176b42013-09-03 10:09:50 +02003777 if (ret) {
3778 return 1;
3779 }
3780 return 0;
3781}
3782
Kevin Wolfb6133b82014-08-05 14:17:13 +02003783typedef struct BenchData {
3784 BlockBackend *blk;
3785 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003786 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003787 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003788 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003789 int nrreq;
3790 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003791 int flush_interval;
3792 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003793 uint8_t *buf;
3794 QEMUIOVector *qiov;
3795
3796 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003797 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003798 uint64_t offset;
3799} BenchData;
3800
Kevin Wolf55d539c2016-06-03 13:59:41 +02003801static void bench_undrained_flush_cb(void *opaque, int ret)
3802{
3803 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003804 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003805 exit(EXIT_FAILURE);
3806 }
3807}
3808
Kevin Wolfb6133b82014-08-05 14:17:13 +02003809static void bench_cb(void *opaque, int ret)
3810{
3811 BenchData *b = opaque;
3812 BlockAIOCB *acb;
3813
3814 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003815 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003816 exit(EXIT_FAILURE);
3817 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003818
3819 if (b->in_flush) {
3820 /* Just finished a flush with drained queue: Start next requests */
3821 assert(b->in_flight == 0);
3822 b->in_flush = false;
3823 } else if (b->in_flight > 0) {
3824 int remaining = b->n - b->in_flight;
3825
Kevin Wolfb6133b82014-08-05 14:17:13 +02003826 b->n--;
3827 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003828
3829 /* Time for flush? Drain queue if requested, then flush */
3830 if (b->flush_interval && remaining % b->flush_interval == 0) {
3831 if (!b->in_flight || !b->drain_on_flush) {
3832 BlockCompletionFunc *cb;
3833
3834 if (b->drain_on_flush) {
3835 b->in_flush = true;
3836 cb = bench_cb;
3837 } else {
3838 cb = bench_undrained_flush_cb;
3839 }
3840
3841 acb = blk_aio_flush(b->blk, cb, b);
3842 if (!acb) {
3843 error_report("Failed to issue flush request");
3844 exit(EXIT_FAILURE);
3845 }
3846 }
3847 if (b->drain_on_flush) {
3848 return;
3849 }
3850 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003851 }
3852
3853 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003854 int64_t offset = b->offset;
3855 /* blk_aio_* might look for completed I/Os and kick bench_cb
3856 * again, so make sure this operation is counted by in_flight
3857 * and b->offset is ready for the next submission.
3858 */
3859 b->in_flight++;
3860 b->offset += b->step;
3861 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003862 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003863 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003864 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003865 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003866 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003867 if (!acb) {
3868 error_report("Failed to issue request");
3869 exit(EXIT_FAILURE);
3870 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003871 }
3872}
3873
3874static int img_bench(int argc, char **argv)
3875{
3876 int c, ret = 0;
3877 const char *fmt = NULL, *filename;
3878 bool quiet = false;
3879 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003880 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003881 int count = 75000;
3882 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003883 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003884 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003885 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003886 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003887 int flush_interval = 0;
3888 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003889 int64_t image_size;
3890 BlockBackend *blk = NULL;
3891 BenchData data = {};
3892 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003893 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003894 struct timeval t1, t2;
3895 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08003896 bool force_share = false;
Fam Zheng79d46582018-01-16 14:08:58 +08003897 size_t buf_size;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003898
3899 for (;;) {
3900 static const struct option long_options[] = {
3901 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003902 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003903 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003904 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003905 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08003906 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003907 {0, 0, 0, 0}
3908 };
Fam Zheng335e9932017-05-03 00:35:39 +08003909 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003910 if (c == -1) {
3911 break;
3912 }
3913
3914 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003915 case ':':
3916 missing_argument(argv[optind - 1]);
3917 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003918 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003919 unrecognized_option(argv[optind - 1]);
3920 break;
3921 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02003922 help();
3923 break;
3924 case 'c':
3925 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003926 unsigned long res;
3927
3928 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003929 error_report("Invalid request count specified");
3930 return 1;
3931 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003932 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003933 break;
3934 }
3935 case 'd':
3936 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003937 unsigned long res;
3938
3939 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003940 error_report("Invalid queue depth specified");
3941 return 1;
3942 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003943 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003944 break;
3945 }
3946 case 'f':
3947 fmt = optarg;
3948 break;
3949 case 'n':
3950 flags |= BDRV_O_NATIVE_AIO;
3951 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003952 case 'o':
3953 {
Markus Armbruster606caa02017-02-21 21:14:04 +01003954 offset = cvtnum(optarg);
3955 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02003956 error_report("Invalid offset specified");
3957 return 1;
3958 }
3959 break;
3960 }
3961 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003962 case 'q':
3963 quiet = true;
3964 break;
3965 case 's':
3966 {
3967 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003968
Markus Armbruster606caa02017-02-21 21:14:04 +01003969 sval = cvtnum(optarg);
3970 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003971 error_report("Invalid buffer size specified");
3972 return 1;
3973 }
3974
3975 bufsize = sval;
3976 break;
3977 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003978 case 'S':
3979 {
3980 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003981
Markus Armbruster606caa02017-02-21 21:14:04 +01003982 sval = cvtnum(optarg);
3983 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02003984 error_report("Invalid step size specified");
3985 return 1;
3986 }
3987
3988 step = sval;
3989 break;
3990 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003991 case 't':
3992 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3993 if (ret < 0) {
3994 error_report("Invalid cache mode");
3995 ret = -1;
3996 goto out;
3997 }
3998 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003999 case 'w':
4000 flags |= BDRV_O_RDWR;
4001 is_write = true;
4002 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004003 case 'U':
4004 force_share = true;
4005 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004006 case OPTION_PATTERN:
4007 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004008 unsigned long res;
4009
4010 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004011 error_report("Invalid pattern byte specified");
4012 return 1;
4013 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004014 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004015 break;
4016 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02004017 case OPTION_FLUSH_INTERVAL:
4018 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004019 unsigned long res;
4020
4021 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004022 error_report("Invalid flush interval specified");
4023 return 1;
4024 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004025 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004026 break;
4027 }
4028 case OPTION_NO_DRAIN:
4029 drain_on_flush = false;
4030 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004031 case OPTION_IMAGE_OPTS:
4032 image_opts = true;
4033 break;
4034 }
4035 }
4036
4037 if (optind != argc - 1) {
4038 error_exit("Expecting one image file name");
4039 }
4040 filename = argv[argc - 1];
4041
Kevin Wolf55d539c2016-06-03 13:59:41 +02004042 if (!is_write && flush_interval) {
4043 error_report("--flush-interval is only available in write tests");
4044 ret = -1;
4045 goto out;
4046 }
4047 if (flush_interval && flush_interval < depth) {
4048 error_report("Flush interval can't be smaller than depth");
4049 ret = -1;
4050 goto out;
4051 }
4052
Fam Zheng335e9932017-05-03 00:35:39 +08004053 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4054 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004055 if (!blk) {
4056 ret = -1;
4057 goto out;
4058 }
4059
4060 image_size = blk_getlength(blk);
4061 if (image_size < 0) {
4062 ret = image_size;
4063 goto out;
4064 }
4065
4066 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004067 .blk = blk,
4068 .image_size = image_size,
4069 .bufsize = bufsize,
4070 .step = step ?: bufsize,
4071 .nrreq = depth,
4072 .n = count,
4073 .offset = offset,
4074 .write = is_write,
4075 .flush_interval = flush_interval,
4076 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004077 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004078 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004079 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004080 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004081 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004082 if (flush_interval) {
4083 printf("Sending flush every %d requests\n", flush_interval);
4084 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004085
Fam Zheng79d46582018-01-16 14:08:58 +08004086 buf_size = data.nrreq * data.bufsize;
4087 data.buf = blk_blockalign(blk, buf_size);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004088 memset(data.buf, pattern, data.nrreq * data.bufsize);
4089
Fam Zheng79d46582018-01-16 14:08:58 +08004090 blk_register_buf(blk, data.buf, buf_size);
4091
Kevin Wolfb6133b82014-08-05 14:17:13 +02004092 data.qiov = g_new(QEMUIOVector, data.nrreq);
4093 for (i = 0; i < data.nrreq; i++) {
4094 qemu_iovec_init(&data.qiov[i], 1);
4095 qemu_iovec_add(&data.qiov[i],
4096 data.buf + i * data.bufsize, data.bufsize);
4097 }
4098
4099 gettimeofday(&t1, NULL);
4100 bench_cb(&data, 0);
4101
4102 while (data.n > 0) {
4103 main_loop_wait(false);
4104 }
4105 gettimeofday(&t2, NULL);
4106
4107 printf("Run completed in %3.3f seconds.\n",
4108 (t2.tv_sec - t1.tv_sec)
4109 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4110
4111out:
Fam Zheng79d46582018-01-16 14:08:58 +08004112 if (data.buf) {
4113 blk_unregister_buf(blk, data.buf);
4114 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004115 qemu_vfree(data.buf);
4116 blk_unref(blk);
4117
4118 if (ret) {
4119 return 1;
4120 }
4121 return 0;
4122}
4123
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004124#define C_BS 01
4125#define C_COUNT 02
4126#define C_IF 04
4127#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004128#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004129
4130struct DdInfo {
4131 unsigned int flags;
4132 int64_t count;
4133};
4134
4135struct DdIo {
4136 int bsz; /* Block size */
4137 char *filename;
4138 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004139 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004140};
4141
4142struct DdOpts {
4143 const char *name;
4144 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4145 unsigned int flag;
4146};
4147
4148static int img_dd_bs(const char *arg,
4149 struct DdIo *in, struct DdIo *out,
4150 struct DdInfo *dd)
4151{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004152 int64_t res;
4153
Markus Armbruster606caa02017-02-21 21:14:04 +01004154 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004155
Markus Armbruster606caa02017-02-21 21:14:04 +01004156 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004157 error_report("invalid number: '%s'", arg);
4158 return 1;
4159 }
4160 in->bsz = out->bsz = res;
4161
4162 return 0;
4163}
4164
4165static int img_dd_count(const char *arg,
4166 struct DdIo *in, struct DdIo *out,
4167 struct DdInfo *dd)
4168{
Markus Armbruster606caa02017-02-21 21:14:04 +01004169 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004170
Markus Armbruster606caa02017-02-21 21:14:04 +01004171 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004172 error_report("invalid number: '%s'", arg);
4173 return 1;
4174 }
4175
4176 return 0;
4177}
4178
4179static int img_dd_if(const char *arg,
4180 struct DdIo *in, struct DdIo *out,
4181 struct DdInfo *dd)
4182{
4183 in->filename = g_strdup(arg);
4184
4185 return 0;
4186}
4187
4188static int img_dd_of(const char *arg,
4189 struct DdIo *in, struct DdIo *out,
4190 struct DdInfo *dd)
4191{
4192 out->filename = g_strdup(arg);
4193
4194 return 0;
4195}
4196
Reda Sallahif7c15532016-08-10 16:16:09 +02004197static int img_dd_skip(const char *arg,
4198 struct DdIo *in, struct DdIo *out,
4199 struct DdInfo *dd)
4200{
Markus Armbruster606caa02017-02-21 21:14:04 +01004201 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004202
Markus Armbruster606caa02017-02-21 21:14:04 +01004203 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004204 error_report("invalid number: '%s'", arg);
4205 return 1;
4206 }
4207
4208 return 0;
4209}
4210
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004211static int img_dd(int argc, char **argv)
4212{
4213 int ret = 0;
4214 char *arg = NULL;
4215 char *tmp;
4216 BlockDriver *drv = NULL, *proto_drv = NULL;
4217 BlockBackend *blk1 = NULL, *blk2 = NULL;
4218 QemuOpts *opts = NULL;
4219 QemuOptsList *create_opts = NULL;
4220 Error *local_err = NULL;
4221 bool image_opts = false;
4222 int c, i;
4223 const char *out_fmt = "raw";
4224 const char *fmt = NULL;
4225 int64_t size = 0;
4226 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004227 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004228 struct DdInfo dd = {
4229 .flags = 0,
4230 .count = 0,
4231 };
4232 struct DdIo in = {
4233 .bsz = 512, /* Block size is by default 512 bytes */
4234 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004235 .buf = NULL,
4236 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004237 };
4238 struct DdIo out = {
4239 .bsz = 512,
4240 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004241 .buf = NULL,
4242 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004243 };
4244
4245 const struct DdOpts options[] = {
4246 { "bs", img_dd_bs, C_BS },
4247 { "count", img_dd_count, C_COUNT },
4248 { "if", img_dd_if, C_IF },
4249 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004250 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004251 { NULL, NULL, 0 }
4252 };
4253 const struct option long_options[] = {
4254 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004255 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004256 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004257 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004258 { 0, 0, 0, 0 }
4259 };
4260
Fam Zheng335e9932017-05-03 00:35:39 +08004261 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004262 if (c == EOF) {
4263 break;
4264 }
4265 switch (c) {
4266 case 'O':
4267 out_fmt = optarg;
4268 break;
4269 case 'f':
4270 fmt = optarg;
4271 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004272 case ':':
4273 missing_argument(argv[optind - 1]);
4274 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004275 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004276 unrecognized_option(argv[optind - 1]);
4277 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004278 case 'h':
4279 help();
4280 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004281 case 'U':
4282 force_share = true;
4283 break;
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004284 case OPTION_OBJECT:
4285 if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004286 ret = -1;
4287 goto out;
4288 }
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004289 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004290 case OPTION_IMAGE_OPTS:
4291 image_opts = true;
4292 break;
4293 }
4294 }
4295
4296 for (i = optind; i < argc; i++) {
4297 int j;
4298 arg = g_strdup(argv[i]);
4299
4300 tmp = strchr(arg, '=');
4301 if (tmp == NULL) {
4302 error_report("unrecognized operand %s", arg);
4303 ret = -1;
4304 goto out;
4305 }
4306
4307 *tmp++ = '\0';
4308
4309 for (j = 0; options[j].name != NULL; j++) {
4310 if (!strcmp(arg, options[j].name)) {
4311 break;
4312 }
4313 }
4314 if (options[j].name == NULL) {
4315 error_report("unrecognized operand %s", arg);
4316 ret = -1;
4317 goto out;
4318 }
4319
4320 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4321 ret = -1;
4322 goto out;
4323 }
4324 dd.flags |= options[j].flag;
4325 g_free(arg);
4326 arg = NULL;
4327 }
4328
4329 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4330 error_report("Must specify both input and output files");
4331 ret = -1;
4332 goto out;
4333 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004334
4335 if (qemu_opts_foreach(&qemu_object_opts,
4336 user_creatable_add_opts_foreach,
4337 NULL, NULL)) {
4338 ret = -1;
4339 goto out;
4340 }
4341
Fam Zheng335e9932017-05-03 00:35:39 +08004342 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4343 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004344
4345 if (!blk1) {
4346 ret = -1;
4347 goto out;
4348 }
4349
4350 drv = bdrv_find_format(out_fmt);
4351 if (!drv) {
4352 error_report("Unknown file format");
4353 ret = -1;
4354 goto out;
4355 }
4356 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4357
4358 if (!proto_drv) {
4359 error_report_err(local_err);
4360 ret = -1;
4361 goto out;
4362 }
4363 if (!drv->create_opts) {
4364 error_report("Format driver '%s' does not support image creation",
4365 drv->format_name);
4366 ret = -1;
4367 goto out;
4368 }
4369 if (!proto_drv->create_opts) {
4370 error_report("Protocol driver '%s' does not support image creation",
4371 proto_drv->format_name);
4372 ret = -1;
4373 goto out;
4374 }
4375 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4376 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4377
4378 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4379
4380 size = blk_getlength(blk1);
4381 if (size < 0) {
4382 error_report("Failed to get size for '%s'", in.filename);
4383 ret = -1;
4384 goto out;
4385 }
4386
4387 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4388 dd.count * in.bsz < size) {
4389 size = dd.count * in.bsz;
4390 }
4391
Reda Sallahif7c15532016-08-10 16:16:09 +02004392 /* Overflow means the specified offset is beyond input image's size */
4393 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4394 size < in.bsz * in.offset)) {
4395 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4396 } else {
4397 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4398 size - in.bsz * in.offset, &error_abort);
4399 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004400
4401 ret = bdrv_create(drv, out.filename, opts, &local_err);
4402 if (ret < 0) {
4403 error_reportf_err(local_err,
4404 "%s: error while creating output image: ",
4405 out.filename);
4406 ret = -1;
4407 goto out;
4408 }
4409
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004410 /* TODO, we can't honour --image-opts for the target,
4411 * since it needs to be given in a format compatible
4412 * with the bdrv_create() call above which does not
4413 * support image-opts style.
4414 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01004415 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004416 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004417
4418 if (!blk2) {
4419 ret = -1;
4420 goto out;
4421 }
4422
Reda Sallahif7c15532016-08-10 16:16:09 +02004423 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4424 size < in.offset * in.bsz)) {
4425 /* We give a warning if the skip option is bigger than the input
4426 * size and create an empty output disk image (i.e. like dd(1)).
4427 */
4428 error_report("%s: cannot skip to specified offset", in.filename);
4429 in_pos = size;
4430 } else {
4431 in_pos = in.offset * in.bsz;
4432 }
4433
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004434 in.buf = g_new(uint8_t, in.bsz);
4435
Reda Sallahif7c15532016-08-10 16:16:09 +02004436 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004437 int in_ret, out_ret;
4438
4439 if (in_pos + in.bsz > size) {
4440 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4441 } else {
4442 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4443 }
4444 if (in_ret < 0) {
4445 error_report("error while reading from input image file: %s",
4446 strerror(-in_ret));
4447 ret = -1;
4448 goto out;
4449 }
4450 in_pos += in_ret;
4451
4452 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4453
4454 if (out_ret < 0) {
4455 error_report("error while writing to output image file: %s",
4456 strerror(-out_ret));
4457 ret = -1;
4458 goto out;
4459 }
4460 out_pos += out_ret;
4461 }
4462
4463out:
4464 g_free(arg);
4465 qemu_opts_del(opts);
4466 qemu_opts_free(create_opts);
4467 blk_unref(blk1);
4468 blk_unref(blk2);
4469 g_free(in.filename);
4470 g_free(out.filename);
4471 g_free(in.buf);
4472 g_free(out.buf);
4473
4474 if (ret) {
4475 return 1;
4476 }
4477 return 0;
4478}
4479
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004480static void dump_json_block_measure_info(BlockMeasureInfo *info)
4481{
4482 QString *str;
4483 QObject *obj;
4484 Visitor *v = qobject_output_visitor_new(&obj);
4485
4486 visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
4487 visit_complete(v, &obj);
4488 str = qobject_to_json_pretty(obj);
4489 assert(str != NULL);
4490 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004491 qobject_unref(obj);
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004492 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004493 qobject_unref(str);
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004494}
4495
4496static int img_measure(int argc, char **argv)
4497{
4498 static const struct option long_options[] = {
4499 {"help", no_argument, 0, 'h'},
4500 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4501 {"object", required_argument, 0, OPTION_OBJECT},
4502 {"output", required_argument, 0, OPTION_OUTPUT},
4503 {"size", required_argument, 0, OPTION_SIZE},
4504 {"force-share", no_argument, 0, 'U'},
4505 {0, 0, 0, 0}
4506 };
4507 OutputFormat output_format = OFORMAT_HUMAN;
4508 BlockBackend *in_blk = NULL;
4509 BlockDriver *drv;
4510 const char *filename = NULL;
4511 const char *fmt = NULL;
4512 const char *out_fmt = "raw";
4513 char *options = NULL;
4514 char *snapshot_name = NULL;
4515 bool force_share = false;
4516 QemuOpts *opts = NULL;
4517 QemuOpts *object_opts = NULL;
4518 QemuOpts *sn_opts = NULL;
4519 QemuOptsList *create_opts = NULL;
4520 bool image_opts = false;
4521 uint64_t img_size = UINT64_MAX;
4522 BlockMeasureInfo *info = NULL;
4523 Error *local_err = NULL;
4524 int ret = 1;
4525 int c;
4526
4527 while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
4528 long_options, NULL)) != -1) {
4529 switch (c) {
4530 case '?':
4531 case 'h':
4532 help();
4533 break;
4534 case 'f':
4535 fmt = optarg;
4536 break;
4537 case 'O':
4538 out_fmt = optarg;
4539 break;
4540 case 'o':
4541 if (!is_valid_option_list(optarg)) {
4542 error_report("Invalid option list: %s", optarg);
4543 goto out;
4544 }
4545 if (!options) {
4546 options = g_strdup(optarg);
4547 } else {
4548 char *old_options = options;
4549 options = g_strdup_printf("%s,%s", options, optarg);
4550 g_free(old_options);
4551 }
4552 break;
4553 case 'l':
4554 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
4555 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
4556 optarg, false);
4557 if (!sn_opts) {
4558 error_report("Failed in parsing snapshot param '%s'",
4559 optarg);
4560 goto out;
4561 }
4562 } else {
4563 snapshot_name = optarg;
4564 }
4565 break;
4566 case 'U':
4567 force_share = true;
4568 break;
4569 case OPTION_OBJECT:
4570 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
4571 optarg, true);
4572 if (!object_opts) {
4573 goto out;
4574 }
4575 break;
4576 case OPTION_IMAGE_OPTS:
4577 image_opts = true;
4578 break;
4579 case OPTION_OUTPUT:
4580 if (!strcmp(optarg, "json")) {
4581 output_format = OFORMAT_JSON;
4582 } else if (!strcmp(optarg, "human")) {
4583 output_format = OFORMAT_HUMAN;
4584 } else {
4585 error_report("--output must be used with human or json "
4586 "as argument.");
4587 goto out;
4588 }
4589 break;
4590 case OPTION_SIZE:
4591 {
4592 int64_t sval;
4593
4594 sval = cvtnum(optarg);
4595 if (sval < 0) {
4596 if (sval == -ERANGE) {
4597 error_report("Image size must be less than 8 EiB!");
4598 } else {
4599 error_report("Invalid image size specified! You may use "
4600 "k, M, G, T, P or E suffixes for ");
4601 error_report("kilobytes, megabytes, gigabytes, terabytes, "
4602 "petabytes and exabytes.");
4603 }
4604 goto out;
4605 }
4606 img_size = (uint64_t)sval;
4607 }
4608 break;
4609 }
4610 }
4611
4612 if (qemu_opts_foreach(&qemu_object_opts,
4613 user_creatable_add_opts_foreach,
4614 NULL, NULL)) {
4615 goto out;
4616 }
4617
4618 if (argc - optind > 1) {
4619 error_report("At most one filename argument is allowed.");
4620 goto out;
4621 } else if (argc - optind == 1) {
4622 filename = argv[optind];
4623 }
4624
4625 if (!filename &&
4626 (object_opts || image_opts || fmt || snapshot_name || sn_opts)) {
4627 error_report("--object, --image-opts, -f, and -l "
4628 "require a filename argument.");
4629 goto out;
4630 }
4631 if (filename && img_size != UINT64_MAX) {
4632 error_report("--size N cannot be used together with a filename.");
4633 goto out;
4634 }
4635 if (!filename && img_size == UINT64_MAX) {
4636 error_report("Either --size N or one filename must be specified.");
4637 goto out;
4638 }
4639
4640 if (filename) {
4641 in_blk = img_open(image_opts, filename, fmt, 0,
4642 false, false, force_share);
4643 if (!in_blk) {
4644 goto out;
4645 }
4646
4647 if (sn_opts) {
4648 bdrv_snapshot_load_tmp(blk_bs(in_blk),
4649 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
4650 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
4651 &local_err);
4652 } else if (snapshot_name != NULL) {
4653 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
4654 snapshot_name, &local_err);
4655 }
4656 if (local_err) {
4657 error_reportf_err(local_err, "Failed to load snapshot: ");
4658 goto out;
4659 }
4660 }
4661
4662 drv = bdrv_find_format(out_fmt);
4663 if (!drv) {
4664 error_report("Unknown file format '%s'", out_fmt);
4665 goto out;
4666 }
4667 if (!drv->create_opts) {
4668 error_report("Format driver '%s' does not support image creation",
4669 drv->format_name);
4670 goto out;
4671 }
4672
4673 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4674 create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
4675 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4676 if (options) {
4677 qemu_opts_do_parse(opts, options, NULL, &local_err);
4678 if (local_err) {
4679 error_report_err(local_err);
4680 error_report("Invalid options for file format '%s'", out_fmt);
4681 goto out;
4682 }
4683 }
4684 if (img_size != UINT64_MAX) {
4685 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
4686 }
4687
4688 info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
4689 if (local_err) {
4690 error_report_err(local_err);
4691 goto out;
4692 }
4693
4694 if (output_format == OFORMAT_HUMAN) {
4695 printf("required size: %" PRIu64 "\n", info->required);
4696 printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
4697 } else {
4698 dump_json_block_measure_info(info);
4699 }
4700
4701 ret = 0;
4702
4703out:
4704 qapi_free_BlockMeasureInfo(info);
4705 qemu_opts_del(object_opts);
4706 qemu_opts_del(opts);
4707 qemu_opts_del(sn_opts);
4708 qemu_opts_free(create_opts);
4709 g_free(options);
4710 blk_unref(in_blk);
4711 return ret;
4712}
Kevin Wolfb6133b82014-08-05 14:17:13 +02004713
Anthony Liguoric227f092009-10-01 16:12:16 -05004714static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004715#define DEF(option, callback, arg_string) \
4716 { option, callback },
4717#include "qemu-img-cmds.h"
4718#undef DEF
4719#undef GEN_DOCS
4720 { NULL, NULL, },
4721};
4722
bellardea2384d2004-08-01 21:59:26 +00004723int main(int argc, char **argv)
4724{
Anthony Liguoric227f092009-10-01 16:12:16 -05004725 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004726 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004727 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004728 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004729 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004730 static const struct option long_options[] = {
4731 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004732 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004733 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004734 {0, 0, 0, 0}
4735 };
bellardea2384d2004-08-01 21:59:26 +00004736
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004737#ifdef CONFIG_POSIX
4738 signal(SIGPIPE, SIG_IGN);
4739#endif
4740
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004741 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004742 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004743 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004744
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004745 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004746 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004747 exit(EXIT_FAILURE);
4748 }
4749
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004750 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004751
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004752 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004753 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004754 if (argc < 2) {
4755 error_exit("Not enough arguments");
4756 }
Stuart Brady153859b2009-06-07 00:42:17 +01004757
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004758 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004759 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004760 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004761
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004762 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004763 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004764 case ':':
4765 missing_argument(argv[optind - 1]);
4766 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08004767 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004768 unrecognized_option(argv[optind - 1]);
4769 return 0;
4770 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03004771 help();
4772 return 0;
4773 case 'V':
4774 printf(QEMU_IMG_VERSION);
4775 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004776 case 'T':
4777 g_free(trace_file);
4778 trace_file = trace_opt_parse(optarg);
4779 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004780 }
bellardea2384d2004-08-01 21:59:26 +00004781 }
Stuart Brady153859b2009-06-07 00:42:17 +01004782
Denis V. Lunev10985132016-06-17 17:44:13 +03004783 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004784
Denis V. Lunev10985132016-06-17 17:44:13 +03004785 /* reset getopt_long scanning */
4786 argc -= optind;
4787 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004788 return 0;
4789 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004790 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004791 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004792
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004793 if (!trace_init_backends()) {
4794 exit(1);
4795 }
4796 trace_init_file(trace_file);
4797 qemu_set_log(LOG_TRACE);
4798
Denis V. Lunev10985132016-06-17 17:44:13 +03004799 /* find the command */
4800 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4801 if (!strcmp(cmdname, cmd->name)) {
4802 return cmd->handler(argc, argv);
4803 }
4804 }
Jeff Cody7db16892014-04-25 17:02:32 -04004805
Stuart Brady153859b2009-06-07 00:42:17 +01004806 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004807 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004808}