blob: 8023233c3dbee410e8f37e7c57f9862e47d409b1 [file] [log] [blame]
bellardea2384d2004-08-01 21:59:26 +00001/*
bellardfb43f4d2006-08-07 21:34:46 +00002 * QEMU disk image utility
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardea2384d2004-08-01 21:59:26 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Peter Maydell80c71a22016-01-18 18:01:42 +000024#include "qemu/osdep.h"
Eric Blakec2a3d7d2017-07-21 08:50:47 -050025#include <getopt.h>
26
Fam Zheng67a1de02016-06-01 17:44:21 +080027#include "qemu-version.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010028#include "qapi/error.h"
BenoƮt Canetc054b3f2012-09-05 13:09:02 +020029#include "qapi-visit.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010030#include "qapi/qobject-output-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010031#include "qapi/qmp/qjson.h"
Fam Zheng335e9932017-05-03 00:35:39 +080032#include "qapi/qmp/qbool.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020033#include "qemu/cutils.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000034#include "qemu/config-file.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010035#include "qemu/option.h"
36#include "qemu/error-report.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030037#include "qemu/log.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000038#include "qom/object_interfaces.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010039#include "sysemu/sysemu.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020040#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010041#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020042#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080043#include "block/qapi.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010044#include "crypto/init.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030045#include "trace/control.h"
bellarde8445332006-06-14 15:32:10 +000046
Don Slutz61979a62015-01-09 10:17:35 -050047#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
Thomas Huth0781dd62016-10-05 11:54:44 +020048 "\n" QEMU_COPYRIGHT "\n"
Jeff Cody5f6979c2014-04-28 14:37:18 -040049
Anthony Liguoric227f092009-10-01 16:12:16 -050050typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010051 const char *name;
52 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050053} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010054
Federico Simoncelli8599ea42013-01-28 06:59:47 -050055enum {
56 OPTION_OUTPUT = 256,
57 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000058 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000059 OPTION_IMAGE_OPTS = 259,
Kevin Wolfb6495fa2015-07-10 18:09:18 +020060 OPTION_PATTERN = 260,
Kevin Wolf55d539c2016-06-03 13:59:41 +020061 OPTION_FLUSH_INTERVAL = 261,
62 OPTION_NO_DRAIN = 262,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +010063 OPTION_TARGET_IMAGE_OPTS = 263,
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +010064 OPTION_SIZE = 264,
Max Reitzdc5f6902017-06-13 22:20:55 +020065 OPTION_PREALLOCATION = 265,
Pavel Butsykin4ffca892017-09-18 15:42:27 +030066 OPTION_SHRINK = 266,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050067};
68
69typedef enum OutputFormat {
70 OFORMAT_JSON,
71 OFORMAT_HUMAN,
72} OutputFormat;
73
Kevin Wolfe6996142016-03-15 13:03:11 +010074/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040075#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000076
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010077static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000078{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010079 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000080}
81
Fam Zhengac1307a2014-04-22 13:36:11 +080082static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
83{
84 va_list ap;
85
86 error_printf("qemu-img: ");
87
88 va_start(ap, fmt);
89 error_vprintf(fmt, ap);
90 va_end(ap);
91
92 error_printf("\nTry 'qemu-img --help' for more information\n");
93 exit(EXIT_FAILURE);
94}
95
Stefan Hajnoczic9192972017-03-17 18:45:41 +080096static void QEMU_NORETURN missing_argument(const char *option)
97{
98 error_exit("missing argument for option '%s'", option);
99}
100
101static void QEMU_NORETURN unrecognized_option(const char *option)
102{
103 error_exit("unrecognized option '%s'", option);
104}
105
blueswir1d2c639d2009-01-24 18:19:25 +0000106/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +0800107static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +0000108{
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100109 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -0400110 QEMU_IMG_VERSION
Denis V. Lunev10985132016-06-17 17:44:13 +0300111 "usage: qemu-img [standard options] command [command options]\n"
malc3f020d72010-02-08 12:04:56 +0300112 "QEMU disk image utility\n"
113 "\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300114 " '-h', '--help' display this help and exit\n"
115 " '-V', '--version' output version information and exit\n"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +0300116 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
117 " specify tracing options\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300118 "\n"
malc3f020d72010-02-08 12:04:56 +0300119 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100120#define DEF(option, callback, arg_string) \
121 " " arg_string "\n"
122#include "qemu-img-cmds.h"
123#undef DEF
124#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +0300125 "\n"
126 "Command parameters:\n"
127 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000128 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
129 " manual page for a description of the object properties. The most common\n"
130 " object type is a 'secret', which is used to supply passwords and/or\n"
131 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300132 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400133 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800134 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
135 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100136 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
137 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300138 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200139 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
140 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
141 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300142 " 'output_filename' is the destination disk image filename\n"
143 " 'output_fmt' is the destination format\n"
144 " 'options' is a comma separated list of format specific options in a\n"
145 " name=value format. Use -o ? for an overview of the options supported by the\n"
146 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800147 " 'snapshot_param' is param used for internal snapshot, format\n"
148 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
149 " '[ID_OR_NAME]'\n"
150 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
151 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300152 " '-c' indicates that target image must be compressed (qcow format only)\n"
John Snow6e6e55f2017-07-17 20:34:22 -0400153 " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
154 " new backing file match exactly. The image doesn't need a working\n"
155 " backing file before rebasing in this case (useful for renaming the\n"
156 " backing file). For image creation, allow creating without attempting\n"
157 " to open the backing file.\n"
malc3f020d72010-02-08 12:04:56 +0300158 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200159 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100160 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200161 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
162 " contain only zeros for qemu-img to create a sparse image during\n"
163 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
164 " unallocated or zero sectors, and the destination image will always be\n"
165 " fully allocated\n"
BenoƮt Canetc054b3f2012-09-05 13:09:02 +0200166 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100167 " '-n' skips the target volume creation (useful if the volume is created\n"
168 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300169 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200170 "Parameters to check subcommand:\n"
171 " '-r' tries to repair any inconsistencies that are found during the check.\n"
172 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
173 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200174 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200175 "\n"
Peter Lieven2d9187b2017-02-28 13:40:07 +0100176 "Parameters to convert subcommand:\n"
177 " '-m' specifies how many coroutines work in parallel during the convert\n"
178 " process (defaults to 8)\n"
179 " '-W' allow to write to the target out of order rather than sequential\n"
180 "\n"
malc3f020d72010-02-08 12:04:56 +0300181 "Parameters to snapshot subcommand:\n"
182 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
183 " '-a' applies a snapshot (revert disk to saved state)\n"
184 " '-c' creates a snapshot\n"
185 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100186 " '-l' lists all snapshots in the given image\n"
187 "\n"
188 "Parameters to compare subcommand:\n"
189 " '-f' first image format\n"
190 " '-F' second image format\n"
Reda Sallahi86ce1f62016-08-10 04:43:12 +0200191 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
192 "\n"
193 "Parameters to dd subcommand:\n"
194 " 'bs=BYTES' read and write up to BYTES bytes at a time "
195 "(default: 512)\n"
196 " 'count=N' copy only N input blocks\n"
197 " 'if=FILE' read from FILE\n"
Reda Sallahif7c15532016-08-10 16:16:09 +0200198 " 'of=FILE' write to FILE\n"
199 " 'skip=N' skip N bs-sized blocks at the start of input\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100200
201 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100202 bdrv_iterate_format(format_print, NULL);
Eric Blakef5048cb2017-08-03 11:33:53 -0500203 printf("\n\n" QEMU_HELP_BOTTOM "\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800204 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000205}
206
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000207static QemuOptsList qemu_object_opts = {
208 .name = "object",
209 .implied_opt_name = "qom-type",
210 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
211 .desc = {
212 { }
213 },
214};
215
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000216static QemuOptsList qemu_source_opts = {
217 .name = "source",
218 .implied_opt_name = "file",
219 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
220 .desc = {
221 { }
222 },
223};
224
Stefan Weil7c30f652013-06-16 17:01:05 +0200225static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100226{
227 int ret = 0;
228 if (!quiet) {
229 va_list args;
230 va_start(args, fmt);
231 ret = vprintf(fmt, args);
232 va_end(args);
233 }
234 return ret;
235}
236
bellardea2384d2004-08-01 21:59:26 +0000237
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100238static int print_block_option_help(const char *filename, const char *fmt)
239{
240 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800241 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500242 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100243
244 /* Find driver and parse its options */
245 drv = bdrv_find_format(fmt);
246 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100247 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100248 return 1;
249 }
250
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800251 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100252 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500253 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100254 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100255 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800256 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100257 return 1;
258 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800259 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100260 }
261
Chunyan Liu83d05212014-06-05 17:20:51 +0800262 qemu_opts_print_help(create_opts);
263 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100264 return 0;
265}
266
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000267
Max Reitzefaa7c42016-03-16 19:54:38 +0100268static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100269 QemuOpts *opts, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800270 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000271{
272 QDict *options;
273 Error *local_err = NULL;
274 BlockBackend *blk;
275 options = qemu_opts_to_qdict(opts, NULL);
Fam Zheng335e9932017-05-03 00:35:39 +0800276 if (force_share) {
277 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
278 && !qdict_get_bool(options, BDRV_OPT_FORCE_SHARE)) {
279 error_report("--force-share/-U conflicts with image options");
Fam Zhengadb998c2017-05-15 22:10:14 +0800280 QDECREF(options);
Fam Zheng335e9932017-05-03 00:35:39 +0800281 return NULL;
282 }
Eric Blake579cf1d2017-05-15 14:54:39 -0500283 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800284 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100285 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000286 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100287 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000288 return NULL;
289 }
Kevin Wolfce099542016-03-15 13:01:04 +0100290 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000291
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000292 return blk;
293}
294
Max Reitzefaa7c42016-03-16 19:54:38 +0100295static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100296 QDict *options,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000297 const char *fmt, int flags,
Fam Zheng335e9932017-05-03 00:35:39 +0800298 bool writethrough, bool quiet,
299 bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000300{
301 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200302 Error *local_err = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100303
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100304 if (!options) {
305 options = qdict_new();
306 }
bellard75c23802004-08-27 21:28:58 +0000307 if (fmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500308 qdict_put_str(options, "driver", fmt);
bellard75c23802004-08-27 21:28:58 +0000309 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100310
Fam Zheng335e9932017-05-03 00:35:39 +0800311 if (force_share) {
Eric Blake579cf1d2017-05-15 14:54:39 -0500312 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800313 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100314 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500315 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100316 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000317 return NULL;
bellard75c23802004-08-27 21:28:58 +0000318 }
Kevin Wolfce099542016-03-15 13:01:04 +0100319 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100320
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200321 return blk;
bellard75c23802004-08-27 21:28:58 +0000322}
323
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000324
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100325static int img_add_key_secrets(void *opaque,
326 const char *name, const char *value,
327 Error **errp)
328{
329 QDict *options = opaque;
330
331 if (g_str_has_suffix(name, "key-secret")) {
Eric Blake187f47e2017-06-24 12:10:07 -0600332 qdict_put_str(options, name, value);
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100333 }
334
335 return 0;
336}
337
338static BlockBackend *img_open_new_file(const char *filename,
339 QemuOpts *create_opts,
340 const char *fmt, int flags,
341 bool writethrough, bool quiet,
342 bool force_share)
343{
344 QDict *options = NULL;
345
346 options = qdict_new();
347 qemu_opt_foreach(create_opts, img_add_key_secrets, options, &error_abort);
348
349 return img_open_file(filename, options, fmt, flags, writethrough, quiet,
350 force_share);
351}
352
353
Max Reitzefaa7c42016-03-16 19:54:38 +0100354static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000355 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100356 const char *fmt, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800357 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000358{
359 BlockBackend *blk;
360 if (image_opts) {
361 QemuOpts *opts;
362 if (fmt) {
363 error_report("--image-opts and --format are mutually exclusive");
364 return NULL;
365 }
366 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
367 filename, true);
368 if (!opts) {
369 return NULL;
370 }
Fam Zheng335e9932017-05-03 00:35:39 +0800371 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
372 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000373 } else {
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100374 blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
Fam Zheng335e9932017-05-03 00:35:39 +0800375 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000376 }
377 return blk;
378}
379
380
Chunyan Liu83d05212014-06-05 17:20:51 +0800381static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100382 const char *base_filename,
383 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200384{
Markus Armbruster6750e792015-02-12 17:43:08 +0100385 Error *err = NULL;
386
Kevin Wolfefa84d42009-05-18 16:42:12 +0200387 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100388 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100389 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100390 error_report("Backing file not supported for file format '%s'",
391 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100392 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900393 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200394 }
395 }
396 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100397 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100398 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100399 error_report("Backing file format not supported for file "
400 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100401 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900402 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200403 }
404 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900405 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200406}
407
Markus Armbruster606caa02017-02-21 21:14:04 +0100408static int64_t cvtnum(const char *s)
409{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100410 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100411 uint64_t value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100412
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100413 err = qemu_strtosz(s, NULL, &value);
414 if (err < 0) {
415 return err;
416 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100417 if (value > INT64_MAX) {
418 return -ERANGE;
419 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100420 return value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100421}
422
bellardea2384d2004-08-01 21:59:26 +0000423static int img_create(int argc, char **argv)
424{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200425 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100426 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000427 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000428 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000429 const char *filename;
430 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200431 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200432 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100433 bool quiet = false;
John Snow6e6e55f2017-07-17 20:34:22 -0400434 int flags = 0;
ths3b46e622007-09-17 08:09:54 +0000435
bellardea2384d2004-08-01 21:59:26 +0000436 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000437 static const struct option long_options[] = {
438 {"help", no_argument, 0, 'h'},
439 {"object", required_argument, 0, OPTION_OBJECT},
440 {0, 0, 0, 0}
441 };
John Snow6e6e55f2017-07-17 20:34:22 -0400442 c = getopt_long(argc, argv, ":F:b:f:ho:qu",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000443 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100444 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000445 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100446 }
bellardea2384d2004-08-01 21:59:26 +0000447 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800448 case ':':
449 missing_argument(argv[optind - 1]);
450 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100451 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800452 unrecognized_option(argv[optind - 1]);
453 break;
bellardea2384d2004-08-01 21:59:26 +0000454 case 'h':
455 help();
456 break;
aliguori9230eaf2009-03-28 17:55:19 +0000457 case 'F':
458 base_fmt = optarg;
459 break;
bellardea2384d2004-08-01 21:59:26 +0000460 case 'b':
461 base_filename = optarg;
462 break;
463 case 'f':
464 fmt = optarg;
465 break;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200466 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100467 if (!is_valid_option_list(optarg)) {
468 error_report("Invalid option list: %s", optarg);
469 goto fail;
470 }
471 if (!options) {
472 options = g_strdup(optarg);
473 } else {
474 char *old_options = options;
475 options = g_strdup_printf("%s,%s", options, optarg);
476 g_free(old_options);
477 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200478 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100479 case 'q':
480 quiet = true;
481 break;
John Snow6e6e55f2017-07-17 20:34:22 -0400482 case 'u':
483 flags |= BDRV_O_NO_BACKING;
484 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000485 case OPTION_OBJECT: {
486 QemuOpts *opts;
487 opts = qemu_opts_parse_noisily(&qemu_object_opts,
488 optarg, true);
489 if (!opts) {
490 goto fail;
491 }
492 } break;
bellardea2384d2004-08-01 21:59:26 +0000493 }
494 }
aliguori9230eaf2009-03-28 17:55:19 +0000495
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900496 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100497 filename = (optind < argc) ? argv[optind] : NULL;
498 if (options && has_help_option(options)) {
499 g_free(options);
500 return print_block_option_help(filename, fmt);
501 }
502
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100503 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800504 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100505 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100506 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900507
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000508 if (qemu_opts_foreach(&qemu_object_opts,
509 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200510 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000511 goto fail;
512 }
513
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100514 /* Get image size, if specified */
515 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100516 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100517
518 sval = cvtnum(argv[optind++]);
519 if (sval < 0) {
liguang79443392012-12-17 09:49:23 +0800520 if (sval == -ERANGE) {
521 error_report("Image size must be less than 8 EiB!");
522 } else {
523 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200524 "G, T, P or E suffixes for ");
525 error_report("kilobytes, megabytes, gigabytes, terabytes, "
526 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800527 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100528 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100529 }
530 img_size = (uint64_t)sval;
531 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200532 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800533 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200534 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100535
Luiz Capitulino9b375252012-11-30 10:52:05 -0200536 bdrv_img_create(filename, fmt, base_filename, base_fmt,
John Snow6e6e55f2017-07-17 20:34:22 -0400537 options, img_size, flags, quiet, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100538 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100539 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100540 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900541 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200542
Kevin Wolf77386bf2014-02-21 16:24:04 +0100543 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000544 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100545
546fail:
547 g_free(options);
548 return 1;
bellardea2384d2004-08-01 21:59:26 +0000549}
550
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100551static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500552{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500553 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500554 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100555 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600556
557 visit_type_ImageCheck(v, NULL, &check, &error_abort);
558 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500559 str = qobject_to_json_pretty(obj);
560 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100561 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500562 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600563 visit_free(v);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500564 QDECREF(str);
565}
566
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100567static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500568{
569 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100570 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500571 } else {
572 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100573 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
574 "Data may be corrupted, or further writes to the image "
575 "may corrupt it.\n",
576 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500577 }
578
579 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100580 qprintf(quiet,
581 "\n%" PRId64 " leaked clusters were found on the image.\n"
582 "This means waste of disk space, but no harm to data.\n",
583 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500584 }
585
586 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100587 qprintf(quiet,
588 "\n%" PRId64
589 " internal errors have occurred during the check.\n",
590 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500591 }
592 }
593
594 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100595 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
596 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
597 check->allocated_clusters, check->total_clusters,
598 check->allocated_clusters * 100.0 / check->total_clusters,
599 check->fragmented_clusters * 100.0 / check->allocated_clusters,
600 check->compressed_clusters * 100.0 /
601 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500602 }
603
604 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100605 qprintf(quiet,
606 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500607 }
608}
609
610static int collect_image_check(BlockDriverState *bs,
611 ImageCheck *check,
612 const char *filename,
613 const char *fmt,
614 int fix)
615{
616 int ret;
617 BdrvCheckResult result;
618
619 ret = bdrv_check(bs, &result, fix);
620 if (ret < 0) {
621 return ret;
622 }
623
624 check->filename = g_strdup(filename);
625 check->format = g_strdup(bdrv_get_format_name(bs));
626 check->check_errors = result.check_errors;
627 check->corruptions = result.corruptions;
628 check->has_corruptions = result.corruptions != 0;
629 check->leaks = result.leaks;
630 check->has_leaks = result.leaks != 0;
631 check->corruptions_fixed = result.corruptions_fixed;
632 check->has_corruptions_fixed = result.corruptions != 0;
633 check->leaks_fixed = result.leaks_fixed;
634 check->has_leaks_fixed = result.leaks != 0;
635 check->image_end_offset = result.image_end_offset;
636 check->has_image_end_offset = result.image_end_offset != 0;
637 check->total_clusters = result.bfi.total_clusters;
638 check->has_total_clusters = result.bfi.total_clusters != 0;
639 check->allocated_clusters = result.bfi.allocated_clusters;
640 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
641 check->fragmented_clusters = result.bfi.fragmented_clusters;
642 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100643 check->compressed_clusters = result.bfi.compressed_clusters;
644 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500645
646 return 0;
647}
648
Kevin Wolfe076f332010-06-29 11:43:13 +0200649/*
650 * Checks an image for consistency. Exit codes:
651 *
Max Reitzd6635c42014-06-02 22:15:21 +0200652 * 0 - Check completed, image is good
653 * 1 - Check not completed because of internal errors
654 * 2 - Check completed, image is corrupted
655 * 3 - Check completed, image has leaked clusters, but is good otherwise
656 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200657 */
aliguori15859692009-04-21 23:11:53 +0000658static int img_check(int argc, char **argv)
659{
660 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500661 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200662 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200663 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000664 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200665 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100666 int flags = BDRV_O_CHECK;
667 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200668 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100669 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000670 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +0800671 bool force_share = false;
aliguori15859692009-04-21 23:11:53 +0000672
673 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500674 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200675 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100676
aliguori15859692009-04-21 23:11:53 +0000677 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500678 int option_index = 0;
679 static const struct option long_options[] = {
680 {"help", no_argument, 0, 'h'},
681 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530682 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500683 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000684 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000685 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +0800686 {"force-share", no_argument, 0, 'U'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500687 {0, 0, 0, 0}
688 };
Fam Zheng335e9932017-05-03 00:35:39 +0800689 c = getopt_long(argc, argv, ":hf:r:T:qU",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500690 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100691 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000692 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100693 }
aliguori15859692009-04-21 23:11:53 +0000694 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800695 case ':':
696 missing_argument(argv[optind - 1]);
697 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100698 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800699 unrecognized_option(argv[optind - 1]);
700 break;
aliguori15859692009-04-21 23:11:53 +0000701 case 'h':
702 help();
703 break;
704 case 'f':
705 fmt = optarg;
706 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200707 case 'r':
708 flags |= BDRV_O_RDWR;
709
710 if (!strcmp(optarg, "leaks")) {
711 fix = BDRV_FIX_LEAKS;
712 } else if (!strcmp(optarg, "all")) {
713 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
714 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800715 error_exit("Unknown option value for -r "
716 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200717 }
718 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500719 case OPTION_OUTPUT:
720 output = optarg;
721 break;
Max Reitz40055952014-07-22 22:58:42 +0200722 case 'T':
723 cache = optarg;
724 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100725 case 'q':
726 quiet = true;
727 break;
Fam Zheng335e9932017-05-03 00:35:39 +0800728 case 'U':
729 force_share = true;
730 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000731 case OPTION_OBJECT: {
732 QemuOpts *opts;
733 opts = qemu_opts_parse_noisily(&qemu_object_opts,
734 optarg, true);
735 if (!opts) {
736 return 1;
737 }
738 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000739 case OPTION_IMAGE_OPTS:
740 image_opts = true;
741 break;
aliguori15859692009-04-21 23:11:53 +0000742 }
743 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200744 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800745 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100746 }
aliguori15859692009-04-21 23:11:53 +0000747 filename = argv[optind++];
748
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500749 if (output && !strcmp(output, "json")) {
750 output_format = OFORMAT_JSON;
751 } else if (output && !strcmp(output, "human")) {
752 output_format = OFORMAT_HUMAN;
753 } else if (output) {
754 error_report("--output must be used with human or json as argument.");
755 return 1;
756 }
757
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000758 if (qemu_opts_foreach(&qemu_object_opts,
759 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200760 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000761 return 1;
762 }
763
Kevin Wolfce099542016-03-15 13:01:04 +0100764 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200765 if (ret < 0) {
766 error_report("Invalid source cache option: %s", cache);
767 return 1;
768 }
769
Fam Zheng335e9932017-05-03 00:35:39 +0800770 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
771 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200772 if (!blk) {
773 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900774 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200775 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500776
777 check = g_new0(ImageCheck, 1);
778 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200779
780 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200781 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200782 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500783 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200784 }
785
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500786 if (check->corruptions_fixed || check->leaks_fixed) {
787 int corruptions_fixed, leaks_fixed;
788
789 leaks_fixed = check->leaks_fixed;
790 corruptions_fixed = check->corruptions_fixed;
791
792 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100793 qprintf(quiet,
794 "The following inconsistencies were found and repaired:\n\n"
795 " %" PRId64 " leaked clusters\n"
796 " %" PRId64 " corruptions\n\n"
797 "Double checking the fixed image now...\n",
798 check->leaks_fixed,
799 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500800 }
801
802 ret = collect_image_check(bs, check, filename, fmt, 0);
803
804 check->leaks_fixed = leaks_fixed;
805 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200806 }
807
Max Reitz832390a2014-10-23 15:29:12 +0200808 if (!ret) {
809 switch (output_format) {
810 case OFORMAT_HUMAN:
811 dump_human_image_check(check, quiet);
812 break;
813 case OFORMAT_JSON:
814 dump_json_image_check(check, quiet);
815 break;
816 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500817 }
818
819 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200820 if (ret) {
821 error_report("Check failed: %s", strerror(-ret));
822 } else {
823 error_report("Check failed");
824 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500825 ret = 1;
826 goto fail;
827 }
828
829 if (check->corruptions) {
830 ret = 2;
831 } else if (check->leaks) {
832 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200833 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500834 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000835 }
836
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500837fail:
838 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200839 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500840 return ret;
aliguori15859692009-04-21 23:11:53 +0000841}
842
Max Reitzd4a32382014-10-24 15:57:37 +0200843typedef struct CommonBlockJobCBInfo {
844 BlockDriverState *bs;
845 Error **errp;
846} CommonBlockJobCBInfo;
847
848static void common_block_job_cb(void *opaque, int ret)
849{
850 CommonBlockJobCBInfo *cbi = opaque;
851
852 if (ret < 0) {
853 error_setg_errno(cbi->errp, -ret, "Block job failed");
854 }
Max Reitzd4a32382014-10-24 15:57:37 +0200855}
856
857static void run_block_job(BlockJob *job, Error **errp)
858{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200859 AioContext *aio_context = blk_get_aio_context(job->blk);
sochin.jiang4172a002017-06-15 14:47:33 +0800860 int ret = 0;
Max Reitzd4a32382014-10-24 15:57:37 +0200861
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200862 aio_context_acquire(aio_context);
sochin.jiang4172a002017-06-15 14:47:33 +0800863 block_job_ref(job);
Max Reitzd4a32382014-10-24 15:57:37 +0200864 do {
865 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500866 qemu_progress_print(job->len ?
867 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
sochin.jiang4172a002017-06-15 14:47:33 +0800868 } while (!job->ready && !job->completed);
Max Reitzd4a32382014-10-24 15:57:37 +0200869
sochin.jiang4172a002017-06-15 14:47:33 +0800870 if (!job->completed) {
871 ret = block_job_complete_sync(job, errp);
872 } else {
873 ret = job->ret;
874 }
875 block_job_unref(job);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200876 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200877
sochin.jiang4172a002017-06-15 14:47:33 +0800878 /* publish completion progress only when success */
879 if (!ret) {
880 qemu_progress_print(100.f, 0);
881 }
Max Reitzd4a32382014-10-24 15:57:37 +0200882}
883
bellardea2384d2004-08-01 21:59:26 +0000884static int img_commit(int argc, char **argv)
885{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400886 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200887 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200888 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200889 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100890 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200891 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100892 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200893 Error *local_err = NULL;
894 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000895 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200896 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000897
898 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400899 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200900 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000901 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000902 static const struct option long_options[] = {
903 {"help", no_argument, 0, 'h'},
904 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000905 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000906 {0, 0, 0, 0}
907 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800908 c = getopt_long(argc, argv, ":f:ht:b:dpq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000909 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100910 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000911 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100912 }
bellardea2384d2004-08-01 21:59:26 +0000913 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800914 case ':':
915 missing_argument(argv[optind - 1]);
916 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100917 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800918 unrecognized_option(argv[optind - 1]);
919 break;
bellardea2384d2004-08-01 21:59:26 +0000920 case 'h':
921 help();
922 break;
923 case 'f':
924 fmt = optarg;
925 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400926 case 't':
927 cache = optarg;
928 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200929 case 'b':
930 base = optarg;
931 /* -b implies -d */
932 drop = true;
933 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200934 case 'd':
935 drop = true;
936 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200937 case 'p':
938 progress = true;
939 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100940 case 'q':
941 quiet = true;
942 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000943 case OPTION_OBJECT: {
944 QemuOpts *opts;
945 opts = qemu_opts_parse_noisily(&qemu_object_opts,
946 optarg, true);
947 if (!opts) {
948 return 1;
949 }
950 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000951 case OPTION_IMAGE_OPTS:
952 image_opts = true;
953 break;
bellardea2384d2004-08-01 21:59:26 +0000954 }
955 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200956
957 /* Progress is not shown in Quiet mode */
958 if (quiet) {
959 progress = false;
960 }
961
Kevin Wolffc11eb22013-08-05 10:53:04 +0200962 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800963 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100964 }
bellardea2384d2004-08-01 21:59:26 +0000965 filename = argv[optind++];
966
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000967 if (qemu_opts_foreach(&qemu_object_opts,
968 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200969 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000970 return 1;
971 }
972
Max Reitz9a86fe42014-10-24 15:57:38 +0200973 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100974 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400975 if (ret < 0) {
976 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100977 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400978 }
979
Fam Zheng335e9932017-05-03 00:35:39 +0800980 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
981 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200982 if (!blk) {
983 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900984 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200985 bs = blk_bs(blk);
986
Max Reitz687fa1d2014-10-24 15:57:39 +0200987 qemu_progress_init(progress, 1.f);
988 qemu_progress_print(0.f, 100);
989
Max Reitz1b22bff2014-10-24 15:57:40 +0200990 if (base) {
991 base_bs = bdrv_find_backing_image(bs, base);
992 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +0100993 error_setg(&local_err,
994 "Did not find '%s' in the backing chain of '%s'",
995 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +0200996 goto done;
997 }
998 } else {
999 /* This is different from QMP, which by default uses the deepest file in
1000 * the backing chain (i.e., the very base); however, the traditional
1001 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +02001002 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +02001003 if (!base_bs) {
1004 error_setg(&local_err, "Image does not have a backing file");
1005 goto done;
1006 }
bellardea2384d2004-08-01 21:59:26 +00001007 }
1008
Max Reitzd4a32382014-10-24 15:57:37 +02001009 cbi = (CommonBlockJobCBInfo){
1010 .errp = &local_err,
1011 .bs = bs,
1012 };
1013
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001014 aio_context = bdrv_get_aio_context(bs);
1015 aio_context_acquire(aio_context);
John Snow47970df2016-10-27 12:06:57 -04001016 commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
Kevin Wolf0db832f2017-02-20 18:10:05 +01001017 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +08001018 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001019 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +02001020 if (local_err) {
1021 goto done;
1022 }
1023
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001024 /* When the block job completes, the BlockBackend reference will point to
1025 * the old backing file. In order to avoid that the top image is already
1026 * deleted, so we can still empty it afterwards, increment the reference
1027 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +02001028 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001029 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001030 }
1031
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001032 job = block_job_get("commit");
1033 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001034 if (local_err) {
1035 goto unref_backing;
1036 }
1037
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001038 if (!drop && bs->drv->bdrv_make_empty) {
1039 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001040 if (ret) {
1041 error_setg_errno(&local_err, -ret, "Could not empty %s",
1042 filename);
1043 goto unref_backing;
1044 }
1045 }
1046
1047unref_backing:
1048 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001049 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001050 }
Max Reitzd4a32382014-10-24 15:57:37 +02001051
1052done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001053 qemu_progress_end();
1054
Markus Armbruster26f54e92014-10-07 13:59:04 +02001055 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001056
1057 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001058 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001059 return 1;
1060 }
Max Reitzd4a32382014-10-24 15:57:37 +02001061
1062 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001063 return 0;
1064}
1065
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001066/*
Eric Blakedebb38a2017-10-11 22:47:11 -05001067 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1068 * of the first sector boundary within buf where the sector contains a
1069 * non-zero byte. This function is robust to a buffer that is not
1070 * sector-aligned.
1071 */
1072static int64_t find_nonzero(const uint8_t *buf, int64_t n)
1073{
1074 int64_t i;
1075 int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE);
1076
1077 for (i = 0; i < end; i += BDRV_SECTOR_SIZE) {
1078 if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) {
1079 return i;
1080 }
1081 }
1082 if (i < n && !buffer_is_zero(buf + i, n - end)) {
1083 return i;
1084 }
1085 return -1;
1086}
1087
1088/*
thsf58c7b32008-06-05 21:53:49 +00001089 * Returns true iff the first sector pointed to by 'buf' contains at least
1090 * a non-NUL byte.
1091 *
1092 * 'pnum' is set to the number of sectors (including and immediately following
1093 * the first one) that are known to be in the same allocated/unallocated state.
1094 */
bellardea2384d2004-08-01 21:59:26 +00001095static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1096{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001097 bool is_zero;
1098 int i;
bellardea2384d2004-08-01 21:59:26 +00001099
1100 if (n <= 0) {
1101 *pnum = 0;
1102 return 0;
1103 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001104 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001105 for(i = 1; i < n; i++) {
1106 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001107 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001108 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001109 }
bellardea2384d2004-08-01 21:59:26 +00001110 }
1111 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001112 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001113}
1114
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001115/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001116 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1117 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1118 * breaking up write requests for only small sparse areas.
1119 */
1120static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1121 int min)
1122{
1123 int ret;
1124 int num_checked, num_used;
1125
1126 if (n < min) {
1127 min = n;
1128 }
1129
1130 ret = is_allocated_sectors(buf, n, pnum);
1131 if (!ret) {
1132 return ret;
1133 }
1134
1135 num_used = *pnum;
1136 buf += BDRV_SECTOR_SIZE * *pnum;
1137 n -= *pnum;
1138 num_checked = num_used;
1139
1140 while (n > 0) {
1141 ret = is_allocated_sectors(buf, n, pnum);
1142
1143 buf += BDRV_SECTOR_SIZE * *pnum;
1144 n -= *pnum;
1145 num_checked += *pnum;
1146 if (ret) {
1147 num_used = num_checked;
1148 } else if (*pnum >= min) {
1149 break;
1150 }
1151 }
1152
1153 *pnum = num_used;
1154 return 1;
1155}
1156
1157/*
Eric Blakedc61cd32017-10-11 22:47:14 -05001158 * Compares two buffers sector by sector. Returns 0 if the first
1159 * sector of each buffer matches, non-zero otherwise.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001160 *
Eric Blakedc61cd32017-10-11 22:47:14 -05001161 * pnum is set to the sector-aligned size of the buffer prefix that
1162 * has the same matching status as the first sector.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001163 */
Eric Blakedc61cd32017-10-11 22:47:14 -05001164static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2,
1165 int64_t bytes, int64_t *pnum)
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001166{
Radim KrčmÔř8c1ac472015-02-20 17:06:15 +01001167 bool res;
Eric Blakedc61cd32017-10-11 22:47:14 -05001168 int64_t i = MIN(bytes, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001169
Eric Blakedc61cd32017-10-11 22:47:14 -05001170 assert(bytes > 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001171
Eric Blakedc61cd32017-10-11 22:47:14 -05001172 res = !!memcmp(buf1, buf2, i);
1173 while (i < bytes) {
1174 int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001175
Eric Blakedc61cd32017-10-11 22:47:14 -05001176 if (!!memcmp(buf1 + i, buf2 + i, len) != res) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001177 break;
1178 }
Eric Blakedc61cd32017-10-11 22:47:14 -05001179 i += len;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001180 }
1181
1182 *pnum = i;
1183 return res;
1184}
1185
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001186#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001187
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001188/*
1189 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1190 *
Eric Blake0608e402017-10-11 22:47:12 -05001191 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1192 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1193 * failure), and 4 on error (the exit status for read errors), after emitting
1194 * an error message.
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001195 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001196 * @param blk: BlockBackend for the image
Eric Blakec41508e2017-10-11 22:47:13 -05001197 * @param offset: Starting offset to check
1198 * @param bytes: Number of bytes to check
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001199 * @param filename: Name of disk file we are checking (logging purpose)
1200 * @param buffer: Allocated buffer for storing read data
1201 * @param quiet: Flag for quiet mode
1202 */
Eric Blakec41508e2017-10-11 22:47:13 -05001203static int check_empty_sectors(BlockBackend *blk, int64_t offset,
1204 int64_t bytes, const char *filename,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001205 uint8_t *buffer, bool quiet)
1206{
Eric Blakedebb38a2017-10-11 22:47:11 -05001207 int ret = 0;
1208 int64_t idx;
1209
Eric Blakec41508e2017-10-11 22:47:13 -05001210 ret = blk_pread(blk, offset, buffer, bytes);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001211 if (ret < 0) {
1212 error_report("Error while reading offset %" PRId64 " of %s: %s",
Eric Blakec41508e2017-10-11 22:47:13 -05001213 offset, filename, strerror(-ret));
Eric Blake0608e402017-10-11 22:47:12 -05001214 return 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001215 }
Eric Blakec41508e2017-10-11 22:47:13 -05001216 idx = find_nonzero(buffer, bytes);
Eric Blakedebb38a2017-10-11 22:47:11 -05001217 if (idx >= 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001218 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blakec41508e2017-10-11 22:47:13 -05001219 offset + idx);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001220 return 1;
1221 }
1222
1223 return 0;
1224}
1225
1226/*
1227 * Compares two images. Exit codes:
1228 *
1229 * 0 - Images are identical
1230 * 1 - Images differ
1231 * >1 - Error occurred
1232 */
1233static int img_compare(int argc, char **argv)
1234{
Max Reitz40055952014-07-22 22:58:42 +02001235 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001236 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001237 BlockDriverState *bs1, *bs2;
Eric Blake033d9fc2017-10-11 22:47:16 -05001238 int64_t total_size1, total_size2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001239 uint8_t *buf1 = NULL, *buf2 = NULL;
Eric Blake31826642017-10-11 22:47:08 -05001240 int64_t pnum1, pnum2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001241 int allocated1, allocated2;
1242 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1243 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001244 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001245 bool writethrough;
Eric Blake033d9fc2017-10-11 22:47:16 -05001246 int64_t total_size;
1247 int64_t offset = 0;
1248 int64_t chunk;
Eric Blakedc61cd32017-10-11 22:47:14 -05001249 int c;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001250 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001251 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001252 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001253
Max Reitz40055952014-07-22 22:58:42 +02001254 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001255 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001256 static const struct option long_options[] = {
1257 {"help", no_argument, 0, 'h'},
1258 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001259 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001260 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001261 {0, 0, 0, 0}
1262 };
Fam Zheng335e9932017-05-03 00:35:39 +08001263 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001264 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001265 if (c == -1) {
1266 break;
1267 }
1268 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001269 case ':':
1270 missing_argument(argv[optind - 1]);
1271 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001272 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001273 unrecognized_option(argv[optind - 1]);
1274 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001275 case 'h':
1276 help();
1277 break;
1278 case 'f':
1279 fmt1 = optarg;
1280 break;
1281 case 'F':
1282 fmt2 = optarg;
1283 break;
Max Reitz40055952014-07-22 22:58:42 +02001284 case 'T':
1285 cache = optarg;
1286 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001287 case 'p':
1288 progress = true;
1289 break;
1290 case 'q':
1291 quiet = true;
1292 break;
1293 case 's':
1294 strict = true;
1295 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001296 case 'U':
1297 force_share = true;
1298 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001299 case OPTION_OBJECT: {
1300 QemuOpts *opts;
1301 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1302 optarg, true);
1303 if (!opts) {
1304 ret = 2;
1305 goto out4;
1306 }
1307 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001308 case OPTION_IMAGE_OPTS:
1309 image_opts = true;
1310 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001311 }
1312 }
1313
1314 /* Progress is not shown in Quiet mode */
1315 if (quiet) {
1316 progress = false;
1317 }
1318
1319
Kevin Wolffc11eb22013-08-05 10:53:04 +02001320 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001321 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001322 }
1323 filename1 = argv[optind++];
1324 filename2 = argv[optind++];
1325
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001326 if (qemu_opts_foreach(&qemu_object_opts,
1327 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001328 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001329 ret = 2;
1330 goto out4;
1331 }
1332
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001333 /* Initialize before goto out */
1334 qemu_progress_init(progress, 2.0);
1335
Kevin Wolfce099542016-03-15 13:01:04 +01001336 flags = 0;
1337 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001338 if (ret < 0) {
1339 error_report("Invalid source cache option: %s", cache);
1340 ret = 2;
1341 goto out3;
1342 }
1343
Fam Zheng335e9932017-05-03 00:35:39 +08001344 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1345 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001346 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001347 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001348 goto out3;
1349 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001350
Fam Zheng335e9932017-05-03 00:35:39 +08001351 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1352 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001353 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001354 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001355 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001356 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001357 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001358 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001359
Max Reitzf1d3cd72015-02-05 13:58:18 -05001360 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1361 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
Eric Blake033d9fc2017-10-11 22:47:16 -05001362 total_size1 = blk_getlength(blk1);
1363 if (total_size1 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001364 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001365 filename1, strerror(-total_size1));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001366 ret = 4;
1367 goto out;
1368 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001369 total_size2 = blk_getlength(blk2);
1370 if (total_size2 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001371 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001372 filename2, strerror(-total_size2));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001373 ret = 4;
1374 goto out;
1375 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001376 total_size = MIN(total_size1, total_size2);
1377 progress_base = MAX(total_size1, total_size2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001378
1379 qemu_progress_print(0, 100);
1380
Eric Blake033d9fc2017-10-11 22:47:16 -05001381 if (strict && total_size1 != total_size2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001382 ret = 1;
1383 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1384 goto out;
1385 }
1386
Eric Blake033d9fc2017-10-11 22:47:16 -05001387 while (offset < total_size) {
Eric Blake31826642017-10-11 22:47:08 -05001388 int status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001389
Eric Blake033d9fc2017-10-11 22:47:16 -05001390 status1 = bdrv_block_status_above(bs1, NULL, offset,
1391 total_size1 - offset, &pnum1, NULL,
1392 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001393 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001394 ret = 3;
1395 error_report("Sector allocation test failed for %s", filename1);
1396 goto out;
1397 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001398 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001399
Eric Blake033d9fc2017-10-11 22:47:16 -05001400 status2 = bdrv_block_status_above(bs2, NULL, offset,
1401 total_size2 - offset, &pnum2, NULL,
1402 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001403 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001404 ret = 3;
1405 error_report("Sector allocation test failed for %s", filename2);
1406 goto out;
1407 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001408 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
Eric Blake7daddc62017-10-11 22:47:09 -05001409
1410 assert(pnum1 && pnum2);
Eric Blake033d9fc2017-10-11 22:47:16 -05001411 chunk = MIN(pnum1, pnum2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001412
Fam Zheng25ad8e62016-01-13 16:37:41 +08001413 if (strict) {
Eric Blake31826642017-10-11 22:47:08 -05001414 if (status1 != status2) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001415 ret = 1;
1416 qprintf(quiet, "Strict mode: Offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001417 " block status mismatch!\n", offset);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001418 goto out;
1419 }
1420 }
1421 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
Eric Blake7daddc62017-10-11 22:47:09 -05001422 /* nothing to do */
Fam Zheng25ad8e62016-01-13 16:37:41 +08001423 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001424 if (allocated1) {
Eric Blakedc61cd32017-10-11 22:47:14 -05001425 int64_t pnum;
1426
Eric Blake033d9fc2017-10-11 22:47:16 -05001427 chunk = MIN(chunk, IO_BUF_SIZE);
1428 ret = blk_pread(blk1, offset, buf1, chunk);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001429 if (ret < 0) {
1430 error_report("Error while reading offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001431 " of %s: %s",
1432 offset, filename1, strerror(-ret));
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001433 ret = 4;
1434 goto out;
1435 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001436 ret = blk_pread(blk2, offset, buf2, chunk);
1437 if (ret < 0) {
1438 error_report("Error while reading offset %" PRId64
1439 " of %s: %s",
1440 offset, filename2, strerror(-ret));
1441 ret = 4;
1442 goto out;
1443 }
1444 ret = compare_buffers(buf1, buf2, chunk, &pnum);
1445 if (ret || pnum != chunk) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001446 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blake033d9fc2017-10-11 22:47:16 -05001447 offset + (ret ? 0 : pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001448 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001449 goto out;
1450 }
1451 }
1452 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001453 chunk = MIN(chunk, IO_BUF_SIZE);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001454 if (allocated1) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001455 ret = check_empty_sectors(blk1, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001456 filename1, buf1, quiet);
1457 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001458 ret = check_empty_sectors(blk2, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001459 filename2, buf1, quiet);
1460 }
1461 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001462 goto out;
1463 }
1464 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001465 offset += chunk;
1466 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001467 }
1468
Eric Blake033d9fc2017-10-11 22:47:16 -05001469 if (total_size1 != total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001470 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001471 const char *filename_over;
1472
1473 qprintf(quiet, "Warning: Image size mismatch!\n");
Eric Blake033d9fc2017-10-11 22:47:16 -05001474 if (total_size1 > total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001475 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001476 filename_over = filename1;
1477 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001478 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001479 filename_over = filename2;
1480 }
1481
Eric Blake033d9fc2017-10-11 22:47:16 -05001482 while (offset < progress_base) {
1483 ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset,
1484 progress_base - offset, &chunk,
1485 NULL, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001486 if (ret < 0) {
1487 ret = 3;
1488 error_report("Sector allocation test failed for %s",
1489 filename_over);
1490 goto out;
1491
1492 }
Eric Blake391cb1a2017-10-11 22:47:10 -05001493 if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001494 chunk = MIN(chunk, IO_BUF_SIZE);
1495 ret = check_empty_sectors(blk_over, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001496 filename_over, buf1, quiet);
1497 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001498 goto out;
1499 }
1500 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001501 offset += chunk;
1502 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001503 }
1504 }
1505
1506 qprintf(quiet, "Images are identical.\n");
1507 ret = 0;
1508
1509out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001510 qemu_vfree(buf1);
1511 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001512 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001513out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001514 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001515out3:
1516 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001517out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001518 return ret;
1519}
1520
Kevin Wolf690c7302015-03-19 13:33:32 +01001521enum ImgConvertBlockStatus {
1522 BLK_DATA,
1523 BLK_ZERO,
1524 BLK_BACKING_FILE,
1525};
1526
Peter Lieven2d9187b2017-02-28 13:40:07 +01001527#define MAX_COROUTINES 16
1528
Kevin Wolf690c7302015-03-19 13:33:32 +01001529typedef struct ImgConvertState {
1530 BlockBackend **src;
1531 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001532 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001533 int64_t total_sectors;
1534 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001535 int64_t allocated_done;
1536 int64_t sector_num;
1537 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001538 enum ImgConvertBlockStatus status;
1539 int64_t sector_next_status;
1540 BlockBackend *target;
1541 bool has_zero_init;
1542 bool compressed;
1543 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001544 bool wr_in_order;
Kevin Wolf690c7302015-03-19 13:33:32 +01001545 int min_sparse;
1546 size_t cluster_sectors;
1547 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001548 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001549 int running_coroutines;
1550 Coroutine *co[MAX_COROUTINES];
1551 int64_t wait_sector_num[MAX_COROUTINES];
1552 CoMutex lock;
1553 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001554} ImgConvertState;
1555
Peter Lieven2d9187b2017-02-28 13:40:07 +01001556static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1557 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001558{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001559 *src_cur = 0;
1560 *src_cur_offset = 0;
1561 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1562 *src_cur_offset += s->src_sectors[*src_cur];
1563 (*src_cur)++;
1564 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001565 }
1566}
1567
1568static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1569{
Eric Blake31826642017-10-11 22:47:08 -05001570 int64_t src_cur_offset;
1571 int ret, n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001572
Peter Lieven2d9187b2017-02-28 13:40:07 +01001573 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001574
1575 assert(s->total_sectors > sector_num);
1576 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1577
1578 if (s->sector_next_status <= sector_num) {
Eric Blake31826642017-10-11 22:47:08 -05001579 int64_t count = n * BDRV_SECTOR_SIZE;
1580
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001581 if (s->target_has_backing) {
Eric Blake237d78f2017-10-11 22:47:03 -05001582
1583 ret = bdrv_block_status(blk_bs(s->src[src_cur]),
1584 (sector_num - src_cur_offset) *
1585 BDRV_SECTOR_SIZE,
1586 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001587 } else {
Eric Blake31826642017-10-11 22:47:08 -05001588 ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL,
1589 (sector_num - src_cur_offset) *
1590 BDRV_SECTOR_SIZE,
1591 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001592 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001593 if (ret < 0) {
1594 return ret;
1595 }
Eric Blake31826642017-10-11 22:47:08 -05001596 n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
Kevin Wolf690c7302015-03-19 13:33:32 +01001597
1598 if (ret & BDRV_BLOCK_ZERO) {
1599 s->status = BLK_ZERO;
1600 } else if (ret & BDRV_BLOCK_DATA) {
1601 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001602 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001603 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001604 }
1605
1606 s->sector_next_status = sector_num + n;
1607 }
1608
1609 n = MIN(n, s->sector_next_status - sector_num);
1610 if (s->status == BLK_DATA) {
1611 n = MIN(n, s->buf_sectors);
1612 }
1613
1614 /* We need to write complete clusters for compressed images, so if an
1615 * unallocated area is shorter than that, we must consider the whole
1616 * cluster allocated. */
1617 if (s->compressed) {
1618 if (n < s->cluster_sectors) {
1619 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1620 s->status = BLK_DATA;
1621 } else {
1622 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1623 }
1624 }
1625
1626 return n;
1627}
1628
Peter Lieven2d9187b2017-02-28 13:40:07 +01001629static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1630 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001631{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001632 int n, ret;
1633 QEMUIOVector qiov;
1634 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001635
Kevin Wolf690c7302015-03-19 13:33:32 +01001636 assert(nb_sectors <= s->buf_sectors);
1637 while (nb_sectors > 0) {
1638 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001639 int src_cur;
1640 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001641
1642 /* In the case of compression with multiple source files, we can get a
1643 * nb_sectors that spreads into the next part. So we must be able to
1644 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001645 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1646 blk = s->src[src_cur];
1647 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001648
Peter Lieven2d9187b2017-02-28 13:40:07 +01001649 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1650 iov.iov_base = buf;
1651 iov.iov_len = n << BDRV_SECTOR_BITS;
1652 qemu_iovec_init_external(&qiov, &iov, 1);
1653
1654 ret = blk_co_preadv(
1655 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1656 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001657 if (ret < 0) {
1658 return ret;
1659 }
1660
1661 sector_num += n;
1662 nb_sectors -= n;
1663 buf += n * BDRV_SECTOR_SIZE;
1664 }
1665
1666 return 0;
1667}
1668
Peter Lieven2d9187b2017-02-28 13:40:07 +01001669
1670static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1671 int nb_sectors, uint8_t *buf,
1672 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001673{
1674 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001675 QEMUIOVector qiov;
1676 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001677
1678 while (nb_sectors > 0) {
1679 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001680 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1681
Peter Lieven2d9187b2017-02-28 13:40:07 +01001682 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001683 case BLK_BACKING_FILE:
1684 /* If we have a backing file, leave clusters unallocated that are
1685 * unallocated in the source image, so that the backing file is
1686 * visible at the respective offset. */
1687 assert(s->target_has_backing);
1688 break;
1689
1690 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001691 /* If we're told to keep the target fully allocated (-S 0) or there
1692 * is real non-zero data, we must write it. Otherwise we can treat
1693 * it as zero sectors.
1694 * Compressed clusters need to be written as a whole, so in that
1695 * case we can only save the write if the buffer is completely
1696 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001697 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001698 (!s->compressed &&
1699 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1700 (s->compressed &&
1701 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001702 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001703 iov.iov_base = buf;
1704 iov.iov_len = n << BDRV_SECTOR_BITS;
1705 qemu_iovec_init_external(&qiov, &iov, 1);
1706
1707 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
Lidong Chendb933fb2017-04-27 10:58:27 +08001708 n << BDRV_SECTOR_BITS, &qiov, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001709 if (ret < 0) {
1710 return ret;
1711 }
1712 break;
1713 }
1714 /* fall-through */
1715
1716 case BLK_ZERO:
1717 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001718 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001719 break;
1720 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001721 ret = blk_co_pwrite_zeroes(s->target,
1722 sector_num << BDRV_SECTOR_BITS,
1723 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001724 if (ret < 0) {
1725 return ret;
1726 }
1727 break;
1728 }
1729
1730 sector_num += n;
1731 nb_sectors -= n;
1732 buf += n * BDRV_SECTOR_SIZE;
1733 }
1734
1735 return 0;
1736}
1737
Peter Lieven2d9187b2017-02-28 13:40:07 +01001738static void coroutine_fn convert_co_do_copy(void *opaque)
1739{
1740 ImgConvertState *s = opaque;
1741 uint8_t *buf = NULL;
1742 int ret, i;
1743 int index = -1;
1744
1745 for (i = 0; i < s->num_coroutines; i++) {
1746 if (s->co[i] == qemu_coroutine_self()) {
1747 index = i;
1748 break;
1749 }
1750 }
1751 assert(index >= 0);
1752
1753 s->running_coroutines++;
1754 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1755
1756 while (1) {
1757 int n;
1758 int64_t sector_num;
1759 enum ImgConvertBlockStatus status;
1760
1761 qemu_co_mutex_lock(&s->lock);
1762 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1763 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001764 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001765 }
1766 n = convert_iteration_sectors(s, s->sector_num);
1767 if (n < 0) {
1768 qemu_co_mutex_unlock(&s->lock);
1769 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001770 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001771 }
1772 /* save current sector and allocation status to local variables */
1773 sector_num = s->sector_num;
1774 status = s->status;
1775 if (!s->min_sparse && s->status == BLK_ZERO) {
1776 n = MIN(n, s->buf_sectors);
1777 }
1778 /* increment global sector counter so that other coroutines can
1779 * already continue reading beyond this request */
1780 s->sector_num += n;
1781 qemu_co_mutex_unlock(&s->lock);
1782
1783 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1784 s->allocated_done += n;
1785 qemu_progress_print(100.0 * s->allocated_done /
1786 s->allocated_sectors, 0);
1787 }
1788
1789 if (status == BLK_DATA) {
1790 ret = convert_co_read(s, sector_num, n, buf);
1791 if (ret < 0) {
1792 error_report("error while reading sector %" PRId64
1793 ": %s", sector_num, strerror(-ret));
1794 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001795 }
1796 } else if (!s->min_sparse && status == BLK_ZERO) {
1797 status = BLK_DATA;
1798 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1799 }
1800
1801 if (s->wr_in_order) {
1802 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03001803 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001804 s->wait_sector_num[index] = sector_num;
1805 qemu_coroutine_yield();
1806 }
1807 s->wait_sector_num[index] = -1;
1808 }
1809
Anton Nefedovb91127e2017-04-26 11:33:15 +03001810 if (s->ret == -EINPROGRESS) {
1811 ret = convert_co_write(s, sector_num, n, buf, status);
1812 if (ret < 0) {
1813 error_report("error while writing sector %" PRId64
1814 ": %s", sector_num, strerror(-ret));
1815 s->ret = ret;
1816 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001817 }
1818
1819 if (s->wr_in_order) {
1820 /* reenter the coroutine that might have waited
1821 * for this write to complete */
1822 s->wr_offs = sector_num + n;
1823 for (i = 0; i < s->num_coroutines; i++) {
1824 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1825 /*
1826 * A -> B -> A cannot occur because A has
1827 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1828 * B will never enter A during this time window.
1829 */
1830 qemu_coroutine_enter(s->co[i]);
1831 break;
1832 }
1833 }
1834 }
1835 }
1836
Peter Lieven2d9187b2017-02-28 13:40:07 +01001837 qemu_vfree(buf);
1838 s->co[index] = NULL;
1839 s->running_coroutines--;
1840 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1841 /* the convert job finished successfully */
1842 s->ret = 0;
1843 }
1844}
1845
Kevin Wolf690c7302015-03-19 13:33:32 +01001846static int convert_do_copy(ImgConvertState *s)
1847{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001848 int ret, i, n;
1849 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001850
1851 /* Check whether we have zero initialisation or can get it efficiently */
1852 s->has_zero_init = s->min_sparse && !s->target_has_backing
1853 ? bdrv_has_zero_init(blk_bs(s->target))
1854 : false;
1855
1856 if (!s->has_zero_init && !s->target_has_backing &&
1857 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1858 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001859 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001860 if (ret == 0) {
1861 s->has_zero_init = true;
1862 }
1863 }
1864
1865 /* Allocate buffer for copied data. For compressed images, only one cluster
1866 * can be copied at a time. */
1867 if (s->compressed) {
1868 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1869 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001870 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001871 }
1872 s->buf_sectors = s->cluster_sectors;
1873 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001874
Kevin Wolf690c7302015-03-19 13:33:32 +01001875 while (sector_num < s->total_sectors) {
1876 n = convert_iteration_sectors(s, sector_num);
1877 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001878 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001879 }
Max Reitzaad15de2016-03-24 23:33:57 +01001880 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1881 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001882 s->allocated_sectors += n;
1883 }
1884 sector_num += n;
1885 }
1886
1887 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001888 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001889 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001890
Peter Lieven2d9187b2017-02-28 13:40:07 +01001891 qemu_co_mutex_init(&s->lock);
1892 for (i = 0; i < s->num_coroutines; i++) {
1893 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1894 s->wait_sector_num[i] = -1;
1895 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001896 }
1897
Anton Nefedovb91127e2017-04-26 11:33:15 +03001898 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001899 main_loop_wait(false);
1900 }
1901
1902 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001903 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001904 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001905 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001906 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001907 }
1908 }
1909
Peter Lieven2d9187b2017-02-28 13:40:07 +01001910 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001911}
1912
bellardea2384d2004-08-01 21:59:26 +00001913static int img_convert(int argc, char **argv)
1914{
Peter Lieven9fd77f92017-04-21 11:11:55 +02001915 int c, bs_i, flags, src_flags = 0;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001916 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02001917 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1918 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001919 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00001920 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001921 BlockDriverState *out_bs;
1922 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08001923 QemuOptsList *create_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001924 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02001925 Error *local_err = NULL;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001926 bool writethrough, src_writethrough, quiet = false, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001927 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001928 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08001929 bool force_share = false;
bellardea2384d2004-08-01 21:59:26 +00001930
Peter Lieven9fd77f92017-04-21 11:11:55 +02001931 ImgConvertState s = (ImgConvertState) {
1932 /* Need at least 4k of zeros for sparse detection */
1933 .min_sparse = 8,
1934 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1935 .wr_in_order = true,
1936 .num_coroutines = 8,
1937 };
1938
bellardea2384d2004-08-01 21:59:26 +00001939 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001940 static const struct option long_options[] = {
1941 {"help", no_argument, 0, 'h'},
1942 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001943 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001944 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001945 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001946 {0, 0, 0, 0}
1947 };
Daniel P. Berrange6b4df542017-07-04 13:30:09 +01001948 c = getopt_long(argc, argv, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001949 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001950 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001951 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001952 }
bellardea2384d2004-08-01 21:59:26 +00001953 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001954 case ':':
1955 missing_argument(argv[optind - 1]);
1956 break;
Jes Sorensenef873942010-12-06 15:25:40 +01001957 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001958 unrecognized_option(argv[optind - 1]);
1959 break;
bellardea2384d2004-08-01 21:59:26 +00001960 case 'h':
1961 help();
1962 break;
1963 case 'f':
1964 fmt = optarg;
1965 break;
1966 case 'O':
1967 out_fmt = optarg;
1968 break;
thsf58c7b32008-06-05 21:53:49 +00001969 case 'B':
1970 out_baseimg = optarg;
1971 break;
bellardea2384d2004-08-01 21:59:26 +00001972 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02001973 s.compressed = true;
bellardea2384d2004-08-01 21:59:26 +00001974 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001975 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001976 if (!is_valid_option_list(optarg)) {
1977 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001978 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001979 }
1980 if (!options) {
1981 options = g_strdup(optarg);
1982 } else {
1983 char *old_options = options;
1984 options = g_strdup_printf("%s,%s", options, optarg);
1985 g_free(old_options);
1986 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001987 break;
edison51ef6722010-09-21 19:58:41 -07001988 case 's':
1989 snapshot_name = optarg;
1990 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001991 case 'l':
1992 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01001993 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
1994 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08001995 if (!sn_opts) {
1996 error_report("Failed in parsing snapshot param '%s'",
1997 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001998 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08001999 }
2000 } else {
2001 snapshot_name = optarg;
2002 }
2003 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002004 case 'S':
2005 {
2006 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002007
2008 sval = cvtnum(optarg);
2009 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02002010 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002011 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002012 }
2013
Peter Lieven9fd77f92017-04-21 11:11:55 +02002014 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002015 break;
2016 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002017 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002018 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002019 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002020 case 't':
2021 cache = optarg;
2022 break;
Max Reitz40055952014-07-22 22:58:42 +02002023 case 'T':
2024 src_cache = optarg;
2025 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002026 case 'q':
2027 quiet = true;
2028 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002029 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002030 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002031 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002032 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002033 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2034 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002035 error_report("Invalid number of coroutines. Allowed number of"
2036 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002037 goto fail_getopt;
2038 }
2039 break;
2040 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002041 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002042 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002043 case 'U':
2044 force_share = true;
2045 break;
Max Reitz3258b912017-04-26 15:46:47 +02002046 case OPTION_OBJECT: {
2047 QemuOpts *object_opts;
2048 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2049 optarg, true);
2050 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002051 goto fail_getopt;
2052 }
2053 break;
Max Reitz3258b912017-04-26 15:46:47 +02002054 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002055 case OPTION_IMAGE_OPTS:
2056 image_opts = true;
2057 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002058 case OPTION_TARGET_IMAGE_OPTS:
2059 tgt_image_opts = true;
2060 break;
bellardea2384d2004-08-01 21:59:26 +00002061 }
2062 }
ths3b46e622007-09-17 08:09:54 +00002063
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002064 if (!out_fmt && !tgt_image_opts) {
2065 out_fmt = "raw";
2066 }
2067
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002068 if (qemu_opts_foreach(&qemu_object_opts,
2069 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002070 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002071 goto fail_getopt;
2072 }
2073
Peter Lieven9fd77f92017-04-21 11:11:55 +02002074 if (!s.wr_in_order && s.compressed) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002075 error_report("Out of order write and compress are mutually exclusive");
Peter Lieven9fd77f92017-04-21 11:11:55 +02002076 goto fail_getopt;
2077 }
2078
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002079 if (tgt_image_opts && !skip_create) {
2080 error_report("--target-image-opts requires use of -n flag");
2081 goto fail_getopt;
2082 }
2083
Peter Lieven9fd77f92017-04-21 11:11:55 +02002084 s.src_num = argc - optind - 1;
2085 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2086
2087 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002088 if (out_fmt) {
2089 ret = print_block_option_help(out_filename, out_fmt);
2090 goto fail_getopt;
2091 } else {
2092 error_report("Option help requires a format be specified");
2093 goto fail_getopt;
2094 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002095 }
2096
2097 if (s.src_num < 1) {
2098 error_report("Must specify image file name");
2099 goto fail_getopt;
2100 }
2101
2102
Peter Lieven9fd77f92017-04-21 11:11:55 +02002103 /* ret is still -EINVAL until here */
2104 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2105 if (ret < 0) {
2106 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002107 goto fail_getopt;
2108 }
2109
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002110 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002111 if (quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002112 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002113 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002114 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002115 qemu_progress_print(0, 100);
2116
Peter Lieven9fd77f92017-04-21 11:11:55 +02002117 s.src = g_new0(BlockBackend *, s.src_num);
2118 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002119
Peter Lieven9fd77f92017-04-21 11:11:55 +02002120 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2121 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Fam Zheng335e9932017-05-03 00:35:39 +08002122 fmt, src_flags, src_writethrough, quiet,
2123 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002124 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002125 ret = -1;
2126 goto out;
2127 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002128 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2129 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002130 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002131 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002132 ret = -1;
2133 goto out;
2134 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002135 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002136 }
bellardea2384d2004-08-01 21:59:26 +00002137
Wenchao Xiaef806542013-12-04 17:10:57 +08002138 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002139 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002140 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2141 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2142 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002143 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002144 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002145 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002146 ret = -1;
2147 goto out;
2148 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002149
Peter Lieven9fd77f92017-04-21 11:11:55 +02002150 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2151 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002152 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002153 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002154 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002155 ret = -1;
2156 goto out;
edison51ef6722010-09-21 19:58:41 -07002157 }
2158
Max Reitz2e024cd2015-02-11 09:58:46 -05002159 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002160 /* Find driver and parse its options */
2161 drv = bdrv_find_format(out_fmt);
2162 if (!drv) {
2163 error_report("Unknown file format '%s'", out_fmt);
2164 ret = -1;
2165 goto out;
2166 }
2167
2168 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2169 if (!proto_drv) {
2170 error_report_err(local_err);
2171 ret = -1;
2172 goto out;
2173 }
2174
Max Reitz2e024cd2015-02-11 09:58:46 -05002175 if (!drv->create_opts) {
2176 error_report("Format driver '%s' does not support image creation",
2177 drv->format_name);
2178 ret = -1;
2179 goto out;
2180 }
Max Reitzf75613c2014-12-02 18:32:46 +01002181
Max Reitz2e024cd2015-02-11 09:58:46 -05002182 if (!proto_drv->create_opts) {
2183 error_report("Protocol driver '%s' does not support image creation",
2184 proto_drv->format_name);
2185 ret = -1;
2186 goto out;
2187 }
Max Reitzf75613c2014-12-02 18:32:46 +01002188
Max Reitz2e024cd2015-02-11 09:58:46 -05002189 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2190 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002191
Max Reitz2e024cd2015-02-11 09:58:46 -05002192 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002193 if (options) {
2194 qemu_opts_do_parse(opts, options, NULL, &local_err);
2195 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002196 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002197 ret = -1;
2198 goto out;
2199 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002200 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002201
Peter Lieven9fd77f92017-04-21 11:11:55 +02002202 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002203 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002204 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2205 if (ret < 0) {
2206 goto out;
2207 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002208 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002209
Kevin Wolfa18953f2010-10-14 15:46:04 +02002210 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002211 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002212 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002213 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002214 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002215 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002216
Max Reitz48758a82017-04-26 15:46:48 +02002217 if (s.src_num > 1 && out_baseimg) {
2218 error_report("Having a backing file for the target makes no sense when "
2219 "concatenating multiple input images");
2220 ret = -1;
2221 goto out;
2222 }
2223
Kevin Wolfefa84d42009-05-18 16:42:12 +02002224 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002225 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002226 bool encryption =
2227 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002228 const char *encryptfmt =
2229 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
Chunyan Liu83d05212014-06-05 17:20:51 +08002230 const char *preallocation =
2231 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002232
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002233 if (drv && !drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002234 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002235 ret = -1;
2236 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002237 }
2238
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002239 if (encryption || encryptfmt) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002240 error_report("Compression and encryption not supported at "
2241 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002242 ret = -1;
2243 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002244 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002245
Chunyan Liu83d05212014-06-05 17:20:51 +08002246 if (preallocation
2247 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002248 {
2249 error_report("Compression and preallocation not supported at "
2250 "the same time");
2251 ret = -1;
2252 goto out;
2253 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002254 }
2255
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002256 if (!skip_create) {
2257 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002258 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002259 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002260 error_reportf_err(local_err, "%s: error while converting %s: ",
2261 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002262 goto out;
bellardea2384d2004-08-01 21:59:26 +00002263 }
2264 }
ths3b46e622007-09-17 08:09:54 +00002265
Peter Lieven9fd77f92017-04-21 11:11:55 +02002266 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002267 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002268 if (ret < 0) {
2269 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002270 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002271 }
2272
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002273 if (skip_create) {
2274 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2275 flags, writethrough, quiet, false);
2276 } else {
2277 /* TODO ultimately we should allow --target-image-opts
2278 * to be used even when -n is not given.
2279 * That has to wait for bdrv_create to be improved
2280 * to allow filenames in option syntax
2281 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01002282 s.target = img_open_new_file(out_filename, opts, out_fmt,
2283 flags, writethrough, quiet, false);
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002284 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002285 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002286 ret = -1;
2287 goto out;
2288 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002289 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002290
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002291 if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2292 error_report("Compression not supported for this file format");
2293 ret = -1;
2294 goto out;
2295 }
2296
Eric Blake5def6b82016-06-23 16:37:19 -06002297 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002298 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2299 * as maximum. */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002300 s.buf_sectors = MIN(32768,
2301 MAX(s.buf_sectors,
2302 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2303 out_bs->bl.pdiscard_alignment >>
2304 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002305
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002306 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002307 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002308 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002309 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002310 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002311 ret = -1;
2312 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002313 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002314 error_report("output file is smaller than input file");
2315 ret = -1;
2316 goto out;
2317 }
2318 }
2319
Peter Lieven24f833c2013-11-27 11:07:07 +01002320 ret = bdrv_get_info(out_bs, &bdi);
2321 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002322 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002323 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002324 goto out;
2325 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002326 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002327 s.compressed = s.compressed || bdi.needs_compressed_writes;
2328 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Peter Lieven24f833c2013-11-27 11:07:07 +01002329 }
2330
Peter Lieven9fd77f92017-04-21 11:11:55 +02002331 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002332out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002333 if (!ret) {
2334 qemu_progress_print(100, 0);
2335 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002336 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002337 qemu_opts_del(opts);
2338 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002339 qemu_opts_del(sn_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002340 blk_unref(s.target);
2341 if (s.src) {
2342 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2343 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002344 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002345 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002346 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002347 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002348fail_getopt:
2349 g_free(options);
2350
Peter Lieven9fd77f92017-04-21 11:11:55 +02002351 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002352}
2353
bellard57d1a2b2004-08-03 21:15:11 +00002354
bellardfaea38e2006-08-05 21:31:00 +00002355static void dump_snapshots(BlockDriverState *bs)
2356{
2357 QEMUSnapshotInfo *sn_tab, *sn;
2358 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002359
2360 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2361 if (nb_sns <= 0)
2362 return;
2363 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002364 bdrv_snapshot_dump(fprintf, stdout, NULL);
2365 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002366 for(i = 0; i < nb_sns; i++) {
2367 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002368 bdrv_snapshot_dump(fprintf, stdout, sn);
2369 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002370 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002371 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002372}
2373
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002374static void dump_json_image_info_list(ImageInfoList *list)
2375{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002376 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002377 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002378 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002379
2380 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2381 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002382 str = qobject_to_json_pretty(obj);
2383 assert(str != NULL);
2384 printf("%s\n", qstring_get_str(str));
2385 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002386 visit_free(v);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002387 QDECREF(str);
2388}
2389
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002390static void dump_json_image_info(ImageInfo *info)
2391{
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002392 QString *str;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002393 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002394 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002395
2396 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2397 visit_complete(v, &obj);
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002398 str = qobject_to_json_pretty(obj);
2399 assert(str != NULL);
2400 printf("%s\n", qstring_get_str(str));
2401 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002402 visit_free(v);
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002403 QDECREF(str);
2404}
2405
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002406static void dump_human_image_info_list(ImageInfoList *list)
2407{
2408 ImageInfoList *elem;
2409 bool delim = false;
2410
2411 for (elem = list; elem; elem = elem->next) {
2412 if (delim) {
2413 printf("\n");
2414 }
2415 delim = true;
2416
Wenchao Xia5b917042013-05-25 11:09:45 +08002417 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002418 }
2419}
2420
2421static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2422{
2423 return strcmp(a, b) == 0;
2424}
2425
2426/**
2427 * Open an image file chain and return an ImageInfoList
2428 *
2429 * @filename: topmost image filename
2430 * @fmt: topmost image format (may be NULL to autodetect)
2431 * @chain: true - enumerate entire backing file chain
2432 * false - only topmost image file
2433 *
2434 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2435 * image file. If there was an error a message will have been printed to
2436 * stderr.
2437 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002438static ImageInfoList *collect_image_info_list(bool image_opts,
2439 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002440 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002441 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002442{
2443 ImageInfoList *head = NULL;
2444 ImageInfoList **last = &head;
2445 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002446 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002447
2448 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2449
2450 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002451 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002452 BlockDriverState *bs;
2453 ImageInfo *info;
2454 ImageInfoList *elem;
2455
2456 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2457 error_report("Backing file '%s' creates an infinite loop.",
2458 filename);
2459 goto err;
2460 }
2461 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2462
Max Reitzefaa7c42016-03-16 19:54:38 +01002463 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002464 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2465 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002466 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002467 goto err;
2468 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002469 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002470
Wenchao Xia43526ec2013-06-06 12:27:58 +08002471 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002472 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002473 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002474 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002475 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002476 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002477
2478 elem = g_new0(ImageInfoList, 1);
2479 elem->value = info;
2480 *last = elem;
2481 last = &elem->next;
2482
Markus Armbruster26f54e92014-10-07 13:59:04 +02002483 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002484
2485 filename = fmt = NULL;
2486 if (chain) {
2487 if (info->has_full_backing_filename) {
2488 filename = info->full_backing_filename;
2489 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002490 error_report("Could not determine absolute backing filename,"
2491 " but backing filename '%s' present",
2492 info->backing_filename);
2493 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002494 }
2495 if (info->has_backing_filename_format) {
2496 fmt = info->backing_filename_format;
2497 }
2498 }
2499 }
2500 g_hash_table_destroy(filenames);
2501 return head;
2502
2503err:
2504 qapi_free_ImageInfoList(head);
2505 g_hash_table_destroy(filenames);
2506 return NULL;
2507}
2508
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002509static int img_info(int argc, char **argv)
2510{
2511 int c;
2512 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002513 bool chain = false;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002514 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002515 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002516 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002517 bool force_share = false;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002518
bellardea2384d2004-08-01 21:59:26 +00002519 fmt = NULL;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002520 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002521 for(;;) {
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002522 int option_index = 0;
2523 static const struct option long_options[] = {
2524 {"help", no_argument, 0, 'h'},
2525 {"format", required_argument, 0, 'f'},
2526 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002527 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002528 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002529 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002530 {"force-share", no_argument, 0, 'U'},
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002531 {0, 0, 0, 0}
2532 };
Fam Zheng335e9932017-05-03 00:35:39 +08002533 c = getopt_long(argc, argv, ":f:hU",
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002534 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002535 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002536 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002537 }
bellardea2384d2004-08-01 21:59:26 +00002538 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002539 case ':':
2540 missing_argument(argv[optind - 1]);
2541 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002542 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002543 unrecognized_option(argv[optind - 1]);
2544 break;
bellardea2384d2004-08-01 21:59:26 +00002545 case 'h':
2546 help();
2547 break;
2548 case 'f':
2549 fmt = optarg;
2550 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002551 case 'U':
2552 force_share = true;
2553 break;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002554 case OPTION_OUTPUT:
2555 output = optarg;
2556 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002557 case OPTION_BACKING_CHAIN:
2558 chain = true;
2559 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002560 case OPTION_OBJECT: {
2561 QemuOpts *opts;
2562 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2563 optarg, true);
2564 if (!opts) {
2565 return 1;
2566 }
2567 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002568 case OPTION_IMAGE_OPTS:
2569 image_opts = true;
2570 break;
bellardea2384d2004-08-01 21:59:26 +00002571 }
2572 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002573 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002574 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002575 }
bellardea2384d2004-08-01 21:59:26 +00002576 filename = argv[optind++];
2577
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002578 if (output && !strcmp(output, "json")) {
2579 output_format = OFORMAT_JSON;
2580 } else if (output && !strcmp(output, "human")) {
2581 output_format = OFORMAT_HUMAN;
2582 } else if (output) {
2583 error_report("--output must be used with human or json as argument.");
2584 return 1;
2585 }
2586
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002587 if (qemu_opts_foreach(&qemu_object_opts,
2588 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002589 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002590 return 1;
2591 }
2592
Fam Zheng335e9932017-05-03 00:35:39 +08002593 list = collect_image_info_list(image_opts, filename, fmt, chain,
2594 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002595 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002596 return 1;
2597 }
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002598
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002599 switch (output_format) {
2600 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002601 dump_human_image_info_list(list);
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002602 break;
2603 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002604 if (chain) {
2605 dump_json_image_info_list(list);
2606 } else {
2607 dump_json_image_info(list->value);
2608 }
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002609 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002610 }
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002611
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002612 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002613 return 0;
2614}
2615
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002616static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2617 MapEntry *next)
2618{
2619 switch (output_format) {
2620 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002621 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002622 error_report("File contains external, encrypted or compressed clusters.");
2623 exit(1);
2624 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002625 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002626 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002627 e->start, e->length,
2628 e->has_offset ? e->offset : 0,
2629 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002630 }
2631 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2632 * Modify the flags here to allow more coalescing.
2633 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002634 if (next && (!next->data || next->zero)) {
2635 next->data = false;
2636 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002637 }
2638 break;
2639 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002640 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2641 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002642 (e->start == 0 ? "[" : ",\n"),
2643 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002644 e->zero ? "true" : "false",
2645 e->data ? "true" : "false");
2646 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002647 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002648 }
2649 putchar('}');
2650
2651 if (!next) {
2652 printf("]\n");
2653 }
2654 break;
2655 }
2656}
2657
Eric Blake5e344dd2017-10-11 22:47:02 -05002658static int get_block_status(BlockDriverState *bs, int64_t offset,
2659 int64_t bytes, MapEntry *e)
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002660{
Eric Blake237d78f2017-10-11 22:47:03 -05002661 int ret;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002662 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002663 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002664 bool has_offset;
Eric Blake237d78f2017-10-11 22:47:03 -05002665 int64_t map;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002666
2667 /* As an optimization, we could cache the current range of unallocated
2668 * clusters in each file of the chain, and avoid querying the same
2669 * range repeatedly.
2670 */
2671
2672 depth = 0;
2673 for (;;) {
Eric Blake237d78f2017-10-11 22:47:03 -05002674 ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002675 if (ret < 0) {
2676 return ret;
2677 }
Eric Blake237d78f2017-10-11 22:47:03 -05002678 assert(bytes);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002679 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2680 break;
2681 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002682 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002683 if (bs == NULL) {
2684 ret = 0;
2685 break;
2686 }
2687
2688 depth++;
2689 }
2690
John Snow28756452016-02-05 13:12:33 -05002691 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2692
2693 *e = (MapEntry) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002694 .start = offset,
Eric Blake237d78f2017-10-11 22:47:03 -05002695 .length = bytes,
John Snow28756452016-02-05 13:12:33 -05002696 .data = !!(ret & BDRV_BLOCK_DATA),
2697 .zero = !!(ret & BDRV_BLOCK_ZERO),
Eric Blake237d78f2017-10-11 22:47:03 -05002698 .offset = map,
John Snow28756452016-02-05 13:12:33 -05002699 .has_offset = has_offset,
2700 .depth = depth,
2701 .has_filename = file && has_offset,
2702 .filename = file && has_offset ? file->filename : NULL,
2703 };
2704
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002705 return 0;
2706}
2707
Fam Zheng16b0d552016-01-26 11:59:02 +08002708static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2709{
2710 if (curr->length == 0) {
2711 return false;
2712 }
2713 if (curr->zero != next->zero ||
2714 curr->data != next->data ||
2715 curr->depth != next->depth ||
2716 curr->has_filename != next->has_filename ||
2717 curr->has_offset != next->has_offset) {
2718 return false;
2719 }
2720 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2721 return false;
2722 }
2723 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2724 return false;
2725 }
2726 return true;
2727}
2728
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002729static int img_map(int argc, char **argv)
2730{
2731 int c;
2732 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002733 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002734 BlockDriverState *bs;
2735 const char *filename, *fmt, *output;
2736 int64_t length;
2737 MapEntry curr = { .length = 0 }, next;
2738 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002739 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002740 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002741
2742 fmt = NULL;
2743 output = NULL;
2744 for (;;) {
2745 int option_index = 0;
2746 static const struct option long_options[] = {
2747 {"help", no_argument, 0, 'h'},
2748 {"format", required_argument, 0, 'f'},
2749 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002750 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002751 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002752 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002753 {0, 0, 0, 0}
2754 };
Fam Zheng335e9932017-05-03 00:35:39 +08002755 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002756 long_options, &option_index);
2757 if (c == -1) {
2758 break;
2759 }
2760 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002761 case ':':
2762 missing_argument(argv[optind - 1]);
2763 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002764 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002765 unrecognized_option(argv[optind - 1]);
2766 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002767 case 'h':
2768 help();
2769 break;
2770 case 'f':
2771 fmt = optarg;
2772 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002773 case 'U':
2774 force_share = true;
2775 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002776 case OPTION_OUTPUT:
2777 output = optarg;
2778 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002779 case OPTION_OBJECT: {
2780 QemuOpts *opts;
2781 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2782 optarg, true);
2783 if (!opts) {
2784 return 1;
2785 }
2786 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002787 case OPTION_IMAGE_OPTS:
2788 image_opts = true;
2789 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002790 }
2791 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002792 if (optind != argc - 1) {
2793 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002794 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002795 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002796
2797 if (output && !strcmp(output, "json")) {
2798 output_format = OFORMAT_JSON;
2799 } else if (output && !strcmp(output, "human")) {
2800 output_format = OFORMAT_HUMAN;
2801 } else if (output) {
2802 error_report("--output must be used with human or json as argument.");
2803 return 1;
2804 }
2805
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002806 if (qemu_opts_foreach(&qemu_object_opts,
2807 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002808 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002809 return 1;
2810 }
2811
Fam Zheng335e9932017-05-03 00:35:39 +08002812 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002813 if (!blk) {
2814 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002815 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002816 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002817
2818 if (output_format == OFORMAT_HUMAN) {
2819 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2820 }
2821
Max Reitzf1d3cd72015-02-05 13:58:18 -05002822 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002823 while (curr.start + curr.length < length) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002824 int64_t offset = curr.start + curr.length;
2825 int64_t n;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002826
2827 /* Probe up to 1 GiB at a time. */
Eric Blake5e344dd2017-10-11 22:47:02 -05002828 n = QEMU_ALIGN_DOWN(MIN(1 << 30, length - offset), BDRV_SECTOR_SIZE);
2829 ret = get_block_status(bs, offset, n, &next);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002830
2831 if (ret < 0) {
2832 error_report("Could not read file metadata: %s", strerror(-ret));
2833 goto out;
2834 }
2835
Fam Zheng16b0d552016-01-26 11:59:02 +08002836 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002837 curr.length += next.length;
2838 continue;
2839 }
2840
2841 if (curr.length > 0) {
2842 dump_map_entry(output_format, &curr, &next);
2843 }
2844 curr = next;
2845 }
2846
2847 dump_map_entry(output_format, &curr, NULL);
2848
2849out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002850 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002851 return ret < 0;
2852}
2853
aliguorif7b4a942009-01-07 17:40:15 +00002854#define SNAPSHOT_LIST 1
2855#define SNAPSHOT_CREATE 2
2856#define SNAPSHOT_APPLY 3
2857#define SNAPSHOT_DELETE 4
2858
Stuart Brady153859b2009-06-07 00:42:17 +01002859static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002860{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002861 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002862 BlockDriverState *bs;
2863 QEMUSnapshotInfo sn;
2864 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002865 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002866 int action = 0;
2867 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002868 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002869 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002870 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002871 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00002872
Kevin Wolfce099542016-03-15 13:01:04 +01002873 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002874 /* Parse commandline parameters */
2875 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002876 static const struct option long_options[] = {
2877 {"help", no_argument, 0, 'h'},
2878 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002879 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002880 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002881 {0, 0, 0, 0}
2882 };
Fam Zheng335e9932017-05-03 00:35:39 +08002883 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002884 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002885 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002886 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002887 }
aliguorif7b4a942009-01-07 17:40:15 +00002888 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002889 case ':':
2890 missing_argument(argv[optind - 1]);
2891 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002892 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002893 unrecognized_option(argv[optind - 1]);
2894 break;
aliguorif7b4a942009-01-07 17:40:15 +00002895 case 'h':
2896 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002897 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002898 case 'l':
2899 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002900 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002901 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002902 }
2903 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002904 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002905 break;
2906 case 'a':
2907 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002908 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002909 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002910 }
2911 action = SNAPSHOT_APPLY;
2912 snapshot_name = optarg;
2913 break;
2914 case 'c':
2915 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002916 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002917 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002918 }
2919 action = SNAPSHOT_CREATE;
2920 snapshot_name = optarg;
2921 break;
2922 case 'd':
2923 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002924 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002925 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002926 }
2927 action = SNAPSHOT_DELETE;
2928 snapshot_name = optarg;
2929 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002930 case 'q':
2931 quiet = true;
2932 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002933 case 'U':
2934 force_share = true;
2935 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002936 case OPTION_OBJECT: {
2937 QemuOpts *opts;
2938 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2939 optarg, true);
2940 if (!opts) {
2941 return 1;
2942 }
2943 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002944 case OPTION_IMAGE_OPTS:
2945 image_opts = true;
2946 break;
aliguorif7b4a942009-01-07 17:40:15 +00002947 }
2948 }
2949
Kevin Wolffc11eb22013-08-05 10:53:04 +02002950 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002951 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002952 }
aliguorif7b4a942009-01-07 17:40:15 +00002953 filename = argv[optind++];
2954
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002955 if (qemu_opts_foreach(&qemu_object_opts,
2956 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002957 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002958 return 1;
2959 }
2960
aliguorif7b4a942009-01-07 17:40:15 +00002961 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08002962 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
2963 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002964 if (!blk) {
2965 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002966 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002967 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002968
2969 /* Perform the requested action */
2970 switch(action) {
2971 case SNAPSHOT_LIST:
2972 dump_snapshots(bs);
2973 break;
2974
2975 case SNAPSHOT_CREATE:
2976 memset(&sn, 0, sizeof(sn));
2977 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2978
2979 qemu_gettimeofday(&tv);
2980 sn.date_sec = tv.tv_sec;
2981 sn.date_nsec = tv.tv_usec * 1000;
2982
2983 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002984 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002985 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002986 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002987 }
aliguorif7b4a942009-01-07 17:40:15 +00002988 break;
2989
2990 case SNAPSHOT_APPLY:
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01002991 ret = bdrv_snapshot_goto(bs, snapshot_name, &err);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002992 if (ret) {
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01002993 error_reportf_err(err, "Could not apply snapshot '%s': ",
2994 snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002995 }
aliguorif7b4a942009-01-07 17:40:15 +00002996 break;
2997
2998 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002999 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003000 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003001 error_reportf_err(err, "Could not delete snapshot '%s': ",
3002 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003003 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003004 }
aliguorif7b4a942009-01-07 17:40:15 +00003005 break;
3006 }
3007
3008 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003009 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003010 if (ret) {
3011 return 1;
3012 }
Stuart Brady153859b2009-06-07 00:42:17 +01003013 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003014}
3015
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003016static int img_rebase(int argc, char **argv)
3017{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003018 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003019 uint8_t *buf_old = NULL;
3020 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05003021 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003022 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003023 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3024 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003025 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003026 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003027 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003028 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003029 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003030 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003031 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003032
3033 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003034 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003035 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003036 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003037 out_baseimg = NULL;
3038 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003039 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003040 static const struct option long_options[] = {
3041 {"help", no_argument, 0, 'h'},
3042 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003043 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003044 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003045 {0, 0, 0, 0}
3046 };
Fam Zheng335e9932017-05-03 00:35:39 +08003047 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003048 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003049 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003050 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003051 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003052 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003053 case ':':
3054 missing_argument(argv[optind - 1]);
3055 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003056 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003057 unrecognized_option(argv[optind - 1]);
3058 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003059 case 'h':
3060 help();
3061 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003062 case 'f':
3063 fmt = optarg;
3064 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003065 case 'F':
3066 out_basefmt = optarg;
3067 break;
3068 case 'b':
3069 out_baseimg = optarg;
3070 break;
3071 case 'u':
3072 unsafe = 1;
3073 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003074 case 'p':
3075 progress = 1;
3076 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003077 case 't':
3078 cache = optarg;
3079 break;
Max Reitz40055952014-07-22 22:58:42 +02003080 case 'T':
3081 src_cache = optarg;
3082 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003083 case 'q':
3084 quiet = true;
3085 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003086 case OPTION_OBJECT: {
3087 QemuOpts *opts;
3088 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3089 optarg, true);
3090 if (!opts) {
3091 return 1;
3092 }
3093 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003094 case OPTION_IMAGE_OPTS:
3095 image_opts = true;
3096 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003097 case 'U':
3098 force_share = true;
3099 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003100 }
3101 }
3102
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003103 if (quiet) {
3104 progress = 0;
3105 }
3106
Fam Zhengac1307a2014-04-22 13:36:11 +08003107 if (optind != argc - 1) {
3108 error_exit("Expecting one image file name");
3109 }
3110 if (!unsafe && !out_baseimg) {
3111 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003112 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003113 filename = argv[optind++];
3114
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003115 if (qemu_opts_foreach(&qemu_object_opts,
3116 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003117 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003118 return 1;
3119 }
3120
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003121 qemu_progress_init(progress, 2.0);
3122 qemu_progress_print(0, 100);
3123
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003124 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003125 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003126 if (ret < 0) {
3127 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003128 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003129 }
3130
Kevin Wolfce099542016-03-15 13:01:04 +01003131 src_flags = 0;
3132 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003133 if (ret < 0) {
3134 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003135 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003136 }
3137
Kevin Wolfce099542016-03-15 13:01:04 +01003138 /* The source files are opened read-only, don't care about WCE */
3139 assert((src_flags & BDRV_O_RDWR) == 0);
3140 (void) src_writethrough;
3141
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003142 /*
3143 * Open the images.
3144 *
3145 * Ignore the old backing file for unsafe rebase in case we want to correct
3146 * the reference to a renamed or moved backing file.
3147 */
Fam Zheng335e9932017-05-03 00:35:39 +08003148 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3149 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003150 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003151 ret = -1;
3152 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003153 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003154 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003155
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003156 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003157 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003158 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003159 ret = -1;
3160 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003161 }
3162 }
3163
3164 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003165 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003166 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003167 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003168
Max Reitz644483d2015-02-05 13:58:17 -05003169 if (bs->backing_format[0] != '\0') {
3170 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003171 qdict_put_str(options, "driver", bs->backing_format);
Max Reitz644483d2015-02-05 13:58:17 -05003172 }
3173
Fam Zheng335e9932017-05-03 00:35:39 +08003174 if (force_share) {
3175 if (!options) {
3176 options = qdict_new();
3177 }
Eric Blake579cf1d2017-05-15 14:54:39 -05003178 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +08003179 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003180 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003181 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003182 options, src_flags, &local_err);
3183 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003184 error_reportf_err(local_err,
3185 "Could not open old backing file '%s': ",
3186 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003187 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003188 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003189 }
Max Reitz644483d2015-02-05 13:58:17 -05003190
Alex Bligha6166732012-10-16 13:46:18 +01003191 if (out_baseimg[0]) {
Fam Zheng335e9932017-05-03 00:35:39 +08003192 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003193 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003194 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003195 }
3196 if (force_share) {
3197 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003198 }
3199
Max Reitzefaa7c42016-03-16 19:54:38 +01003200 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003201 options, src_flags, &local_err);
3202 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003203 error_reportf_err(local_err,
3204 "Could not open new backing file '%s': ",
3205 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003206 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003207 goto out;
3208 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003209 }
3210 }
3211
3212 /*
3213 * Check each unallocated cluster in the COW file. If it is unallocated,
3214 * accesses go to the backing file. We must therefore compare this cluster
3215 * in the old and new backing file, and if they differ we need to copy it
3216 * from the old backing file into the COW file.
3217 *
3218 * If qemu-img crashes during this step, no harm is done. The content of
3219 * the image is the same as the original one at any time.
3220 */
3221 if (!unsafe) {
Eric Blake41536282017-10-11 22:47:15 -05003222 int64_t size;
3223 int64_t old_backing_size;
3224 int64_t new_backing_size = 0;
3225 uint64_t offset;
3226 int64_t n;
Kevin Wolf1f710492012-10-12 14:29:18 +02003227 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003228
Max Reitzf1d3cd72015-02-05 13:58:18 -05003229 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3230 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003231
Eric Blake41536282017-10-11 22:47:15 -05003232 size = blk_getlength(blk);
3233 if (size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003234 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003235 filename, strerror(-size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003236 ret = -1;
3237 goto out;
3238 }
Eric Blake41536282017-10-11 22:47:15 -05003239 old_backing_size = blk_getlength(blk_old_backing);
3240 if (old_backing_size < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003241 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003242
3243 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3244 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003245 backing_name, strerror(-old_backing_size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003246 ret = -1;
3247 goto out;
3248 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003249 if (blk_new_backing) {
Eric Blake41536282017-10-11 22:47:15 -05003250 new_backing_size = blk_getlength(blk_new_backing);
3251 if (new_backing_size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003252 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003253 out_baseimg, strerror(-new_backing_size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003254 ret = -1;
3255 goto out;
3256 }
Alex Bligha6166732012-10-16 13:46:18 +01003257 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003258
Eric Blake41536282017-10-11 22:47:15 -05003259 if (size != 0) {
3260 local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE));
Kevin Wolf1f710492012-10-12 14:29:18 +02003261 }
3262
Eric Blake41536282017-10-11 22:47:15 -05003263 for (offset = 0; offset < size; offset += n) {
3264 /* How many bytes can we handle with the next read? */
3265 n = MIN(IO_BUF_SIZE, size - offset);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003266
3267 /* If the cluster is allocated, we don't need to take action */
Eric Blake41536282017-10-11 22:47:15 -05003268 ret = bdrv_is_allocated(bs, offset, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003269 if (ret < 0) {
3270 error_report("error while reading image metadata: %s",
3271 strerror(-ret));
3272 goto out;
3273 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003274 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003275 continue;
3276 }
3277
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003278 /*
3279 * Read old and new backing file and take into consideration that
3280 * backing files may be smaller than the COW image.
3281 */
Eric Blake41536282017-10-11 22:47:15 -05003282 if (offset >= old_backing_size) {
3283 memset(buf_old, 0, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003284 } else {
Eric Blake41536282017-10-11 22:47:15 -05003285 if (offset + n > old_backing_size) {
3286 n = old_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003287 }
3288
Eric Blake41536282017-10-11 22:47:15 -05003289 ret = blk_pread(blk_old_backing, offset, buf_old, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003290 if (ret < 0) {
3291 error_report("error while reading from old backing file");
3292 goto out;
3293 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003294 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003295
Eric Blake41536282017-10-11 22:47:15 -05003296 if (offset >= new_backing_size || !blk_new_backing) {
3297 memset(buf_new, 0, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003298 } else {
Eric Blake41536282017-10-11 22:47:15 -05003299 if (offset + n > new_backing_size) {
3300 n = new_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003301 }
3302
Eric Blake41536282017-10-11 22:47:15 -05003303 ret = blk_pread(blk_new_backing, offset, buf_new, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003304 if (ret < 0) {
3305 error_report("error while reading from new backing file");
3306 goto out;
3307 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003308 }
3309
3310 /* If they differ, we need to write to the COW file */
3311 uint64_t written = 0;
3312
Eric Blake41536282017-10-11 22:47:15 -05003313 while (written < n) {
Eric Blakedc61cd32017-10-11 22:47:14 -05003314 int64_t pnum;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003315
Eric Blake41536282017-10-11 22:47:15 -05003316 if (compare_buffers(buf_old + written, buf_new + written,
3317 n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003318 {
Eric Blake41536282017-10-11 22:47:15 -05003319 ret = blk_pwrite(blk, offset + written,
Eric Blakedc61cd32017-10-11 22:47:14 -05003320 buf_old + written, pnum, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003321 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003322 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003323 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003324 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003325 }
3326 }
3327
3328 written += pnum;
3329 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003330 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003331 }
3332 }
3333
3334 /*
3335 * Change the backing file. All clusters that are different from the old
3336 * backing file are overwritten in the COW file now, so the visible content
3337 * doesn't change when we switch the backing file.
3338 */
Alex Bligha6166732012-10-16 13:46:18 +01003339 if (out_baseimg && *out_baseimg) {
3340 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3341 } else {
3342 ret = bdrv_change_backing_file(bs, NULL, NULL);
3343 }
3344
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003345 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003346 error_report("Could not change the backing file to '%s': No "
3347 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003348 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003349 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003350 out_baseimg, strerror(-ret));
3351 }
3352
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003353 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003354 /*
3355 * TODO At this point it is possible to check if any clusters that are
3356 * allocated in the COW file are the same in the backing file. If so, they
3357 * could be dropped from the COW file. Don't do this before switching the
3358 * backing file, in case of a crash this would lead to corruption.
3359 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003360out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003361 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003362 /* Cleanup */
3363 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003364 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003365 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003366 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003367 qemu_vfree(buf_old);
3368 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003369
Markus Armbruster26f54e92014-10-07 13:59:04 +02003370 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003371 if (ret) {
3372 return 1;
3373 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003374 return 0;
3375}
3376
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003377static int img_resize(int argc, char **argv)
3378{
Markus Armbruster6750e792015-02-12 17:43:08 +01003379 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003380 int c, ret, relative;
3381 const char *filename, *fmt, *size;
Max Reitzdc5f6902017-06-13 22:20:55 +02003382 int64_t n, total_size, current_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003383 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003384 BlockBackend *blk = NULL;
Max Reitzdc5f6902017-06-13 22:20:55 +02003385 PreallocMode prealloc = PREALLOC_MODE_OFF;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003386 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003387
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003388 static QemuOptsList resize_options = {
3389 .name = "resize_options",
3390 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3391 .desc = {
3392 {
3393 .name = BLOCK_OPT_SIZE,
3394 .type = QEMU_OPT_SIZE,
3395 .help = "Virtual disk size"
3396 }, {
3397 /* end of list */
3398 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003399 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003400 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003401 bool image_opts = false;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003402 bool shrink = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003403
Kevin Wolfe80fec72011-04-29 10:58:12 +02003404 /* Remove size from argv manually so that negative numbers are not treated
3405 * as options by getopt. */
3406 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003407 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003408 return 1;
3409 }
3410
3411 size = argv[--argc];
3412
3413 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003414 fmt = NULL;
3415 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003416 static const struct option long_options[] = {
3417 {"help", no_argument, 0, 'h'},
3418 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003419 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Max Reitzdc5f6902017-06-13 22:20:55 +02003420 {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003421 {"shrink", no_argument, 0, OPTION_SHRINK},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003422 {0, 0, 0, 0}
3423 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003424 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003425 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003426 if (c == -1) {
3427 break;
3428 }
3429 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003430 case ':':
3431 missing_argument(argv[optind - 1]);
3432 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003433 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003434 unrecognized_option(argv[optind - 1]);
3435 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003436 case 'h':
3437 help();
3438 break;
3439 case 'f':
3440 fmt = optarg;
3441 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003442 case 'q':
3443 quiet = true;
3444 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003445 case OPTION_OBJECT: {
3446 QemuOpts *opts;
3447 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3448 optarg, true);
3449 if (!opts) {
3450 return 1;
3451 }
3452 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003453 case OPTION_IMAGE_OPTS:
3454 image_opts = true;
3455 break;
Max Reitzdc5f6902017-06-13 22:20:55 +02003456 case OPTION_PREALLOCATION:
Marc-AndrƩ Lureauf7abe0e2017-08-24 10:46:10 +02003457 prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
Markus Armbruster06c60b62017-08-24 10:45:57 +02003458 PREALLOC_MODE__MAX, NULL);
Max Reitzdc5f6902017-06-13 22:20:55 +02003459 if (prealloc == PREALLOC_MODE__MAX) {
3460 error_report("Invalid preallocation mode '%s'", optarg);
3461 return 1;
3462 }
3463 break;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003464 case OPTION_SHRINK:
3465 shrink = true;
3466 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003467 }
3468 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003469 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003470 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003471 }
3472 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003473
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003474 if (qemu_opts_foreach(&qemu_object_opts,
3475 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003476 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003477 return 1;
3478 }
3479
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003480 /* Choose grow, shrink, or absolute resize mode */
3481 switch (size[0]) {
3482 case '+':
3483 relative = 1;
3484 size++;
3485 break;
3486 case '-':
3487 relative = -1;
3488 size++;
3489 break;
3490 default:
3491 relative = 0;
3492 break;
3493 }
3494
3495 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003496 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003497 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003498 if (err) {
3499 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003500 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003501 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003502 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003503 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003504 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3505 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003506
Max Reitzefaa7c42016-03-16 19:54:38 +01003507 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003508 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3509 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003510 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003511 ret = -1;
3512 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003513 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003514
Max Reitzdc5f6902017-06-13 22:20:55 +02003515 current_size = blk_getlength(blk);
3516 if (current_size < 0) {
3517 error_report("Failed to inquire current image length: %s",
3518 strerror(-current_size));
3519 ret = -1;
3520 goto out;
3521 }
3522
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003523 if (relative) {
Max Reitzdc5f6902017-06-13 22:20:55 +02003524 total_size = current_size + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003525 } else {
3526 total_size = n;
3527 }
3528 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003529 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003530 ret = -1;
3531 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003532 }
3533
Max Reitzdc5f6902017-06-13 22:20:55 +02003534 if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
3535 error_report("Preallocation can only be used for growing images");
3536 ret = -1;
3537 goto out;
3538 }
3539
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003540 if (total_size < current_size && !shrink) {
3541 warn_report("Shrinking an image will delete all data beyond the "
3542 "shrunken image's end. Before performing such an "
3543 "operation, make sure there is no important data there.");
3544
3545 if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) {
3546 error_report(
3547 "Use the --shrink option to perform a shrink operation.");
3548 ret = -1;
3549 goto out;
3550 } else {
3551 warn_report("Using the --shrink option will suppress this message. "
3552 "Note that future versions of qemu-img may refuse to "
3553 "shrink images without this option.");
3554 }
3555 }
3556
Max Reitzdc5f6902017-06-13 22:20:55 +02003557 ret = blk_truncate(blk, total_size, prealloc, &err);
Max Reitzed3d2ec2017-03-28 22:51:27 +02003558 if (!ret) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003559 qprintf(quiet, "Image resized.\n");
Max Reitzed3d2ec2017-03-28 22:51:27 +02003560 } else {
3561 error_report_err(err);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003562 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003563out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003564 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003565 if (ret) {
3566 return 1;
3567 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003568 return 0;
3569}
3570
Max Reitz76a3a342014-10-27 11:12:51 +01003571static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003572 int64_t offset, int64_t total_work_size,
3573 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003574{
3575 qemu_progress_print(100.f * offset / total_work_size, 0);
3576}
3577
Max Reitz6f176b42013-09-03 10:09:50 +02003578static int img_amend(int argc, char **argv)
3579{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003580 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003581 int c, ret = 0;
3582 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003583 QemuOptsList *create_opts = NULL;
3584 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003585 const char *fmt = NULL, *filename, *cache;
3586 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003587 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003588 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003589 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003590 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003591 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003592
Max Reitzbd39e6e2014-07-22 22:58:43 +02003593 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003594 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003595 static const struct option long_options[] = {
3596 {"help", no_argument, 0, 'h'},
3597 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003598 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003599 {0, 0, 0, 0}
3600 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003601 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003602 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003603 if (c == -1) {
3604 break;
3605 }
3606
3607 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003608 case ':':
3609 missing_argument(argv[optind - 1]);
3610 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003611 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003612 unrecognized_option(argv[optind - 1]);
3613 break;
3614 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003615 help();
3616 break;
3617 case 'o':
3618 if (!is_valid_option_list(optarg)) {
3619 error_report("Invalid option list: %s", optarg);
3620 ret = -1;
3621 goto out_no_progress;
3622 }
3623 if (!options) {
3624 options = g_strdup(optarg);
3625 } else {
3626 char *old_options = options;
3627 options = g_strdup_printf("%s,%s", options, optarg);
3628 g_free(old_options);
3629 }
3630 break;
3631 case 'f':
3632 fmt = optarg;
3633 break;
3634 case 't':
3635 cache = optarg;
3636 break;
3637 case 'p':
3638 progress = true;
3639 break;
3640 case 'q':
3641 quiet = true;
3642 break;
3643 case OPTION_OBJECT:
3644 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3645 optarg, true);
3646 if (!opts) {
3647 ret = -1;
3648 goto out_no_progress;
3649 }
3650 break;
3651 case OPTION_IMAGE_OPTS:
3652 image_opts = true;
3653 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003654 }
3655 }
3656
Max Reitz6f176b42013-09-03 10:09:50 +02003657 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003658 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003659 }
3660
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003661 if (qemu_opts_foreach(&qemu_object_opts,
3662 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003663 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003664 ret = -1;
3665 goto out_no_progress;
3666 }
3667
Max Reitz76a3a342014-10-27 11:12:51 +01003668 if (quiet) {
3669 progress = false;
3670 }
3671 qemu_progress_init(progress, 1.0);
3672
Kevin Wolfa283cb62014-02-21 16:24:07 +01003673 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3674 if (fmt && has_help_option(options)) {
3675 /* If a format is explicitly specified (and possibly no filename is
3676 * given), print option help here */
3677 ret = print_block_option_help(filename, fmt);
3678 goto out;
3679 }
3680
3681 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003682 error_report("Expecting one image file name");
3683 ret = -1;
3684 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003685 }
Max Reitz6f176b42013-09-03 10:09:50 +02003686
Kevin Wolfce099542016-03-15 13:01:04 +01003687 flags = BDRV_O_RDWR;
3688 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003689 if (ret < 0) {
3690 error_report("Invalid cache option: %s", cache);
3691 goto out;
3692 }
3693
Fam Zheng335e9932017-05-03 00:35:39 +08003694 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3695 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003696 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003697 ret = -1;
3698 goto out;
3699 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003700 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003701
3702 fmt = bs->drv->format_name;
3703
Kevin Wolf626f84f2014-02-21 16:24:06 +01003704 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003705 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003706 ret = print_block_option_help(filename, fmt);
3707 goto out;
3708 }
3709
Max Reitzb2439d22014-12-02 18:32:47 +01003710 if (!bs->drv->create_opts) {
3711 error_report("Format driver '%s' does not support any options to amend",
3712 fmt);
3713 ret = -1;
3714 goto out;
3715 }
3716
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003717 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003718 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003719 qemu_opts_do_parse(opts, options, NULL, &err);
3720 if (err) {
3721 error_report_err(err);
3722 ret = -1;
3723 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003724 }
3725
Max Reitz76a3a342014-10-27 11:12:51 +01003726 /* In case the driver does not call amend_status_cb() */
3727 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003728 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003729 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003730 if (ret < 0) {
3731 error_report("Error while amending options: %s", strerror(-ret));
3732 goto out;
3733 }
3734
3735out:
Max Reitz76a3a342014-10-27 11:12:51 +01003736 qemu_progress_end();
3737
Max Reitze814dff2015-08-20 16:00:38 -07003738out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003739 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003740 qemu_opts_del(opts);
3741 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003742 g_free(options);
3743
Max Reitz6f176b42013-09-03 10:09:50 +02003744 if (ret) {
3745 return 1;
3746 }
3747 return 0;
3748}
3749
Kevin Wolfb6133b82014-08-05 14:17:13 +02003750typedef struct BenchData {
3751 BlockBackend *blk;
3752 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003753 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003754 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003755 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003756 int nrreq;
3757 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003758 int flush_interval;
3759 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003760 uint8_t *buf;
3761 QEMUIOVector *qiov;
3762
3763 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003764 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003765 uint64_t offset;
3766} BenchData;
3767
Kevin Wolf55d539c2016-06-03 13:59:41 +02003768static void bench_undrained_flush_cb(void *opaque, int ret)
3769{
3770 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003771 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003772 exit(EXIT_FAILURE);
3773 }
3774}
3775
Kevin Wolfb6133b82014-08-05 14:17:13 +02003776static void bench_cb(void *opaque, int ret)
3777{
3778 BenchData *b = opaque;
3779 BlockAIOCB *acb;
3780
3781 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003782 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003783 exit(EXIT_FAILURE);
3784 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003785
3786 if (b->in_flush) {
3787 /* Just finished a flush with drained queue: Start next requests */
3788 assert(b->in_flight == 0);
3789 b->in_flush = false;
3790 } else if (b->in_flight > 0) {
3791 int remaining = b->n - b->in_flight;
3792
Kevin Wolfb6133b82014-08-05 14:17:13 +02003793 b->n--;
3794 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003795
3796 /* Time for flush? Drain queue if requested, then flush */
3797 if (b->flush_interval && remaining % b->flush_interval == 0) {
3798 if (!b->in_flight || !b->drain_on_flush) {
3799 BlockCompletionFunc *cb;
3800
3801 if (b->drain_on_flush) {
3802 b->in_flush = true;
3803 cb = bench_cb;
3804 } else {
3805 cb = bench_undrained_flush_cb;
3806 }
3807
3808 acb = blk_aio_flush(b->blk, cb, b);
3809 if (!acb) {
3810 error_report("Failed to issue flush request");
3811 exit(EXIT_FAILURE);
3812 }
3813 }
3814 if (b->drain_on_flush) {
3815 return;
3816 }
3817 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003818 }
3819
3820 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003821 int64_t offset = b->offset;
3822 /* blk_aio_* might look for completed I/Os and kick bench_cb
3823 * again, so make sure this operation is counted by in_flight
3824 * and b->offset is ready for the next submission.
3825 */
3826 b->in_flight++;
3827 b->offset += b->step;
3828 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003829 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003830 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003831 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003832 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003833 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003834 if (!acb) {
3835 error_report("Failed to issue request");
3836 exit(EXIT_FAILURE);
3837 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003838 }
3839}
3840
3841static int img_bench(int argc, char **argv)
3842{
3843 int c, ret = 0;
3844 const char *fmt = NULL, *filename;
3845 bool quiet = false;
3846 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003847 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003848 int count = 75000;
3849 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003850 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003851 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003852 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003853 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003854 int flush_interval = 0;
3855 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003856 int64_t image_size;
3857 BlockBackend *blk = NULL;
3858 BenchData data = {};
3859 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003860 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003861 struct timeval t1, t2;
3862 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08003863 bool force_share = false;
Fam Zheng79d46582018-01-16 14:08:58 +08003864 size_t buf_size;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003865
3866 for (;;) {
3867 static const struct option long_options[] = {
3868 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003869 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003870 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003871 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003872 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08003873 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003874 {0, 0, 0, 0}
3875 };
Fam Zheng335e9932017-05-03 00:35:39 +08003876 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003877 if (c == -1) {
3878 break;
3879 }
3880
3881 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003882 case ':':
3883 missing_argument(argv[optind - 1]);
3884 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003885 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003886 unrecognized_option(argv[optind - 1]);
3887 break;
3888 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02003889 help();
3890 break;
3891 case 'c':
3892 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003893 unsigned long res;
3894
3895 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003896 error_report("Invalid request count specified");
3897 return 1;
3898 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003899 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003900 break;
3901 }
3902 case 'd':
3903 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003904 unsigned long res;
3905
3906 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003907 error_report("Invalid queue depth specified");
3908 return 1;
3909 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003910 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003911 break;
3912 }
3913 case 'f':
3914 fmt = optarg;
3915 break;
3916 case 'n':
3917 flags |= BDRV_O_NATIVE_AIO;
3918 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003919 case 'o':
3920 {
Markus Armbruster606caa02017-02-21 21:14:04 +01003921 offset = cvtnum(optarg);
3922 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02003923 error_report("Invalid offset specified");
3924 return 1;
3925 }
3926 break;
3927 }
3928 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003929 case 'q':
3930 quiet = true;
3931 break;
3932 case 's':
3933 {
3934 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003935
Markus Armbruster606caa02017-02-21 21:14:04 +01003936 sval = cvtnum(optarg);
3937 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003938 error_report("Invalid buffer size specified");
3939 return 1;
3940 }
3941
3942 bufsize = sval;
3943 break;
3944 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003945 case 'S':
3946 {
3947 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003948
Markus Armbruster606caa02017-02-21 21:14:04 +01003949 sval = cvtnum(optarg);
3950 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02003951 error_report("Invalid step size specified");
3952 return 1;
3953 }
3954
3955 step = sval;
3956 break;
3957 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003958 case 't':
3959 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3960 if (ret < 0) {
3961 error_report("Invalid cache mode");
3962 ret = -1;
3963 goto out;
3964 }
3965 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003966 case 'w':
3967 flags |= BDRV_O_RDWR;
3968 is_write = true;
3969 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003970 case 'U':
3971 force_share = true;
3972 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003973 case OPTION_PATTERN:
3974 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003975 unsigned long res;
3976
3977 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003978 error_report("Invalid pattern byte specified");
3979 return 1;
3980 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003981 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003982 break;
3983 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003984 case OPTION_FLUSH_INTERVAL:
3985 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003986 unsigned long res;
3987
3988 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02003989 error_report("Invalid flush interval specified");
3990 return 1;
3991 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003992 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003993 break;
3994 }
3995 case OPTION_NO_DRAIN:
3996 drain_on_flush = false;
3997 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003998 case OPTION_IMAGE_OPTS:
3999 image_opts = true;
4000 break;
4001 }
4002 }
4003
4004 if (optind != argc - 1) {
4005 error_exit("Expecting one image file name");
4006 }
4007 filename = argv[argc - 1];
4008
Kevin Wolf55d539c2016-06-03 13:59:41 +02004009 if (!is_write && flush_interval) {
4010 error_report("--flush-interval is only available in write tests");
4011 ret = -1;
4012 goto out;
4013 }
4014 if (flush_interval && flush_interval < depth) {
4015 error_report("Flush interval can't be smaller than depth");
4016 ret = -1;
4017 goto out;
4018 }
4019
Fam Zheng335e9932017-05-03 00:35:39 +08004020 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4021 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004022 if (!blk) {
4023 ret = -1;
4024 goto out;
4025 }
4026
4027 image_size = blk_getlength(blk);
4028 if (image_size < 0) {
4029 ret = image_size;
4030 goto out;
4031 }
4032
4033 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004034 .blk = blk,
4035 .image_size = image_size,
4036 .bufsize = bufsize,
4037 .step = step ?: bufsize,
4038 .nrreq = depth,
4039 .n = count,
4040 .offset = offset,
4041 .write = is_write,
4042 .flush_interval = flush_interval,
4043 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004044 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004045 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004046 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004047 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004048 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004049 if (flush_interval) {
4050 printf("Sending flush every %d requests\n", flush_interval);
4051 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004052
Fam Zheng79d46582018-01-16 14:08:58 +08004053 buf_size = data.nrreq * data.bufsize;
4054 data.buf = blk_blockalign(blk, buf_size);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004055 memset(data.buf, pattern, data.nrreq * data.bufsize);
4056
Fam Zheng79d46582018-01-16 14:08:58 +08004057 blk_register_buf(blk, data.buf, buf_size);
4058
Kevin Wolfb6133b82014-08-05 14:17:13 +02004059 data.qiov = g_new(QEMUIOVector, data.nrreq);
4060 for (i = 0; i < data.nrreq; i++) {
4061 qemu_iovec_init(&data.qiov[i], 1);
4062 qemu_iovec_add(&data.qiov[i],
4063 data.buf + i * data.bufsize, data.bufsize);
4064 }
4065
4066 gettimeofday(&t1, NULL);
4067 bench_cb(&data, 0);
4068
4069 while (data.n > 0) {
4070 main_loop_wait(false);
4071 }
4072 gettimeofday(&t2, NULL);
4073
4074 printf("Run completed in %3.3f seconds.\n",
4075 (t2.tv_sec - t1.tv_sec)
4076 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4077
4078out:
Fam Zheng79d46582018-01-16 14:08:58 +08004079 if (data.buf) {
4080 blk_unregister_buf(blk, data.buf);
4081 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004082 qemu_vfree(data.buf);
4083 blk_unref(blk);
4084
4085 if (ret) {
4086 return 1;
4087 }
4088 return 0;
4089}
4090
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004091#define C_BS 01
4092#define C_COUNT 02
4093#define C_IF 04
4094#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004095#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004096
4097struct DdInfo {
4098 unsigned int flags;
4099 int64_t count;
4100};
4101
4102struct DdIo {
4103 int bsz; /* Block size */
4104 char *filename;
4105 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004106 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004107};
4108
4109struct DdOpts {
4110 const char *name;
4111 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4112 unsigned int flag;
4113};
4114
4115static int img_dd_bs(const char *arg,
4116 struct DdIo *in, struct DdIo *out,
4117 struct DdInfo *dd)
4118{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004119 int64_t res;
4120
Markus Armbruster606caa02017-02-21 21:14:04 +01004121 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004122
Markus Armbruster606caa02017-02-21 21:14:04 +01004123 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004124 error_report("invalid number: '%s'", arg);
4125 return 1;
4126 }
4127 in->bsz = out->bsz = res;
4128
4129 return 0;
4130}
4131
4132static int img_dd_count(const char *arg,
4133 struct DdIo *in, struct DdIo *out,
4134 struct DdInfo *dd)
4135{
Markus Armbruster606caa02017-02-21 21:14:04 +01004136 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004137
Markus Armbruster606caa02017-02-21 21:14:04 +01004138 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004139 error_report("invalid number: '%s'", arg);
4140 return 1;
4141 }
4142
4143 return 0;
4144}
4145
4146static int img_dd_if(const char *arg,
4147 struct DdIo *in, struct DdIo *out,
4148 struct DdInfo *dd)
4149{
4150 in->filename = g_strdup(arg);
4151
4152 return 0;
4153}
4154
4155static int img_dd_of(const char *arg,
4156 struct DdIo *in, struct DdIo *out,
4157 struct DdInfo *dd)
4158{
4159 out->filename = g_strdup(arg);
4160
4161 return 0;
4162}
4163
Reda Sallahif7c15532016-08-10 16:16:09 +02004164static int img_dd_skip(const char *arg,
4165 struct DdIo *in, struct DdIo *out,
4166 struct DdInfo *dd)
4167{
Markus Armbruster606caa02017-02-21 21:14:04 +01004168 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004169
Markus Armbruster606caa02017-02-21 21:14:04 +01004170 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004171 error_report("invalid number: '%s'", arg);
4172 return 1;
4173 }
4174
4175 return 0;
4176}
4177
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004178static int img_dd(int argc, char **argv)
4179{
4180 int ret = 0;
4181 char *arg = NULL;
4182 char *tmp;
4183 BlockDriver *drv = NULL, *proto_drv = NULL;
4184 BlockBackend *blk1 = NULL, *blk2 = NULL;
4185 QemuOpts *opts = NULL;
4186 QemuOptsList *create_opts = NULL;
4187 Error *local_err = NULL;
4188 bool image_opts = false;
4189 int c, i;
4190 const char *out_fmt = "raw";
4191 const char *fmt = NULL;
4192 int64_t size = 0;
4193 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004194 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004195 struct DdInfo dd = {
4196 .flags = 0,
4197 .count = 0,
4198 };
4199 struct DdIo in = {
4200 .bsz = 512, /* Block size is by default 512 bytes */
4201 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004202 .buf = NULL,
4203 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004204 };
4205 struct DdIo out = {
4206 .bsz = 512,
4207 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004208 .buf = NULL,
4209 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004210 };
4211
4212 const struct DdOpts options[] = {
4213 { "bs", img_dd_bs, C_BS },
4214 { "count", img_dd_count, C_COUNT },
4215 { "if", img_dd_if, C_IF },
4216 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004217 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004218 { NULL, NULL, 0 }
4219 };
4220 const struct option long_options[] = {
4221 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004222 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004223 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004224 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004225 { 0, 0, 0, 0 }
4226 };
4227
Fam Zheng335e9932017-05-03 00:35:39 +08004228 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004229 if (c == EOF) {
4230 break;
4231 }
4232 switch (c) {
4233 case 'O':
4234 out_fmt = optarg;
4235 break;
4236 case 'f':
4237 fmt = optarg;
4238 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004239 case ':':
4240 missing_argument(argv[optind - 1]);
4241 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004242 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004243 unrecognized_option(argv[optind - 1]);
4244 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004245 case 'h':
4246 help();
4247 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004248 case 'U':
4249 force_share = true;
4250 break;
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004251 case OPTION_OBJECT:
4252 if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004253 ret = -1;
4254 goto out;
4255 }
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004256 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004257 case OPTION_IMAGE_OPTS:
4258 image_opts = true;
4259 break;
4260 }
4261 }
4262
4263 for (i = optind; i < argc; i++) {
4264 int j;
4265 arg = g_strdup(argv[i]);
4266
4267 tmp = strchr(arg, '=');
4268 if (tmp == NULL) {
4269 error_report("unrecognized operand %s", arg);
4270 ret = -1;
4271 goto out;
4272 }
4273
4274 *tmp++ = '\0';
4275
4276 for (j = 0; options[j].name != NULL; j++) {
4277 if (!strcmp(arg, options[j].name)) {
4278 break;
4279 }
4280 }
4281 if (options[j].name == NULL) {
4282 error_report("unrecognized operand %s", arg);
4283 ret = -1;
4284 goto out;
4285 }
4286
4287 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4288 ret = -1;
4289 goto out;
4290 }
4291 dd.flags |= options[j].flag;
4292 g_free(arg);
4293 arg = NULL;
4294 }
4295
4296 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4297 error_report("Must specify both input and output files");
4298 ret = -1;
4299 goto out;
4300 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004301
4302 if (qemu_opts_foreach(&qemu_object_opts,
4303 user_creatable_add_opts_foreach,
4304 NULL, NULL)) {
4305 ret = -1;
4306 goto out;
4307 }
4308
Fam Zheng335e9932017-05-03 00:35:39 +08004309 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4310 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004311
4312 if (!blk1) {
4313 ret = -1;
4314 goto out;
4315 }
4316
4317 drv = bdrv_find_format(out_fmt);
4318 if (!drv) {
4319 error_report("Unknown file format");
4320 ret = -1;
4321 goto out;
4322 }
4323 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4324
4325 if (!proto_drv) {
4326 error_report_err(local_err);
4327 ret = -1;
4328 goto out;
4329 }
4330 if (!drv->create_opts) {
4331 error_report("Format driver '%s' does not support image creation",
4332 drv->format_name);
4333 ret = -1;
4334 goto out;
4335 }
4336 if (!proto_drv->create_opts) {
4337 error_report("Protocol driver '%s' does not support image creation",
4338 proto_drv->format_name);
4339 ret = -1;
4340 goto out;
4341 }
4342 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4343 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4344
4345 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4346
4347 size = blk_getlength(blk1);
4348 if (size < 0) {
4349 error_report("Failed to get size for '%s'", in.filename);
4350 ret = -1;
4351 goto out;
4352 }
4353
4354 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4355 dd.count * in.bsz < size) {
4356 size = dd.count * in.bsz;
4357 }
4358
Reda Sallahif7c15532016-08-10 16:16:09 +02004359 /* Overflow means the specified offset is beyond input image's size */
4360 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4361 size < in.bsz * in.offset)) {
4362 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4363 } else {
4364 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4365 size - in.bsz * in.offset, &error_abort);
4366 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004367
4368 ret = bdrv_create(drv, out.filename, opts, &local_err);
4369 if (ret < 0) {
4370 error_reportf_err(local_err,
4371 "%s: error while creating output image: ",
4372 out.filename);
4373 ret = -1;
4374 goto out;
4375 }
4376
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004377 /* TODO, we can't honour --image-opts for the target,
4378 * since it needs to be given in a format compatible
4379 * with the bdrv_create() call above which does not
4380 * support image-opts style.
4381 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01004382 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004383 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004384
4385 if (!blk2) {
4386 ret = -1;
4387 goto out;
4388 }
4389
Reda Sallahif7c15532016-08-10 16:16:09 +02004390 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4391 size < in.offset * in.bsz)) {
4392 /* We give a warning if the skip option is bigger than the input
4393 * size and create an empty output disk image (i.e. like dd(1)).
4394 */
4395 error_report("%s: cannot skip to specified offset", in.filename);
4396 in_pos = size;
4397 } else {
4398 in_pos = in.offset * in.bsz;
4399 }
4400
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004401 in.buf = g_new(uint8_t, in.bsz);
4402
Reda Sallahif7c15532016-08-10 16:16:09 +02004403 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004404 int in_ret, out_ret;
4405
4406 if (in_pos + in.bsz > size) {
4407 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4408 } else {
4409 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4410 }
4411 if (in_ret < 0) {
4412 error_report("error while reading from input image file: %s",
4413 strerror(-in_ret));
4414 ret = -1;
4415 goto out;
4416 }
4417 in_pos += in_ret;
4418
4419 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4420
4421 if (out_ret < 0) {
4422 error_report("error while writing to output image file: %s",
4423 strerror(-out_ret));
4424 ret = -1;
4425 goto out;
4426 }
4427 out_pos += out_ret;
4428 }
4429
4430out:
4431 g_free(arg);
4432 qemu_opts_del(opts);
4433 qemu_opts_free(create_opts);
4434 blk_unref(blk1);
4435 blk_unref(blk2);
4436 g_free(in.filename);
4437 g_free(out.filename);
4438 g_free(in.buf);
4439 g_free(out.buf);
4440
4441 if (ret) {
4442 return 1;
4443 }
4444 return 0;
4445}
4446
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004447static void dump_json_block_measure_info(BlockMeasureInfo *info)
4448{
4449 QString *str;
4450 QObject *obj;
4451 Visitor *v = qobject_output_visitor_new(&obj);
4452
4453 visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
4454 visit_complete(v, &obj);
4455 str = qobject_to_json_pretty(obj);
4456 assert(str != NULL);
4457 printf("%s\n", qstring_get_str(str));
4458 qobject_decref(obj);
4459 visit_free(v);
4460 QDECREF(str);
4461}
4462
4463static int img_measure(int argc, char **argv)
4464{
4465 static const struct option long_options[] = {
4466 {"help", no_argument, 0, 'h'},
4467 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4468 {"object", required_argument, 0, OPTION_OBJECT},
4469 {"output", required_argument, 0, OPTION_OUTPUT},
4470 {"size", required_argument, 0, OPTION_SIZE},
4471 {"force-share", no_argument, 0, 'U'},
4472 {0, 0, 0, 0}
4473 };
4474 OutputFormat output_format = OFORMAT_HUMAN;
4475 BlockBackend *in_blk = NULL;
4476 BlockDriver *drv;
4477 const char *filename = NULL;
4478 const char *fmt = NULL;
4479 const char *out_fmt = "raw";
4480 char *options = NULL;
4481 char *snapshot_name = NULL;
4482 bool force_share = false;
4483 QemuOpts *opts = NULL;
4484 QemuOpts *object_opts = NULL;
4485 QemuOpts *sn_opts = NULL;
4486 QemuOptsList *create_opts = NULL;
4487 bool image_opts = false;
4488 uint64_t img_size = UINT64_MAX;
4489 BlockMeasureInfo *info = NULL;
4490 Error *local_err = NULL;
4491 int ret = 1;
4492 int c;
4493
4494 while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
4495 long_options, NULL)) != -1) {
4496 switch (c) {
4497 case '?':
4498 case 'h':
4499 help();
4500 break;
4501 case 'f':
4502 fmt = optarg;
4503 break;
4504 case 'O':
4505 out_fmt = optarg;
4506 break;
4507 case 'o':
4508 if (!is_valid_option_list(optarg)) {
4509 error_report("Invalid option list: %s", optarg);
4510 goto out;
4511 }
4512 if (!options) {
4513 options = g_strdup(optarg);
4514 } else {
4515 char *old_options = options;
4516 options = g_strdup_printf("%s,%s", options, optarg);
4517 g_free(old_options);
4518 }
4519 break;
4520 case 'l':
4521 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
4522 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
4523 optarg, false);
4524 if (!sn_opts) {
4525 error_report("Failed in parsing snapshot param '%s'",
4526 optarg);
4527 goto out;
4528 }
4529 } else {
4530 snapshot_name = optarg;
4531 }
4532 break;
4533 case 'U':
4534 force_share = true;
4535 break;
4536 case OPTION_OBJECT:
4537 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
4538 optarg, true);
4539 if (!object_opts) {
4540 goto out;
4541 }
4542 break;
4543 case OPTION_IMAGE_OPTS:
4544 image_opts = true;
4545 break;
4546 case OPTION_OUTPUT:
4547 if (!strcmp(optarg, "json")) {
4548 output_format = OFORMAT_JSON;
4549 } else if (!strcmp(optarg, "human")) {
4550 output_format = OFORMAT_HUMAN;
4551 } else {
4552 error_report("--output must be used with human or json "
4553 "as argument.");
4554 goto out;
4555 }
4556 break;
4557 case OPTION_SIZE:
4558 {
4559 int64_t sval;
4560
4561 sval = cvtnum(optarg);
4562 if (sval < 0) {
4563 if (sval == -ERANGE) {
4564 error_report("Image size must be less than 8 EiB!");
4565 } else {
4566 error_report("Invalid image size specified! You may use "
4567 "k, M, G, T, P or E suffixes for ");
4568 error_report("kilobytes, megabytes, gigabytes, terabytes, "
4569 "petabytes and exabytes.");
4570 }
4571 goto out;
4572 }
4573 img_size = (uint64_t)sval;
4574 }
4575 break;
4576 }
4577 }
4578
4579 if (qemu_opts_foreach(&qemu_object_opts,
4580 user_creatable_add_opts_foreach,
4581 NULL, NULL)) {
4582 goto out;
4583 }
4584
4585 if (argc - optind > 1) {
4586 error_report("At most one filename argument is allowed.");
4587 goto out;
4588 } else if (argc - optind == 1) {
4589 filename = argv[optind];
4590 }
4591
4592 if (!filename &&
4593 (object_opts || image_opts || fmt || snapshot_name || sn_opts)) {
4594 error_report("--object, --image-opts, -f, and -l "
4595 "require a filename argument.");
4596 goto out;
4597 }
4598 if (filename && img_size != UINT64_MAX) {
4599 error_report("--size N cannot be used together with a filename.");
4600 goto out;
4601 }
4602 if (!filename && img_size == UINT64_MAX) {
4603 error_report("Either --size N or one filename must be specified.");
4604 goto out;
4605 }
4606
4607 if (filename) {
4608 in_blk = img_open(image_opts, filename, fmt, 0,
4609 false, false, force_share);
4610 if (!in_blk) {
4611 goto out;
4612 }
4613
4614 if (sn_opts) {
4615 bdrv_snapshot_load_tmp(blk_bs(in_blk),
4616 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
4617 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
4618 &local_err);
4619 } else if (snapshot_name != NULL) {
4620 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
4621 snapshot_name, &local_err);
4622 }
4623 if (local_err) {
4624 error_reportf_err(local_err, "Failed to load snapshot: ");
4625 goto out;
4626 }
4627 }
4628
4629 drv = bdrv_find_format(out_fmt);
4630 if (!drv) {
4631 error_report("Unknown file format '%s'", out_fmt);
4632 goto out;
4633 }
4634 if (!drv->create_opts) {
4635 error_report("Format driver '%s' does not support image creation",
4636 drv->format_name);
4637 goto out;
4638 }
4639
4640 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4641 create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
4642 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4643 if (options) {
4644 qemu_opts_do_parse(opts, options, NULL, &local_err);
4645 if (local_err) {
4646 error_report_err(local_err);
4647 error_report("Invalid options for file format '%s'", out_fmt);
4648 goto out;
4649 }
4650 }
4651 if (img_size != UINT64_MAX) {
4652 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
4653 }
4654
4655 info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
4656 if (local_err) {
4657 error_report_err(local_err);
4658 goto out;
4659 }
4660
4661 if (output_format == OFORMAT_HUMAN) {
4662 printf("required size: %" PRIu64 "\n", info->required);
4663 printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
4664 } else {
4665 dump_json_block_measure_info(info);
4666 }
4667
4668 ret = 0;
4669
4670out:
4671 qapi_free_BlockMeasureInfo(info);
4672 qemu_opts_del(object_opts);
4673 qemu_opts_del(opts);
4674 qemu_opts_del(sn_opts);
4675 qemu_opts_free(create_opts);
4676 g_free(options);
4677 blk_unref(in_blk);
4678 return ret;
4679}
Kevin Wolfb6133b82014-08-05 14:17:13 +02004680
Anthony Liguoric227f092009-10-01 16:12:16 -05004681static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004682#define DEF(option, callback, arg_string) \
4683 { option, callback },
4684#include "qemu-img-cmds.h"
4685#undef DEF
4686#undef GEN_DOCS
4687 { NULL, NULL, },
4688};
4689
bellardea2384d2004-08-01 21:59:26 +00004690int main(int argc, char **argv)
4691{
Anthony Liguoric227f092009-10-01 16:12:16 -05004692 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004693 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004694 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004695 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004696 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004697 static const struct option long_options[] = {
4698 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004699 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004700 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004701 {0, 0, 0, 0}
4702 };
bellardea2384d2004-08-01 21:59:26 +00004703
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004704#ifdef CONFIG_POSIX
4705 signal(SIGPIPE, SIG_IGN);
4706#endif
4707
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004708 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004709 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004710 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004711
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004712 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004713 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004714 exit(EXIT_FAILURE);
4715 }
4716
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004717 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004718
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004719 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004720 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004721 if (argc < 2) {
4722 error_exit("Not enough arguments");
4723 }
Stuart Brady153859b2009-06-07 00:42:17 +01004724
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004725 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004726 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004727 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004728
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004729 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004730 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004731 case ':':
4732 missing_argument(argv[optind - 1]);
4733 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08004734 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004735 unrecognized_option(argv[optind - 1]);
4736 return 0;
4737 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03004738 help();
4739 return 0;
4740 case 'V':
4741 printf(QEMU_IMG_VERSION);
4742 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004743 case 'T':
4744 g_free(trace_file);
4745 trace_file = trace_opt_parse(optarg);
4746 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004747 }
bellardea2384d2004-08-01 21:59:26 +00004748 }
Stuart Brady153859b2009-06-07 00:42:17 +01004749
Denis V. Lunev10985132016-06-17 17:44:13 +03004750 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004751
Denis V. Lunev10985132016-06-17 17:44:13 +03004752 /* reset getopt_long scanning */
4753 argc -= optind;
4754 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004755 return 0;
4756 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004757 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004758 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004759
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004760 if (!trace_init_backends()) {
4761 exit(1);
4762 }
4763 trace_init_file(trace_file);
4764 qemu_set_log(LOG_TRACE);
4765
Denis V. Lunev10985132016-06-17 17:44:13 +03004766 /* find the command */
4767 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4768 if (!strcmp(cmdname, cmd->name)) {
4769 return cmd->handler(argc, argv);
4770 }
4771 }
Jeff Cody7db16892014-04-25 17:02:32 -04004772
Stuart Brady153859b2009-06-07 00:42:17 +01004773 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004774 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004775}