blob: a85eb3536eb3c5bf6e7a00750a9c8e325749352b [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"
BenoƮt Canetc054b3f2012-09-05 13:09:02 +020030#include "qapi-visit.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"
Fam Zheng335e9932017-05-03 00:35:39 +080033#include "qapi/qmp/qbool.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010034#include "qapi/qmp/qdict.h"
Markus Armbrusterfc81fa12018-02-01 12:18:40 +010035#include "qapi/qmp/qstring.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020036#include "qemu/cutils.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000037#include "qemu/config-file.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010038#include "qemu/option.h"
39#include "qemu/error-report.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030040#include "qemu/log.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000041#include "qom/object_interfaces.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010042#include "sysemu/sysemu.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020043#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010044#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020045#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080046#include "block/qapi.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010047#include "crypto/init.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030048#include "trace/control.h"
bellarde8445332006-06-14 15:32:10 +000049
Don Slutz61979a62015-01-09 10:17:35 -050050#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
Thomas Huth0781dd62016-10-05 11:54:44 +020051 "\n" QEMU_COPYRIGHT "\n"
Jeff Cody5f6979c2014-04-28 14:37:18 -040052
Anthony Liguoric227f092009-10-01 16:12:16 -050053typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010054 const char *name;
55 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050056} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010057
Federico Simoncelli8599ea42013-01-28 06:59:47 -050058enum {
59 OPTION_OUTPUT = 256,
60 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000061 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000062 OPTION_IMAGE_OPTS = 259,
Kevin Wolfb6495fa2015-07-10 18:09:18 +020063 OPTION_PATTERN = 260,
Kevin Wolf55d539c2016-06-03 13:59:41 +020064 OPTION_FLUSH_INTERVAL = 261,
65 OPTION_NO_DRAIN = 262,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +010066 OPTION_TARGET_IMAGE_OPTS = 263,
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +010067 OPTION_SIZE = 264,
Max Reitzdc5f6902017-06-13 22:20:55 +020068 OPTION_PREALLOCATION = 265,
Pavel Butsykin4ffca892017-09-18 15:42:27 +030069 OPTION_SHRINK = 266,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050070};
71
72typedef enum OutputFormat {
73 OFORMAT_JSON,
74 OFORMAT_HUMAN,
75} OutputFormat;
76
Kevin Wolfe6996142016-03-15 13:03:11 +010077/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040078#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000079
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010080static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000081{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010082 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000083}
84
Fam Zhengac1307a2014-04-22 13:36:11 +080085static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
86{
87 va_list ap;
88
89 error_printf("qemu-img: ");
90
91 va_start(ap, fmt);
92 error_vprintf(fmt, ap);
93 va_end(ap);
94
95 error_printf("\nTry 'qemu-img --help' for more information\n");
96 exit(EXIT_FAILURE);
97}
98
Stefan Hajnoczic9192972017-03-17 18:45:41 +080099static void QEMU_NORETURN missing_argument(const char *option)
100{
101 error_exit("missing argument for option '%s'", option);
102}
103
104static void QEMU_NORETURN unrecognized_option(const char *option)
105{
106 error_exit("unrecognized option '%s'", option);
107}
108
blueswir1d2c639d2009-01-24 18:19:25 +0000109/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +0800110static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +0000111{
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100112 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -0400113 QEMU_IMG_VERSION
Denis V. Lunev10985132016-06-17 17:44:13 +0300114 "usage: qemu-img [standard options] command [command options]\n"
malc3f020d72010-02-08 12:04:56 +0300115 "QEMU disk image utility\n"
116 "\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300117 " '-h', '--help' display this help and exit\n"
118 " '-V', '--version' output version information and exit\n"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +0300119 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
120 " specify tracing options\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300121 "\n"
malc3f020d72010-02-08 12:04:56 +0300122 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100123#define DEF(option, callback, arg_string) \
124 " " arg_string "\n"
125#include "qemu-img-cmds.h"
126#undef DEF
127#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +0300128 "\n"
129 "Command parameters:\n"
130 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000131 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
132 " manual page for a description of the object properties. The most common\n"
133 " object type is a 'secret', which is used to supply passwords and/or\n"
134 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300135 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400136 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800137 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
138 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100139 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
140 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300141 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200142 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
143 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
144 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300145 " 'output_filename' is the destination disk image filename\n"
146 " 'output_fmt' is the destination format\n"
147 " 'options' is a comma separated list of format specific options in a\n"
148 " name=value format. Use -o ? for an overview of the options supported by the\n"
149 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800150 " 'snapshot_param' is param used for internal snapshot, format\n"
151 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
152 " '[ID_OR_NAME]'\n"
153 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
154 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300155 " '-c' indicates that target image must be compressed (qcow format only)\n"
John Snow6e6e55f2017-07-17 20:34:22 -0400156 " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
157 " new backing file match exactly. The image doesn't need a working\n"
158 " backing file before rebasing in this case (useful for renaming the\n"
159 " backing file). For image creation, allow creating without attempting\n"
160 " to open the backing file.\n"
malc3f020d72010-02-08 12:04:56 +0300161 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200162 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100163 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200164 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
165 " contain only zeros for qemu-img to create a sparse image during\n"
166 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
167 " unallocated or zero sectors, and the destination image will always be\n"
168 " fully allocated\n"
BenoƮt Canetc054b3f2012-09-05 13:09:02 +0200169 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100170 " '-n' skips the target volume creation (useful if the volume is created\n"
171 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300172 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200173 "Parameters to check subcommand:\n"
174 " '-r' tries to repair any inconsistencies that are found during the check.\n"
175 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
176 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200177 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200178 "\n"
Peter Lieven2d9187b2017-02-28 13:40:07 +0100179 "Parameters to convert subcommand:\n"
180 " '-m' specifies how many coroutines work in parallel during the convert\n"
181 " process (defaults to 8)\n"
182 " '-W' allow to write to the target out of order rather than sequential\n"
183 "\n"
malc3f020d72010-02-08 12:04:56 +0300184 "Parameters to snapshot subcommand:\n"
185 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
186 " '-a' applies a snapshot (revert disk to saved state)\n"
187 " '-c' creates a snapshot\n"
188 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100189 " '-l' lists all snapshots in the given image\n"
190 "\n"
191 "Parameters to compare subcommand:\n"
192 " '-f' first image format\n"
193 " '-F' second image format\n"
Reda Sallahi86ce1f62016-08-10 04:43:12 +0200194 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
195 "\n"
196 "Parameters to dd subcommand:\n"
197 " 'bs=BYTES' read and write up to BYTES bytes at a time "
198 "(default: 512)\n"
199 " 'count=N' copy only N input blocks\n"
200 " 'if=FILE' read from FILE\n"
Reda Sallahif7c15532016-08-10 16:16:09 +0200201 " 'of=FILE' write to FILE\n"
202 " 'skip=N' skip N bs-sized blocks at the start of input\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100203
204 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100205 bdrv_iterate_format(format_print, NULL);
Eric Blakef5048cb2017-08-03 11:33:53 -0500206 printf("\n\n" QEMU_HELP_BOTTOM "\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800207 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000208}
209
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000210static QemuOptsList qemu_object_opts = {
211 .name = "object",
212 .implied_opt_name = "qom-type",
213 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
214 .desc = {
215 { }
216 },
217};
218
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000219static QemuOptsList qemu_source_opts = {
220 .name = "source",
221 .implied_opt_name = "file",
222 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
223 .desc = {
224 { }
225 },
226};
227
Stefan Weil7c30f652013-06-16 17:01:05 +0200228static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100229{
230 int ret = 0;
231 if (!quiet) {
232 va_list args;
233 va_start(args, fmt);
234 ret = vprintf(fmt, args);
235 va_end(args);
236 }
237 return ret;
238}
239
bellardea2384d2004-08-01 21:59:26 +0000240
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100241static int print_block_option_help(const char *filename, const char *fmt)
242{
243 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800244 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500245 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100246
247 /* Find driver and parse its options */
248 drv = bdrv_find_format(fmt);
249 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100250 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100251 return 1;
252 }
253
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800254 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100255 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500256 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100257 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100258 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800259 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100260 return 1;
261 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800262 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100263 }
264
Chunyan Liu83d05212014-06-05 17:20:51 +0800265 qemu_opts_print_help(create_opts);
266 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100267 return 0;
268}
269
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000270
Max Reitzefaa7c42016-03-16 19:54:38 +0100271static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100272 QemuOpts *opts, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800273 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000274{
275 QDict *options;
276 Error *local_err = NULL;
277 BlockBackend *blk;
278 options = qemu_opts_to_qdict(opts, NULL);
Fam Zheng335e9932017-05-03 00:35:39 +0800279 if (force_share) {
280 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
281 && !qdict_get_bool(options, BDRV_OPT_FORCE_SHARE)) {
282 error_report("--force-share/-U conflicts with image options");
Fam Zhengadb998c2017-05-15 22:10:14 +0800283 QDECREF(options);
Fam Zheng335e9932017-05-03 00:35:39 +0800284 return NULL;
285 }
Eric Blake579cf1d2017-05-15 14:54:39 -0500286 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800287 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100288 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000289 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100290 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000291 return NULL;
292 }
Kevin Wolfce099542016-03-15 13:01:04 +0100293 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000294
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000295 return blk;
296}
297
Max Reitzefaa7c42016-03-16 19:54:38 +0100298static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100299 QDict *options,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000300 const char *fmt, int flags,
Fam Zheng335e9932017-05-03 00:35:39 +0800301 bool writethrough, bool quiet,
302 bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000303{
304 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200305 Error *local_err = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100306
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100307 if (!options) {
308 options = qdict_new();
309 }
bellard75c23802004-08-27 21:28:58 +0000310 if (fmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500311 qdict_put_str(options, "driver", fmt);
bellard75c23802004-08-27 21:28:58 +0000312 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100313
Fam Zheng335e9932017-05-03 00:35:39 +0800314 if (force_share) {
Eric Blake579cf1d2017-05-15 14:54:39 -0500315 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800316 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100317 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500318 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100319 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000320 return NULL;
bellard75c23802004-08-27 21:28:58 +0000321 }
Kevin Wolfce099542016-03-15 13:01:04 +0100322 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100323
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200324 return blk;
bellard75c23802004-08-27 21:28:58 +0000325}
326
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000327
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100328static int img_add_key_secrets(void *opaque,
329 const char *name, const char *value,
330 Error **errp)
331{
332 QDict *options = opaque;
333
334 if (g_str_has_suffix(name, "key-secret")) {
Eric Blake187f47e2017-06-24 12:10:07 -0600335 qdict_put_str(options, name, value);
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100336 }
337
338 return 0;
339}
340
341static BlockBackend *img_open_new_file(const char *filename,
342 QemuOpts *create_opts,
343 const char *fmt, int flags,
344 bool writethrough, bool quiet,
345 bool force_share)
346{
347 QDict *options = NULL;
348
349 options = qdict_new();
350 qemu_opt_foreach(create_opts, img_add_key_secrets, options, &error_abort);
351
352 return img_open_file(filename, options, fmt, flags, writethrough, quiet,
353 force_share);
354}
355
356
Max Reitzefaa7c42016-03-16 19:54:38 +0100357static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000358 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100359 const char *fmt, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800360 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000361{
362 BlockBackend *blk;
363 if (image_opts) {
364 QemuOpts *opts;
365 if (fmt) {
366 error_report("--image-opts and --format are mutually exclusive");
367 return NULL;
368 }
369 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
370 filename, true);
371 if (!opts) {
372 return NULL;
373 }
Fam Zheng335e9932017-05-03 00:35:39 +0800374 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
375 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000376 } else {
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100377 blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
Fam Zheng335e9932017-05-03 00:35:39 +0800378 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000379 }
380 return blk;
381}
382
383
Chunyan Liu83d05212014-06-05 17:20:51 +0800384static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100385 const char *base_filename,
386 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200387{
Markus Armbruster6750e792015-02-12 17:43:08 +0100388 Error *err = NULL;
389
Kevin Wolfefa84d42009-05-18 16:42:12 +0200390 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100391 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100392 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100393 error_report("Backing file not supported for file format '%s'",
394 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100395 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900396 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200397 }
398 }
399 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100400 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100401 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100402 error_report("Backing file format not supported for file "
403 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100404 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900405 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200406 }
407 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900408 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200409}
410
Markus Armbruster606caa02017-02-21 21:14:04 +0100411static int64_t cvtnum(const char *s)
412{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100413 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100414 uint64_t value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100415
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100416 err = qemu_strtosz(s, NULL, &value);
417 if (err < 0) {
418 return err;
419 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100420 if (value > INT64_MAX) {
421 return -ERANGE;
422 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100423 return value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100424}
425
bellardea2384d2004-08-01 21:59:26 +0000426static int img_create(int argc, char **argv)
427{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200428 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100429 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000430 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000431 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000432 const char *filename;
433 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200434 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200435 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100436 bool quiet = false;
John Snow6e6e55f2017-07-17 20:34:22 -0400437 int flags = 0;
ths3b46e622007-09-17 08:09:54 +0000438
bellardea2384d2004-08-01 21:59:26 +0000439 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000440 static const struct option long_options[] = {
441 {"help", no_argument, 0, 'h'},
442 {"object", required_argument, 0, OPTION_OBJECT},
443 {0, 0, 0, 0}
444 };
John Snow6e6e55f2017-07-17 20:34:22 -0400445 c = getopt_long(argc, argv, ":F:b:f:ho:qu",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000446 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100447 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000448 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100449 }
bellardea2384d2004-08-01 21:59:26 +0000450 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800451 case ':':
452 missing_argument(argv[optind - 1]);
453 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100454 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800455 unrecognized_option(argv[optind - 1]);
456 break;
bellardea2384d2004-08-01 21:59:26 +0000457 case 'h':
458 help();
459 break;
aliguori9230eaf2009-03-28 17:55:19 +0000460 case 'F':
461 base_fmt = optarg;
462 break;
bellardea2384d2004-08-01 21:59:26 +0000463 case 'b':
464 base_filename = optarg;
465 break;
466 case 'f':
467 fmt = optarg;
468 break;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200469 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100470 if (!is_valid_option_list(optarg)) {
471 error_report("Invalid option list: %s", optarg);
472 goto fail;
473 }
474 if (!options) {
475 options = g_strdup(optarg);
476 } else {
477 char *old_options = options;
478 options = g_strdup_printf("%s,%s", options, optarg);
479 g_free(old_options);
480 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200481 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100482 case 'q':
483 quiet = true;
484 break;
John Snow6e6e55f2017-07-17 20:34:22 -0400485 case 'u':
486 flags |= BDRV_O_NO_BACKING;
487 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000488 case OPTION_OBJECT: {
489 QemuOpts *opts;
490 opts = qemu_opts_parse_noisily(&qemu_object_opts,
491 optarg, true);
492 if (!opts) {
493 goto fail;
494 }
495 } break;
bellardea2384d2004-08-01 21:59:26 +0000496 }
497 }
aliguori9230eaf2009-03-28 17:55:19 +0000498
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900499 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100500 filename = (optind < argc) ? argv[optind] : NULL;
501 if (options && has_help_option(options)) {
502 g_free(options);
503 return print_block_option_help(filename, fmt);
504 }
505
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100506 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800507 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100508 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100509 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900510
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000511 if (qemu_opts_foreach(&qemu_object_opts,
512 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200513 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000514 goto fail;
515 }
516
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100517 /* Get image size, if specified */
518 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100519 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100520
521 sval = cvtnum(argv[optind++]);
522 if (sval < 0) {
liguang79443392012-12-17 09:49:23 +0800523 if (sval == -ERANGE) {
524 error_report("Image size must be less than 8 EiB!");
525 } else {
526 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200527 "G, T, P or E suffixes for ");
528 error_report("kilobytes, megabytes, gigabytes, terabytes, "
529 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800530 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100531 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100532 }
533 img_size = (uint64_t)sval;
534 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200535 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800536 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200537 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100538
Luiz Capitulino9b375252012-11-30 10:52:05 -0200539 bdrv_img_create(filename, fmt, base_filename, base_fmt,
John Snow6e6e55f2017-07-17 20:34:22 -0400540 options, img_size, flags, quiet, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100541 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100542 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100543 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900544 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200545
Kevin Wolf77386bf2014-02-21 16:24:04 +0100546 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000547 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100548
549fail:
550 g_free(options);
551 return 1;
bellardea2384d2004-08-01 21:59:26 +0000552}
553
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100554static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500555{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500556 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500557 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100558 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600559
560 visit_type_ImageCheck(v, NULL, &check, &error_abort);
561 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500562 str = qobject_to_json_pretty(obj);
563 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100564 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500565 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600566 visit_free(v);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500567 QDECREF(str);
568}
569
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100570static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500571{
572 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100573 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500574 } else {
575 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100576 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
577 "Data may be corrupted, or further writes to the image "
578 "may corrupt it.\n",
579 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500580 }
581
582 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100583 qprintf(quiet,
584 "\n%" PRId64 " leaked clusters were found on the image.\n"
585 "This means waste of disk space, but no harm to data.\n",
586 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500587 }
588
589 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100590 qprintf(quiet,
591 "\n%" PRId64
592 " internal errors have occurred during the check.\n",
593 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500594 }
595 }
596
597 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100598 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
599 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
600 check->allocated_clusters, check->total_clusters,
601 check->allocated_clusters * 100.0 / check->total_clusters,
602 check->fragmented_clusters * 100.0 / check->allocated_clusters,
603 check->compressed_clusters * 100.0 /
604 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500605 }
606
607 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100608 qprintf(quiet,
609 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500610 }
611}
612
613static int collect_image_check(BlockDriverState *bs,
614 ImageCheck *check,
615 const char *filename,
616 const char *fmt,
617 int fix)
618{
619 int ret;
620 BdrvCheckResult result;
621
622 ret = bdrv_check(bs, &result, fix);
623 if (ret < 0) {
624 return ret;
625 }
626
627 check->filename = g_strdup(filename);
628 check->format = g_strdup(bdrv_get_format_name(bs));
629 check->check_errors = result.check_errors;
630 check->corruptions = result.corruptions;
631 check->has_corruptions = result.corruptions != 0;
632 check->leaks = result.leaks;
633 check->has_leaks = result.leaks != 0;
634 check->corruptions_fixed = result.corruptions_fixed;
635 check->has_corruptions_fixed = result.corruptions != 0;
636 check->leaks_fixed = result.leaks_fixed;
637 check->has_leaks_fixed = result.leaks != 0;
638 check->image_end_offset = result.image_end_offset;
639 check->has_image_end_offset = result.image_end_offset != 0;
640 check->total_clusters = result.bfi.total_clusters;
641 check->has_total_clusters = result.bfi.total_clusters != 0;
642 check->allocated_clusters = result.bfi.allocated_clusters;
643 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
644 check->fragmented_clusters = result.bfi.fragmented_clusters;
645 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100646 check->compressed_clusters = result.bfi.compressed_clusters;
647 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500648
649 return 0;
650}
651
Kevin Wolfe076f332010-06-29 11:43:13 +0200652/*
653 * Checks an image for consistency. Exit codes:
654 *
Max Reitzd6635c42014-06-02 22:15:21 +0200655 * 0 - Check completed, image is good
656 * 1 - Check not completed because of internal errors
657 * 2 - Check completed, image is corrupted
658 * 3 - Check completed, image has leaked clusters, but is good otherwise
659 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200660 */
aliguori15859692009-04-21 23:11:53 +0000661static int img_check(int argc, char **argv)
662{
663 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500664 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200665 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200666 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000667 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200668 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100669 int flags = BDRV_O_CHECK;
670 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200671 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100672 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000673 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +0800674 bool force_share = false;
aliguori15859692009-04-21 23:11:53 +0000675
676 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500677 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200678 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100679
aliguori15859692009-04-21 23:11:53 +0000680 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500681 int option_index = 0;
682 static const struct option long_options[] = {
683 {"help", no_argument, 0, 'h'},
684 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530685 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500686 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000687 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000688 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +0800689 {"force-share", no_argument, 0, 'U'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500690 {0, 0, 0, 0}
691 };
Fam Zheng335e9932017-05-03 00:35:39 +0800692 c = getopt_long(argc, argv, ":hf:r:T:qU",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500693 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100694 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000695 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100696 }
aliguori15859692009-04-21 23:11:53 +0000697 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800698 case ':':
699 missing_argument(argv[optind - 1]);
700 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100701 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800702 unrecognized_option(argv[optind - 1]);
703 break;
aliguori15859692009-04-21 23:11:53 +0000704 case 'h':
705 help();
706 break;
707 case 'f':
708 fmt = optarg;
709 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200710 case 'r':
711 flags |= BDRV_O_RDWR;
712
713 if (!strcmp(optarg, "leaks")) {
714 fix = BDRV_FIX_LEAKS;
715 } else if (!strcmp(optarg, "all")) {
716 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
717 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800718 error_exit("Unknown option value for -r "
719 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200720 }
721 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500722 case OPTION_OUTPUT:
723 output = optarg;
724 break;
Max Reitz40055952014-07-22 22:58:42 +0200725 case 'T':
726 cache = optarg;
727 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100728 case 'q':
729 quiet = true;
730 break;
Fam Zheng335e9932017-05-03 00:35:39 +0800731 case 'U':
732 force_share = true;
733 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000734 case OPTION_OBJECT: {
735 QemuOpts *opts;
736 opts = qemu_opts_parse_noisily(&qemu_object_opts,
737 optarg, true);
738 if (!opts) {
739 return 1;
740 }
741 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000742 case OPTION_IMAGE_OPTS:
743 image_opts = true;
744 break;
aliguori15859692009-04-21 23:11:53 +0000745 }
746 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200747 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800748 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100749 }
aliguori15859692009-04-21 23:11:53 +0000750 filename = argv[optind++];
751
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500752 if (output && !strcmp(output, "json")) {
753 output_format = OFORMAT_JSON;
754 } else if (output && !strcmp(output, "human")) {
755 output_format = OFORMAT_HUMAN;
756 } else if (output) {
757 error_report("--output must be used with human or json as argument.");
758 return 1;
759 }
760
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000761 if (qemu_opts_foreach(&qemu_object_opts,
762 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200763 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000764 return 1;
765 }
766
Kevin Wolfce099542016-03-15 13:01:04 +0100767 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200768 if (ret < 0) {
769 error_report("Invalid source cache option: %s", cache);
770 return 1;
771 }
772
Fam Zheng335e9932017-05-03 00:35:39 +0800773 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
774 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200775 if (!blk) {
776 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900777 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200778 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500779
780 check = g_new0(ImageCheck, 1);
781 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200782
783 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200784 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200785 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500786 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200787 }
788
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500789 if (check->corruptions_fixed || check->leaks_fixed) {
790 int corruptions_fixed, leaks_fixed;
791
792 leaks_fixed = check->leaks_fixed;
793 corruptions_fixed = check->corruptions_fixed;
794
795 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100796 qprintf(quiet,
797 "The following inconsistencies were found and repaired:\n\n"
798 " %" PRId64 " leaked clusters\n"
799 " %" PRId64 " corruptions\n\n"
800 "Double checking the fixed image now...\n",
801 check->leaks_fixed,
802 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500803 }
804
805 ret = collect_image_check(bs, check, filename, fmt, 0);
806
807 check->leaks_fixed = leaks_fixed;
808 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200809 }
810
Max Reitz832390a2014-10-23 15:29:12 +0200811 if (!ret) {
812 switch (output_format) {
813 case OFORMAT_HUMAN:
814 dump_human_image_check(check, quiet);
815 break;
816 case OFORMAT_JSON:
817 dump_json_image_check(check, quiet);
818 break;
819 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500820 }
821
822 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200823 if (ret) {
824 error_report("Check failed: %s", strerror(-ret));
825 } else {
826 error_report("Check failed");
827 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500828 ret = 1;
829 goto fail;
830 }
831
832 if (check->corruptions) {
833 ret = 2;
834 } else if (check->leaks) {
835 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200836 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500837 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000838 }
839
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500840fail:
841 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200842 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500843 return ret;
aliguori15859692009-04-21 23:11:53 +0000844}
845
Max Reitzd4a32382014-10-24 15:57:37 +0200846typedef struct CommonBlockJobCBInfo {
847 BlockDriverState *bs;
848 Error **errp;
849} CommonBlockJobCBInfo;
850
851static void common_block_job_cb(void *opaque, int ret)
852{
853 CommonBlockJobCBInfo *cbi = opaque;
854
855 if (ret < 0) {
856 error_setg_errno(cbi->errp, -ret, "Block job failed");
857 }
Max Reitzd4a32382014-10-24 15:57:37 +0200858}
859
860static void run_block_job(BlockJob *job, Error **errp)
861{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200862 AioContext *aio_context = blk_get_aio_context(job->blk);
sochin.jiang4172a002017-06-15 14:47:33 +0800863 int ret = 0;
Max Reitzd4a32382014-10-24 15:57:37 +0200864
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200865 aio_context_acquire(aio_context);
sochin.jiang4172a002017-06-15 14:47:33 +0800866 block_job_ref(job);
Max Reitzd4a32382014-10-24 15:57:37 +0200867 do {
868 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500869 qemu_progress_print(job->len ?
870 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
sochin.jiang4172a002017-06-15 14:47:33 +0800871 } while (!job->ready && !job->completed);
Max Reitzd4a32382014-10-24 15:57:37 +0200872
sochin.jiang4172a002017-06-15 14:47:33 +0800873 if (!job->completed) {
874 ret = block_job_complete_sync(job, errp);
875 } else {
876 ret = job->ret;
877 }
878 block_job_unref(job);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200879 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200880
sochin.jiang4172a002017-06-15 14:47:33 +0800881 /* publish completion progress only when success */
882 if (!ret) {
883 qemu_progress_print(100.f, 0);
884 }
Max Reitzd4a32382014-10-24 15:57:37 +0200885}
886
bellardea2384d2004-08-01 21:59:26 +0000887static int img_commit(int argc, char **argv)
888{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400889 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200890 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200891 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200892 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100893 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200894 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100895 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200896 Error *local_err = NULL;
897 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000898 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200899 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000900
901 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400902 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200903 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000904 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000905 static const struct option long_options[] = {
906 {"help", no_argument, 0, 'h'},
907 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000908 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000909 {0, 0, 0, 0}
910 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800911 c = getopt_long(argc, argv, ":f:ht:b:dpq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000912 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100913 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000914 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100915 }
bellardea2384d2004-08-01 21:59:26 +0000916 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800917 case ':':
918 missing_argument(argv[optind - 1]);
919 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100920 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800921 unrecognized_option(argv[optind - 1]);
922 break;
bellardea2384d2004-08-01 21:59:26 +0000923 case 'h':
924 help();
925 break;
926 case 'f':
927 fmt = optarg;
928 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400929 case 't':
930 cache = optarg;
931 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200932 case 'b':
933 base = optarg;
934 /* -b implies -d */
935 drop = true;
936 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200937 case 'd':
938 drop = true;
939 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200940 case 'p':
941 progress = true;
942 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100943 case 'q':
944 quiet = true;
945 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000946 case OPTION_OBJECT: {
947 QemuOpts *opts;
948 opts = qemu_opts_parse_noisily(&qemu_object_opts,
949 optarg, true);
950 if (!opts) {
951 return 1;
952 }
953 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000954 case OPTION_IMAGE_OPTS:
955 image_opts = true;
956 break;
bellardea2384d2004-08-01 21:59:26 +0000957 }
958 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200959
960 /* Progress is not shown in Quiet mode */
961 if (quiet) {
962 progress = false;
963 }
964
Kevin Wolffc11eb22013-08-05 10:53:04 +0200965 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800966 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100967 }
bellardea2384d2004-08-01 21:59:26 +0000968 filename = argv[optind++];
969
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000970 if (qemu_opts_foreach(&qemu_object_opts,
971 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200972 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000973 return 1;
974 }
975
Max Reitz9a86fe42014-10-24 15:57:38 +0200976 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100977 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400978 if (ret < 0) {
979 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100980 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400981 }
982
Fam Zheng335e9932017-05-03 00:35:39 +0800983 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
984 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200985 if (!blk) {
986 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900987 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200988 bs = blk_bs(blk);
989
Max Reitz687fa1d2014-10-24 15:57:39 +0200990 qemu_progress_init(progress, 1.f);
991 qemu_progress_print(0.f, 100);
992
Max Reitz1b22bff2014-10-24 15:57:40 +0200993 if (base) {
994 base_bs = bdrv_find_backing_image(bs, base);
995 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +0100996 error_setg(&local_err,
997 "Did not find '%s' in the backing chain of '%s'",
998 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +0200999 goto done;
1000 }
1001 } else {
1002 /* This is different from QMP, which by default uses the deepest file in
1003 * the backing chain (i.e., the very base); however, the traditional
1004 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +02001005 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +02001006 if (!base_bs) {
1007 error_setg(&local_err, "Image does not have a backing file");
1008 goto done;
1009 }
bellardea2384d2004-08-01 21:59:26 +00001010 }
1011
Max Reitzd4a32382014-10-24 15:57:37 +02001012 cbi = (CommonBlockJobCBInfo){
1013 .errp = &local_err,
1014 .bs = bs,
1015 };
1016
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001017 aio_context = bdrv_get_aio_context(bs);
1018 aio_context_acquire(aio_context);
John Snow47970df2016-10-27 12:06:57 -04001019 commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
Kevin Wolf0db832f2017-02-20 18:10:05 +01001020 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +08001021 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001022 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +02001023 if (local_err) {
1024 goto done;
1025 }
1026
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001027 /* When the block job completes, the BlockBackend reference will point to
1028 * the old backing file. In order to avoid that the top image is already
1029 * deleted, so we can still empty it afterwards, increment the reference
1030 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +02001031 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001032 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001033 }
1034
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001035 job = block_job_get("commit");
1036 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001037 if (local_err) {
1038 goto unref_backing;
1039 }
1040
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001041 if (!drop && bs->drv->bdrv_make_empty) {
1042 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001043 if (ret) {
1044 error_setg_errno(&local_err, -ret, "Could not empty %s",
1045 filename);
1046 goto unref_backing;
1047 }
1048 }
1049
1050unref_backing:
1051 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001052 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001053 }
Max Reitzd4a32382014-10-24 15:57:37 +02001054
1055done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001056 qemu_progress_end();
1057
Markus Armbruster26f54e92014-10-07 13:59:04 +02001058 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001059
1060 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001061 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001062 return 1;
1063 }
Max Reitzd4a32382014-10-24 15:57:37 +02001064
1065 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001066 return 0;
1067}
1068
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001069/*
Eric Blakedebb38a2017-10-11 22:47:11 -05001070 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1071 * of the first sector boundary within buf where the sector contains a
1072 * non-zero byte. This function is robust to a buffer that is not
1073 * sector-aligned.
1074 */
1075static int64_t find_nonzero(const uint8_t *buf, int64_t n)
1076{
1077 int64_t i;
1078 int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE);
1079
1080 for (i = 0; i < end; i += BDRV_SECTOR_SIZE) {
1081 if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) {
1082 return i;
1083 }
1084 }
1085 if (i < n && !buffer_is_zero(buf + i, n - end)) {
1086 return i;
1087 }
1088 return -1;
1089}
1090
1091/*
thsf58c7b32008-06-05 21:53:49 +00001092 * Returns true iff the first sector pointed to by 'buf' contains at least
1093 * a non-NUL byte.
1094 *
1095 * 'pnum' is set to the number of sectors (including and immediately following
1096 * the first one) that are known to be in the same allocated/unallocated state.
1097 */
bellardea2384d2004-08-01 21:59:26 +00001098static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1099{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001100 bool is_zero;
1101 int i;
bellardea2384d2004-08-01 21:59:26 +00001102
1103 if (n <= 0) {
1104 *pnum = 0;
1105 return 0;
1106 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001107 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001108 for(i = 1; i < n; i++) {
1109 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001110 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001111 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001112 }
bellardea2384d2004-08-01 21:59:26 +00001113 }
1114 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001115 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001116}
1117
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001118/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001119 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1120 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1121 * breaking up write requests for only small sparse areas.
1122 */
1123static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1124 int min)
1125{
1126 int ret;
1127 int num_checked, num_used;
1128
1129 if (n < min) {
1130 min = n;
1131 }
1132
1133 ret = is_allocated_sectors(buf, n, pnum);
1134 if (!ret) {
1135 return ret;
1136 }
1137
1138 num_used = *pnum;
1139 buf += BDRV_SECTOR_SIZE * *pnum;
1140 n -= *pnum;
1141 num_checked = num_used;
1142
1143 while (n > 0) {
1144 ret = is_allocated_sectors(buf, n, pnum);
1145
1146 buf += BDRV_SECTOR_SIZE * *pnum;
1147 n -= *pnum;
1148 num_checked += *pnum;
1149 if (ret) {
1150 num_used = num_checked;
1151 } else if (*pnum >= min) {
1152 break;
1153 }
1154 }
1155
1156 *pnum = num_used;
1157 return 1;
1158}
1159
1160/*
Eric Blakedc61cd32017-10-11 22:47:14 -05001161 * Compares two buffers sector by sector. Returns 0 if the first
1162 * sector of each buffer matches, non-zero otherwise.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001163 *
Eric Blakedc61cd32017-10-11 22:47:14 -05001164 * pnum is set to the sector-aligned size of the buffer prefix that
1165 * has the same matching status as the first sector.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001166 */
Eric Blakedc61cd32017-10-11 22:47:14 -05001167static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2,
1168 int64_t bytes, int64_t *pnum)
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001169{
Radim KrčmÔř8c1ac472015-02-20 17:06:15 +01001170 bool res;
Eric Blakedc61cd32017-10-11 22:47:14 -05001171 int64_t i = MIN(bytes, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001172
Eric Blakedc61cd32017-10-11 22:47:14 -05001173 assert(bytes > 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001174
Eric Blakedc61cd32017-10-11 22:47:14 -05001175 res = !!memcmp(buf1, buf2, i);
1176 while (i < bytes) {
1177 int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001178
Eric Blakedc61cd32017-10-11 22:47:14 -05001179 if (!!memcmp(buf1 + i, buf2 + i, len) != res) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001180 break;
1181 }
Eric Blakedc61cd32017-10-11 22:47:14 -05001182 i += len;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001183 }
1184
1185 *pnum = i;
1186 return res;
1187}
1188
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001189#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001190
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001191/*
1192 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1193 *
Eric Blake0608e402017-10-11 22:47:12 -05001194 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1195 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1196 * failure), and 4 on error (the exit status for read errors), after emitting
1197 * an error message.
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001198 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001199 * @param blk: BlockBackend for the image
Eric Blakec41508e2017-10-11 22:47:13 -05001200 * @param offset: Starting offset to check
1201 * @param bytes: Number of bytes to check
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001202 * @param filename: Name of disk file we are checking (logging purpose)
1203 * @param buffer: Allocated buffer for storing read data
1204 * @param quiet: Flag for quiet mode
1205 */
Eric Blakec41508e2017-10-11 22:47:13 -05001206static int check_empty_sectors(BlockBackend *blk, int64_t offset,
1207 int64_t bytes, const char *filename,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001208 uint8_t *buffer, bool quiet)
1209{
Eric Blakedebb38a2017-10-11 22:47:11 -05001210 int ret = 0;
1211 int64_t idx;
1212
Eric Blakec41508e2017-10-11 22:47:13 -05001213 ret = blk_pread(blk, offset, buffer, bytes);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001214 if (ret < 0) {
1215 error_report("Error while reading offset %" PRId64 " of %s: %s",
Eric Blakec41508e2017-10-11 22:47:13 -05001216 offset, filename, strerror(-ret));
Eric Blake0608e402017-10-11 22:47:12 -05001217 return 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001218 }
Eric Blakec41508e2017-10-11 22:47:13 -05001219 idx = find_nonzero(buffer, bytes);
Eric Blakedebb38a2017-10-11 22:47:11 -05001220 if (idx >= 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001221 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blakec41508e2017-10-11 22:47:13 -05001222 offset + idx);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001223 return 1;
1224 }
1225
1226 return 0;
1227}
1228
1229/*
1230 * Compares two images. Exit codes:
1231 *
1232 * 0 - Images are identical
1233 * 1 - Images differ
1234 * >1 - Error occurred
1235 */
1236static int img_compare(int argc, char **argv)
1237{
Max Reitz40055952014-07-22 22:58:42 +02001238 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001239 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001240 BlockDriverState *bs1, *bs2;
Eric Blake033d9fc2017-10-11 22:47:16 -05001241 int64_t total_size1, total_size2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001242 uint8_t *buf1 = NULL, *buf2 = NULL;
Eric Blake31826642017-10-11 22:47:08 -05001243 int64_t pnum1, pnum2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001244 int allocated1, allocated2;
1245 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1246 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001247 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001248 bool writethrough;
Eric Blake033d9fc2017-10-11 22:47:16 -05001249 int64_t total_size;
1250 int64_t offset = 0;
1251 int64_t chunk;
Eric Blakedc61cd32017-10-11 22:47:14 -05001252 int c;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001253 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001254 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001255 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001256
Max Reitz40055952014-07-22 22:58:42 +02001257 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001258 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001259 static const struct option long_options[] = {
1260 {"help", no_argument, 0, 'h'},
1261 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001262 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001263 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001264 {0, 0, 0, 0}
1265 };
Fam Zheng335e9932017-05-03 00:35:39 +08001266 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001267 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001268 if (c == -1) {
1269 break;
1270 }
1271 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001272 case ':':
1273 missing_argument(argv[optind - 1]);
1274 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001275 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001276 unrecognized_option(argv[optind - 1]);
1277 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001278 case 'h':
1279 help();
1280 break;
1281 case 'f':
1282 fmt1 = optarg;
1283 break;
1284 case 'F':
1285 fmt2 = optarg;
1286 break;
Max Reitz40055952014-07-22 22:58:42 +02001287 case 'T':
1288 cache = optarg;
1289 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001290 case 'p':
1291 progress = true;
1292 break;
1293 case 'q':
1294 quiet = true;
1295 break;
1296 case 's':
1297 strict = true;
1298 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001299 case 'U':
1300 force_share = true;
1301 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001302 case OPTION_OBJECT: {
1303 QemuOpts *opts;
1304 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1305 optarg, true);
1306 if (!opts) {
1307 ret = 2;
1308 goto out4;
1309 }
1310 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001311 case OPTION_IMAGE_OPTS:
1312 image_opts = true;
1313 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001314 }
1315 }
1316
1317 /* Progress is not shown in Quiet mode */
1318 if (quiet) {
1319 progress = false;
1320 }
1321
1322
Kevin Wolffc11eb22013-08-05 10:53:04 +02001323 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001324 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001325 }
1326 filename1 = argv[optind++];
1327 filename2 = argv[optind++];
1328
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001329 if (qemu_opts_foreach(&qemu_object_opts,
1330 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001331 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001332 ret = 2;
1333 goto out4;
1334 }
1335
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001336 /* Initialize before goto out */
1337 qemu_progress_init(progress, 2.0);
1338
Kevin Wolfce099542016-03-15 13:01:04 +01001339 flags = 0;
1340 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001341 if (ret < 0) {
1342 error_report("Invalid source cache option: %s", cache);
1343 ret = 2;
1344 goto out3;
1345 }
1346
Fam Zheng335e9932017-05-03 00:35:39 +08001347 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1348 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001349 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001350 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001351 goto out3;
1352 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001353
Fam Zheng335e9932017-05-03 00:35:39 +08001354 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1355 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001356 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001357 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001358 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001359 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001360 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001361 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001362
Max Reitzf1d3cd72015-02-05 13:58:18 -05001363 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1364 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
Eric Blake033d9fc2017-10-11 22:47:16 -05001365 total_size1 = blk_getlength(blk1);
1366 if (total_size1 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001367 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001368 filename1, strerror(-total_size1));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001369 ret = 4;
1370 goto out;
1371 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001372 total_size2 = blk_getlength(blk2);
1373 if (total_size2 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001374 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001375 filename2, strerror(-total_size2));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001376 ret = 4;
1377 goto out;
1378 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001379 total_size = MIN(total_size1, total_size2);
1380 progress_base = MAX(total_size1, total_size2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001381
1382 qemu_progress_print(0, 100);
1383
Eric Blake033d9fc2017-10-11 22:47:16 -05001384 if (strict && total_size1 != total_size2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001385 ret = 1;
1386 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1387 goto out;
1388 }
1389
Eric Blake033d9fc2017-10-11 22:47:16 -05001390 while (offset < total_size) {
Eric Blake31826642017-10-11 22:47:08 -05001391 int status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001392
Eric Blake033d9fc2017-10-11 22:47:16 -05001393 status1 = bdrv_block_status_above(bs1, NULL, offset,
1394 total_size1 - offset, &pnum1, NULL,
1395 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001396 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001397 ret = 3;
1398 error_report("Sector allocation test failed for %s", filename1);
1399 goto out;
1400 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001401 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001402
Eric Blake033d9fc2017-10-11 22:47:16 -05001403 status2 = bdrv_block_status_above(bs2, NULL, offset,
1404 total_size2 - offset, &pnum2, NULL,
1405 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001406 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001407 ret = 3;
1408 error_report("Sector allocation test failed for %s", filename2);
1409 goto out;
1410 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001411 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
Eric Blake7daddc62017-10-11 22:47:09 -05001412
1413 assert(pnum1 && pnum2);
Eric Blake033d9fc2017-10-11 22:47:16 -05001414 chunk = MIN(pnum1, pnum2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001415
Fam Zheng25ad8e62016-01-13 16:37:41 +08001416 if (strict) {
Eric Blake31826642017-10-11 22:47:08 -05001417 if (status1 != status2) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001418 ret = 1;
1419 qprintf(quiet, "Strict mode: Offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001420 " block status mismatch!\n", offset);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001421 goto out;
1422 }
1423 }
1424 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
Eric Blake7daddc62017-10-11 22:47:09 -05001425 /* nothing to do */
Fam Zheng25ad8e62016-01-13 16:37:41 +08001426 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001427 if (allocated1) {
Eric Blakedc61cd32017-10-11 22:47:14 -05001428 int64_t pnum;
1429
Eric Blake033d9fc2017-10-11 22:47:16 -05001430 chunk = MIN(chunk, IO_BUF_SIZE);
1431 ret = blk_pread(blk1, offset, buf1, chunk);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001432 if (ret < 0) {
1433 error_report("Error while reading offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001434 " of %s: %s",
1435 offset, filename1, strerror(-ret));
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001436 ret = 4;
1437 goto out;
1438 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001439 ret = blk_pread(blk2, offset, buf2, chunk);
1440 if (ret < 0) {
1441 error_report("Error while reading offset %" PRId64
1442 " of %s: %s",
1443 offset, filename2, strerror(-ret));
1444 ret = 4;
1445 goto out;
1446 }
1447 ret = compare_buffers(buf1, buf2, chunk, &pnum);
1448 if (ret || pnum != chunk) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001449 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blake033d9fc2017-10-11 22:47:16 -05001450 offset + (ret ? 0 : pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001451 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001452 goto out;
1453 }
1454 }
1455 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001456 chunk = MIN(chunk, IO_BUF_SIZE);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001457 if (allocated1) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001458 ret = check_empty_sectors(blk1, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001459 filename1, buf1, quiet);
1460 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001461 ret = check_empty_sectors(blk2, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001462 filename2, buf1, quiet);
1463 }
1464 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001465 goto out;
1466 }
1467 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001468 offset += chunk;
1469 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001470 }
1471
Eric Blake033d9fc2017-10-11 22:47:16 -05001472 if (total_size1 != total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001473 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001474 const char *filename_over;
1475
1476 qprintf(quiet, "Warning: Image size mismatch!\n");
Eric Blake033d9fc2017-10-11 22:47:16 -05001477 if (total_size1 > total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001478 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001479 filename_over = filename1;
1480 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001481 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001482 filename_over = filename2;
1483 }
1484
Eric Blake033d9fc2017-10-11 22:47:16 -05001485 while (offset < progress_base) {
1486 ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset,
1487 progress_base - offset, &chunk,
1488 NULL, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001489 if (ret < 0) {
1490 ret = 3;
1491 error_report("Sector allocation test failed for %s",
1492 filename_over);
1493 goto out;
1494
1495 }
Eric Blake391cb1a2017-10-11 22:47:10 -05001496 if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001497 chunk = MIN(chunk, IO_BUF_SIZE);
1498 ret = check_empty_sectors(blk_over, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001499 filename_over, buf1, quiet);
1500 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001501 goto out;
1502 }
1503 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001504 offset += chunk;
1505 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001506 }
1507 }
1508
1509 qprintf(quiet, "Images are identical.\n");
1510 ret = 0;
1511
1512out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001513 qemu_vfree(buf1);
1514 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001515 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001516out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001517 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001518out3:
1519 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001520out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001521 return ret;
1522}
1523
Kevin Wolf690c7302015-03-19 13:33:32 +01001524enum ImgConvertBlockStatus {
1525 BLK_DATA,
1526 BLK_ZERO,
1527 BLK_BACKING_FILE,
1528};
1529
Peter Lieven2d9187b2017-02-28 13:40:07 +01001530#define MAX_COROUTINES 16
1531
Kevin Wolf690c7302015-03-19 13:33:32 +01001532typedef struct ImgConvertState {
1533 BlockBackend **src;
1534 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001535 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001536 int64_t total_sectors;
1537 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001538 int64_t allocated_done;
1539 int64_t sector_num;
1540 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001541 enum ImgConvertBlockStatus status;
1542 int64_t sector_next_status;
1543 BlockBackend *target;
1544 bool has_zero_init;
1545 bool compressed;
1546 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001547 bool wr_in_order;
Kevin Wolf690c7302015-03-19 13:33:32 +01001548 int min_sparse;
1549 size_t cluster_sectors;
1550 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001551 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001552 int running_coroutines;
1553 Coroutine *co[MAX_COROUTINES];
1554 int64_t wait_sector_num[MAX_COROUTINES];
1555 CoMutex lock;
1556 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001557} ImgConvertState;
1558
Peter Lieven2d9187b2017-02-28 13:40:07 +01001559static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1560 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001561{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001562 *src_cur = 0;
1563 *src_cur_offset = 0;
1564 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1565 *src_cur_offset += s->src_sectors[*src_cur];
1566 (*src_cur)++;
1567 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001568 }
1569}
1570
1571static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1572{
Eric Blake31826642017-10-11 22:47:08 -05001573 int64_t src_cur_offset;
1574 int ret, n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001575
Peter Lieven2d9187b2017-02-28 13:40:07 +01001576 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001577
1578 assert(s->total_sectors > sector_num);
1579 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1580
1581 if (s->sector_next_status <= sector_num) {
Eric Blake31826642017-10-11 22:47:08 -05001582 int64_t count = n * BDRV_SECTOR_SIZE;
1583
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001584 if (s->target_has_backing) {
Eric Blake237d78f2017-10-11 22:47:03 -05001585
1586 ret = bdrv_block_status(blk_bs(s->src[src_cur]),
1587 (sector_num - src_cur_offset) *
1588 BDRV_SECTOR_SIZE,
1589 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001590 } else {
Eric Blake31826642017-10-11 22:47:08 -05001591 ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL,
1592 (sector_num - src_cur_offset) *
1593 BDRV_SECTOR_SIZE,
1594 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001595 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001596 if (ret < 0) {
1597 return ret;
1598 }
Eric Blake31826642017-10-11 22:47:08 -05001599 n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
Kevin Wolf690c7302015-03-19 13:33:32 +01001600
1601 if (ret & BDRV_BLOCK_ZERO) {
1602 s->status = BLK_ZERO;
1603 } else if (ret & BDRV_BLOCK_DATA) {
1604 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001605 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001606 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001607 }
1608
1609 s->sector_next_status = sector_num + n;
1610 }
1611
1612 n = MIN(n, s->sector_next_status - sector_num);
1613 if (s->status == BLK_DATA) {
1614 n = MIN(n, s->buf_sectors);
1615 }
1616
1617 /* We need to write complete clusters for compressed images, so if an
1618 * unallocated area is shorter than that, we must consider the whole
1619 * cluster allocated. */
1620 if (s->compressed) {
1621 if (n < s->cluster_sectors) {
1622 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1623 s->status = BLK_DATA;
1624 } else {
1625 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1626 }
1627 }
1628
1629 return n;
1630}
1631
Peter Lieven2d9187b2017-02-28 13:40:07 +01001632static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1633 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001634{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001635 int n, ret;
1636 QEMUIOVector qiov;
1637 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001638
Kevin Wolf690c7302015-03-19 13:33:32 +01001639 assert(nb_sectors <= s->buf_sectors);
1640 while (nb_sectors > 0) {
1641 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001642 int src_cur;
1643 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001644
1645 /* In the case of compression with multiple source files, we can get a
1646 * nb_sectors that spreads into the next part. So we must be able to
1647 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001648 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1649 blk = s->src[src_cur];
1650 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001651
Peter Lieven2d9187b2017-02-28 13:40:07 +01001652 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1653 iov.iov_base = buf;
1654 iov.iov_len = n << BDRV_SECTOR_BITS;
1655 qemu_iovec_init_external(&qiov, &iov, 1);
1656
1657 ret = blk_co_preadv(
1658 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1659 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001660 if (ret < 0) {
1661 return ret;
1662 }
1663
1664 sector_num += n;
1665 nb_sectors -= n;
1666 buf += n * BDRV_SECTOR_SIZE;
1667 }
1668
1669 return 0;
1670}
1671
Peter Lieven2d9187b2017-02-28 13:40:07 +01001672
1673static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1674 int nb_sectors, uint8_t *buf,
1675 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001676{
1677 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001678 QEMUIOVector qiov;
1679 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001680
1681 while (nb_sectors > 0) {
1682 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001683 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1684
Peter Lieven2d9187b2017-02-28 13:40:07 +01001685 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001686 case BLK_BACKING_FILE:
1687 /* If we have a backing file, leave clusters unallocated that are
1688 * unallocated in the source image, so that the backing file is
1689 * visible at the respective offset. */
1690 assert(s->target_has_backing);
1691 break;
1692
1693 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001694 /* If we're told to keep the target fully allocated (-S 0) or there
1695 * is real non-zero data, we must write it. Otherwise we can treat
1696 * it as zero sectors.
1697 * Compressed clusters need to be written as a whole, so in that
1698 * case we can only save the write if the buffer is completely
1699 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001700 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001701 (!s->compressed &&
1702 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1703 (s->compressed &&
1704 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001705 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001706 iov.iov_base = buf;
1707 iov.iov_len = n << BDRV_SECTOR_BITS;
1708 qemu_iovec_init_external(&qiov, &iov, 1);
1709
1710 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
Lidong Chendb933fb2017-04-27 10:58:27 +08001711 n << BDRV_SECTOR_BITS, &qiov, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001712 if (ret < 0) {
1713 return ret;
1714 }
1715 break;
1716 }
1717 /* fall-through */
1718
1719 case BLK_ZERO:
1720 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001721 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001722 break;
1723 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001724 ret = blk_co_pwrite_zeroes(s->target,
1725 sector_num << BDRV_SECTOR_BITS,
1726 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001727 if (ret < 0) {
1728 return ret;
1729 }
1730 break;
1731 }
1732
1733 sector_num += n;
1734 nb_sectors -= n;
1735 buf += n * BDRV_SECTOR_SIZE;
1736 }
1737
1738 return 0;
1739}
1740
Peter Lieven2d9187b2017-02-28 13:40:07 +01001741static void coroutine_fn convert_co_do_copy(void *opaque)
1742{
1743 ImgConvertState *s = opaque;
1744 uint8_t *buf = NULL;
1745 int ret, i;
1746 int index = -1;
1747
1748 for (i = 0; i < s->num_coroutines; i++) {
1749 if (s->co[i] == qemu_coroutine_self()) {
1750 index = i;
1751 break;
1752 }
1753 }
1754 assert(index >= 0);
1755
1756 s->running_coroutines++;
1757 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1758
1759 while (1) {
1760 int n;
1761 int64_t sector_num;
1762 enum ImgConvertBlockStatus status;
1763
1764 qemu_co_mutex_lock(&s->lock);
1765 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1766 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001767 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001768 }
1769 n = convert_iteration_sectors(s, s->sector_num);
1770 if (n < 0) {
1771 qemu_co_mutex_unlock(&s->lock);
1772 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001773 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001774 }
1775 /* save current sector and allocation status to local variables */
1776 sector_num = s->sector_num;
1777 status = s->status;
1778 if (!s->min_sparse && s->status == BLK_ZERO) {
1779 n = MIN(n, s->buf_sectors);
1780 }
1781 /* increment global sector counter so that other coroutines can
1782 * already continue reading beyond this request */
1783 s->sector_num += n;
1784 qemu_co_mutex_unlock(&s->lock);
1785
1786 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1787 s->allocated_done += n;
1788 qemu_progress_print(100.0 * s->allocated_done /
1789 s->allocated_sectors, 0);
1790 }
1791
1792 if (status == BLK_DATA) {
1793 ret = convert_co_read(s, sector_num, n, buf);
1794 if (ret < 0) {
1795 error_report("error while reading sector %" PRId64
1796 ": %s", sector_num, strerror(-ret));
1797 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001798 }
1799 } else if (!s->min_sparse && status == BLK_ZERO) {
1800 status = BLK_DATA;
1801 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1802 }
1803
1804 if (s->wr_in_order) {
1805 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03001806 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001807 s->wait_sector_num[index] = sector_num;
1808 qemu_coroutine_yield();
1809 }
1810 s->wait_sector_num[index] = -1;
1811 }
1812
Anton Nefedovb91127e2017-04-26 11:33:15 +03001813 if (s->ret == -EINPROGRESS) {
1814 ret = convert_co_write(s, sector_num, n, buf, status);
1815 if (ret < 0) {
1816 error_report("error while writing sector %" PRId64
1817 ": %s", sector_num, strerror(-ret));
1818 s->ret = ret;
1819 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001820 }
1821
1822 if (s->wr_in_order) {
1823 /* reenter the coroutine that might have waited
1824 * for this write to complete */
1825 s->wr_offs = sector_num + n;
1826 for (i = 0; i < s->num_coroutines; i++) {
1827 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1828 /*
1829 * A -> B -> A cannot occur because A has
1830 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1831 * B will never enter A during this time window.
1832 */
1833 qemu_coroutine_enter(s->co[i]);
1834 break;
1835 }
1836 }
1837 }
1838 }
1839
Peter Lieven2d9187b2017-02-28 13:40:07 +01001840 qemu_vfree(buf);
1841 s->co[index] = NULL;
1842 s->running_coroutines--;
1843 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1844 /* the convert job finished successfully */
1845 s->ret = 0;
1846 }
1847}
1848
Kevin Wolf690c7302015-03-19 13:33:32 +01001849static int convert_do_copy(ImgConvertState *s)
1850{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001851 int ret, i, n;
1852 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001853
1854 /* Check whether we have zero initialisation or can get it efficiently */
1855 s->has_zero_init = s->min_sparse && !s->target_has_backing
1856 ? bdrv_has_zero_init(blk_bs(s->target))
1857 : false;
1858
1859 if (!s->has_zero_init && !s->target_has_backing &&
1860 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1861 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001862 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001863 if (ret == 0) {
1864 s->has_zero_init = true;
1865 }
1866 }
1867
1868 /* Allocate buffer for copied data. For compressed images, only one cluster
1869 * can be copied at a time. */
1870 if (s->compressed) {
1871 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1872 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001873 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001874 }
1875 s->buf_sectors = s->cluster_sectors;
1876 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001877
Kevin Wolf690c7302015-03-19 13:33:32 +01001878 while (sector_num < s->total_sectors) {
1879 n = convert_iteration_sectors(s, sector_num);
1880 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001881 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001882 }
Max Reitzaad15de2016-03-24 23:33:57 +01001883 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1884 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001885 s->allocated_sectors += n;
1886 }
1887 sector_num += n;
1888 }
1889
1890 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001891 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001892 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001893
Peter Lieven2d9187b2017-02-28 13:40:07 +01001894 qemu_co_mutex_init(&s->lock);
1895 for (i = 0; i < s->num_coroutines; i++) {
1896 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1897 s->wait_sector_num[i] = -1;
1898 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001899 }
1900
Anton Nefedovb91127e2017-04-26 11:33:15 +03001901 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001902 main_loop_wait(false);
1903 }
1904
1905 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001906 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001907 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001908 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001909 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001910 }
1911 }
1912
Peter Lieven2d9187b2017-02-28 13:40:07 +01001913 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001914}
1915
bellardea2384d2004-08-01 21:59:26 +00001916static int img_convert(int argc, char **argv)
1917{
Peter Lieven9fd77f92017-04-21 11:11:55 +02001918 int c, bs_i, flags, src_flags = 0;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001919 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02001920 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1921 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001922 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00001923 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001924 BlockDriverState *out_bs;
1925 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08001926 QemuOptsList *create_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001927 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02001928 Error *local_err = NULL;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001929 bool writethrough, src_writethrough, quiet = false, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001930 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001931 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08001932 bool force_share = false;
bellardea2384d2004-08-01 21:59:26 +00001933
Peter Lieven9fd77f92017-04-21 11:11:55 +02001934 ImgConvertState s = (ImgConvertState) {
1935 /* Need at least 4k of zeros for sparse detection */
1936 .min_sparse = 8,
1937 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1938 .wr_in_order = true,
1939 .num_coroutines = 8,
1940 };
1941
bellardea2384d2004-08-01 21:59:26 +00001942 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001943 static const struct option long_options[] = {
1944 {"help", no_argument, 0, 'h'},
1945 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001946 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001947 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001948 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001949 {0, 0, 0, 0}
1950 };
Daniel P. Berrange6b4df542017-07-04 13:30:09 +01001951 c = getopt_long(argc, argv, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001952 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001953 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001954 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001955 }
bellardea2384d2004-08-01 21:59:26 +00001956 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001957 case ':':
1958 missing_argument(argv[optind - 1]);
1959 break;
Jes Sorensenef873942010-12-06 15:25:40 +01001960 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001961 unrecognized_option(argv[optind - 1]);
1962 break;
bellardea2384d2004-08-01 21:59:26 +00001963 case 'h':
1964 help();
1965 break;
1966 case 'f':
1967 fmt = optarg;
1968 break;
1969 case 'O':
1970 out_fmt = optarg;
1971 break;
thsf58c7b32008-06-05 21:53:49 +00001972 case 'B':
1973 out_baseimg = optarg;
1974 break;
bellardea2384d2004-08-01 21:59:26 +00001975 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02001976 s.compressed = true;
bellardea2384d2004-08-01 21:59:26 +00001977 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001978 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001979 if (!is_valid_option_list(optarg)) {
1980 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001981 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001982 }
1983 if (!options) {
1984 options = g_strdup(optarg);
1985 } else {
1986 char *old_options = options;
1987 options = g_strdup_printf("%s,%s", options, optarg);
1988 g_free(old_options);
1989 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001990 break;
edison51ef6722010-09-21 19:58:41 -07001991 case 's':
1992 snapshot_name = optarg;
1993 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001994 case 'l':
1995 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01001996 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
1997 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08001998 if (!sn_opts) {
1999 error_report("Failed in parsing snapshot param '%s'",
2000 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002001 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002002 }
2003 } else {
2004 snapshot_name = optarg;
2005 }
2006 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002007 case 'S':
2008 {
2009 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002010
2011 sval = cvtnum(optarg);
2012 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02002013 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002014 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002015 }
2016
Peter Lieven9fd77f92017-04-21 11:11:55 +02002017 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002018 break;
2019 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002020 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002021 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002022 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002023 case 't':
2024 cache = optarg;
2025 break;
Max Reitz40055952014-07-22 22:58:42 +02002026 case 'T':
2027 src_cache = optarg;
2028 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002029 case 'q':
2030 quiet = true;
2031 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002032 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002033 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002034 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002035 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002036 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2037 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002038 error_report("Invalid number of coroutines. Allowed number of"
2039 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002040 goto fail_getopt;
2041 }
2042 break;
2043 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002044 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002045 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002046 case 'U':
2047 force_share = true;
2048 break;
Max Reitz3258b912017-04-26 15:46:47 +02002049 case OPTION_OBJECT: {
2050 QemuOpts *object_opts;
2051 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2052 optarg, true);
2053 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002054 goto fail_getopt;
2055 }
2056 break;
Max Reitz3258b912017-04-26 15:46:47 +02002057 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002058 case OPTION_IMAGE_OPTS:
2059 image_opts = true;
2060 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002061 case OPTION_TARGET_IMAGE_OPTS:
2062 tgt_image_opts = true;
2063 break;
bellardea2384d2004-08-01 21:59:26 +00002064 }
2065 }
ths3b46e622007-09-17 08:09:54 +00002066
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002067 if (!out_fmt && !tgt_image_opts) {
2068 out_fmt = "raw";
2069 }
2070
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002071 if (qemu_opts_foreach(&qemu_object_opts,
2072 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002073 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002074 goto fail_getopt;
2075 }
2076
Peter Lieven9fd77f92017-04-21 11:11:55 +02002077 if (!s.wr_in_order && s.compressed) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002078 error_report("Out of order write and compress are mutually exclusive");
Peter Lieven9fd77f92017-04-21 11:11:55 +02002079 goto fail_getopt;
2080 }
2081
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002082 if (tgt_image_opts && !skip_create) {
2083 error_report("--target-image-opts requires use of -n flag");
2084 goto fail_getopt;
2085 }
2086
Peter Lieven9fd77f92017-04-21 11:11:55 +02002087 s.src_num = argc - optind - 1;
2088 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2089
2090 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002091 if (out_fmt) {
2092 ret = print_block_option_help(out_filename, out_fmt);
2093 goto fail_getopt;
2094 } else {
2095 error_report("Option help requires a format be specified");
2096 goto fail_getopt;
2097 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002098 }
2099
2100 if (s.src_num < 1) {
2101 error_report("Must specify image file name");
2102 goto fail_getopt;
2103 }
2104
2105
Peter Lieven9fd77f92017-04-21 11:11:55 +02002106 /* ret is still -EINVAL until here */
2107 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2108 if (ret < 0) {
2109 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002110 goto fail_getopt;
2111 }
2112
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002113 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002114 if (quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002115 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002116 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002117 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002118 qemu_progress_print(0, 100);
2119
Peter Lieven9fd77f92017-04-21 11:11:55 +02002120 s.src = g_new0(BlockBackend *, s.src_num);
2121 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002122
Peter Lieven9fd77f92017-04-21 11:11:55 +02002123 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2124 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Fam Zheng335e9932017-05-03 00:35:39 +08002125 fmt, src_flags, src_writethrough, quiet,
2126 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002127 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002128 ret = -1;
2129 goto out;
2130 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002131 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2132 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002133 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002134 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002135 ret = -1;
2136 goto out;
2137 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002138 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002139 }
bellardea2384d2004-08-01 21:59:26 +00002140
Wenchao Xiaef806542013-12-04 17:10:57 +08002141 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002142 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002143 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2144 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2145 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002146 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002147 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002148 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002149 ret = -1;
2150 goto out;
2151 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002152
Peter Lieven9fd77f92017-04-21 11:11:55 +02002153 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2154 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002155 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002156 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002157 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002158 ret = -1;
2159 goto out;
edison51ef6722010-09-21 19:58:41 -07002160 }
2161
Max Reitz2e024cd2015-02-11 09:58:46 -05002162 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002163 /* Find driver and parse its options */
2164 drv = bdrv_find_format(out_fmt);
2165 if (!drv) {
2166 error_report("Unknown file format '%s'", out_fmt);
2167 ret = -1;
2168 goto out;
2169 }
2170
2171 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2172 if (!proto_drv) {
2173 error_report_err(local_err);
2174 ret = -1;
2175 goto out;
2176 }
2177
Max Reitz2e024cd2015-02-11 09:58:46 -05002178 if (!drv->create_opts) {
2179 error_report("Format driver '%s' does not support image creation",
2180 drv->format_name);
2181 ret = -1;
2182 goto out;
2183 }
Max Reitzf75613c2014-12-02 18:32:46 +01002184
Max Reitz2e024cd2015-02-11 09:58:46 -05002185 if (!proto_drv->create_opts) {
2186 error_report("Protocol driver '%s' does not support image creation",
2187 proto_drv->format_name);
2188 ret = -1;
2189 goto out;
2190 }
Max Reitzf75613c2014-12-02 18:32:46 +01002191
Max Reitz2e024cd2015-02-11 09:58:46 -05002192 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2193 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002194
Max Reitz2e024cd2015-02-11 09:58:46 -05002195 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002196 if (options) {
2197 qemu_opts_do_parse(opts, options, NULL, &local_err);
2198 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002199 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002200 ret = -1;
2201 goto out;
2202 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002203 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002204
Peter Lieven9fd77f92017-04-21 11:11:55 +02002205 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002206 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002207 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2208 if (ret < 0) {
2209 goto out;
2210 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002211 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002212
Kevin Wolfa18953f2010-10-14 15:46:04 +02002213 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002214 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002215 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002216 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002217 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002218 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002219
Max Reitz48758a82017-04-26 15:46:48 +02002220 if (s.src_num > 1 && out_baseimg) {
2221 error_report("Having a backing file for the target makes no sense when "
2222 "concatenating multiple input images");
2223 ret = -1;
2224 goto out;
2225 }
2226
Kevin Wolfefa84d42009-05-18 16:42:12 +02002227 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002228 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002229 bool encryption =
2230 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002231 const char *encryptfmt =
2232 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
Chunyan Liu83d05212014-06-05 17:20:51 +08002233 const char *preallocation =
2234 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002235
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002236 if (drv && !drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002237 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002238 ret = -1;
2239 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002240 }
2241
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002242 if (encryption || encryptfmt) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002243 error_report("Compression and encryption not supported at "
2244 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002245 ret = -1;
2246 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002247 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002248
Chunyan Liu83d05212014-06-05 17:20:51 +08002249 if (preallocation
2250 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002251 {
2252 error_report("Compression and preallocation not supported at "
2253 "the same time");
2254 ret = -1;
2255 goto out;
2256 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002257 }
2258
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002259 if (!skip_create) {
2260 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002261 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002262 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002263 error_reportf_err(local_err, "%s: error while converting %s: ",
2264 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002265 goto out;
bellardea2384d2004-08-01 21:59:26 +00002266 }
2267 }
ths3b46e622007-09-17 08:09:54 +00002268
Peter Lieven9fd77f92017-04-21 11:11:55 +02002269 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002270 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002271 if (ret < 0) {
2272 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002273 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002274 }
2275
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002276 if (skip_create) {
2277 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2278 flags, writethrough, quiet, false);
2279 } else {
2280 /* TODO ultimately we should allow --target-image-opts
2281 * to be used even when -n is not given.
2282 * That has to wait for bdrv_create to be improved
2283 * to allow filenames in option syntax
2284 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01002285 s.target = img_open_new_file(out_filename, opts, out_fmt,
2286 flags, writethrough, quiet, false);
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002287 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002288 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002289 ret = -1;
2290 goto out;
2291 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002292 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002293
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002294 if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2295 error_report("Compression not supported for this file format");
2296 ret = -1;
2297 goto out;
2298 }
2299
Eric Blake5def6b82016-06-23 16:37:19 -06002300 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002301 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2302 * as maximum. */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002303 s.buf_sectors = MIN(32768,
2304 MAX(s.buf_sectors,
2305 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2306 out_bs->bl.pdiscard_alignment >>
2307 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002308
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002309 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002310 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002311 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002312 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002313 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002314 ret = -1;
2315 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002316 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002317 error_report("output file is smaller than input file");
2318 ret = -1;
2319 goto out;
2320 }
2321 }
2322
Peter Lieven24f833c2013-11-27 11:07:07 +01002323 ret = bdrv_get_info(out_bs, &bdi);
2324 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002325 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002326 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002327 goto out;
2328 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002329 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002330 s.compressed = s.compressed || bdi.needs_compressed_writes;
2331 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Peter Lieven24f833c2013-11-27 11:07:07 +01002332 }
2333
Peter Lieven9fd77f92017-04-21 11:11:55 +02002334 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002335out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002336 if (!ret) {
2337 qemu_progress_print(100, 0);
2338 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002339 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002340 qemu_opts_del(opts);
2341 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002342 qemu_opts_del(sn_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002343 blk_unref(s.target);
2344 if (s.src) {
2345 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2346 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002347 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002348 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002349 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002350 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002351fail_getopt:
2352 g_free(options);
2353
Peter Lieven9fd77f92017-04-21 11:11:55 +02002354 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002355}
2356
bellard57d1a2b2004-08-03 21:15:11 +00002357
bellardfaea38e2006-08-05 21:31:00 +00002358static void dump_snapshots(BlockDriverState *bs)
2359{
2360 QEMUSnapshotInfo *sn_tab, *sn;
2361 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002362
2363 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2364 if (nb_sns <= 0)
2365 return;
2366 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002367 bdrv_snapshot_dump(fprintf, stdout, NULL);
2368 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002369 for(i = 0; i < nb_sns; i++) {
2370 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002371 bdrv_snapshot_dump(fprintf, stdout, sn);
2372 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002373 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002374 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002375}
2376
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002377static void dump_json_image_info_list(ImageInfoList *list)
2378{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002379 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002380 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002381 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002382
2383 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2384 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002385 str = qobject_to_json_pretty(obj);
2386 assert(str != NULL);
2387 printf("%s\n", qstring_get_str(str));
2388 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002389 visit_free(v);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002390 QDECREF(str);
2391}
2392
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002393static void dump_json_image_info(ImageInfo *info)
2394{
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002395 QString *str;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002396 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002397 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002398
2399 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2400 visit_complete(v, &obj);
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002401 str = qobject_to_json_pretty(obj);
2402 assert(str != NULL);
2403 printf("%s\n", qstring_get_str(str));
2404 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002405 visit_free(v);
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002406 QDECREF(str);
2407}
2408
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002409static void dump_human_image_info_list(ImageInfoList *list)
2410{
2411 ImageInfoList *elem;
2412 bool delim = false;
2413
2414 for (elem = list; elem; elem = elem->next) {
2415 if (delim) {
2416 printf("\n");
2417 }
2418 delim = true;
2419
Wenchao Xia5b917042013-05-25 11:09:45 +08002420 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002421 }
2422}
2423
2424static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2425{
2426 return strcmp(a, b) == 0;
2427}
2428
2429/**
2430 * Open an image file chain and return an ImageInfoList
2431 *
2432 * @filename: topmost image filename
2433 * @fmt: topmost image format (may be NULL to autodetect)
2434 * @chain: true - enumerate entire backing file chain
2435 * false - only topmost image file
2436 *
2437 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2438 * image file. If there was an error a message will have been printed to
2439 * stderr.
2440 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002441static ImageInfoList *collect_image_info_list(bool image_opts,
2442 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002443 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002444 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002445{
2446 ImageInfoList *head = NULL;
2447 ImageInfoList **last = &head;
2448 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002449 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002450
2451 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2452
2453 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002454 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002455 BlockDriverState *bs;
2456 ImageInfo *info;
2457 ImageInfoList *elem;
2458
2459 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2460 error_report("Backing file '%s' creates an infinite loop.",
2461 filename);
2462 goto err;
2463 }
2464 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2465
Max Reitzefaa7c42016-03-16 19:54:38 +01002466 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002467 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2468 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002469 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002470 goto err;
2471 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002472 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002473
Wenchao Xia43526ec2013-06-06 12:27:58 +08002474 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002475 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002476 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002477 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002478 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002479 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002480
2481 elem = g_new0(ImageInfoList, 1);
2482 elem->value = info;
2483 *last = elem;
2484 last = &elem->next;
2485
Markus Armbruster26f54e92014-10-07 13:59:04 +02002486 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002487
2488 filename = fmt = NULL;
2489 if (chain) {
2490 if (info->has_full_backing_filename) {
2491 filename = info->full_backing_filename;
2492 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002493 error_report("Could not determine absolute backing filename,"
2494 " but backing filename '%s' present",
2495 info->backing_filename);
2496 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002497 }
2498 if (info->has_backing_filename_format) {
2499 fmt = info->backing_filename_format;
2500 }
2501 }
2502 }
2503 g_hash_table_destroy(filenames);
2504 return head;
2505
2506err:
2507 qapi_free_ImageInfoList(head);
2508 g_hash_table_destroy(filenames);
2509 return NULL;
2510}
2511
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002512static int img_info(int argc, char **argv)
2513{
2514 int c;
2515 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002516 bool chain = false;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002517 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002518 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002519 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002520 bool force_share = false;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002521
bellardea2384d2004-08-01 21:59:26 +00002522 fmt = NULL;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002523 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002524 for(;;) {
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002525 int option_index = 0;
2526 static const struct option long_options[] = {
2527 {"help", no_argument, 0, 'h'},
2528 {"format", required_argument, 0, 'f'},
2529 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002530 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002531 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002532 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002533 {"force-share", no_argument, 0, 'U'},
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002534 {0, 0, 0, 0}
2535 };
Fam Zheng335e9932017-05-03 00:35:39 +08002536 c = getopt_long(argc, argv, ":f:hU",
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002537 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002538 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002539 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002540 }
bellardea2384d2004-08-01 21:59:26 +00002541 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002542 case ':':
2543 missing_argument(argv[optind - 1]);
2544 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002545 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002546 unrecognized_option(argv[optind - 1]);
2547 break;
bellardea2384d2004-08-01 21:59:26 +00002548 case 'h':
2549 help();
2550 break;
2551 case 'f':
2552 fmt = optarg;
2553 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002554 case 'U':
2555 force_share = true;
2556 break;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002557 case OPTION_OUTPUT:
2558 output = optarg;
2559 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002560 case OPTION_BACKING_CHAIN:
2561 chain = true;
2562 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002563 case OPTION_OBJECT: {
2564 QemuOpts *opts;
2565 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2566 optarg, true);
2567 if (!opts) {
2568 return 1;
2569 }
2570 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002571 case OPTION_IMAGE_OPTS:
2572 image_opts = true;
2573 break;
bellardea2384d2004-08-01 21:59:26 +00002574 }
2575 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002576 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002577 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002578 }
bellardea2384d2004-08-01 21:59:26 +00002579 filename = argv[optind++];
2580
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002581 if (output && !strcmp(output, "json")) {
2582 output_format = OFORMAT_JSON;
2583 } else if (output && !strcmp(output, "human")) {
2584 output_format = OFORMAT_HUMAN;
2585 } else if (output) {
2586 error_report("--output must be used with human or json as argument.");
2587 return 1;
2588 }
2589
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002590 if (qemu_opts_foreach(&qemu_object_opts,
2591 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002592 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002593 return 1;
2594 }
2595
Fam Zheng335e9932017-05-03 00:35:39 +08002596 list = collect_image_info_list(image_opts, filename, fmt, chain,
2597 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002598 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002599 return 1;
2600 }
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002601
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002602 switch (output_format) {
2603 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002604 dump_human_image_info_list(list);
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002605 break;
2606 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002607 if (chain) {
2608 dump_json_image_info_list(list);
2609 } else {
2610 dump_json_image_info(list->value);
2611 }
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002612 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002613 }
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002614
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002615 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002616 return 0;
2617}
2618
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002619static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2620 MapEntry *next)
2621{
2622 switch (output_format) {
2623 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002624 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002625 error_report("File contains external, encrypted or compressed clusters.");
2626 exit(1);
2627 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002628 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002629 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002630 e->start, e->length,
2631 e->has_offset ? e->offset : 0,
2632 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002633 }
2634 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2635 * Modify the flags here to allow more coalescing.
2636 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002637 if (next && (!next->data || next->zero)) {
2638 next->data = false;
2639 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002640 }
2641 break;
2642 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002643 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2644 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002645 (e->start == 0 ? "[" : ",\n"),
2646 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002647 e->zero ? "true" : "false",
2648 e->data ? "true" : "false");
2649 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002650 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002651 }
2652 putchar('}');
2653
2654 if (!next) {
2655 printf("]\n");
2656 }
2657 break;
2658 }
2659}
2660
Eric Blake5e344dd2017-10-11 22:47:02 -05002661static int get_block_status(BlockDriverState *bs, int64_t offset,
2662 int64_t bytes, MapEntry *e)
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002663{
Eric Blake237d78f2017-10-11 22:47:03 -05002664 int ret;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002665 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002666 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002667 bool has_offset;
Eric Blake237d78f2017-10-11 22:47:03 -05002668 int64_t map;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002669
2670 /* As an optimization, we could cache the current range of unallocated
2671 * clusters in each file of the chain, and avoid querying the same
2672 * range repeatedly.
2673 */
2674
2675 depth = 0;
2676 for (;;) {
Eric Blake237d78f2017-10-11 22:47:03 -05002677 ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002678 if (ret < 0) {
2679 return ret;
2680 }
Eric Blake237d78f2017-10-11 22:47:03 -05002681 assert(bytes);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002682 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2683 break;
2684 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002685 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002686 if (bs == NULL) {
2687 ret = 0;
2688 break;
2689 }
2690
2691 depth++;
2692 }
2693
John Snow28756452016-02-05 13:12:33 -05002694 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2695
2696 *e = (MapEntry) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002697 .start = offset,
Eric Blake237d78f2017-10-11 22:47:03 -05002698 .length = bytes,
John Snow28756452016-02-05 13:12:33 -05002699 .data = !!(ret & BDRV_BLOCK_DATA),
2700 .zero = !!(ret & BDRV_BLOCK_ZERO),
Eric Blake237d78f2017-10-11 22:47:03 -05002701 .offset = map,
John Snow28756452016-02-05 13:12:33 -05002702 .has_offset = has_offset,
2703 .depth = depth,
2704 .has_filename = file && has_offset,
2705 .filename = file && has_offset ? file->filename : NULL,
2706 };
2707
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002708 return 0;
2709}
2710
Fam Zheng16b0d552016-01-26 11:59:02 +08002711static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2712{
2713 if (curr->length == 0) {
2714 return false;
2715 }
2716 if (curr->zero != next->zero ||
2717 curr->data != next->data ||
2718 curr->depth != next->depth ||
2719 curr->has_filename != next->has_filename ||
2720 curr->has_offset != next->has_offset) {
2721 return false;
2722 }
2723 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2724 return false;
2725 }
2726 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2727 return false;
2728 }
2729 return true;
2730}
2731
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002732static int img_map(int argc, char **argv)
2733{
2734 int c;
2735 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002736 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002737 BlockDriverState *bs;
2738 const char *filename, *fmt, *output;
2739 int64_t length;
2740 MapEntry curr = { .length = 0 }, next;
2741 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002742 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002743 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002744
2745 fmt = NULL;
2746 output = NULL;
2747 for (;;) {
2748 int option_index = 0;
2749 static const struct option long_options[] = {
2750 {"help", no_argument, 0, 'h'},
2751 {"format", required_argument, 0, 'f'},
2752 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002753 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002754 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002755 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002756 {0, 0, 0, 0}
2757 };
Fam Zheng335e9932017-05-03 00:35:39 +08002758 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002759 long_options, &option_index);
2760 if (c == -1) {
2761 break;
2762 }
2763 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002764 case ':':
2765 missing_argument(argv[optind - 1]);
2766 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002767 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002768 unrecognized_option(argv[optind - 1]);
2769 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002770 case 'h':
2771 help();
2772 break;
2773 case 'f':
2774 fmt = optarg;
2775 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002776 case 'U':
2777 force_share = true;
2778 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002779 case OPTION_OUTPUT:
2780 output = optarg;
2781 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002782 case OPTION_OBJECT: {
2783 QemuOpts *opts;
2784 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2785 optarg, true);
2786 if (!opts) {
2787 return 1;
2788 }
2789 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002790 case OPTION_IMAGE_OPTS:
2791 image_opts = true;
2792 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002793 }
2794 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002795 if (optind != argc - 1) {
2796 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002797 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002798 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002799
2800 if (output && !strcmp(output, "json")) {
2801 output_format = OFORMAT_JSON;
2802 } else if (output && !strcmp(output, "human")) {
2803 output_format = OFORMAT_HUMAN;
2804 } else if (output) {
2805 error_report("--output must be used with human or json as argument.");
2806 return 1;
2807 }
2808
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002809 if (qemu_opts_foreach(&qemu_object_opts,
2810 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002811 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002812 return 1;
2813 }
2814
Fam Zheng335e9932017-05-03 00:35:39 +08002815 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002816 if (!blk) {
2817 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002818 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002819 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002820
2821 if (output_format == OFORMAT_HUMAN) {
2822 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2823 }
2824
Max Reitzf1d3cd72015-02-05 13:58:18 -05002825 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002826 while (curr.start + curr.length < length) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002827 int64_t offset = curr.start + curr.length;
2828 int64_t n;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002829
2830 /* Probe up to 1 GiB at a time. */
Eric Blake5e344dd2017-10-11 22:47:02 -05002831 n = QEMU_ALIGN_DOWN(MIN(1 << 30, length - offset), BDRV_SECTOR_SIZE);
2832 ret = get_block_status(bs, offset, n, &next);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002833
2834 if (ret < 0) {
2835 error_report("Could not read file metadata: %s", strerror(-ret));
2836 goto out;
2837 }
2838
Fam Zheng16b0d552016-01-26 11:59:02 +08002839 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002840 curr.length += next.length;
2841 continue;
2842 }
2843
2844 if (curr.length > 0) {
2845 dump_map_entry(output_format, &curr, &next);
2846 }
2847 curr = next;
2848 }
2849
2850 dump_map_entry(output_format, &curr, NULL);
2851
2852out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002853 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002854 return ret < 0;
2855}
2856
aliguorif7b4a942009-01-07 17:40:15 +00002857#define SNAPSHOT_LIST 1
2858#define SNAPSHOT_CREATE 2
2859#define SNAPSHOT_APPLY 3
2860#define SNAPSHOT_DELETE 4
2861
Stuart Brady153859b2009-06-07 00:42:17 +01002862static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002863{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002864 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002865 BlockDriverState *bs;
2866 QEMUSnapshotInfo sn;
2867 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002868 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002869 int action = 0;
2870 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002871 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002872 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002873 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002874 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00002875
Kevin Wolfce099542016-03-15 13:01:04 +01002876 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002877 /* Parse commandline parameters */
2878 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002879 static const struct option long_options[] = {
2880 {"help", no_argument, 0, 'h'},
2881 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002882 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002883 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002884 {0, 0, 0, 0}
2885 };
Fam Zheng335e9932017-05-03 00:35:39 +08002886 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002887 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002888 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002889 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002890 }
aliguorif7b4a942009-01-07 17:40:15 +00002891 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002892 case ':':
2893 missing_argument(argv[optind - 1]);
2894 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002895 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002896 unrecognized_option(argv[optind - 1]);
2897 break;
aliguorif7b4a942009-01-07 17:40:15 +00002898 case 'h':
2899 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002900 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002901 case 'l':
2902 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002903 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002904 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002905 }
2906 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002907 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002908 break;
2909 case 'a':
2910 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002911 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002912 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002913 }
2914 action = SNAPSHOT_APPLY;
2915 snapshot_name = optarg;
2916 break;
2917 case 'c':
2918 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002919 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002920 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002921 }
2922 action = SNAPSHOT_CREATE;
2923 snapshot_name = optarg;
2924 break;
2925 case 'd':
2926 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002927 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002928 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002929 }
2930 action = SNAPSHOT_DELETE;
2931 snapshot_name = optarg;
2932 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002933 case 'q':
2934 quiet = true;
2935 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002936 case 'U':
2937 force_share = true;
2938 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002939 case OPTION_OBJECT: {
2940 QemuOpts *opts;
2941 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2942 optarg, true);
2943 if (!opts) {
2944 return 1;
2945 }
2946 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002947 case OPTION_IMAGE_OPTS:
2948 image_opts = true;
2949 break;
aliguorif7b4a942009-01-07 17:40:15 +00002950 }
2951 }
2952
Kevin Wolffc11eb22013-08-05 10:53:04 +02002953 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002954 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002955 }
aliguorif7b4a942009-01-07 17:40:15 +00002956 filename = argv[optind++];
2957
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002958 if (qemu_opts_foreach(&qemu_object_opts,
2959 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002960 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002961 return 1;
2962 }
2963
aliguorif7b4a942009-01-07 17:40:15 +00002964 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08002965 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
2966 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002967 if (!blk) {
2968 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002969 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002970 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002971
2972 /* Perform the requested action */
2973 switch(action) {
2974 case SNAPSHOT_LIST:
2975 dump_snapshots(bs);
2976 break;
2977
2978 case SNAPSHOT_CREATE:
2979 memset(&sn, 0, sizeof(sn));
2980 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2981
2982 qemu_gettimeofday(&tv);
2983 sn.date_sec = tv.tv_sec;
2984 sn.date_nsec = tv.tv_usec * 1000;
2985
2986 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002987 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002988 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002989 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002990 }
aliguorif7b4a942009-01-07 17:40:15 +00002991 break;
2992
2993 case SNAPSHOT_APPLY:
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01002994 ret = bdrv_snapshot_goto(bs, snapshot_name, &err);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002995 if (ret) {
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01002996 error_reportf_err(err, "Could not apply snapshot '%s': ",
2997 snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002998 }
aliguorif7b4a942009-01-07 17:40:15 +00002999 break;
3000
3001 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003002 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003003 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003004 error_reportf_err(err, "Could not delete snapshot '%s': ",
3005 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003006 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003007 }
aliguorif7b4a942009-01-07 17:40:15 +00003008 break;
3009 }
3010
3011 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003012 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003013 if (ret) {
3014 return 1;
3015 }
Stuart Brady153859b2009-06-07 00:42:17 +01003016 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003017}
3018
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003019static int img_rebase(int argc, char **argv)
3020{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003021 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003022 uint8_t *buf_old = NULL;
3023 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05003024 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003025 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003026 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3027 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003028 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003029 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003030 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003031 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003032 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003033 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003034 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003035
3036 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003037 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003038 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003039 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003040 out_baseimg = NULL;
3041 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003042 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003043 static const struct option long_options[] = {
3044 {"help", no_argument, 0, 'h'},
3045 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003046 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003047 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003048 {0, 0, 0, 0}
3049 };
Fam Zheng335e9932017-05-03 00:35:39 +08003050 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003051 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003052 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003053 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003054 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003055 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003056 case ':':
3057 missing_argument(argv[optind - 1]);
3058 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003059 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003060 unrecognized_option(argv[optind - 1]);
3061 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003062 case 'h':
3063 help();
3064 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003065 case 'f':
3066 fmt = optarg;
3067 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003068 case 'F':
3069 out_basefmt = optarg;
3070 break;
3071 case 'b':
3072 out_baseimg = optarg;
3073 break;
3074 case 'u':
3075 unsafe = 1;
3076 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003077 case 'p':
3078 progress = 1;
3079 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003080 case 't':
3081 cache = optarg;
3082 break;
Max Reitz40055952014-07-22 22:58:42 +02003083 case 'T':
3084 src_cache = optarg;
3085 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003086 case 'q':
3087 quiet = true;
3088 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003089 case OPTION_OBJECT: {
3090 QemuOpts *opts;
3091 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3092 optarg, true);
3093 if (!opts) {
3094 return 1;
3095 }
3096 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003097 case OPTION_IMAGE_OPTS:
3098 image_opts = true;
3099 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003100 case 'U':
3101 force_share = true;
3102 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003103 }
3104 }
3105
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003106 if (quiet) {
3107 progress = 0;
3108 }
3109
Fam Zhengac1307a2014-04-22 13:36:11 +08003110 if (optind != argc - 1) {
3111 error_exit("Expecting one image file name");
3112 }
3113 if (!unsafe && !out_baseimg) {
3114 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003115 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003116 filename = argv[optind++];
3117
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003118 if (qemu_opts_foreach(&qemu_object_opts,
3119 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003120 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003121 return 1;
3122 }
3123
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003124 qemu_progress_init(progress, 2.0);
3125 qemu_progress_print(0, 100);
3126
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003127 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003128 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003129 if (ret < 0) {
3130 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003131 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003132 }
3133
Kevin Wolfce099542016-03-15 13:01:04 +01003134 src_flags = 0;
3135 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003136 if (ret < 0) {
3137 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003138 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003139 }
3140
Kevin Wolfce099542016-03-15 13:01:04 +01003141 /* The source files are opened read-only, don't care about WCE */
3142 assert((src_flags & BDRV_O_RDWR) == 0);
3143 (void) src_writethrough;
3144
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003145 /*
3146 * Open the images.
3147 *
3148 * Ignore the old backing file for unsafe rebase in case we want to correct
3149 * the reference to a renamed or moved backing file.
3150 */
Fam Zheng335e9932017-05-03 00:35:39 +08003151 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3152 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003153 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003154 ret = -1;
3155 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003156 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003157 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003158
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003159 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003160 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003161 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003162 ret = -1;
3163 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003164 }
3165 }
3166
3167 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003168 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003169 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003170 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003171
Max Reitz644483d2015-02-05 13:58:17 -05003172 if (bs->backing_format[0] != '\0') {
3173 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003174 qdict_put_str(options, "driver", bs->backing_format);
Max Reitz644483d2015-02-05 13:58:17 -05003175 }
3176
Fam Zheng335e9932017-05-03 00:35:39 +08003177 if (force_share) {
3178 if (!options) {
3179 options = qdict_new();
3180 }
Eric Blake579cf1d2017-05-15 14:54:39 -05003181 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +08003182 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003183 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003184 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003185 options, src_flags, &local_err);
3186 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003187 error_reportf_err(local_err,
3188 "Could not open old backing file '%s': ",
3189 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003190 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003191 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003192 }
Max Reitz644483d2015-02-05 13:58:17 -05003193
Alex Bligha6166732012-10-16 13:46:18 +01003194 if (out_baseimg[0]) {
Fam Zheng335e9932017-05-03 00:35:39 +08003195 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003196 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003197 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003198 }
3199 if (force_share) {
3200 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003201 }
3202
Max Reitzefaa7c42016-03-16 19:54:38 +01003203 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003204 options, src_flags, &local_err);
3205 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003206 error_reportf_err(local_err,
3207 "Could not open new backing file '%s': ",
3208 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003209 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003210 goto out;
3211 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003212 }
3213 }
3214
3215 /*
3216 * Check each unallocated cluster in the COW file. If it is unallocated,
3217 * accesses go to the backing file. We must therefore compare this cluster
3218 * in the old and new backing file, and if they differ we need to copy it
3219 * from the old backing file into the COW file.
3220 *
3221 * If qemu-img crashes during this step, no harm is done. The content of
3222 * the image is the same as the original one at any time.
3223 */
3224 if (!unsafe) {
Eric Blake41536282017-10-11 22:47:15 -05003225 int64_t size;
3226 int64_t old_backing_size;
3227 int64_t new_backing_size = 0;
3228 uint64_t offset;
3229 int64_t n;
Kevin Wolf1f710492012-10-12 14:29:18 +02003230 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003231
Max Reitzf1d3cd72015-02-05 13:58:18 -05003232 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3233 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003234
Eric Blake41536282017-10-11 22:47:15 -05003235 size = blk_getlength(blk);
3236 if (size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003237 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003238 filename, strerror(-size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003239 ret = -1;
3240 goto out;
3241 }
Eric Blake41536282017-10-11 22:47:15 -05003242 old_backing_size = blk_getlength(blk_old_backing);
3243 if (old_backing_size < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003244 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003245
3246 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3247 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003248 backing_name, strerror(-old_backing_size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003249 ret = -1;
3250 goto out;
3251 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003252 if (blk_new_backing) {
Eric Blake41536282017-10-11 22:47:15 -05003253 new_backing_size = blk_getlength(blk_new_backing);
3254 if (new_backing_size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003255 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003256 out_baseimg, strerror(-new_backing_size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003257 ret = -1;
3258 goto out;
3259 }
Alex Bligha6166732012-10-16 13:46:18 +01003260 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003261
Eric Blake41536282017-10-11 22:47:15 -05003262 if (size != 0) {
3263 local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE));
Kevin Wolf1f710492012-10-12 14:29:18 +02003264 }
3265
Eric Blake41536282017-10-11 22:47:15 -05003266 for (offset = 0; offset < size; offset += n) {
3267 /* How many bytes can we handle with the next read? */
3268 n = MIN(IO_BUF_SIZE, size - offset);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003269
3270 /* If the cluster is allocated, we don't need to take action */
Eric Blake41536282017-10-11 22:47:15 -05003271 ret = bdrv_is_allocated(bs, offset, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003272 if (ret < 0) {
3273 error_report("error while reading image metadata: %s",
3274 strerror(-ret));
3275 goto out;
3276 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003277 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003278 continue;
3279 }
3280
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003281 /*
3282 * Read old and new backing file and take into consideration that
3283 * backing files may be smaller than the COW image.
3284 */
Eric Blake41536282017-10-11 22:47:15 -05003285 if (offset >= old_backing_size) {
3286 memset(buf_old, 0, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003287 } else {
Eric Blake41536282017-10-11 22:47:15 -05003288 if (offset + n > old_backing_size) {
3289 n = old_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003290 }
3291
Eric Blake41536282017-10-11 22:47:15 -05003292 ret = blk_pread(blk_old_backing, offset, buf_old, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003293 if (ret < 0) {
3294 error_report("error while reading from old backing file");
3295 goto out;
3296 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003297 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003298
Eric Blake41536282017-10-11 22:47:15 -05003299 if (offset >= new_backing_size || !blk_new_backing) {
3300 memset(buf_new, 0, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003301 } else {
Eric Blake41536282017-10-11 22:47:15 -05003302 if (offset + n > new_backing_size) {
3303 n = new_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003304 }
3305
Eric Blake41536282017-10-11 22:47:15 -05003306 ret = blk_pread(blk_new_backing, offset, buf_new, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003307 if (ret < 0) {
3308 error_report("error while reading from new backing file");
3309 goto out;
3310 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003311 }
3312
3313 /* If they differ, we need to write to the COW file */
3314 uint64_t written = 0;
3315
Eric Blake41536282017-10-11 22:47:15 -05003316 while (written < n) {
Eric Blakedc61cd32017-10-11 22:47:14 -05003317 int64_t pnum;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003318
Eric Blake41536282017-10-11 22:47:15 -05003319 if (compare_buffers(buf_old + written, buf_new + written,
3320 n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003321 {
Eric Blake41536282017-10-11 22:47:15 -05003322 ret = blk_pwrite(blk, offset + written,
Eric Blakedc61cd32017-10-11 22:47:14 -05003323 buf_old + written, pnum, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003324 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003325 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003326 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003327 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003328 }
3329 }
3330
3331 written += pnum;
3332 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003333 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003334 }
3335 }
3336
3337 /*
3338 * Change the backing file. All clusters that are different from the old
3339 * backing file are overwritten in the COW file now, so the visible content
3340 * doesn't change when we switch the backing file.
3341 */
Alex Bligha6166732012-10-16 13:46:18 +01003342 if (out_baseimg && *out_baseimg) {
3343 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3344 } else {
3345 ret = bdrv_change_backing_file(bs, NULL, NULL);
3346 }
3347
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003348 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003349 error_report("Could not change the backing file to '%s': No "
3350 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003351 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003352 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003353 out_baseimg, strerror(-ret));
3354 }
3355
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003356 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003357 /*
3358 * TODO At this point it is possible to check if any clusters that are
3359 * allocated in the COW file are the same in the backing file. If so, they
3360 * could be dropped from the COW file. Don't do this before switching the
3361 * backing file, in case of a crash this would lead to corruption.
3362 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003363out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003364 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003365 /* Cleanup */
3366 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003367 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003368 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003369 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003370 qemu_vfree(buf_old);
3371 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003372
Markus Armbruster26f54e92014-10-07 13:59:04 +02003373 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003374 if (ret) {
3375 return 1;
3376 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003377 return 0;
3378}
3379
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003380static int img_resize(int argc, char **argv)
3381{
Markus Armbruster6750e792015-02-12 17:43:08 +01003382 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003383 int c, ret, relative;
3384 const char *filename, *fmt, *size;
Max Reitzdc5f6902017-06-13 22:20:55 +02003385 int64_t n, total_size, current_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003386 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003387 BlockBackend *blk = NULL;
Max Reitzdc5f6902017-06-13 22:20:55 +02003388 PreallocMode prealloc = PREALLOC_MODE_OFF;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003389 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003390
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003391 static QemuOptsList resize_options = {
3392 .name = "resize_options",
3393 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3394 .desc = {
3395 {
3396 .name = BLOCK_OPT_SIZE,
3397 .type = QEMU_OPT_SIZE,
3398 .help = "Virtual disk size"
3399 }, {
3400 /* end of list */
3401 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003402 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003403 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003404 bool image_opts = false;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003405 bool shrink = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003406
Kevin Wolfe80fec72011-04-29 10:58:12 +02003407 /* Remove size from argv manually so that negative numbers are not treated
3408 * as options by getopt. */
3409 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003410 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003411 return 1;
3412 }
3413
3414 size = argv[--argc];
3415
3416 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003417 fmt = NULL;
3418 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003419 static const struct option long_options[] = {
3420 {"help", no_argument, 0, 'h'},
3421 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003422 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Max Reitzdc5f6902017-06-13 22:20:55 +02003423 {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003424 {"shrink", no_argument, 0, OPTION_SHRINK},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003425 {0, 0, 0, 0}
3426 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003427 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003428 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003429 if (c == -1) {
3430 break;
3431 }
3432 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003433 case ':':
3434 missing_argument(argv[optind - 1]);
3435 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003436 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003437 unrecognized_option(argv[optind - 1]);
3438 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003439 case 'h':
3440 help();
3441 break;
3442 case 'f':
3443 fmt = optarg;
3444 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003445 case 'q':
3446 quiet = true;
3447 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003448 case OPTION_OBJECT: {
3449 QemuOpts *opts;
3450 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3451 optarg, true);
3452 if (!opts) {
3453 return 1;
3454 }
3455 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003456 case OPTION_IMAGE_OPTS:
3457 image_opts = true;
3458 break;
Max Reitzdc5f6902017-06-13 22:20:55 +02003459 case OPTION_PREALLOCATION:
Marc-AndrƩ Lureauf7abe0e2017-08-24 10:46:10 +02003460 prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
Markus Armbruster06c60b62017-08-24 10:45:57 +02003461 PREALLOC_MODE__MAX, NULL);
Max Reitzdc5f6902017-06-13 22:20:55 +02003462 if (prealloc == PREALLOC_MODE__MAX) {
3463 error_report("Invalid preallocation mode '%s'", optarg);
3464 return 1;
3465 }
3466 break;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003467 case OPTION_SHRINK:
3468 shrink = true;
3469 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003470 }
3471 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003472 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003473 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003474 }
3475 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003476
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003477 if (qemu_opts_foreach(&qemu_object_opts,
3478 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003479 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003480 return 1;
3481 }
3482
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003483 /* Choose grow, shrink, or absolute resize mode */
3484 switch (size[0]) {
3485 case '+':
3486 relative = 1;
3487 size++;
3488 break;
3489 case '-':
3490 relative = -1;
3491 size++;
3492 break;
3493 default:
3494 relative = 0;
3495 break;
3496 }
3497
3498 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003499 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003500 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003501 if (err) {
3502 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003503 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003504 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003505 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003506 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003507 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3508 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003509
Max Reitzefaa7c42016-03-16 19:54:38 +01003510 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003511 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3512 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003513 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003514 ret = -1;
3515 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003516 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003517
Max Reitzdc5f6902017-06-13 22:20:55 +02003518 current_size = blk_getlength(blk);
3519 if (current_size < 0) {
3520 error_report("Failed to inquire current image length: %s",
3521 strerror(-current_size));
3522 ret = -1;
3523 goto out;
3524 }
3525
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003526 if (relative) {
Max Reitzdc5f6902017-06-13 22:20:55 +02003527 total_size = current_size + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003528 } else {
3529 total_size = n;
3530 }
3531 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003532 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003533 ret = -1;
3534 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003535 }
3536
Max Reitzdc5f6902017-06-13 22:20:55 +02003537 if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
3538 error_report("Preallocation can only be used for growing images");
3539 ret = -1;
3540 goto out;
3541 }
3542
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003543 if (total_size < current_size && !shrink) {
3544 warn_report("Shrinking an image will delete all data beyond the "
3545 "shrunken image's end. Before performing such an "
3546 "operation, make sure there is no important data there.");
3547
3548 if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) {
3549 error_report(
3550 "Use the --shrink option to perform a shrink operation.");
3551 ret = -1;
3552 goto out;
3553 } else {
3554 warn_report("Using the --shrink option will suppress this message. "
3555 "Note that future versions of qemu-img may refuse to "
3556 "shrink images without this option.");
3557 }
3558 }
3559
Max Reitzdc5f6902017-06-13 22:20:55 +02003560 ret = blk_truncate(blk, total_size, prealloc, &err);
Max Reitzed3d2ec2017-03-28 22:51:27 +02003561 if (!ret) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003562 qprintf(quiet, "Image resized.\n");
Max Reitzed3d2ec2017-03-28 22:51:27 +02003563 } else {
3564 error_report_err(err);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003565 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003566out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003567 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003568 if (ret) {
3569 return 1;
3570 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003571 return 0;
3572}
3573
Max Reitz76a3a342014-10-27 11:12:51 +01003574static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003575 int64_t offset, int64_t total_work_size,
3576 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003577{
3578 qemu_progress_print(100.f * offset / total_work_size, 0);
3579}
3580
Max Reitz6f176b42013-09-03 10:09:50 +02003581static int img_amend(int argc, char **argv)
3582{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003583 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003584 int c, ret = 0;
3585 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003586 QemuOptsList *create_opts = NULL;
3587 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003588 const char *fmt = NULL, *filename, *cache;
3589 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003590 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003591 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003592 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003593 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003594 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003595
Max Reitzbd39e6e2014-07-22 22:58:43 +02003596 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003597 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003598 static const struct option long_options[] = {
3599 {"help", no_argument, 0, 'h'},
3600 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003601 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003602 {0, 0, 0, 0}
3603 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003604 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003605 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003606 if (c == -1) {
3607 break;
3608 }
3609
3610 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003611 case ':':
3612 missing_argument(argv[optind - 1]);
3613 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003614 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003615 unrecognized_option(argv[optind - 1]);
3616 break;
3617 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003618 help();
3619 break;
3620 case 'o':
3621 if (!is_valid_option_list(optarg)) {
3622 error_report("Invalid option list: %s", optarg);
3623 ret = -1;
3624 goto out_no_progress;
3625 }
3626 if (!options) {
3627 options = g_strdup(optarg);
3628 } else {
3629 char *old_options = options;
3630 options = g_strdup_printf("%s,%s", options, optarg);
3631 g_free(old_options);
3632 }
3633 break;
3634 case 'f':
3635 fmt = optarg;
3636 break;
3637 case 't':
3638 cache = optarg;
3639 break;
3640 case 'p':
3641 progress = true;
3642 break;
3643 case 'q':
3644 quiet = true;
3645 break;
3646 case OPTION_OBJECT:
3647 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3648 optarg, true);
3649 if (!opts) {
3650 ret = -1;
3651 goto out_no_progress;
3652 }
3653 break;
3654 case OPTION_IMAGE_OPTS:
3655 image_opts = true;
3656 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003657 }
3658 }
3659
Max Reitz6f176b42013-09-03 10:09:50 +02003660 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003661 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003662 }
3663
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003664 if (qemu_opts_foreach(&qemu_object_opts,
3665 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003666 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003667 ret = -1;
3668 goto out_no_progress;
3669 }
3670
Max Reitz76a3a342014-10-27 11:12:51 +01003671 if (quiet) {
3672 progress = false;
3673 }
3674 qemu_progress_init(progress, 1.0);
3675
Kevin Wolfa283cb62014-02-21 16:24:07 +01003676 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3677 if (fmt && has_help_option(options)) {
3678 /* If a format is explicitly specified (and possibly no filename is
3679 * given), print option help here */
3680 ret = print_block_option_help(filename, fmt);
3681 goto out;
3682 }
3683
3684 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003685 error_report("Expecting one image file name");
3686 ret = -1;
3687 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003688 }
Max Reitz6f176b42013-09-03 10:09:50 +02003689
Kevin Wolfce099542016-03-15 13:01:04 +01003690 flags = BDRV_O_RDWR;
3691 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003692 if (ret < 0) {
3693 error_report("Invalid cache option: %s", cache);
3694 goto out;
3695 }
3696
Fam Zheng335e9932017-05-03 00:35:39 +08003697 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3698 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003699 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003700 ret = -1;
3701 goto out;
3702 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003703 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003704
3705 fmt = bs->drv->format_name;
3706
Kevin Wolf626f84f2014-02-21 16:24:06 +01003707 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003708 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003709 ret = print_block_option_help(filename, fmt);
3710 goto out;
3711 }
3712
Max Reitzb2439d22014-12-02 18:32:47 +01003713 if (!bs->drv->create_opts) {
3714 error_report("Format driver '%s' does not support any options to amend",
3715 fmt);
3716 ret = -1;
3717 goto out;
3718 }
3719
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003720 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003721 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003722 qemu_opts_do_parse(opts, options, NULL, &err);
3723 if (err) {
3724 error_report_err(err);
3725 ret = -1;
3726 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003727 }
3728
Max Reitz76a3a342014-10-27 11:12:51 +01003729 /* In case the driver does not call amend_status_cb() */
3730 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003731 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003732 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003733 if (ret < 0) {
3734 error_report("Error while amending options: %s", strerror(-ret));
3735 goto out;
3736 }
3737
3738out:
Max Reitz76a3a342014-10-27 11:12:51 +01003739 qemu_progress_end();
3740
Max Reitze814dff2015-08-20 16:00:38 -07003741out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003742 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003743 qemu_opts_del(opts);
3744 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003745 g_free(options);
3746
Max Reitz6f176b42013-09-03 10:09:50 +02003747 if (ret) {
3748 return 1;
3749 }
3750 return 0;
3751}
3752
Kevin Wolfb6133b82014-08-05 14:17:13 +02003753typedef struct BenchData {
3754 BlockBackend *blk;
3755 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003756 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003757 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003758 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003759 int nrreq;
3760 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003761 int flush_interval;
3762 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003763 uint8_t *buf;
3764 QEMUIOVector *qiov;
3765
3766 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003767 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003768 uint64_t offset;
3769} BenchData;
3770
Kevin Wolf55d539c2016-06-03 13:59:41 +02003771static void bench_undrained_flush_cb(void *opaque, int ret)
3772{
3773 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003774 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003775 exit(EXIT_FAILURE);
3776 }
3777}
3778
Kevin Wolfb6133b82014-08-05 14:17:13 +02003779static void bench_cb(void *opaque, int ret)
3780{
3781 BenchData *b = opaque;
3782 BlockAIOCB *acb;
3783
3784 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003785 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003786 exit(EXIT_FAILURE);
3787 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003788
3789 if (b->in_flush) {
3790 /* Just finished a flush with drained queue: Start next requests */
3791 assert(b->in_flight == 0);
3792 b->in_flush = false;
3793 } else if (b->in_flight > 0) {
3794 int remaining = b->n - b->in_flight;
3795
Kevin Wolfb6133b82014-08-05 14:17:13 +02003796 b->n--;
3797 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003798
3799 /* Time for flush? Drain queue if requested, then flush */
3800 if (b->flush_interval && remaining % b->flush_interval == 0) {
3801 if (!b->in_flight || !b->drain_on_flush) {
3802 BlockCompletionFunc *cb;
3803
3804 if (b->drain_on_flush) {
3805 b->in_flush = true;
3806 cb = bench_cb;
3807 } else {
3808 cb = bench_undrained_flush_cb;
3809 }
3810
3811 acb = blk_aio_flush(b->blk, cb, b);
3812 if (!acb) {
3813 error_report("Failed to issue flush request");
3814 exit(EXIT_FAILURE);
3815 }
3816 }
3817 if (b->drain_on_flush) {
3818 return;
3819 }
3820 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003821 }
3822
3823 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003824 int64_t offset = b->offset;
3825 /* blk_aio_* might look for completed I/Os and kick bench_cb
3826 * again, so make sure this operation is counted by in_flight
3827 * and b->offset is ready for the next submission.
3828 */
3829 b->in_flight++;
3830 b->offset += b->step;
3831 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003832 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003833 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003834 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003835 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003836 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003837 if (!acb) {
3838 error_report("Failed to issue request");
3839 exit(EXIT_FAILURE);
3840 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003841 }
3842}
3843
3844static int img_bench(int argc, char **argv)
3845{
3846 int c, ret = 0;
3847 const char *fmt = NULL, *filename;
3848 bool quiet = false;
3849 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003850 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003851 int count = 75000;
3852 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003853 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003854 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003855 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003856 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003857 int flush_interval = 0;
3858 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003859 int64_t image_size;
3860 BlockBackend *blk = NULL;
3861 BenchData data = {};
3862 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003863 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003864 struct timeval t1, t2;
3865 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08003866 bool force_share = false;
Fam Zheng79d46582018-01-16 14:08:58 +08003867 size_t buf_size;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003868
3869 for (;;) {
3870 static const struct option long_options[] = {
3871 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003872 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003873 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003874 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003875 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08003876 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003877 {0, 0, 0, 0}
3878 };
Fam Zheng335e9932017-05-03 00:35:39 +08003879 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003880 if (c == -1) {
3881 break;
3882 }
3883
3884 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003885 case ':':
3886 missing_argument(argv[optind - 1]);
3887 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003888 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003889 unrecognized_option(argv[optind - 1]);
3890 break;
3891 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02003892 help();
3893 break;
3894 case 'c':
3895 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003896 unsigned long res;
3897
3898 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003899 error_report("Invalid request count specified");
3900 return 1;
3901 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003902 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003903 break;
3904 }
3905 case 'd':
3906 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003907 unsigned long res;
3908
3909 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003910 error_report("Invalid queue depth specified");
3911 return 1;
3912 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003913 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003914 break;
3915 }
3916 case 'f':
3917 fmt = optarg;
3918 break;
3919 case 'n':
3920 flags |= BDRV_O_NATIVE_AIO;
3921 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003922 case 'o':
3923 {
Markus Armbruster606caa02017-02-21 21:14:04 +01003924 offset = cvtnum(optarg);
3925 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02003926 error_report("Invalid offset specified");
3927 return 1;
3928 }
3929 break;
3930 }
3931 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003932 case 'q':
3933 quiet = true;
3934 break;
3935 case 's':
3936 {
3937 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003938
Markus Armbruster606caa02017-02-21 21:14:04 +01003939 sval = cvtnum(optarg);
3940 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003941 error_report("Invalid buffer size specified");
3942 return 1;
3943 }
3944
3945 bufsize = sval;
3946 break;
3947 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003948 case 'S':
3949 {
3950 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003951
Markus Armbruster606caa02017-02-21 21:14:04 +01003952 sval = cvtnum(optarg);
3953 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02003954 error_report("Invalid step size specified");
3955 return 1;
3956 }
3957
3958 step = sval;
3959 break;
3960 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003961 case 't':
3962 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3963 if (ret < 0) {
3964 error_report("Invalid cache mode");
3965 ret = -1;
3966 goto out;
3967 }
3968 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003969 case 'w':
3970 flags |= BDRV_O_RDWR;
3971 is_write = true;
3972 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003973 case 'U':
3974 force_share = true;
3975 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003976 case OPTION_PATTERN:
3977 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003978 unsigned long res;
3979
3980 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003981 error_report("Invalid pattern byte specified");
3982 return 1;
3983 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003984 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003985 break;
3986 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003987 case OPTION_FLUSH_INTERVAL:
3988 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003989 unsigned long res;
3990
3991 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02003992 error_report("Invalid flush interval specified");
3993 return 1;
3994 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003995 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003996 break;
3997 }
3998 case OPTION_NO_DRAIN:
3999 drain_on_flush = false;
4000 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004001 case OPTION_IMAGE_OPTS:
4002 image_opts = true;
4003 break;
4004 }
4005 }
4006
4007 if (optind != argc - 1) {
4008 error_exit("Expecting one image file name");
4009 }
4010 filename = argv[argc - 1];
4011
Kevin Wolf55d539c2016-06-03 13:59:41 +02004012 if (!is_write && flush_interval) {
4013 error_report("--flush-interval is only available in write tests");
4014 ret = -1;
4015 goto out;
4016 }
4017 if (flush_interval && flush_interval < depth) {
4018 error_report("Flush interval can't be smaller than depth");
4019 ret = -1;
4020 goto out;
4021 }
4022
Fam Zheng335e9932017-05-03 00:35:39 +08004023 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4024 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004025 if (!blk) {
4026 ret = -1;
4027 goto out;
4028 }
4029
4030 image_size = blk_getlength(blk);
4031 if (image_size < 0) {
4032 ret = image_size;
4033 goto out;
4034 }
4035
4036 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004037 .blk = blk,
4038 .image_size = image_size,
4039 .bufsize = bufsize,
4040 .step = step ?: bufsize,
4041 .nrreq = depth,
4042 .n = count,
4043 .offset = offset,
4044 .write = is_write,
4045 .flush_interval = flush_interval,
4046 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004047 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004048 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004049 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004050 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004051 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004052 if (flush_interval) {
4053 printf("Sending flush every %d requests\n", flush_interval);
4054 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004055
Fam Zheng79d46582018-01-16 14:08:58 +08004056 buf_size = data.nrreq * data.bufsize;
4057 data.buf = blk_blockalign(blk, buf_size);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004058 memset(data.buf, pattern, data.nrreq * data.bufsize);
4059
Fam Zheng79d46582018-01-16 14:08:58 +08004060 blk_register_buf(blk, data.buf, buf_size);
4061
Kevin Wolfb6133b82014-08-05 14:17:13 +02004062 data.qiov = g_new(QEMUIOVector, data.nrreq);
4063 for (i = 0; i < data.nrreq; i++) {
4064 qemu_iovec_init(&data.qiov[i], 1);
4065 qemu_iovec_add(&data.qiov[i],
4066 data.buf + i * data.bufsize, data.bufsize);
4067 }
4068
4069 gettimeofday(&t1, NULL);
4070 bench_cb(&data, 0);
4071
4072 while (data.n > 0) {
4073 main_loop_wait(false);
4074 }
4075 gettimeofday(&t2, NULL);
4076
4077 printf("Run completed in %3.3f seconds.\n",
4078 (t2.tv_sec - t1.tv_sec)
4079 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4080
4081out:
Fam Zheng79d46582018-01-16 14:08:58 +08004082 if (data.buf) {
4083 blk_unregister_buf(blk, data.buf);
4084 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004085 qemu_vfree(data.buf);
4086 blk_unref(blk);
4087
4088 if (ret) {
4089 return 1;
4090 }
4091 return 0;
4092}
4093
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004094#define C_BS 01
4095#define C_COUNT 02
4096#define C_IF 04
4097#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004098#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004099
4100struct DdInfo {
4101 unsigned int flags;
4102 int64_t count;
4103};
4104
4105struct DdIo {
4106 int bsz; /* Block size */
4107 char *filename;
4108 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004109 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004110};
4111
4112struct DdOpts {
4113 const char *name;
4114 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4115 unsigned int flag;
4116};
4117
4118static int img_dd_bs(const char *arg,
4119 struct DdIo *in, struct DdIo *out,
4120 struct DdInfo *dd)
4121{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004122 int64_t res;
4123
Markus Armbruster606caa02017-02-21 21:14:04 +01004124 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004125
Markus Armbruster606caa02017-02-21 21:14:04 +01004126 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004127 error_report("invalid number: '%s'", arg);
4128 return 1;
4129 }
4130 in->bsz = out->bsz = res;
4131
4132 return 0;
4133}
4134
4135static int img_dd_count(const char *arg,
4136 struct DdIo *in, struct DdIo *out,
4137 struct DdInfo *dd)
4138{
Markus Armbruster606caa02017-02-21 21:14:04 +01004139 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004140
Markus Armbruster606caa02017-02-21 21:14:04 +01004141 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004142 error_report("invalid number: '%s'", arg);
4143 return 1;
4144 }
4145
4146 return 0;
4147}
4148
4149static int img_dd_if(const char *arg,
4150 struct DdIo *in, struct DdIo *out,
4151 struct DdInfo *dd)
4152{
4153 in->filename = g_strdup(arg);
4154
4155 return 0;
4156}
4157
4158static int img_dd_of(const char *arg,
4159 struct DdIo *in, struct DdIo *out,
4160 struct DdInfo *dd)
4161{
4162 out->filename = g_strdup(arg);
4163
4164 return 0;
4165}
4166
Reda Sallahif7c15532016-08-10 16:16:09 +02004167static int img_dd_skip(const char *arg,
4168 struct DdIo *in, struct DdIo *out,
4169 struct DdInfo *dd)
4170{
Markus Armbruster606caa02017-02-21 21:14:04 +01004171 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004172
Markus Armbruster606caa02017-02-21 21:14:04 +01004173 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004174 error_report("invalid number: '%s'", arg);
4175 return 1;
4176 }
4177
4178 return 0;
4179}
4180
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004181static int img_dd(int argc, char **argv)
4182{
4183 int ret = 0;
4184 char *arg = NULL;
4185 char *tmp;
4186 BlockDriver *drv = NULL, *proto_drv = NULL;
4187 BlockBackend *blk1 = NULL, *blk2 = NULL;
4188 QemuOpts *opts = NULL;
4189 QemuOptsList *create_opts = NULL;
4190 Error *local_err = NULL;
4191 bool image_opts = false;
4192 int c, i;
4193 const char *out_fmt = "raw";
4194 const char *fmt = NULL;
4195 int64_t size = 0;
4196 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004197 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004198 struct DdInfo dd = {
4199 .flags = 0,
4200 .count = 0,
4201 };
4202 struct DdIo in = {
4203 .bsz = 512, /* Block size is by default 512 bytes */
4204 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004205 .buf = NULL,
4206 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004207 };
4208 struct DdIo out = {
4209 .bsz = 512,
4210 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004211 .buf = NULL,
4212 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004213 };
4214
4215 const struct DdOpts options[] = {
4216 { "bs", img_dd_bs, C_BS },
4217 { "count", img_dd_count, C_COUNT },
4218 { "if", img_dd_if, C_IF },
4219 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004220 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004221 { NULL, NULL, 0 }
4222 };
4223 const struct option long_options[] = {
4224 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004225 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004226 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004227 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004228 { 0, 0, 0, 0 }
4229 };
4230
Fam Zheng335e9932017-05-03 00:35:39 +08004231 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004232 if (c == EOF) {
4233 break;
4234 }
4235 switch (c) {
4236 case 'O':
4237 out_fmt = optarg;
4238 break;
4239 case 'f':
4240 fmt = optarg;
4241 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004242 case ':':
4243 missing_argument(argv[optind - 1]);
4244 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004245 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004246 unrecognized_option(argv[optind - 1]);
4247 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004248 case 'h':
4249 help();
4250 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004251 case 'U':
4252 force_share = true;
4253 break;
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004254 case OPTION_OBJECT:
4255 if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004256 ret = -1;
4257 goto out;
4258 }
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004259 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004260 case OPTION_IMAGE_OPTS:
4261 image_opts = true;
4262 break;
4263 }
4264 }
4265
4266 for (i = optind; i < argc; i++) {
4267 int j;
4268 arg = g_strdup(argv[i]);
4269
4270 tmp = strchr(arg, '=');
4271 if (tmp == NULL) {
4272 error_report("unrecognized operand %s", arg);
4273 ret = -1;
4274 goto out;
4275 }
4276
4277 *tmp++ = '\0';
4278
4279 for (j = 0; options[j].name != NULL; j++) {
4280 if (!strcmp(arg, options[j].name)) {
4281 break;
4282 }
4283 }
4284 if (options[j].name == NULL) {
4285 error_report("unrecognized operand %s", arg);
4286 ret = -1;
4287 goto out;
4288 }
4289
4290 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4291 ret = -1;
4292 goto out;
4293 }
4294 dd.flags |= options[j].flag;
4295 g_free(arg);
4296 arg = NULL;
4297 }
4298
4299 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4300 error_report("Must specify both input and output files");
4301 ret = -1;
4302 goto out;
4303 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004304
4305 if (qemu_opts_foreach(&qemu_object_opts,
4306 user_creatable_add_opts_foreach,
4307 NULL, NULL)) {
4308 ret = -1;
4309 goto out;
4310 }
4311
Fam Zheng335e9932017-05-03 00:35:39 +08004312 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4313 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004314
4315 if (!blk1) {
4316 ret = -1;
4317 goto out;
4318 }
4319
4320 drv = bdrv_find_format(out_fmt);
4321 if (!drv) {
4322 error_report("Unknown file format");
4323 ret = -1;
4324 goto out;
4325 }
4326 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4327
4328 if (!proto_drv) {
4329 error_report_err(local_err);
4330 ret = -1;
4331 goto out;
4332 }
4333 if (!drv->create_opts) {
4334 error_report("Format driver '%s' does not support image creation",
4335 drv->format_name);
4336 ret = -1;
4337 goto out;
4338 }
4339 if (!proto_drv->create_opts) {
4340 error_report("Protocol driver '%s' does not support image creation",
4341 proto_drv->format_name);
4342 ret = -1;
4343 goto out;
4344 }
4345 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4346 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4347
4348 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4349
4350 size = blk_getlength(blk1);
4351 if (size < 0) {
4352 error_report("Failed to get size for '%s'", in.filename);
4353 ret = -1;
4354 goto out;
4355 }
4356
4357 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4358 dd.count * in.bsz < size) {
4359 size = dd.count * in.bsz;
4360 }
4361
Reda Sallahif7c15532016-08-10 16:16:09 +02004362 /* Overflow means the specified offset is beyond input image's size */
4363 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4364 size < in.bsz * in.offset)) {
4365 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4366 } else {
4367 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4368 size - in.bsz * in.offset, &error_abort);
4369 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004370
4371 ret = bdrv_create(drv, out.filename, opts, &local_err);
4372 if (ret < 0) {
4373 error_reportf_err(local_err,
4374 "%s: error while creating output image: ",
4375 out.filename);
4376 ret = -1;
4377 goto out;
4378 }
4379
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004380 /* TODO, we can't honour --image-opts for the target,
4381 * since it needs to be given in a format compatible
4382 * with the bdrv_create() call above which does not
4383 * support image-opts style.
4384 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01004385 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004386 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004387
4388 if (!blk2) {
4389 ret = -1;
4390 goto out;
4391 }
4392
Reda Sallahif7c15532016-08-10 16:16:09 +02004393 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4394 size < in.offset * in.bsz)) {
4395 /* We give a warning if the skip option is bigger than the input
4396 * size and create an empty output disk image (i.e. like dd(1)).
4397 */
4398 error_report("%s: cannot skip to specified offset", in.filename);
4399 in_pos = size;
4400 } else {
4401 in_pos = in.offset * in.bsz;
4402 }
4403
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004404 in.buf = g_new(uint8_t, in.bsz);
4405
Reda Sallahif7c15532016-08-10 16:16:09 +02004406 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004407 int in_ret, out_ret;
4408
4409 if (in_pos + in.bsz > size) {
4410 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4411 } else {
4412 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4413 }
4414 if (in_ret < 0) {
4415 error_report("error while reading from input image file: %s",
4416 strerror(-in_ret));
4417 ret = -1;
4418 goto out;
4419 }
4420 in_pos += in_ret;
4421
4422 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4423
4424 if (out_ret < 0) {
4425 error_report("error while writing to output image file: %s",
4426 strerror(-out_ret));
4427 ret = -1;
4428 goto out;
4429 }
4430 out_pos += out_ret;
4431 }
4432
4433out:
4434 g_free(arg);
4435 qemu_opts_del(opts);
4436 qemu_opts_free(create_opts);
4437 blk_unref(blk1);
4438 blk_unref(blk2);
4439 g_free(in.filename);
4440 g_free(out.filename);
4441 g_free(in.buf);
4442 g_free(out.buf);
4443
4444 if (ret) {
4445 return 1;
4446 }
4447 return 0;
4448}
4449
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004450static void dump_json_block_measure_info(BlockMeasureInfo *info)
4451{
4452 QString *str;
4453 QObject *obj;
4454 Visitor *v = qobject_output_visitor_new(&obj);
4455
4456 visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
4457 visit_complete(v, &obj);
4458 str = qobject_to_json_pretty(obj);
4459 assert(str != NULL);
4460 printf("%s\n", qstring_get_str(str));
4461 qobject_decref(obj);
4462 visit_free(v);
4463 QDECREF(str);
4464}
4465
4466static int img_measure(int argc, char **argv)
4467{
4468 static const struct option long_options[] = {
4469 {"help", no_argument, 0, 'h'},
4470 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4471 {"object", required_argument, 0, OPTION_OBJECT},
4472 {"output", required_argument, 0, OPTION_OUTPUT},
4473 {"size", required_argument, 0, OPTION_SIZE},
4474 {"force-share", no_argument, 0, 'U'},
4475 {0, 0, 0, 0}
4476 };
4477 OutputFormat output_format = OFORMAT_HUMAN;
4478 BlockBackend *in_blk = NULL;
4479 BlockDriver *drv;
4480 const char *filename = NULL;
4481 const char *fmt = NULL;
4482 const char *out_fmt = "raw";
4483 char *options = NULL;
4484 char *snapshot_name = NULL;
4485 bool force_share = false;
4486 QemuOpts *opts = NULL;
4487 QemuOpts *object_opts = NULL;
4488 QemuOpts *sn_opts = NULL;
4489 QemuOptsList *create_opts = NULL;
4490 bool image_opts = false;
4491 uint64_t img_size = UINT64_MAX;
4492 BlockMeasureInfo *info = NULL;
4493 Error *local_err = NULL;
4494 int ret = 1;
4495 int c;
4496
4497 while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
4498 long_options, NULL)) != -1) {
4499 switch (c) {
4500 case '?':
4501 case 'h':
4502 help();
4503 break;
4504 case 'f':
4505 fmt = optarg;
4506 break;
4507 case 'O':
4508 out_fmt = optarg;
4509 break;
4510 case 'o':
4511 if (!is_valid_option_list(optarg)) {
4512 error_report("Invalid option list: %s", optarg);
4513 goto out;
4514 }
4515 if (!options) {
4516 options = g_strdup(optarg);
4517 } else {
4518 char *old_options = options;
4519 options = g_strdup_printf("%s,%s", options, optarg);
4520 g_free(old_options);
4521 }
4522 break;
4523 case 'l':
4524 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
4525 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
4526 optarg, false);
4527 if (!sn_opts) {
4528 error_report("Failed in parsing snapshot param '%s'",
4529 optarg);
4530 goto out;
4531 }
4532 } else {
4533 snapshot_name = optarg;
4534 }
4535 break;
4536 case 'U':
4537 force_share = true;
4538 break;
4539 case OPTION_OBJECT:
4540 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
4541 optarg, true);
4542 if (!object_opts) {
4543 goto out;
4544 }
4545 break;
4546 case OPTION_IMAGE_OPTS:
4547 image_opts = true;
4548 break;
4549 case OPTION_OUTPUT:
4550 if (!strcmp(optarg, "json")) {
4551 output_format = OFORMAT_JSON;
4552 } else if (!strcmp(optarg, "human")) {
4553 output_format = OFORMAT_HUMAN;
4554 } else {
4555 error_report("--output must be used with human or json "
4556 "as argument.");
4557 goto out;
4558 }
4559 break;
4560 case OPTION_SIZE:
4561 {
4562 int64_t sval;
4563
4564 sval = cvtnum(optarg);
4565 if (sval < 0) {
4566 if (sval == -ERANGE) {
4567 error_report("Image size must be less than 8 EiB!");
4568 } else {
4569 error_report("Invalid image size specified! You may use "
4570 "k, M, G, T, P or E suffixes for ");
4571 error_report("kilobytes, megabytes, gigabytes, terabytes, "
4572 "petabytes and exabytes.");
4573 }
4574 goto out;
4575 }
4576 img_size = (uint64_t)sval;
4577 }
4578 break;
4579 }
4580 }
4581
4582 if (qemu_opts_foreach(&qemu_object_opts,
4583 user_creatable_add_opts_foreach,
4584 NULL, NULL)) {
4585 goto out;
4586 }
4587
4588 if (argc - optind > 1) {
4589 error_report("At most one filename argument is allowed.");
4590 goto out;
4591 } else if (argc - optind == 1) {
4592 filename = argv[optind];
4593 }
4594
4595 if (!filename &&
4596 (object_opts || image_opts || fmt || snapshot_name || sn_opts)) {
4597 error_report("--object, --image-opts, -f, and -l "
4598 "require a filename argument.");
4599 goto out;
4600 }
4601 if (filename && img_size != UINT64_MAX) {
4602 error_report("--size N cannot be used together with a filename.");
4603 goto out;
4604 }
4605 if (!filename && img_size == UINT64_MAX) {
4606 error_report("Either --size N or one filename must be specified.");
4607 goto out;
4608 }
4609
4610 if (filename) {
4611 in_blk = img_open(image_opts, filename, fmt, 0,
4612 false, false, force_share);
4613 if (!in_blk) {
4614 goto out;
4615 }
4616
4617 if (sn_opts) {
4618 bdrv_snapshot_load_tmp(blk_bs(in_blk),
4619 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
4620 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
4621 &local_err);
4622 } else if (snapshot_name != NULL) {
4623 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
4624 snapshot_name, &local_err);
4625 }
4626 if (local_err) {
4627 error_reportf_err(local_err, "Failed to load snapshot: ");
4628 goto out;
4629 }
4630 }
4631
4632 drv = bdrv_find_format(out_fmt);
4633 if (!drv) {
4634 error_report("Unknown file format '%s'", out_fmt);
4635 goto out;
4636 }
4637 if (!drv->create_opts) {
4638 error_report("Format driver '%s' does not support image creation",
4639 drv->format_name);
4640 goto out;
4641 }
4642
4643 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4644 create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
4645 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4646 if (options) {
4647 qemu_opts_do_parse(opts, options, NULL, &local_err);
4648 if (local_err) {
4649 error_report_err(local_err);
4650 error_report("Invalid options for file format '%s'", out_fmt);
4651 goto out;
4652 }
4653 }
4654 if (img_size != UINT64_MAX) {
4655 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
4656 }
4657
4658 info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
4659 if (local_err) {
4660 error_report_err(local_err);
4661 goto out;
4662 }
4663
4664 if (output_format == OFORMAT_HUMAN) {
4665 printf("required size: %" PRIu64 "\n", info->required);
4666 printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
4667 } else {
4668 dump_json_block_measure_info(info);
4669 }
4670
4671 ret = 0;
4672
4673out:
4674 qapi_free_BlockMeasureInfo(info);
4675 qemu_opts_del(object_opts);
4676 qemu_opts_del(opts);
4677 qemu_opts_del(sn_opts);
4678 qemu_opts_free(create_opts);
4679 g_free(options);
4680 blk_unref(in_blk);
4681 return ret;
4682}
Kevin Wolfb6133b82014-08-05 14:17:13 +02004683
Anthony Liguoric227f092009-10-01 16:12:16 -05004684static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004685#define DEF(option, callback, arg_string) \
4686 { option, callback },
4687#include "qemu-img-cmds.h"
4688#undef DEF
4689#undef GEN_DOCS
4690 { NULL, NULL, },
4691};
4692
bellardea2384d2004-08-01 21:59:26 +00004693int main(int argc, char **argv)
4694{
Anthony Liguoric227f092009-10-01 16:12:16 -05004695 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004696 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004697 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004698 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004699 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004700 static const struct option long_options[] = {
4701 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004702 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004703 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004704 {0, 0, 0, 0}
4705 };
bellardea2384d2004-08-01 21:59:26 +00004706
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004707#ifdef CONFIG_POSIX
4708 signal(SIGPIPE, SIG_IGN);
4709#endif
4710
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004711 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004712 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004713 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004714
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004715 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004716 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004717 exit(EXIT_FAILURE);
4718 }
4719
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004720 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004721
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004722 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004723 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004724 if (argc < 2) {
4725 error_exit("Not enough arguments");
4726 }
Stuart Brady153859b2009-06-07 00:42:17 +01004727
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004728 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004729 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004730 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004731
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004732 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004733 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004734 case ':':
4735 missing_argument(argv[optind - 1]);
4736 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08004737 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004738 unrecognized_option(argv[optind - 1]);
4739 return 0;
4740 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03004741 help();
4742 return 0;
4743 case 'V':
4744 printf(QEMU_IMG_VERSION);
4745 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004746 case 'T':
4747 g_free(trace_file);
4748 trace_file = trace_opt_parse(optarg);
4749 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004750 }
bellardea2384d2004-08-01 21:59:26 +00004751 }
Stuart Brady153859b2009-06-07 00:42:17 +01004752
Denis V. Lunev10985132016-06-17 17:44:13 +03004753 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004754
Denis V. Lunev10985132016-06-17 17:44:13 +03004755 /* reset getopt_long scanning */
4756 argc -= optind;
4757 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004758 return 0;
4759 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004760 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004761 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004762
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004763 if (!trace_init_backends()) {
4764 exit(1);
4765 }
4766 trace_init_file(trace_file);
4767 qemu_set_log(LOG_TRACE);
4768
Denis V. Lunev10985132016-06-17 17:44:13 +03004769 /* find the command */
4770 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4771 if (!strcmp(cmdname, cmd->name)) {
4772 return cmd->handler(argc, argv);
4773 }
4774 }
Jeff Cody7db16892014-04-25 17:02:32 -04004775
Stuart Brady153859b2009-06-07 00:42:17 +01004776 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004777 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004778}