blob: ceb4ca7ab0b4ae775e5967a92afa94ecf393c163 [file] [log] [blame]
bellardea2384d2004-08-01 21:59:26 +00001/*
bellardfb43f4d2006-08-07 21:34:46 +00002 * QEMU disk image utility
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardea2384d2004-08-01 21:59:26 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Markus Armbruster452fcdb2018-02-01 12:18:39 +010024
Joshua Duongc984de52018-09-09 22:10:37 -070025#ifdef _MSC_VER
26#define USE_QEMU_GETOPT
27#endif
28
Peter Maydell80c71a22016-01-18 18:01:42 +000029#include "qemu/osdep.h"
Joshua Duonga7038392018-10-01 17:37:02 -070030#ifndef _MSC_VER
Eric Blakec2a3d7d2017-07-21 08:50:47 -050031#include <getopt.h>
Joshua Duonga7038392018-10-01 17:37:02 -070032#endif
Eric Blakec2a3d7d2017-07-21 08:50:47 -050033
Fam Zheng67a1de02016-06-01 17:44:21 +080034#include "qemu-version.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010035#include "qapi/error.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010036#include "qapi/qapi-visit-block-core.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010037#include "qapi/qobject-output-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010038#include "qapi/qmp/qjson.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010039#include "qapi/qmp/qdict.h"
Markus Armbrusterfc81fa12018-02-01 12:18:40 +010040#include "qapi/qmp/qstring.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020041#include "qemu/cutils.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000042#include "qemu/config-file.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010043#include "qemu/option.h"
44#include "qemu/error-report.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030045#include "qemu/log.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000046#include "qom/object_interfaces.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010047#include "sysemu/sysemu.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020048#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010049#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020050#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080051#include "block/qapi.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010052#include "crypto/init.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030053#include "trace/control.h"
bellarde8445332006-06-14 15:32:10 +000054
Don Slutz61979a62015-01-09 10:17:35 -050055#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
Thomas Huth0781dd62016-10-05 11:54:44 +020056 "\n" QEMU_COPYRIGHT "\n"
Jeff Cody5f6979c2014-04-28 14:37:18 -040057
Anthony Liguoric227f092009-10-01 16:12:16 -050058typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010059 const char *name;
60 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050061} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010062
Federico Simoncelli8599ea42013-01-28 06:59:47 -050063enum {
64 OPTION_OUTPUT = 256,
65 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000066 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000067 OPTION_IMAGE_OPTS = 259,
Kevin Wolfb6495fa2015-07-10 18:09:18 +020068 OPTION_PATTERN = 260,
Kevin Wolf55d539c2016-06-03 13:59:41 +020069 OPTION_FLUSH_INTERVAL = 261,
70 OPTION_NO_DRAIN = 262,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +010071 OPTION_TARGET_IMAGE_OPTS = 263,
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +010072 OPTION_SIZE = 264,
Max Reitzdc5f6902017-06-13 22:20:55 +020073 OPTION_PREALLOCATION = 265,
Pavel Butsykin4ffca892017-09-18 15:42:27 +030074 OPTION_SHRINK = 266,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050075};
76
77typedef enum OutputFormat {
78 OFORMAT_JSON,
79 OFORMAT_HUMAN,
80} OutputFormat;
81
Kevin Wolfe6996142016-03-15 13:03:11 +010082/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040083#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000084
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010085static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000086{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010087 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000088}
89
Fam Zhengac1307a2014-04-22 13:36:11 +080090static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
91{
92 va_list ap;
93
94 error_printf("qemu-img: ");
95
96 va_start(ap, fmt);
97 error_vprintf(fmt, ap);
98 va_end(ap);
99
100 error_printf("\nTry 'qemu-img --help' for more information\n");
101 exit(EXIT_FAILURE);
102}
103
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800104static void QEMU_NORETURN missing_argument(const char *option)
105{
106 error_exit("missing argument for option '%s'", option);
107}
108
109static void QEMU_NORETURN unrecognized_option(const char *option)
110{
111 error_exit("unrecognized option '%s'", option);
112}
113
blueswir1d2c639d2009-01-24 18:19:25 +0000114/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +0800115static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +0000116{
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100117 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -0400118 QEMU_IMG_VERSION
Denis V. Lunev10985132016-06-17 17:44:13 +0300119 "usage: qemu-img [standard options] command [command options]\n"
malc3f020d72010-02-08 12:04:56 +0300120 "QEMU disk image utility\n"
121 "\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300122 " '-h', '--help' display this help and exit\n"
123 " '-V', '--version' output version information and exit\n"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +0300124 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
125 " specify tracing options\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300126 "\n"
malc3f020d72010-02-08 12:04:56 +0300127 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100128#define DEF(option, callback, arg_string) \
129 " " arg_string "\n"
130#include "qemu-img-cmds.h"
131#undef DEF
132#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +0300133 "\n"
134 "Command parameters:\n"
135 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000136 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
137 " manual page for a description of the object properties. The most common\n"
138 " object type is a 'secret', which is used to supply passwords and/or\n"
139 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300140 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400141 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800142 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
143 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100144 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
145 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300146 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200147 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
148 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
149 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300150 " 'output_filename' is the destination disk image filename\n"
151 " 'output_fmt' is the destination format\n"
152 " 'options' is a comma separated list of format specific options in a\n"
153 " name=value format. Use -o ? for an overview of the options supported by the\n"
154 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800155 " 'snapshot_param' is param used for internal snapshot, format\n"
156 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
157 " '[ID_OR_NAME]'\n"
158 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
159 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300160 " '-c' indicates that target image must be compressed (qcow format only)\n"
John Snow6e6e55f2017-07-17 20:34:22 -0400161 " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
162 " new backing file match exactly. The image doesn't need a working\n"
163 " backing file before rebasing in this case (useful for renaming the\n"
164 " backing file). For image creation, allow creating without attempting\n"
165 " to open the backing file.\n"
malc3f020d72010-02-08 12:04:56 +0300166 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200167 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100168 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200169 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
170 " contain only zeros for qemu-img to create a sparse image during\n"
171 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
172 " unallocated or zero sectors, and the destination image will always be\n"
173 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200174 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100175 " '-n' skips the target volume creation (useful if the volume is created\n"
176 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300177 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200178 "Parameters to check subcommand:\n"
179 " '-r' tries to repair any inconsistencies that are found during the check.\n"
180 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
181 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200182 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200183 "\n"
Peter Lieven2d9187b2017-02-28 13:40:07 +0100184 "Parameters to convert subcommand:\n"
185 " '-m' specifies how many coroutines work in parallel during the convert\n"
186 " process (defaults to 8)\n"
187 " '-W' allow to write to the target out of order rather than sequential\n"
188 "\n"
malc3f020d72010-02-08 12:04:56 +0300189 "Parameters to snapshot subcommand:\n"
190 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
191 " '-a' applies a snapshot (revert disk to saved state)\n"
192 " '-c' creates a snapshot\n"
193 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100194 " '-l' lists all snapshots in the given image\n"
195 "\n"
196 "Parameters to compare subcommand:\n"
197 " '-f' first image format\n"
198 " '-F' second image format\n"
Reda Sallahi86ce1f62016-08-10 04:43:12 +0200199 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
200 "\n"
201 "Parameters to dd subcommand:\n"
202 " 'bs=BYTES' read and write up to BYTES bytes at a time "
203 "(default: 512)\n"
204 " 'count=N' copy only N input blocks\n"
205 " 'if=FILE' read from FILE\n"
Reda Sallahif7c15532016-08-10 16:16:09 +0200206 " 'of=FILE' write to FILE\n"
207 " 'skip=N' skip N bs-sized blocks at the start of input\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100208
209 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100210 bdrv_iterate_format(format_print, NULL);
Eric Blakef5048cb2017-08-03 11:33:53 -0500211 printf("\n\n" QEMU_HELP_BOTTOM "\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800212 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000213}
214
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000215static QemuOptsList qemu_object_opts = {
216 .name = "object",
217 .implied_opt_name = "qom-type",
218 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
219 .desc = {
220 { }
221 },
222};
223
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000224static QemuOptsList qemu_source_opts = {
225 .name = "source",
226 .implied_opt_name = "file",
227 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
228 .desc = {
229 { }
230 },
231};
232
Stefan Weil7c30f652013-06-16 17:01:05 +0200233static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100234{
235 int ret = 0;
236 if (!quiet) {
237 va_list args;
238 va_start(args, fmt);
239 ret = vprintf(fmt, args);
240 va_end(args);
241 }
242 return ret;
243}
244
bellardea2384d2004-08-01 21:59:26 +0000245
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100246static int print_block_option_help(const char *filename, const char *fmt)
247{
248 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800249 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500250 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100251
252 /* Find driver and parse its options */
253 drv = bdrv_find_format(fmt);
254 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100255 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100256 return 1;
257 }
258
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800259 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100260 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500261 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100262 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100263 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800264 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100265 return 1;
266 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800267 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100268 }
269
Chunyan Liu83d05212014-06-05 17:20:51 +0800270 qemu_opts_print_help(create_opts);
271 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100272 return 0;
273}
274
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000275
Max Reitzefaa7c42016-03-16 19:54:38 +0100276static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100277 QemuOpts *opts, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800278 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000279{
280 QDict *options;
281 Error *local_err = NULL;
282 BlockBackend *blk;
283 options = qemu_opts_to_qdict(opts, NULL);
Fam Zheng335e9932017-05-03 00:35:39 +0800284 if (force_share) {
285 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
286 && !qdict_get_bool(options, BDRV_OPT_FORCE_SHARE)) {
287 error_report("--force-share/-U conflicts with image options");
Fam Zhengadb998c2017-05-15 22:10:14 +0800288 QDECREF(options);
Fam Zheng335e9932017-05-03 00:35:39 +0800289 return NULL;
290 }
Eric Blake579cf1d2017-05-15 14:54:39 -0500291 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800292 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100293 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000294 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100295 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000296 return NULL;
297 }
Kevin Wolfce099542016-03-15 13:01:04 +0100298 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000299
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000300 return blk;
301}
302
Max Reitzefaa7c42016-03-16 19:54:38 +0100303static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100304 QDict *options,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000305 const char *fmt, int flags,
Fam Zheng335e9932017-05-03 00:35:39 +0800306 bool writethrough, bool quiet,
307 bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000308{
309 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200310 Error *local_err = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100311
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100312 if (!options) {
Max Reitz5bd31322015-02-05 13:58:16 -0500313 options = qdict_new();
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100314 }
bellard75c23802004-08-27 21:28:58 +0000315 if (fmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500316 qdict_put_str(options, "driver", fmt);
bellard75c23802004-08-27 21:28:58 +0000317 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100318
Fam Zheng335e9932017-05-03 00:35:39 +0800319 if (force_share) {
Eric Blake579cf1d2017-05-15 14:54:39 -0500320 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800321 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100322 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500323 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100324 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000325 return NULL;
bellard75c23802004-08-27 21:28:58 +0000326 }
Kevin Wolfce099542016-03-15 13:01:04 +0100327 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100328
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200329 return blk;
bellard75c23802004-08-27 21:28:58 +0000330}
331
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000332
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100333static int img_add_key_secrets(void *opaque,
334 const char *name, const char *value,
335 Error **errp)
336{
337 QDict *options = opaque;
338
339 if (g_str_has_suffix(name, "key-secret")) {
Eric Blake187f47e2017-06-24 12:10:07 -0600340 qdict_put_str(options, name, value);
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100341 }
342
343 return 0;
344}
345
346static BlockBackend *img_open_new_file(const char *filename,
347 QemuOpts *create_opts,
348 const char *fmt, int flags,
349 bool writethrough, bool quiet,
350 bool force_share)
351{
352 QDict *options = NULL;
353
354 options = qdict_new();
355 qemu_opt_foreach(create_opts, img_add_key_secrets, options, &error_abort);
356
357 return img_open_file(filename, options, fmt, flags, writethrough, quiet,
358 force_share);
359}
360
361
Max Reitzefaa7c42016-03-16 19:54:38 +0100362static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000363 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100364 const char *fmt, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800365 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000366{
367 BlockBackend *blk;
368 if (image_opts) {
369 QemuOpts *opts;
370 if (fmt) {
371 error_report("--image-opts and --format are mutually exclusive");
372 return NULL;
373 }
374 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
375 filename, true);
376 if (!opts) {
377 return NULL;
378 }
Fam Zheng335e9932017-05-03 00:35:39 +0800379 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
380 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000381 } else {
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100382 blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
Fam Zheng335e9932017-05-03 00:35:39 +0800383 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000384 }
385 return blk;
386}
387
388
Chunyan Liu83d05212014-06-05 17:20:51 +0800389static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d9e2010-12-07 17:44:34 +0100390 const char *base_filename,
391 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200392{
Markus Armbruster6750e792015-02-12 17:43:08 +0100393 Error *err = NULL;
394
Kevin Wolfefa84d42009-05-18 16:42:12 +0200395 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100396 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100397 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100398 error_report("Backing file not supported for file format '%s'",
399 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100400 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900401 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200402 }
403 }
404 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100405 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100406 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100407 error_report("Backing file format not supported for file "
408 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100409 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900410 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200411 }
412 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900413 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200414}
415
Markus Armbruster606caa02017-02-21 21:14:04 +0100416static int64_t cvtnum(const char *s)
417{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100418 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100419 uint64_t value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100420
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100421 err = qemu_strtosz(s, NULL, &value);
422 if (err < 0) {
423 return err;
424 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100425 if (value > INT64_MAX) {
426 return -ERANGE;
427 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100428 return value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100429}
430
bellardea2384d2004-08-01 21:59:26 +0000431static int img_create(int argc, char **argv)
432{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200433 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100434 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000435 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000436 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000437 const char *filename;
438 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200439 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200440 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100441 bool quiet = false;
John Snow6e6e55f2017-07-17 20:34:22 -0400442 int flags = 0;
ths3b46e622007-09-17 08:09:54 +0000443
bellardea2384d2004-08-01 21:59:26 +0000444 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000445 static const struct option long_options[] = {
446 {"help", no_argument, 0, 'h'},
447 {"object", required_argument, 0, OPTION_OBJECT},
448 {0, 0, 0, 0}
449 };
John Snow6e6e55f2017-07-17 20:34:22 -0400450 c = getopt_long(argc, argv, ":F:b:f:ho:qu",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000451 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100452 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000453 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100454 }
bellardea2384d2004-08-01 21:59:26 +0000455 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800456 case ':':
457 missing_argument(argv[optind - 1]);
458 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100459 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800460 unrecognized_option(argv[optind - 1]);
461 break;
bellardea2384d2004-08-01 21:59:26 +0000462 case 'h':
463 help();
464 break;
aliguori9230eaf2009-03-28 17:55:19 +0000465 case 'F':
466 base_fmt = optarg;
467 break;
bellardea2384d2004-08-01 21:59:26 +0000468 case 'b':
469 base_filename = optarg;
470 break;
471 case 'f':
472 fmt = optarg;
473 break;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200474 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100475 if (!is_valid_option_list(optarg)) {
476 error_report("Invalid option list: %s", optarg);
477 goto fail;
478 }
479 if (!options) {
480 options = g_strdup(optarg);
481 } else {
482 char *old_options = options;
483 options = g_strdup_printf("%s,%s", options, optarg);
484 g_free(old_options);
485 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200486 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100487 case 'q':
488 quiet = true;
489 break;
John Snow6e6e55f2017-07-17 20:34:22 -0400490 case 'u':
491 flags |= BDRV_O_NO_BACKING;
492 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000493 case OPTION_OBJECT: {
494 QemuOpts *opts;
495 opts = qemu_opts_parse_noisily(&qemu_object_opts,
496 optarg, true);
497 if (!opts) {
498 goto fail;
499 }
500 } break;
bellardea2384d2004-08-01 21:59:26 +0000501 }
502 }
aliguori9230eaf2009-03-28 17:55:19 +0000503
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900504 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100505 filename = (optind < argc) ? argv[optind] : NULL;
506 if (options && has_help_option(options)) {
507 g_free(options);
508 return print_block_option_help(filename, fmt);
509 }
510
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100511 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800512 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100513 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100514 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900515
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000516 if (qemu_opts_foreach(&qemu_object_opts,
517 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200518 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000519 goto fail;
520 }
521
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100522 /* Get image size, if specified */
523 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100524 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100525
526 sval = cvtnum(argv[optind++]);
527 if (sval < 0) {
liguang79443392012-12-17 09:49:23 +0800528 if (sval == -ERANGE) {
529 error_report("Image size must be less than 8 EiB!");
530 } else {
531 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200532 "G, T, P or E suffixes for ");
533 error_report("kilobytes, megabytes, gigabytes, terabytes, "
534 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800535 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100536 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100537 }
538 img_size = (uint64_t)sval;
539 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200540 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800541 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200542 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100543
Luiz Capitulino9b375252012-11-30 10:52:05 -0200544 bdrv_img_create(filename, fmt, base_filename, base_fmt,
John Snow6e6e55f2017-07-17 20:34:22 -0400545 options, img_size, flags, quiet, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100546 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100547 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100548 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900549 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200550
Kevin Wolf77386bf2014-02-21 16:24:04 +0100551 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000552 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100553
554fail:
555 g_free(options);
556 return 1;
bellardea2384d2004-08-01 21:59:26 +0000557}
558
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100559static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500560{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500561 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500562 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100563 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600564
565 visit_type_ImageCheck(v, NULL, &check, &error_abort);
566 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500567 str = qobject_to_json_pretty(obj);
568 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100569 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500570 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600571 visit_free(v);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500572 QDECREF(str);
573}
574
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100575static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500576{
577 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100578 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500579 } else {
580 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100581 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
582 "Data may be corrupted, or further writes to the image "
583 "may corrupt it.\n",
584 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500585 }
586
587 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100588 qprintf(quiet,
589 "\n%" PRId64 " leaked clusters were found on the image.\n"
590 "This means waste of disk space, but no harm to data.\n",
591 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500592 }
593
594 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100595 qprintf(quiet,
596 "\n%" PRId64
597 " internal errors have occurred during the check.\n",
598 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500599 }
600 }
601
602 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100603 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
604 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
605 check->allocated_clusters, check->total_clusters,
606 check->allocated_clusters * 100.0 / check->total_clusters,
607 check->fragmented_clusters * 100.0 / check->allocated_clusters,
608 check->compressed_clusters * 100.0 /
609 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500610 }
611
612 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100613 qprintf(quiet,
614 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500615 }
616}
617
618static int collect_image_check(BlockDriverState *bs,
619 ImageCheck *check,
620 const char *filename,
621 const char *fmt,
622 int fix)
623{
624 int ret;
625 BdrvCheckResult result;
626
627 ret = bdrv_check(bs, &result, fix);
628 if (ret < 0) {
629 return ret;
630 }
631
632 check->filename = g_strdup(filename);
633 check->format = g_strdup(bdrv_get_format_name(bs));
634 check->check_errors = result.check_errors;
635 check->corruptions = result.corruptions;
636 check->has_corruptions = result.corruptions != 0;
637 check->leaks = result.leaks;
638 check->has_leaks = result.leaks != 0;
639 check->corruptions_fixed = result.corruptions_fixed;
640 check->has_corruptions_fixed = result.corruptions != 0;
641 check->leaks_fixed = result.leaks_fixed;
642 check->has_leaks_fixed = result.leaks != 0;
643 check->image_end_offset = result.image_end_offset;
644 check->has_image_end_offset = result.image_end_offset != 0;
645 check->total_clusters = result.bfi.total_clusters;
646 check->has_total_clusters = result.bfi.total_clusters != 0;
647 check->allocated_clusters = result.bfi.allocated_clusters;
648 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
649 check->fragmented_clusters = result.bfi.fragmented_clusters;
650 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100651 check->compressed_clusters = result.bfi.compressed_clusters;
652 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500653
654 return 0;
655}
656
Kevin Wolfe076f332010-06-29 11:43:13 +0200657/*
658 * Checks an image for consistency. Exit codes:
659 *
Max Reitzd6635c42014-06-02 22:15:21 +0200660 * 0 - Check completed, image is good
661 * 1 - Check not completed because of internal errors
662 * 2 - Check completed, image is corrupted
663 * 3 - Check completed, image has leaked clusters, but is good otherwise
664 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200665 */
aliguori15859692009-04-21 23:11:53 +0000666static int img_check(int argc, char **argv)
667{
668 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500669 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200670 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200671 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000672 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200673 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100674 int flags = BDRV_O_CHECK;
675 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200676 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100677 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000678 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +0800679 bool force_share = false;
aliguori15859692009-04-21 23:11:53 +0000680
681 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500682 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200683 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100684
aliguori15859692009-04-21 23:11:53 +0000685 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500686 int option_index = 0;
687 static const struct option long_options[] = {
688 {"help", no_argument, 0, 'h'},
689 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530690 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500691 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000692 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000693 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +0800694 {"force-share", no_argument, 0, 'U'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500695 {0, 0, 0, 0}
696 };
Fam Zheng335e9932017-05-03 00:35:39 +0800697 c = getopt_long(argc, argv, ":hf:r:T:qU",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500698 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100699 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000700 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100701 }
aliguori15859692009-04-21 23:11:53 +0000702 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800703 case ':':
704 missing_argument(argv[optind - 1]);
705 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100706 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800707 unrecognized_option(argv[optind - 1]);
708 break;
aliguori15859692009-04-21 23:11:53 +0000709 case 'h':
710 help();
711 break;
712 case 'f':
713 fmt = optarg;
714 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200715 case 'r':
716 flags |= BDRV_O_RDWR;
717
718 if (!strcmp(optarg, "leaks")) {
719 fix = BDRV_FIX_LEAKS;
720 } else if (!strcmp(optarg, "all")) {
721 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
722 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800723 error_exit("Unknown option value for -r "
724 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200725 }
726 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500727 case OPTION_OUTPUT:
728 output = optarg;
729 break;
Max Reitz40055952014-07-22 22:58:42 +0200730 case 'T':
731 cache = optarg;
732 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100733 case 'q':
734 quiet = true;
735 break;
Fam Zheng335e9932017-05-03 00:35:39 +0800736 case 'U':
737 force_share = true;
738 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000739 case OPTION_OBJECT: {
740 QemuOpts *opts;
741 opts = qemu_opts_parse_noisily(&qemu_object_opts,
742 optarg, true);
743 if (!opts) {
744 return 1;
745 }
746 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000747 case OPTION_IMAGE_OPTS:
748 image_opts = true;
749 break;
aliguori15859692009-04-21 23:11:53 +0000750 }
751 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200752 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800753 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100754 }
aliguori15859692009-04-21 23:11:53 +0000755 filename = argv[optind++];
756
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500757 if (output && !strcmp(output, "json")) {
758 output_format = OFORMAT_JSON;
759 } else if (output && !strcmp(output, "human")) {
760 output_format = OFORMAT_HUMAN;
761 } else if (output) {
762 error_report("--output must be used with human or json as argument.");
763 return 1;
764 }
765
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000766 if (qemu_opts_foreach(&qemu_object_opts,
767 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200768 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000769 return 1;
770 }
771
Kevin Wolfce099542016-03-15 13:01:04 +0100772 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200773 if (ret < 0) {
774 error_report("Invalid source cache option: %s", cache);
775 return 1;
776 }
777
Fam Zheng335e9932017-05-03 00:35:39 +0800778 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
779 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200780 if (!blk) {
781 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900782 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200783 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500784
785 check = g_new0(ImageCheck, 1);
786 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200787
788 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200789 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200790 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500791 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200792 }
793
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500794 if (check->corruptions_fixed || check->leaks_fixed) {
795 int corruptions_fixed, leaks_fixed;
796
797 leaks_fixed = check->leaks_fixed;
798 corruptions_fixed = check->corruptions_fixed;
799
800 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100801 qprintf(quiet,
802 "The following inconsistencies were found and repaired:\n\n"
803 " %" PRId64 " leaked clusters\n"
804 " %" PRId64 " corruptions\n\n"
805 "Double checking the fixed image now...\n",
806 check->leaks_fixed,
807 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500808 }
809
810 ret = collect_image_check(bs, check, filename, fmt, 0);
811
812 check->leaks_fixed = leaks_fixed;
813 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200814 }
815
Max Reitz832390a2014-10-23 15:29:12 +0200816 if (!ret) {
817 switch (output_format) {
818 case OFORMAT_HUMAN:
819 dump_human_image_check(check, quiet);
820 break;
821 case OFORMAT_JSON:
822 dump_json_image_check(check, quiet);
823 break;
824 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500825 }
826
827 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200828 if (ret) {
829 error_report("Check failed: %s", strerror(-ret));
830 } else {
831 error_report("Check failed");
832 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500833 ret = 1;
834 goto fail;
835 }
836
837 if (check->corruptions) {
838 ret = 2;
839 } else if (check->leaks) {
840 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200841 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500842 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000843 }
844
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500845fail:
846 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200847 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500848 return ret;
aliguori15859692009-04-21 23:11:53 +0000849}
850
Max Reitzd4a32382014-10-24 15:57:37 +0200851typedef struct CommonBlockJobCBInfo {
852 BlockDriverState *bs;
853 Error **errp;
854} CommonBlockJobCBInfo;
855
856static void common_block_job_cb(void *opaque, int ret)
857{
858 CommonBlockJobCBInfo *cbi = opaque;
859
860 if (ret < 0) {
861 error_setg_errno(cbi->errp, -ret, "Block job failed");
862 }
Max Reitzd4a32382014-10-24 15:57:37 +0200863}
864
865static void run_block_job(BlockJob *job, Error **errp)
866{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200867 AioContext *aio_context = blk_get_aio_context(job->blk);
sochin.jiang4172a002017-06-15 14:47:33 +0800868 int ret = 0;
Max Reitzd4a32382014-10-24 15:57:37 +0200869
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200870 aio_context_acquire(aio_context);
sochin.jiang4172a002017-06-15 14:47:33 +0800871 block_job_ref(job);
Max Reitzd4a32382014-10-24 15:57:37 +0200872 do {
873 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500874 qemu_progress_print(job->len ?
875 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
sochin.jiang4172a002017-06-15 14:47:33 +0800876 } while (!job->ready && !job->completed);
Max Reitzd4a32382014-10-24 15:57:37 +0200877
sochin.jiang4172a002017-06-15 14:47:33 +0800878 if (!job->completed) {
879 ret = block_job_complete_sync(job, errp);
880 } else {
881 ret = job->ret;
882 }
883 block_job_unref(job);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200884 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200885
sochin.jiang4172a002017-06-15 14:47:33 +0800886 /* publish completion progress only when success */
887 if (!ret) {
888 qemu_progress_print(100.f, 0);
889 }
Max Reitzd4a32382014-10-24 15:57:37 +0200890}
891
bellardea2384d2004-08-01 21:59:26 +0000892static int img_commit(int argc, char **argv)
893{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400894 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200895 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200896 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200897 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100898 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200899 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100900 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200901 Error *local_err = NULL;
902 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000903 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200904 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000905
906 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400907 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200908 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000909 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000910 static const struct option long_options[] = {
911 {"help", no_argument, 0, 'h'},
912 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000913 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000914 {0, 0, 0, 0}
915 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800916 c = getopt_long(argc, argv, ":f:ht:b:dpq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000917 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100918 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000919 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100920 }
bellardea2384d2004-08-01 21:59:26 +0000921 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800922 case ':':
923 missing_argument(argv[optind - 1]);
924 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100925 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800926 unrecognized_option(argv[optind - 1]);
927 break;
bellardea2384d2004-08-01 21:59:26 +0000928 case 'h':
929 help();
930 break;
931 case 'f':
932 fmt = optarg;
933 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400934 case 't':
935 cache = optarg;
936 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200937 case 'b':
938 base = optarg;
939 /* -b implies -d */
940 drop = true;
941 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200942 case 'd':
943 drop = true;
944 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200945 case 'p':
946 progress = true;
947 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100948 case 'q':
949 quiet = true;
950 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000951 case OPTION_OBJECT: {
952 QemuOpts *opts;
953 opts = qemu_opts_parse_noisily(&qemu_object_opts,
954 optarg, true);
955 if (!opts) {
956 return 1;
957 }
958 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000959 case OPTION_IMAGE_OPTS:
960 image_opts = true;
961 break;
bellardea2384d2004-08-01 21:59:26 +0000962 }
963 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200964
965 /* Progress is not shown in Quiet mode */
966 if (quiet) {
967 progress = false;
968 }
969
Kevin Wolffc11eb22013-08-05 10:53:04 +0200970 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800971 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100972 }
bellardea2384d2004-08-01 21:59:26 +0000973 filename = argv[optind++];
974
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000975 if (qemu_opts_foreach(&qemu_object_opts,
976 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200977 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000978 return 1;
979 }
980
Max Reitz9a86fe42014-10-24 15:57:38 +0200981 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100982 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400983 if (ret < 0) {
984 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100985 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400986 }
987
Fam Zheng335e9932017-05-03 00:35:39 +0800988 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
989 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200990 if (!blk) {
991 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900992 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200993 bs = blk_bs(blk);
994
Max Reitz687fa1d2014-10-24 15:57:39 +0200995 qemu_progress_init(progress, 1.f);
996 qemu_progress_print(0.f, 100);
997
Max Reitz1b22bff2014-10-24 15:57:40 +0200998 if (base) {
999 base_bs = bdrv_find_backing_image(bs, base);
1000 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +01001001 error_setg(&local_err,
1002 "Did not find '%s' in the backing chain of '%s'",
1003 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +02001004 goto done;
1005 }
1006 } else {
1007 /* This is different from QMP, which by default uses the deepest file in
1008 * the backing chain (i.e., the very base); however, the traditional
1009 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +02001010 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +02001011 if (!base_bs) {
1012 error_setg(&local_err, "Image does not have a backing file");
1013 goto done;
1014 }
bellardea2384d2004-08-01 21:59:26 +00001015 }
1016
Max Reitzd4a32382014-10-24 15:57:37 +02001017 cbi = (CommonBlockJobCBInfo){
1018 .errp = &local_err,
1019 .bs = bs,
1020 };
1021
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001022 aio_context = bdrv_get_aio_context(bs);
1023 aio_context_acquire(aio_context);
John Snow47970df2016-10-27 12:06:57 -04001024 commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
Kevin Wolf0db832f2017-02-20 18:10:05 +01001025 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +08001026 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001027 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +02001028 if (local_err) {
1029 goto done;
1030 }
1031
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001032 /* When the block job completes, the BlockBackend reference will point to
1033 * the old backing file. In order to avoid that the top image is already
1034 * deleted, so we can still empty it afterwards, increment the reference
1035 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +02001036 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001037 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001038 }
1039
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001040 job = block_job_get("commit");
1041 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001042 if (local_err) {
1043 goto unref_backing;
1044 }
1045
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001046 if (!drop && bs->drv->bdrv_make_empty) {
1047 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001048 if (ret) {
1049 error_setg_errno(&local_err, -ret, "Could not empty %s",
1050 filename);
1051 goto unref_backing;
1052 }
1053 }
1054
1055unref_backing:
1056 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001057 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001058 }
Max Reitzd4a32382014-10-24 15:57:37 +02001059
1060done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001061 qemu_progress_end();
1062
Markus Armbruster26f54e92014-10-07 13:59:04 +02001063 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001064
1065 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001066 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001067 return 1;
1068 }
Max Reitzd4a32382014-10-24 15:57:37 +02001069
1070 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001071 return 0;
1072}
1073
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001074/*
Eric Blakedebb38a2017-10-11 22:47:11 -05001075 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1076 * of the first sector boundary within buf where the sector contains a
1077 * non-zero byte. This function is robust to a buffer that is not
1078 * sector-aligned.
1079 */
1080static int64_t find_nonzero(const uint8_t *buf, int64_t n)
1081{
1082 int64_t i;
1083 int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE);
1084
1085 for (i = 0; i < end; i += BDRV_SECTOR_SIZE) {
1086 if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) {
1087 return i;
1088 }
1089 }
1090 if (i < n && !buffer_is_zero(buf + i, n - end)) {
1091 return i;
1092 }
1093 return -1;
1094}
1095
1096/*
thsf58c7b32008-06-05 21:53:49 +00001097 * Returns true iff the first sector pointed to by 'buf' contains at least
1098 * a non-NUL byte.
1099 *
1100 * 'pnum' is set to the number of sectors (including and immediately following
1101 * the first one) that are known to be in the same allocated/unallocated state.
1102 */
bellardea2384d2004-08-01 21:59:26 +00001103static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1104{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001105 bool is_zero;
1106 int i;
bellardea2384d2004-08-01 21:59:26 +00001107
1108 if (n <= 0) {
1109 *pnum = 0;
1110 return 0;
1111 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001112 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001113 for(i = 1; i < n; i++) {
1114 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001115 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001116 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001117 }
bellardea2384d2004-08-01 21:59:26 +00001118 }
1119 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001120 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001121}
1122
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001123/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001124 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1125 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1126 * breaking up write requests for only small sparse areas.
1127 */
1128static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1129 int min)
1130{
1131 int ret;
1132 int num_checked, num_used;
1133
1134 if (n < min) {
1135 min = n;
1136 }
1137
1138 ret = is_allocated_sectors(buf, n, pnum);
1139 if (!ret) {
1140 return ret;
1141 }
1142
1143 num_used = *pnum;
1144 buf += BDRV_SECTOR_SIZE * *pnum;
1145 n -= *pnum;
1146 num_checked = num_used;
1147
1148 while (n > 0) {
1149 ret = is_allocated_sectors(buf, n, pnum);
1150
1151 buf += BDRV_SECTOR_SIZE * *pnum;
1152 n -= *pnum;
1153 num_checked += *pnum;
1154 if (ret) {
1155 num_used = num_checked;
1156 } else if (*pnum >= min) {
1157 break;
1158 }
1159 }
1160
1161 *pnum = num_used;
1162 return 1;
1163}
1164
1165/*
Eric Blakedc61cd32017-10-11 22:47:14 -05001166 * Compares two buffers sector by sector. Returns 0 if the first
1167 * sector of each buffer matches, non-zero otherwise.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001168 *
Eric Blakedc61cd32017-10-11 22:47:14 -05001169 * pnum is set to the sector-aligned size of the buffer prefix that
1170 * has the same matching status as the first sector.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001171 */
Eric Blakedc61cd32017-10-11 22:47:14 -05001172static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2,
1173 int64_t bytes, int64_t *pnum)
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001174{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001175 bool res;
Eric Blakedc61cd32017-10-11 22:47:14 -05001176 int64_t i = MIN(bytes, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001177
Eric Blakedc61cd32017-10-11 22:47:14 -05001178 assert(bytes > 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001179
Eric Blakedc61cd32017-10-11 22:47:14 -05001180 res = !!memcmp(buf1, buf2, i);
1181 while (i < bytes) {
1182 int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001183
Eric Blakedc61cd32017-10-11 22:47:14 -05001184 if (!!memcmp(buf1 + i, buf2 + i, len) != res) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001185 break;
1186 }
Eric Blakedc61cd32017-10-11 22:47:14 -05001187 i += len;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001188 }
1189
1190 *pnum = i;
1191 return res;
1192}
1193
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001194#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001195
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001196/*
1197 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1198 *
Eric Blake0608e402017-10-11 22:47:12 -05001199 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1200 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1201 * failure), and 4 on error (the exit status for read errors), after emitting
1202 * an error message.
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001203 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001204 * @param blk: BlockBackend for the image
Eric Blakec41508e2017-10-11 22:47:13 -05001205 * @param offset: Starting offset to check
1206 * @param bytes: Number of bytes to check
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001207 * @param filename: Name of disk file we are checking (logging purpose)
1208 * @param buffer: Allocated buffer for storing read data
1209 * @param quiet: Flag for quiet mode
1210 */
Eric Blakec41508e2017-10-11 22:47:13 -05001211static int check_empty_sectors(BlockBackend *blk, int64_t offset,
1212 int64_t bytes, const char *filename,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001213 uint8_t *buffer, bool quiet)
1214{
Eric Blakedebb38a2017-10-11 22:47:11 -05001215 int ret = 0;
1216 int64_t idx;
1217
Eric Blakec41508e2017-10-11 22:47:13 -05001218 ret = blk_pread(blk, offset, buffer, bytes);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001219 if (ret < 0) {
1220 error_report("Error while reading offset %" PRId64 " of %s: %s",
Eric Blakec41508e2017-10-11 22:47:13 -05001221 offset, filename, strerror(-ret));
Eric Blake0608e402017-10-11 22:47:12 -05001222 return 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001223 }
Eric Blakec41508e2017-10-11 22:47:13 -05001224 idx = find_nonzero(buffer, bytes);
Eric Blakedebb38a2017-10-11 22:47:11 -05001225 if (idx >= 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001226 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blakec41508e2017-10-11 22:47:13 -05001227 offset + idx);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001228 return 1;
1229 }
1230
1231 return 0;
1232}
1233
1234/*
1235 * Compares two images. Exit codes:
1236 *
1237 * 0 - Images are identical
1238 * 1 - Images differ
1239 * >1 - Error occurred
1240 */
1241static int img_compare(int argc, char **argv)
1242{
Max Reitz40055952014-07-22 22:58:42 +02001243 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001244 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001245 BlockDriverState *bs1, *bs2;
Eric Blake033d9fc2017-10-11 22:47:16 -05001246 int64_t total_size1, total_size2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001247 uint8_t *buf1 = NULL, *buf2 = NULL;
Eric Blake31826642017-10-11 22:47:08 -05001248 int64_t pnum1, pnum2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001249 int allocated1, allocated2;
1250 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1251 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001252 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001253 bool writethrough;
Eric Blake033d9fc2017-10-11 22:47:16 -05001254 int64_t total_size;
1255 int64_t offset = 0;
1256 int64_t chunk;
Eric Blakedc61cd32017-10-11 22:47:14 -05001257 int c;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001258 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001259 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001260 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001261
Max Reitz40055952014-07-22 22:58:42 +02001262 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001263 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001264 static const struct option long_options[] = {
1265 {"help", no_argument, 0, 'h'},
1266 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001267 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001268 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001269 {0, 0, 0, 0}
1270 };
Fam Zheng335e9932017-05-03 00:35:39 +08001271 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001272 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001273 if (c == -1) {
1274 break;
1275 }
1276 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001277 case ':':
1278 missing_argument(argv[optind - 1]);
1279 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001280 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001281 unrecognized_option(argv[optind - 1]);
1282 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001283 case 'h':
1284 help();
1285 break;
1286 case 'f':
1287 fmt1 = optarg;
1288 break;
1289 case 'F':
1290 fmt2 = optarg;
1291 break;
Max Reitz40055952014-07-22 22:58:42 +02001292 case 'T':
1293 cache = optarg;
1294 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001295 case 'p':
1296 progress = true;
1297 break;
1298 case 'q':
1299 quiet = true;
1300 break;
1301 case 's':
1302 strict = true;
1303 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001304 case 'U':
1305 force_share = true;
1306 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001307 case OPTION_OBJECT: {
1308 QemuOpts *opts;
1309 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1310 optarg, true);
1311 if (!opts) {
1312 ret = 2;
1313 goto out4;
1314 }
1315 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001316 case OPTION_IMAGE_OPTS:
1317 image_opts = true;
1318 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001319 }
1320 }
1321
1322 /* Progress is not shown in Quiet mode */
1323 if (quiet) {
1324 progress = false;
1325 }
1326
1327
Kevin Wolffc11eb22013-08-05 10:53:04 +02001328 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001329 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001330 }
1331 filename1 = argv[optind++];
1332 filename2 = argv[optind++];
1333
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001334 if (qemu_opts_foreach(&qemu_object_opts,
1335 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001336 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001337 ret = 2;
1338 goto out4;
1339 }
1340
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001341 /* Initialize before goto out */
1342 qemu_progress_init(progress, 2.0);
1343
Kevin Wolfce099542016-03-15 13:01:04 +01001344 flags = 0;
1345 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001346 if (ret < 0) {
1347 error_report("Invalid source cache option: %s", cache);
1348 ret = 2;
1349 goto out3;
1350 }
1351
Fam Zheng335e9932017-05-03 00:35:39 +08001352 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1353 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001354 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001355 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001356 goto out3;
1357 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001358
Fam Zheng335e9932017-05-03 00:35:39 +08001359 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1360 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001361 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001362 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001363 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001364 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001365 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001366 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001367
Max Reitzf1d3cd72015-02-05 13:58:18 -05001368 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1369 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
Eric Blake033d9fc2017-10-11 22:47:16 -05001370 total_size1 = blk_getlength(blk1);
1371 if (total_size1 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001372 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001373 filename1, strerror(-total_size1));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001374 ret = 4;
1375 goto out;
1376 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001377 total_size2 = blk_getlength(blk2);
1378 if (total_size2 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001379 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001380 filename2, strerror(-total_size2));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001381 ret = 4;
1382 goto out;
1383 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001384 total_size = MIN(total_size1, total_size2);
1385 progress_base = MAX(total_size1, total_size2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001386
1387 qemu_progress_print(0, 100);
1388
Eric Blake033d9fc2017-10-11 22:47:16 -05001389 if (strict && total_size1 != total_size2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001390 ret = 1;
1391 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1392 goto out;
1393 }
1394
Eric Blake033d9fc2017-10-11 22:47:16 -05001395 while (offset < total_size) {
Eric Blake31826642017-10-11 22:47:08 -05001396 int status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001397
Eric Blake033d9fc2017-10-11 22:47:16 -05001398 status1 = bdrv_block_status_above(bs1, NULL, offset,
1399 total_size1 - offset, &pnum1, NULL,
1400 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001401 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001402 ret = 3;
1403 error_report("Sector allocation test failed for %s", filename1);
1404 goto out;
1405 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001406 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001407
Eric Blake033d9fc2017-10-11 22:47:16 -05001408 status2 = bdrv_block_status_above(bs2, NULL, offset,
1409 total_size2 - offset, &pnum2, NULL,
1410 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001411 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001412 ret = 3;
1413 error_report("Sector allocation test failed for %s", filename2);
1414 goto out;
1415 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001416 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
Eric Blake7daddc62017-10-11 22:47:09 -05001417
1418 assert(pnum1 && pnum2);
Eric Blake033d9fc2017-10-11 22:47:16 -05001419 chunk = MIN(pnum1, pnum2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001420
Fam Zheng25ad8e62016-01-13 16:37:41 +08001421 if (strict) {
Eric Blake31826642017-10-11 22:47:08 -05001422 if (status1 != status2) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001423 ret = 1;
1424 qprintf(quiet, "Strict mode: Offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001425 " block status mismatch!\n", offset);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001426 goto out;
1427 }
1428 }
1429 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
Eric Blake7daddc62017-10-11 22:47:09 -05001430 /* nothing to do */
Fam Zheng25ad8e62016-01-13 16:37:41 +08001431 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001432 if (allocated1) {
Eric Blakedc61cd32017-10-11 22:47:14 -05001433 int64_t pnum;
1434
Eric Blake033d9fc2017-10-11 22:47:16 -05001435 chunk = MIN(chunk, IO_BUF_SIZE);
1436 ret = blk_pread(blk1, offset, buf1, chunk);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001437 if (ret < 0) {
1438 error_report("Error while reading offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001439 " of %s: %s",
1440 offset, filename1, strerror(-ret));
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001441 ret = 4;
1442 goto out;
1443 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001444 ret = blk_pread(blk2, offset, buf2, chunk);
1445 if (ret < 0) {
1446 error_report("Error while reading offset %" PRId64
1447 " of %s: %s",
1448 offset, filename2, strerror(-ret));
1449 ret = 4;
1450 goto out;
1451 }
1452 ret = compare_buffers(buf1, buf2, chunk, &pnum);
1453 if (ret || pnum != chunk) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001454 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blake033d9fc2017-10-11 22:47:16 -05001455 offset + (ret ? 0 : pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001456 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001457 goto out;
1458 }
1459 }
1460 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001461 chunk = MIN(chunk, IO_BUF_SIZE);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001462 if (allocated1) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001463 ret = check_empty_sectors(blk1, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001464 filename1, buf1, quiet);
1465 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001466 ret = check_empty_sectors(blk2, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001467 filename2, buf1, quiet);
1468 }
1469 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001470 goto out;
1471 }
1472 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001473 offset += chunk;
1474 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001475 }
1476
Eric Blake033d9fc2017-10-11 22:47:16 -05001477 if (total_size1 != total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001478 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001479 const char *filename_over;
1480
1481 qprintf(quiet, "Warning: Image size mismatch!\n");
Eric Blake033d9fc2017-10-11 22:47:16 -05001482 if (total_size1 > total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001483 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001484 filename_over = filename1;
1485 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001486 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001487 filename_over = filename2;
1488 }
1489
Eric Blake033d9fc2017-10-11 22:47:16 -05001490 while (offset < progress_base) {
1491 ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset,
1492 progress_base - offset, &chunk,
1493 NULL, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001494 if (ret < 0) {
1495 ret = 3;
1496 error_report("Sector allocation test failed for %s",
1497 filename_over);
1498 goto out;
1499
1500 }
Eric Blake391cb1a2017-10-11 22:47:10 -05001501 if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001502 chunk = MIN(chunk, IO_BUF_SIZE);
1503 ret = check_empty_sectors(blk_over, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001504 filename_over, buf1, quiet);
1505 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001506 goto out;
1507 }
1508 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001509 offset += chunk;
1510 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001511 }
1512 }
1513
1514 qprintf(quiet, "Images are identical.\n");
1515 ret = 0;
1516
1517out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001518 qemu_vfree(buf1);
1519 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001520 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001521out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001522 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001523out3:
1524 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001525out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001526 return ret;
1527}
1528
Kevin Wolf690c7302015-03-19 13:33:32 +01001529enum ImgConvertBlockStatus {
1530 BLK_DATA,
1531 BLK_ZERO,
1532 BLK_BACKING_FILE,
1533};
1534
Peter Lieven2d9187b2017-02-28 13:40:07 +01001535#define MAX_COROUTINES 16
1536
Kevin Wolf690c7302015-03-19 13:33:32 +01001537typedef struct ImgConvertState {
1538 BlockBackend **src;
1539 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001540 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001541 int64_t total_sectors;
1542 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001543 int64_t allocated_done;
1544 int64_t sector_num;
1545 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001546 enum ImgConvertBlockStatus status;
1547 int64_t sector_next_status;
1548 BlockBackend *target;
1549 bool has_zero_init;
1550 bool compressed;
1551 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001552 bool wr_in_order;
Kevin Wolf690c7302015-03-19 13:33:32 +01001553 int min_sparse;
1554 size_t cluster_sectors;
1555 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001556 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001557 int running_coroutines;
1558 Coroutine *co[MAX_COROUTINES];
1559 int64_t wait_sector_num[MAX_COROUTINES];
1560 CoMutex lock;
1561 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001562} ImgConvertState;
1563
Peter Lieven2d9187b2017-02-28 13:40:07 +01001564static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1565 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001566{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001567 *src_cur = 0;
1568 *src_cur_offset = 0;
1569 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1570 *src_cur_offset += s->src_sectors[*src_cur];
1571 (*src_cur)++;
1572 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001573 }
1574}
1575
1576static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1577{
Eric Blake31826642017-10-11 22:47:08 -05001578 int64_t src_cur_offset;
1579 int ret, n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001580
Peter Lieven2d9187b2017-02-28 13:40:07 +01001581 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001582
1583 assert(s->total_sectors > sector_num);
1584 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1585
1586 if (s->sector_next_status <= sector_num) {
Eric Blake31826642017-10-11 22:47:08 -05001587 int64_t count = n * BDRV_SECTOR_SIZE;
1588
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001589 if (s->target_has_backing) {
Eric Blake237d78f2017-10-11 22:47:03 -05001590
1591 ret = bdrv_block_status(blk_bs(s->src[src_cur]),
1592 (sector_num - src_cur_offset) *
1593 BDRV_SECTOR_SIZE,
1594 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001595 } else {
Eric Blake31826642017-10-11 22:47:08 -05001596 ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL,
1597 (sector_num - src_cur_offset) *
1598 BDRV_SECTOR_SIZE,
1599 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001600 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001601 if (ret < 0) {
1602 return ret;
1603 }
Eric Blake31826642017-10-11 22:47:08 -05001604 n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
Kevin Wolf690c7302015-03-19 13:33:32 +01001605
1606 if (ret & BDRV_BLOCK_ZERO) {
1607 s->status = BLK_ZERO;
1608 } else if (ret & BDRV_BLOCK_DATA) {
1609 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001610 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001611 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001612 }
1613
1614 s->sector_next_status = sector_num + n;
1615 }
1616
1617 n = MIN(n, s->sector_next_status - sector_num);
1618 if (s->status == BLK_DATA) {
1619 n = MIN(n, s->buf_sectors);
1620 }
1621
1622 /* We need to write complete clusters for compressed images, so if an
1623 * unallocated area is shorter than that, we must consider the whole
1624 * cluster allocated. */
1625 if (s->compressed) {
1626 if (n < s->cluster_sectors) {
1627 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1628 s->status = BLK_DATA;
1629 } else {
1630 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1631 }
1632 }
1633
1634 return n;
1635}
1636
Peter Lieven2d9187b2017-02-28 13:40:07 +01001637static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1638 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001639{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001640 int n, ret;
1641 QEMUIOVector qiov;
1642 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001643
Kevin Wolf690c7302015-03-19 13:33:32 +01001644 assert(nb_sectors <= s->buf_sectors);
1645 while (nb_sectors > 0) {
1646 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001647 int src_cur;
1648 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001649
1650 /* In the case of compression with multiple source files, we can get a
1651 * nb_sectors that spreads into the next part. So we must be able to
1652 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001653 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1654 blk = s->src[src_cur];
1655 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001656
Peter Lieven2d9187b2017-02-28 13:40:07 +01001657 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1658 iov.iov_base = buf;
1659 iov.iov_len = n << BDRV_SECTOR_BITS;
1660 qemu_iovec_init_external(&qiov, &iov, 1);
1661
1662 ret = blk_co_preadv(
1663 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1664 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001665 if (ret < 0) {
1666 return ret;
1667 }
1668
1669 sector_num += n;
1670 nb_sectors -= n;
1671 buf += n * BDRV_SECTOR_SIZE;
1672 }
1673
1674 return 0;
1675}
1676
Peter Lieven2d9187b2017-02-28 13:40:07 +01001677
1678static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1679 int nb_sectors, uint8_t *buf,
1680 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001681{
1682 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001683 QEMUIOVector qiov;
1684 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001685
1686 while (nb_sectors > 0) {
1687 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001688 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1689
Peter Lieven2d9187b2017-02-28 13:40:07 +01001690 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001691 case BLK_BACKING_FILE:
1692 /* If we have a backing file, leave clusters unallocated that are
1693 * unallocated in the source image, so that the backing file is
1694 * visible at the respective offset. */
1695 assert(s->target_has_backing);
1696 break;
1697
1698 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001699 /* If we're told to keep the target fully allocated (-S 0) or there
1700 * is real non-zero data, we must write it. Otherwise we can treat
1701 * it as zero sectors.
1702 * Compressed clusters need to be written as a whole, so in that
1703 * case we can only save the write if the buffer is completely
1704 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001705 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001706 (!s->compressed &&
1707 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1708 (s->compressed &&
1709 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001710 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001711 iov.iov_base = buf;
1712 iov.iov_len = n << BDRV_SECTOR_BITS;
1713 qemu_iovec_init_external(&qiov, &iov, 1);
1714
1715 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
Lidong Chendb933fb2017-04-27 10:58:27 +08001716 n << BDRV_SECTOR_BITS, &qiov, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001717 if (ret < 0) {
1718 return ret;
1719 }
1720 break;
1721 }
1722 /* fall-through */
1723
1724 case BLK_ZERO:
1725 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001726 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001727 break;
1728 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001729 ret = blk_co_pwrite_zeroes(s->target,
1730 sector_num << BDRV_SECTOR_BITS,
1731 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001732 if (ret < 0) {
1733 return ret;
1734 }
1735 break;
1736 }
1737
1738 sector_num += n;
1739 nb_sectors -= n;
1740 buf += n * BDRV_SECTOR_SIZE;
1741 }
1742
1743 return 0;
1744}
1745
Peter Lieven2d9187b2017-02-28 13:40:07 +01001746static void coroutine_fn convert_co_do_copy(void *opaque)
1747{
1748 ImgConvertState *s = opaque;
1749 uint8_t *buf = NULL;
1750 int ret, i;
1751 int index = -1;
1752
1753 for (i = 0; i < s->num_coroutines; i++) {
1754 if (s->co[i] == qemu_coroutine_self()) {
1755 index = i;
1756 break;
1757 }
1758 }
1759 assert(index >= 0);
1760
1761 s->running_coroutines++;
1762 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1763
1764 while (1) {
1765 int n;
1766 int64_t sector_num;
1767 enum ImgConvertBlockStatus status;
1768
1769 qemu_co_mutex_lock(&s->lock);
1770 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1771 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001772 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001773 }
1774 n = convert_iteration_sectors(s, s->sector_num);
1775 if (n < 0) {
1776 qemu_co_mutex_unlock(&s->lock);
1777 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001778 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001779 }
1780 /* save current sector and allocation status to local variables */
1781 sector_num = s->sector_num;
1782 status = s->status;
1783 if (!s->min_sparse && s->status == BLK_ZERO) {
1784 n = MIN(n, s->buf_sectors);
1785 }
1786 /* increment global sector counter so that other coroutines can
1787 * already continue reading beyond this request */
1788 s->sector_num += n;
1789 qemu_co_mutex_unlock(&s->lock);
1790
1791 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1792 s->allocated_done += n;
1793 qemu_progress_print(100.0 * s->allocated_done /
1794 s->allocated_sectors, 0);
1795 }
1796
1797 if (status == BLK_DATA) {
1798 ret = convert_co_read(s, sector_num, n, buf);
1799 if (ret < 0) {
1800 error_report("error while reading sector %" PRId64
1801 ": %s", sector_num, strerror(-ret));
1802 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001803 }
1804 } else if (!s->min_sparse && status == BLK_ZERO) {
1805 status = BLK_DATA;
1806 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1807 }
1808
1809 if (s->wr_in_order) {
1810 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03001811 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001812 s->wait_sector_num[index] = sector_num;
1813 qemu_coroutine_yield();
1814 }
1815 s->wait_sector_num[index] = -1;
1816 }
1817
Anton Nefedovb91127e2017-04-26 11:33:15 +03001818 if (s->ret == -EINPROGRESS) {
1819 ret = convert_co_write(s, sector_num, n, buf, status);
1820 if (ret < 0) {
1821 error_report("error while writing sector %" PRId64
1822 ": %s", sector_num, strerror(-ret));
1823 s->ret = ret;
1824 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001825 }
1826
1827 if (s->wr_in_order) {
1828 /* reenter the coroutine that might have waited
1829 * for this write to complete */
1830 s->wr_offs = sector_num + n;
1831 for (i = 0; i < s->num_coroutines; i++) {
1832 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1833 /*
1834 * A -> B -> A cannot occur because A has
1835 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1836 * B will never enter A during this time window.
1837 */
1838 qemu_coroutine_enter(s->co[i]);
1839 break;
1840 }
1841 }
1842 }
1843 }
1844
Peter Lieven2d9187b2017-02-28 13:40:07 +01001845 qemu_vfree(buf);
1846 s->co[index] = NULL;
1847 s->running_coroutines--;
1848 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1849 /* the convert job finished successfully */
1850 s->ret = 0;
1851 }
1852}
1853
Kevin Wolf690c7302015-03-19 13:33:32 +01001854static int convert_do_copy(ImgConvertState *s)
1855{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001856 int ret, i, n;
1857 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001858
1859 /* Check whether we have zero initialisation or can get it efficiently */
1860 s->has_zero_init = s->min_sparse && !s->target_has_backing
1861 ? bdrv_has_zero_init(blk_bs(s->target))
1862 : false;
1863
1864 if (!s->has_zero_init && !s->target_has_backing &&
1865 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1866 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001867 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001868 if (ret == 0) {
1869 s->has_zero_init = true;
1870 }
1871 }
1872
1873 /* Allocate buffer for copied data. For compressed images, only one cluster
1874 * can be copied at a time. */
1875 if (s->compressed) {
1876 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1877 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001878 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001879 }
1880 s->buf_sectors = s->cluster_sectors;
1881 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001882
Kevin Wolf690c7302015-03-19 13:33:32 +01001883 while (sector_num < s->total_sectors) {
1884 n = convert_iteration_sectors(s, sector_num);
1885 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001886 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001887 }
Max Reitzaad15de2016-03-24 23:33:57 +01001888 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1889 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001890 s->allocated_sectors += n;
1891 }
1892 sector_num += n;
1893 }
1894
1895 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001896 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001897 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001898
Peter Lieven2d9187b2017-02-28 13:40:07 +01001899 qemu_co_mutex_init(&s->lock);
1900 for (i = 0; i < s->num_coroutines; i++) {
1901 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1902 s->wait_sector_num[i] = -1;
1903 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001904 }
1905
Anton Nefedovb91127e2017-04-26 11:33:15 +03001906 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001907 main_loop_wait(false);
1908 }
1909
1910 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001911 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001912 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001913 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001914 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001915 }
1916 }
1917
Peter Lieven2d9187b2017-02-28 13:40:07 +01001918 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001919}
1920
bellardea2384d2004-08-01 21:59:26 +00001921static int img_convert(int argc, char **argv)
1922{
Peter Lieven9fd77f92017-04-21 11:11:55 +02001923 int c, bs_i, flags, src_flags = 0;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001924 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02001925 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1926 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001927 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00001928 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001929 BlockDriverState *out_bs;
1930 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08001931 QemuOptsList *create_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001932 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02001933 Error *local_err = NULL;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001934 bool writethrough, src_writethrough, quiet = false, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001935 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001936 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08001937 bool force_share = false;
bellardea2384d2004-08-01 21:59:26 +00001938
Peter Lieven9fd77f92017-04-21 11:11:55 +02001939 ImgConvertState s = (ImgConvertState) {
1940 /* Need at least 4k of zeros for sparse detection */
1941 .min_sparse = 8,
1942 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1943 .wr_in_order = true,
1944 .num_coroutines = 8,
1945 };
1946
bellardea2384d2004-08-01 21:59:26 +00001947 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001948 static const struct option long_options[] = {
1949 {"help", no_argument, 0, 'h'},
1950 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001951 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001952 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001953 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001954 {0, 0, 0, 0}
1955 };
Daniel P. Berrange6b4df542017-07-04 13:30:09 +01001956 c = getopt_long(argc, argv, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001957 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001958 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001959 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001960 }
bellardea2384d2004-08-01 21:59:26 +00001961 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001962 case ':':
1963 missing_argument(argv[optind - 1]);
1964 break;
Jes Sorensenef873942010-12-06 15:25:40 +01001965 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001966 unrecognized_option(argv[optind - 1]);
1967 break;
bellardea2384d2004-08-01 21:59:26 +00001968 case 'h':
1969 help();
1970 break;
1971 case 'f':
1972 fmt = optarg;
1973 break;
1974 case 'O':
1975 out_fmt = optarg;
1976 break;
thsf58c7b32008-06-05 21:53:49 +00001977 case 'B':
1978 out_baseimg = optarg;
1979 break;
bellardea2384d2004-08-01 21:59:26 +00001980 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02001981 s.compressed = true;
bellardea2384d2004-08-01 21:59:26 +00001982 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001983 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001984 if (!is_valid_option_list(optarg)) {
1985 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001986 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001987 }
1988 if (!options) {
1989 options = g_strdup(optarg);
1990 } else {
1991 char *old_options = options;
1992 options = g_strdup_printf("%s,%s", options, optarg);
1993 g_free(old_options);
1994 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001995 break;
edison51ef6722010-09-21 19:58:41 -07001996 case 's':
1997 snapshot_name = optarg;
1998 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001999 case 'l':
2000 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01002001 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2002 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08002003 if (!sn_opts) {
2004 error_report("Failed in parsing snapshot param '%s'",
2005 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002006 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002007 }
2008 } else {
2009 snapshot_name = optarg;
2010 }
2011 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002012 case 'S':
2013 {
2014 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002015
2016 sval = cvtnum(optarg);
2017 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02002018 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002019 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002020 }
2021
Peter Lieven9fd77f92017-04-21 11:11:55 +02002022 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002023 break;
2024 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002025 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002026 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002027 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002028 case 't':
2029 cache = optarg;
2030 break;
Max Reitz40055952014-07-22 22:58:42 +02002031 case 'T':
2032 src_cache = optarg;
2033 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002034 case 'q':
2035 quiet = true;
2036 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002037 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002038 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002039 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002040 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002041 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2042 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002043 error_report("Invalid number of coroutines. Allowed number of"
2044 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002045 goto fail_getopt;
2046 }
2047 break;
2048 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002049 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002050 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002051 case 'U':
2052 force_share = true;
2053 break;
Max Reitz3258b912017-04-26 15:46:47 +02002054 case OPTION_OBJECT: {
2055 QemuOpts *object_opts;
2056 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2057 optarg, true);
2058 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002059 goto fail_getopt;
2060 }
2061 break;
Max Reitz3258b912017-04-26 15:46:47 +02002062 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002063 case OPTION_IMAGE_OPTS:
2064 image_opts = true;
2065 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002066 case OPTION_TARGET_IMAGE_OPTS:
2067 tgt_image_opts = true;
2068 break;
bellardea2384d2004-08-01 21:59:26 +00002069 }
2070 }
ths3b46e622007-09-17 08:09:54 +00002071
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002072 if (!out_fmt && !tgt_image_opts) {
2073 out_fmt = "raw";
2074 }
2075
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002076 if (qemu_opts_foreach(&qemu_object_opts,
2077 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002078 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002079 goto fail_getopt;
2080 }
2081
Peter Lieven9fd77f92017-04-21 11:11:55 +02002082 if (!s.wr_in_order && s.compressed) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002083 error_report("Out of order write and compress are mutually exclusive");
Peter Lieven9fd77f92017-04-21 11:11:55 +02002084 goto fail_getopt;
2085 }
2086
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002087 if (tgt_image_opts && !skip_create) {
2088 error_report("--target-image-opts requires use of -n flag");
2089 goto fail_getopt;
2090 }
2091
Peter Lieven9fd77f92017-04-21 11:11:55 +02002092 s.src_num = argc - optind - 1;
2093 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2094
2095 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002096 if (out_fmt) {
2097 ret = print_block_option_help(out_filename, out_fmt);
2098 goto fail_getopt;
2099 } else {
2100 error_report("Option help requires a format be specified");
2101 goto fail_getopt;
2102 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002103 }
2104
2105 if (s.src_num < 1) {
2106 error_report("Must specify image file name");
2107 goto fail_getopt;
2108 }
2109
2110
Peter Lieven9fd77f92017-04-21 11:11:55 +02002111 /* ret is still -EINVAL until here */
2112 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2113 if (ret < 0) {
2114 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002115 goto fail_getopt;
2116 }
2117
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002118 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002119 if (quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002120 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002121 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002122 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002123 qemu_progress_print(0, 100);
2124
Peter Lieven9fd77f92017-04-21 11:11:55 +02002125 s.src = g_new0(BlockBackend *, s.src_num);
2126 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002127
Peter Lieven9fd77f92017-04-21 11:11:55 +02002128 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2129 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Fam Zheng335e9932017-05-03 00:35:39 +08002130 fmt, src_flags, src_writethrough, quiet,
2131 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002132 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002133 ret = -1;
2134 goto out;
2135 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002136 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2137 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002138 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002139 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002140 ret = -1;
2141 goto out;
2142 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002143 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002144 }
bellardea2384d2004-08-01 21:59:26 +00002145
Wenchao Xiaef806542013-12-04 17:10:57 +08002146 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002147 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002148 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2149 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2150 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002151 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002152 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002153 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002154 ret = -1;
2155 goto out;
2156 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002157
Peter Lieven9fd77f92017-04-21 11:11:55 +02002158 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2159 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002160 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002161 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002162 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002163 ret = -1;
2164 goto out;
edison51ef6722010-09-21 19:58:41 -07002165 }
2166
Max Reitz2e024cd2015-02-11 09:58:46 -05002167 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002168 /* Find driver and parse its options */
2169 drv = bdrv_find_format(out_fmt);
2170 if (!drv) {
2171 error_report("Unknown file format '%s'", out_fmt);
2172 ret = -1;
2173 goto out;
2174 }
2175
2176 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2177 if (!proto_drv) {
2178 error_report_err(local_err);
2179 ret = -1;
2180 goto out;
2181 }
2182
Max Reitz2e024cd2015-02-11 09:58:46 -05002183 if (!drv->create_opts) {
2184 error_report("Format driver '%s' does not support image creation",
2185 drv->format_name);
2186 ret = -1;
2187 goto out;
2188 }
Max Reitzf75613c2014-12-02 18:32:46 +01002189
Max Reitz2e024cd2015-02-11 09:58:46 -05002190 if (!proto_drv->create_opts) {
2191 error_report("Protocol driver '%s' does not support image creation",
2192 proto_drv->format_name);
2193 ret = -1;
2194 goto out;
2195 }
Max Reitzf75613c2014-12-02 18:32:46 +01002196
Max Reitz2e024cd2015-02-11 09:58:46 -05002197 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2198 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002199
Max Reitz2e024cd2015-02-11 09:58:46 -05002200 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002201 if (options) {
2202 qemu_opts_do_parse(opts, options, NULL, &local_err);
2203 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002204 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002205 ret = -1;
2206 goto out;
2207 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002208 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002209
Peter Lieven9fd77f92017-04-21 11:11:55 +02002210 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002211 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002212 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2213 if (ret < 0) {
2214 goto out;
2215 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002216 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002217
Kevin Wolfa18953f2010-10-14 15:46:04 +02002218 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002219 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002220 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002221 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002222 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002223 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002224
Max Reitz48758a82017-04-26 15:46:48 +02002225 if (s.src_num > 1 && out_baseimg) {
2226 error_report("Having a backing file for the target makes no sense when "
2227 "concatenating multiple input images");
2228 ret = -1;
2229 goto out;
2230 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002231
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002232 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002233 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002234 bool encryption =
2235 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002236 const char *encryptfmt =
2237 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
Chunyan Liu83d05212014-06-05 17:20:51 +08002238 const char *preallocation =
2239 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002240
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002241 if (drv && !drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002242 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002243 ret = -1;
2244 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002245 }
2246
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002247 if (encryption || encryptfmt) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002248 error_report("Compression and encryption not supported at "
2249 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002250 ret = -1;
2251 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002252 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002253
Chunyan Liu83d05212014-06-05 17:20:51 +08002254 if (preallocation
2255 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002256 {
2257 error_report("Compression and preallocation not supported at "
2258 "the same time");
2259 ret = -1;
2260 goto out;
2261 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002262 }
2263
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002264 if (!skip_create) {
2265 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002266 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002267 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002268 error_reportf_err(local_err, "%s: error while converting %s: ",
2269 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002270 goto out;
bellardea2384d2004-08-01 21:59:26 +00002271 }
2272 }
ths3b46e622007-09-17 08:09:54 +00002273
Peter Lieven9fd77f92017-04-21 11:11:55 +02002274 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002275 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002276 if (ret < 0) {
2277 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002278 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002279 }
2280
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002281 if (skip_create) {
2282 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2283 flags, writethrough, quiet, false);
2284 } else {
2285 /* TODO ultimately we should allow --target-image-opts
2286 * to be used even when -n is not given.
2287 * That has to wait for bdrv_create to be improved
2288 * to allow filenames in option syntax
2289 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01002290 s.target = img_open_new_file(out_filename, opts, out_fmt,
2291 flags, writethrough, quiet, false);
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002292 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002293 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002294 ret = -1;
2295 goto out;
2296 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002297 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002298
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002299 if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2300 error_report("Compression not supported for this file format");
2301 ret = -1;
2302 goto out;
2303 }
bellardea2384d2004-08-01 21:59:26 +00002304
Eric Blake5def6b82016-06-23 16:37:19 -06002305 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002306 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2307 * as maximum. */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002308 s.buf_sectors = MIN(32768,
2309 MAX(s.buf_sectors,
2310 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2311 out_bs->bl.pdiscard_alignment >>
2312 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002313
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002314 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002315 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002316 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002317 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002318 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002319 ret = -1;
2320 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002321 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002322 error_report("output file is smaller than input file");
2323 ret = -1;
2324 goto out;
2325 }
2326 }
2327
Peter Lieven24f833c2013-11-27 11:07:07 +01002328 ret = bdrv_get_info(out_bs, &bdi);
2329 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002330 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002331 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002332 goto out;
2333 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002334 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002335 s.compressed = s.compressed || bdi.needs_compressed_writes;
2336 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Peter Lieven24f833c2013-11-27 11:07:07 +01002337 }
2338
Peter Lieven9fd77f92017-04-21 11:11:55 +02002339 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002340out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002341 if (!ret) {
2342 qemu_progress_print(100, 0);
2343 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002344 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002345 qemu_opts_del(opts);
2346 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002347 qemu_opts_del(sn_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002348 blk_unref(s.target);
2349 if (s.src) {
2350 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2351 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002352 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002353 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002354 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002355 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002356fail_getopt:
2357 g_free(options);
2358
Peter Lieven9fd77f92017-04-21 11:11:55 +02002359 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002360}
2361
bellard57d1a2b2004-08-03 21:15:11 +00002362
bellardfaea38e2006-08-05 21:31:00 +00002363static void dump_snapshots(BlockDriverState *bs)
2364{
2365 QEMUSnapshotInfo *sn_tab, *sn;
2366 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002367
2368 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2369 if (nb_sns <= 0)
2370 return;
2371 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002372 bdrv_snapshot_dump(fprintf, stdout, NULL);
2373 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002374 for(i = 0; i < nb_sns; i++) {
2375 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002376 bdrv_snapshot_dump(fprintf, stdout, sn);
2377 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002378 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002379 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002380}
2381
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002382static void dump_json_image_info_list(ImageInfoList *list)
2383{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002384 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002385 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002386 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002387
2388 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2389 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002390 str = qobject_to_json_pretty(obj);
2391 assert(str != NULL);
2392 printf("%s\n", qstring_get_str(str));
2393 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002394 visit_free(v);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002395 QDECREF(str);
2396}
2397
Benoît Canetc054b3f2012-09-05 13:09:02 +02002398static void dump_json_image_info(ImageInfo *info)
2399{
Benoît Canetc054b3f2012-09-05 13:09:02 +02002400 QString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002401 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002402 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002403
2404 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2405 visit_complete(v, &obj);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002406 str = qobject_to_json_pretty(obj);
2407 assert(str != NULL);
2408 printf("%s\n", qstring_get_str(str));
2409 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002410 visit_free(v);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002411 QDECREF(str);
2412}
2413
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002414static void dump_human_image_info_list(ImageInfoList *list)
2415{
2416 ImageInfoList *elem;
2417 bool delim = false;
2418
2419 for (elem = list; elem; elem = elem->next) {
2420 if (delim) {
2421 printf("\n");
2422 }
2423 delim = true;
2424
Wenchao Xia5b917042013-05-25 11:09:45 +08002425 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002426 }
2427}
2428
2429static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2430{
2431 return strcmp(a, b) == 0;
2432}
2433
2434/**
2435 * Open an image file chain and return an ImageInfoList
2436 *
2437 * @filename: topmost image filename
2438 * @fmt: topmost image format (may be NULL to autodetect)
2439 * @chain: true - enumerate entire backing file chain
2440 * false - only topmost image file
2441 *
2442 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2443 * image file. If there was an error a message will have been printed to
2444 * stderr.
2445 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002446static ImageInfoList *collect_image_info_list(bool image_opts,
2447 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002448 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002449 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002450{
2451 ImageInfoList *head = NULL;
2452 ImageInfoList **last = &head;
2453 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002454 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002455
2456 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2457
2458 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002459 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002460 BlockDriverState *bs;
2461 ImageInfo *info;
2462 ImageInfoList *elem;
2463
2464 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2465 error_report("Backing file '%s' creates an infinite loop.",
2466 filename);
2467 goto err;
2468 }
2469 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2470
Max Reitzefaa7c42016-03-16 19:54:38 +01002471 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002472 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2473 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002474 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002475 goto err;
2476 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002477 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002478
Wenchao Xia43526ec2013-06-06 12:27:58 +08002479 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002480 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002481 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002482 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002483 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002484 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002485
2486 elem = g_new0(ImageInfoList, 1);
2487 elem->value = info;
2488 *last = elem;
2489 last = &elem->next;
2490
Markus Armbruster26f54e92014-10-07 13:59:04 +02002491 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002492
2493 filename = fmt = NULL;
2494 if (chain) {
2495 if (info->has_full_backing_filename) {
2496 filename = info->full_backing_filename;
2497 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002498 error_report("Could not determine absolute backing filename,"
2499 " but backing filename '%s' present",
2500 info->backing_filename);
2501 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002502 }
2503 if (info->has_backing_filename_format) {
2504 fmt = info->backing_filename_format;
2505 }
2506 }
2507 }
2508 g_hash_table_destroy(filenames);
2509 return head;
2510
2511err:
2512 qapi_free_ImageInfoList(head);
2513 g_hash_table_destroy(filenames);
2514 return NULL;
2515}
2516
Benoît Canetc054b3f2012-09-05 13:09:02 +02002517static int img_info(int argc, char **argv)
2518{
2519 int c;
2520 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002521 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002522 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002523 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002524 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002525 bool force_share = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002526
bellardea2384d2004-08-01 21:59:26 +00002527 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002528 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002529 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002530 int option_index = 0;
2531 static const struct option long_options[] = {
2532 {"help", no_argument, 0, 'h'},
2533 {"format", required_argument, 0, 'f'},
2534 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002535 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002536 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002537 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002538 {"force-share", no_argument, 0, 'U'},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002539 {0, 0, 0, 0}
2540 };
Fam Zheng335e9932017-05-03 00:35:39 +08002541 c = getopt_long(argc, argv, ":f:hU",
Benoît Canetc054b3f2012-09-05 13:09:02 +02002542 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002543 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002544 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002545 }
bellardea2384d2004-08-01 21:59:26 +00002546 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002547 case ':':
2548 missing_argument(argv[optind - 1]);
2549 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002550 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002551 unrecognized_option(argv[optind - 1]);
2552 break;
bellardea2384d2004-08-01 21:59:26 +00002553 case 'h':
2554 help();
2555 break;
2556 case 'f':
2557 fmt = optarg;
2558 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002559 case 'U':
2560 force_share = true;
2561 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002562 case OPTION_OUTPUT:
2563 output = optarg;
2564 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002565 case OPTION_BACKING_CHAIN:
2566 chain = true;
2567 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002568 case OPTION_OBJECT: {
2569 QemuOpts *opts;
2570 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2571 optarg, true);
2572 if (!opts) {
2573 return 1;
2574 }
2575 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002576 case OPTION_IMAGE_OPTS:
2577 image_opts = true;
2578 break;
bellardea2384d2004-08-01 21:59:26 +00002579 }
2580 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002581 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002582 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002583 }
bellardea2384d2004-08-01 21:59:26 +00002584 filename = argv[optind++];
2585
Benoît Canetc054b3f2012-09-05 13:09:02 +02002586 if (output && !strcmp(output, "json")) {
2587 output_format = OFORMAT_JSON;
2588 } else if (output && !strcmp(output, "human")) {
2589 output_format = OFORMAT_HUMAN;
2590 } else if (output) {
2591 error_report("--output must be used with human or json as argument.");
2592 return 1;
2593 }
2594
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002595 if (qemu_opts_foreach(&qemu_object_opts,
2596 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002597 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002598 return 1;
2599 }
2600
Fam Zheng335e9932017-05-03 00:35:39 +08002601 list = collect_image_info_list(image_opts, filename, fmt, chain,
2602 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002603 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002604 return 1;
2605 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002606
Benoît Canetc054b3f2012-09-05 13:09:02 +02002607 switch (output_format) {
2608 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002609 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002610 break;
2611 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002612 if (chain) {
2613 dump_json_image_info_list(list);
2614 } else {
2615 dump_json_image_info(list->value);
2616 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002617 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002618 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002619
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002620 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002621 return 0;
2622}
2623
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002624static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2625 MapEntry *next)
2626{
2627 switch (output_format) {
2628 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002629 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002630 error_report("File contains external, encrypted or compressed clusters.");
2631 exit(1);
2632 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002633 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002634 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002635 e->start, e->length,
2636 e->has_offset ? e->offset : 0,
2637 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002638 }
2639 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2640 * Modify the flags here to allow more coalescing.
2641 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002642 if (next && (!next->data || next->zero)) {
2643 next->data = false;
2644 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002645 }
2646 break;
2647 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002648 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2649 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002650 (e->start == 0 ? "[" : ",\n"),
2651 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002652 e->zero ? "true" : "false",
2653 e->data ? "true" : "false");
2654 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002655 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002656 }
2657 putchar('}');
2658
2659 if (!next) {
2660 printf("]\n");
2661 }
2662 break;
2663 }
2664}
2665
Eric Blake5e344dd2017-10-11 22:47:02 -05002666static int get_block_status(BlockDriverState *bs, int64_t offset,
2667 int64_t bytes, MapEntry *e)
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002668{
Eric Blake237d78f2017-10-11 22:47:03 -05002669 int ret;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002670 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002671 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002672 bool has_offset;
Eric Blake237d78f2017-10-11 22:47:03 -05002673 int64_t map;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002674
2675 /* As an optimization, we could cache the current range of unallocated
2676 * clusters in each file of the chain, and avoid querying the same
2677 * range repeatedly.
2678 */
2679
2680 depth = 0;
2681 for (;;) {
Eric Blake237d78f2017-10-11 22:47:03 -05002682 ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002683 if (ret < 0) {
2684 return ret;
2685 }
Eric Blake237d78f2017-10-11 22:47:03 -05002686 assert(bytes);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002687 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2688 break;
2689 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002690 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002691 if (bs == NULL) {
2692 ret = 0;
2693 break;
2694 }
2695
2696 depth++;
2697 }
2698
John Snow28756452016-02-05 13:12:33 -05002699 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2700
2701 *e = (MapEntry) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002702 .start = offset,
Eric Blake237d78f2017-10-11 22:47:03 -05002703 .length = bytes,
John Snow28756452016-02-05 13:12:33 -05002704 .data = !!(ret & BDRV_BLOCK_DATA),
2705 .zero = !!(ret & BDRV_BLOCK_ZERO),
Eric Blake237d78f2017-10-11 22:47:03 -05002706 .offset = map,
John Snow28756452016-02-05 13:12:33 -05002707 .has_offset = has_offset,
2708 .depth = depth,
2709 .has_filename = file && has_offset,
2710 .filename = file && has_offset ? file->filename : NULL,
2711 };
2712
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002713 return 0;
2714}
2715
Fam Zheng16b0d552016-01-26 11:59:02 +08002716static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2717{
2718 if (curr->length == 0) {
2719 return false;
2720 }
2721 if (curr->zero != next->zero ||
2722 curr->data != next->data ||
2723 curr->depth != next->depth ||
2724 curr->has_filename != next->has_filename ||
2725 curr->has_offset != next->has_offset) {
2726 return false;
2727 }
2728 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2729 return false;
2730 }
2731 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2732 return false;
2733 }
2734 return true;
2735}
2736
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002737static int img_map(int argc, char **argv)
2738{
2739 int c;
2740 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002741 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002742 BlockDriverState *bs;
2743 const char *filename, *fmt, *output;
2744 int64_t length;
2745 MapEntry curr = { .length = 0 }, next;
2746 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002747 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002748 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002749
2750 fmt = NULL;
2751 output = NULL;
2752 for (;;) {
2753 int option_index = 0;
2754 static const struct option long_options[] = {
2755 {"help", no_argument, 0, 'h'},
2756 {"format", required_argument, 0, 'f'},
2757 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002758 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002759 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002760 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002761 {0, 0, 0, 0}
2762 };
Fam Zheng335e9932017-05-03 00:35:39 +08002763 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002764 long_options, &option_index);
2765 if (c == -1) {
2766 break;
2767 }
2768 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002769 case ':':
2770 missing_argument(argv[optind - 1]);
2771 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002772 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002773 unrecognized_option(argv[optind - 1]);
2774 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002775 case 'h':
2776 help();
2777 break;
2778 case 'f':
2779 fmt = optarg;
2780 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002781 case 'U':
2782 force_share = true;
2783 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002784 case OPTION_OUTPUT:
2785 output = optarg;
2786 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002787 case OPTION_OBJECT: {
2788 QemuOpts *opts;
2789 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2790 optarg, true);
2791 if (!opts) {
2792 return 1;
2793 }
2794 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002795 case OPTION_IMAGE_OPTS:
2796 image_opts = true;
2797 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002798 }
2799 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002800 if (optind != argc - 1) {
2801 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002802 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002803 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002804
2805 if (output && !strcmp(output, "json")) {
2806 output_format = OFORMAT_JSON;
2807 } else if (output && !strcmp(output, "human")) {
2808 output_format = OFORMAT_HUMAN;
2809 } else if (output) {
2810 error_report("--output must be used with human or json as argument.");
2811 return 1;
2812 }
2813
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002814 if (qemu_opts_foreach(&qemu_object_opts,
2815 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002816 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002817 return 1;
2818 }
2819
Fam Zheng335e9932017-05-03 00:35:39 +08002820 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002821 if (!blk) {
2822 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002823 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002824 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002825
2826 if (output_format == OFORMAT_HUMAN) {
2827 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2828 }
2829
Max Reitzf1d3cd72015-02-05 13:58:18 -05002830 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002831 while (curr.start + curr.length < length) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002832 int64_t offset = curr.start + curr.length;
2833 int64_t n;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002834
2835 /* Probe up to 1 GiB at a time. */
Eric Blake5e344dd2017-10-11 22:47:02 -05002836 n = QEMU_ALIGN_DOWN(MIN(1 << 30, length - offset), BDRV_SECTOR_SIZE);
2837 ret = get_block_status(bs, offset, n, &next);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002838
2839 if (ret < 0) {
2840 error_report("Could not read file metadata: %s", strerror(-ret));
2841 goto out;
2842 }
2843
Fam Zheng16b0d552016-01-26 11:59:02 +08002844 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002845 curr.length += next.length;
2846 continue;
2847 }
2848
2849 if (curr.length > 0) {
2850 dump_map_entry(output_format, &curr, &next);
2851 }
2852 curr = next;
2853 }
2854
2855 dump_map_entry(output_format, &curr, NULL);
2856
2857out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002858 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002859 return ret < 0;
2860}
2861
aliguorif7b4a942009-01-07 17:40:15 +00002862#define SNAPSHOT_LIST 1
2863#define SNAPSHOT_CREATE 2
2864#define SNAPSHOT_APPLY 3
2865#define SNAPSHOT_DELETE 4
2866
Stuart Brady153859b2009-06-07 00:42:17 +01002867static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002868{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002869 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002870 BlockDriverState *bs;
2871 QEMUSnapshotInfo sn;
2872 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002873 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002874 int action = 0;
2875 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002876 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002877 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002878 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002879 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00002880
Kevin Wolfce099542016-03-15 13:01:04 +01002881 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002882 /* Parse commandline parameters */
2883 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002884 static const struct option long_options[] = {
2885 {"help", no_argument, 0, 'h'},
2886 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002887 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002888 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002889 {0, 0, 0, 0}
2890 };
Fam Zheng335e9932017-05-03 00:35:39 +08002891 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002892 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002893 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002894 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002895 }
aliguorif7b4a942009-01-07 17:40:15 +00002896 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002897 case ':':
2898 missing_argument(argv[optind - 1]);
2899 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002900 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002901 unrecognized_option(argv[optind - 1]);
2902 break;
aliguorif7b4a942009-01-07 17:40:15 +00002903 case 'h':
2904 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002905 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002906 case 'l':
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_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002912 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002913 break;
2914 case 'a':
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_APPLY;
2920 snapshot_name = optarg;
2921 break;
2922 case 'c':
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_CREATE;
2928 snapshot_name = optarg;
2929 break;
2930 case 'd':
2931 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002932 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002933 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002934 }
2935 action = SNAPSHOT_DELETE;
2936 snapshot_name = optarg;
2937 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002938 case 'q':
2939 quiet = true;
2940 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002941 case 'U':
2942 force_share = true;
2943 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002944 case OPTION_OBJECT: {
2945 QemuOpts *opts;
2946 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2947 optarg, true);
2948 if (!opts) {
2949 return 1;
2950 }
2951 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002952 case OPTION_IMAGE_OPTS:
2953 image_opts = true;
2954 break;
aliguorif7b4a942009-01-07 17:40:15 +00002955 }
2956 }
2957
Kevin Wolffc11eb22013-08-05 10:53:04 +02002958 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002959 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002960 }
aliguorif7b4a942009-01-07 17:40:15 +00002961 filename = argv[optind++];
2962
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002963 if (qemu_opts_foreach(&qemu_object_opts,
2964 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002965 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002966 return 1;
2967 }
2968
aliguorif7b4a942009-01-07 17:40:15 +00002969 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08002970 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
2971 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002972 if (!blk) {
2973 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002974 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002975 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002976
2977 /* Perform the requested action */
2978 switch(action) {
2979 case SNAPSHOT_LIST:
2980 dump_snapshots(bs);
2981 break;
2982
2983 case SNAPSHOT_CREATE:
2984 memset(&sn, 0, sizeof(sn));
2985 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2986
2987 qemu_gettimeofday(&tv);
2988 sn.date_sec = tv.tv_sec;
2989 sn.date_nsec = tv.tv_usec * 1000;
2990
2991 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002992 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002993 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002994 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002995 }
aliguorif7b4a942009-01-07 17:40:15 +00002996 break;
2997
2998 case SNAPSHOT_APPLY:
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01002999 ret = bdrv_snapshot_goto(bs, snapshot_name, &err);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003000 if (ret) {
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01003001 error_reportf_err(err, "Could not apply snapshot '%s': ",
3002 snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003003 }
aliguorif7b4a942009-01-07 17:40:15 +00003004 break;
3005
3006 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003007 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003008 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003009 error_reportf_err(err, "Could not delete snapshot '%s': ",
3010 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003011 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003012 }
aliguorif7b4a942009-01-07 17:40:15 +00003013 break;
3014 }
3015
3016 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003017 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003018 if (ret) {
3019 return 1;
3020 }
Stuart Brady153859b2009-06-07 00:42:17 +01003021 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003022}
3023
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003024static int img_rebase(int argc, char **argv)
3025{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003026 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003027 uint8_t *buf_old = NULL;
3028 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05003029 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003030 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003031 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3032 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003033 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003034 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003035 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003036 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003037 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003038 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003039 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003040
3041 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003042 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003043 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003044 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003045 out_baseimg = NULL;
3046 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003047 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003048 static const struct option long_options[] = {
3049 {"help", no_argument, 0, 'h'},
3050 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003051 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003052 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003053 {0, 0, 0, 0}
3054 };
Fam Zheng335e9932017-05-03 00:35:39 +08003055 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003056 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003057 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003058 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003059 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003060 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003061 case ':':
3062 missing_argument(argv[optind - 1]);
3063 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003064 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003065 unrecognized_option(argv[optind - 1]);
3066 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003067 case 'h':
3068 help();
3069 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003070 case 'f':
3071 fmt = optarg;
3072 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003073 case 'F':
3074 out_basefmt = optarg;
3075 break;
3076 case 'b':
3077 out_baseimg = optarg;
3078 break;
3079 case 'u':
3080 unsafe = 1;
3081 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003082 case 'p':
3083 progress = 1;
3084 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003085 case 't':
3086 cache = optarg;
3087 break;
Max Reitz40055952014-07-22 22:58:42 +02003088 case 'T':
3089 src_cache = optarg;
3090 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003091 case 'q':
3092 quiet = true;
3093 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003094 case OPTION_OBJECT: {
3095 QemuOpts *opts;
3096 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3097 optarg, true);
3098 if (!opts) {
3099 return 1;
3100 }
3101 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003102 case OPTION_IMAGE_OPTS:
3103 image_opts = true;
3104 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003105 case 'U':
3106 force_share = true;
3107 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003108 }
3109 }
3110
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003111 if (quiet) {
3112 progress = 0;
3113 }
3114
Fam Zhengac1307a2014-04-22 13:36:11 +08003115 if (optind != argc - 1) {
3116 error_exit("Expecting one image file name");
3117 }
3118 if (!unsafe && !out_baseimg) {
3119 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003120 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003121 filename = argv[optind++];
3122
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003123 if (qemu_opts_foreach(&qemu_object_opts,
3124 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003125 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003126 return 1;
3127 }
3128
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003129 qemu_progress_init(progress, 2.0);
3130 qemu_progress_print(0, 100);
3131
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003132 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003133 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003134 if (ret < 0) {
3135 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003136 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003137 }
3138
Kevin Wolfce099542016-03-15 13:01:04 +01003139 src_flags = 0;
3140 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003141 if (ret < 0) {
3142 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003143 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003144 }
3145
Kevin Wolfce099542016-03-15 13:01:04 +01003146 /* The source files are opened read-only, don't care about WCE */
3147 assert((src_flags & BDRV_O_RDWR) == 0);
3148 (void) src_writethrough;
3149
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003150 /*
3151 * Open the images.
3152 *
3153 * Ignore the old backing file for unsafe rebase in case we want to correct
3154 * the reference to a renamed or moved backing file.
3155 */
Fam Zheng335e9932017-05-03 00:35:39 +08003156 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3157 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003158 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003159 ret = -1;
3160 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003161 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003162 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003163
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003164 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003165 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003166 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003167 ret = -1;
3168 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003169 }
3170 }
3171
3172 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003173 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003174 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003175 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003176
Max Reitz644483d2015-02-05 13:58:17 -05003177 if (bs->backing_format[0] != '\0') {
3178 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003179 qdict_put_str(options, "driver", bs->backing_format);
Max Reitz644483d2015-02-05 13:58:17 -05003180 }
3181
Fam Zheng335e9932017-05-03 00:35:39 +08003182 if (force_share) {
3183 if (!options) {
3184 options = qdict_new();
3185 }
Eric Blake579cf1d2017-05-15 14:54:39 -05003186 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +08003187 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003188 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003189 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003190 options, src_flags, &local_err);
3191 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003192 error_reportf_err(local_err,
3193 "Could not open old backing file '%s': ",
3194 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003195 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003196 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003197 }
Max Reitz644483d2015-02-05 13:58:17 -05003198
Alex Bligha6166732012-10-16 13:46:18 +01003199 if (out_baseimg[0]) {
Fam Zheng335e9932017-05-03 00:35:39 +08003200 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003201 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003202 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003203 }
3204 if (force_share) {
3205 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003206 }
3207
Max Reitzefaa7c42016-03-16 19:54:38 +01003208 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003209 options, src_flags, &local_err);
3210 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003211 error_reportf_err(local_err,
3212 "Could not open new backing file '%s': ",
3213 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003214 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003215 goto out;
3216 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003217 }
3218 }
3219
3220 /*
3221 * Check each unallocated cluster in the COW file. If it is unallocated,
3222 * accesses go to the backing file. We must therefore compare this cluster
3223 * in the old and new backing file, and if they differ we need to copy it
3224 * from the old backing file into the COW file.
3225 *
3226 * If qemu-img crashes during this step, no harm is done. The content of
3227 * the image is the same as the original one at any time.
3228 */
3229 if (!unsafe) {
Eric Blake41536282017-10-11 22:47:15 -05003230 int64_t size;
3231 int64_t old_backing_size;
3232 int64_t new_backing_size = 0;
3233 uint64_t offset;
3234 int64_t n;
Kevin Wolf1f710492012-10-12 14:29:18 +02003235 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003236
Max Reitzf1d3cd72015-02-05 13:58:18 -05003237 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3238 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003239
Eric Blake41536282017-10-11 22:47:15 -05003240 size = blk_getlength(blk);
3241 if (size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003242 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003243 filename, strerror(-size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003244 ret = -1;
3245 goto out;
3246 }
Eric Blake41536282017-10-11 22:47:15 -05003247 old_backing_size = blk_getlength(blk_old_backing);
3248 if (old_backing_size < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003249 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003250
3251 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3252 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003253 backing_name, strerror(-old_backing_size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003254 ret = -1;
3255 goto out;
3256 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003257 if (blk_new_backing) {
Eric Blake41536282017-10-11 22:47:15 -05003258 new_backing_size = blk_getlength(blk_new_backing);
3259 if (new_backing_size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003260 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003261 out_baseimg, strerror(-new_backing_size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003262 ret = -1;
3263 goto out;
3264 }
Alex Bligha6166732012-10-16 13:46:18 +01003265 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003266
Eric Blake41536282017-10-11 22:47:15 -05003267 if (size != 0) {
3268 local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE));
Kevin Wolf1f710492012-10-12 14:29:18 +02003269 }
3270
Eric Blake41536282017-10-11 22:47:15 -05003271 for (offset = 0; offset < size; offset += n) {
3272 /* How many bytes can we handle with the next read? */
3273 n = MIN(IO_BUF_SIZE, size - offset);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003274
3275 /* If the cluster is allocated, we don't need to take action */
Eric Blake41536282017-10-11 22:47:15 -05003276 ret = bdrv_is_allocated(bs, offset, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003277 if (ret < 0) {
3278 error_report("error while reading image metadata: %s",
3279 strerror(-ret));
3280 goto out;
3281 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003282 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003283 continue;
3284 }
3285
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003286 /*
3287 * Read old and new backing file and take into consideration that
3288 * backing files may be smaller than the COW image.
3289 */
Eric Blake41536282017-10-11 22:47:15 -05003290 if (offset >= old_backing_size) {
3291 memset(buf_old, 0, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003292 } else {
Eric Blake41536282017-10-11 22:47:15 -05003293 if (offset + n > old_backing_size) {
3294 n = old_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003295 }
3296
Eric Blake41536282017-10-11 22:47:15 -05003297 ret = blk_pread(blk_old_backing, offset, buf_old, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003298 if (ret < 0) {
3299 error_report("error while reading from old backing file");
3300 goto out;
3301 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003302 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003303
Eric Blake41536282017-10-11 22:47:15 -05003304 if (offset >= new_backing_size || !blk_new_backing) {
3305 memset(buf_new, 0, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003306 } else {
Eric Blake41536282017-10-11 22:47:15 -05003307 if (offset + n > new_backing_size) {
3308 n = new_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003309 }
3310
Eric Blake41536282017-10-11 22:47:15 -05003311 ret = blk_pread(blk_new_backing, offset, buf_new, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003312 if (ret < 0) {
3313 error_report("error while reading from new backing file");
3314 goto out;
3315 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003316 }
3317
3318 /* If they differ, we need to write to the COW file */
3319 uint64_t written = 0;
3320
3321 while (written < n) {
Eric Blakedc61cd32017-10-11 22:47:14 -05003322 int64_t pnum;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003323
Eric Blake41536282017-10-11 22:47:15 -05003324 if (compare_buffers(buf_old + written, buf_new + written,
3325 n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003326 {
Eric Blake41536282017-10-11 22:47:15 -05003327 ret = blk_pwrite(blk, offset + written,
Eric Blakedc61cd32017-10-11 22:47:14 -05003328 buf_old + written, pnum, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003329 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003330 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003331 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003332 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003333 }
3334 }
3335
3336 written += pnum;
3337 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003338 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003339 }
3340 }
3341
3342 /*
3343 * Change the backing file. All clusters that are different from the old
3344 * backing file are overwritten in the COW file now, so the visible content
3345 * doesn't change when we switch the backing file.
3346 */
Alex Bligha6166732012-10-16 13:46:18 +01003347 if (out_baseimg && *out_baseimg) {
3348 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3349 } else {
3350 ret = bdrv_change_backing_file(bs, NULL, NULL);
3351 }
3352
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003353 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003354 error_report("Could not change the backing file to '%s': No "
3355 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003356 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003357 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003358 out_baseimg, strerror(-ret));
3359 }
3360
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003361 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003362 /*
3363 * TODO At this point it is possible to check if any clusters that are
3364 * allocated in the COW file are the same in the backing file. If so, they
3365 * could be dropped from the COW file. Don't do this before switching the
3366 * backing file, in case of a crash this would lead to corruption.
3367 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003368out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003369 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003370 /* Cleanup */
3371 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003372 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003373 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003374 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003375 qemu_vfree(buf_old);
3376 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003377
Markus Armbruster26f54e92014-10-07 13:59:04 +02003378 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003379 if (ret) {
3380 return 1;
3381 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003382 return 0;
3383}
3384
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003385static int img_resize(int argc, char **argv)
3386{
Markus Armbruster6750e792015-02-12 17:43:08 +01003387 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003388 int c, ret, relative;
3389 const char *filename, *fmt, *size;
Max Reitzdc5f6902017-06-13 22:20:55 +02003390 int64_t n, total_size, current_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003391 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003392 BlockBackend *blk = NULL;
Max Reitzdc5f6902017-06-13 22:20:55 +02003393 PreallocMode prealloc = PREALLOC_MODE_OFF;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003394 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003395
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003396 static QemuOptsList resize_options = {
3397 .name = "resize_options",
3398 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3399 .desc = {
3400 {
3401 .name = BLOCK_OPT_SIZE,
3402 .type = QEMU_OPT_SIZE,
3403 .help = "Virtual disk size"
3404 }, {
3405 /* end of list */
3406 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003407 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003408 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003409 bool image_opts = false;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003410 bool shrink = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003411
Kevin Wolfe80fec72011-04-29 10:58:12 +02003412 /* Remove size from argv manually so that negative numbers are not treated
3413 * as options by getopt. */
3414 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003415 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003416 return 1;
3417 }
3418
3419 size = argv[--argc];
3420
3421 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003422 fmt = NULL;
3423 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003424 static const struct option long_options[] = {
3425 {"help", no_argument, 0, 'h'},
3426 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003427 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Max Reitzdc5f6902017-06-13 22:20:55 +02003428 {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003429 {"shrink", no_argument, 0, OPTION_SHRINK},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003430 {0, 0, 0, 0}
3431 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003432 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003433 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003434 if (c == -1) {
3435 break;
3436 }
3437 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003438 case ':':
3439 missing_argument(argv[optind - 1]);
3440 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003441 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003442 unrecognized_option(argv[optind - 1]);
3443 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003444 case 'h':
3445 help();
3446 break;
3447 case 'f':
3448 fmt = optarg;
3449 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003450 case 'q':
3451 quiet = true;
3452 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003453 case OPTION_OBJECT: {
3454 QemuOpts *opts;
3455 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3456 optarg, true);
3457 if (!opts) {
3458 return 1;
3459 }
3460 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003461 case OPTION_IMAGE_OPTS:
3462 image_opts = true;
3463 break;
Max Reitzdc5f6902017-06-13 22:20:55 +02003464 case OPTION_PREALLOCATION:
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02003465 prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
Markus Armbruster06c60b62017-08-24 10:45:57 +02003466 PREALLOC_MODE__MAX, NULL);
Max Reitzdc5f6902017-06-13 22:20:55 +02003467 if (prealloc == PREALLOC_MODE__MAX) {
3468 error_report("Invalid preallocation mode '%s'", optarg);
3469 return 1;
3470 }
3471 break;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003472 case OPTION_SHRINK:
3473 shrink = true;
3474 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003475 }
3476 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003477 if (optind != argc - 1) {
Max Reitzbe8fbd42018-02-05 17:27:45 +01003478 error_exit("Expecting image file name and size");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003479 }
3480 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003481
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003482 if (qemu_opts_foreach(&qemu_object_opts,
3483 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003484 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003485 return 1;
3486 }
3487
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003488 /* Choose grow, shrink, or absolute resize mode */
3489 switch (size[0]) {
3490 case '+':
3491 relative = 1;
3492 size++;
3493 break;
3494 case '-':
3495 relative = -1;
3496 size++;
3497 break;
3498 default:
3499 relative = 0;
3500 break;
3501 }
3502
3503 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003504 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003505 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003506 if (err) {
3507 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003508 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003509 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003510 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003511 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003512 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3513 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003514
Max Reitzefaa7c42016-03-16 19:54:38 +01003515 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003516 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3517 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003518 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003519 ret = -1;
3520 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003521 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003522
Max Reitzdc5f6902017-06-13 22:20:55 +02003523 current_size = blk_getlength(blk);
3524 if (current_size < 0) {
3525 error_report("Failed to inquire current image length: %s",
3526 strerror(-current_size));
3527 ret = -1;
3528 goto out;
3529 }
3530
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003531 if (relative) {
Max Reitzdc5f6902017-06-13 22:20:55 +02003532 total_size = current_size + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003533 } else {
3534 total_size = n;
3535 }
3536 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003537 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003538 ret = -1;
3539 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003540 }
3541
Max Reitzdc5f6902017-06-13 22:20:55 +02003542 if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
3543 error_report("Preallocation can only be used for growing images");
3544 ret = -1;
3545 goto out;
3546 }
3547
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003548 if (total_size < current_size && !shrink) {
3549 warn_report("Shrinking an image will delete all data beyond the "
3550 "shrunken image's end. Before performing such an "
3551 "operation, make sure there is no important data there.");
3552
3553 if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) {
3554 error_report(
3555 "Use the --shrink option to perform a shrink operation.");
3556 ret = -1;
3557 goto out;
3558 } else {
3559 warn_report("Using the --shrink option will suppress this message. "
3560 "Note that future versions of qemu-img may refuse to "
3561 "shrink images without this option.");
3562 }
3563 }
3564
Max Reitzdc5f6902017-06-13 22:20:55 +02003565 ret = blk_truncate(blk, total_size, prealloc, &err);
Max Reitzed3d2ec2017-03-28 22:51:27 +02003566 if (!ret) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003567 qprintf(quiet, "Image resized.\n");
Max Reitzed3d2ec2017-03-28 22:51:27 +02003568 } else {
3569 error_report_err(err);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003570 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003571out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003572 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003573 if (ret) {
3574 return 1;
3575 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003576 return 0;
3577}
3578
Max Reitz76a3a342014-10-27 11:12:51 +01003579static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003580 int64_t offset, int64_t total_work_size,
3581 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003582{
3583 qemu_progress_print(100.f * offset / total_work_size, 0);
3584}
3585
Max Reitz6f176b42013-09-03 10:09:50 +02003586static int img_amend(int argc, char **argv)
3587{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003588 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003589 int c, ret = 0;
3590 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003591 QemuOptsList *create_opts = NULL;
3592 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003593 const char *fmt = NULL, *filename, *cache;
3594 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003595 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003596 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003597 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003598 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003599 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003600
Max Reitzbd39e6e2014-07-22 22:58:43 +02003601 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003602 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003603 static const struct option long_options[] = {
3604 {"help", no_argument, 0, 'h'},
3605 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003606 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003607 {0, 0, 0, 0}
3608 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003609 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003610 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003611 if (c == -1) {
3612 break;
3613 }
3614
3615 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003616 case ':':
3617 missing_argument(argv[optind - 1]);
3618 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003619 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003620 unrecognized_option(argv[optind - 1]);
3621 break;
3622 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003623 help();
3624 break;
3625 case 'o':
3626 if (!is_valid_option_list(optarg)) {
3627 error_report("Invalid option list: %s", optarg);
3628 ret = -1;
3629 goto out_no_progress;
3630 }
3631 if (!options) {
3632 options = g_strdup(optarg);
3633 } else {
3634 char *old_options = options;
3635 options = g_strdup_printf("%s,%s", options, optarg);
3636 g_free(old_options);
3637 }
3638 break;
3639 case 'f':
3640 fmt = optarg;
3641 break;
3642 case 't':
3643 cache = optarg;
3644 break;
3645 case 'p':
3646 progress = true;
3647 break;
3648 case 'q':
3649 quiet = true;
3650 break;
3651 case OPTION_OBJECT:
3652 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3653 optarg, true);
3654 if (!opts) {
3655 ret = -1;
3656 goto out_no_progress;
3657 }
3658 break;
3659 case OPTION_IMAGE_OPTS:
3660 image_opts = true;
3661 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003662 }
3663 }
3664
Max Reitz6f176b42013-09-03 10:09:50 +02003665 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003666 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003667 }
3668
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003669 if (qemu_opts_foreach(&qemu_object_opts,
3670 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003671 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003672 ret = -1;
3673 goto out_no_progress;
3674 }
3675
Max Reitz76a3a342014-10-27 11:12:51 +01003676 if (quiet) {
3677 progress = false;
3678 }
3679 qemu_progress_init(progress, 1.0);
3680
Kevin Wolfa283cb62014-02-21 16:24:07 +01003681 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3682 if (fmt && has_help_option(options)) {
3683 /* If a format is explicitly specified (and possibly no filename is
3684 * given), print option help here */
3685 ret = print_block_option_help(filename, fmt);
3686 goto out;
3687 }
3688
3689 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003690 error_report("Expecting one image file name");
3691 ret = -1;
3692 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003693 }
Max Reitz6f176b42013-09-03 10:09:50 +02003694
Kevin Wolfce099542016-03-15 13:01:04 +01003695 flags = BDRV_O_RDWR;
3696 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003697 if (ret < 0) {
3698 error_report("Invalid cache option: %s", cache);
3699 goto out;
3700 }
3701
Fam Zheng335e9932017-05-03 00:35:39 +08003702 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3703 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003704 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003705 ret = -1;
3706 goto out;
3707 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003708 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003709
3710 fmt = bs->drv->format_name;
3711
Kevin Wolf626f84f2014-02-21 16:24:06 +01003712 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003713 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003714 ret = print_block_option_help(filename, fmt);
3715 goto out;
3716 }
3717
Max Reitzb2439d22014-12-02 18:32:47 +01003718 if (!bs->drv->create_opts) {
3719 error_report("Format driver '%s' does not support any options to amend",
3720 fmt);
3721 ret = -1;
3722 goto out;
3723 }
3724
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003725 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003726 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003727 qemu_opts_do_parse(opts, options, NULL, &err);
3728 if (err) {
3729 error_report_err(err);
3730 ret = -1;
3731 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003732 }
3733
Max Reitz76a3a342014-10-27 11:12:51 +01003734 /* In case the driver does not call amend_status_cb() */
3735 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003736 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003737 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003738 if (ret < 0) {
3739 error_report("Error while amending options: %s", strerror(-ret));
3740 goto out;
3741 }
3742
3743out:
Max Reitz76a3a342014-10-27 11:12:51 +01003744 qemu_progress_end();
3745
Max Reitze814dff2015-08-20 16:00:38 -07003746out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003747 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003748 qemu_opts_del(opts);
3749 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003750 g_free(options);
3751
Max Reitz6f176b42013-09-03 10:09:50 +02003752 if (ret) {
3753 return 1;
3754 }
3755 return 0;
3756}
3757
Kevin Wolfb6133b82014-08-05 14:17:13 +02003758typedef struct BenchData {
3759 BlockBackend *blk;
3760 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003761 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003762 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003763 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003764 int nrreq;
3765 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003766 int flush_interval;
3767 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003768 uint8_t *buf;
3769 QEMUIOVector *qiov;
3770
3771 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003772 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003773 uint64_t offset;
3774} BenchData;
3775
Kevin Wolf55d539c2016-06-03 13:59:41 +02003776static void bench_undrained_flush_cb(void *opaque, int ret)
3777{
3778 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003779 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003780 exit(EXIT_FAILURE);
3781 }
3782}
3783
Kevin Wolfb6133b82014-08-05 14:17:13 +02003784static void bench_cb(void *opaque, int ret)
3785{
3786 BenchData *b = opaque;
3787 BlockAIOCB *acb;
3788
3789 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003790 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003791 exit(EXIT_FAILURE);
3792 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003793
3794 if (b->in_flush) {
3795 /* Just finished a flush with drained queue: Start next requests */
3796 assert(b->in_flight == 0);
3797 b->in_flush = false;
3798 } else if (b->in_flight > 0) {
3799 int remaining = b->n - b->in_flight;
3800
Kevin Wolfb6133b82014-08-05 14:17:13 +02003801 b->n--;
3802 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003803
3804 /* Time for flush? Drain queue if requested, then flush */
3805 if (b->flush_interval && remaining % b->flush_interval == 0) {
3806 if (!b->in_flight || !b->drain_on_flush) {
3807 BlockCompletionFunc *cb;
3808
3809 if (b->drain_on_flush) {
3810 b->in_flush = true;
3811 cb = bench_cb;
3812 } else {
3813 cb = bench_undrained_flush_cb;
3814 }
3815
3816 acb = blk_aio_flush(b->blk, cb, b);
3817 if (!acb) {
3818 error_report("Failed to issue flush request");
3819 exit(EXIT_FAILURE);
3820 }
3821 }
3822 if (b->drain_on_flush) {
3823 return;
3824 }
3825 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003826 }
3827
3828 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003829 int64_t offset = b->offset;
3830 /* blk_aio_* might look for completed I/Os and kick bench_cb
3831 * again, so make sure this operation is counted by in_flight
3832 * and b->offset is ready for the next submission.
3833 */
3834 b->in_flight++;
3835 b->offset += b->step;
3836 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003837 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003838 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003839 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003840 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003841 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003842 if (!acb) {
3843 error_report("Failed to issue request");
3844 exit(EXIT_FAILURE);
3845 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003846 }
3847}
3848
3849static int img_bench(int argc, char **argv)
3850{
3851 int c, ret = 0;
3852 const char *fmt = NULL, *filename;
3853 bool quiet = false;
3854 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003855 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003856 int count = 75000;
3857 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003858 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003859 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003860 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003861 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003862 int flush_interval = 0;
3863 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003864 int64_t image_size;
3865 BlockBackend *blk = NULL;
3866 BenchData data = {};
3867 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003868 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003869 struct timeval t1, t2;
3870 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08003871 bool force_share = false;
Fam Zheng79d46582018-01-16 14:08:58 +08003872 size_t buf_size;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003873
3874 for (;;) {
3875 static const struct option long_options[] = {
3876 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003877 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003878 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003879 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003880 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08003881 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003882 {0, 0, 0, 0}
3883 };
Fam Zheng335e9932017-05-03 00:35:39 +08003884 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003885 if (c == -1) {
3886 break;
3887 }
3888
3889 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003890 case ':':
3891 missing_argument(argv[optind - 1]);
3892 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003893 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003894 unrecognized_option(argv[optind - 1]);
3895 break;
3896 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02003897 help();
3898 break;
3899 case 'c':
3900 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003901 unsigned long res;
3902
3903 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003904 error_report("Invalid request count specified");
3905 return 1;
3906 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003907 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003908 break;
3909 }
3910 case 'd':
3911 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003912 unsigned long res;
3913
3914 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003915 error_report("Invalid queue depth specified");
3916 return 1;
3917 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003918 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003919 break;
3920 }
3921 case 'f':
3922 fmt = optarg;
3923 break;
3924 case 'n':
3925 flags |= BDRV_O_NATIVE_AIO;
3926 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003927 case 'o':
3928 {
Markus Armbruster606caa02017-02-21 21:14:04 +01003929 offset = cvtnum(optarg);
3930 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02003931 error_report("Invalid offset specified");
3932 return 1;
3933 }
3934 break;
3935 }
3936 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003937 case 'q':
3938 quiet = true;
3939 break;
3940 case 's':
3941 {
3942 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003943
Markus Armbruster606caa02017-02-21 21:14:04 +01003944 sval = cvtnum(optarg);
3945 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003946 error_report("Invalid buffer size specified");
3947 return 1;
3948 }
3949
3950 bufsize = sval;
3951 break;
3952 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003953 case 'S':
3954 {
3955 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003956
Markus Armbruster606caa02017-02-21 21:14:04 +01003957 sval = cvtnum(optarg);
3958 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02003959 error_report("Invalid step size specified");
3960 return 1;
3961 }
3962
3963 step = sval;
3964 break;
3965 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003966 case 't':
3967 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3968 if (ret < 0) {
3969 error_report("Invalid cache mode");
3970 ret = -1;
3971 goto out;
3972 }
3973 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003974 case 'w':
3975 flags |= BDRV_O_RDWR;
3976 is_write = true;
3977 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003978 case 'U':
3979 force_share = true;
3980 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003981 case OPTION_PATTERN:
3982 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003983 unsigned long res;
3984
3985 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003986 error_report("Invalid pattern byte specified");
3987 return 1;
3988 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003989 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003990 break;
3991 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003992 case OPTION_FLUSH_INTERVAL:
3993 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003994 unsigned long res;
3995
3996 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02003997 error_report("Invalid flush interval specified");
3998 return 1;
3999 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004000 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004001 break;
4002 }
4003 case OPTION_NO_DRAIN:
4004 drain_on_flush = false;
4005 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004006 case OPTION_IMAGE_OPTS:
4007 image_opts = true;
4008 break;
4009 }
4010 }
4011
4012 if (optind != argc - 1) {
4013 error_exit("Expecting one image file name");
4014 }
4015 filename = argv[argc - 1];
4016
Kevin Wolf55d539c2016-06-03 13:59:41 +02004017 if (!is_write && flush_interval) {
4018 error_report("--flush-interval is only available in write tests");
4019 ret = -1;
4020 goto out;
4021 }
4022 if (flush_interval && flush_interval < depth) {
4023 error_report("Flush interval can't be smaller than depth");
4024 ret = -1;
4025 goto out;
4026 }
4027
Fam Zheng335e9932017-05-03 00:35:39 +08004028 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4029 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004030 if (!blk) {
4031 ret = -1;
4032 goto out;
4033 }
4034
4035 image_size = blk_getlength(blk);
4036 if (image_size < 0) {
4037 ret = image_size;
4038 goto out;
4039 }
4040
4041 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004042 .blk = blk,
4043 .image_size = image_size,
4044 .bufsize = bufsize,
4045 .step = step ?: bufsize,
4046 .nrreq = depth,
4047 .n = count,
4048 .offset = offset,
4049 .write = is_write,
4050 .flush_interval = flush_interval,
4051 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004052 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004053 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004054 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004055 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004056 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004057 if (flush_interval) {
4058 printf("Sending flush every %d requests\n", flush_interval);
4059 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004060
Fam Zheng79d46582018-01-16 14:08:58 +08004061 buf_size = data.nrreq * data.bufsize;
4062 data.buf = blk_blockalign(blk, buf_size);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004063 memset(data.buf, pattern, data.nrreq * data.bufsize);
4064
Fam Zheng79d46582018-01-16 14:08:58 +08004065 blk_register_buf(blk, data.buf, buf_size);
4066
Kevin Wolfb6133b82014-08-05 14:17:13 +02004067 data.qiov = g_new(QEMUIOVector, data.nrreq);
4068 for (i = 0; i < data.nrreq; i++) {
4069 qemu_iovec_init(&data.qiov[i], 1);
4070 qemu_iovec_add(&data.qiov[i],
4071 data.buf + i * data.bufsize, data.bufsize);
4072 }
4073
4074 gettimeofday(&t1, NULL);
4075 bench_cb(&data, 0);
4076
4077 while (data.n > 0) {
4078 main_loop_wait(false);
4079 }
4080 gettimeofday(&t2, NULL);
4081
4082 printf("Run completed in %3.3f seconds.\n",
4083 (t2.tv_sec - t1.tv_sec)
4084 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4085
4086out:
Fam Zheng79d46582018-01-16 14:08:58 +08004087 if (data.buf) {
4088 blk_unregister_buf(blk, data.buf);
4089 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004090 qemu_vfree(data.buf);
4091 blk_unref(blk);
4092
4093 if (ret) {
4094 return 1;
4095 }
4096 return 0;
4097}
4098
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004099#define C_BS 01
4100#define C_COUNT 02
4101#define C_IF 04
4102#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004103#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004104
4105struct DdInfo {
4106 unsigned int flags;
4107 int64_t count;
4108};
4109
4110struct DdIo {
4111 int bsz; /* Block size */
4112 char *filename;
4113 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004114 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004115};
4116
4117struct DdOpts {
4118 const char *name;
4119 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4120 unsigned int flag;
4121};
4122
4123static int img_dd_bs(const char *arg,
4124 struct DdIo *in, struct DdIo *out,
4125 struct DdInfo *dd)
4126{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004127 int64_t res;
4128
Markus Armbruster606caa02017-02-21 21:14:04 +01004129 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004130
Markus Armbruster606caa02017-02-21 21:14:04 +01004131 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004132 error_report("invalid number: '%s'", arg);
4133 return 1;
4134 }
4135 in->bsz = out->bsz = res;
4136
4137 return 0;
4138}
4139
4140static int img_dd_count(const char *arg,
4141 struct DdIo *in, struct DdIo *out,
4142 struct DdInfo *dd)
4143{
Markus Armbruster606caa02017-02-21 21:14:04 +01004144 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004145
Markus Armbruster606caa02017-02-21 21:14:04 +01004146 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004147 error_report("invalid number: '%s'", arg);
4148 return 1;
4149 }
4150
4151 return 0;
4152}
4153
4154static int img_dd_if(const char *arg,
4155 struct DdIo *in, struct DdIo *out,
4156 struct DdInfo *dd)
4157{
4158 in->filename = g_strdup(arg);
4159
4160 return 0;
4161}
4162
4163static int img_dd_of(const char *arg,
4164 struct DdIo *in, struct DdIo *out,
4165 struct DdInfo *dd)
4166{
4167 out->filename = g_strdup(arg);
4168
4169 return 0;
4170}
4171
Reda Sallahif7c15532016-08-10 16:16:09 +02004172static int img_dd_skip(const char *arg,
4173 struct DdIo *in, struct DdIo *out,
4174 struct DdInfo *dd)
4175{
Markus Armbruster606caa02017-02-21 21:14:04 +01004176 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004177
Markus Armbruster606caa02017-02-21 21:14:04 +01004178 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004179 error_report("invalid number: '%s'", arg);
4180 return 1;
4181 }
4182
4183 return 0;
4184}
4185
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004186static int img_dd(int argc, char **argv)
4187{
4188 int ret = 0;
4189 char *arg = NULL;
4190 char *tmp;
4191 BlockDriver *drv = NULL, *proto_drv = NULL;
4192 BlockBackend *blk1 = NULL, *blk2 = NULL;
4193 QemuOpts *opts = NULL;
4194 QemuOptsList *create_opts = NULL;
4195 Error *local_err = NULL;
4196 bool image_opts = false;
4197 int c, i;
4198 const char *out_fmt = "raw";
4199 const char *fmt = NULL;
4200 int64_t size = 0;
4201 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004202 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004203 struct DdInfo dd = {
4204 .flags = 0,
4205 .count = 0,
4206 };
4207 struct DdIo in = {
4208 .bsz = 512, /* Block size is by default 512 bytes */
4209 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004210 .buf = NULL,
4211 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004212 };
4213 struct DdIo out = {
4214 .bsz = 512,
4215 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004216 .buf = NULL,
4217 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004218 };
4219
4220 const struct DdOpts options[] = {
4221 { "bs", img_dd_bs, C_BS },
4222 { "count", img_dd_count, C_COUNT },
4223 { "if", img_dd_if, C_IF },
4224 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004225 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004226 { NULL, NULL, 0 }
4227 };
4228 const struct option long_options[] = {
4229 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004230 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004231 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004232 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004233 { 0, 0, 0, 0 }
4234 };
4235
Fam Zheng335e9932017-05-03 00:35:39 +08004236 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004237 if (c == EOF) {
4238 break;
4239 }
4240 switch (c) {
4241 case 'O':
4242 out_fmt = optarg;
4243 break;
4244 case 'f':
4245 fmt = optarg;
4246 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004247 case ':':
4248 missing_argument(argv[optind - 1]);
4249 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004250 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004251 unrecognized_option(argv[optind - 1]);
4252 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004253 case 'h':
4254 help();
4255 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004256 case 'U':
4257 force_share = true;
4258 break;
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004259 case OPTION_OBJECT:
4260 if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004261 ret = -1;
4262 goto out;
4263 }
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004264 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004265 case OPTION_IMAGE_OPTS:
4266 image_opts = true;
4267 break;
4268 }
4269 }
4270
4271 for (i = optind; i < argc; i++) {
4272 int j;
4273 arg = g_strdup(argv[i]);
4274
4275 tmp = strchr(arg, '=');
4276 if (tmp == NULL) {
4277 error_report("unrecognized operand %s", arg);
4278 ret = -1;
4279 goto out;
4280 }
4281
4282 *tmp++ = '\0';
4283
4284 for (j = 0; options[j].name != NULL; j++) {
4285 if (!strcmp(arg, options[j].name)) {
4286 break;
4287 }
4288 }
4289 if (options[j].name == NULL) {
4290 error_report("unrecognized operand %s", arg);
4291 ret = -1;
4292 goto out;
4293 }
4294
4295 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4296 ret = -1;
4297 goto out;
4298 }
4299 dd.flags |= options[j].flag;
4300 g_free(arg);
4301 arg = NULL;
4302 }
4303
4304 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4305 error_report("Must specify both input and output files");
4306 ret = -1;
4307 goto out;
4308 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004309
4310 if (qemu_opts_foreach(&qemu_object_opts,
4311 user_creatable_add_opts_foreach,
4312 NULL, NULL)) {
4313 ret = -1;
4314 goto out;
4315 }
4316
Fam Zheng335e9932017-05-03 00:35:39 +08004317 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4318 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004319
4320 if (!blk1) {
4321 ret = -1;
4322 goto out;
4323 }
4324
4325 drv = bdrv_find_format(out_fmt);
4326 if (!drv) {
4327 error_report("Unknown file format");
4328 ret = -1;
4329 goto out;
4330 }
4331 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4332
4333 if (!proto_drv) {
4334 error_report_err(local_err);
4335 ret = -1;
4336 goto out;
4337 }
4338 if (!drv->create_opts) {
4339 error_report("Format driver '%s' does not support image creation",
4340 drv->format_name);
4341 ret = -1;
4342 goto out;
4343 }
4344 if (!proto_drv->create_opts) {
4345 error_report("Protocol driver '%s' does not support image creation",
4346 proto_drv->format_name);
4347 ret = -1;
4348 goto out;
4349 }
4350 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4351 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4352
4353 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4354
4355 size = blk_getlength(blk1);
4356 if (size < 0) {
4357 error_report("Failed to get size for '%s'", in.filename);
4358 ret = -1;
4359 goto out;
4360 }
4361
4362 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4363 dd.count * in.bsz < size) {
4364 size = dd.count * in.bsz;
4365 }
4366
Reda Sallahif7c15532016-08-10 16:16:09 +02004367 /* Overflow means the specified offset is beyond input image's size */
4368 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4369 size < in.bsz * in.offset)) {
4370 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4371 } else {
4372 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4373 size - in.bsz * in.offset, &error_abort);
4374 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004375
4376 ret = bdrv_create(drv, out.filename, opts, &local_err);
4377 if (ret < 0) {
4378 error_reportf_err(local_err,
4379 "%s: error while creating output image: ",
4380 out.filename);
4381 ret = -1;
4382 goto out;
4383 }
4384
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004385 /* TODO, we can't honour --image-opts for the target,
4386 * since it needs to be given in a format compatible
4387 * with the bdrv_create() call above which does not
4388 * support image-opts style.
4389 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01004390 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004391 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004392
4393 if (!blk2) {
4394 ret = -1;
4395 goto out;
4396 }
4397
Reda Sallahif7c15532016-08-10 16:16:09 +02004398 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4399 size < in.offset * in.bsz)) {
4400 /* We give a warning if the skip option is bigger than the input
4401 * size and create an empty output disk image (i.e. like dd(1)).
4402 */
4403 error_report("%s: cannot skip to specified offset", in.filename);
4404 in_pos = size;
4405 } else {
4406 in_pos = in.offset * in.bsz;
4407 }
4408
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004409 in.buf = g_new(uint8_t, in.bsz);
4410
Reda Sallahif7c15532016-08-10 16:16:09 +02004411 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004412 int in_ret, out_ret;
4413
4414 if (in_pos + in.bsz > size) {
4415 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4416 } else {
4417 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4418 }
4419 if (in_ret < 0) {
4420 error_report("error while reading from input image file: %s",
4421 strerror(-in_ret));
4422 ret = -1;
4423 goto out;
4424 }
4425 in_pos += in_ret;
4426
4427 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4428
4429 if (out_ret < 0) {
4430 error_report("error while writing to output image file: %s",
4431 strerror(-out_ret));
4432 ret = -1;
4433 goto out;
4434 }
4435 out_pos += out_ret;
4436 }
4437
4438out:
4439 g_free(arg);
4440 qemu_opts_del(opts);
4441 qemu_opts_free(create_opts);
4442 blk_unref(blk1);
4443 blk_unref(blk2);
4444 g_free(in.filename);
4445 g_free(out.filename);
4446 g_free(in.buf);
4447 g_free(out.buf);
4448
4449 if (ret) {
4450 return 1;
4451 }
4452 return 0;
4453}
4454
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004455static void dump_json_block_measure_info(BlockMeasureInfo *info)
4456{
4457 QString *str;
4458 QObject *obj;
4459 Visitor *v = qobject_output_visitor_new(&obj);
4460
4461 visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
4462 visit_complete(v, &obj);
4463 str = qobject_to_json_pretty(obj);
4464 assert(str != NULL);
4465 printf("%s\n", qstring_get_str(str));
4466 qobject_decref(obj);
4467 visit_free(v);
4468 QDECREF(str);
4469}
4470
4471static int img_measure(int argc, char **argv)
4472{
4473 static const struct option long_options[] = {
4474 {"help", no_argument, 0, 'h'},
4475 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4476 {"object", required_argument, 0, OPTION_OBJECT},
4477 {"output", required_argument, 0, OPTION_OUTPUT},
4478 {"size", required_argument, 0, OPTION_SIZE},
4479 {"force-share", no_argument, 0, 'U'},
4480 {0, 0, 0, 0}
4481 };
4482 OutputFormat output_format = OFORMAT_HUMAN;
4483 BlockBackend *in_blk = NULL;
4484 BlockDriver *drv;
4485 const char *filename = NULL;
4486 const char *fmt = NULL;
4487 const char *out_fmt = "raw";
4488 char *options = NULL;
4489 char *snapshot_name = NULL;
4490 bool force_share = false;
4491 QemuOpts *opts = NULL;
4492 QemuOpts *object_opts = NULL;
4493 QemuOpts *sn_opts = NULL;
4494 QemuOptsList *create_opts = NULL;
4495 bool image_opts = false;
4496 uint64_t img_size = UINT64_MAX;
4497 BlockMeasureInfo *info = NULL;
4498 Error *local_err = NULL;
4499 int ret = 1;
4500 int c;
4501
4502 while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
4503 long_options, NULL)) != -1) {
4504 switch (c) {
4505 case '?':
4506 case 'h':
4507 help();
4508 break;
4509 case 'f':
4510 fmt = optarg;
4511 break;
4512 case 'O':
4513 out_fmt = optarg;
4514 break;
4515 case 'o':
4516 if (!is_valid_option_list(optarg)) {
4517 error_report("Invalid option list: %s", optarg);
4518 goto out;
4519 }
4520 if (!options) {
4521 options = g_strdup(optarg);
4522 } else {
4523 char *old_options = options;
4524 options = g_strdup_printf("%s,%s", options, optarg);
4525 g_free(old_options);
4526 }
4527 break;
4528 case 'l':
4529 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
4530 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
4531 optarg, false);
4532 if (!sn_opts) {
4533 error_report("Failed in parsing snapshot param '%s'",
4534 optarg);
4535 goto out;
4536 }
4537 } else {
4538 snapshot_name = optarg;
4539 }
4540 break;
4541 case 'U':
4542 force_share = true;
4543 break;
4544 case OPTION_OBJECT:
4545 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
4546 optarg, true);
4547 if (!object_opts) {
4548 goto out;
4549 }
4550 break;
4551 case OPTION_IMAGE_OPTS:
4552 image_opts = true;
4553 break;
4554 case OPTION_OUTPUT:
4555 if (!strcmp(optarg, "json")) {
4556 output_format = OFORMAT_JSON;
4557 } else if (!strcmp(optarg, "human")) {
4558 output_format = OFORMAT_HUMAN;
4559 } else {
4560 error_report("--output must be used with human or json "
4561 "as argument.");
4562 goto out;
4563 }
4564 break;
4565 case OPTION_SIZE:
4566 {
4567 int64_t sval;
4568
4569 sval = cvtnum(optarg);
4570 if (sval < 0) {
4571 if (sval == -ERANGE) {
4572 error_report("Image size must be less than 8 EiB!");
4573 } else {
4574 error_report("Invalid image size specified! You may use "
4575 "k, M, G, T, P or E suffixes for ");
4576 error_report("kilobytes, megabytes, gigabytes, terabytes, "
4577 "petabytes and exabytes.");
4578 }
4579 goto out;
4580 }
4581 img_size = (uint64_t)sval;
4582 }
4583 break;
4584 }
4585 }
4586
4587 if (qemu_opts_foreach(&qemu_object_opts,
4588 user_creatable_add_opts_foreach,
4589 NULL, NULL)) {
4590 goto out;
4591 }
4592
4593 if (argc - optind > 1) {
4594 error_report("At most one filename argument is allowed.");
4595 goto out;
4596 } else if (argc - optind == 1) {
4597 filename = argv[optind];
4598 }
4599
4600 if (!filename &&
4601 (object_opts || image_opts || fmt || snapshot_name || sn_opts)) {
4602 error_report("--object, --image-opts, -f, and -l "
4603 "require a filename argument.");
4604 goto out;
4605 }
4606 if (filename && img_size != UINT64_MAX) {
4607 error_report("--size N cannot be used together with a filename.");
4608 goto out;
4609 }
4610 if (!filename && img_size == UINT64_MAX) {
4611 error_report("Either --size N or one filename must be specified.");
4612 goto out;
4613 }
4614
4615 if (filename) {
4616 in_blk = img_open(image_opts, filename, fmt, 0,
4617 false, false, force_share);
4618 if (!in_blk) {
4619 goto out;
4620 }
4621
4622 if (sn_opts) {
4623 bdrv_snapshot_load_tmp(blk_bs(in_blk),
4624 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
4625 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
4626 &local_err);
4627 } else if (snapshot_name != NULL) {
4628 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
4629 snapshot_name, &local_err);
4630 }
4631 if (local_err) {
4632 error_reportf_err(local_err, "Failed to load snapshot: ");
4633 goto out;
4634 }
4635 }
4636
4637 drv = bdrv_find_format(out_fmt);
4638 if (!drv) {
4639 error_report("Unknown file format '%s'", out_fmt);
4640 goto out;
4641 }
4642 if (!drv->create_opts) {
4643 error_report("Format driver '%s' does not support image creation",
4644 drv->format_name);
4645 goto out;
4646 }
4647
4648 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4649 create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
4650 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4651 if (options) {
4652 qemu_opts_do_parse(opts, options, NULL, &local_err);
4653 if (local_err) {
4654 error_report_err(local_err);
4655 error_report("Invalid options for file format '%s'", out_fmt);
4656 goto out;
4657 }
4658 }
4659 if (img_size != UINT64_MAX) {
4660 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
4661 }
4662
4663 info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
4664 if (local_err) {
4665 error_report_err(local_err);
4666 goto out;
4667 }
4668
4669 if (output_format == OFORMAT_HUMAN) {
4670 printf("required size: %" PRIu64 "\n", info->required);
4671 printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
4672 } else {
4673 dump_json_block_measure_info(info);
4674 }
4675
4676 ret = 0;
4677
4678out:
4679 qapi_free_BlockMeasureInfo(info);
4680 qemu_opts_del(object_opts);
4681 qemu_opts_del(opts);
4682 qemu_opts_del(sn_opts);
4683 qemu_opts_free(create_opts);
4684 g_free(options);
4685 blk_unref(in_blk);
4686 return ret;
4687}
Kevin Wolfb6133b82014-08-05 14:17:13 +02004688
Anthony Liguoric227f092009-10-01 16:12:16 -05004689static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004690#define DEF(option, callback, arg_string) \
4691 { option, callback },
4692#include "qemu-img-cmds.h"
4693#undef DEF
4694#undef GEN_DOCS
4695 { NULL, NULL, },
4696};
4697
bellardea2384d2004-08-01 21:59:26 +00004698int main(int argc, char **argv)
4699{
Anthony Liguoric227f092009-10-01 16:12:16 -05004700 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004701 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004702 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004703 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004704 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004705 static const struct option long_options[] = {
4706 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004707 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004708 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004709 {0, 0, 0, 0}
4710 };
bellardea2384d2004-08-01 21:59:26 +00004711
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004712#ifdef CONFIG_POSIX
4713 signal(SIGPIPE, SIG_IGN);
4714#endif
4715
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004716 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004717 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004718 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004719
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004720 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004721 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004722 exit(EXIT_FAILURE);
4723 }
4724
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004725 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004726
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004727 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004728 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004729 if (argc < 2) {
4730 error_exit("Not enough arguments");
4731 }
Stuart Brady153859b2009-06-07 00:42:17 +01004732
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004733 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004734 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004735 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004736
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004737 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004738 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004739 case ':':
4740 missing_argument(argv[optind - 1]);
4741 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08004742 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004743 unrecognized_option(argv[optind - 1]);
4744 return 0;
4745 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03004746 help();
4747 return 0;
4748 case 'V':
4749 printf(QEMU_IMG_VERSION);
4750 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004751 case 'T':
4752 g_free(trace_file);
4753 trace_file = trace_opt_parse(optarg);
4754 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004755 }
bellardea2384d2004-08-01 21:59:26 +00004756 }
Stuart Brady153859b2009-06-07 00:42:17 +01004757
Denis V. Lunev10985132016-06-17 17:44:13 +03004758 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004759
Denis V. Lunev10985132016-06-17 17:44:13 +03004760 /* reset getopt_long scanning */
4761 argc -= optind;
4762 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004763 return 0;
4764 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004765 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004766 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004767
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004768 if (!trace_init_backends()) {
4769 exit(1);
4770 }
4771 trace_init_file(trace_file);
4772 qemu_set_log(LOG_TRACE);
4773
Denis V. Lunev10985132016-06-17 17:44:13 +03004774 /* find the command */
4775 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4776 if (!strcmp(cmdname, cmd->name)) {
4777 return cmd->handler(argc, argv);
4778 }
4779 }
Jeff Cody7db16892014-04-25 17:02:32 -04004780
Stuart Brady153859b2009-06-07 00:42:17 +01004781 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004782 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004783}