blob: 418f061736ea2cde0232a6d41bfb8b573806e82d [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,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050063};
64
65typedef enum OutputFormat {
66 OFORMAT_JSON,
67 OFORMAT_HUMAN,
68} OutputFormat;
69
Kevin Wolfe6996142016-03-15 13:03:11 +010070/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040071#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000072
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010073static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000074{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010075 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000076}
77
Fam Zhengac1307a2014-04-22 13:36:11 +080078static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
79{
80 va_list ap;
81
82 error_printf("qemu-img: ");
83
84 va_start(ap, fmt);
85 error_vprintf(fmt, ap);
86 va_end(ap);
87
88 error_printf("\nTry 'qemu-img --help' for more information\n");
89 exit(EXIT_FAILURE);
90}
91
Stefan Hajnoczic9192972017-03-17 18:45:41 +080092static void QEMU_NORETURN missing_argument(const char *option)
93{
94 error_exit("missing argument for option '%s'", option);
95}
96
97static void QEMU_NORETURN unrecognized_option(const char *option)
98{
99 error_exit("unrecognized option '%s'", option);
100}
101
blueswir1d2c639d2009-01-24 18:19:25 +0000102/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +0800103static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +0000104{
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100105 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -0400106 QEMU_IMG_VERSION
Denis V. Lunev10985132016-06-17 17:44:13 +0300107 "usage: qemu-img [standard options] command [command options]\n"
malc3f020d72010-02-08 12:04:56 +0300108 "QEMU disk image utility\n"
109 "\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300110 " '-h', '--help' display this help and exit\n"
111 " '-V', '--version' output version information and exit\n"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +0300112 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
113 " specify tracing options\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300114 "\n"
malc3f020d72010-02-08 12:04:56 +0300115 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100116#define DEF(option, callback, arg_string) \
117 " " arg_string "\n"
118#include "qemu-img-cmds.h"
119#undef DEF
120#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +0300121 "\n"
122 "Command parameters:\n"
123 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000124 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
125 " manual page for a description of the object properties. The most common\n"
126 " object type is a 'secret', which is used to supply passwords and/or\n"
127 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300128 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400129 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800130 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
131 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100132 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
133 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300134 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200135 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
136 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
137 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300138 " 'output_filename' is the destination disk image filename\n"
139 " 'output_fmt' is the destination format\n"
140 " 'options' is a comma separated list of format specific options in a\n"
141 " name=value format. Use -o ? for an overview of the options supported by the\n"
142 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800143 " 'snapshot_param' is param used for internal snapshot, format\n"
144 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
145 " '[ID_OR_NAME]'\n"
146 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
147 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300148 " '-c' indicates that target image must be compressed (qcow format only)\n"
149 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
150 " match exactly. The image doesn't need a working backing file before\n"
151 " rebasing in this case (useful for renaming the backing file)\n"
152 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200153 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100154 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200155 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
156 " contain only zeros for qemu-img to create a sparse image during\n"
157 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
158 " unallocated or zero sectors, and the destination image will always be\n"
159 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200160 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100161 " '-n' skips the target volume creation (useful if the volume is created\n"
162 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300163 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200164 "Parameters to check subcommand:\n"
165 " '-r' tries to repair any inconsistencies that are found during the check.\n"
166 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
167 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200168 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200169 "\n"
Peter Lieven2d9187b2017-02-28 13:40:07 +0100170 "Parameters to convert subcommand:\n"
171 " '-m' specifies how many coroutines work in parallel during the convert\n"
172 " process (defaults to 8)\n"
173 " '-W' allow to write to the target out of order rather than sequential\n"
174 "\n"
malc3f020d72010-02-08 12:04:56 +0300175 "Parameters to snapshot subcommand:\n"
176 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
177 " '-a' applies a snapshot (revert disk to saved state)\n"
178 " '-c' creates a snapshot\n"
179 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100180 " '-l' lists all snapshots in the given image\n"
181 "\n"
182 "Parameters to compare subcommand:\n"
183 " '-f' first image format\n"
184 " '-F' second image format\n"
Reda Sallahi86ce1f62016-08-10 04:43:12 +0200185 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
186 "\n"
187 "Parameters to dd subcommand:\n"
188 " 'bs=BYTES' read and write up to BYTES bytes at a time "
189 "(default: 512)\n"
190 " 'count=N' copy only N input blocks\n"
191 " 'if=FILE' read from FILE\n"
Reda Sallahif7c15532016-08-10 16:16:09 +0200192 " 'of=FILE' write to FILE\n"
193 " 'skip=N' skip N bs-sized blocks at the start of input\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100194
195 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100196 bdrv_iterate_format(format_print, NULL);
bellardea2384d2004-08-01 21:59:26 +0000197 printf("\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800198 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000199}
200
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000201static QemuOptsList qemu_object_opts = {
202 .name = "object",
203 .implied_opt_name = "qom-type",
204 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
205 .desc = {
206 { }
207 },
208};
209
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000210static QemuOptsList qemu_source_opts = {
211 .name = "source",
212 .implied_opt_name = "file",
213 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
214 .desc = {
215 { }
216 },
217};
218
Stefan Weil7c30f652013-06-16 17:01:05 +0200219static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100220{
221 int ret = 0;
222 if (!quiet) {
223 va_list args;
224 va_start(args, fmt);
225 ret = vprintf(fmt, args);
226 va_end(args);
227 }
228 return ret;
229}
230
bellardea2384d2004-08-01 21:59:26 +0000231
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100232static int print_block_option_help(const char *filename, const char *fmt)
233{
234 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800235 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500236 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100237
238 /* Find driver and parse its options */
239 drv = bdrv_find_format(fmt);
240 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100241 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100242 return 1;
243 }
244
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800245 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100246 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500247 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100248 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100249 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800250 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100251 return 1;
252 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800253 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100254 }
255
Chunyan Liu83d05212014-06-05 17:20:51 +0800256 qemu_opts_print_help(create_opts);
257 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100258 return 0;
259}
260
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000261
262static int img_open_password(BlockBackend *blk, const char *filename,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000263 int flags, bool quiet)
bellard75c23802004-08-27 21:28:58 +0000264{
265 BlockDriverState *bs;
bellard75c23802004-08-27 21:28:58 +0000266 char password[256];
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000267
268 bs = blk_bs(blk);
Daniel P. Berrange4ef130f2016-03-21 14:11:43 +0000269 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs) &&
270 !(flags & BDRV_O_NO_IO)) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000271 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
272 if (qemu_read_password(password, sizeof(password)) < 0) {
273 error_report("No password given");
274 return -1;
275 }
276 if (bdrv_set_key(bs, password) < 0) {
277 error_report("invalid password");
278 return -1;
279 }
280 }
281 return 0;
282}
283
284
Max Reitzefaa7c42016-03-16 19:54:38 +0100285static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100286 QemuOpts *opts, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800287 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000288{
289 QDict *options;
290 Error *local_err = NULL;
291 BlockBackend *blk;
292 options = qemu_opts_to_qdict(opts, NULL);
Fam Zheng335e9932017-05-03 00:35:39 +0800293 if (force_share) {
294 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
295 && !qdict_get_bool(options, BDRV_OPT_FORCE_SHARE)) {
296 error_report("--force-share/-U conflicts with image options");
297 return NULL;
298 }
299 qdict_put(options, BDRV_OPT_FORCE_SHARE, qbool_from_bool(true));
300 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100301 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000302 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100303 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000304 return NULL;
305 }
Kevin Wolfce099542016-03-15 13:01:04 +0100306 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000307
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000308 if (img_open_password(blk, optstr, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000309 blk_unref(blk);
310 return NULL;
311 }
312 return blk;
313}
314
Max Reitzefaa7c42016-03-16 19:54:38 +0100315static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000316 const char *fmt, int flags,
Fam Zheng335e9932017-05-03 00:35:39 +0800317 bool writethrough, bool quiet,
318 bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000319{
320 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200321 Error *local_err = NULL;
Fam Zheng335e9932017-05-03 00:35:39 +0800322 QDict *options = qdict_new();
Kevin Wolfad717132010-12-16 15:37:41 +0100323
bellard75c23802004-08-27 21:28:58 +0000324 if (fmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500325 qdict_put_str(options, "driver", fmt);
bellard75c23802004-08-27 21:28:58 +0000326 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100327
Fam Zheng335e9932017-05-03 00:35:39 +0800328 if (force_share) {
329 qdict_put(options, BDRV_OPT_FORCE_SHARE, qbool_from_bool(true));
330 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100331 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500332 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100333 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000334 return NULL;
bellard75c23802004-08-27 21:28:58 +0000335 }
Kevin Wolfce099542016-03-15 13:01:04 +0100336 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100337
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000338 if (img_open_password(blk, filename, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000339 blk_unref(blk);
340 return NULL;
bellard75c23802004-08-27 21:28:58 +0000341 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200342 return blk;
bellard75c23802004-08-27 21:28:58 +0000343}
344
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000345
Max Reitzefaa7c42016-03-16 19:54:38 +0100346static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000347 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100348 const char *fmt, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800349 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000350{
351 BlockBackend *blk;
352 if (image_opts) {
353 QemuOpts *opts;
354 if (fmt) {
355 error_report("--image-opts and --format are mutually exclusive");
356 return NULL;
357 }
358 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
359 filename, true);
360 if (!opts) {
361 return NULL;
362 }
Fam Zheng335e9932017-05-03 00:35:39 +0800363 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
364 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000365 } else {
Fam Zheng335e9932017-05-03 00:35:39 +0800366 blk = img_open_file(filename, fmt, flags, writethrough, quiet,
367 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000368 }
369 return blk;
370}
371
372
Chunyan Liu83d05212014-06-05 17:20:51 +0800373static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100374 const char *base_filename,
375 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200376{
Markus Armbruster6750e792015-02-12 17:43:08 +0100377 Error *err = NULL;
378
Kevin Wolfefa84d42009-05-18 16:42:12 +0200379 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100380 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100381 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100382 error_report("Backing file not supported for file format '%s'",
383 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100384 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900385 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200386 }
387 }
388 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100389 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100390 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100391 error_report("Backing file format not supported for file "
392 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100393 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900394 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200395 }
396 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900397 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200398}
399
Markus Armbruster606caa02017-02-21 21:14:04 +0100400static int64_t cvtnum(const char *s)
401{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100402 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100403 uint64_t value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100404
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100405 err = qemu_strtosz(s, NULL, &value);
406 if (err < 0) {
407 return err;
408 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100409 if (value > INT64_MAX) {
410 return -ERANGE;
411 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100412 return value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100413}
414
bellardea2384d2004-08-01 21:59:26 +0000415static int img_create(int argc, char **argv)
416{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200417 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100418 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000419 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000420 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000421 const char *filename;
422 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200423 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200424 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100425 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000426
bellardea2384d2004-08-01 21:59:26 +0000427 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000428 static const struct option long_options[] = {
429 {"help", no_argument, 0, 'h'},
430 {"object", required_argument, 0, OPTION_OBJECT},
431 {0, 0, 0, 0}
432 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800433 c = getopt_long(argc, argv, ":F:b:f:he6o:q",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000434 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100435 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000436 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100437 }
bellardea2384d2004-08-01 21:59:26 +0000438 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800439 case ':':
440 missing_argument(argv[optind - 1]);
441 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100442 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800443 unrecognized_option(argv[optind - 1]);
444 break;
bellardea2384d2004-08-01 21:59:26 +0000445 case 'h':
446 help();
447 break;
aliguori9230eaf2009-03-28 17:55:19 +0000448 case 'F':
449 base_fmt = optarg;
450 break;
bellardea2384d2004-08-01 21:59:26 +0000451 case 'b':
452 base_filename = optarg;
453 break;
454 case 'f':
455 fmt = optarg;
456 break;
457 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200458 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100459 "encryption\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100460 goto fail;
thsd8871c52007-10-24 16:11:42 +0000461 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200462 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100463 "compat6\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100464 goto fail;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200465 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100466 if (!is_valid_option_list(optarg)) {
467 error_report("Invalid option list: %s", optarg);
468 goto fail;
469 }
470 if (!options) {
471 options = g_strdup(optarg);
472 } else {
473 char *old_options = options;
474 options = g_strdup_printf("%s,%s", options, optarg);
475 g_free(old_options);
476 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200477 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100478 case 'q':
479 quiet = true;
480 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000481 case OPTION_OBJECT: {
482 QemuOpts *opts;
483 opts = qemu_opts_parse_noisily(&qemu_object_opts,
484 optarg, true);
485 if (!opts) {
486 goto fail;
487 }
488 } break;
bellardea2384d2004-08-01 21:59:26 +0000489 }
490 }
aliguori9230eaf2009-03-28 17:55:19 +0000491
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900492 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100493 filename = (optind < argc) ? argv[optind] : NULL;
494 if (options && has_help_option(options)) {
495 g_free(options);
496 return print_block_option_help(filename, fmt);
497 }
498
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100499 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800500 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100501 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100502 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900503
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000504 if (qemu_opts_foreach(&qemu_object_opts,
505 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200506 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000507 goto fail;
508 }
509
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100510 /* Get image size, if specified */
511 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100512 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100513
514 sval = cvtnum(argv[optind++]);
515 if (sval < 0) {
liguang79443392012-12-17 09:49:23 +0800516 if (sval == -ERANGE) {
517 error_report("Image size must be less than 8 EiB!");
518 } else {
519 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200520 "G, T, P or E suffixes for ");
521 error_report("kilobytes, megabytes, gigabytes, terabytes, "
522 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800523 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100524 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100525 }
526 img_size = (uint64_t)sval;
527 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200528 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800529 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200530 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100531
Luiz Capitulino9b375252012-11-30 10:52:05 -0200532 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Fam Zheng92172832017-04-21 20:27:01 +0800533 options, img_size, 0, quiet, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100534 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100535 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100536 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900537 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200538
Kevin Wolf77386bf2014-02-21 16:24:04 +0100539 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000540 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100541
542fail:
543 g_free(options);
544 return 1;
bellardea2384d2004-08-01 21:59:26 +0000545}
546
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100547static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500548{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500549 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500550 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100551 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600552
553 visit_type_ImageCheck(v, NULL, &check, &error_abort);
554 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500555 str = qobject_to_json_pretty(obj);
556 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100557 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500558 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600559 visit_free(v);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500560 QDECREF(str);
561}
562
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100563static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500564{
565 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100566 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500567 } else {
568 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100569 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
570 "Data may be corrupted, or further writes to the image "
571 "may corrupt it.\n",
572 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500573 }
574
575 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100576 qprintf(quiet,
577 "\n%" PRId64 " leaked clusters were found on the image.\n"
578 "This means waste of disk space, but no harm to data.\n",
579 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500580 }
581
582 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100583 qprintf(quiet,
584 "\n%" PRId64
585 " internal errors have occurred during the check.\n",
586 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500587 }
588 }
589
590 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100591 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
592 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
593 check->allocated_clusters, check->total_clusters,
594 check->allocated_clusters * 100.0 / check->total_clusters,
595 check->fragmented_clusters * 100.0 / check->allocated_clusters,
596 check->compressed_clusters * 100.0 /
597 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500598 }
599
600 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100601 qprintf(quiet,
602 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500603 }
604}
605
606static int collect_image_check(BlockDriverState *bs,
607 ImageCheck *check,
608 const char *filename,
609 const char *fmt,
610 int fix)
611{
612 int ret;
613 BdrvCheckResult result;
614
615 ret = bdrv_check(bs, &result, fix);
616 if (ret < 0) {
617 return ret;
618 }
619
620 check->filename = g_strdup(filename);
621 check->format = g_strdup(bdrv_get_format_name(bs));
622 check->check_errors = result.check_errors;
623 check->corruptions = result.corruptions;
624 check->has_corruptions = result.corruptions != 0;
625 check->leaks = result.leaks;
626 check->has_leaks = result.leaks != 0;
627 check->corruptions_fixed = result.corruptions_fixed;
628 check->has_corruptions_fixed = result.corruptions != 0;
629 check->leaks_fixed = result.leaks_fixed;
630 check->has_leaks_fixed = result.leaks != 0;
631 check->image_end_offset = result.image_end_offset;
632 check->has_image_end_offset = result.image_end_offset != 0;
633 check->total_clusters = result.bfi.total_clusters;
634 check->has_total_clusters = result.bfi.total_clusters != 0;
635 check->allocated_clusters = result.bfi.allocated_clusters;
636 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
637 check->fragmented_clusters = result.bfi.fragmented_clusters;
638 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100639 check->compressed_clusters = result.bfi.compressed_clusters;
640 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500641
642 return 0;
643}
644
Kevin Wolfe076f332010-06-29 11:43:13 +0200645/*
646 * Checks an image for consistency. Exit codes:
647 *
Max Reitzd6635c42014-06-02 22:15:21 +0200648 * 0 - Check completed, image is good
649 * 1 - Check not completed because of internal errors
650 * 2 - Check completed, image is corrupted
651 * 3 - Check completed, image has leaked clusters, but is good otherwise
652 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200653 */
aliguori15859692009-04-21 23:11:53 +0000654static int img_check(int argc, char **argv)
655{
656 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500657 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200658 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200659 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000660 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200661 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100662 int flags = BDRV_O_CHECK;
663 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200664 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100665 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000666 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +0800667 bool force_share = false;
aliguori15859692009-04-21 23:11:53 +0000668
669 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500670 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200671 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100672
aliguori15859692009-04-21 23:11:53 +0000673 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500674 int option_index = 0;
675 static const struct option long_options[] = {
676 {"help", no_argument, 0, 'h'},
677 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530678 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500679 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000680 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000681 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +0800682 {"force-share", no_argument, 0, 'U'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500683 {0, 0, 0, 0}
684 };
Fam Zheng335e9932017-05-03 00:35:39 +0800685 c = getopt_long(argc, argv, ":hf:r:T:qU",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500686 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100687 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000688 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100689 }
aliguori15859692009-04-21 23:11:53 +0000690 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800691 case ':':
692 missing_argument(argv[optind - 1]);
693 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100694 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800695 unrecognized_option(argv[optind - 1]);
696 break;
aliguori15859692009-04-21 23:11:53 +0000697 case 'h':
698 help();
699 break;
700 case 'f':
701 fmt = optarg;
702 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200703 case 'r':
704 flags |= BDRV_O_RDWR;
705
706 if (!strcmp(optarg, "leaks")) {
707 fix = BDRV_FIX_LEAKS;
708 } else if (!strcmp(optarg, "all")) {
709 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
710 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800711 error_exit("Unknown option value for -r "
712 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200713 }
714 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500715 case OPTION_OUTPUT:
716 output = optarg;
717 break;
Max Reitz40055952014-07-22 22:58:42 +0200718 case 'T':
719 cache = optarg;
720 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100721 case 'q':
722 quiet = true;
723 break;
Fam Zheng335e9932017-05-03 00:35:39 +0800724 case 'U':
725 force_share = true;
726 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000727 case OPTION_OBJECT: {
728 QemuOpts *opts;
729 opts = qemu_opts_parse_noisily(&qemu_object_opts,
730 optarg, true);
731 if (!opts) {
732 return 1;
733 }
734 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000735 case OPTION_IMAGE_OPTS:
736 image_opts = true;
737 break;
aliguori15859692009-04-21 23:11:53 +0000738 }
739 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200740 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800741 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100742 }
aliguori15859692009-04-21 23:11:53 +0000743 filename = argv[optind++];
744
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500745 if (output && !strcmp(output, "json")) {
746 output_format = OFORMAT_JSON;
747 } else if (output && !strcmp(output, "human")) {
748 output_format = OFORMAT_HUMAN;
749 } else if (output) {
750 error_report("--output must be used with human or json as argument.");
751 return 1;
752 }
753
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000754 if (qemu_opts_foreach(&qemu_object_opts,
755 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200756 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000757 return 1;
758 }
759
Kevin Wolfce099542016-03-15 13:01:04 +0100760 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200761 if (ret < 0) {
762 error_report("Invalid source cache option: %s", cache);
763 return 1;
764 }
765
Fam Zheng335e9932017-05-03 00:35:39 +0800766 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
767 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200768 if (!blk) {
769 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900770 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200771 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500772
773 check = g_new0(ImageCheck, 1);
774 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200775
776 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200777 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200778 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500779 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200780 }
781
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500782 if (check->corruptions_fixed || check->leaks_fixed) {
783 int corruptions_fixed, leaks_fixed;
784
785 leaks_fixed = check->leaks_fixed;
786 corruptions_fixed = check->corruptions_fixed;
787
788 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100789 qprintf(quiet,
790 "The following inconsistencies were found and repaired:\n\n"
791 " %" PRId64 " leaked clusters\n"
792 " %" PRId64 " corruptions\n\n"
793 "Double checking the fixed image now...\n",
794 check->leaks_fixed,
795 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500796 }
797
798 ret = collect_image_check(bs, check, filename, fmt, 0);
799
800 check->leaks_fixed = leaks_fixed;
801 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200802 }
803
Max Reitz832390a2014-10-23 15:29:12 +0200804 if (!ret) {
805 switch (output_format) {
806 case OFORMAT_HUMAN:
807 dump_human_image_check(check, quiet);
808 break;
809 case OFORMAT_JSON:
810 dump_json_image_check(check, quiet);
811 break;
812 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500813 }
814
815 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200816 if (ret) {
817 error_report("Check failed: %s", strerror(-ret));
818 } else {
819 error_report("Check failed");
820 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500821 ret = 1;
822 goto fail;
823 }
824
825 if (check->corruptions) {
826 ret = 2;
827 } else if (check->leaks) {
828 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200829 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500830 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000831 }
832
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500833fail:
834 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200835 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500836 return ret;
aliguori15859692009-04-21 23:11:53 +0000837}
838
Max Reitzd4a32382014-10-24 15:57:37 +0200839typedef struct CommonBlockJobCBInfo {
840 BlockDriverState *bs;
841 Error **errp;
842} CommonBlockJobCBInfo;
843
844static void common_block_job_cb(void *opaque, int ret)
845{
846 CommonBlockJobCBInfo *cbi = opaque;
847
848 if (ret < 0) {
849 error_setg_errno(cbi->errp, -ret, "Block job failed");
850 }
Max Reitzd4a32382014-10-24 15:57:37 +0200851}
852
853static void run_block_job(BlockJob *job, Error **errp)
854{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200855 AioContext *aio_context = blk_get_aio_context(job->blk);
Max Reitzd4a32382014-10-24 15:57:37 +0200856
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100857 /* FIXME In error cases, the job simply goes away and we access a dangling
858 * pointer below. */
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200859 aio_context_acquire(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +0200860 do {
861 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500862 qemu_progress_print(job->len ?
863 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200864 } while (!job->ready);
865
866 block_job_complete_sync(job, errp);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200867 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200868
869 /* A block job may finish instantaneously without publishing any progress,
870 * so just signal completion here */
871 qemu_progress_print(100.f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200872}
873
bellardea2384d2004-08-01 21:59:26 +0000874static int img_commit(int argc, char **argv)
875{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400876 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200877 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200878 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200879 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100880 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200881 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100882 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200883 Error *local_err = NULL;
884 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000885 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200886 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000887
888 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400889 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200890 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000891 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000892 static const struct option long_options[] = {
893 {"help", no_argument, 0, 'h'},
894 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000895 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000896 {0, 0, 0, 0}
897 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800898 c = getopt_long(argc, argv, ":f:ht:b:dpq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000899 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100900 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000901 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100902 }
bellardea2384d2004-08-01 21:59:26 +0000903 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800904 case ':':
905 missing_argument(argv[optind - 1]);
906 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100907 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800908 unrecognized_option(argv[optind - 1]);
909 break;
bellardea2384d2004-08-01 21:59:26 +0000910 case 'h':
911 help();
912 break;
913 case 'f':
914 fmt = optarg;
915 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400916 case 't':
917 cache = optarg;
918 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200919 case 'b':
920 base = optarg;
921 /* -b implies -d */
922 drop = true;
923 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200924 case 'd':
925 drop = true;
926 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200927 case 'p':
928 progress = true;
929 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100930 case 'q':
931 quiet = true;
932 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000933 case OPTION_OBJECT: {
934 QemuOpts *opts;
935 opts = qemu_opts_parse_noisily(&qemu_object_opts,
936 optarg, true);
937 if (!opts) {
938 return 1;
939 }
940 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000941 case OPTION_IMAGE_OPTS:
942 image_opts = true;
943 break;
bellardea2384d2004-08-01 21:59:26 +0000944 }
945 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200946
947 /* Progress is not shown in Quiet mode */
948 if (quiet) {
949 progress = false;
950 }
951
Kevin Wolffc11eb22013-08-05 10:53:04 +0200952 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800953 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100954 }
bellardea2384d2004-08-01 21:59:26 +0000955 filename = argv[optind++];
956
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000957 if (qemu_opts_foreach(&qemu_object_opts,
958 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200959 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000960 return 1;
961 }
962
Max Reitz9a86fe42014-10-24 15:57:38 +0200963 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100964 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400965 if (ret < 0) {
966 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100967 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400968 }
969
Fam Zheng335e9932017-05-03 00:35:39 +0800970 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
971 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200972 if (!blk) {
973 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900974 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200975 bs = blk_bs(blk);
976
Max Reitz687fa1d2014-10-24 15:57:39 +0200977 qemu_progress_init(progress, 1.f);
978 qemu_progress_print(0.f, 100);
979
Max Reitz1b22bff2014-10-24 15:57:40 +0200980 if (base) {
981 base_bs = bdrv_find_backing_image(bs, base);
982 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +0100983 error_setg(&local_err,
984 "Did not find '%s' in the backing chain of '%s'",
985 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +0200986 goto done;
987 }
988 } else {
989 /* This is different from QMP, which by default uses the deepest file in
990 * the backing chain (i.e., the very base); however, the traditional
991 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +0200992 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +0200993 if (!base_bs) {
994 error_setg(&local_err, "Image does not have a backing file");
995 goto done;
996 }
bellardea2384d2004-08-01 21:59:26 +0000997 }
998
Max Reitzd4a32382014-10-24 15:57:37 +0200999 cbi = (CommonBlockJobCBInfo){
1000 .errp = &local_err,
1001 .bs = bs,
1002 };
1003
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001004 aio_context = bdrv_get_aio_context(bs);
1005 aio_context_acquire(aio_context);
John Snow47970df2016-10-27 12:06:57 -04001006 commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
Kevin Wolf0db832f2017-02-20 18:10:05 +01001007 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +08001008 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001009 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +02001010 if (local_err) {
1011 goto done;
1012 }
1013
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001014 /* When the block job completes, the BlockBackend reference will point to
1015 * the old backing file. In order to avoid that the top image is already
1016 * deleted, so we can still empty it afterwards, increment the reference
1017 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +02001018 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001019 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001020 }
1021
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001022 job = block_job_get("commit");
1023 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001024 if (local_err) {
1025 goto unref_backing;
1026 }
1027
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001028 if (!drop && bs->drv->bdrv_make_empty) {
1029 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001030 if (ret) {
1031 error_setg_errno(&local_err, -ret, "Could not empty %s",
1032 filename);
1033 goto unref_backing;
1034 }
1035 }
1036
1037unref_backing:
1038 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001039 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001040 }
Max Reitzd4a32382014-10-24 15:57:37 +02001041
1042done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001043 qemu_progress_end();
1044
Markus Armbruster26f54e92014-10-07 13:59:04 +02001045 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001046
1047 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001048 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001049 return 1;
1050 }
Max Reitzd4a32382014-10-24 15:57:37 +02001051
1052 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001053 return 0;
1054}
1055
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001056/*
thsf58c7b32008-06-05 21:53:49 +00001057 * Returns true iff the first sector pointed to by 'buf' contains at least
1058 * a non-NUL byte.
1059 *
1060 * 'pnum' is set to the number of sectors (including and immediately following
1061 * the first one) that are known to be in the same allocated/unallocated state.
1062 */
bellardea2384d2004-08-01 21:59:26 +00001063static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1064{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001065 bool is_zero;
1066 int i;
bellardea2384d2004-08-01 21:59:26 +00001067
1068 if (n <= 0) {
1069 *pnum = 0;
1070 return 0;
1071 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001072 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001073 for(i = 1; i < n; i++) {
1074 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001075 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001076 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001077 }
bellardea2384d2004-08-01 21:59:26 +00001078 }
1079 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001080 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001081}
1082
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001083/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001084 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1085 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1086 * breaking up write requests for only small sparse areas.
1087 */
1088static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1089 int min)
1090{
1091 int ret;
1092 int num_checked, num_used;
1093
1094 if (n < min) {
1095 min = n;
1096 }
1097
1098 ret = is_allocated_sectors(buf, n, pnum);
1099 if (!ret) {
1100 return ret;
1101 }
1102
1103 num_used = *pnum;
1104 buf += BDRV_SECTOR_SIZE * *pnum;
1105 n -= *pnum;
1106 num_checked = num_used;
1107
1108 while (n > 0) {
1109 ret = is_allocated_sectors(buf, n, pnum);
1110
1111 buf += BDRV_SECTOR_SIZE * *pnum;
1112 n -= *pnum;
1113 num_checked += *pnum;
1114 if (ret) {
1115 num_used = num_checked;
1116 } else if (*pnum >= min) {
1117 break;
1118 }
1119 }
1120
1121 *pnum = num_used;
1122 return 1;
1123}
1124
1125/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001126 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1127 * buffers matches, non-zero otherwise.
1128 *
1129 * pnum is set to the number of sectors (including and immediately following
1130 * the first one) that are known to have the same comparison result
1131 */
1132static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1133 int *pnum)
1134{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001135 bool res;
1136 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001137
1138 if (n <= 0) {
1139 *pnum = 0;
1140 return 0;
1141 }
1142
1143 res = !!memcmp(buf1, buf2, 512);
1144 for(i = 1; i < n; i++) {
1145 buf1 += 512;
1146 buf2 += 512;
1147
1148 if (!!memcmp(buf1, buf2, 512) != res) {
1149 break;
1150 }
1151 }
1152
1153 *pnum = i;
1154 return res;
1155}
1156
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001157#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001158
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001159static int64_t sectors_to_bytes(int64_t sectors)
1160{
1161 return sectors << BDRV_SECTOR_BITS;
1162}
1163
1164static int64_t sectors_to_process(int64_t total, int64_t from)
1165{
1166 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1167}
1168
1169/*
1170 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1171 *
1172 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1173 * data and negative value on error.
1174 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001175 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001176 * @param sect_num: Number of first sector to check
1177 * @param sect_count: Number of sectors to check
1178 * @param filename: Name of disk file we are checking (logging purpose)
1179 * @param buffer: Allocated buffer for storing read data
1180 * @param quiet: Flag for quiet mode
1181 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001182static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001183 int sect_count, const char *filename,
1184 uint8_t *buffer, bool quiet)
1185{
1186 int pnum, ret = 0;
Eric Blake91669202016-05-06 10:26:43 -06001187 ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer,
1188 sect_count << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001189 if (ret < 0) {
1190 error_report("Error while reading offset %" PRId64 " of %s: %s",
1191 sectors_to_bytes(sect_num), filename, strerror(-ret));
1192 return ret;
1193 }
1194 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1195 if (ret || pnum != sect_count) {
1196 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1197 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1198 return 1;
1199 }
1200
1201 return 0;
1202}
1203
1204/*
1205 * Compares two images. Exit codes:
1206 *
1207 * 0 - Images are identical
1208 * 1 - Images differ
1209 * >1 - Error occurred
1210 */
1211static int img_compare(int argc, char **argv)
1212{
Max Reitz40055952014-07-22 22:58:42 +02001213 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001214 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001215 BlockDriverState *bs1, *bs2;
1216 int64_t total_sectors1, total_sectors2;
1217 uint8_t *buf1 = NULL, *buf2 = NULL;
1218 int pnum1, pnum2;
1219 int allocated1, allocated2;
1220 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1221 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001222 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001223 bool writethrough;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001224 int64_t total_sectors;
1225 int64_t sector_num = 0;
1226 int64_t nb_sectors;
1227 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001228 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001229 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001230 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001231
Max Reitz40055952014-07-22 22:58:42 +02001232 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001233 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001234 static const struct option long_options[] = {
1235 {"help", no_argument, 0, 'h'},
1236 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001237 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001238 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001239 {0, 0, 0, 0}
1240 };
Fam Zheng335e9932017-05-03 00:35:39 +08001241 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001242 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001243 if (c == -1) {
1244 break;
1245 }
1246 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001247 case ':':
1248 missing_argument(argv[optind - 1]);
1249 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001250 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001251 unrecognized_option(argv[optind - 1]);
1252 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001253 case 'h':
1254 help();
1255 break;
1256 case 'f':
1257 fmt1 = optarg;
1258 break;
1259 case 'F':
1260 fmt2 = optarg;
1261 break;
Max Reitz40055952014-07-22 22:58:42 +02001262 case 'T':
1263 cache = optarg;
1264 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001265 case 'p':
1266 progress = true;
1267 break;
1268 case 'q':
1269 quiet = true;
1270 break;
1271 case 's':
1272 strict = true;
1273 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001274 case 'U':
1275 force_share = true;
1276 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001277 case OPTION_OBJECT: {
1278 QemuOpts *opts;
1279 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1280 optarg, true);
1281 if (!opts) {
1282 ret = 2;
1283 goto out4;
1284 }
1285 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001286 case OPTION_IMAGE_OPTS:
1287 image_opts = true;
1288 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001289 }
1290 }
1291
1292 /* Progress is not shown in Quiet mode */
1293 if (quiet) {
1294 progress = false;
1295 }
1296
1297
Kevin Wolffc11eb22013-08-05 10:53:04 +02001298 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001299 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001300 }
1301 filename1 = argv[optind++];
1302 filename2 = argv[optind++];
1303
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001304 if (qemu_opts_foreach(&qemu_object_opts,
1305 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001306 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001307 ret = 2;
1308 goto out4;
1309 }
1310
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001311 /* Initialize before goto out */
1312 qemu_progress_init(progress, 2.0);
1313
Kevin Wolfce099542016-03-15 13:01:04 +01001314 flags = 0;
1315 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001316 if (ret < 0) {
1317 error_report("Invalid source cache option: %s", cache);
1318 ret = 2;
1319 goto out3;
1320 }
1321
Fam Zheng335e9932017-05-03 00:35:39 +08001322 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1323 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001324 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001325 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001326 goto out3;
1327 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001328
Fam Zheng335e9932017-05-03 00:35:39 +08001329 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1330 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001331 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001332 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001333 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001334 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001335 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001336 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001337
Max Reitzf1d3cd72015-02-05 13:58:18 -05001338 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1339 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1340 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001341 if (total_sectors1 < 0) {
1342 error_report("Can't get size of %s: %s",
1343 filename1, strerror(-total_sectors1));
1344 ret = 4;
1345 goto out;
1346 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001347 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001348 if (total_sectors2 < 0) {
1349 error_report("Can't get size of %s: %s",
1350 filename2, strerror(-total_sectors2));
1351 ret = 4;
1352 goto out;
1353 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001354 total_sectors = MIN(total_sectors1, total_sectors2);
1355 progress_base = MAX(total_sectors1, total_sectors2);
1356
1357 qemu_progress_print(0, 100);
1358
1359 if (strict && total_sectors1 != total_sectors2) {
1360 ret = 1;
1361 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1362 goto out;
1363 }
1364
1365 for (;;) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001366 int64_t status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001367 BlockDriverState *file;
1368
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001369 nb_sectors = sectors_to_process(total_sectors, sector_num);
1370 if (nb_sectors <= 0) {
1371 break;
1372 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001373 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1374 total_sectors1 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001375 &pnum1, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001376 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001377 ret = 3;
1378 error_report("Sector allocation test failed for %s", filename1);
1379 goto out;
1380 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001381 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001382
Fam Zheng25ad8e62016-01-13 16:37:41 +08001383 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1384 total_sectors2 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001385 &pnum2, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001386 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001387 ret = 3;
1388 error_report("Sector allocation test failed for %s", filename2);
1389 goto out;
1390 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001391 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1392 if (pnum1) {
1393 nb_sectors = MIN(nb_sectors, pnum1);
1394 }
1395 if (pnum2) {
1396 nb_sectors = MIN(nb_sectors, pnum2);
1397 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001398
Fam Zheng25ad8e62016-01-13 16:37:41 +08001399 if (strict) {
1400 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1401 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1402 ret = 1;
1403 qprintf(quiet, "Strict mode: Offset %" PRId64
1404 " block status mismatch!\n",
1405 sectors_to_bytes(sector_num));
1406 goto out;
1407 }
1408 }
1409 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1410 nb_sectors = MIN(pnum1, pnum2);
1411 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001412 if (allocated1) {
Eric Blake91669202016-05-06 10:26:43 -06001413 ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1,
1414 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001415 if (ret < 0) {
1416 error_report("Error while reading offset %" PRId64 " of %s:"
1417 " %s", sectors_to_bytes(sector_num), filename1,
1418 strerror(-ret));
1419 ret = 4;
1420 goto out;
1421 }
Eric Blake91669202016-05-06 10:26:43 -06001422 ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2,
1423 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001424 if (ret < 0) {
1425 error_report("Error while reading offset %" PRId64
1426 " of %s: %s", sectors_to_bytes(sector_num),
1427 filename2, strerror(-ret));
1428 ret = 4;
1429 goto out;
1430 }
1431 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1432 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001433 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1434 sectors_to_bytes(
1435 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001436 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001437 goto out;
1438 }
1439 }
1440 } else {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001441
1442 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001443 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001444 filename1, buf1, quiet);
1445 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001446 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001447 filename2, buf1, quiet);
1448 }
1449 if (ret) {
1450 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001451 error_report("Error while reading offset %" PRId64 ": %s",
1452 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001453 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001454 }
1455 goto out;
1456 }
1457 }
1458 sector_num += nb_sectors;
1459 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1460 }
1461
1462 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001463 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001464 int64_t total_sectors_over;
1465 const char *filename_over;
1466
1467 qprintf(quiet, "Warning: Image size mismatch!\n");
1468 if (total_sectors1 > total_sectors2) {
1469 total_sectors_over = total_sectors1;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001470 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001471 filename_over = filename1;
1472 } else {
1473 total_sectors_over = total_sectors2;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001474 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001475 filename_over = filename2;
1476 }
1477
1478 for (;;) {
1479 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1480 if (nb_sectors <= 0) {
1481 break;
1482 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001483 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001484 nb_sectors, &pnum);
1485 if (ret < 0) {
1486 ret = 3;
1487 error_report("Sector allocation test failed for %s",
1488 filename_over);
1489 goto out;
1490
1491 }
1492 nb_sectors = pnum;
1493 if (ret) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001494 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001495 filename_over, buf1, quiet);
1496 if (ret) {
1497 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001498 error_report("Error while reading offset %" PRId64
1499 " of %s: %s", sectors_to_bytes(sector_num),
1500 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001501 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001502 }
1503 goto out;
1504 }
1505 }
1506 sector_num += nb_sectors;
1507 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1508 }
1509 }
1510
1511 qprintf(quiet, "Images are identical.\n");
1512 ret = 0;
1513
1514out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001515 qemu_vfree(buf1);
1516 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001517 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001518out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001519 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001520out3:
1521 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001522out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001523 return ret;
1524}
1525
Kevin Wolf690c7302015-03-19 13:33:32 +01001526enum ImgConvertBlockStatus {
1527 BLK_DATA,
1528 BLK_ZERO,
1529 BLK_BACKING_FILE,
1530};
1531
Peter Lieven2d9187b2017-02-28 13:40:07 +01001532#define MAX_COROUTINES 16
1533
Kevin Wolf690c7302015-03-19 13:33:32 +01001534typedef struct ImgConvertState {
1535 BlockBackend **src;
1536 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001537 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001538 int64_t total_sectors;
1539 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001540 int64_t allocated_done;
1541 int64_t sector_num;
1542 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001543 enum ImgConvertBlockStatus status;
1544 int64_t sector_next_status;
1545 BlockBackend *target;
1546 bool has_zero_init;
1547 bool compressed;
1548 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001549 bool wr_in_order;
Kevin Wolf690c7302015-03-19 13:33:32 +01001550 int min_sparse;
1551 size_t cluster_sectors;
1552 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001553 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001554 int running_coroutines;
1555 Coroutine *co[MAX_COROUTINES];
1556 int64_t wait_sector_num[MAX_COROUTINES];
1557 CoMutex lock;
1558 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001559} ImgConvertState;
1560
Peter Lieven2d9187b2017-02-28 13:40:07 +01001561static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1562 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001563{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001564 *src_cur = 0;
1565 *src_cur_offset = 0;
1566 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1567 *src_cur_offset += s->src_sectors[*src_cur];
1568 (*src_cur)++;
1569 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001570 }
1571}
1572
1573static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1574{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001575 int64_t ret, src_cur_offset;
1576 int n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001577
Peter Lieven2d9187b2017-02-28 13:40:07 +01001578 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001579
1580 assert(s->total_sectors > sector_num);
1581 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1582
1583 if (s->sector_next_status <= sector_num) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08001584 BlockDriverState *file;
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001585 if (s->target_has_backing) {
1586 ret = bdrv_get_block_status(blk_bs(s->src[src_cur]),
1587 sector_num - src_cur_offset,
1588 n, &n, &file);
1589 } else {
1590 ret = bdrv_get_block_status_above(blk_bs(s->src[src_cur]), NULL,
1591 sector_num - src_cur_offset,
1592 n, &n, &file);
1593 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001594 if (ret < 0) {
1595 return ret;
1596 }
1597
1598 if (ret & BDRV_BLOCK_ZERO) {
1599 s->status = BLK_ZERO;
1600 } else if (ret & BDRV_BLOCK_DATA) {
1601 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001602 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001603 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001604 }
1605
1606 s->sector_next_status = sector_num + n;
1607 }
1608
1609 n = MIN(n, s->sector_next_status - sector_num);
1610 if (s->status == BLK_DATA) {
1611 n = MIN(n, s->buf_sectors);
1612 }
1613
1614 /* We need to write complete clusters for compressed images, so if an
1615 * unallocated area is shorter than that, we must consider the whole
1616 * cluster allocated. */
1617 if (s->compressed) {
1618 if (n < s->cluster_sectors) {
1619 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1620 s->status = BLK_DATA;
1621 } else {
1622 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1623 }
1624 }
1625
1626 return n;
1627}
1628
Peter Lieven2d9187b2017-02-28 13:40:07 +01001629static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1630 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001631{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001632 int n, ret;
1633 QEMUIOVector qiov;
1634 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001635
Kevin Wolf690c7302015-03-19 13:33:32 +01001636 assert(nb_sectors <= s->buf_sectors);
1637 while (nb_sectors > 0) {
1638 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001639 int src_cur;
1640 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001641
1642 /* In the case of compression with multiple source files, we can get a
1643 * nb_sectors that spreads into the next part. So we must be able to
1644 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001645 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1646 blk = s->src[src_cur];
1647 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001648
Peter Lieven2d9187b2017-02-28 13:40:07 +01001649 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1650 iov.iov_base = buf;
1651 iov.iov_len = n << BDRV_SECTOR_BITS;
1652 qemu_iovec_init_external(&qiov, &iov, 1);
1653
1654 ret = blk_co_preadv(
1655 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1656 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001657 if (ret < 0) {
1658 return ret;
1659 }
1660
1661 sector_num += n;
1662 nb_sectors -= n;
1663 buf += n * BDRV_SECTOR_SIZE;
1664 }
1665
1666 return 0;
1667}
1668
Peter Lieven2d9187b2017-02-28 13:40:07 +01001669
1670static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1671 int nb_sectors, uint8_t *buf,
1672 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001673{
1674 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001675 QEMUIOVector qiov;
1676 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001677
1678 while (nb_sectors > 0) {
1679 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001680 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1681
Peter Lieven2d9187b2017-02-28 13:40:07 +01001682 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001683 case BLK_BACKING_FILE:
1684 /* If we have a backing file, leave clusters unallocated that are
1685 * unallocated in the source image, so that the backing file is
1686 * visible at the respective offset. */
1687 assert(s->target_has_backing);
1688 break;
1689
1690 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001691 /* If we're told to keep the target fully allocated (-S 0) or there
1692 * is real non-zero data, we must write it. Otherwise we can treat
1693 * it as zero sectors.
1694 * Compressed clusters need to be written as a whole, so in that
1695 * case we can only save the write if the buffer is completely
1696 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001697 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001698 (!s->compressed &&
1699 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1700 (s->compressed &&
1701 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001702 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001703 iov.iov_base = buf;
1704 iov.iov_len = n << BDRV_SECTOR_BITS;
1705 qemu_iovec_init_external(&qiov, &iov, 1);
1706
1707 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
Lidong Chendb933fb2017-04-27 10:58:27 +08001708 n << BDRV_SECTOR_BITS, &qiov, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001709 if (ret < 0) {
1710 return ret;
1711 }
1712 break;
1713 }
1714 /* fall-through */
1715
1716 case BLK_ZERO:
1717 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001718 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001719 break;
1720 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001721 ret = blk_co_pwrite_zeroes(s->target,
1722 sector_num << BDRV_SECTOR_BITS,
1723 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001724 if (ret < 0) {
1725 return ret;
1726 }
1727 break;
1728 }
1729
1730 sector_num += n;
1731 nb_sectors -= n;
1732 buf += n * BDRV_SECTOR_SIZE;
1733 }
1734
1735 return 0;
1736}
1737
Peter Lieven2d9187b2017-02-28 13:40:07 +01001738static void coroutine_fn convert_co_do_copy(void *opaque)
1739{
1740 ImgConvertState *s = opaque;
1741 uint8_t *buf = NULL;
1742 int ret, i;
1743 int index = -1;
1744
1745 for (i = 0; i < s->num_coroutines; i++) {
1746 if (s->co[i] == qemu_coroutine_self()) {
1747 index = i;
1748 break;
1749 }
1750 }
1751 assert(index >= 0);
1752
1753 s->running_coroutines++;
1754 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1755
1756 while (1) {
1757 int n;
1758 int64_t sector_num;
1759 enum ImgConvertBlockStatus status;
1760
1761 qemu_co_mutex_lock(&s->lock);
1762 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1763 qemu_co_mutex_unlock(&s->lock);
1764 goto out;
1765 }
1766 n = convert_iteration_sectors(s, s->sector_num);
1767 if (n < 0) {
1768 qemu_co_mutex_unlock(&s->lock);
1769 s->ret = n;
1770 goto out;
1771 }
1772 /* save current sector and allocation status to local variables */
1773 sector_num = s->sector_num;
1774 status = s->status;
1775 if (!s->min_sparse && s->status == BLK_ZERO) {
1776 n = MIN(n, s->buf_sectors);
1777 }
1778 /* increment global sector counter so that other coroutines can
1779 * already continue reading beyond this request */
1780 s->sector_num += n;
1781 qemu_co_mutex_unlock(&s->lock);
1782
1783 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1784 s->allocated_done += n;
1785 qemu_progress_print(100.0 * s->allocated_done /
1786 s->allocated_sectors, 0);
1787 }
1788
1789 if (status == BLK_DATA) {
1790 ret = convert_co_read(s, sector_num, n, buf);
1791 if (ret < 0) {
1792 error_report("error while reading sector %" PRId64
1793 ": %s", sector_num, strerror(-ret));
1794 s->ret = ret;
1795 goto out;
1796 }
1797 } else if (!s->min_sparse && status == BLK_ZERO) {
1798 status = BLK_DATA;
1799 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1800 }
1801
1802 if (s->wr_in_order) {
1803 /* keep writes in order */
1804 while (s->wr_offs != sector_num) {
1805 if (s->ret != -EINPROGRESS) {
1806 goto out;
1807 }
1808 s->wait_sector_num[index] = sector_num;
1809 qemu_coroutine_yield();
1810 }
1811 s->wait_sector_num[index] = -1;
1812 }
1813
1814 ret = convert_co_write(s, sector_num, n, buf, status);
1815 if (ret < 0) {
1816 error_report("error while writing sector %" PRId64
1817 ": %s", sector_num, strerror(-ret));
1818 s->ret = ret;
1819 goto out;
1820 }
1821
1822 if (s->wr_in_order) {
1823 /* reenter the coroutine that might have waited
1824 * for this write to complete */
1825 s->wr_offs = sector_num + n;
1826 for (i = 0; i < s->num_coroutines; i++) {
1827 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1828 /*
1829 * A -> B -> A cannot occur because A has
1830 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1831 * B will never enter A during this time window.
1832 */
1833 qemu_coroutine_enter(s->co[i]);
1834 break;
1835 }
1836 }
1837 }
1838 }
1839
1840out:
1841 qemu_vfree(buf);
1842 s->co[index] = NULL;
1843 s->running_coroutines--;
1844 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1845 /* the convert job finished successfully */
1846 s->ret = 0;
1847 }
1848}
1849
Kevin Wolf690c7302015-03-19 13:33:32 +01001850static int convert_do_copy(ImgConvertState *s)
1851{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001852 int ret, i, n;
1853 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001854
1855 /* Check whether we have zero initialisation or can get it efficiently */
1856 s->has_zero_init = s->min_sparse && !s->target_has_backing
1857 ? bdrv_has_zero_init(blk_bs(s->target))
1858 : false;
1859
1860 if (!s->has_zero_init && !s->target_has_backing &&
1861 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1862 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001863 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001864 if (ret == 0) {
1865 s->has_zero_init = true;
1866 }
1867 }
1868
1869 /* Allocate buffer for copied data. For compressed images, only one cluster
1870 * can be copied at a time. */
1871 if (s->compressed) {
1872 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1873 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001874 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001875 }
1876 s->buf_sectors = s->cluster_sectors;
1877 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001878
Kevin Wolf690c7302015-03-19 13:33:32 +01001879 while (sector_num < s->total_sectors) {
1880 n = convert_iteration_sectors(s, sector_num);
1881 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001882 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001883 }
Max Reitzaad15de2016-03-24 23:33:57 +01001884 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1885 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001886 s->allocated_sectors += n;
1887 }
1888 sector_num += n;
1889 }
1890
1891 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001892 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001893 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001894
Peter Lieven2d9187b2017-02-28 13:40:07 +01001895 qemu_co_mutex_init(&s->lock);
1896 for (i = 0; i < s->num_coroutines; i++) {
1897 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1898 s->wait_sector_num[i] = -1;
1899 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001900 }
1901
Peter Lieven2d9187b2017-02-28 13:40:07 +01001902 while (s->ret == -EINPROGRESS) {
1903 main_loop_wait(false);
1904 }
1905
1906 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001907 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001908 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001909 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001910 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001911 }
1912 }
1913
Peter Lieven2d9187b2017-02-28 13:40:07 +01001914 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001915}
1916
bellardea2384d2004-08-01 21:59:26 +00001917static int img_convert(int argc, char **argv)
1918{
Peter Lieven9fd77f92017-04-21 11:11:55 +02001919 int c, bs_i, flags, src_flags = 0;
1920 const char *fmt = NULL, *out_fmt = "raw", *cache = "unsafe",
1921 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1922 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001923 BlockDriver *drv, *proto_drv;
bellardfaea38e2006-08-05 21:31:00 +00001924 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001925 BlockDriverState *out_bs;
1926 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08001927 QemuOptsList *create_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001928 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02001929 Error *local_err = NULL;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001930 bool writethrough, src_writethrough, quiet = false, image_opts = false,
1931 skip_create = false, progress = false;
1932 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08001933 bool force_share = false;
bellardea2384d2004-08-01 21:59:26 +00001934
Peter Lieven9fd77f92017-04-21 11:11:55 +02001935 ImgConvertState s = (ImgConvertState) {
1936 /* Need at least 4k of zeros for sparse detection */
1937 .min_sparse = 8,
1938 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1939 .wr_in_order = true,
1940 .num_coroutines = 8,
1941 };
1942
bellardea2384d2004-08-01 21:59:26 +00001943 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001944 static const struct option long_options[] = {
1945 {"help", no_argument, 0, 'h'},
1946 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001947 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001948 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001949 {0, 0, 0, 0}
1950 };
Fam Zheng335e9932017-05-03 00:35:39 +08001951 c = getopt_long(argc, argv, ":hf:O:B:ce6o:s:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001952 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001953 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001954 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001955 }
bellardea2384d2004-08-01 21:59:26 +00001956 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001957 case ':':
1958 missing_argument(argv[optind - 1]);
1959 break;
Jes Sorensenef873942010-12-06 15:25:40 +01001960 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001961 unrecognized_option(argv[optind - 1]);
1962 break;
bellardea2384d2004-08-01 21:59:26 +00001963 case 'h':
1964 help();
1965 break;
1966 case 'f':
1967 fmt = optarg;
1968 break;
1969 case 'O':
1970 out_fmt = optarg;
1971 break;
thsf58c7b32008-06-05 21:53:49 +00001972 case 'B':
1973 out_baseimg = optarg;
1974 break;
bellardea2384d2004-08-01 21:59:26 +00001975 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02001976 s.compressed = true;
bellardea2384d2004-08-01 21:59:26 +00001977 break;
1978 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001979 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001980 "encryption\' instead!");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001981 goto fail_getopt;
thsec36ba12007-09-16 21:59:02 +00001982 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001983 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001984 "compat6\' instead!");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001985 goto fail_getopt;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001986 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001987 if (!is_valid_option_list(optarg)) {
1988 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001989 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001990 }
1991 if (!options) {
1992 options = g_strdup(optarg);
1993 } else {
1994 char *old_options = options;
1995 options = g_strdup_printf("%s,%s", options, optarg);
1996 g_free(old_options);
1997 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001998 break;
edison51ef6722010-09-21 19:58:41 -07001999 case 's':
2000 snapshot_name = optarg;
2001 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08002002 case 'l':
2003 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01002004 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2005 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08002006 if (!sn_opts) {
2007 error_report("Failed in parsing snapshot param '%s'",
2008 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002009 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002010 }
2011 } else {
2012 snapshot_name = optarg;
2013 }
2014 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002015 case 'S':
2016 {
2017 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002018
2019 sval = cvtnum(optarg);
2020 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02002021 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002022 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002023 }
2024
Peter Lieven9fd77f92017-04-21 11:11:55 +02002025 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002026 break;
2027 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002028 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002029 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002030 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002031 case 't':
2032 cache = optarg;
2033 break;
Max Reitz40055952014-07-22 22:58:42 +02002034 case 'T':
2035 src_cache = optarg;
2036 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002037 case 'q':
2038 quiet = true;
2039 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002040 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002041 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002042 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002043 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002044 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2045 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002046 error_report("Invalid number of coroutines. Allowed number of"
2047 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002048 goto fail_getopt;
2049 }
2050 break;
2051 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002052 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002053 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002054 case 'U':
2055 force_share = true;
2056 break;
Max Reitz3258b912017-04-26 15:46:47 +02002057 case OPTION_OBJECT: {
2058 QemuOpts *object_opts;
2059 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2060 optarg, true);
2061 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002062 goto fail_getopt;
2063 }
2064 break;
Max Reitz3258b912017-04-26 15:46:47 +02002065 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002066 case OPTION_IMAGE_OPTS:
2067 image_opts = true;
2068 break;
bellardea2384d2004-08-01 21:59:26 +00002069 }
2070 }
ths3b46e622007-09-17 08:09:54 +00002071
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002072 if (qemu_opts_foreach(&qemu_object_opts,
2073 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002074 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002075 goto fail_getopt;
2076 }
2077
Peter Lieven9fd77f92017-04-21 11:11:55 +02002078 if (!s.wr_in_order && s.compressed) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002079 error_report("Out of order write and compress are mutually exclusive");
Peter Lieven9fd77f92017-04-21 11:11:55 +02002080 goto fail_getopt;
2081 }
2082
2083 s.src_num = argc - optind - 1;
2084 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2085
2086 if (options && has_help_option(options)) {
2087 ret = print_block_option_help(out_filename, out_fmt);
2088 goto fail_getopt;
2089 }
2090
2091 if (s.src_num < 1) {
2092 error_report("Must specify image file name");
2093 goto fail_getopt;
2094 }
2095
2096
Peter Lieven9fd77f92017-04-21 11:11:55 +02002097 /* ret is still -EINVAL until here */
2098 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2099 if (ret < 0) {
2100 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002101 goto fail_getopt;
2102 }
2103
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002104 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002105 if (quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002106 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002107 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002108 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002109 qemu_progress_print(0, 100);
2110
Peter Lieven9fd77f92017-04-21 11:11:55 +02002111 s.src = g_new0(BlockBackend *, s.src_num);
2112 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002113
Peter Lieven9fd77f92017-04-21 11:11:55 +02002114 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2115 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Fam Zheng335e9932017-05-03 00:35:39 +08002116 fmt, src_flags, src_writethrough, quiet,
2117 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002118 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002119 ret = -1;
2120 goto out;
2121 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002122 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2123 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002124 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002125 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002126 ret = -1;
2127 goto out;
2128 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002129 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002130 }
bellardea2384d2004-08-01 21:59:26 +00002131
Wenchao Xiaef806542013-12-04 17:10:57 +08002132 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002133 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002134 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2135 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2136 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002137 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002138 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002139 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002140 ret = -1;
2141 goto out;
2142 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002143
Peter Lieven9fd77f92017-04-21 11:11:55 +02002144 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2145 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002146 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002147 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002148 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002149 ret = -1;
2150 goto out;
edison51ef6722010-09-21 19:58:41 -07002151 }
2152
Kevin Wolfefa84d42009-05-18 16:42:12 +02002153 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +00002154 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002155 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002156 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002157 ret = -1;
2158 goto out;
2159 }
balrog926c2d22007-10-31 01:11:44 +00002160
Max Reitzb65a5e12015-02-05 13:58:12 -05002161 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002162 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +01002163 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002164 ret = -1;
2165 goto out;
2166 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09002167
Max Reitz2e024cd2015-02-11 09:58:46 -05002168 if (!skip_create) {
2169 if (!drv->create_opts) {
2170 error_report("Format driver '%s' does not support image creation",
2171 drv->format_name);
2172 ret = -1;
2173 goto out;
2174 }
Max Reitzf75613c2014-12-02 18:32:46 +01002175
Max Reitz2e024cd2015-02-11 09:58:46 -05002176 if (!proto_drv->create_opts) {
2177 error_report("Protocol driver '%s' does not support image creation",
2178 proto_drv->format_name);
2179 ret = -1;
2180 goto out;
2181 }
Max Reitzf75613c2014-12-02 18:32:46 +01002182
Max Reitz2e024cd2015-02-11 09:58:46 -05002183 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2184 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002185
Max Reitz2e024cd2015-02-11 09:58:46 -05002186 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002187 if (options) {
2188 qemu_opts_do_parse(opts, options, NULL, &local_err);
2189 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002190 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002191 ret = -1;
2192 goto out;
2193 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002194 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002195
Peter Lieven9fd77f92017-04-21 11:11:55 +02002196 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002197 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002198 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2199 if (ret < 0) {
2200 goto out;
2201 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002202 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002203
Kevin Wolfa18953f2010-10-14 15:46:04 +02002204 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002205 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002206 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002207 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002208 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002209 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002210
Max Reitz48758a82017-04-26 15:46:48 +02002211 if (s.src_num > 1 && out_baseimg) {
2212 error_report("Having a backing file for the target makes no sense when "
2213 "concatenating multiple input images");
2214 ret = -1;
2215 goto out;
2216 }
2217
Kevin Wolfefa84d42009-05-18 16:42:12 +02002218 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002219 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002220 bool encryption =
2221 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
2222 const char *preallocation =
2223 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002224
Pavel Butsykin35fadca2016-07-22 11:17:48 +03002225 if (!drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002226 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002227 ret = -1;
2228 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002229 }
2230
Chunyan Liu83d05212014-06-05 17:20:51 +08002231 if (encryption) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002232 error_report("Compression and encryption not supported at "
2233 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002234 ret = -1;
2235 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002236 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002237
Chunyan Liu83d05212014-06-05 17:20:51 +08002238 if (preallocation
2239 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002240 {
2241 error_report("Compression and preallocation not supported at "
2242 "the same time");
2243 ret = -1;
2244 goto out;
2245 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002246 }
2247
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002248 if (!skip_create) {
2249 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002250 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002251 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002252 error_reportf_err(local_err, "%s: error while converting %s: ",
2253 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002254 goto out;
bellardea2384d2004-08-01 21:59:26 +00002255 }
2256 }
ths3b46e622007-09-17 08:09:54 +00002257
Peter Lieven9fd77f92017-04-21 11:11:55 +02002258 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002259 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002260 if (ret < 0) {
2261 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002262 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002263 }
2264
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002265 /* XXX we should allow --image-opts to trigger use of
2266 * img_open() here, but then we have trouble with
2267 * the bdrv_create() call which takes different params.
2268 * Not critical right now, so fix can wait...
2269 */
Fam Zheng335e9932017-05-03 00:35:39 +08002270 s.target = img_open_file(out_filename, out_fmt, flags, writethrough, quiet,
2271 false);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002272 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002273 ret = -1;
2274 goto out;
2275 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002276 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002277
Eric Blake5def6b82016-06-23 16:37:19 -06002278 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002279 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2280 * as maximum. */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002281 s.buf_sectors = MIN(32768,
2282 MAX(s.buf_sectors,
2283 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2284 out_bs->bl.pdiscard_alignment >>
2285 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002286
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002287 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002288 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002289 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002290 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002291 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002292 ret = -1;
2293 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002294 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002295 error_report("output file is smaller than input file");
2296 ret = -1;
2297 goto out;
2298 }
2299 }
2300
Peter Lieven24f833c2013-11-27 11:07:07 +01002301 ret = bdrv_get_info(out_bs, &bdi);
2302 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002303 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002304 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002305 goto out;
2306 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002307 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002308 s.compressed = s.compressed || bdi.needs_compressed_writes;
2309 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Peter Lieven24f833c2013-11-27 11:07:07 +01002310 }
2311
Peter Lieven9fd77f92017-04-21 11:11:55 +02002312 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002313out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002314 if (!ret) {
2315 qemu_progress_print(100, 0);
2316 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002317 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002318 qemu_opts_del(opts);
2319 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002320 qemu_opts_del(sn_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002321 blk_unref(s.target);
2322 if (s.src) {
2323 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2324 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002325 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002326 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002327 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002328 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002329fail_getopt:
2330 g_free(options);
2331
Peter Lieven9fd77f92017-04-21 11:11:55 +02002332 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002333}
2334
bellard57d1a2b2004-08-03 21:15:11 +00002335
bellardfaea38e2006-08-05 21:31:00 +00002336static void dump_snapshots(BlockDriverState *bs)
2337{
2338 QEMUSnapshotInfo *sn_tab, *sn;
2339 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002340
2341 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2342 if (nb_sns <= 0)
2343 return;
2344 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002345 bdrv_snapshot_dump(fprintf, stdout, NULL);
2346 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002347 for(i = 0; i < nb_sns; i++) {
2348 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002349 bdrv_snapshot_dump(fprintf, stdout, sn);
2350 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002351 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002352 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002353}
2354
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002355static void dump_json_image_info_list(ImageInfoList *list)
2356{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002357 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002358 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002359 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002360
2361 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2362 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002363 str = qobject_to_json_pretty(obj);
2364 assert(str != NULL);
2365 printf("%s\n", qstring_get_str(str));
2366 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002367 visit_free(v);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002368 QDECREF(str);
2369}
2370
Benoît Canetc054b3f2012-09-05 13:09:02 +02002371static void dump_json_image_info(ImageInfo *info)
2372{
Benoît Canetc054b3f2012-09-05 13:09:02 +02002373 QString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002374 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002375 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002376
2377 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2378 visit_complete(v, &obj);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002379 str = qobject_to_json_pretty(obj);
2380 assert(str != NULL);
2381 printf("%s\n", qstring_get_str(str));
2382 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002383 visit_free(v);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002384 QDECREF(str);
2385}
2386
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002387static void dump_human_image_info_list(ImageInfoList *list)
2388{
2389 ImageInfoList *elem;
2390 bool delim = false;
2391
2392 for (elem = list; elem; elem = elem->next) {
2393 if (delim) {
2394 printf("\n");
2395 }
2396 delim = true;
2397
Wenchao Xia5b917042013-05-25 11:09:45 +08002398 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002399 }
2400}
2401
2402static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2403{
2404 return strcmp(a, b) == 0;
2405}
2406
2407/**
2408 * Open an image file chain and return an ImageInfoList
2409 *
2410 * @filename: topmost image filename
2411 * @fmt: topmost image format (may be NULL to autodetect)
2412 * @chain: true - enumerate entire backing file chain
2413 * false - only topmost image file
2414 *
2415 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2416 * image file. If there was an error a message will have been printed to
2417 * stderr.
2418 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002419static ImageInfoList *collect_image_info_list(bool image_opts,
2420 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002421 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002422 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002423{
2424 ImageInfoList *head = NULL;
2425 ImageInfoList **last = &head;
2426 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002427 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002428
2429 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2430
2431 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002432 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002433 BlockDriverState *bs;
2434 ImageInfo *info;
2435 ImageInfoList *elem;
2436
2437 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2438 error_report("Backing file '%s' creates an infinite loop.",
2439 filename);
2440 goto err;
2441 }
2442 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2443
Max Reitzefaa7c42016-03-16 19:54:38 +01002444 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002445 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2446 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002447 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002448 goto err;
2449 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002450 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002451
Wenchao Xia43526ec2013-06-06 12:27:58 +08002452 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002453 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002454 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002455 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002456 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002457 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002458
2459 elem = g_new0(ImageInfoList, 1);
2460 elem->value = info;
2461 *last = elem;
2462 last = &elem->next;
2463
Markus Armbruster26f54e92014-10-07 13:59:04 +02002464 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002465
2466 filename = fmt = NULL;
2467 if (chain) {
2468 if (info->has_full_backing_filename) {
2469 filename = info->full_backing_filename;
2470 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002471 error_report("Could not determine absolute backing filename,"
2472 " but backing filename '%s' present",
2473 info->backing_filename);
2474 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002475 }
2476 if (info->has_backing_filename_format) {
2477 fmt = info->backing_filename_format;
2478 }
2479 }
2480 }
2481 g_hash_table_destroy(filenames);
2482 return head;
2483
2484err:
2485 qapi_free_ImageInfoList(head);
2486 g_hash_table_destroy(filenames);
2487 return NULL;
2488}
2489
Benoît Canetc054b3f2012-09-05 13:09:02 +02002490static int img_info(int argc, char **argv)
2491{
2492 int c;
2493 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002494 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002495 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002496 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002497 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002498 bool force_share = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002499
bellardea2384d2004-08-01 21:59:26 +00002500 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002501 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002502 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002503 int option_index = 0;
2504 static const struct option long_options[] = {
2505 {"help", no_argument, 0, 'h'},
2506 {"format", required_argument, 0, 'f'},
2507 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002508 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002509 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002510 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002511 {"force-share", no_argument, 0, 'U'},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002512 {0, 0, 0, 0}
2513 };
Fam Zheng335e9932017-05-03 00:35:39 +08002514 c = getopt_long(argc, argv, ":f:hU",
Benoît Canetc054b3f2012-09-05 13:09:02 +02002515 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002516 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002517 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002518 }
bellardea2384d2004-08-01 21:59:26 +00002519 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002520 case ':':
2521 missing_argument(argv[optind - 1]);
2522 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002523 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002524 unrecognized_option(argv[optind - 1]);
2525 break;
bellardea2384d2004-08-01 21:59:26 +00002526 case 'h':
2527 help();
2528 break;
2529 case 'f':
2530 fmt = optarg;
2531 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002532 case 'U':
2533 force_share = true;
2534 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002535 case OPTION_OUTPUT:
2536 output = optarg;
2537 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002538 case OPTION_BACKING_CHAIN:
2539 chain = true;
2540 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002541 case OPTION_OBJECT: {
2542 QemuOpts *opts;
2543 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2544 optarg, true);
2545 if (!opts) {
2546 return 1;
2547 }
2548 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002549 case OPTION_IMAGE_OPTS:
2550 image_opts = true;
2551 break;
bellardea2384d2004-08-01 21:59:26 +00002552 }
2553 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002554 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002555 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002556 }
bellardea2384d2004-08-01 21:59:26 +00002557 filename = argv[optind++];
2558
Benoît Canetc054b3f2012-09-05 13:09:02 +02002559 if (output && !strcmp(output, "json")) {
2560 output_format = OFORMAT_JSON;
2561 } else if (output && !strcmp(output, "human")) {
2562 output_format = OFORMAT_HUMAN;
2563 } else if (output) {
2564 error_report("--output must be used with human or json as argument.");
2565 return 1;
2566 }
2567
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002568 if (qemu_opts_foreach(&qemu_object_opts,
2569 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002570 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002571 return 1;
2572 }
2573
Fam Zheng335e9932017-05-03 00:35:39 +08002574 list = collect_image_info_list(image_opts, filename, fmt, chain,
2575 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002576 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002577 return 1;
2578 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002579
Benoît Canetc054b3f2012-09-05 13:09:02 +02002580 switch (output_format) {
2581 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002582 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002583 break;
2584 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002585 if (chain) {
2586 dump_json_image_info_list(list);
2587 } else {
2588 dump_json_image_info(list->value);
2589 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002590 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002591 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002592
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002593 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002594 return 0;
2595}
2596
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002597static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2598 MapEntry *next)
2599{
2600 switch (output_format) {
2601 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002602 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002603 error_report("File contains external, encrypted or compressed clusters.");
2604 exit(1);
2605 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002606 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002607 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002608 e->start, e->length,
2609 e->has_offset ? e->offset : 0,
2610 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002611 }
2612 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2613 * Modify the flags here to allow more coalescing.
2614 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002615 if (next && (!next->data || next->zero)) {
2616 next->data = false;
2617 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002618 }
2619 break;
2620 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002621 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2622 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002623 (e->start == 0 ? "[" : ",\n"),
2624 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002625 e->zero ? "true" : "false",
2626 e->data ? "true" : "false");
2627 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002628 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002629 }
2630 putchar('}');
2631
2632 if (!next) {
2633 printf("]\n");
2634 }
2635 break;
2636 }
2637}
2638
2639static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2640 int nb_sectors, MapEntry *e)
2641{
2642 int64_t ret;
2643 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002644 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002645 bool has_offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002646
2647 /* As an optimization, we could cache the current range of unallocated
2648 * clusters in each file of the chain, and avoid querying the same
2649 * range repeatedly.
2650 */
2651
2652 depth = 0;
2653 for (;;) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08002654 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2655 &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002656 if (ret < 0) {
2657 return ret;
2658 }
2659 assert(nb_sectors);
2660 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2661 break;
2662 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002663 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002664 if (bs == NULL) {
2665 ret = 0;
2666 break;
2667 }
2668
2669 depth++;
2670 }
2671
John Snow28756452016-02-05 13:12:33 -05002672 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2673
2674 *e = (MapEntry) {
2675 .start = sector_num * BDRV_SECTOR_SIZE,
2676 .length = nb_sectors * BDRV_SECTOR_SIZE,
2677 .data = !!(ret & BDRV_BLOCK_DATA),
2678 .zero = !!(ret & BDRV_BLOCK_ZERO),
2679 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2680 .has_offset = has_offset,
2681 .depth = depth,
2682 .has_filename = file && has_offset,
2683 .filename = file && has_offset ? file->filename : NULL,
2684 };
2685
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002686 return 0;
2687}
2688
Fam Zheng16b0d552016-01-26 11:59:02 +08002689static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2690{
2691 if (curr->length == 0) {
2692 return false;
2693 }
2694 if (curr->zero != next->zero ||
2695 curr->data != next->data ||
2696 curr->depth != next->depth ||
2697 curr->has_filename != next->has_filename ||
2698 curr->has_offset != next->has_offset) {
2699 return false;
2700 }
2701 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2702 return false;
2703 }
2704 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2705 return false;
2706 }
2707 return true;
2708}
2709
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002710static int img_map(int argc, char **argv)
2711{
2712 int c;
2713 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002714 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002715 BlockDriverState *bs;
2716 const char *filename, *fmt, *output;
2717 int64_t length;
2718 MapEntry curr = { .length = 0 }, next;
2719 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002720 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002721 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002722
2723 fmt = NULL;
2724 output = NULL;
2725 for (;;) {
2726 int option_index = 0;
2727 static const struct option long_options[] = {
2728 {"help", no_argument, 0, 'h'},
2729 {"format", required_argument, 0, 'f'},
2730 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002731 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002732 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002733 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002734 {0, 0, 0, 0}
2735 };
Fam Zheng335e9932017-05-03 00:35:39 +08002736 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002737 long_options, &option_index);
2738 if (c == -1) {
2739 break;
2740 }
2741 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002742 case ':':
2743 missing_argument(argv[optind - 1]);
2744 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002745 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002746 unrecognized_option(argv[optind - 1]);
2747 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002748 case 'h':
2749 help();
2750 break;
2751 case 'f':
2752 fmt = optarg;
2753 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002754 case 'U':
2755 force_share = true;
2756 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002757 case OPTION_OUTPUT:
2758 output = optarg;
2759 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002760 case OPTION_OBJECT: {
2761 QemuOpts *opts;
2762 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2763 optarg, true);
2764 if (!opts) {
2765 return 1;
2766 }
2767 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002768 case OPTION_IMAGE_OPTS:
2769 image_opts = true;
2770 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002771 }
2772 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002773 if (optind != argc - 1) {
2774 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002775 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002776 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002777
2778 if (output && !strcmp(output, "json")) {
2779 output_format = OFORMAT_JSON;
2780 } else if (output && !strcmp(output, "human")) {
2781 output_format = OFORMAT_HUMAN;
2782 } else if (output) {
2783 error_report("--output must be used with human or json as argument.");
2784 return 1;
2785 }
2786
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002787 if (qemu_opts_foreach(&qemu_object_opts,
2788 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002789 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002790 return 1;
2791 }
2792
Fam Zheng335e9932017-05-03 00:35:39 +08002793 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002794 if (!blk) {
2795 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002796 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002797 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002798
2799 if (output_format == OFORMAT_HUMAN) {
2800 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2801 }
2802
Max Reitzf1d3cd72015-02-05 13:58:18 -05002803 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002804 while (curr.start + curr.length < length) {
2805 int64_t nsectors_left;
2806 int64_t sector_num;
2807 int n;
2808
2809 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2810
2811 /* Probe up to 1 GiB at a time. */
2812 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2813 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2814 ret = get_block_status(bs, sector_num, n, &next);
2815
2816 if (ret < 0) {
2817 error_report("Could not read file metadata: %s", strerror(-ret));
2818 goto out;
2819 }
2820
Fam Zheng16b0d552016-01-26 11:59:02 +08002821 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002822 curr.length += next.length;
2823 continue;
2824 }
2825
2826 if (curr.length > 0) {
2827 dump_map_entry(output_format, &curr, &next);
2828 }
2829 curr = next;
2830 }
2831
2832 dump_map_entry(output_format, &curr, NULL);
2833
2834out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002835 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002836 return ret < 0;
2837}
2838
aliguorif7b4a942009-01-07 17:40:15 +00002839#define SNAPSHOT_LIST 1
2840#define SNAPSHOT_CREATE 2
2841#define SNAPSHOT_APPLY 3
2842#define SNAPSHOT_DELETE 4
2843
Stuart Brady153859b2009-06-07 00:42:17 +01002844static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002845{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002846 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002847 BlockDriverState *bs;
2848 QEMUSnapshotInfo sn;
2849 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002850 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002851 int action = 0;
2852 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002853 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002854 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002855 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002856 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00002857
Kevin Wolfce099542016-03-15 13:01:04 +01002858 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002859 /* Parse commandline parameters */
2860 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002861 static const struct option long_options[] = {
2862 {"help", no_argument, 0, 'h'},
2863 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002864 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002865 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002866 {0, 0, 0, 0}
2867 };
Fam Zheng335e9932017-05-03 00:35:39 +08002868 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002869 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002870 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002871 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002872 }
aliguorif7b4a942009-01-07 17:40:15 +00002873 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002874 case ':':
2875 missing_argument(argv[optind - 1]);
2876 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002877 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002878 unrecognized_option(argv[optind - 1]);
2879 break;
aliguorif7b4a942009-01-07 17:40:15 +00002880 case 'h':
2881 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002882 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002883 case 'l':
2884 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002885 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002886 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002887 }
2888 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002889 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002890 break;
2891 case 'a':
2892 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002893 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002894 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002895 }
2896 action = SNAPSHOT_APPLY;
2897 snapshot_name = optarg;
2898 break;
2899 case 'c':
2900 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002901 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002902 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002903 }
2904 action = SNAPSHOT_CREATE;
2905 snapshot_name = optarg;
2906 break;
2907 case 'd':
2908 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002909 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002910 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002911 }
2912 action = SNAPSHOT_DELETE;
2913 snapshot_name = optarg;
2914 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002915 case 'q':
2916 quiet = true;
2917 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002918 case 'U':
2919 force_share = true;
2920 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002921 case OPTION_OBJECT: {
2922 QemuOpts *opts;
2923 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2924 optarg, true);
2925 if (!opts) {
2926 return 1;
2927 }
2928 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002929 case OPTION_IMAGE_OPTS:
2930 image_opts = true;
2931 break;
aliguorif7b4a942009-01-07 17:40:15 +00002932 }
2933 }
2934
Kevin Wolffc11eb22013-08-05 10:53:04 +02002935 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002936 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002937 }
aliguorif7b4a942009-01-07 17:40:15 +00002938 filename = argv[optind++];
2939
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002940 if (qemu_opts_foreach(&qemu_object_opts,
2941 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002942 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002943 return 1;
2944 }
2945
aliguorif7b4a942009-01-07 17:40:15 +00002946 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08002947 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
2948 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002949 if (!blk) {
2950 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002951 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002952 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002953
2954 /* Perform the requested action */
2955 switch(action) {
2956 case SNAPSHOT_LIST:
2957 dump_snapshots(bs);
2958 break;
2959
2960 case SNAPSHOT_CREATE:
2961 memset(&sn, 0, sizeof(sn));
2962 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2963
2964 qemu_gettimeofday(&tv);
2965 sn.date_sec = tv.tv_sec;
2966 sn.date_nsec = tv.tv_usec * 1000;
2967
2968 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002969 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002970 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002971 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002972 }
aliguorif7b4a942009-01-07 17:40:15 +00002973 break;
2974
2975 case SNAPSHOT_APPLY:
2976 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002977 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002978 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002979 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002980 }
aliguorif7b4a942009-01-07 17:40:15 +00002981 break;
2982
2983 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002984 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002985 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002986 error_reportf_err(err, "Could not delete snapshot '%s': ",
2987 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002988 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002989 }
aliguorif7b4a942009-01-07 17:40:15 +00002990 break;
2991 }
2992
2993 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02002994 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002995 if (ret) {
2996 return 1;
2997 }
Stuart Brady153859b2009-06-07 00:42:17 +01002998 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002999}
3000
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003001static int img_rebase(int argc, char **argv)
3002{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003003 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003004 uint8_t *buf_old = NULL;
3005 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05003006 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003007 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003008 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3009 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003010 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003011 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003012 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003013 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003014 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003015 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003016 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003017
3018 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003019 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003020 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003021 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003022 out_baseimg = NULL;
3023 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003024 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003025 static const struct option long_options[] = {
3026 {"help", no_argument, 0, 'h'},
3027 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003028 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003029 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003030 {0, 0, 0, 0}
3031 };
Fam Zheng335e9932017-05-03 00:35:39 +08003032 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003033 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003034 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003035 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003036 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003037 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003038 case ':':
3039 missing_argument(argv[optind - 1]);
3040 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003041 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003042 unrecognized_option(argv[optind - 1]);
3043 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003044 case 'h':
3045 help();
3046 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003047 case 'f':
3048 fmt = optarg;
3049 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003050 case 'F':
3051 out_basefmt = optarg;
3052 break;
3053 case 'b':
3054 out_baseimg = optarg;
3055 break;
3056 case 'u':
3057 unsafe = 1;
3058 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003059 case 'p':
3060 progress = 1;
3061 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003062 case 't':
3063 cache = optarg;
3064 break;
Max Reitz40055952014-07-22 22:58:42 +02003065 case 'T':
3066 src_cache = optarg;
3067 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003068 case 'q':
3069 quiet = true;
3070 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003071 case OPTION_OBJECT: {
3072 QemuOpts *opts;
3073 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3074 optarg, true);
3075 if (!opts) {
3076 return 1;
3077 }
3078 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003079 case OPTION_IMAGE_OPTS:
3080 image_opts = true;
3081 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003082 case 'U':
3083 force_share = true;
3084 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003085 }
3086 }
3087
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003088 if (quiet) {
3089 progress = 0;
3090 }
3091
Fam Zhengac1307a2014-04-22 13:36:11 +08003092 if (optind != argc - 1) {
3093 error_exit("Expecting one image file name");
3094 }
3095 if (!unsafe && !out_baseimg) {
3096 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003097 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003098 filename = argv[optind++];
3099
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003100 if (qemu_opts_foreach(&qemu_object_opts,
3101 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003102 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003103 return 1;
3104 }
3105
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003106 qemu_progress_init(progress, 2.0);
3107 qemu_progress_print(0, 100);
3108
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003109 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003110 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003111 if (ret < 0) {
3112 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003113 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003114 }
3115
Kevin Wolfce099542016-03-15 13:01:04 +01003116 src_flags = 0;
3117 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003118 if (ret < 0) {
3119 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003120 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003121 }
3122
Kevin Wolfce099542016-03-15 13:01:04 +01003123 /* The source files are opened read-only, don't care about WCE */
3124 assert((src_flags & BDRV_O_RDWR) == 0);
3125 (void) src_writethrough;
3126
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003127 /*
3128 * Open the images.
3129 *
3130 * Ignore the old backing file for unsafe rebase in case we want to correct
3131 * the reference to a renamed or moved backing file.
3132 */
Fam Zheng335e9932017-05-03 00:35:39 +08003133 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3134 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003135 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003136 ret = -1;
3137 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003138 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003139 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003140
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003141 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003142 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003143 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003144 ret = -1;
3145 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003146 }
3147 }
3148
3149 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003150 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003151 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003152 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003153
Max Reitz644483d2015-02-05 13:58:17 -05003154 if (bs->backing_format[0] != '\0') {
3155 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003156 qdict_put_str(options, "driver", bs->backing_format);
Max Reitz644483d2015-02-05 13:58:17 -05003157 }
3158
Fam Zheng335e9932017-05-03 00:35:39 +08003159 if (force_share) {
3160 if (!options) {
3161 options = qdict_new();
3162 }
3163 qdict_put(options, BDRV_OPT_FORCE_SHARE,
3164 qbool_from_bool(true));
3165 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003166 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003167 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003168 options, src_flags, &local_err);
3169 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003170 error_reportf_err(local_err,
3171 "Could not open old backing file '%s': ",
3172 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003173 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003174 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003175 }
Max Reitz644483d2015-02-05 13:58:17 -05003176
Alex Bligha6166732012-10-16 13:46:18 +01003177 if (out_baseimg[0]) {
Fam Zheng335e9932017-05-03 00:35:39 +08003178 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003179 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003180 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003181 }
3182 if (force_share) {
3183 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003184 }
3185
Max Reitzefaa7c42016-03-16 19:54:38 +01003186 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003187 options, src_flags, &local_err);
3188 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003189 error_reportf_err(local_err,
3190 "Could not open new backing file '%s': ",
3191 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003192 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003193 goto out;
3194 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003195 }
3196 }
3197
3198 /*
3199 * Check each unallocated cluster in the COW file. If it is unallocated,
3200 * accesses go to the backing file. We must therefore compare this cluster
3201 * in the old and new backing file, and if they differ we need to copy it
3202 * from the old backing file into the COW file.
3203 *
3204 * If qemu-img crashes during this step, no harm is done. The content of
3205 * the image is the same as the original one at any time.
3206 */
3207 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003208 int64_t num_sectors;
3209 int64_t old_backing_num_sectors;
3210 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003211 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003212 int n;
Kevin Wolf1f710492012-10-12 14:29:18 +02003213 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003214
Max Reitzf1d3cd72015-02-05 13:58:18 -05003215 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3216 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003217
Max Reitzf1d3cd72015-02-05 13:58:18 -05003218 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003219 if (num_sectors < 0) {
3220 error_report("Could not get size of '%s': %s",
3221 filename, strerror(-num_sectors));
3222 ret = -1;
3223 goto out;
3224 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003225 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003226 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003227 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003228
3229 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3230 error_report("Could not get size of '%s': %s",
3231 backing_name, strerror(-old_backing_num_sectors));
3232 ret = -1;
3233 goto out;
3234 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003235 if (blk_new_backing) {
3236 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003237 if (new_backing_num_sectors < 0) {
3238 error_report("Could not get size of '%s': %s",
3239 out_baseimg, strerror(-new_backing_num_sectors));
3240 ret = -1;
3241 goto out;
3242 }
Alex Bligha6166732012-10-16 13:46:18 +01003243 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003244
Kevin Wolf1f710492012-10-12 14:29:18 +02003245 if (num_sectors != 0) {
3246 local_progress = (float)100 /
3247 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
3248 }
3249
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003250 for (sector = 0; sector < num_sectors; sector += n) {
3251
3252 /* How many sectors can we handle with the next read? */
3253 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
3254 n = (IO_BUF_SIZE / 512);
3255 } else {
3256 n = num_sectors - sector;
3257 }
3258
3259 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02003260 ret = bdrv_is_allocated(bs, sector, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003261 if (ret < 0) {
3262 error_report("error while reading image metadata: %s",
3263 strerror(-ret));
3264 goto out;
3265 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003266 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003267 continue;
3268 }
3269
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003270 /*
3271 * Read old and new backing file and take into consideration that
3272 * backing files may be smaller than the COW image.
3273 */
3274 if (sector >= old_backing_num_sectors) {
3275 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3276 } else {
3277 if (sector + n > old_backing_num_sectors) {
3278 n = old_backing_num_sectors - sector;
3279 }
3280
Eric Blake91669202016-05-06 10:26:43 -06003281 ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS,
3282 buf_old, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003283 if (ret < 0) {
3284 error_report("error while reading from old backing file");
3285 goto out;
3286 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003287 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003288
Max Reitzf1d3cd72015-02-05 13:58:18 -05003289 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003290 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3291 } else {
3292 if (sector + n > new_backing_num_sectors) {
3293 n = new_backing_num_sectors - sector;
3294 }
3295
Eric Blake91669202016-05-06 10:26:43 -06003296 ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS,
3297 buf_new, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003298 if (ret < 0) {
3299 error_report("error while reading from new backing file");
3300 goto out;
3301 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003302 }
3303
3304 /* If they differ, we need to write to the COW file */
3305 uint64_t written = 0;
3306
3307 while (written < n) {
3308 int pnum;
3309
3310 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003311 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003312 {
Eric Blake91669202016-05-06 10:26:43 -06003313 ret = blk_pwrite(blk,
3314 (sector + written) << BDRV_SECTOR_BITS,
3315 buf_old + written * 512,
3316 pnum << BDRV_SECTOR_BITS, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003317 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003318 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003319 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003320 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003321 }
3322 }
3323
3324 written += pnum;
3325 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003326 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003327 }
3328 }
3329
3330 /*
3331 * Change the backing file. All clusters that are different from the old
3332 * backing file are overwritten in the COW file now, so the visible content
3333 * doesn't change when we switch the backing file.
3334 */
Alex Bligha6166732012-10-16 13:46:18 +01003335 if (out_baseimg && *out_baseimg) {
3336 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3337 } else {
3338 ret = bdrv_change_backing_file(bs, NULL, NULL);
3339 }
3340
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003341 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003342 error_report("Could not change the backing file to '%s': No "
3343 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003344 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003345 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003346 out_baseimg, strerror(-ret));
3347 }
3348
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003349 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003350 /*
3351 * TODO At this point it is possible to check if any clusters that are
3352 * allocated in the COW file are the same in the backing file. If so, they
3353 * could be dropped from the COW file. Don't do this before switching the
3354 * backing file, in case of a crash this would lead to corruption.
3355 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003356out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003357 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003358 /* Cleanup */
3359 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003360 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003361 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003362 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003363 qemu_vfree(buf_old);
3364 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003365
Markus Armbruster26f54e92014-10-07 13:59:04 +02003366 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003367 if (ret) {
3368 return 1;
3369 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003370 return 0;
3371}
3372
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003373static int img_resize(int argc, char **argv)
3374{
Markus Armbruster6750e792015-02-12 17:43:08 +01003375 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003376 int c, ret, relative;
3377 const char *filename, *fmt, *size;
3378 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003379 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003380 BlockBackend *blk = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003381 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003382
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003383 static QemuOptsList resize_options = {
3384 .name = "resize_options",
3385 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3386 .desc = {
3387 {
3388 .name = BLOCK_OPT_SIZE,
3389 .type = QEMU_OPT_SIZE,
3390 .help = "Virtual disk size"
3391 }, {
3392 /* end of list */
3393 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003394 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003395 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003396 bool image_opts = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003397
Kevin Wolfe80fec72011-04-29 10:58:12 +02003398 /* Remove size from argv manually so that negative numbers are not treated
3399 * as options by getopt. */
3400 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003401 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003402 return 1;
3403 }
3404
3405 size = argv[--argc];
3406
3407 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003408 fmt = NULL;
3409 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003410 static const struct option long_options[] = {
3411 {"help", no_argument, 0, 'h'},
3412 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003413 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003414 {0, 0, 0, 0}
3415 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003416 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003417 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003418 if (c == -1) {
3419 break;
3420 }
3421 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003422 case ':':
3423 missing_argument(argv[optind - 1]);
3424 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003425 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003426 unrecognized_option(argv[optind - 1]);
3427 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003428 case 'h':
3429 help();
3430 break;
3431 case 'f':
3432 fmt = optarg;
3433 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003434 case 'q':
3435 quiet = true;
3436 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003437 case OPTION_OBJECT: {
3438 QemuOpts *opts;
3439 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3440 optarg, true);
3441 if (!opts) {
3442 return 1;
3443 }
3444 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003445 case OPTION_IMAGE_OPTS:
3446 image_opts = true;
3447 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003448 }
3449 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003450 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003451 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003452 }
3453 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003454
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003455 if (qemu_opts_foreach(&qemu_object_opts,
3456 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003457 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003458 return 1;
3459 }
3460
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003461 /* Choose grow, shrink, or absolute resize mode */
3462 switch (size[0]) {
3463 case '+':
3464 relative = 1;
3465 size++;
3466 break;
3467 case '-':
3468 relative = -1;
3469 size++;
3470 break;
3471 default:
3472 relative = 0;
3473 break;
3474 }
3475
3476 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003477 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003478 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003479 if (err) {
3480 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003481 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003482 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003483 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003484 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003485 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3486 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003487
Max Reitzefaa7c42016-03-16 19:54:38 +01003488 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003489 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3490 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003491 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003492 ret = -1;
3493 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003494 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003495
3496 if (relative) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003497 total_size = blk_getlength(blk) + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003498 } else {
3499 total_size = n;
3500 }
3501 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003502 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003503 ret = -1;
3504 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003505 }
3506
Max Reitzed3d2ec2017-03-28 22:51:27 +02003507 ret = blk_truncate(blk, total_size, &err);
3508 if (!ret) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003509 qprintf(quiet, "Image resized.\n");
Max Reitzed3d2ec2017-03-28 22:51:27 +02003510 } else {
3511 error_report_err(err);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003512 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003513out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003514 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003515 if (ret) {
3516 return 1;
3517 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003518 return 0;
3519}
3520
Max Reitz76a3a342014-10-27 11:12:51 +01003521static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003522 int64_t offset, int64_t total_work_size,
3523 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003524{
3525 qemu_progress_print(100.f * offset / total_work_size, 0);
3526}
3527
Max Reitz6f176b42013-09-03 10:09:50 +02003528static int img_amend(int argc, char **argv)
3529{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003530 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003531 int c, ret = 0;
3532 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003533 QemuOptsList *create_opts = NULL;
3534 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003535 const char *fmt = NULL, *filename, *cache;
3536 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003537 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003538 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003539 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003540 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003541 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003542
Max Reitzbd39e6e2014-07-22 22:58:43 +02003543 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003544 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003545 static const struct option long_options[] = {
3546 {"help", no_argument, 0, 'h'},
3547 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003548 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003549 {0, 0, 0, 0}
3550 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003551 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003552 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003553 if (c == -1) {
3554 break;
3555 }
3556
3557 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003558 case ':':
3559 missing_argument(argv[optind - 1]);
3560 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003561 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003562 unrecognized_option(argv[optind - 1]);
3563 break;
3564 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003565 help();
3566 break;
3567 case 'o':
3568 if (!is_valid_option_list(optarg)) {
3569 error_report("Invalid option list: %s", optarg);
3570 ret = -1;
3571 goto out_no_progress;
3572 }
3573 if (!options) {
3574 options = g_strdup(optarg);
3575 } else {
3576 char *old_options = options;
3577 options = g_strdup_printf("%s,%s", options, optarg);
3578 g_free(old_options);
3579 }
3580 break;
3581 case 'f':
3582 fmt = optarg;
3583 break;
3584 case 't':
3585 cache = optarg;
3586 break;
3587 case 'p':
3588 progress = true;
3589 break;
3590 case 'q':
3591 quiet = true;
3592 break;
3593 case OPTION_OBJECT:
3594 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3595 optarg, true);
3596 if (!opts) {
3597 ret = -1;
3598 goto out_no_progress;
3599 }
3600 break;
3601 case OPTION_IMAGE_OPTS:
3602 image_opts = true;
3603 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003604 }
3605 }
3606
Max Reitz6f176b42013-09-03 10:09:50 +02003607 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003608 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003609 }
3610
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003611 if (qemu_opts_foreach(&qemu_object_opts,
3612 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003613 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003614 ret = -1;
3615 goto out_no_progress;
3616 }
3617
Max Reitz76a3a342014-10-27 11:12:51 +01003618 if (quiet) {
3619 progress = false;
3620 }
3621 qemu_progress_init(progress, 1.0);
3622
Kevin Wolfa283cb62014-02-21 16:24:07 +01003623 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3624 if (fmt && has_help_option(options)) {
3625 /* If a format is explicitly specified (and possibly no filename is
3626 * given), print option help here */
3627 ret = print_block_option_help(filename, fmt);
3628 goto out;
3629 }
3630
3631 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003632 error_report("Expecting one image file name");
3633 ret = -1;
3634 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003635 }
Max Reitz6f176b42013-09-03 10:09:50 +02003636
Kevin Wolfce099542016-03-15 13:01:04 +01003637 flags = BDRV_O_RDWR;
3638 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003639 if (ret < 0) {
3640 error_report("Invalid cache option: %s", cache);
3641 goto out;
3642 }
3643
Fam Zheng335e9932017-05-03 00:35:39 +08003644 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3645 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003646 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003647 ret = -1;
3648 goto out;
3649 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003650 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003651
3652 fmt = bs->drv->format_name;
3653
Kevin Wolf626f84f2014-02-21 16:24:06 +01003654 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003655 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003656 ret = print_block_option_help(filename, fmt);
3657 goto out;
3658 }
3659
Max Reitzb2439d22014-12-02 18:32:47 +01003660 if (!bs->drv->create_opts) {
3661 error_report("Format driver '%s' does not support any options to amend",
3662 fmt);
3663 ret = -1;
3664 goto out;
3665 }
3666
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003667 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003668 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003669 qemu_opts_do_parse(opts, options, NULL, &err);
3670 if (err) {
3671 error_report_err(err);
3672 ret = -1;
3673 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003674 }
3675
Max Reitz76a3a342014-10-27 11:12:51 +01003676 /* In case the driver does not call amend_status_cb() */
3677 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003678 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003679 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003680 if (ret < 0) {
3681 error_report("Error while amending options: %s", strerror(-ret));
3682 goto out;
3683 }
3684
3685out:
Max Reitz76a3a342014-10-27 11:12:51 +01003686 qemu_progress_end();
3687
Max Reitze814dff2015-08-20 16:00:38 -07003688out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003689 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003690 qemu_opts_del(opts);
3691 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003692 g_free(options);
3693
Max Reitz6f176b42013-09-03 10:09:50 +02003694 if (ret) {
3695 return 1;
3696 }
3697 return 0;
3698}
3699
Kevin Wolfb6133b82014-08-05 14:17:13 +02003700typedef struct BenchData {
3701 BlockBackend *blk;
3702 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003703 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003704 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003705 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003706 int nrreq;
3707 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003708 int flush_interval;
3709 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003710 uint8_t *buf;
3711 QEMUIOVector *qiov;
3712
3713 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003714 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003715 uint64_t offset;
3716} BenchData;
3717
Kevin Wolf55d539c2016-06-03 13:59:41 +02003718static void bench_undrained_flush_cb(void *opaque, int ret)
3719{
3720 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003721 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003722 exit(EXIT_FAILURE);
3723 }
3724}
3725
Kevin Wolfb6133b82014-08-05 14:17:13 +02003726static void bench_cb(void *opaque, int ret)
3727{
3728 BenchData *b = opaque;
3729 BlockAIOCB *acb;
3730
3731 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003732 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003733 exit(EXIT_FAILURE);
3734 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003735
3736 if (b->in_flush) {
3737 /* Just finished a flush with drained queue: Start next requests */
3738 assert(b->in_flight == 0);
3739 b->in_flush = false;
3740 } else if (b->in_flight > 0) {
3741 int remaining = b->n - b->in_flight;
3742
Kevin Wolfb6133b82014-08-05 14:17:13 +02003743 b->n--;
3744 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003745
3746 /* Time for flush? Drain queue if requested, then flush */
3747 if (b->flush_interval && remaining % b->flush_interval == 0) {
3748 if (!b->in_flight || !b->drain_on_flush) {
3749 BlockCompletionFunc *cb;
3750
3751 if (b->drain_on_flush) {
3752 b->in_flush = true;
3753 cb = bench_cb;
3754 } else {
3755 cb = bench_undrained_flush_cb;
3756 }
3757
3758 acb = blk_aio_flush(b->blk, cb, b);
3759 if (!acb) {
3760 error_report("Failed to issue flush request");
3761 exit(EXIT_FAILURE);
3762 }
3763 }
3764 if (b->drain_on_flush) {
3765 return;
3766 }
3767 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003768 }
3769
3770 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003771 int64_t offset = b->offset;
3772 /* blk_aio_* might look for completed I/Os and kick bench_cb
3773 * again, so make sure this operation is counted by in_flight
3774 * and b->offset is ready for the next submission.
3775 */
3776 b->in_flight++;
3777 b->offset += b->step;
3778 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003779 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003780 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003781 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003782 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003783 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003784 if (!acb) {
3785 error_report("Failed to issue request");
3786 exit(EXIT_FAILURE);
3787 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003788 }
3789}
3790
3791static int img_bench(int argc, char **argv)
3792{
3793 int c, ret = 0;
3794 const char *fmt = NULL, *filename;
3795 bool quiet = false;
3796 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003797 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003798 int count = 75000;
3799 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003800 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003801 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003802 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003803 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003804 int flush_interval = 0;
3805 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003806 int64_t image_size;
3807 BlockBackend *blk = NULL;
3808 BenchData data = {};
3809 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003810 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003811 struct timeval t1, t2;
3812 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08003813 bool force_share = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003814
3815 for (;;) {
3816 static const struct option long_options[] = {
3817 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003818 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003819 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003820 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003821 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08003822 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003823 {0, 0, 0, 0}
3824 };
Fam Zheng335e9932017-05-03 00:35:39 +08003825 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003826 if (c == -1) {
3827 break;
3828 }
3829
3830 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003831 case ':':
3832 missing_argument(argv[optind - 1]);
3833 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003834 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003835 unrecognized_option(argv[optind - 1]);
3836 break;
3837 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02003838 help();
3839 break;
3840 case 'c':
3841 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003842 unsigned long res;
3843
3844 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003845 error_report("Invalid request count specified");
3846 return 1;
3847 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003848 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003849 break;
3850 }
3851 case 'd':
3852 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003853 unsigned long res;
3854
3855 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003856 error_report("Invalid queue depth specified");
3857 return 1;
3858 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003859 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003860 break;
3861 }
3862 case 'f':
3863 fmt = optarg;
3864 break;
3865 case 'n':
3866 flags |= BDRV_O_NATIVE_AIO;
3867 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003868 case 'o':
3869 {
Markus Armbruster606caa02017-02-21 21:14:04 +01003870 offset = cvtnum(optarg);
3871 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02003872 error_report("Invalid offset specified");
3873 return 1;
3874 }
3875 break;
3876 }
3877 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003878 case 'q':
3879 quiet = true;
3880 break;
3881 case 's':
3882 {
3883 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003884
Markus Armbruster606caa02017-02-21 21:14:04 +01003885 sval = cvtnum(optarg);
3886 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003887 error_report("Invalid buffer size specified");
3888 return 1;
3889 }
3890
3891 bufsize = sval;
3892 break;
3893 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003894 case 'S':
3895 {
3896 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003897
Markus Armbruster606caa02017-02-21 21:14:04 +01003898 sval = cvtnum(optarg);
3899 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02003900 error_report("Invalid step size specified");
3901 return 1;
3902 }
3903
3904 step = sval;
3905 break;
3906 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003907 case 't':
3908 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3909 if (ret < 0) {
3910 error_report("Invalid cache mode");
3911 ret = -1;
3912 goto out;
3913 }
3914 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003915 case 'w':
3916 flags |= BDRV_O_RDWR;
3917 is_write = true;
3918 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003919 case 'U':
3920 force_share = true;
3921 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003922 case OPTION_PATTERN:
3923 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003924 unsigned long res;
3925
3926 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003927 error_report("Invalid pattern byte specified");
3928 return 1;
3929 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003930 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003931 break;
3932 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003933 case OPTION_FLUSH_INTERVAL:
3934 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003935 unsigned long res;
3936
3937 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02003938 error_report("Invalid flush interval specified");
3939 return 1;
3940 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003941 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003942 break;
3943 }
3944 case OPTION_NO_DRAIN:
3945 drain_on_flush = false;
3946 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003947 case OPTION_IMAGE_OPTS:
3948 image_opts = true;
3949 break;
3950 }
3951 }
3952
3953 if (optind != argc - 1) {
3954 error_exit("Expecting one image file name");
3955 }
3956 filename = argv[argc - 1];
3957
Kevin Wolf55d539c2016-06-03 13:59:41 +02003958 if (!is_write && flush_interval) {
3959 error_report("--flush-interval is only available in write tests");
3960 ret = -1;
3961 goto out;
3962 }
3963 if (flush_interval && flush_interval < depth) {
3964 error_report("Flush interval can't be smaller than depth");
3965 ret = -1;
3966 goto out;
3967 }
3968
Fam Zheng335e9932017-05-03 00:35:39 +08003969 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3970 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003971 if (!blk) {
3972 ret = -1;
3973 goto out;
3974 }
3975
3976 image_size = blk_getlength(blk);
3977 if (image_size < 0) {
3978 ret = image_size;
3979 goto out;
3980 }
3981
3982 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02003983 .blk = blk,
3984 .image_size = image_size,
3985 .bufsize = bufsize,
3986 .step = step ?: bufsize,
3987 .nrreq = depth,
3988 .n = count,
3989 .offset = offset,
3990 .write = is_write,
3991 .flush_interval = flush_interval,
3992 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02003993 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02003994 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02003995 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02003996 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02003997 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02003998 if (flush_interval) {
3999 printf("Sending flush every %d requests\n", flush_interval);
4000 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004001
4002 data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004003 memset(data.buf, pattern, data.nrreq * data.bufsize);
4004
Kevin Wolfb6133b82014-08-05 14:17:13 +02004005 data.qiov = g_new(QEMUIOVector, data.nrreq);
4006 for (i = 0; i < data.nrreq; i++) {
4007 qemu_iovec_init(&data.qiov[i], 1);
4008 qemu_iovec_add(&data.qiov[i],
4009 data.buf + i * data.bufsize, data.bufsize);
4010 }
4011
4012 gettimeofday(&t1, NULL);
4013 bench_cb(&data, 0);
4014
4015 while (data.n > 0) {
4016 main_loop_wait(false);
4017 }
4018 gettimeofday(&t2, NULL);
4019
4020 printf("Run completed in %3.3f seconds.\n",
4021 (t2.tv_sec - t1.tv_sec)
4022 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4023
4024out:
4025 qemu_vfree(data.buf);
4026 blk_unref(blk);
4027
4028 if (ret) {
4029 return 1;
4030 }
4031 return 0;
4032}
4033
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004034#define C_BS 01
4035#define C_COUNT 02
4036#define C_IF 04
4037#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004038#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004039
4040struct DdInfo {
4041 unsigned int flags;
4042 int64_t count;
4043};
4044
4045struct DdIo {
4046 int bsz; /* Block size */
4047 char *filename;
4048 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004049 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004050};
4051
4052struct DdOpts {
4053 const char *name;
4054 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4055 unsigned int flag;
4056};
4057
4058static int img_dd_bs(const char *arg,
4059 struct DdIo *in, struct DdIo *out,
4060 struct DdInfo *dd)
4061{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004062 int64_t res;
4063
Markus Armbruster606caa02017-02-21 21:14:04 +01004064 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004065
Markus Armbruster606caa02017-02-21 21:14:04 +01004066 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004067 error_report("invalid number: '%s'", arg);
4068 return 1;
4069 }
4070 in->bsz = out->bsz = res;
4071
4072 return 0;
4073}
4074
4075static int img_dd_count(const char *arg,
4076 struct DdIo *in, struct DdIo *out,
4077 struct DdInfo *dd)
4078{
Markus Armbruster606caa02017-02-21 21:14:04 +01004079 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004080
Markus Armbruster606caa02017-02-21 21:14:04 +01004081 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004082 error_report("invalid number: '%s'", arg);
4083 return 1;
4084 }
4085
4086 return 0;
4087}
4088
4089static int img_dd_if(const char *arg,
4090 struct DdIo *in, struct DdIo *out,
4091 struct DdInfo *dd)
4092{
4093 in->filename = g_strdup(arg);
4094
4095 return 0;
4096}
4097
4098static int img_dd_of(const char *arg,
4099 struct DdIo *in, struct DdIo *out,
4100 struct DdInfo *dd)
4101{
4102 out->filename = g_strdup(arg);
4103
4104 return 0;
4105}
4106
Reda Sallahif7c15532016-08-10 16:16:09 +02004107static int img_dd_skip(const char *arg,
4108 struct DdIo *in, struct DdIo *out,
4109 struct DdInfo *dd)
4110{
Markus Armbruster606caa02017-02-21 21:14:04 +01004111 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004112
Markus Armbruster606caa02017-02-21 21:14:04 +01004113 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004114 error_report("invalid number: '%s'", arg);
4115 return 1;
4116 }
4117
4118 return 0;
4119}
4120
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004121static int img_dd(int argc, char **argv)
4122{
4123 int ret = 0;
4124 char *arg = NULL;
4125 char *tmp;
4126 BlockDriver *drv = NULL, *proto_drv = NULL;
4127 BlockBackend *blk1 = NULL, *blk2 = NULL;
4128 QemuOpts *opts = NULL;
4129 QemuOptsList *create_opts = NULL;
4130 Error *local_err = NULL;
4131 bool image_opts = false;
4132 int c, i;
4133 const char *out_fmt = "raw";
4134 const char *fmt = NULL;
4135 int64_t size = 0;
4136 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004137 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004138 struct DdInfo dd = {
4139 .flags = 0,
4140 .count = 0,
4141 };
4142 struct DdIo in = {
4143 .bsz = 512, /* Block size is by default 512 bytes */
4144 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004145 .buf = NULL,
4146 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004147 };
4148 struct DdIo out = {
4149 .bsz = 512,
4150 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004151 .buf = NULL,
4152 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004153 };
4154
4155 const struct DdOpts options[] = {
4156 { "bs", img_dd_bs, C_BS },
4157 { "count", img_dd_count, C_COUNT },
4158 { "if", img_dd_if, C_IF },
4159 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004160 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004161 { NULL, NULL, 0 }
4162 };
4163 const struct option long_options[] = {
4164 { "help", no_argument, 0, 'h'},
4165 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004166 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004167 { 0, 0, 0, 0 }
4168 };
4169
Fam Zheng335e9932017-05-03 00:35:39 +08004170 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004171 if (c == EOF) {
4172 break;
4173 }
4174 switch (c) {
4175 case 'O':
4176 out_fmt = optarg;
4177 break;
4178 case 'f':
4179 fmt = optarg;
4180 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004181 case ':':
4182 missing_argument(argv[optind - 1]);
4183 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004184 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004185 unrecognized_option(argv[optind - 1]);
4186 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004187 case 'h':
4188 help();
4189 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004190 case 'U':
4191 force_share = true;
4192 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004193 case OPTION_IMAGE_OPTS:
4194 image_opts = true;
4195 break;
4196 }
4197 }
4198
4199 for (i = optind; i < argc; i++) {
4200 int j;
4201 arg = g_strdup(argv[i]);
4202
4203 tmp = strchr(arg, '=');
4204 if (tmp == NULL) {
4205 error_report("unrecognized operand %s", arg);
4206 ret = -1;
4207 goto out;
4208 }
4209
4210 *tmp++ = '\0';
4211
4212 for (j = 0; options[j].name != NULL; j++) {
4213 if (!strcmp(arg, options[j].name)) {
4214 break;
4215 }
4216 }
4217 if (options[j].name == NULL) {
4218 error_report("unrecognized operand %s", arg);
4219 ret = -1;
4220 goto out;
4221 }
4222
4223 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4224 ret = -1;
4225 goto out;
4226 }
4227 dd.flags |= options[j].flag;
4228 g_free(arg);
4229 arg = NULL;
4230 }
4231
4232 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4233 error_report("Must specify both input and output files");
4234 ret = -1;
4235 goto out;
4236 }
Fam Zheng335e9932017-05-03 00:35:39 +08004237 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4238 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004239
4240 if (!blk1) {
4241 ret = -1;
4242 goto out;
4243 }
4244
4245 drv = bdrv_find_format(out_fmt);
4246 if (!drv) {
4247 error_report("Unknown file format");
4248 ret = -1;
4249 goto out;
4250 }
4251 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4252
4253 if (!proto_drv) {
4254 error_report_err(local_err);
4255 ret = -1;
4256 goto out;
4257 }
4258 if (!drv->create_opts) {
4259 error_report("Format driver '%s' does not support image creation",
4260 drv->format_name);
4261 ret = -1;
4262 goto out;
4263 }
4264 if (!proto_drv->create_opts) {
4265 error_report("Protocol driver '%s' does not support image creation",
4266 proto_drv->format_name);
4267 ret = -1;
4268 goto out;
4269 }
4270 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4271 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4272
4273 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4274
4275 size = blk_getlength(blk1);
4276 if (size < 0) {
4277 error_report("Failed to get size for '%s'", in.filename);
4278 ret = -1;
4279 goto out;
4280 }
4281
4282 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4283 dd.count * in.bsz < size) {
4284 size = dd.count * in.bsz;
4285 }
4286
Reda Sallahif7c15532016-08-10 16:16:09 +02004287 /* Overflow means the specified offset is beyond input image's size */
4288 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4289 size < in.bsz * in.offset)) {
4290 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4291 } else {
4292 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4293 size - in.bsz * in.offset, &error_abort);
4294 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004295
4296 ret = bdrv_create(drv, out.filename, opts, &local_err);
4297 if (ret < 0) {
4298 error_reportf_err(local_err,
4299 "%s: error while creating output image: ",
4300 out.filename);
4301 ret = -1;
4302 goto out;
4303 }
4304
4305 blk2 = img_open(image_opts, out.filename, out_fmt, BDRV_O_RDWR,
Fam Zheng335e9932017-05-03 00:35:39 +08004306 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004307
4308 if (!blk2) {
4309 ret = -1;
4310 goto out;
4311 }
4312
Reda Sallahif7c15532016-08-10 16:16:09 +02004313 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4314 size < in.offset * in.bsz)) {
4315 /* We give a warning if the skip option is bigger than the input
4316 * size and create an empty output disk image (i.e. like dd(1)).
4317 */
4318 error_report("%s: cannot skip to specified offset", in.filename);
4319 in_pos = size;
4320 } else {
4321 in_pos = in.offset * in.bsz;
4322 }
4323
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004324 in.buf = g_new(uint8_t, in.bsz);
4325
Reda Sallahif7c15532016-08-10 16:16:09 +02004326 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004327 int in_ret, out_ret;
4328
4329 if (in_pos + in.bsz > size) {
4330 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4331 } else {
4332 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4333 }
4334 if (in_ret < 0) {
4335 error_report("error while reading from input image file: %s",
4336 strerror(-in_ret));
4337 ret = -1;
4338 goto out;
4339 }
4340 in_pos += in_ret;
4341
4342 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4343
4344 if (out_ret < 0) {
4345 error_report("error while writing to output image file: %s",
4346 strerror(-out_ret));
4347 ret = -1;
4348 goto out;
4349 }
4350 out_pos += out_ret;
4351 }
4352
4353out:
4354 g_free(arg);
4355 qemu_opts_del(opts);
4356 qemu_opts_free(create_opts);
4357 blk_unref(blk1);
4358 blk_unref(blk2);
4359 g_free(in.filename);
4360 g_free(out.filename);
4361 g_free(in.buf);
4362 g_free(out.buf);
4363
4364 if (ret) {
4365 return 1;
4366 }
4367 return 0;
4368}
4369
Kevin Wolfb6133b82014-08-05 14:17:13 +02004370
Anthony Liguoric227f092009-10-01 16:12:16 -05004371static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004372#define DEF(option, callback, arg_string) \
4373 { option, callback },
4374#include "qemu-img-cmds.h"
4375#undef DEF
4376#undef GEN_DOCS
4377 { NULL, NULL, },
4378};
4379
bellardea2384d2004-08-01 21:59:26 +00004380int main(int argc, char **argv)
4381{
Anthony Liguoric227f092009-10-01 16:12:16 -05004382 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004383 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004384 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004385 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004386 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004387 static const struct option long_options[] = {
4388 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004389 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004390 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004391 {0, 0, 0, 0}
4392 };
bellardea2384d2004-08-01 21:59:26 +00004393
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004394#ifdef CONFIG_POSIX
4395 signal(SIGPIPE, SIG_IGN);
4396#endif
4397
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004398 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004399 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004400 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004401
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004402 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004403 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004404 exit(EXIT_FAILURE);
4405 }
4406
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004407 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004408
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004409 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004410 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004411 if (argc < 2) {
4412 error_exit("Not enough arguments");
4413 }
Stuart Brady153859b2009-06-07 00:42:17 +01004414
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004415 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004416 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004417 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004418
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004419 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004420 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004421 case ':':
4422 missing_argument(argv[optind - 1]);
4423 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08004424 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004425 unrecognized_option(argv[optind - 1]);
4426 return 0;
4427 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03004428 help();
4429 return 0;
4430 case 'V':
4431 printf(QEMU_IMG_VERSION);
4432 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004433 case 'T':
4434 g_free(trace_file);
4435 trace_file = trace_opt_parse(optarg);
4436 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004437 }
bellardea2384d2004-08-01 21:59:26 +00004438 }
Stuart Brady153859b2009-06-07 00:42:17 +01004439
Denis V. Lunev10985132016-06-17 17:44:13 +03004440 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004441
Denis V. Lunev10985132016-06-17 17:44:13 +03004442 /* reset getopt_long scanning */
4443 argc -= optind;
4444 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004445 return 0;
4446 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004447 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004448 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004449
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004450 if (!trace_init_backends()) {
4451 exit(1);
4452 }
4453 trace_init_file(trace_file);
4454 qemu_set_log(LOG_TRACE);
4455
Denis V. Lunev10985132016-06-17 17:44:13 +03004456 /* find the command */
4457 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4458 if (!strcmp(cmdname, cmd->name)) {
4459 return cmd->handler(argc, argv);
4460 }
4461 }
Jeff Cody7db16892014-04-25 17:02:32 -04004462
Stuart Brady153859b2009-06-07 00:42:17 +01004463 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004464 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004465}