blob: 930ae88a0dab002a9ee7b5c3b4db02d320a1aa8f [file] [log] [blame]
bellardea2384d2004-08-01 21:59:26 +00001/*
bellardfb43f4d2006-08-07 21:34:46 +00002 * QEMU disk image utility
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardea2384d2004-08-01 21:59:26 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Peter Maydell80c71a22016-01-18 18:01:42 +000024#include "qemu/osdep.h"
Fam Zheng67a1de02016-06-01 17:44:21 +080025#include "qemu-version.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010026#include "qapi/error.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020027#include "qapi-visit.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010028#include "qapi/qobject-output-visitor.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010029#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010030#include "qapi/qmp/qjson.h"
Fam Zheng335e9932017-05-03 00:35:39 +080031#include "qapi/qmp/qbool.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020032#include "qemu/cutils.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000033#include "qemu/config-file.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010034#include "qemu/option.h"
35#include "qemu/error-report.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030036#include "qemu/log.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000037#include "qom/object_interfaces.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/sysemu.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020039#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010040#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020041#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080042#include "block/qapi.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010043#include "crypto/init.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030044#include "trace/control.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020045#include <getopt.h>
bellarde8445332006-06-14 15:32:10 +000046
Don Slutz61979a62015-01-09 10:17:35 -050047#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
Thomas Huth0781dd62016-10-05 11:54:44 +020048 "\n" QEMU_COPYRIGHT "\n"
Jeff Cody5f6979c2014-04-28 14:37:18 -040049
Anthony Liguoric227f092009-10-01 16:12:16 -050050typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010051 const char *name;
52 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050053} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010054
Federico Simoncelli8599ea42013-01-28 06:59:47 -050055enum {
56 OPTION_OUTPUT = 256,
57 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000058 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000059 OPTION_IMAGE_OPTS = 259,
Kevin Wolfb6495fa2015-07-10 18:09:18 +020060 OPTION_PATTERN = 260,
Kevin Wolf55d539c2016-06-03 13:59:41 +020061 OPTION_FLUSH_INTERVAL = 261,
62 OPTION_NO_DRAIN = 262,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +010063 OPTION_TARGET_IMAGE_OPTS = 263,
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +010064 OPTION_SIZE = 264,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050065};
66
67typedef enum OutputFormat {
68 OFORMAT_JSON,
69 OFORMAT_HUMAN,
70} OutputFormat;
71
Kevin Wolfe6996142016-03-15 13:03:11 +010072/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040073#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000074
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010075static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000076{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010077 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000078}
79
Fam Zhengac1307a2014-04-22 13:36:11 +080080static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
81{
82 va_list ap;
83
84 error_printf("qemu-img: ");
85
86 va_start(ap, fmt);
87 error_vprintf(fmt, ap);
88 va_end(ap);
89
90 error_printf("\nTry 'qemu-img --help' for more information\n");
91 exit(EXIT_FAILURE);
92}
93
Stefan Hajnoczic9192972017-03-17 18:45:41 +080094static void QEMU_NORETURN missing_argument(const char *option)
95{
96 error_exit("missing argument for option '%s'", option);
97}
98
99static void QEMU_NORETURN unrecognized_option(const char *option)
100{
101 error_exit("unrecognized option '%s'", option);
102}
103
blueswir1d2c639d2009-01-24 18:19:25 +0000104/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +0800105static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +0000106{
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100107 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -0400108 QEMU_IMG_VERSION
Denis V. Lunev10985132016-06-17 17:44:13 +0300109 "usage: qemu-img [standard options] command [command options]\n"
malc3f020d72010-02-08 12:04:56 +0300110 "QEMU disk image utility\n"
111 "\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300112 " '-h', '--help' display this help and exit\n"
113 " '-V', '--version' output version information and exit\n"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +0300114 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
115 " specify tracing options\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300116 "\n"
malc3f020d72010-02-08 12:04:56 +0300117 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100118#define DEF(option, callback, arg_string) \
119 " " arg_string "\n"
120#include "qemu-img-cmds.h"
121#undef DEF
122#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +0300123 "\n"
124 "Command parameters:\n"
125 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000126 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
127 " manual page for a description of the object properties. The most common\n"
128 " object type is a 'secret', which is used to supply passwords and/or\n"
129 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300130 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400131 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800132 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
133 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100134 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
135 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300136 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200137 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
138 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
139 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300140 " 'output_filename' is the destination disk image filename\n"
141 " 'output_fmt' is the destination format\n"
142 " 'options' is a comma separated list of format specific options in a\n"
143 " name=value format. Use -o ? for an overview of the options supported by the\n"
144 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800145 " 'snapshot_param' is param used for internal snapshot, format\n"
146 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
147 " '[ID_OR_NAME]'\n"
148 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
149 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300150 " '-c' indicates that target image must be compressed (qcow format only)\n"
151 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
152 " match exactly. The image doesn't need a working backing file before\n"
153 " rebasing in this case (useful for renaming the backing file)\n"
154 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200155 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100156 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200157 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
158 " contain only zeros for qemu-img to create a sparse image during\n"
159 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
160 " unallocated or zero sectors, and the destination image will always be\n"
161 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200162 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100163 " '-n' skips the target volume creation (useful if the volume is created\n"
164 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300165 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200166 "Parameters to check subcommand:\n"
167 " '-r' tries to repair any inconsistencies that are found during the check.\n"
168 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
169 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200170 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200171 "\n"
Peter Lieven2d9187b2017-02-28 13:40:07 +0100172 "Parameters to convert subcommand:\n"
173 " '-m' specifies how many coroutines work in parallel during the convert\n"
174 " process (defaults to 8)\n"
175 " '-W' allow to write to the target out of order rather than sequential\n"
176 "\n"
malc3f020d72010-02-08 12:04:56 +0300177 "Parameters to snapshot subcommand:\n"
178 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
179 " '-a' applies a snapshot (revert disk to saved state)\n"
180 " '-c' creates a snapshot\n"
181 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100182 " '-l' lists all snapshots in the given image\n"
183 "\n"
184 "Parameters to compare subcommand:\n"
185 " '-f' first image format\n"
186 " '-F' second image format\n"
Reda Sallahi86ce1f62016-08-10 04:43:12 +0200187 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
188 "\n"
189 "Parameters to dd subcommand:\n"
190 " 'bs=BYTES' read and write up to BYTES bytes at a time "
191 "(default: 512)\n"
192 " 'count=N' copy only N input blocks\n"
193 " 'if=FILE' read from FILE\n"
Reda Sallahif7c15532016-08-10 16:16:09 +0200194 " 'of=FILE' write to FILE\n"
195 " 'skip=N' skip N bs-sized blocks at the start of input\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100196
197 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100198 bdrv_iterate_format(format_print, NULL);
bellardea2384d2004-08-01 21:59:26 +0000199 printf("\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800200 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000201}
202
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000203static QemuOptsList qemu_object_opts = {
204 .name = "object",
205 .implied_opt_name = "qom-type",
206 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
207 .desc = {
208 { }
209 },
210};
211
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000212static QemuOptsList qemu_source_opts = {
213 .name = "source",
214 .implied_opt_name = "file",
215 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
216 .desc = {
217 { }
218 },
219};
220
Stefan Weil7c30f652013-06-16 17:01:05 +0200221static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100222{
223 int ret = 0;
224 if (!quiet) {
225 va_list args;
226 va_start(args, fmt);
227 ret = vprintf(fmt, args);
228 va_end(args);
229 }
230 return ret;
231}
232
bellardea2384d2004-08-01 21:59:26 +0000233
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100234static int print_block_option_help(const char *filename, const char *fmt)
235{
236 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800237 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500238 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100239
240 /* Find driver and parse its options */
241 drv = bdrv_find_format(fmt);
242 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100243 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100244 return 1;
245 }
246
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800247 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100248 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500249 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100250 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100251 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800252 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100253 return 1;
254 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800255 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100256 }
257
Chunyan Liu83d05212014-06-05 17:20:51 +0800258 qemu_opts_print_help(create_opts);
259 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100260 return 0;
261}
262
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000263
Max Reitzefaa7c42016-03-16 19:54:38 +0100264static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100265 QemuOpts *opts, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800266 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000267{
268 QDict *options;
269 Error *local_err = NULL;
270 BlockBackend *blk;
271 options = qemu_opts_to_qdict(opts, NULL);
Fam Zheng335e9932017-05-03 00:35:39 +0800272 if (force_share) {
273 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
274 && !qdict_get_bool(options, BDRV_OPT_FORCE_SHARE)) {
275 error_report("--force-share/-U conflicts with image options");
Fam Zhengadb998c2017-05-15 22:10:14 +0800276 QDECREF(options);
Fam Zheng335e9932017-05-03 00:35:39 +0800277 return NULL;
278 }
Eric Blake579cf1d2017-05-15 14:54:39 -0500279 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800280 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100281 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000282 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100283 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000284 return NULL;
285 }
Kevin Wolfce099542016-03-15 13:01:04 +0100286 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000287
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000288 return blk;
289}
290
Max Reitzefaa7c42016-03-16 19:54:38 +0100291static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100292 QDict *options,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000293 const char *fmt, int flags,
Fam Zheng335e9932017-05-03 00:35:39 +0800294 bool writethrough, bool quiet,
295 bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000296{
297 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200298 Error *local_err = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100299
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100300 if (!options) {
301 options = qdict_new();
302 }
bellard75c23802004-08-27 21:28:58 +0000303 if (fmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500304 qdict_put_str(options, "driver", fmt);
bellard75c23802004-08-27 21:28:58 +0000305 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100306
Fam Zheng335e9932017-05-03 00:35:39 +0800307 if (force_share) {
Eric Blake579cf1d2017-05-15 14:54:39 -0500308 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800309 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100310 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500311 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100312 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000313 return NULL;
bellard75c23802004-08-27 21:28:58 +0000314 }
Kevin Wolfce099542016-03-15 13:01:04 +0100315 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100316
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200317 return blk;
bellard75c23802004-08-27 21:28:58 +0000318}
319
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000320
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100321static int img_add_key_secrets(void *opaque,
322 const char *name, const char *value,
323 Error **errp)
324{
325 QDict *options = opaque;
326
327 if (g_str_has_suffix(name, "key-secret")) {
328 qdict_put(options, name, qstring_from_str(value));
329 }
330
331 return 0;
332}
333
334static BlockBackend *img_open_new_file(const char *filename,
335 QemuOpts *create_opts,
336 const char *fmt, int flags,
337 bool writethrough, bool quiet,
338 bool force_share)
339{
340 QDict *options = NULL;
341
342 options = qdict_new();
343 qemu_opt_foreach(create_opts, img_add_key_secrets, options, &error_abort);
344
345 return img_open_file(filename, options, fmt, flags, writethrough, quiet,
346 force_share);
347}
348
349
Max Reitzefaa7c42016-03-16 19:54:38 +0100350static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000351 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100352 const char *fmt, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800353 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000354{
355 BlockBackend *blk;
356 if (image_opts) {
357 QemuOpts *opts;
358 if (fmt) {
359 error_report("--image-opts and --format are mutually exclusive");
360 return NULL;
361 }
362 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
363 filename, true);
364 if (!opts) {
365 return NULL;
366 }
Fam Zheng335e9932017-05-03 00:35:39 +0800367 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
368 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000369 } else {
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100370 blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
Fam Zheng335e9932017-05-03 00:35:39 +0800371 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000372 }
373 return blk;
374}
375
376
Chunyan Liu83d05212014-06-05 17:20:51 +0800377static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100378 const char *base_filename,
379 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200380{
Markus Armbruster6750e792015-02-12 17:43:08 +0100381 Error *err = NULL;
382
Kevin Wolfefa84d42009-05-18 16:42:12 +0200383 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100384 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100385 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100386 error_report("Backing file not supported for file format '%s'",
387 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100388 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900389 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200390 }
391 }
392 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100393 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100394 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100395 error_report("Backing file format not supported for file "
396 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100397 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900398 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200399 }
400 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900401 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200402}
403
Markus Armbruster606caa02017-02-21 21:14:04 +0100404static int64_t cvtnum(const char *s)
405{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100406 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100407 uint64_t value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100408
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100409 err = qemu_strtosz(s, NULL, &value);
410 if (err < 0) {
411 return err;
412 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100413 if (value > INT64_MAX) {
414 return -ERANGE;
415 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100416 return value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100417}
418
bellardea2384d2004-08-01 21:59:26 +0000419static int img_create(int argc, char **argv)
420{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200421 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100422 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000423 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000424 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000425 const char *filename;
426 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200427 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200428 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100429 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000430
bellardea2384d2004-08-01 21:59:26 +0000431 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000432 static const struct option long_options[] = {
433 {"help", no_argument, 0, 'h'},
434 {"object", required_argument, 0, OPTION_OBJECT},
435 {0, 0, 0, 0}
436 };
Daniel P. Berrange6b4df542017-07-04 13:30:09 +0100437 c = getopt_long(argc, argv, ":F:b:f:ho:q",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000438 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100439 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000440 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100441 }
bellardea2384d2004-08-01 21:59:26 +0000442 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800443 case ':':
444 missing_argument(argv[optind - 1]);
445 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100446 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800447 unrecognized_option(argv[optind - 1]);
448 break;
bellardea2384d2004-08-01 21:59:26 +0000449 case 'h':
450 help();
451 break;
aliguori9230eaf2009-03-28 17:55:19 +0000452 case 'F':
453 base_fmt = optarg;
454 break;
bellardea2384d2004-08-01 21:59:26 +0000455 case 'b':
456 base_filename = optarg;
457 break;
458 case 'f':
459 fmt = optarg;
460 break;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200461 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100462 if (!is_valid_option_list(optarg)) {
463 error_report("Invalid option list: %s", optarg);
464 goto fail;
465 }
466 if (!options) {
467 options = g_strdup(optarg);
468 } else {
469 char *old_options = options;
470 options = g_strdup_printf("%s,%s", options, optarg);
471 g_free(old_options);
472 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200473 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100474 case 'q':
475 quiet = true;
476 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000477 case OPTION_OBJECT: {
478 QemuOpts *opts;
479 opts = qemu_opts_parse_noisily(&qemu_object_opts,
480 optarg, true);
481 if (!opts) {
482 goto fail;
483 }
484 } break;
bellardea2384d2004-08-01 21:59:26 +0000485 }
486 }
aliguori9230eaf2009-03-28 17:55:19 +0000487
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900488 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100489 filename = (optind < argc) ? argv[optind] : NULL;
490 if (options && has_help_option(options)) {
491 g_free(options);
492 return print_block_option_help(filename, fmt);
493 }
494
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100495 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800496 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100497 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100498 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900499
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000500 if (qemu_opts_foreach(&qemu_object_opts,
501 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200502 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000503 goto fail;
504 }
505
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100506 /* Get image size, if specified */
507 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100508 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100509
510 sval = cvtnum(argv[optind++]);
511 if (sval < 0) {
liguang79443392012-12-17 09:49:23 +0800512 if (sval == -ERANGE) {
513 error_report("Image size must be less than 8 EiB!");
514 } else {
515 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200516 "G, T, P or E suffixes for ");
517 error_report("kilobytes, megabytes, gigabytes, terabytes, "
518 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800519 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100520 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100521 }
522 img_size = (uint64_t)sval;
523 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200524 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800525 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200526 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100527
Luiz Capitulino9b375252012-11-30 10:52:05 -0200528 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Fam Zheng92172832017-04-21 20:27:01 +0800529 options, img_size, 0, quiet, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100530 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100531 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100532 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900533 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200534
Kevin Wolf77386bf2014-02-21 16:24:04 +0100535 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000536 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100537
538fail:
539 g_free(options);
540 return 1;
bellardea2384d2004-08-01 21:59:26 +0000541}
542
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100543static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500544{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500545 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500546 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100547 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600548
549 visit_type_ImageCheck(v, NULL, &check, &error_abort);
550 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500551 str = qobject_to_json_pretty(obj);
552 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100553 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500554 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600555 visit_free(v);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500556 QDECREF(str);
557}
558
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100559static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500560{
561 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100562 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500563 } else {
564 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100565 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
566 "Data may be corrupted, or further writes to the image "
567 "may corrupt it.\n",
568 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500569 }
570
571 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100572 qprintf(quiet,
573 "\n%" PRId64 " leaked clusters were found on the image.\n"
574 "This means waste of disk space, but no harm to data.\n",
575 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500576 }
577
578 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100579 qprintf(quiet,
580 "\n%" PRId64
581 " internal errors have occurred during the check.\n",
582 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500583 }
584 }
585
586 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100587 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
588 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
589 check->allocated_clusters, check->total_clusters,
590 check->allocated_clusters * 100.0 / check->total_clusters,
591 check->fragmented_clusters * 100.0 / check->allocated_clusters,
592 check->compressed_clusters * 100.0 /
593 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500594 }
595
596 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100597 qprintf(quiet,
598 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500599 }
600}
601
602static int collect_image_check(BlockDriverState *bs,
603 ImageCheck *check,
604 const char *filename,
605 const char *fmt,
606 int fix)
607{
608 int ret;
609 BdrvCheckResult result;
610
611 ret = bdrv_check(bs, &result, fix);
612 if (ret < 0) {
613 return ret;
614 }
615
616 check->filename = g_strdup(filename);
617 check->format = g_strdup(bdrv_get_format_name(bs));
618 check->check_errors = result.check_errors;
619 check->corruptions = result.corruptions;
620 check->has_corruptions = result.corruptions != 0;
621 check->leaks = result.leaks;
622 check->has_leaks = result.leaks != 0;
623 check->corruptions_fixed = result.corruptions_fixed;
624 check->has_corruptions_fixed = result.corruptions != 0;
625 check->leaks_fixed = result.leaks_fixed;
626 check->has_leaks_fixed = result.leaks != 0;
627 check->image_end_offset = result.image_end_offset;
628 check->has_image_end_offset = result.image_end_offset != 0;
629 check->total_clusters = result.bfi.total_clusters;
630 check->has_total_clusters = result.bfi.total_clusters != 0;
631 check->allocated_clusters = result.bfi.allocated_clusters;
632 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
633 check->fragmented_clusters = result.bfi.fragmented_clusters;
634 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100635 check->compressed_clusters = result.bfi.compressed_clusters;
636 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500637
638 return 0;
639}
640
Kevin Wolfe076f332010-06-29 11:43:13 +0200641/*
642 * Checks an image for consistency. Exit codes:
643 *
Max Reitzd6635c42014-06-02 22:15:21 +0200644 * 0 - Check completed, image is good
645 * 1 - Check not completed because of internal errors
646 * 2 - Check completed, image is corrupted
647 * 3 - Check completed, image has leaked clusters, but is good otherwise
648 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200649 */
aliguori15859692009-04-21 23:11:53 +0000650static int img_check(int argc, char **argv)
651{
652 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500653 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200654 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200655 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000656 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200657 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100658 int flags = BDRV_O_CHECK;
659 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200660 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100661 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000662 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +0800663 bool force_share = false;
aliguori15859692009-04-21 23:11:53 +0000664
665 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500666 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200667 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100668
aliguori15859692009-04-21 23:11:53 +0000669 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500670 int option_index = 0;
671 static const struct option long_options[] = {
672 {"help", no_argument, 0, 'h'},
673 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530674 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500675 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000676 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000677 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +0800678 {"force-share", no_argument, 0, 'U'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500679 {0, 0, 0, 0}
680 };
Fam Zheng335e9932017-05-03 00:35:39 +0800681 c = getopt_long(argc, argv, ":hf:r:T:qU",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500682 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100683 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000684 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100685 }
aliguori15859692009-04-21 23:11:53 +0000686 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800687 case ':':
688 missing_argument(argv[optind - 1]);
689 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100690 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800691 unrecognized_option(argv[optind - 1]);
692 break;
aliguori15859692009-04-21 23:11:53 +0000693 case 'h':
694 help();
695 break;
696 case 'f':
697 fmt = optarg;
698 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200699 case 'r':
700 flags |= BDRV_O_RDWR;
701
702 if (!strcmp(optarg, "leaks")) {
703 fix = BDRV_FIX_LEAKS;
704 } else if (!strcmp(optarg, "all")) {
705 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
706 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800707 error_exit("Unknown option value for -r "
708 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200709 }
710 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500711 case OPTION_OUTPUT:
712 output = optarg;
713 break;
Max Reitz40055952014-07-22 22:58:42 +0200714 case 'T':
715 cache = optarg;
716 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100717 case 'q':
718 quiet = true;
719 break;
Fam Zheng335e9932017-05-03 00:35:39 +0800720 case 'U':
721 force_share = true;
722 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000723 case OPTION_OBJECT: {
724 QemuOpts *opts;
725 opts = qemu_opts_parse_noisily(&qemu_object_opts,
726 optarg, true);
727 if (!opts) {
728 return 1;
729 }
730 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000731 case OPTION_IMAGE_OPTS:
732 image_opts = true;
733 break;
aliguori15859692009-04-21 23:11:53 +0000734 }
735 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200736 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800737 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100738 }
aliguori15859692009-04-21 23:11:53 +0000739 filename = argv[optind++];
740
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500741 if (output && !strcmp(output, "json")) {
742 output_format = OFORMAT_JSON;
743 } else if (output && !strcmp(output, "human")) {
744 output_format = OFORMAT_HUMAN;
745 } else if (output) {
746 error_report("--output must be used with human or json as argument.");
747 return 1;
748 }
749
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000750 if (qemu_opts_foreach(&qemu_object_opts,
751 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200752 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000753 return 1;
754 }
755
Kevin Wolfce099542016-03-15 13:01:04 +0100756 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200757 if (ret < 0) {
758 error_report("Invalid source cache option: %s", cache);
759 return 1;
760 }
761
Fam Zheng335e9932017-05-03 00:35:39 +0800762 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
763 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200764 if (!blk) {
765 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900766 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200767 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500768
769 check = g_new0(ImageCheck, 1);
770 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200771
772 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200773 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200774 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500775 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200776 }
777
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500778 if (check->corruptions_fixed || check->leaks_fixed) {
779 int corruptions_fixed, leaks_fixed;
780
781 leaks_fixed = check->leaks_fixed;
782 corruptions_fixed = check->corruptions_fixed;
783
784 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100785 qprintf(quiet,
786 "The following inconsistencies were found and repaired:\n\n"
787 " %" PRId64 " leaked clusters\n"
788 " %" PRId64 " corruptions\n\n"
789 "Double checking the fixed image now...\n",
790 check->leaks_fixed,
791 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500792 }
793
794 ret = collect_image_check(bs, check, filename, fmt, 0);
795
796 check->leaks_fixed = leaks_fixed;
797 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200798 }
799
Max Reitz832390a2014-10-23 15:29:12 +0200800 if (!ret) {
801 switch (output_format) {
802 case OFORMAT_HUMAN:
803 dump_human_image_check(check, quiet);
804 break;
805 case OFORMAT_JSON:
806 dump_json_image_check(check, quiet);
807 break;
808 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500809 }
810
811 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200812 if (ret) {
813 error_report("Check failed: %s", strerror(-ret));
814 } else {
815 error_report("Check failed");
816 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500817 ret = 1;
818 goto fail;
819 }
820
821 if (check->corruptions) {
822 ret = 2;
823 } else if (check->leaks) {
824 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200825 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500826 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000827 }
828
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500829fail:
830 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200831 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500832 return ret;
aliguori15859692009-04-21 23:11:53 +0000833}
834
Max Reitzd4a32382014-10-24 15:57:37 +0200835typedef struct CommonBlockJobCBInfo {
836 BlockDriverState *bs;
837 Error **errp;
838} CommonBlockJobCBInfo;
839
840static void common_block_job_cb(void *opaque, int ret)
841{
842 CommonBlockJobCBInfo *cbi = opaque;
843
844 if (ret < 0) {
845 error_setg_errno(cbi->errp, -ret, "Block job failed");
846 }
Max Reitzd4a32382014-10-24 15:57:37 +0200847}
848
849static void run_block_job(BlockJob *job, Error **errp)
850{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200851 AioContext *aio_context = blk_get_aio_context(job->blk);
sochin.jiang4172a002017-06-15 14:47:33 +0800852 int ret = 0;
Max Reitzd4a32382014-10-24 15:57:37 +0200853
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200854 aio_context_acquire(aio_context);
sochin.jiang4172a002017-06-15 14:47:33 +0800855 block_job_ref(job);
Max Reitzd4a32382014-10-24 15:57:37 +0200856 do {
857 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500858 qemu_progress_print(job->len ?
859 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
sochin.jiang4172a002017-06-15 14:47:33 +0800860 } while (!job->ready && !job->completed);
Max Reitzd4a32382014-10-24 15:57:37 +0200861
sochin.jiang4172a002017-06-15 14:47:33 +0800862 if (!job->completed) {
863 ret = block_job_complete_sync(job, errp);
864 } else {
865 ret = job->ret;
866 }
867 block_job_unref(job);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200868 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200869
sochin.jiang4172a002017-06-15 14:47:33 +0800870 /* publish completion progress only when success */
871 if (!ret) {
872 qemu_progress_print(100.f, 0);
873 }
Max Reitzd4a32382014-10-24 15:57:37 +0200874}
875
bellardea2384d2004-08-01 21:59:26 +0000876static int img_commit(int argc, char **argv)
877{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400878 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200879 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200880 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200881 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100882 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200883 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100884 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200885 Error *local_err = NULL;
886 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000887 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200888 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000889
890 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400891 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200892 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000893 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000894 static const struct option long_options[] = {
895 {"help", no_argument, 0, 'h'},
896 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000897 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000898 {0, 0, 0, 0}
899 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800900 c = getopt_long(argc, argv, ":f:ht:b:dpq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000901 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100902 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000903 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100904 }
bellardea2384d2004-08-01 21:59:26 +0000905 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800906 case ':':
907 missing_argument(argv[optind - 1]);
908 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100909 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800910 unrecognized_option(argv[optind - 1]);
911 break;
bellardea2384d2004-08-01 21:59:26 +0000912 case 'h':
913 help();
914 break;
915 case 'f':
916 fmt = optarg;
917 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400918 case 't':
919 cache = optarg;
920 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200921 case 'b':
922 base = optarg;
923 /* -b implies -d */
924 drop = true;
925 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200926 case 'd':
927 drop = true;
928 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200929 case 'p':
930 progress = true;
931 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100932 case 'q':
933 quiet = true;
934 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000935 case OPTION_OBJECT: {
936 QemuOpts *opts;
937 opts = qemu_opts_parse_noisily(&qemu_object_opts,
938 optarg, true);
939 if (!opts) {
940 return 1;
941 }
942 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000943 case OPTION_IMAGE_OPTS:
944 image_opts = true;
945 break;
bellardea2384d2004-08-01 21:59:26 +0000946 }
947 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200948
949 /* Progress is not shown in Quiet mode */
950 if (quiet) {
951 progress = false;
952 }
953
Kevin Wolffc11eb22013-08-05 10:53:04 +0200954 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800955 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100956 }
bellardea2384d2004-08-01 21:59:26 +0000957 filename = argv[optind++];
958
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000959 if (qemu_opts_foreach(&qemu_object_opts,
960 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200961 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000962 return 1;
963 }
964
Max Reitz9a86fe42014-10-24 15:57:38 +0200965 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100966 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400967 if (ret < 0) {
968 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100969 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400970 }
971
Fam Zheng335e9932017-05-03 00:35:39 +0800972 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
973 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200974 if (!blk) {
975 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900976 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200977 bs = blk_bs(blk);
978
Max Reitz687fa1d2014-10-24 15:57:39 +0200979 qemu_progress_init(progress, 1.f);
980 qemu_progress_print(0.f, 100);
981
Max Reitz1b22bff2014-10-24 15:57:40 +0200982 if (base) {
983 base_bs = bdrv_find_backing_image(bs, base);
984 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +0100985 error_setg(&local_err,
986 "Did not find '%s' in the backing chain of '%s'",
987 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +0200988 goto done;
989 }
990 } else {
991 /* This is different from QMP, which by default uses the deepest file in
992 * the backing chain (i.e., the very base); however, the traditional
993 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +0200994 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +0200995 if (!base_bs) {
996 error_setg(&local_err, "Image does not have a backing file");
997 goto done;
998 }
bellardea2384d2004-08-01 21:59:26 +0000999 }
1000
Max Reitzd4a32382014-10-24 15:57:37 +02001001 cbi = (CommonBlockJobCBInfo){
1002 .errp = &local_err,
1003 .bs = bs,
1004 };
1005
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001006 aio_context = bdrv_get_aio_context(bs);
1007 aio_context_acquire(aio_context);
John Snow47970df2016-10-27 12:06:57 -04001008 commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
Kevin Wolf0db832f2017-02-20 18:10:05 +01001009 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +08001010 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001011 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +02001012 if (local_err) {
1013 goto done;
1014 }
1015
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001016 /* When the block job completes, the BlockBackend reference will point to
1017 * the old backing file. In order to avoid that the top image is already
1018 * deleted, so we can still empty it afterwards, increment the reference
1019 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +02001020 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001021 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001022 }
1023
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001024 job = block_job_get("commit");
1025 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001026 if (local_err) {
1027 goto unref_backing;
1028 }
1029
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001030 if (!drop && bs->drv->bdrv_make_empty) {
1031 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001032 if (ret) {
1033 error_setg_errno(&local_err, -ret, "Could not empty %s",
1034 filename);
1035 goto unref_backing;
1036 }
1037 }
1038
1039unref_backing:
1040 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001041 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001042 }
Max Reitzd4a32382014-10-24 15:57:37 +02001043
1044done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001045 qemu_progress_end();
1046
Markus Armbruster26f54e92014-10-07 13:59:04 +02001047 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001048
1049 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001050 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001051 return 1;
1052 }
Max Reitzd4a32382014-10-24 15:57:37 +02001053
1054 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001055 return 0;
1056}
1057
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001058/*
thsf58c7b32008-06-05 21:53:49 +00001059 * Returns true iff the first sector pointed to by 'buf' contains at least
1060 * a non-NUL byte.
1061 *
1062 * 'pnum' is set to the number of sectors (including and immediately following
1063 * the first one) that are known to be in the same allocated/unallocated state.
1064 */
bellardea2384d2004-08-01 21:59:26 +00001065static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1066{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001067 bool is_zero;
1068 int i;
bellardea2384d2004-08-01 21:59:26 +00001069
1070 if (n <= 0) {
1071 *pnum = 0;
1072 return 0;
1073 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001074 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001075 for(i = 1; i < n; i++) {
1076 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001077 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001078 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001079 }
bellardea2384d2004-08-01 21:59:26 +00001080 }
1081 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001082 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001083}
1084
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001085/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001086 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1087 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1088 * breaking up write requests for only small sparse areas.
1089 */
1090static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1091 int min)
1092{
1093 int ret;
1094 int num_checked, num_used;
1095
1096 if (n < min) {
1097 min = n;
1098 }
1099
1100 ret = is_allocated_sectors(buf, n, pnum);
1101 if (!ret) {
1102 return ret;
1103 }
1104
1105 num_used = *pnum;
1106 buf += BDRV_SECTOR_SIZE * *pnum;
1107 n -= *pnum;
1108 num_checked = num_used;
1109
1110 while (n > 0) {
1111 ret = is_allocated_sectors(buf, n, pnum);
1112
1113 buf += BDRV_SECTOR_SIZE * *pnum;
1114 n -= *pnum;
1115 num_checked += *pnum;
1116 if (ret) {
1117 num_used = num_checked;
1118 } else if (*pnum >= min) {
1119 break;
1120 }
1121 }
1122
1123 *pnum = num_used;
1124 return 1;
1125}
1126
1127/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001128 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1129 * buffers matches, non-zero otherwise.
1130 *
1131 * pnum is set to the number of sectors (including and immediately following
1132 * the first one) that are known to have the same comparison result
1133 */
1134static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1135 int *pnum)
1136{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001137 bool res;
1138 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001139
1140 if (n <= 0) {
1141 *pnum = 0;
1142 return 0;
1143 }
1144
1145 res = !!memcmp(buf1, buf2, 512);
1146 for(i = 1; i < n; i++) {
1147 buf1 += 512;
1148 buf2 += 512;
1149
1150 if (!!memcmp(buf1, buf2, 512) != res) {
1151 break;
1152 }
1153 }
1154
1155 *pnum = i;
1156 return res;
1157}
1158
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001159#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001160
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001161static int64_t sectors_to_bytes(int64_t sectors)
1162{
1163 return sectors << BDRV_SECTOR_BITS;
1164}
1165
1166static int64_t sectors_to_process(int64_t total, int64_t from)
1167{
1168 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1169}
1170
1171/*
1172 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1173 *
1174 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1175 * data and negative value on error.
1176 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001177 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001178 * @param sect_num: Number of first sector to check
1179 * @param sect_count: Number of sectors to check
1180 * @param filename: Name of disk file we are checking (logging purpose)
1181 * @param buffer: Allocated buffer for storing read data
1182 * @param quiet: Flag for quiet mode
1183 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001184static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001185 int sect_count, const char *filename,
1186 uint8_t *buffer, bool quiet)
1187{
1188 int pnum, ret = 0;
Eric Blake91669202016-05-06 10:26:43 -06001189 ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer,
1190 sect_count << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001191 if (ret < 0) {
1192 error_report("Error while reading offset %" PRId64 " of %s: %s",
1193 sectors_to_bytes(sect_num), filename, strerror(-ret));
1194 return ret;
1195 }
1196 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1197 if (ret || pnum != sect_count) {
1198 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1199 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1200 return 1;
1201 }
1202
1203 return 0;
1204}
1205
1206/*
1207 * Compares two images. Exit codes:
1208 *
1209 * 0 - Images are identical
1210 * 1 - Images differ
1211 * >1 - Error occurred
1212 */
1213static int img_compare(int argc, char **argv)
1214{
Max Reitz40055952014-07-22 22:58:42 +02001215 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001216 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001217 BlockDriverState *bs1, *bs2;
1218 int64_t total_sectors1, total_sectors2;
1219 uint8_t *buf1 = NULL, *buf2 = NULL;
1220 int pnum1, pnum2;
1221 int allocated1, allocated2;
1222 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1223 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001224 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001225 bool writethrough;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001226 int64_t total_sectors;
1227 int64_t sector_num = 0;
1228 int64_t nb_sectors;
1229 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001230 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001231 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001232 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001233
Max Reitz40055952014-07-22 22:58:42 +02001234 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001235 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001236 static const struct option long_options[] = {
1237 {"help", no_argument, 0, 'h'},
1238 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001239 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001240 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001241 {0, 0, 0, 0}
1242 };
Fam Zheng335e9932017-05-03 00:35:39 +08001243 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001244 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001245 if (c == -1) {
1246 break;
1247 }
1248 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001249 case ':':
1250 missing_argument(argv[optind - 1]);
1251 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001252 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001253 unrecognized_option(argv[optind - 1]);
1254 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001255 case 'h':
1256 help();
1257 break;
1258 case 'f':
1259 fmt1 = optarg;
1260 break;
1261 case 'F':
1262 fmt2 = optarg;
1263 break;
Max Reitz40055952014-07-22 22:58:42 +02001264 case 'T':
1265 cache = optarg;
1266 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001267 case 'p':
1268 progress = true;
1269 break;
1270 case 'q':
1271 quiet = true;
1272 break;
1273 case 's':
1274 strict = true;
1275 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001276 case 'U':
1277 force_share = true;
1278 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001279 case OPTION_OBJECT: {
1280 QemuOpts *opts;
1281 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1282 optarg, true);
1283 if (!opts) {
1284 ret = 2;
1285 goto out4;
1286 }
1287 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001288 case OPTION_IMAGE_OPTS:
1289 image_opts = true;
1290 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001291 }
1292 }
1293
1294 /* Progress is not shown in Quiet mode */
1295 if (quiet) {
1296 progress = false;
1297 }
1298
1299
Kevin Wolffc11eb22013-08-05 10:53:04 +02001300 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001301 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001302 }
1303 filename1 = argv[optind++];
1304 filename2 = argv[optind++];
1305
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001306 if (qemu_opts_foreach(&qemu_object_opts,
1307 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001308 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001309 ret = 2;
1310 goto out4;
1311 }
1312
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001313 /* Initialize before goto out */
1314 qemu_progress_init(progress, 2.0);
1315
Kevin Wolfce099542016-03-15 13:01:04 +01001316 flags = 0;
1317 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001318 if (ret < 0) {
1319 error_report("Invalid source cache option: %s", cache);
1320 ret = 2;
1321 goto out3;
1322 }
1323
Fam Zheng335e9932017-05-03 00:35:39 +08001324 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1325 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001326 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001327 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001328 goto out3;
1329 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001330
Fam Zheng335e9932017-05-03 00:35:39 +08001331 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1332 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001333 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001334 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001335 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001336 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001337 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001338 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001339
Max Reitzf1d3cd72015-02-05 13:58:18 -05001340 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1341 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1342 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001343 if (total_sectors1 < 0) {
1344 error_report("Can't get size of %s: %s",
1345 filename1, strerror(-total_sectors1));
1346 ret = 4;
1347 goto out;
1348 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001349 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001350 if (total_sectors2 < 0) {
1351 error_report("Can't get size of %s: %s",
1352 filename2, strerror(-total_sectors2));
1353 ret = 4;
1354 goto out;
1355 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001356 total_sectors = MIN(total_sectors1, total_sectors2);
1357 progress_base = MAX(total_sectors1, total_sectors2);
1358
1359 qemu_progress_print(0, 100);
1360
1361 if (strict && total_sectors1 != total_sectors2) {
1362 ret = 1;
1363 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1364 goto out;
1365 }
1366
1367 for (;;) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001368 int64_t status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001369 BlockDriverState *file;
1370
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001371 nb_sectors = sectors_to_process(total_sectors, sector_num);
1372 if (nb_sectors <= 0) {
1373 break;
1374 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001375 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1376 total_sectors1 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001377 &pnum1, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001378 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001379 ret = 3;
1380 error_report("Sector allocation test failed for %s", filename1);
1381 goto out;
1382 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001383 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001384
Fam Zheng25ad8e62016-01-13 16:37:41 +08001385 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1386 total_sectors2 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001387 &pnum2, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001388 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001389 ret = 3;
1390 error_report("Sector allocation test failed for %s", filename2);
1391 goto out;
1392 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001393 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1394 if (pnum1) {
1395 nb_sectors = MIN(nb_sectors, pnum1);
1396 }
1397 if (pnum2) {
1398 nb_sectors = MIN(nb_sectors, pnum2);
1399 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001400
Fam Zheng25ad8e62016-01-13 16:37:41 +08001401 if (strict) {
1402 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1403 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1404 ret = 1;
1405 qprintf(quiet, "Strict mode: Offset %" PRId64
1406 " block status mismatch!\n",
1407 sectors_to_bytes(sector_num));
1408 goto out;
1409 }
1410 }
1411 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1412 nb_sectors = MIN(pnum1, pnum2);
1413 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001414 if (allocated1) {
Eric Blake91669202016-05-06 10:26:43 -06001415 ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1,
1416 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001417 if (ret < 0) {
1418 error_report("Error while reading offset %" PRId64 " of %s:"
1419 " %s", sectors_to_bytes(sector_num), filename1,
1420 strerror(-ret));
1421 ret = 4;
1422 goto out;
1423 }
Eric Blake91669202016-05-06 10:26:43 -06001424 ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2,
1425 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001426 if (ret < 0) {
1427 error_report("Error while reading offset %" PRId64
1428 " of %s: %s", sectors_to_bytes(sector_num),
1429 filename2, strerror(-ret));
1430 ret = 4;
1431 goto out;
1432 }
1433 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1434 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001435 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1436 sectors_to_bytes(
1437 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001438 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001439 goto out;
1440 }
1441 }
1442 } else {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001443
1444 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001445 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001446 filename1, buf1, quiet);
1447 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001448 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001449 filename2, buf1, quiet);
1450 }
1451 if (ret) {
1452 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001453 error_report("Error while reading offset %" PRId64 ": %s",
1454 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001455 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001456 }
1457 goto out;
1458 }
1459 }
1460 sector_num += nb_sectors;
1461 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1462 }
1463
1464 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001465 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001466 int64_t total_sectors_over;
1467 const char *filename_over;
1468
1469 qprintf(quiet, "Warning: Image size mismatch!\n");
1470 if (total_sectors1 > total_sectors2) {
1471 total_sectors_over = total_sectors1;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001472 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001473 filename_over = filename1;
1474 } else {
1475 total_sectors_over = total_sectors2;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001476 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001477 filename_over = filename2;
1478 }
1479
1480 for (;;) {
Eric Blake51b0a482017-07-07 07:44:59 -05001481 int64_t count;
1482
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001483 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1484 if (nb_sectors <= 0) {
1485 break;
1486 }
Eric Blake51b0a482017-07-07 07:44:59 -05001487 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL,
1488 sector_num * BDRV_SECTOR_SIZE,
1489 nb_sectors * BDRV_SECTOR_SIZE,
1490 &count);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001491 if (ret < 0) {
1492 ret = 3;
1493 error_report("Sector allocation test failed for %s",
1494 filename_over);
1495 goto out;
1496
1497 }
Eric Blake51b0a482017-07-07 07:44:59 -05001498 /* TODO relax this once bdrv_is_allocated_above does not enforce
1499 * sector alignment */
1500 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
1501 nb_sectors = count >> BDRV_SECTOR_BITS;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001502 if (ret) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001503 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001504 filename_over, buf1, quiet);
1505 if (ret) {
1506 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001507 error_report("Error while reading offset %" PRId64
1508 " of %s: %s", sectors_to_bytes(sector_num),
1509 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001510 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001511 }
1512 goto out;
1513 }
1514 }
1515 sector_num += nb_sectors;
1516 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1517 }
1518 }
1519
1520 qprintf(quiet, "Images are identical.\n");
1521 ret = 0;
1522
1523out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001524 qemu_vfree(buf1);
1525 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001526 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001527out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001528 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001529out3:
1530 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001531out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001532 return ret;
1533}
1534
Kevin Wolf690c7302015-03-19 13:33:32 +01001535enum ImgConvertBlockStatus {
1536 BLK_DATA,
1537 BLK_ZERO,
1538 BLK_BACKING_FILE,
1539};
1540
Peter Lieven2d9187b2017-02-28 13:40:07 +01001541#define MAX_COROUTINES 16
1542
Kevin Wolf690c7302015-03-19 13:33:32 +01001543typedef struct ImgConvertState {
1544 BlockBackend **src;
1545 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001546 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001547 int64_t total_sectors;
1548 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001549 int64_t allocated_done;
1550 int64_t sector_num;
1551 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001552 enum ImgConvertBlockStatus status;
1553 int64_t sector_next_status;
1554 BlockBackend *target;
1555 bool has_zero_init;
1556 bool compressed;
1557 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001558 bool wr_in_order;
Kevin Wolf690c7302015-03-19 13:33:32 +01001559 int min_sparse;
1560 size_t cluster_sectors;
1561 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001562 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001563 int running_coroutines;
1564 Coroutine *co[MAX_COROUTINES];
1565 int64_t wait_sector_num[MAX_COROUTINES];
1566 CoMutex lock;
1567 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001568} ImgConvertState;
1569
Peter Lieven2d9187b2017-02-28 13:40:07 +01001570static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1571 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001572{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001573 *src_cur = 0;
1574 *src_cur_offset = 0;
1575 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1576 *src_cur_offset += s->src_sectors[*src_cur];
1577 (*src_cur)++;
1578 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001579 }
1580}
1581
1582static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1583{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001584 int64_t ret, src_cur_offset;
1585 int n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001586
Peter Lieven2d9187b2017-02-28 13:40:07 +01001587 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001588
1589 assert(s->total_sectors > sector_num);
1590 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1591
1592 if (s->sector_next_status <= sector_num) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08001593 BlockDriverState *file;
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001594 if (s->target_has_backing) {
1595 ret = bdrv_get_block_status(blk_bs(s->src[src_cur]),
1596 sector_num - src_cur_offset,
1597 n, &n, &file);
1598 } else {
1599 ret = bdrv_get_block_status_above(blk_bs(s->src[src_cur]), NULL,
1600 sector_num - src_cur_offset,
1601 n, &n, &file);
1602 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001603 if (ret < 0) {
1604 return ret;
1605 }
1606
1607 if (ret & BDRV_BLOCK_ZERO) {
1608 s->status = BLK_ZERO;
1609 } else if (ret & BDRV_BLOCK_DATA) {
1610 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001611 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001612 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001613 }
1614
1615 s->sector_next_status = sector_num + n;
1616 }
1617
1618 n = MIN(n, s->sector_next_status - sector_num);
1619 if (s->status == BLK_DATA) {
1620 n = MIN(n, s->buf_sectors);
1621 }
1622
1623 /* We need to write complete clusters for compressed images, so if an
1624 * unallocated area is shorter than that, we must consider the whole
1625 * cluster allocated. */
1626 if (s->compressed) {
1627 if (n < s->cluster_sectors) {
1628 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1629 s->status = BLK_DATA;
1630 } else {
1631 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1632 }
1633 }
1634
1635 return n;
1636}
1637
Peter Lieven2d9187b2017-02-28 13:40:07 +01001638static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1639 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001640{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001641 int n, ret;
1642 QEMUIOVector qiov;
1643 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001644
Kevin Wolf690c7302015-03-19 13:33:32 +01001645 assert(nb_sectors <= s->buf_sectors);
1646 while (nb_sectors > 0) {
1647 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001648 int src_cur;
1649 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001650
1651 /* In the case of compression with multiple source files, we can get a
1652 * nb_sectors that spreads into the next part. So we must be able to
1653 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001654 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1655 blk = s->src[src_cur];
1656 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001657
Peter Lieven2d9187b2017-02-28 13:40:07 +01001658 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1659 iov.iov_base = buf;
1660 iov.iov_len = n << BDRV_SECTOR_BITS;
1661 qemu_iovec_init_external(&qiov, &iov, 1);
1662
1663 ret = blk_co_preadv(
1664 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1665 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001666 if (ret < 0) {
1667 return ret;
1668 }
1669
1670 sector_num += n;
1671 nb_sectors -= n;
1672 buf += n * BDRV_SECTOR_SIZE;
1673 }
1674
1675 return 0;
1676}
1677
Peter Lieven2d9187b2017-02-28 13:40:07 +01001678
1679static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1680 int nb_sectors, uint8_t *buf,
1681 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001682{
1683 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001684 QEMUIOVector qiov;
1685 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001686
1687 while (nb_sectors > 0) {
1688 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001689 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1690
Peter Lieven2d9187b2017-02-28 13:40:07 +01001691 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001692 case BLK_BACKING_FILE:
1693 /* If we have a backing file, leave clusters unallocated that are
1694 * unallocated in the source image, so that the backing file is
1695 * visible at the respective offset. */
1696 assert(s->target_has_backing);
1697 break;
1698
1699 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001700 /* If we're told to keep the target fully allocated (-S 0) or there
1701 * is real non-zero data, we must write it. Otherwise we can treat
1702 * it as zero sectors.
1703 * Compressed clusters need to be written as a whole, so in that
1704 * case we can only save the write if the buffer is completely
1705 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001706 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001707 (!s->compressed &&
1708 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1709 (s->compressed &&
1710 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001711 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001712 iov.iov_base = buf;
1713 iov.iov_len = n << BDRV_SECTOR_BITS;
1714 qemu_iovec_init_external(&qiov, &iov, 1);
1715
1716 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
Lidong Chendb933fb2017-04-27 10:58:27 +08001717 n << BDRV_SECTOR_BITS, &qiov, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001718 if (ret < 0) {
1719 return ret;
1720 }
1721 break;
1722 }
1723 /* fall-through */
1724
1725 case BLK_ZERO:
1726 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001727 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001728 break;
1729 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001730 ret = blk_co_pwrite_zeroes(s->target,
1731 sector_num << BDRV_SECTOR_BITS,
1732 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001733 if (ret < 0) {
1734 return ret;
1735 }
1736 break;
1737 }
1738
1739 sector_num += n;
1740 nb_sectors -= n;
1741 buf += n * BDRV_SECTOR_SIZE;
1742 }
1743
1744 return 0;
1745}
1746
Peter Lieven2d9187b2017-02-28 13:40:07 +01001747static void coroutine_fn convert_co_do_copy(void *opaque)
1748{
1749 ImgConvertState *s = opaque;
1750 uint8_t *buf = NULL;
1751 int ret, i;
1752 int index = -1;
1753
1754 for (i = 0; i < s->num_coroutines; i++) {
1755 if (s->co[i] == qemu_coroutine_self()) {
1756 index = i;
1757 break;
1758 }
1759 }
1760 assert(index >= 0);
1761
1762 s->running_coroutines++;
1763 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1764
1765 while (1) {
1766 int n;
1767 int64_t sector_num;
1768 enum ImgConvertBlockStatus status;
1769
1770 qemu_co_mutex_lock(&s->lock);
1771 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1772 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001773 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001774 }
1775 n = convert_iteration_sectors(s, s->sector_num);
1776 if (n < 0) {
1777 qemu_co_mutex_unlock(&s->lock);
1778 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001779 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001780 }
1781 /* save current sector and allocation status to local variables */
1782 sector_num = s->sector_num;
1783 status = s->status;
1784 if (!s->min_sparse && s->status == BLK_ZERO) {
1785 n = MIN(n, s->buf_sectors);
1786 }
1787 /* increment global sector counter so that other coroutines can
1788 * already continue reading beyond this request */
1789 s->sector_num += n;
1790 qemu_co_mutex_unlock(&s->lock);
1791
1792 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1793 s->allocated_done += n;
1794 qemu_progress_print(100.0 * s->allocated_done /
1795 s->allocated_sectors, 0);
1796 }
1797
1798 if (status == BLK_DATA) {
1799 ret = convert_co_read(s, sector_num, n, buf);
1800 if (ret < 0) {
1801 error_report("error while reading sector %" PRId64
1802 ": %s", sector_num, strerror(-ret));
1803 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001804 }
1805 } else if (!s->min_sparse && status == BLK_ZERO) {
1806 status = BLK_DATA;
1807 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1808 }
1809
1810 if (s->wr_in_order) {
1811 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03001812 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001813 s->wait_sector_num[index] = sector_num;
1814 qemu_coroutine_yield();
1815 }
1816 s->wait_sector_num[index] = -1;
1817 }
1818
Anton Nefedovb91127e2017-04-26 11:33:15 +03001819 if (s->ret == -EINPROGRESS) {
1820 ret = convert_co_write(s, sector_num, n, buf, status);
1821 if (ret < 0) {
1822 error_report("error while writing sector %" PRId64
1823 ": %s", sector_num, strerror(-ret));
1824 s->ret = ret;
1825 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001826 }
1827
1828 if (s->wr_in_order) {
1829 /* reenter the coroutine that might have waited
1830 * for this write to complete */
1831 s->wr_offs = sector_num + n;
1832 for (i = 0; i < s->num_coroutines; i++) {
1833 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1834 /*
1835 * A -> B -> A cannot occur because A has
1836 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1837 * B will never enter A during this time window.
1838 */
1839 qemu_coroutine_enter(s->co[i]);
1840 break;
1841 }
1842 }
1843 }
1844 }
1845
Peter Lieven2d9187b2017-02-28 13:40:07 +01001846 qemu_vfree(buf);
1847 s->co[index] = NULL;
1848 s->running_coroutines--;
1849 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1850 /* the convert job finished successfully */
1851 s->ret = 0;
1852 }
1853}
1854
Kevin Wolf690c7302015-03-19 13:33:32 +01001855static int convert_do_copy(ImgConvertState *s)
1856{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001857 int ret, i, n;
1858 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001859
1860 /* Check whether we have zero initialisation or can get it efficiently */
1861 s->has_zero_init = s->min_sparse && !s->target_has_backing
1862 ? bdrv_has_zero_init(blk_bs(s->target))
1863 : false;
1864
1865 if (!s->has_zero_init && !s->target_has_backing &&
1866 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1867 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001868 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001869 if (ret == 0) {
1870 s->has_zero_init = true;
1871 }
1872 }
1873
1874 /* Allocate buffer for copied data. For compressed images, only one cluster
1875 * can be copied at a time. */
1876 if (s->compressed) {
1877 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1878 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001879 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001880 }
1881 s->buf_sectors = s->cluster_sectors;
1882 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001883
Kevin Wolf690c7302015-03-19 13:33:32 +01001884 while (sector_num < s->total_sectors) {
1885 n = convert_iteration_sectors(s, sector_num);
1886 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001887 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001888 }
Max Reitzaad15de2016-03-24 23:33:57 +01001889 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1890 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001891 s->allocated_sectors += n;
1892 }
1893 sector_num += n;
1894 }
1895
1896 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001897 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001898 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001899
Peter Lieven2d9187b2017-02-28 13:40:07 +01001900 qemu_co_mutex_init(&s->lock);
1901 for (i = 0; i < s->num_coroutines; i++) {
1902 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1903 s->wait_sector_num[i] = -1;
1904 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001905 }
1906
Anton Nefedovb91127e2017-04-26 11:33:15 +03001907 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001908 main_loop_wait(false);
1909 }
1910
1911 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001912 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001913 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001914 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001915 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001916 }
1917 }
1918
Peter Lieven2d9187b2017-02-28 13:40:07 +01001919 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001920}
1921
bellardea2384d2004-08-01 21:59:26 +00001922static int img_convert(int argc, char **argv)
1923{
Peter Lieven9fd77f92017-04-21 11:11:55 +02001924 int c, bs_i, flags, src_flags = 0;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001925 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02001926 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1927 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001928 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00001929 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001930 BlockDriverState *out_bs;
1931 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08001932 QemuOptsList *create_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001933 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02001934 Error *local_err = NULL;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001935 bool writethrough, src_writethrough, quiet = false, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001936 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001937 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08001938 bool force_share = false;
bellardea2384d2004-08-01 21:59:26 +00001939
Peter Lieven9fd77f92017-04-21 11:11:55 +02001940 ImgConvertState s = (ImgConvertState) {
1941 /* Need at least 4k of zeros for sparse detection */
1942 .min_sparse = 8,
1943 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1944 .wr_in_order = true,
1945 .num_coroutines = 8,
1946 };
1947
bellardea2384d2004-08-01 21:59:26 +00001948 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001949 static const struct option long_options[] = {
1950 {"help", no_argument, 0, 'h'},
1951 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001952 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001953 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001954 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001955 {0, 0, 0, 0}
1956 };
Daniel P. Berrange6b4df542017-07-04 13:30:09 +01001957 c = getopt_long(argc, argv, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001958 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001959 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001960 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001961 }
bellardea2384d2004-08-01 21:59:26 +00001962 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001963 case ':':
1964 missing_argument(argv[optind - 1]);
1965 break;
Jes Sorensenef873942010-12-06 15:25:40 +01001966 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001967 unrecognized_option(argv[optind - 1]);
1968 break;
bellardea2384d2004-08-01 21:59:26 +00001969 case 'h':
1970 help();
1971 break;
1972 case 'f':
1973 fmt = optarg;
1974 break;
1975 case 'O':
1976 out_fmt = optarg;
1977 break;
thsf58c7b32008-06-05 21:53:49 +00001978 case 'B':
1979 out_baseimg = optarg;
1980 break;
bellardea2384d2004-08-01 21:59:26 +00001981 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02001982 s.compressed = true;
bellardea2384d2004-08-01 21:59:26 +00001983 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001984 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001985 if (!is_valid_option_list(optarg)) {
1986 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001987 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001988 }
1989 if (!options) {
1990 options = g_strdup(optarg);
1991 } else {
1992 char *old_options = options;
1993 options = g_strdup_printf("%s,%s", options, optarg);
1994 g_free(old_options);
1995 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001996 break;
edison51ef6722010-09-21 19:58:41 -07001997 case 's':
1998 snapshot_name = optarg;
1999 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08002000 case 'l':
2001 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01002002 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2003 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08002004 if (!sn_opts) {
2005 error_report("Failed in parsing snapshot param '%s'",
2006 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002007 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002008 }
2009 } else {
2010 snapshot_name = optarg;
2011 }
2012 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002013 case 'S':
2014 {
2015 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002016
2017 sval = cvtnum(optarg);
2018 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02002019 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002020 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002021 }
2022
Peter Lieven9fd77f92017-04-21 11:11:55 +02002023 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002024 break;
2025 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002026 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002027 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002028 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002029 case 't':
2030 cache = optarg;
2031 break;
Max Reitz40055952014-07-22 22:58:42 +02002032 case 'T':
2033 src_cache = optarg;
2034 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002035 case 'q':
2036 quiet = true;
2037 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002038 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002039 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002040 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002041 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002042 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2043 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002044 error_report("Invalid number of coroutines. Allowed number of"
2045 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002046 goto fail_getopt;
2047 }
2048 break;
2049 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002050 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002051 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002052 case 'U':
2053 force_share = true;
2054 break;
Max Reitz3258b912017-04-26 15:46:47 +02002055 case OPTION_OBJECT: {
2056 QemuOpts *object_opts;
2057 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2058 optarg, true);
2059 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002060 goto fail_getopt;
2061 }
2062 break;
Max Reitz3258b912017-04-26 15:46:47 +02002063 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002064 case OPTION_IMAGE_OPTS:
2065 image_opts = true;
2066 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002067 case OPTION_TARGET_IMAGE_OPTS:
2068 tgt_image_opts = true;
2069 break;
bellardea2384d2004-08-01 21:59:26 +00002070 }
2071 }
ths3b46e622007-09-17 08:09:54 +00002072
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002073 if (!out_fmt && !tgt_image_opts) {
2074 out_fmt = "raw";
2075 }
2076
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002077 if (qemu_opts_foreach(&qemu_object_opts,
2078 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002079 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002080 goto fail_getopt;
2081 }
2082
Peter Lieven9fd77f92017-04-21 11:11:55 +02002083 if (!s.wr_in_order && s.compressed) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002084 error_report("Out of order write and compress are mutually exclusive");
Peter Lieven9fd77f92017-04-21 11:11:55 +02002085 goto fail_getopt;
2086 }
2087
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002088 if (tgt_image_opts && !skip_create) {
2089 error_report("--target-image-opts requires use of -n flag");
2090 goto fail_getopt;
2091 }
2092
Peter Lieven9fd77f92017-04-21 11:11:55 +02002093 s.src_num = argc - optind - 1;
2094 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2095
2096 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002097 if (out_fmt) {
2098 ret = print_block_option_help(out_filename, out_fmt);
2099 goto fail_getopt;
2100 } else {
2101 error_report("Option help requires a format be specified");
2102 goto fail_getopt;
2103 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002104 }
2105
2106 if (s.src_num < 1) {
2107 error_report("Must specify image file name");
2108 goto fail_getopt;
2109 }
2110
2111
Peter Lieven9fd77f92017-04-21 11:11:55 +02002112 /* ret is still -EINVAL until here */
2113 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2114 if (ret < 0) {
2115 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002116 goto fail_getopt;
2117 }
2118
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002119 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002120 if (quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002121 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002122 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002123 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002124 qemu_progress_print(0, 100);
2125
Peter Lieven9fd77f92017-04-21 11:11:55 +02002126 s.src = g_new0(BlockBackend *, s.src_num);
2127 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002128
Peter Lieven9fd77f92017-04-21 11:11:55 +02002129 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2130 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Fam Zheng335e9932017-05-03 00:35:39 +08002131 fmt, src_flags, src_writethrough, quiet,
2132 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002133 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002134 ret = -1;
2135 goto out;
2136 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002137 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2138 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002139 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002140 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002141 ret = -1;
2142 goto out;
2143 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002144 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002145 }
bellardea2384d2004-08-01 21:59:26 +00002146
Wenchao Xiaef806542013-12-04 17:10:57 +08002147 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002148 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002149 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2150 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2151 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002152 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002153 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002154 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002155 ret = -1;
2156 goto out;
2157 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002158
Peter Lieven9fd77f92017-04-21 11:11:55 +02002159 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2160 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002161 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002162 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002163 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002164 ret = -1;
2165 goto out;
edison51ef6722010-09-21 19:58:41 -07002166 }
2167
Max Reitz2e024cd2015-02-11 09:58:46 -05002168 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002169 /* Find driver and parse its options */
2170 drv = bdrv_find_format(out_fmt);
2171 if (!drv) {
2172 error_report("Unknown file format '%s'", out_fmt);
2173 ret = -1;
2174 goto out;
2175 }
2176
2177 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2178 if (!proto_drv) {
2179 error_report_err(local_err);
2180 ret = -1;
2181 goto out;
2182 }
2183
Max Reitz2e024cd2015-02-11 09:58:46 -05002184 if (!drv->create_opts) {
2185 error_report("Format driver '%s' does not support image creation",
2186 drv->format_name);
2187 ret = -1;
2188 goto out;
2189 }
Max Reitzf75613c2014-12-02 18:32:46 +01002190
Max Reitz2e024cd2015-02-11 09:58:46 -05002191 if (!proto_drv->create_opts) {
2192 error_report("Protocol driver '%s' does not support image creation",
2193 proto_drv->format_name);
2194 ret = -1;
2195 goto out;
2196 }
Max Reitzf75613c2014-12-02 18:32:46 +01002197
Max Reitz2e024cd2015-02-11 09:58:46 -05002198 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2199 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002200
Max Reitz2e024cd2015-02-11 09:58:46 -05002201 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002202 if (options) {
2203 qemu_opts_do_parse(opts, options, NULL, &local_err);
2204 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002205 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002206 ret = -1;
2207 goto out;
2208 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002209 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002210
Peter Lieven9fd77f92017-04-21 11:11:55 +02002211 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002212 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002213 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2214 if (ret < 0) {
2215 goto out;
2216 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002217 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002218
Kevin Wolfa18953f2010-10-14 15:46:04 +02002219 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002220 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002221 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002222 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002223 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002224 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002225
Max Reitz48758a82017-04-26 15:46:48 +02002226 if (s.src_num > 1 && out_baseimg) {
2227 error_report("Having a backing file for the target makes no sense when "
2228 "concatenating multiple input images");
2229 ret = -1;
2230 goto out;
2231 }
2232
Kevin Wolfefa84d42009-05-18 16:42:12 +02002233 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002234 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002235 bool encryption =
2236 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002237 const char *encryptfmt =
2238 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
Chunyan Liu83d05212014-06-05 17:20:51 +08002239 const char *preallocation =
2240 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002241
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002242 if (drv && !drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002243 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002244 ret = -1;
2245 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002246 }
2247
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002248 if (encryption || encryptfmt) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002249 error_report("Compression and encryption not supported at "
2250 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002251 ret = -1;
2252 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002253 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002254
Chunyan Liu83d05212014-06-05 17:20:51 +08002255 if (preallocation
2256 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002257 {
2258 error_report("Compression and preallocation not supported at "
2259 "the same time");
2260 ret = -1;
2261 goto out;
2262 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002263 }
2264
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002265 if (!skip_create) {
2266 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002267 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002268 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002269 error_reportf_err(local_err, "%s: error while converting %s: ",
2270 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002271 goto out;
bellardea2384d2004-08-01 21:59:26 +00002272 }
2273 }
ths3b46e622007-09-17 08:09:54 +00002274
Peter Lieven9fd77f92017-04-21 11:11:55 +02002275 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002276 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002277 if (ret < 0) {
2278 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002279 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002280 }
2281
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002282 if (skip_create) {
2283 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2284 flags, writethrough, quiet, false);
2285 } else {
2286 /* TODO ultimately we should allow --target-image-opts
2287 * to be used even when -n is not given.
2288 * That has to wait for bdrv_create to be improved
2289 * to allow filenames in option syntax
2290 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01002291 s.target = img_open_new_file(out_filename, opts, out_fmt,
2292 flags, writethrough, quiet, false);
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002293 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002294 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002295 ret = -1;
2296 goto out;
2297 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002298 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002299
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002300 if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2301 error_report("Compression not supported for this file format");
2302 ret = -1;
2303 goto out;
2304 }
2305
Eric Blake5def6b82016-06-23 16:37:19 -06002306 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002307 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2308 * as maximum. */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002309 s.buf_sectors = MIN(32768,
2310 MAX(s.buf_sectors,
2311 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2312 out_bs->bl.pdiscard_alignment >>
2313 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002314
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002315 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002316 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002317 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002318 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002319 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002320 ret = -1;
2321 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002322 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002323 error_report("output file is smaller than input file");
2324 ret = -1;
2325 goto out;
2326 }
2327 }
2328
Peter Lieven24f833c2013-11-27 11:07:07 +01002329 ret = bdrv_get_info(out_bs, &bdi);
2330 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002331 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002332 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002333 goto out;
2334 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002335 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002336 s.compressed = s.compressed || bdi.needs_compressed_writes;
2337 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Peter Lieven24f833c2013-11-27 11:07:07 +01002338 }
2339
Peter Lieven9fd77f92017-04-21 11:11:55 +02002340 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002341out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002342 if (!ret) {
2343 qemu_progress_print(100, 0);
2344 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002345 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002346 qemu_opts_del(opts);
2347 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002348 qemu_opts_del(sn_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002349 blk_unref(s.target);
2350 if (s.src) {
2351 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2352 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002353 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002354 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002355 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002356 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002357fail_getopt:
2358 g_free(options);
2359
Peter Lieven9fd77f92017-04-21 11:11:55 +02002360 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002361}
2362
bellard57d1a2b2004-08-03 21:15:11 +00002363
bellardfaea38e2006-08-05 21:31:00 +00002364static void dump_snapshots(BlockDriverState *bs)
2365{
2366 QEMUSnapshotInfo *sn_tab, *sn;
2367 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002368
2369 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2370 if (nb_sns <= 0)
2371 return;
2372 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002373 bdrv_snapshot_dump(fprintf, stdout, NULL);
2374 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002375 for(i = 0; i < nb_sns; i++) {
2376 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002377 bdrv_snapshot_dump(fprintf, stdout, sn);
2378 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002379 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002380 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002381}
2382
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002383static void dump_json_image_info_list(ImageInfoList *list)
2384{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002385 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002386 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002387 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002388
2389 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2390 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002391 str = qobject_to_json_pretty(obj);
2392 assert(str != NULL);
2393 printf("%s\n", qstring_get_str(str));
2394 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002395 visit_free(v);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002396 QDECREF(str);
2397}
2398
Benoît Canetc054b3f2012-09-05 13:09:02 +02002399static void dump_json_image_info(ImageInfo *info)
2400{
Benoît Canetc054b3f2012-09-05 13:09:02 +02002401 QString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002402 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002403 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002404
2405 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2406 visit_complete(v, &obj);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002407 str = qobject_to_json_pretty(obj);
2408 assert(str != NULL);
2409 printf("%s\n", qstring_get_str(str));
2410 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002411 visit_free(v);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002412 QDECREF(str);
2413}
2414
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002415static void dump_human_image_info_list(ImageInfoList *list)
2416{
2417 ImageInfoList *elem;
2418 bool delim = false;
2419
2420 for (elem = list; elem; elem = elem->next) {
2421 if (delim) {
2422 printf("\n");
2423 }
2424 delim = true;
2425
Wenchao Xia5b917042013-05-25 11:09:45 +08002426 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002427 }
2428}
2429
2430static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2431{
2432 return strcmp(a, b) == 0;
2433}
2434
2435/**
2436 * Open an image file chain and return an ImageInfoList
2437 *
2438 * @filename: topmost image filename
2439 * @fmt: topmost image format (may be NULL to autodetect)
2440 * @chain: true - enumerate entire backing file chain
2441 * false - only topmost image file
2442 *
2443 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2444 * image file. If there was an error a message will have been printed to
2445 * stderr.
2446 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002447static ImageInfoList *collect_image_info_list(bool image_opts,
2448 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002449 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002450 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002451{
2452 ImageInfoList *head = NULL;
2453 ImageInfoList **last = &head;
2454 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002455 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002456
2457 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2458
2459 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002460 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002461 BlockDriverState *bs;
2462 ImageInfo *info;
2463 ImageInfoList *elem;
2464
2465 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2466 error_report("Backing file '%s' creates an infinite loop.",
2467 filename);
2468 goto err;
2469 }
2470 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2471
Max Reitzefaa7c42016-03-16 19:54:38 +01002472 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002473 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2474 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002475 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002476 goto err;
2477 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002478 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002479
Wenchao Xia43526ec2013-06-06 12:27:58 +08002480 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002481 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002482 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002483 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002484 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002485 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002486
2487 elem = g_new0(ImageInfoList, 1);
2488 elem->value = info;
2489 *last = elem;
2490 last = &elem->next;
2491
Markus Armbruster26f54e92014-10-07 13:59:04 +02002492 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002493
2494 filename = fmt = NULL;
2495 if (chain) {
2496 if (info->has_full_backing_filename) {
2497 filename = info->full_backing_filename;
2498 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002499 error_report("Could not determine absolute backing filename,"
2500 " but backing filename '%s' present",
2501 info->backing_filename);
2502 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002503 }
2504 if (info->has_backing_filename_format) {
2505 fmt = info->backing_filename_format;
2506 }
2507 }
2508 }
2509 g_hash_table_destroy(filenames);
2510 return head;
2511
2512err:
2513 qapi_free_ImageInfoList(head);
2514 g_hash_table_destroy(filenames);
2515 return NULL;
2516}
2517
Benoît Canetc054b3f2012-09-05 13:09:02 +02002518static int img_info(int argc, char **argv)
2519{
2520 int c;
2521 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002522 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002523 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002524 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002525 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002526 bool force_share = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002527
bellardea2384d2004-08-01 21:59:26 +00002528 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002529 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002530 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002531 int option_index = 0;
2532 static const struct option long_options[] = {
2533 {"help", no_argument, 0, 'h'},
2534 {"format", required_argument, 0, 'f'},
2535 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002536 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002537 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002538 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002539 {"force-share", no_argument, 0, 'U'},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002540 {0, 0, 0, 0}
2541 };
Fam Zheng335e9932017-05-03 00:35:39 +08002542 c = getopt_long(argc, argv, ":f:hU",
Benoît Canetc054b3f2012-09-05 13:09:02 +02002543 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002544 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002545 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002546 }
bellardea2384d2004-08-01 21:59:26 +00002547 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002548 case ':':
2549 missing_argument(argv[optind - 1]);
2550 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002551 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002552 unrecognized_option(argv[optind - 1]);
2553 break;
bellardea2384d2004-08-01 21:59:26 +00002554 case 'h':
2555 help();
2556 break;
2557 case 'f':
2558 fmt = optarg;
2559 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002560 case 'U':
2561 force_share = true;
2562 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002563 case OPTION_OUTPUT:
2564 output = optarg;
2565 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002566 case OPTION_BACKING_CHAIN:
2567 chain = true;
2568 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002569 case OPTION_OBJECT: {
2570 QemuOpts *opts;
2571 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2572 optarg, true);
2573 if (!opts) {
2574 return 1;
2575 }
2576 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002577 case OPTION_IMAGE_OPTS:
2578 image_opts = true;
2579 break;
bellardea2384d2004-08-01 21:59:26 +00002580 }
2581 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002582 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002583 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002584 }
bellardea2384d2004-08-01 21:59:26 +00002585 filename = argv[optind++];
2586
Benoît Canetc054b3f2012-09-05 13:09:02 +02002587 if (output && !strcmp(output, "json")) {
2588 output_format = OFORMAT_JSON;
2589 } else if (output && !strcmp(output, "human")) {
2590 output_format = OFORMAT_HUMAN;
2591 } else if (output) {
2592 error_report("--output must be used with human or json as argument.");
2593 return 1;
2594 }
2595
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002596 if (qemu_opts_foreach(&qemu_object_opts,
2597 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002598 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002599 return 1;
2600 }
2601
Fam Zheng335e9932017-05-03 00:35:39 +08002602 list = collect_image_info_list(image_opts, filename, fmt, chain,
2603 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002604 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002605 return 1;
2606 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002607
Benoît Canetc054b3f2012-09-05 13:09:02 +02002608 switch (output_format) {
2609 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002610 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002611 break;
2612 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002613 if (chain) {
2614 dump_json_image_info_list(list);
2615 } else {
2616 dump_json_image_info(list->value);
2617 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002618 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002619 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002620
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002621 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002622 return 0;
2623}
2624
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002625static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2626 MapEntry *next)
2627{
2628 switch (output_format) {
2629 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002630 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002631 error_report("File contains external, encrypted or compressed clusters.");
2632 exit(1);
2633 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002634 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002635 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002636 e->start, e->length,
2637 e->has_offset ? e->offset : 0,
2638 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002639 }
2640 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2641 * Modify the flags here to allow more coalescing.
2642 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002643 if (next && (!next->data || next->zero)) {
2644 next->data = false;
2645 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002646 }
2647 break;
2648 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002649 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2650 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002651 (e->start == 0 ? "[" : ",\n"),
2652 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002653 e->zero ? "true" : "false",
2654 e->data ? "true" : "false");
2655 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002656 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002657 }
2658 putchar('}');
2659
2660 if (!next) {
2661 printf("]\n");
2662 }
2663 break;
2664 }
2665}
2666
2667static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2668 int nb_sectors, MapEntry *e)
2669{
2670 int64_t ret;
2671 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002672 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002673 bool has_offset;
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 (;;) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08002682 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2683 &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002684 if (ret < 0) {
2685 return ret;
2686 }
2687 assert(nb_sectors);
2688 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2689 break;
2690 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002691 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002692 if (bs == NULL) {
2693 ret = 0;
2694 break;
2695 }
2696
2697 depth++;
2698 }
2699
John Snow28756452016-02-05 13:12:33 -05002700 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2701
2702 *e = (MapEntry) {
2703 .start = sector_num * BDRV_SECTOR_SIZE,
2704 .length = nb_sectors * BDRV_SECTOR_SIZE,
2705 .data = !!(ret & BDRV_BLOCK_DATA),
2706 .zero = !!(ret & BDRV_BLOCK_ZERO),
2707 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2708 .has_offset = has_offset,
2709 .depth = depth,
2710 .has_filename = file && has_offset,
2711 .filename = file && has_offset ? file->filename : NULL,
2712 };
2713
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002714 return 0;
2715}
2716
Fam Zheng16b0d552016-01-26 11:59:02 +08002717static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2718{
2719 if (curr->length == 0) {
2720 return false;
2721 }
2722 if (curr->zero != next->zero ||
2723 curr->data != next->data ||
2724 curr->depth != next->depth ||
2725 curr->has_filename != next->has_filename ||
2726 curr->has_offset != next->has_offset) {
2727 return false;
2728 }
2729 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2730 return false;
2731 }
2732 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2733 return false;
2734 }
2735 return true;
2736}
2737
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002738static int img_map(int argc, char **argv)
2739{
2740 int c;
2741 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002742 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002743 BlockDriverState *bs;
2744 const char *filename, *fmt, *output;
2745 int64_t length;
2746 MapEntry curr = { .length = 0 }, next;
2747 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002748 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002749 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002750
2751 fmt = NULL;
2752 output = NULL;
2753 for (;;) {
2754 int option_index = 0;
2755 static const struct option long_options[] = {
2756 {"help", no_argument, 0, 'h'},
2757 {"format", required_argument, 0, 'f'},
2758 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002759 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002760 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002761 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002762 {0, 0, 0, 0}
2763 };
Fam Zheng335e9932017-05-03 00:35:39 +08002764 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002765 long_options, &option_index);
2766 if (c == -1) {
2767 break;
2768 }
2769 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002770 case ':':
2771 missing_argument(argv[optind - 1]);
2772 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002773 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002774 unrecognized_option(argv[optind - 1]);
2775 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002776 case 'h':
2777 help();
2778 break;
2779 case 'f':
2780 fmt = optarg;
2781 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002782 case 'U':
2783 force_share = true;
2784 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002785 case OPTION_OUTPUT:
2786 output = optarg;
2787 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002788 case OPTION_OBJECT: {
2789 QemuOpts *opts;
2790 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2791 optarg, true);
2792 if (!opts) {
2793 return 1;
2794 }
2795 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002796 case OPTION_IMAGE_OPTS:
2797 image_opts = true;
2798 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002799 }
2800 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002801 if (optind != argc - 1) {
2802 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002803 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002804 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002805
2806 if (output && !strcmp(output, "json")) {
2807 output_format = OFORMAT_JSON;
2808 } else if (output && !strcmp(output, "human")) {
2809 output_format = OFORMAT_HUMAN;
2810 } else if (output) {
2811 error_report("--output must be used with human or json as argument.");
2812 return 1;
2813 }
2814
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002815 if (qemu_opts_foreach(&qemu_object_opts,
2816 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002817 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002818 return 1;
2819 }
2820
Fam Zheng335e9932017-05-03 00:35:39 +08002821 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002822 if (!blk) {
2823 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002824 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002825 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002826
2827 if (output_format == OFORMAT_HUMAN) {
2828 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2829 }
2830
Max Reitzf1d3cd72015-02-05 13:58:18 -05002831 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002832 while (curr.start + curr.length < length) {
2833 int64_t nsectors_left;
2834 int64_t sector_num;
2835 int n;
2836
2837 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2838
2839 /* Probe up to 1 GiB at a time. */
2840 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2841 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2842 ret = get_block_status(bs, sector_num, n, &next);
2843
2844 if (ret < 0) {
2845 error_report("Could not read file metadata: %s", strerror(-ret));
2846 goto out;
2847 }
2848
Fam Zheng16b0d552016-01-26 11:59:02 +08002849 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002850 curr.length += next.length;
2851 continue;
2852 }
2853
2854 if (curr.length > 0) {
2855 dump_map_entry(output_format, &curr, &next);
2856 }
2857 curr = next;
2858 }
2859
2860 dump_map_entry(output_format, &curr, NULL);
2861
2862out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002863 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002864 return ret < 0;
2865}
2866
aliguorif7b4a942009-01-07 17:40:15 +00002867#define SNAPSHOT_LIST 1
2868#define SNAPSHOT_CREATE 2
2869#define SNAPSHOT_APPLY 3
2870#define SNAPSHOT_DELETE 4
2871
Stuart Brady153859b2009-06-07 00:42:17 +01002872static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002873{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002874 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002875 BlockDriverState *bs;
2876 QEMUSnapshotInfo sn;
2877 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002878 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002879 int action = 0;
2880 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002881 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002882 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002883 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002884 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00002885
Kevin Wolfce099542016-03-15 13:01:04 +01002886 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002887 /* Parse commandline parameters */
2888 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002889 static const struct option long_options[] = {
2890 {"help", no_argument, 0, 'h'},
2891 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002892 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002893 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002894 {0, 0, 0, 0}
2895 };
Fam Zheng335e9932017-05-03 00:35:39 +08002896 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002897 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002898 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002899 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002900 }
aliguorif7b4a942009-01-07 17:40:15 +00002901 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002902 case ':':
2903 missing_argument(argv[optind - 1]);
2904 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002905 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002906 unrecognized_option(argv[optind - 1]);
2907 break;
aliguorif7b4a942009-01-07 17:40:15 +00002908 case 'h':
2909 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002910 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002911 case 'l':
2912 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002913 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002914 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002915 }
2916 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002917 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002918 break;
2919 case 'a':
2920 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002921 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002922 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002923 }
2924 action = SNAPSHOT_APPLY;
2925 snapshot_name = optarg;
2926 break;
2927 case 'c':
2928 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002929 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002930 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002931 }
2932 action = SNAPSHOT_CREATE;
2933 snapshot_name = optarg;
2934 break;
2935 case 'd':
2936 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002937 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002938 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002939 }
2940 action = SNAPSHOT_DELETE;
2941 snapshot_name = optarg;
2942 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002943 case 'q':
2944 quiet = true;
2945 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002946 case 'U':
2947 force_share = true;
2948 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002949 case OPTION_OBJECT: {
2950 QemuOpts *opts;
2951 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2952 optarg, true);
2953 if (!opts) {
2954 return 1;
2955 }
2956 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002957 case OPTION_IMAGE_OPTS:
2958 image_opts = true;
2959 break;
aliguorif7b4a942009-01-07 17:40:15 +00002960 }
2961 }
2962
Kevin Wolffc11eb22013-08-05 10:53:04 +02002963 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002964 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002965 }
aliguorif7b4a942009-01-07 17:40:15 +00002966 filename = argv[optind++];
2967
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002968 if (qemu_opts_foreach(&qemu_object_opts,
2969 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002970 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002971 return 1;
2972 }
2973
aliguorif7b4a942009-01-07 17:40:15 +00002974 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08002975 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
2976 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002977 if (!blk) {
2978 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002979 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002980 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002981
2982 /* Perform the requested action */
2983 switch(action) {
2984 case SNAPSHOT_LIST:
2985 dump_snapshots(bs);
2986 break;
2987
2988 case SNAPSHOT_CREATE:
2989 memset(&sn, 0, sizeof(sn));
2990 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2991
2992 qemu_gettimeofday(&tv);
2993 sn.date_sec = tv.tv_sec;
2994 sn.date_nsec = tv.tv_usec * 1000;
2995
2996 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002997 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002998 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002999 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003000 }
aliguorif7b4a942009-01-07 17:40:15 +00003001 break;
3002
3003 case SNAPSHOT_APPLY:
3004 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003005 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003006 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003007 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003008 }
aliguorif7b4a942009-01-07 17:40:15 +00003009 break;
3010
3011 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003012 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003013 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003014 error_reportf_err(err, "Could not delete snapshot '%s': ",
3015 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003016 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003017 }
aliguorif7b4a942009-01-07 17:40:15 +00003018 break;
3019 }
3020
3021 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003022 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003023 if (ret) {
3024 return 1;
3025 }
Stuart Brady153859b2009-06-07 00:42:17 +01003026 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003027}
3028
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003029static int img_rebase(int argc, char **argv)
3030{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003031 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003032 uint8_t *buf_old = NULL;
3033 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05003034 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003035 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003036 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3037 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003038 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003039 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003040 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003041 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003042 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003043 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003044 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003045
3046 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003047 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003048 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003049 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003050 out_baseimg = NULL;
3051 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003052 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003053 static const struct option long_options[] = {
3054 {"help", no_argument, 0, 'h'},
3055 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003056 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003057 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003058 {0, 0, 0, 0}
3059 };
Fam Zheng335e9932017-05-03 00:35:39 +08003060 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003061 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003062 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003063 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003064 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003065 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003066 case ':':
3067 missing_argument(argv[optind - 1]);
3068 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003069 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003070 unrecognized_option(argv[optind - 1]);
3071 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003072 case 'h':
3073 help();
3074 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003075 case 'f':
3076 fmt = optarg;
3077 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003078 case 'F':
3079 out_basefmt = optarg;
3080 break;
3081 case 'b':
3082 out_baseimg = optarg;
3083 break;
3084 case 'u':
3085 unsafe = 1;
3086 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003087 case 'p':
3088 progress = 1;
3089 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003090 case 't':
3091 cache = optarg;
3092 break;
Max Reitz40055952014-07-22 22:58:42 +02003093 case 'T':
3094 src_cache = optarg;
3095 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003096 case 'q':
3097 quiet = true;
3098 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003099 case OPTION_OBJECT: {
3100 QemuOpts *opts;
3101 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3102 optarg, true);
3103 if (!opts) {
3104 return 1;
3105 }
3106 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003107 case OPTION_IMAGE_OPTS:
3108 image_opts = true;
3109 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003110 case 'U':
3111 force_share = true;
3112 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003113 }
3114 }
3115
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003116 if (quiet) {
3117 progress = 0;
3118 }
3119
Fam Zhengac1307a2014-04-22 13:36:11 +08003120 if (optind != argc - 1) {
3121 error_exit("Expecting one image file name");
3122 }
3123 if (!unsafe && !out_baseimg) {
3124 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003125 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003126 filename = argv[optind++];
3127
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003128 if (qemu_opts_foreach(&qemu_object_opts,
3129 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003130 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003131 return 1;
3132 }
3133
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003134 qemu_progress_init(progress, 2.0);
3135 qemu_progress_print(0, 100);
3136
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003137 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003138 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003139 if (ret < 0) {
3140 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003141 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003142 }
3143
Kevin Wolfce099542016-03-15 13:01:04 +01003144 src_flags = 0;
3145 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003146 if (ret < 0) {
3147 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003148 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003149 }
3150
Kevin Wolfce099542016-03-15 13:01:04 +01003151 /* The source files are opened read-only, don't care about WCE */
3152 assert((src_flags & BDRV_O_RDWR) == 0);
3153 (void) src_writethrough;
3154
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003155 /*
3156 * Open the images.
3157 *
3158 * Ignore the old backing file for unsafe rebase in case we want to correct
3159 * the reference to a renamed or moved backing file.
3160 */
Fam Zheng335e9932017-05-03 00:35:39 +08003161 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3162 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003163 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003164 ret = -1;
3165 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003166 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003167 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003168
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003169 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003170 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003171 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003172 ret = -1;
3173 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003174 }
3175 }
3176
3177 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003178 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003179 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003180 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003181
Max Reitz644483d2015-02-05 13:58:17 -05003182 if (bs->backing_format[0] != '\0') {
3183 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003184 qdict_put_str(options, "driver", bs->backing_format);
Max Reitz644483d2015-02-05 13:58:17 -05003185 }
3186
Fam Zheng335e9932017-05-03 00:35:39 +08003187 if (force_share) {
3188 if (!options) {
3189 options = qdict_new();
3190 }
Eric Blake579cf1d2017-05-15 14:54:39 -05003191 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +08003192 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003193 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003194 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003195 options, src_flags, &local_err);
3196 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003197 error_reportf_err(local_err,
3198 "Could not open old backing file '%s': ",
3199 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003200 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003201 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003202 }
Max Reitz644483d2015-02-05 13:58:17 -05003203
Alex Bligha6166732012-10-16 13:46:18 +01003204 if (out_baseimg[0]) {
Fam Zheng335e9932017-05-03 00:35:39 +08003205 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003206 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003207 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003208 }
3209 if (force_share) {
3210 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003211 }
3212
Max Reitzefaa7c42016-03-16 19:54:38 +01003213 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003214 options, src_flags, &local_err);
3215 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003216 error_reportf_err(local_err,
3217 "Could not open new backing file '%s': ",
3218 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003219 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003220 goto out;
3221 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003222 }
3223 }
3224
3225 /*
3226 * Check each unallocated cluster in the COW file. If it is unallocated,
3227 * accesses go to the backing file. We must therefore compare this cluster
3228 * in the old and new backing file, and if they differ we need to copy it
3229 * from the old backing file into the COW file.
3230 *
3231 * If qemu-img crashes during this step, no harm is done. The content of
3232 * the image is the same as the original one at any time.
3233 */
3234 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003235 int64_t num_sectors;
3236 int64_t old_backing_num_sectors;
3237 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003238 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003239 int n;
Eric Blaked6a644b2017-07-07 07:44:57 -05003240 int64_t count;
Kevin Wolf1f710492012-10-12 14:29:18 +02003241 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003242
Max Reitzf1d3cd72015-02-05 13:58:18 -05003243 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3244 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003245
Max Reitzf1d3cd72015-02-05 13:58:18 -05003246 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003247 if (num_sectors < 0) {
3248 error_report("Could not get size of '%s': %s",
3249 filename, strerror(-num_sectors));
3250 ret = -1;
3251 goto out;
3252 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003253 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003254 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003255 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003256
3257 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3258 error_report("Could not get size of '%s': %s",
3259 backing_name, strerror(-old_backing_num_sectors));
3260 ret = -1;
3261 goto out;
3262 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003263 if (blk_new_backing) {
3264 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003265 if (new_backing_num_sectors < 0) {
3266 error_report("Could not get size of '%s': %s",
3267 out_baseimg, strerror(-new_backing_num_sectors));
3268 ret = -1;
3269 goto out;
3270 }
Alex Bligha6166732012-10-16 13:46:18 +01003271 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003272
Kevin Wolf1f710492012-10-12 14:29:18 +02003273 if (num_sectors != 0) {
3274 local_progress = (float)100 /
3275 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
3276 }
3277
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003278 for (sector = 0; sector < num_sectors; sector += n) {
3279
3280 /* How many sectors can we handle with the next read? */
3281 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
3282 n = (IO_BUF_SIZE / 512);
3283 } else {
3284 n = num_sectors - sector;
3285 }
3286
3287 /* If the cluster is allocated, we don't need to take action */
Eric Blaked6a644b2017-07-07 07:44:57 -05003288 ret = bdrv_is_allocated(bs, sector << BDRV_SECTOR_BITS,
3289 n << BDRV_SECTOR_BITS, &count);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003290 if (ret < 0) {
3291 error_report("error while reading image metadata: %s",
3292 strerror(-ret));
3293 goto out;
3294 }
Eric Blaked6a644b2017-07-07 07:44:57 -05003295 /* TODO relax this once bdrv_is_allocated does not enforce
3296 * sector alignment */
3297 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
3298 n = count >> BDRV_SECTOR_BITS;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003299 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003300 continue;
3301 }
3302
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003303 /*
3304 * Read old and new backing file and take into consideration that
3305 * backing files may be smaller than the COW image.
3306 */
3307 if (sector >= old_backing_num_sectors) {
3308 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3309 } else {
3310 if (sector + n > old_backing_num_sectors) {
3311 n = old_backing_num_sectors - sector;
3312 }
3313
Eric Blake91669202016-05-06 10:26:43 -06003314 ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS,
3315 buf_old, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003316 if (ret < 0) {
3317 error_report("error while reading from old backing file");
3318 goto out;
3319 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003320 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003321
Max Reitzf1d3cd72015-02-05 13:58:18 -05003322 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003323 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3324 } else {
3325 if (sector + n > new_backing_num_sectors) {
3326 n = new_backing_num_sectors - sector;
3327 }
3328
Eric Blake91669202016-05-06 10:26:43 -06003329 ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS,
3330 buf_new, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003331 if (ret < 0) {
3332 error_report("error while reading from new backing file");
3333 goto out;
3334 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003335 }
3336
3337 /* If they differ, we need to write to the COW file */
3338 uint64_t written = 0;
3339
3340 while (written < n) {
3341 int pnum;
3342
3343 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003344 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003345 {
Eric Blake91669202016-05-06 10:26:43 -06003346 ret = blk_pwrite(blk,
3347 (sector + written) << BDRV_SECTOR_BITS,
3348 buf_old + written * 512,
3349 pnum << BDRV_SECTOR_BITS, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003350 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003351 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003352 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003353 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003354 }
3355 }
3356
3357 written += pnum;
3358 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003359 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003360 }
3361 }
3362
3363 /*
3364 * Change the backing file. All clusters that are different from the old
3365 * backing file are overwritten in the COW file now, so the visible content
3366 * doesn't change when we switch the backing file.
3367 */
Alex Bligha6166732012-10-16 13:46:18 +01003368 if (out_baseimg && *out_baseimg) {
3369 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3370 } else {
3371 ret = bdrv_change_backing_file(bs, NULL, NULL);
3372 }
3373
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003374 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003375 error_report("Could not change the backing file to '%s': No "
3376 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003377 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003378 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003379 out_baseimg, strerror(-ret));
3380 }
3381
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003382 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003383 /*
3384 * TODO At this point it is possible to check if any clusters that are
3385 * allocated in the COW file are the same in the backing file. If so, they
3386 * could be dropped from the COW file. Don't do this before switching the
3387 * backing file, in case of a crash this would lead to corruption.
3388 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003389out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003390 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003391 /* Cleanup */
3392 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003393 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003394 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003395 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003396 qemu_vfree(buf_old);
3397 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003398
Markus Armbruster26f54e92014-10-07 13:59:04 +02003399 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003400 if (ret) {
3401 return 1;
3402 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003403 return 0;
3404}
3405
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003406static int img_resize(int argc, char **argv)
3407{
Markus Armbruster6750e792015-02-12 17:43:08 +01003408 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003409 int c, ret, relative;
3410 const char *filename, *fmt, *size;
3411 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003412 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003413 BlockBackend *blk = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003414 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003415
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003416 static QemuOptsList resize_options = {
3417 .name = "resize_options",
3418 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3419 .desc = {
3420 {
3421 .name = BLOCK_OPT_SIZE,
3422 .type = QEMU_OPT_SIZE,
3423 .help = "Virtual disk size"
3424 }, {
3425 /* end of list */
3426 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003427 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003428 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003429 bool image_opts = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003430
Kevin Wolfe80fec72011-04-29 10:58:12 +02003431 /* Remove size from argv manually so that negative numbers are not treated
3432 * as options by getopt. */
3433 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003434 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003435 return 1;
3436 }
3437
3438 size = argv[--argc];
3439
3440 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003441 fmt = NULL;
3442 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003443 static const struct option long_options[] = {
3444 {"help", no_argument, 0, 'h'},
3445 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003446 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003447 {0, 0, 0, 0}
3448 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003449 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003450 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003451 if (c == -1) {
3452 break;
3453 }
3454 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003455 case ':':
3456 missing_argument(argv[optind - 1]);
3457 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003458 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003459 unrecognized_option(argv[optind - 1]);
3460 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003461 case 'h':
3462 help();
3463 break;
3464 case 'f':
3465 fmt = optarg;
3466 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003467 case 'q':
3468 quiet = true;
3469 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003470 case OPTION_OBJECT: {
3471 QemuOpts *opts;
3472 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3473 optarg, true);
3474 if (!opts) {
3475 return 1;
3476 }
3477 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003478 case OPTION_IMAGE_OPTS:
3479 image_opts = true;
3480 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003481 }
3482 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003483 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003484 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003485 }
3486 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003487
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003488 if (qemu_opts_foreach(&qemu_object_opts,
3489 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003490 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003491 return 1;
3492 }
3493
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003494 /* Choose grow, shrink, or absolute resize mode */
3495 switch (size[0]) {
3496 case '+':
3497 relative = 1;
3498 size++;
3499 break;
3500 case '-':
3501 relative = -1;
3502 size++;
3503 break;
3504 default:
3505 relative = 0;
3506 break;
3507 }
3508
3509 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003510 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003511 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003512 if (err) {
3513 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003514 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003515 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003516 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003517 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003518 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3519 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003520
Max Reitzefaa7c42016-03-16 19:54:38 +01003521 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003522 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3523 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003524 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003525 ret = -1;
3526 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003527 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003528
3529 if (relative) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003530 total_size = blk_getlength(blk) + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003531 } else {
3532 total_size = n;
3533 }
3534 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003535 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003536 ret = -1;
3537 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003538 }
3539
Max Reitzed3d2ec2017-03-28 22:51:27 +02003540 ret = blk_truncate(blk, total_size, &err);
3541 if (!ret) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003542 qprintf(quiet, "Image resized.\n");
Max Reitzed3d2ec2017-03-28 22:51:27 +02003543 } else {
3544 error_report_err(err);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003545 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003546out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003547 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003548 if (ret) {
3549 return 1;
3550 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003551 return 0;
3552}
3553
Max Reitz76a3a342014-10-27 11:12:51 +01003554static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003555 int64_t offset, int64_t total_work_size,
3556 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003557{
3558 qemu_progress_print(100.f * offset / total_work_size, 0);
3559}
3560
Max Reitz6f176b42013-09-03 10:09:50 +02003561static int img_amend(int argc, char **argv)
3562{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003563 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003564 int c, ret = 0;
3565 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003566 QemuOptsList *create_opts = NULL;
3567 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003568 const char *fmt = NULL, *filename, *cache;
3569 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003570 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003571 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003572 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003573 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003574 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003575
Max Reitzbd39e6e2014-07-22 22:58:43 +02003576 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003577 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003578 static const struct option long_options[] = {
3579 {"help", no_argument, 0, 'h'},
3580 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003581 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003582 {0, 0, 0, 0}
3583 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003584 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003585 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003586 if (c == -1) {
3587 break;
3588 }
3589
3590 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003591 case ':':
3592 missing_argument(argv[optind - 1]);
3593 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003594 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003595 unrecognized_option(argv[optind - 1]);
3596 break;
3597 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003598 help();
3599 break;
3600 case 'o':
3601 if (!is_valid_option_list(optarg)) {
3602 error_report("Invalid option list: %s", optarg);
3603 ret = -1;
3604 goto out_no_progress;
3605 }
3606 if (!options) {
3607 options = g_strdup(optarg);
3608 } else {
3609 char *old_options = options;
3610 options = g_strdup_printf("%s,%s", options, optarg);
3611 g_free(old_options);
3612 }
3613 break;
3614 case 'f':
3615 fmt = optarg;
3616 break;
3617 case 't':
3618 cache = optarg;
3619 break;
3620 case 'p':
3621 progress = true;
3622 break;
3623 case 'q':
3624 quiet = true;
3625 break;
3626 case OPTION_OBJECT:
3627 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3628 optarg, true);
3629 if (!opts) {
3630 ret = -1;
3631 goto out_no_progress;
3632 }
3633 break;
3634 case OPTION_IMAGE_OPTS:
3635 image_opts = true;
3636 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003637 }
3638 }
3639
Max Reitz6f176b42013-09-03 10:09:50 +02003640 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003641 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003642 }
3643
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003644 if (qemu_opts_foreach(&qemu_object_opts,
3645 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003646 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003647 ret = -1;
3648 goto out_no_progress;
3649 }
3650
Max Reitz76a3a342014-10-27 11:12:51 +01003651 if (quiet) {
3652 progress = false;
3653 }
3654 qemu_progress_init(progress, 1.0);
3655
Kevin Wolfa283cb62014-02-21 16:24:07 +01003656 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3657 if (fmt && has_help_option(options)) {
3658 /* If a format is explicitly specified (and possibly no filename is
3659 * given), print option help here */
3660 ret = print_block_option_help(filename, fmt);
3661 goto out;
3662 }
3663
3664 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003665 error_report("Expecting one image file name");
3666 ret = -1;
3667 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003668 }
Max Reitz6f176b42013-09-03 10:09:50 +02003669
Kevin Wolfce099542016-03-15 13:01:04 +01003670 flags = BDRV_O_RDWR;
3671 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003672 if (ret < 0) {
3673 error_report("Invalid cache option: %s", cache);
3674 goto out;
3675 }
3676
Fam Zheng335e9932017-05-03 00:35:39 +08003677 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3678 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003679 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003680 ret = -1;
3681 goto out;
3682 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003683 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003684
3685 fmt = bs->drv->format_name;
3686
Kevin Wolf626f84f2014-02-21 16:24:06 +01003687 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003688 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003689 ret = print_block_option_help(filename, fmt);
3690 goto out;
3691 }
3692
Max Reitzb2439d22014-12-02 18:32:47 +01003693 if (!bs->drv->create_opts) {
3694 error_report("Format driver '%s' does not support any options to amend",
3695 fmt);
3696 ret = -1;
3697 goto out;
3698 }
3699
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003700 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003701 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003702 qemu_opts_do_parse(opts, options, NULL, &err);
3703 if (err) {
3704 error_report_err(err);
3705 ret = -1;
3706 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003707 }
3708
Max Reitz76a3a342014-10-27 11:12:51 +01003709 /* In case the driver does not call amend_status_cb() */
3710 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003711 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003712 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003713 if (ret < 0) {
3714 error_report("Error while amending options: %s", strerror(-ret));
3715 goto out;
3716 }
3717
3718out:
Max Reitz76a3a342014-10-27 11:12:51 +01003719 qemu_progress_end();
3720
Max Reitze814dff2015-08-20 16:00:38 -07003721out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003722 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003723 qemu_opts_del(opts);
3724 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003725 g_free(options);
3726
Max Reitz6f176b42013-09-03 10:09:50 +02003727 if (ret) {
3728 return 1;
3729 }
3730 return 0;
3731}
3732
Kevin Wolfb6133b82014-08-05 14:17:13 +02003733typedef struct BenchData {
3734 BlockBackend *blk;
3735 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003736 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003737 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003738 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003739 int nrreq;
3740 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003741 int flush_interval;
3742 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003743 uint8_t *buf;
3744 QEMUIOVector *qiov;
3745
3746 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003747 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003748 uint64_t offset;
3749} BenchData;
3750
Kevin Wolf55d539c2016-06-03 13:59:41 +02003751static void bench_undrained_flush_cb(void *opaque, int ret)
3752{
3753 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003754 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003755 exit(EXIT_FAILURE);
3756 }
3757}
3758
Kevin Wolfb6133b82014-08-05 14:17:13 +02003759static void bench_cb(void *opaque, int ret)
3760{
3761 BenchData *b = opaque;
3762 BlockAIOCB *acb;
3763
3764 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003765 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003766 exit(EXIT_FAILURE);
3767 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003768
3769 if (b->in_flush) {
3770 /* Just finished a flush with drained queue: Start next requests */
3771 assert(b->in_flight == 0);
3772 b->in_flush = false;
3773 } else if (b->in_flight > 0) {
3774 int remaining = b->n - b->in_flight;
3775
Kevin Wolfb6133b82014-08-05 14:17:13 +02003776 b->n--;
3777 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003778
3779 /* Time for flush? Drain queue if requested, then flush */
3780 if (b->flush_interval && remaining % b->flush_interval == 0) {
3781 if (!b->in_flight || !b->drain_on_flush) {
3782 BlockCompletionFunc *cb;
3783
3784 if (b->drain_on_flush) {
3785 b->in_flush = true;
3786 cb = bench_cb;
3787 } else {
3788 cb = bench_undrained_flush_cb;
3789 }
3790
3791 acb = blk_aio_flush(b->blk, cb, b);
3792 if (!acb) {
3793 error_report("Failed to issue flush request");
3794 exit(EXIT_FAILURE);
3795 }
3796 }
3797 if (b->drain_on_flush) {
3798 return;
3799 }
3800 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003801 }
3802
3803 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003804 int64_t offset = b->offset;
3805 /* blk_aio_* might look for completed I/Os and kick bench_cb
3806 * again, so make sure this operation is counted by in_flight
3807 * and b->offset is ready for the next submission.
3808 */
3809 b->in_flight++;
3810 b->offset += b->step;
3811 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003812 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003813 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003814 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003815 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003816 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003817 if (!acb) {
3818 error_report("Failed to issue request");
3819 exit(EXIT_FAILURE);
3820 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003821 }
3822}
3823
3824static int img_bench(int argc, char **argv)
3825{
3826 int c, ret = 0;
3827 const char *fmt = NULL, *filename;
3828 bool quiet = false;
3829 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003830 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003831 int count = 75000;
3832 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003833 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003834 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003835 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003836 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003837 int flush_interval = 0;
3838 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003839 int64_t image_size;
3840 BlockBackend *blk = NULL;
3841 BenchData data = {};
3842 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003843 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003844 struct timeval t1, t2;
3845 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08003846 bool force_share = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003847
3848 for (;;) {
3849 static const struct option long_options[] = {
3850 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003851 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003852 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003853 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003854 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08003855 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003856 {0, 0, 0, 0}
3857 };
Fam Zheng335e9932017-05-03 00:35:39 +08003858 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003859 if (c == -1) {
3860 break;
3861 }
3862
3863 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003864 case ':':
3865 missing_argument(argv[optind - 1]);
3866 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003867 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003868 unrecognized_option(argv[optind - 1]);
3869 break;
3870 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02003871 help();
3872 break;
3873 case 'c':
3874 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003875 unsigned long res;
3876
3877 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003878 error_report("Invalid request count specified");
3879 return 1;
3880 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003881 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003882 break;
3883 }
3884 case 'd':
3885 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003886 unsigned long res;
3887
3888 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003889 error_report("Invalid queue depth specified");
3890 return 1;
3891 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003892 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003893 break;
3894 }
3895 case 'f':
3896 fmt = optarg;
3897 break;
3898 case 'n':
3899 flags |= BDRV_O_NATIVE_AIO;
3900 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003901 case 'o':
3902 {
Markus Armbruster606caa02017-02-21 21:14:04 +01003903 offset = cvtnum(optarg);
3904 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02003905 error_report("Invalid offset specified");
3906 return 1;
3907 }
3908 break;
3909 }
3910 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003911 case 'q':
3912 quiet = true;
3913 break;
3914 case 's':
3915 {
3916 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003917
Markus Armbruster606caa02017-02-21 21:14:04 +01003918 sval = cvtnum(optarg);
3919 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003920 error_report("Invalid buffer size specified");
3921 return 1;
3922 }
3923
3924 bufsize = sval;
3925 break;
3926 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003927 case 'S':
3928 {
3929 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003930
Markus Armbruster606caa02017-02-21 21:14:04 +01003931 sval = cvtnum(optarg);
3932 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02003933 error_report("Invalid step size specified");
3934 return 1;
3935 }
3936
3937 step = sval;
3938 break;
3939 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003940 case 't':
3941 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3942 if (ret < 0) {
3943 error_report("Invalid cache mode");
3944 ret = -1;
3945 goto out;
3946 }
3947 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003948 case 'w':
3949 flags |= BDRV_O_RDWR;
3950 is_write = true;
3951 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003952 case 'U':
3953 force_share = true;
3954 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003955 case OPTION_PATTERN:
3956 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003957 unsigned long res;
3958
3959 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003960 error_report("Invalid pattern byte specified");
3961 return 1;
3962 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003963 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003964 break;
3965 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003966 case OPTION_FLUSH_INTERVAL:
3967 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003968 unsigned long res;
3969
3970 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02003971 error_report("Invalid flush interval specified");
3972 return 1;
3973 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003974 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003975 break;
3976 }
3977 case OPTION_NO_DRAIN:
3978 drain_on_flush = false;
3979 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003980 case OPTION_IMAGE_OPTS:
3981 image_opts = true;
3982 break;
3983 }
3984 }
3985
3986 if (optind != argc - 1) {
3987 error_exit("Expecting one image file name");
3988 }
3989 filename = argv[argc - 1];
3990
Kevin Wolf55d539c2016-06-03 13:59:41 +02003991 if (!is_write && flush_interval) {
3992 error_report("--flush-interval is only available in write tests");
3993 ret = -1;
3994 goto out;
3995 }
3996 if (flush_interval && flush_interval < depth) {
3997 error_report("Flush interval can't be smaller than depth");
3998 ret = -1;
3999 goto out;
4000 }
4001
Fam Zheng335e9932017-05-03 00:35:39 +08004002 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4003 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004004 if (!blk) {
4005 ret = -1;
4006 goto out;
4007 }
4008
4009 image_size = blk_getlength(blk);
4010 if (image_size < 0) {
4011 ret = image_size;
4012 goto out;
4013 }
4014
4015 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004016 .blk = blk,
4017 .image_size = image_size,
4018 .bufsize = bufsize,
4019 .step = step ?: bufsize,
4020 .nrreq = depth,
4021 .n = count,
4022 .offset = offset,
4023 .write = is_write,
4024 .flush_interval = flush_interval,
4025 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004026 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004027 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004028 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004029 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004030 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004031 if (flush_interval) {
4032 printf("Sending flush every %d requests\n", flush_interval);
4033 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004034
4035 data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004036 memset(data.buf, pattern, data.nrreq * data.bufsize);
4037
Kevin Wolfb6133b82014-08-05 14:17:13 +02004038 data.qiov = g_new(QEMUIOVector, data.nrreq);
4039 for (i = 0; i < data.nrreq; i++) {
4040 qemu_iovec_init(&data.qiov[i], 1);
4041 qemu_iovec_add(&data.qiov[i],
4042 data.buf + i * data.bufsize, data.bufsize);
4043 }
4044
4045 gettimeofday(&t1, NULL);
4046 bench_cb(&data, 0);
4047
4048 while (data.n > 0) {
4049 main_loop_wait(false);
4050 }
4051 gettimeofday(&t2, NULL);
4052
4053 printf("Run completed in %3.3f seconds.\n",
4054 (t2.tv_sec - t1.tv_sec)
4055 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4056
4057out:
4058 qemu_vfree(data.buf);
4059 blk_unref(blk);
4060
4061 if (ret) {
4062 return 1;
4063 }
4064 return 0;
4065}
4066
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004067#define C_BS 01
4068#define C_COUNT 02
4069#define C_IF 04
4070#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004071#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004072
4073struct DdInfo {
4074 unsigned int flags;
4075 int64_t count;
4076};
4077
4078struct DdIo {
4079 int bsz; /* Block size */
4080 char *filename;
4081 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004082 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004083};
4084
4085struct DdOpts {
4086 const char *name;
4087 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4088 unsigned int flag;
4089};
4090
4091static int img_dd_bs(const char *arg,
4092 struct DdIo *in, struct DdIo *out,
4093 struct DdInfo *dd)
4094{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004095 int64_t res;
4096
Markus Armbruster606caa02017-02-21 21:14:04 +01004097 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004098
Markus Armbruster606caa02017-02-21 21:14:04 +01004099 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004100 error_report("invalid number: '%s'", arg);
4101 return 1;
4102 }
4103 in->bsz = out->bsz = res;
4104
4105 return 0;
4106}
4107
4108static int img_dd_count(const char *arg,
4109 struct DdIo *in, struct DdIo *out,
4110 struct DdInfo *dd)
4111{
Markus Armbruster606caa02017-02-21 21:14:04 +01004112 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004113
Markus Armbruster606caa02017-02-21 21:14:04 +01004114 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004115 error_report("invalid number: '%s'", arg);
4116 return 1;
4117 }
4118
4119 return 0;
4120}
4121
4122static int img_dd_if(const char *arg,
4123 struct DdIo *in, struct DdIo *out,
4124 struct DdInfo *dd)
4125{
4126 in->filename = g_strdup(arg);
4127
4128 return 0;
4129}
4130
4131static int img_dd_of(const char *arg,
4132 struct DdIo *in, struct DdIo *out,
4133 struct DdInfo *dd)
4134{
4135 out->filename = g_strdup(arg);
4136
4137 return 0;
4138}
4139
Reda Sallahif7c15532016-08-10 16:16:09 +02004140static int img_dd_skip(const char *arg,
4141 struct DdIo *in, struct DdIo *out,
4142 struct DdInfo *dd)
4143{
Markus Armbruster606caa02017-02-21 21:14:04 +01004144 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004145
Markus Armbruster606caa02017-02-21 21:14:04 +01004146 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004147 error_report("invalid number: '%s'", arg);
4148 return 1;
4149 }
4150
4151 return 0;
4152}
4153
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004154static int img_dd(int argc, char **argv)
4155{
4156 int ret = 0;
4157 char *arg = NULL;
4158 char *tmp;
4159 BlockDriver *drv = NULL, *proto_drv = NULL;
4160 BlockBackend *blk1 = NULL, *blk2 = NULL;
4161 QemuOpts *opts = NULL;
4162 QemuOptsList *create_opts = NULL;
4163 Error *local_err = NULL;
4164 bool image_opts = false;
4165 int c, i;
4166 const char *out_fmt = "raw";
4167 const char *fmt = NULL;
4168 int64_t size = 0;
4169 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004170 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004171 struct DdInfo dd = {
4172 .flags = 0,
4173 .count = 0,
4174 };
4175 struct DdIo in = {
4176 .bsz = 512, /* Block size is by default 512 bytes */
4177 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004178 .buf = NULL,
4179 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004180 };
4181 struct DdIo out = {
4182 .bsz = 512,
4183 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004184 .buf = NULL,
4185 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004186 };
4187
4188 const struct DdOpts options[] = {
4189 { "bs", img_dd_bs, C_BS },
4190 { "count", img_dd_count, C_COUNT },
4191 { "if", img_dd_if, C_IF },
4192 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004193 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004194 { NULL, NULL, 0 }
4195 };
4196 const struct option long_options[] = {
4197 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004198 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004199 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004200 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004201 { 0, 0, 0, 0 }
4202 };
4203
Fam Zheng335e9932017-05-03 00:35:39 +08004204 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004205 if (c == EOF) {
4206 break;
4207 }
4208 switch (c) {
4209 case 'O':
4210 out_fmt = optarg;
4211 break;
4212 case 'f':
4213 fmt = optarg;
4214 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004215 case ':':
4216 missing_argument(argv[optind - 1]);
4217 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004218 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004219 unrecognized_option(argv[optind - 1]);
4220 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004221 case 'h':
4222 help();
4223 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004224 case 'U':
4225 force_share = true;
4226 break;
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004227 case OPTION_OBJECT:
4228 if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004229 ret = -1;
4230 goto out;
4231 }
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004232 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004233 case OPTION_IMAGE_OPTS:
4234 image_opts = true;
4235 break;
4236 }
4237 }
4238
4239 for (i = optind; i < argc; i++) {
4240 int j;
4241 arg = g_strdup(argv[i]);
4242
4243 tmp = strchr(arg, '=');
4244 if (tmp == NULL) {
4245 error_report("unrecognized operand %s", arg);
4246 ret = -1;
4247 goto out;
4248 }
4249
4250 *tmp++ = '\0';
4251
4252 for (j = 0; options[j].name != NULL; j++) {
4253 if (!strcmp(arg, options[j].name)) {
4254 break;
4255 }
4256 }
4257 if (options[j].name == NULL) {
4258 error_report("unrecognized operand %s", arg);
4259 ret = -1;
4260 goto out;
4261 }
4262
4263 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4264 ret = -1;
4265 goto out;
4266 }
4267 dd.flags |= options[j].flag;
4268 g_free(arg);
4269 arg = NULL;
4270 }
4271
4272 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4273 error_report("Must specify both input and output files");
4274 ret = -1;
4275 goto out;
4276 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004277
4278 if (qemu_opts_foreach(&qemu_object_opts,
4279 user_creatable_add_opts_foreach,
4280 NULL, NULL)) {
4281 ret = -1;
4282 goto out;
4283 }
4284
Fam Zheng335e9932017-05-03 00:35:39 +08004285 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4286 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004287
4288 if (!blk1) {
4289 ret = -1;
4290 goto out;
4291 }
4292
4293 drv = bdrv_find_format(out_fmt);
4294 if (!drv) {
4295 error_report("Unknown file format");
4296 ret = -1;
4297 goto out;
4298 }
4299 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4300
4301 if (!proto_drv) {
4302 error_report_err(local_err);
4303 ret = -1;
4304 goto out;
4305 }
4306 if (!drv->create_opts) {
4307 error_report("Format driver '%s' does not support image creation",
4308 drv->format_name);
4309 ret = -1;
4310 goto out;
4311 }
4312 if (!proto_drv->create_opts) {
4313 error_report("Protocol driver '%s' does not support image creation",
4314 proto_drv->format_name);
4315 ret = -1;
4316 goto out;
4317 }
4318 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4319 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4320
4321 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4322
4323 size = blk_getlength(blk1);
4324 if (size < 0) {
4325 error_report("Failed to get size for '%s'", in.filename);
4326 ret = -1;
4327 goto out;
4328 }
4329
4330 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4331 dd.count * in.bsz < size) {
4332 size = dd.count * in.bsz;
4333 }
4334
Reda Sallahif7c15532016-08-10 16:16:09 +02004335 /* Overflow means the specified offset is beyond input image's size */
4336 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4337 size < in.bsz * in.offset)) {
4338 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4339 } else {
4340 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4341 size - in.bsz * in.offset, &error_abort);
4342 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004343
4344 ret = bdrv_create(drv, out.filename, opts, &local_err);
4345 if (ret < 0) {
4346 error_reportf_err(local_err,
4347 "%s: error while creating output image: ",
4348 out.filename);
4349 ret = -1;
4350 goto out;
4351 }
4352
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004353 /* TODO, we can't honour --image-opts for the target,
4354 * since it needs to be given in a format compatible
4355 * with the bdrv_create() call above which does not
4356 * support image-opts style.
4357 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01004358 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004359 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004360
4361 if (!blk2) {
4362 ret = -1;
4363 goto out;
4364 }
4365
Reda Sallahif7c15532016-08-10 16:16:09 +02004366 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4367 size < in.offset * in.bsz)) {
4368 /* We give a warning if the skip option is bigger than the input
4369 * size and create an empty output disk image (i.e. like dd(1)).
4370 */
4371 error_report("%s: cannot skip to specified offset", in.filename);
4372 in_pos = size;
4373 } else {
4374 in_pos = in.offset * in.bsz;
4375 }
4376
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004377 in.buf = g_new(uint8_t, in.bsz);
4378
Reda Sallahif7c15532016-08-10 16:16:09 +02004379 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004380 int in_ret, out_ret;
4381
4382 if (in_pos + in.bsz > size) {
4383 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4384 } else {
4385 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4386 }
4387 if (in_ret < 0) {
4388 error_report("error while reading from input image file: %s",
4389 strerror(-in_ret));
4390 ret = -1;
4391 goto out;
4392 }
4393 in_pos += in_ret;
4394
4395 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4396
4397 if (out_ret < 0) {
4398 error_report("error while writing to output image file: %s",
4399 strerror(-out_ret));
4400 ret = -1;
4401 goto out;
4402 }
4403 out_pos += out_ret;
4404 }
4405
4406out:
4407 g_free(arg);
4408 qemu_opts_del(opts);
4409 qemu_opts_free(create_opts);
4410 blk_unref(blk1);
4411 blk_unref(blk2);
4412 g_free(in.filename);
4413 g_free(out.filename);
4414 g_free(in.buf);
4415 g_free(out.buf);
4416
4417 if (ret) {
4418 return 1;
4419 }
4420 return 0;
4421}
4422
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004423static void dump_json_block_measure_info(BlockMeasureInfo *info)
4424{
4425 QString *str;
4426 QObject *obj;
4427 Visitor *v = qobject_output_visitor_new(&obj);
4428
4429 visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
4430 visit_complete(v, &obj);
4431 str = qobject_to_json_pretty(obj);
4432 assert(str != NULL);
4433 printf("%s\n", qstring_get_str(str));
4434 qobject_decref(obj);
4435 visit_free(v);
4436 QDECREF(str);
4437}
4438
4439static int img_measure(int argc, char **argv)
4440{
4441 static const struct option long_options[] = {
4442 {"help", no_argument, 0, 'h'},
4443 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4444 {"object", required_argument, 0, OPTION_OBJECT},
4445 {"output", required_argument, 0, OPTION_OUTPUT},
4446 {"size", required_argument, 0, OPTION_SIZE},
4447 {"force-share", no_argument, 0, 'U'},
4448 {0, 0, 0, 0}
4449 };
4450 OutputFormat output_format = OFORMAT_HUMAN;
4451 BlockBackend *in_blk = NULL;
4452 BlockDriver *drv;
4453 const char *filename = NULL;
4454 const char *fmt = NULL;
4455 const char *out_fmt = "raw";
4456 char *options = NULL;
4457 char *snapshot_name = NULL;
4458 bool force_share = false;
4459 QemuOpts *opts = NULL;
4460 QemuOpts *object_opts = NULL;
4461 QemuOpts *sn_opts = NULL;
4462 QemuOptsList *create_opts = NULL;
4463 bool image_opts = false;
4464 uint64_t img_size = UINT64_MAX;
4465 BlockMeasureInfo *info = NULL;
4466 Error *local_err = NULL;
4467 int ret = 1;
4468 int c;
4469
4470 while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
4471 long_options, NULL)) != -1) {
4472 switch (c) {
4473 case '?':
4474 case 'h':
4475 help();
4476 break;
4477 case 'f':
4478 fmt = optarg;
4479 break;
4480 case 'O':
4481 out_fmt = optarg;
4482 break;
4483 case 'o':
4484 if (!is_valid_option_list(optarg)) {
4485 error_report("Invalid option list: %s", optarg);
4486 goto out;
4487 }
4488 if (!options) {
4489 options = g_strdup(optarg);
4490 } else {
4491 char *old_options = options;
4492 options = g_strdup_printf("%s,%s", options, optarg);
4493 g_free(old_options);
4494 }
4495 break;
4496 case 'l':
4497 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
4498 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
4499 optarg, false);
4500 if (!sn_opts) {
4501 error_report("Failed in parsing snapshot param '%s'",
4502 optarg);
4503 goto out;
4504 }
4505 } else {
4506 snapshot_name = optarg;
4507 }
4508 break;
4509 case 'U':
4510 force_share = true;
4511 break;
4512 case OPTION_OBJECT:
4513 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
4514 optarg, true);
4515 if (!object_opts) {
4516 goto out;
4517 }
4518 break;
4519 case OPTION_IMAGE_OPTS:
4520 image_opts = true;
4521 break;
4522 case OPTION_OUTPUT:
4523 if (!strcmp(optarg, "json")) {
4524 output_format = OFORMAT_JSON;
4525 } else if (!strcmp(optarg, "human")) {
4526 output_format = OFORMAT_HUMAN;
4527 } else {
4528 error_report("--output must be used with human or json "
4529 "as argument.");
4530 goto out;
4531 }
4532 break;
4533 case OPTION_SIZE:
4534 {
4535 int64_t sval;
4536
4537 sval = cvtnum(optarg);
4538 if (sval < 0) {
4539 if (sval == -ERANGE) {
4540 error_report("Image size must be less than 8 EiB!");
4541 } else {
4542 error_report("Invalid image size specified! You may use "
4543 "k, M, G, T, P or E suffixes for ");
4544 error_report("kilobytes, megabytes, gigabytes, terabytes, "
4545 "petabytes and exabytes.");
4546 }
4547 goto out;
4548 }
4549 img_size = (uint64_t)sval;
4550 }
4551 break;
4552 }
4553 }
4554
4555 if (qemu_opts_foreach(&qemu_object_opts,
4556 user_creatable_add_opts_foreach,
4557 NULL, NULL)) {
4558 goto out;
4559 }
4560
4561 if (argc - optind > 1) {
4562 error_report("At most one filename argument is allowed.");
4563 goto out;
4564 } else if (argc - optind == 1) {
4565 filename = argv[optind];
4566 }
4567
4568 if (!filename &&
4569 (object_opts || image_opts || fmt || snapshot_name || sn_opts)) {
4570 error_report("--object, --image-opts, -f, and -l "
4571 "require a filename argument.");
4572 goto out;
4573 }
4574 if (filename && img_size != UINT64_MAX) {
4575 error_report("--size N cannot be used together with a filename.");
4576 goto out;
4577 }
4578 if (!filename && img_size == UINT64_MAX) {
4579 error_report("Either --size N or one filename must be specified.");
4580 goto out;
4581 }
4582
4583 if (filename) {
4584 in_blk = img_open(image_opts, filename, fmt, 0,
4585 false, false, force_share);
4586 if (!in_blk) {
4587 goto out;
4588 }
4589
4590 if (sn_opts) {
4591 bdrv_snapshot_load_tmp(blk_bs(in_blk),
4592 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
4593 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
4594 &local_err);
4595 } else if (snapshot_name != NULL) {
4596 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
4597 snapshot_name, &local_err);
4598 }
4599 if (local_err) {
4600 error_reportf_err(local_err, "Failed to load snapshot: ");
4601 goto out;
4602 }
4603 }
4604
4605 drv = bdrv_find_format(out_fmt);
4606 if (!drv) {
4607 error_report("Unknown file format '%s'", out_fmt);
4608 goto out;
4609 }
4610 if (!drv->create_opts) {
4611 error_report("Format driver '%s' does not support image creation",
4612 drv->format_name);
4613 goto out;
4614 }
4615
4616 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4617 create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
4618 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4619 if (options) {
4620 qemu_opts_do_parse(opts, options, NULL, &local_err);
4621 if (local_err) {
4622 error_report_err(local_err);
4623 error_report("Invalid options for file format '%s'", out_fmt);
4624 goto out;
4625 }
4626 }
4627 if (img_size != UINT64_MAX) {
4628 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
4629 }
4630
4631 info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
4632 if (local_err) {
4633 error_report_err(local_err);
4634 goto out;
4635 }
4636
4637 if (output_format == OFORMAT_HUMAN) {
4638 printf("required size: %" PRIu64 "\n", info->required);
4639 printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
4640 } else {
4641 dump_json_block_measure_info(info);
4642 }
4643
4644 ret = 0;
4645
4646out:
4647 qapi_free_BlockMeasureInfo(info);
4648 qemu_opts_del(object_opts);
4649 qemu_opts_del(opts);
4650 qemu_opts_del(sn_opts);
4651 qemu_opts_free(create_opts);
4652 g_free(options);
4653 blk_unref(in_blk);
4654 return ret;
4655}
Kevin Wolfb6133b82014-08-05 14:17:13 +02004656
Anthony Liguoric227f092009-10-01 16:12:16 -05004657static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004658#define DEF(option, callback, arg_string) \
4659 { option, callback },
4660#include "qemu-img-cmds.h"
4661#undef DEF
4662#undef GEN_DOCS
4663 { NULL, NULL, },
4664};
4665
bellardea2384d2004-08-01 21:59:26 +00004666int main(int argc, char **argv)
4667{
Anthony Liguoric227f092009-10-01 16:12:16 -05004668 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004669 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004670 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004671 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004672 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004673 static const struct option long_options[] = {
4674 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004675 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004676 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004677 {0, 0, 0, 0}
4678 };
bellardea2384d2004-08-01 21:59:26 +00004679
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004680#ifdef CONFIG_POSIX
4681 signal(SIGPIPE, SIG_IGN);
4682#endif
4683
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004684 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004685 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004686 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004687
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004688 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004689 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004690 exit(EXIT_FAILURE);
4691 }
4692
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004693 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004694
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004695 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004696 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004697 if (argc < 2) {
4698 error_exit("Not enough arguments");
4699 }
Stuart Brady153859b2009-06-07 00:42:17 +01004700
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004701 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004702 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004703 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004704
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004705 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004706 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004707 case ':':
4708 missing_argument(argv[optind - 1]);
4709 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08004710 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004711 unrecognized_option(argv[optind - 1]);
4712 return 0;
4713 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03004714 help();
4715 return 0;
4716 case 'V':
4717 printf(QEMU_IMG_VERSION);
4718 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004719 case 'T':
4720 g_free(trace_file);
4721 trace_file = trace_opt_parse(optarg);
4722 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004723 }
bellardea2384d2004-08-01 21:59:26 +00004724 }
Stuart Brady153859b2009-06-07 00:42:17 +01004725
Denis V. Lunev10985132016-06-17 17:44:13 +03004726 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004727
Denis V. Lunev10985132016-06-17 17:44:13 +03004728 /* reset getopt_long scanning */
4729 argc -= optind;
4730 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004731 return 0;
4732 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004733 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004734 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004735
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004736 if (!trace_init_backends()) {
4737 exit(1);
4738 }
4739 trace_init_file(trace_file);
4740 qemu_set_log(LOG_TRACE);
4741
Denis V. Lunev10985132016-06-17 17:44:13 +03004742 /* find the command */
4743 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4744 if (!strcmp(cmdname, cmd->name)) {
4745 return cmd->handler(argc, argv);
4746 }
4747 }
Jeff Cody7db16892014-04-25 17:02:32 -04004748
Stuart Brady153859b2009-06-07 00:42:17 +01004749 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004750 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004751}