blob: 704488484d8c43890a67deaa1d5d6ed32a50abfc [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"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020031#include "qemu/cutils.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000032#include "qemu/config-file.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010033#include "qemu/option.h"
34#include "qemu/error-report.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030035#include "qemu/log.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000036#include "qom/object_interfaces.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/sysemu.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020038#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010039#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020040#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080041#include "block/qapi.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010042#include "crypto/init.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030043#include "trace/control.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020044#include <getopt.h>
bellarde8445332006-06-14 15:32:10 +000045
Don Slutz61979a62015-01-09 10:17:35 -050046#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
Thomas Huth0781dd62016-10-05 11:54:44 +020047 "\n" QEMU_COPYRIGHT "\n"
Jeff Cody5f6979c2014-04-28 14:37:18 -040048
Anthony Liguoric227f092009-10-01 16:12:16 -050049typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010050 const char *name;
51 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050052} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010053
Federico Simoncelli8599ea42013-01-28 06:59:47 -050054enum {
55 OPTION_OUTPUT = 256,
56 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000057 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000058 OPTION_IMAGE_OPTS = 259,
Kevin Wolfb6495fa2015-07-10 18:09:18 +020059 OPTION_PATTERN = 260,
Kevin Wolf55d539c2016-06-03 13:59:41 +020060 OPTION_FLUSH_INTERVAL = 261,
61 OPTION_NO_DRAIN = 262,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050062};
63
64typedef enum OutputFormat {
65 OFORMAT_JSON,
66 OFORMAT_HUMAN,
67} OutputFormat;
68
Kevin Wolfe6996142016-03-15 13:03:11 +010069/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040070#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000071
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010072static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000073{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010074 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000075}
76
Fam Zhengac1307a2014-04-22 13:36:11 +080077static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
78{
79 va_list ap;
80
81 error_printf("qemu-img: ");
82
83 va_start(ap, fmt);
84 error_vprintf(fmt, ap);
85 va_end(ap);
86
87 error_printf("\nTry 'qemu-img --help' for more information\n");
88 exit(EXIT_FAILURE);
89}
90
Stefan Hajnoczic9192972017-03-17 18:45:41 +080091static void QEMU_NORETURN missing_argument(const char *option)
92{
93 error_exit("missing argument for option '%s'", option);
94}
95
96static void QEMU_NORETURN unrecognized_option(const char *option)
97{
98 error_exit("unrecognized option '%s'", option);
99}
100
blueswir1d2c639d2009-01-24 18:19:25 +0000101/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +0800102static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +0000103{
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100104 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -0400105 QEMU_IMG_VERSION
Denis V. Lunev10985132016-06-17 17:44:13 +0300106 "usage: qemu-img [standard options] command [command options]\n"
malc3f020d72010-02-08 12:04:56 +0300107 "QEMU disk image utility\n"
108 "\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300109 " '-h', '--help' display this help and exit\n"
110 " '-V', '--version' output version information and exit\n"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +0300111 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
112 " specify tracing options\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300113 "\n"
malc3f020d72010-02-08 12:04:56 +0300114 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100115#define DEF(option, callback, arg_string) \
116 " " arg_string "\n"
117#include "qemu-img-cmds.h"
118#undef DEF
119#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +0300120 "\n"
121 "Command parameters:\n"
122 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000123 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
124 " manual page for a description of the object properties. The most common\n"
125 " object type is a 'secret', which is used to supply passwords and/or\n"
126 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300127 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400128 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800129 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
130 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100131 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
132 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300133 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200134 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
135 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
136 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300137 " 'output_filename' is the destination disk image filename\n"
138 " 'output_fmt' is the destination format\n"
139 " 'options' is a comma separated list of format specific options in a\n"
140 " name=value format. Use -o ? for an overview of the options supported by the\n"
141 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800142 " 'snapshot_param' is param used for internal snapshot, format\n"
143 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
144 " '[ID_OR_NAME]'\n"
145 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
146 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300147 " '-c' indicates that target image must be compressed (qcow format only)\n"
148 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
149 " match exactly. The image doesn't need a working backing file before\n"
150 " rebasing in this case (useful for renaming the backing file)\n"
151 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200152 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100153 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200154 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
155 " contain only zeros for qemu-img to create a sparse image during\n"
156 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
157 " unallocated or zero sectors, and the destination image will always be\n"
158 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200159 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100160 " '-n' skips the target volume creation (useful if the volume is created\n"
161 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300162 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200163 "Parameters to check subcommand:\n"
164 " '-r' tries to repair any inconsistencies that are found during the check.\n"
165 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
166 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200167 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200168 "\n"
Peter Lieven2d9187b2017-02-28 13:40:07 +0100169 "Parameters to convert subcommand:\n"
170 " '-m' specifies how many coroutines work in parallel during the convert\n"
171 " process (defaults to 8)\n"
172 " '-W' allow to write to the target out of order rather than sequential\n"
173 "\n"
malc3f020d72010-02-08 12:04:56 +0300174 "Parameters to snapshot subcommand:\n"
175 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
176 " '-a' applies a snapshot (revert disk to saved state)\n"
177 " '-c' creates a snapshot\n"
178 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100179 " '-l' lists all snapshots in the given image\n"
180 "\n"
181 "Parameters to compare subcommand:\n"
182 " '-f' first image format\n"
183 " '-F' second image format\n"
Reda Sallahi86ce1f62016-08-10 04:43:12 +0200184 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
185 "\n"
186 "Parameters to dd subcommand:\n"
187 " 'bs=BYTES' read and write up to BYTES bytes at a time "
188 "(default: 512)\n"
189 " 'count=N' copy only N input blocks\n"
190 " 'if=FILE' read from FILE\n"
Reda Sallahif7c15532016-08-10 16:16:09 +0200191 " 'of=FILE' write to FILE\n"
192 " 'skip=N' skip N bs-sized blocks at the start of input\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100193
194 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100195 bdrv_iterate_format(format_print, NULL);
bellardea2384d2004-08-01 21:59:26 +0000196 printf("\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800197 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000198}
199
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000200static QemuOptsList qemu_object_opts = {
201 .name = "object",
202 .implied_opt_name = "qom-type",
203 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
204 .desc = {
205 { }
206 },
207};
208
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000209static QemuOptsList qemu_source_opts = {
210 .name = "source",
211 .implied_opt_name = "file",
212 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
213 .desc = {
214 { }
215 },
216};
217
Stefan Weil7c30f652013-06-16 17:01:05 +0200218static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100219{
220 int ret = 0;
221 if (!quiet) {
222 va_list args;
223 va_start(args, fmt);
224 ret = vprintf(fmt, args);
225 va_end(args);
226 }
227 return ret;
228}
229
bellardea2384d2004-08-01 21:59:26 +0000230
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100231static int print_block_option_help(const char *filename, const char *fmt)
232{
233 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800234 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500235 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100236
237 /* Find driver and parse its options */
238 drv = bdrv_find_format(fmt);
239 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100240 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100241 return 1;
242 }
243
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800244 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100245 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500246 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100247 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100248 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800249 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100250 return 1;
251 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800252 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100253 }
254
Chunyan Liu83d05212014-06-05 17:20:51 +0800255 qemu_opts_print_help(create_opts);
256 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100257 return 0;
258}
259
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000260
261static int img_open_password(BlockBackend *blk, const char *filename,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000262 int flags, bool quiet)
bellard75c23802004-08-27 21:28:58 +0000263{
264 BlockDriverState *bs;
bellard75c23802004-08-27 21:28:58 +0000265 char password[256];
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000266
267 bs = blk_bs(blk);
Daniel P. Berrange4ef130f2016-03-21 14:11:43 +0000268 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs) &&
269 !(flags & BDRV_O_NO_IO)) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000270 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
271 if (qemu_read_password(password, sizeof(password)) < 0) {
272 error_report("No password given");
273 return -1;
274 }
275 if (bdrv_set_key(bs, password) < 0) {
276 error_report("invalid password");
277 return -1;
278 }
279 }
280 return 0;
281}
282
283
Max Reitzefaa7c42016-03-16 19:54:38 +0100284static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100285 QemuOpts *opts, int flags, bool writethrough,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000286 bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000287{
288 QDict *options;
289 Error *local_err = NULL;
290 BlockBackend *blk;
291 options = qemu_opts_to_qdict(opts, NULL);
Max Reitzefaa7c42016-03-16 19:54:38 +0100292 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000293 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100294 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000295 return NULL;
296 }
Kevin Wolfce099542016-03-15 13:01:04 +0100297 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000298
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000299 if (img_open_password(blk, optstr, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000300 blk_unref(blk);
301 return NULL;
302 }
303 return blk;
304}
305
Max Reitzefaa7c42016-03-16 19:54:38 +0100306static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000307 const char *fmt, int flags,
Kevin Wolfce099542016-03-15 13:01:04 +0100308 bool writethrough, bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000309{
310 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200311 Error *local_err = NULL;
Max Reitz5bd31322015-02-05 13:58:16 -0500312 QDict *options = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100313
bellard75c23802004-08-27 21:28:58 +0000314 if (fmt) {
Max Reitz5bd31322015-02-05 13:58:16 -0500315 options = qdict_new();
316 qdict_put(options, "driver", qstring_from_str(fmt));
bellard75c23802004-08-27 21:28:58 +0000317 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100318
Max Reitzefaa7c42016-03-16 19:54:38 +0100319 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500320 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100321 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000322 return NULL;
bellard75c23802004-08-27 21:28:58 +0000323 }
Kevin Wolfce099542016-03-15 13:01:04 +0100324 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100325
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000326 if (img_open_password(blk, filename, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000327 blk_unref(blk);
328 return NULL;
bellard75c23802004-08-27 21:28:58 +0000329 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200330 return blk;
bellard75c23802004-08-27 21:28:58 +0000331}
332
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000333
Max Reitzefaa7c42016-03-16 19:54:38 +0100334static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000335 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100336 const char *fmt, int flags, bool writethrough,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000337 bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000338{
339 BlockBackend *blk;
340 if (image_opts) {
341 QemuOpts *opts;
342 if (fmt) {
343 error_report("--image-opts and --format are mutually exclusive");
344 return NULL;
345 }
346 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
347 filename, true);
348 if (!opts) {
349 return NULL;
350 }
Kevin Wolfce099542016-03-15 13:01:04 +0100351 blk = img_open_opts(filename, opts, flags, writethrough, quiet);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000352 } else {
Kevin Wolfce099542016-03-15 13:01:04 +0100353 blk = img_open_file(filename, fmt, flags, writethrough, quiet);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000354 }
355 return blk;
356}
357
358
Chunyan Liu83d05212014-06-05 17:20:51 +0800359static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100360 const char *base_filename,
361 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200362{
Markus Armbruster6750e792015-02-12 17:43:08 +0100363 Error *err = NULL;
364
Kevin Wolfefa84d42009-05-18 16:42:12 +0200365 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100366 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100367 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100368 error_report("Backing file not supported for file format '%s'",
369 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100370 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900371 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200372 }
373 }
374 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100375 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100376 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100377 error_report("Backing file format not supported for file "
378 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100379 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900380 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200381 }
382 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900383 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200384}
385
Markus Armbruster606caa02017-02-21 21:14:04 +0100386static int64_t cvtnum(const char *s)
387{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100388 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100389 uint64_t value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100390
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100391 err = qemu_strtosz(s, NULL, &value);
392 if (err < 0) {
393 return err;
394 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100395 if (value > INT64_MAX) {
396 return -ERANGE;
397 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100398 return value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100399}
400
bellardea2384d2004-08-01 21:59:26 +0000401static int img_create(int argc, char **argv)
402{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200403 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100404 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000405 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000406 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000407 const char *filename;
408 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200409 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200410 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100411 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000412
bellardea2384d2004-08-01 21:59:26 +0000413 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000414 static const struct option long_options[] = {
415 {"help", no_argument, 0, 'h'},
416 {"object", required_argument, 0, OPTION_OBJECT},
417 {0, 0, 0, 0}
418 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800419 c = getopt_long(argc, argv, ":F:b:f:he6o:q",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000420 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100421 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000422 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100423 }
bellardea2384d2004-08-01 21:59:26 +0000424 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800425 case ':':
426 missing_argument(argv[optind - 1]);
427 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100428 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800429 unrecognized_option(argv[optind - 1]);
430 break;
bellardea2384d2004-08-01 21:59:26 +0000431 case 'h':
432 help();
433 break;
aliguori9230eaf2009-03-28 17:55:19 +0000434 case 'F':
435 base_fmt = optarg;
436 break;
bellardea2384d2004-08-01 21:59:26 +0000437 case 'b':
438 base_filename = optarg;
439 break;
440 case 'f':
441 fmt = optarg;
442 break;
443 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200444 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100445 "encryption\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100446 goto fail;
thsd8871c52007-10-24 16:11:42 +0000447 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200448 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100449 "compat6\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100450 goto fail;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200451 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100452 if (!is_valid_option_list(optarg)) {
453 error_report("Invalid option list: %s", optarg);
454 goto fail;
455 }
456 if (!options) {
457 options = g_strdup(optarg);
458 } else {
459 char *old_options = options;
460 options = g_strdup_printf("%s,%s", options, optarg);
461 g_free(old_options);
462 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200463 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100464 case 'q':
465 quiet = true;
466 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000467 case OPTION_OBJECT: {
468 QemuOpts *opts;
469 opts = qemu_opts_parse_noisily(&qemu_object_opts,
470 optarg, true);
471 if (!opts) {
472 goto fail;
473 }
474 } break;
bellardea2384d2004-08-01 21:59:26 +0000475 }
476 }
aliguori9230eaf2009-03-28 17:55:19 +0000477
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900478 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100479 filename = (optind < argc) ? argv[optind] : NULL;
480 if (options && has_help_option(options)) {
481 g_free(options);
482 return print_block_option_help(filename, fmt);
483 }
484
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100485 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800486 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100487 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100488 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900489
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000490 if (qemu_opts_foreach(&qemu_object_opts,
491 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200492 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000493 goto fail;
494 }
495
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100496 /* Get image size, if specified */
497 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100498 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100499
500 sval = cvtnum(argv[optind++]);
501 if (sval < 0) {
liguang79443392012-12-17 09:49:23 +0800502 if (sval == -ERANGE) {
503 error_report("Image size must be less than 8 EiB!");
504 } else {
505 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200506 "G, T, P or E suffixes for ");
507 error_report("kilobytes, megabytes, gigabytes, terabytes, "
508 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800509 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100510 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100511 }
512 img_size = (uint64_t)sval;
513 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200514 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800515 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200516 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100517
Luiz Capitulino9b375252012-11-30 10:52:05 -0200518 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Fam Zheng92172832017-04-21 20:27:01 +0800519 options, img_size, 0, quiet, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100520 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100521 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100522 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900523 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200524
Kevin Wolf77386bf2014-02-21 16:24:04 +0100525 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000526 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100527
528fail:
529 g_free(options);
530 return 1;
bellardea2384d2004-08-01 21:59:26 +0000531}
532
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100533static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500534{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500535 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500536 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100537 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600538
539 visit_type_ImageCheck(v, NULL, &check, &error_abort);
540 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500541 str = qobject_to_json_pretty(obj);
542 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100543 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500544 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600545 visit_free(v);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500546 QDECREF(str);
547}
548
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100549static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500550{
551 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100552 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500553 } else {
554 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100555 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
556 "Data may be corrupted, or further writes to the image "
557 "may corrupt it.\n",
558 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500559 }
560
561 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100562 qprintf(quiet,
563 "\n%" PRId64 " leaked clusters were found on the image.\n"
564 "This means waste of disk space, but no harm to data.\n",
565 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500566 }
567
568 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100569 qprintf(quiet,
570 "\n%" PRId64
571 " internal errors have occurred during the check.\n",
572 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500573 }
574 }
575
576 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100577 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
578 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
579 check->allocated_clusters, check->total_clusters,
580 check->allocated_clusters * 100.0 / check->total_clusters,
581 check->fragmented_clusters * 100.0 / check->allocated_clusters,
582 check->compressed_clusters * 100.0 /
583 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500584 }
585
586 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100587 qprintf(quiet,
588 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500589 }
590}
591
592static int collect_image_check(BlockDriverState *bs,
593 ImageCheck *check,
594 const char *filename,
595 const char *fmt,
596 int fix)
597{
598 int ret;
599 BdrvCheckResult result;
600
601 ret = bdrv_check(bs, &result, fix);
602 if (ret < 0) {
603 return ret;
604 }
605
606 check->filename = g_strdup(filename);
607 check->format = g_strdup(bdrv_get_format_name(bs));
608 check->check_errors = result.check_errors;
609 check->corruptions = result.corruptions;
610 check->has_corruptions = result.corruptions != 0;
611 check->leaks = result.leaks;
612 check->has_leaks = result.leaks != 0;
613 check->corruptions_fixed = result.corruptions_fixed;
614 check->has_corruptions_fixed = result.corruptions != 0;
615 check->leaks_fixed = result.leaks_fixed;
616 check->has_leaks_fixed = result.leaks != 0;
617 check->image_end_offset = result.image_end_offset;
618 check->has_image_end_offset = result.image_end_offset != 0;
619 check->total_clusters = result.bfi.total_clusters;
620 check->has_total_clusters = result.bfi.total_clusters != 0;
621 check->allocated_clusters = result.bfi.allocated_clusters;
622 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
623 check->fragmented_clusters = result.bfi.fragmented_clusters;
624 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100625 check->compressed_clusters = result.bfi.compressed_clusters;
626 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500627
628 return 0;
629}
630
Kevin Wolfe076f332010-06-29 11:43:13 +0200631/*
632 * Checks an image for consistency. Exit codes:
633 *
Max Reitzd6635c42014-06-02 22:15:21 +0200634 * 0 - Check completed, image is good
635 * 1 - Check not completed because of internal errors
636 * 2 - Check completed, image is corrupted
637 * 3 - Check completed, image has leaked clusters, but is good otherwise
638 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200639 */
aliguori15859692009-04-21 23:11:53 +0000640static int img_check(int argc, char **argv)
641{
642 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500643 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200644 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200645 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000646 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200647 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100648 int flags = BDRV_O_CHECK;
649 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200650 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100651 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000652 bool image_opts = false;
aliguori15859692009-04-21 23:11:53 +0000653
654 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500655 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200656 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100657
aliguori15859692009-04-21 23:11:53 +0000658 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500659 int option_index = 0;
660 static const struct option long_options[] = {
661 {"help", no_argument, 0, 'h'},
662 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530663 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500664 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000665 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000666 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500667 {0, 0, 0, 0}
668 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800669 c = getopt_long(argc, argv, ":hf:r:T:q",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500670 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100671 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000672 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100673 }
aliguori15859692009-04-21 23:11:53 +0000674 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800675 case ':':
676 missing_argument(argv[optind - 1]);
677 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100678 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800679 unrecognized_option(argv[optind - 1]);
680 break;
aliguori15859692009-04-21 23:11:53 +0000681 case 'h':
682 help();
683 break;
684 case 'f':
685 fmt = optarg;
686 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200687 case 'r':
688 flags |= BDRV_O_RDWR;
689
690 if (!strcmp(optarg, "leaks")) {
691 fix = BDRV_FIX_LEAKS;
692 } else if (!strcmp(optarg, "all")) {
693 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
694 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800695 error_exit("Unknown option value for -r "
696 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200697 }
698 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500699 case OPTION_OUTPUT:
700 output = optarg;
701 break;
Max Reitz40055952014-07-22 22:58:42 +0200702 case 'T':
703 cache = optarg;
704 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100705 case 'q':
706 quiet = true;
707 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000708 case OPTION_OBJECT: {
709 QemuOpts *opts;
710 opts = qemu_opts_parse_noisily(&qemu_object_opts,
711 optarg, true);
712 if (!opts) {
713 return 1;
714 }
715 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000716 case OPTION_IMAGE_OPTS:
717 image_opts = true;
718 break;
aliguori15859692009-04-21 23:11:53 +0000719 }
720 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200721 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800722 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100723 }
aliguori15859692009-04-21 23:11:53 +0000724 filename = argv[optind++];
725
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500726 if (output && !strcmp(output, "json")) {
727 output_format = OFORMAT_JSON;
728 } else if (output && !strcmp(output, "human")) {
729 output_format = OFORMAT_HUMAN;
730 } else if (output) {
731 error_report("--output must be used with human or json as argument.");
732 return 1;
733 }
734
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000735 if (qemu_opts_foreach(&qemu_object_opts,
736 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200737 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000738 return 1;
739 }
740
Kevin Wolfce099542016-03-15 13:01:04 +0100741 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200742 if (ret < 0) {
743 error_report("Invalid source cache option: %s", cache);
744 return 1;
745 }
746
Kevin Wolfce099542016-03-15 13:01:04 +0100747 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200748 if (!blk) {
749 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900750 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200751 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500752
753 check = g_new0(ImageCheck, 1);
754 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200755
756 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200757 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200758 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500759 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200760 }
761
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500762 if (check->corruptions_fixed || check->leaks_fixed) {
763 int corruptions_fixed, leaks_fixed;
764
765 leaks_fixed = check->leaks_fixed;
766 corruptions_fixed = check->corruptions_fixed;
767
768 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100769 qprintf(quiet,
770 "The following inconsistencies were found and repaired:\n\n"
771 " %" PRId64 " leaked clusters\n"
772 " %" PRId64 " corruptions\n\n"
773 "Double checking the fixed image now...\n",
774 check->leaks_fixed,
775 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500776 }
777
778 ret = collect_image_check(bs, check, filename, fmt, 0);
779
780 check->leaks_fixed = leaks_fixed;
781 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200782 }
783
Max Reitz832390a2014-10-23 15:29:12 +0200784 if (!ret) {
785 switch (output_format) {
786 case OFORMAT_HUMAN:
787 dump_human_image_check(check, quiet);
788 break;
789 case OFORMAT_JSON:
790 dump_json_image_check(check, quiet);
791 break;
792 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500793 }
794
795 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200796 if (ret) {
797 error_report("Check failed: %s", strerror(-ret));
798 } else {
799 error_report("Check failed");
800 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500801 ret = 1;
802 goto fail;
803 }
804
805 if (check->corruptions) {
806 ret = 2;
807 } else if (check->leaks) {
808 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200809 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500810 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000811 }
812
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500813fail:
814 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200815 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500816 return ret;
aliguori15859692009-04-21 23:11:53 +0000817}
818
Max Reitzd4a32382014-10-24 15:57:37 +0200819typedef struct CommonBlockJobCBInfo {
820 BlockDriverState *bs;
821 Error **errp;
822} CommonBlockJobCBInfo;
823
824static void common_block_job_cb(void *opaque, int ret)
825{
826 CommonBlockJobCBInfo *cbi = opaque;
827
828 if (ret < 0) {
829 error_setg_errno(cbi->errp, -ret, "Block job failed");
830 }
Max Reitzd4a32382014-10-24 15:57:37 +0200831}
832
833static void run_block_job(BlockJob *job, Error **errp)
834{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200835 AioContext *aio_context = blk_get_aio_context(job->blk);
Max Reitzd4a32382014-10-24 15:57:37 +0200836
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100837 /* FIXME In error cases, the job simply goes away and we access a dangling
838 * pointer below. */
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200839 aio_context_acquire(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +0200840 do {
841 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500842 qemu_progress_print(job->len ?
843 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200844 } while (!job->ready);
845
846 block_job_complete_sync(job, errp);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200847 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200848
849 /* A block job may finish instantaneously without publishing any progress,
850 * so just signal completion here */
851 qemu_progress_print(100.f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200852}
853
bellardea2384d2004-08-01 21:59:26 +0000854static int img_commit(int argc, char **argv)
855{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400856 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200857 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200858 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200859 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100860 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200861 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100862 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200863 Error *local_err = NULL;
864 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000865 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200866 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000867
868 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400869 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200870 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000871 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000872 static const struct option long_options[] = {
873 {"help", no_argument, 0, 'h'},
874 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000875 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000876 {0, 0, 0, 0}
877 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800878 c = getopt_long(argc, argv, ":f:ht:b:dpq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000879 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100880 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000881 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100882 }
bellardea2384d2004-08-01 21:59:26 +0000883 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800884 case ':':
885 missing_argument(argv[optind - 1]);
886 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100887 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800888 unrecognized_option(argv[optind - 1]);
889 break;
bellardea2384d2004-08-01 21:59:26 +0000890 case 'h':
891 help();
892 break;
893 case 'f':
894 fmt = optarg;
895 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400896 case 't':
897 cache = optarg;
898 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200899 case 'b':
900 base = optarg;
901 /* -b implies -d */
902 drop = true;
903 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200904 case 'd':
905 drop = true;
906 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200907 case 'p':
908 progress = true;
909 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100910 case 'q':
911 quiet = true;
912 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000913 case OPTION_OBJECT: {
914 QemuOpts *opts;
915 opts = qemu_opts_parse_noisily(&qemu_object_opts,
916 optarg, true);
917 if (!opts) {
918 return 1;
919 }
920 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000921 case OPTION_IMAGE_OPTS:
922 image_opts = true;
923 break;
bellardea2384d2004-08-01 21:59:26 +0000924 }
925 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200926
927 /* Progress is not shown in Quiet mode */
928 if (quiet) {
929 progress = false;
930 }
931
Kevin Wolffc11eb22013-08-05 10:53:04 +0200932 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800933 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100934 }
bellardea2384d2004-08-01 21:59:26 +0000935 filename = argv[optind++];
936
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000937 if (qemu_opts_foreach(&qemu_object_opts,
938 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200939 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000940 return 1;
941 }
942
Max Reitz9a86fe42014-10-24 15:57:38 +0200943 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100944 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400945 if (ret < 0) {
946 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100947 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400948 }
949
Kevin Wolfce099542016-03-15 13:01:04 +0100950 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200951 if (!blk) {
952 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900953 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200954 bs = blk_bs(blk);
955
Max Reitz687fa1d2014-10-24 15:57:39 +0200956 qemu_progress_init(progress, 1.f);
957 qemu_progress_print(0.f, 100);
958
Max Reitz1b22bff2014-10-24 15:57:40 +0200959 if (base) {
960 base_bs = bdrv_find_backing_image(bs, base);
961 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +0100962 error_setg(&local_err,
963 "Did not find '%s' in the backing chain of '%s'",
964 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +0200965 goto done;
966 }
967 } else {
968 /* This is different from QMP, which by default uses the deepest file in
969 * the backing chain (i.e., the very base); however, the traditional
970 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +0200971 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +0200972 if (!base_bs) {
973 error_setg(&local_err, "Image does not have a backing file");
974 goto done;
975 }
bellardea2384d2004-08-01 21:59:26 +0000976 }
977
Max Reitzd4a32382014-10-24 15:57:37 +0200978 cbi = (CommonBlockJobCBInfo){
979 .errp = &local_err,
980 .bs = bs,
981 };
982
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200983 aio_context = bdrv_get_aio_context(bs);
984 aio_context_acquire(aio_context);
John Snow47970df2016-10-27 12:06:57 -0400985 commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
Kevin Wolf0db832f2017-02-20 18:10:05 +0100986 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +0800987 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200988 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +0200989 if (local_err) {
990 goto done;
991 }
992
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200993 /* When the block job completes, the BlockBackend reference will point to
994 * the old backing file. In order to avoid that the top image is already
995 * deleted, so we can still empty it afterwards, increment the reference
996 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +0200997 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200998 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200999 }
1000
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001001 job = block_job_get("commit");
1002 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001003 if (local_err) {
1004 goto unref_backing;
1005 }
1006
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001007 if (!drop && bs->drv->bdrv_make_empty) {
1008 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001009 if (ret) {
1010 error_setg_errno(&local_err, -ret, "Could not empty %s",
1011 filename);
1012 goto unref_backing;
1013 }
1014 }
1015
1016unref_backing:
1017 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001018 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001019 }
Max Reitzd4a32382014-10-24 15:57:37 +02001020
1021done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001022 qemu_progress_end();
1023
Markus Armbruster26f54e92014-10-07 13:59:04 +02001024 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001025
1026 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001027 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001028 return 1;
1029 }
Max Reitzd4a32382014-10-24 15:57:37 +02001030
1031 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001032 return 0;
1033}
1034
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001035/*
thsf58c7b32008-06-05 21:53:49 +00001036 * Returns true iff the first sector pointed to by 'buf' contains at least
1037 * a non-NUL byte.
1038 *
1039 * 'pnum' is set to the number of sectors (including and immediately following
1040 * the first one) that are known to be in the same allocated/unallocated state.
1041 */
bellardea2384d2004-08-01 21:59:26 +00001042static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1043{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001044 bool is_zero;
1045 int i;
bellardea2384d2004-08-01 21:59:26 +00001046
1047 if (n <= 0) {
1048 *pnum = 0;
1049 return 0;
1050 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001051 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001052 for(i = 1; i < n; i++) {
1053 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001054 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001055 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001056 }
bellardea2384d2004-08-01 21:59:26 +00001057 }
1058 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001059 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001060}
1061
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001062/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001063 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1064 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1065 * breaking up write requests for only small sparse areas.
1066 */
1067static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1068 int min)
1069{
1070 int ret;
1071 int num_checked, num_used;
1072
1073 if (n < min) {
1074 min = n;
1075 }
1076
1077 ret = is_allocated_sectors(buf, n, pnum);
1078 if (!ret) {
1079 return ret;
1080 }
1081
1082 num_used = *pnum;
1083 buf += BDRV_SECTOR_SIZE * *pnum;
1084 n -= *pnum;
1085 num_checked = num_used;
1086
1087 while (n > 0) {
1088 ret = is_allocated_sectors(buf, n, pnum);
1089
1090 buf += BDRV_SECTOR_SIZE * *pnum;
1091 n -= *pnum;
1092 num_checked += *pnum;
1093 if (ret) {
1094 num_used = num_checked;
1095 } else if (*pnum >= min) {
1096 break;
1097 }
1098 }
1099
1100 *pnum = num_used;
1101 return 1;
1102}
1103
1104/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001105 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1106 * buffers matches, non-zero otherwise.
1107 *
1108 * pnum is set to the number of sectors (including and immediately following
1109 * the first one) that are known to have the same comparison result
1110 */
1111static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1112 int *pnum)
1113{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001114 bool res;
1115 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001116
1117 if (n <= 0) {
1118 *pnum = 0;
1119 return 0;
1120 }
1121
1122 res = !!memcmp(buf1, buf2, 512);
1123 for(i = 1; i < n; i++) {
1124 buf1 += 512;
1125 buf2 += 512;
1126
1127 if (!!memcmp(buf1, buf2, 512) != res) {
1128 break;
1129 }
1130 }
1131
1132 *pnum = i;
1133 return res;
1134}
1135
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001136#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001137
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001138static int64_t sectors_to_bytes(int64_t sectors)
1139{
1140 return sectors << BDRV_SECTOR_BITS;
1141}
1142
1143static int64_t sectors_to_process(int64_t total, int64_t from)
1144{
1145 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1146}
1147
1148/*
1149 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1150 *
1151 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1152 * data and negative value on error.
1153 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001154 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001155 * @param sect_num: Number of first sector to check
1156 * @param sect_count: Number of sectors to check
1157 * @param filename: Name of disk file we are checking (logging purpose)
1158 * @param buffer: Allocated buffer for storing read data
1159 * @param quiet: Flag for quiet mode
1160 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001161static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001162 int sect_count, const char *filename,
1163 uint8_t *buffer, bool quiet)
1164{
1165 int pnum, ret = 0;
Eric Blake91669202016-05-06 10:26:43 -06001166 ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer,
1167 sect_count << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001168 if (ret < 0) {
1169 error_report("Error while reading offset %" PRId64 " of %s: %s",
1170 sectors_to_bytes(sect_num), filename, strerror(-ret));
1171 return ret;
1172 }
1173 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1174 if (ret || pnum != sect_count) {
1175 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1176 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1177 return 1;
1178 }
1179
1180 return 0;
1181}
1182
1183/*
1184 * Compares two images. Exit codes:
1185 *
1186 * 0 - Images are identical
1187 * 1 - Images differ
1188 * >1 - Error occurred
1189 */
1190static int img_compare(int argc, char **argv)
1191{
Max Reitz40055952014-07-22 22:58:42 +02001192 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001193 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001194 BlockDriverState *bs1, *bs2;
1195 int64_t total_sectors1, total_sectors2;
1196 uint8_t *buf1 = NULL, *buf2 = NULL;
1197 int pnum1, pnum2;
1198 int allocated1, allocated2;
1199 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1200 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001201 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001202 bool writethrough;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001203 int64_t total_sectors;
1204 int64_t sector_num = 0;
1205 int64_t nb_sectors;
1206 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001207 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001208 bool image_opts = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001209
Max Reitz40055952014-07-22 22:58:42 +02001210 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001211 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001212 static const struct option long_options[] = {
1213 {"help", no_argument, 0, 'h'},
1214 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001215 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001216 {0, 0, 0, 0}
1217 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001218 c = getopt_long(argc, argv, ":hf:F:T:pqs",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001219 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001220 if (c == -1) {
1221 break;
1222 }
1223 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001224 case ':':
1225 missing_argument(argv[optind - 1]);
1226 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001227 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001228 unrecognized_option(argv[optind - 1]);
1229 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001230 case 'h':
1231 help();
1232 break;
1233 case 'f':
1234 fmt1 = optarg;
1235 break;
1236 case 'F':
1237 fmt2 = optarg;
1238 break;
Max Reitz40055952014-07-22 22:58:42 +02001239 case 'T':
1240 cache = optarg;
1241 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001242 case 'p':
1243 progress = true;
1244 break;
1245 case 'q':
1246 quiet = true;
1247 break;
1248 case 's':
1249 strict = true;
1250 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001251 case OPTION_OBJECT: {
1252 QemuOpts *opts;
1253 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1254 optarg, true);
1255 if (!opts) {
1256 ret = 2;
1257 goto out4;
1258 }
1259 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001260 case OPTION_IMAGE_OPTS:
1261 image_opts = true;
1262 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001263 }
1264 }
1265
1266 /* Progress is not shown in Quiet mode */
1267 if (quiet) {
1268 progress = false;
1269 }
1270
1271
Kevin Wolffc11eb22013-08-05 10:53:04 +02001272 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001273 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001274 }
1275 filename1 = argv[optind++];
1276 filename2 = argv[optind++];
1277
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001278 if (qemu_opts_foreach(&qemu_object_opts,
1279 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001280 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001281 ret = 2;
1282 goto out4;
1283 }
1284
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001285 /* Initialize before goto out */
1286 qemu_progress_init(progress, 2.0);
1287
Kevin Wolfce099542016-03-15 13:01:04 +01001288 flags = 0;
1289 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001290 if (ret < 0) {
1291 error_report("Invalid source cache option: %s", cache);
1292 ret = 2;
1293 goto out3;
1294 }
1295
Kevin Wolfce099542016-03-15 13:01:04 +01001296 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001297 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001298 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001299 goto out3;
1300 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001301
Kevin Wolfce099542016-03-15 13:01:04 +01001302 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001303 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001304 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001305 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001306 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001307 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001308 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001309
Max Reitzf1d3cd72015-02-05 13:58:18 -05001310 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1311 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1312 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001313 if (total_sectors1 < 0) {
1314 error_report("Can't get size of %s: %s",
1315 filename1, strerror(-total_sectors1));
1316 ret = 4;
1317 goto out;
1318 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001319 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001320 if (total_sectors2 < 0) {
1321 error_report("Can't get size of %s: %s",
1322 filename2, strerror(-total_sectors2));
1323 ret = 4;
1324 goto out;
1325 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001326 total_sectors = MIN(total_sectors1, total_sectors2);
1327 progress_base = MAX(total_sectors1, total_sectors2);
1328
1329 qemu_progress_print(0, 100);
1330
1331 if (strict && total_sectors1 != total_sectors2) {
1332 ret = 1;
1333 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1334 goto out;
1335 }
1336
1337 for (;;) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001338 int64_t status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001339 BlockDriverState *file;
1340
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001341 nb_sectors = sectors_to_process(total_sectors, sector_num);
1342 if (nb_sectors <= 0) {
1343 break;
1344 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001345 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1346 total_sectors1 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001347 &pnum1, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001348 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001349 ret = 3;
1350 error_report("Sector allocation test failed for %s", filename1);
1351 goto out;
1352 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001353 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001354
Fam Zheng25ad8e62016-01-13 16:37:41 +08001355 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1356 total_sectors2 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001357 &pnum2, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001358 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001359 ret = 3;
1360 error_report("Sector allocation test failed for %s", filename2);
1361 goto out;
1362 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001363 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1364 if (pnum1) {
1365 nb_sectors = MIN(nb_sectors, pnum1);
1366 }
1367 if (pnum2) {
1368 nb_sectors = MIN(nb_sectors, pnum2);
1369 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001370
Fam Zheng25ad8e62016-01-13 16:37:41 +08001371 if (strict) {
1372 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1373 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1374 ret = 1;
1375 qprintf(quiet, "Strict mode: Offset %" PRId64
1376 " block status mismatch!\n",
1377 sectors_to_bytes(sector_num));
1378 goto out;
1379 }
1380 }
1381 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1382 nb_sectors = MIN(pnum1, pnum2);
1383 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001384 if (allocated1) {
Eric Blake91669202016-05-06 10:26:43 -06001385 ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1,
1386 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001387 if (ret < 0) {
1388 error_report("Error while reading offset %" PRId64 " of %s:"
1389 " %s", sectors_to_bytes(sector_num), filename1,
1390 strerror(-ret));
1391 ret = 4;
1392 goto out;
1393 }
Eric Blake91669202016-05-06 10:26:43 -06001394 ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2,
1395 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001396 if (ret < 0) {
1397 error_report("Error while reading offset %" PRId64
1398 " of %s: %s", sectors_to_bytes(sector_num),
1399 filename2, strerror(-ret));
1400 ret = 4;
1401 goto out;
1402 }
1403 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1404 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001405 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1406 sectors_to_bytes(
1407 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001408 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001409 goto out;
1410 }
1411 }
1412 } else {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001413
1414 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001415 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001416 filename1, buf1, quiet);
1417 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001418 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001419 filename2, buf1, quiet);
1420 }
1421 if (ret) {
1422 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001423 error_report("Error while reading offset %" PRId64 ": %s",
1424 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001425 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001426 }
1427 goto out;
1428 }
1429 }
1430 sector_num += nb_sectors;
1431 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1432 }
1433
1434 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001435 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001436 int64_t total_sectors_over;
1437 const char *filename_over;
1438
1439 qprintf(quiet, "Warning: Image size mismatch!\n");
1440 if (total_sectors1 > total_sectors2) {
1441 total_sectors_over = total_sectors1;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001442 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001443 filename_over = filename1;
1444 } else {
1445 total_sectors_over = total_sectors2;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001446 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001447 filename_over = filename2;
1448 }
1449
1450 for (;;) {
1451 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1452 if (nb_sectors <= 0) {
1453 break;
1454 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001455 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001456 nb_sectors, &pnum);
1457 if (ret < 0) {
1458 ret = 3;
1459 error_report("Sector allocation test failed for %s",
1460 filename_over);
1461 goto out;
1462
1463 }
1464 nb_sectors = pnum;
1465 if (ret) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001466 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001467 filename_over, buf1, quiet);
1468 if (ret) {
1469 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001470 error_report("Error while reading offset %" PRId64
1471 " of %s: %s", sectors_to_bytes(sector_num),
1472 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001473 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001474 }
1475 goto out;
1476 }
1477 }
1478 sector_num += nb_sectors;
1479 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1480 }
1481 }
1482
1483 qprintf(quiet, "Images are identical.\n");
1484 ret = 0;
1485
1486out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001487 qemu_vfree(buf1);
1488 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001489 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001490out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001491 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001492out3:
1493 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001494out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001495 return ret;
1496}
1497
Kevin Wolf690c7302015-03-19 13:33:32 +01001498enum ImgConvertBlockStatus {
1499 BLK_DATA,
1500 BLK_ZERO,
1501 BLK_BACKING_FILE,
1502};
1503
Peter Lieven2d9187b2017-02-28 13:40:07 +01001504#define MAX_COROUTINES 16
1505
Kevin Wolf690c7302015-03-19 13:33:32 +01001506typedef struct ImgConvertState {
1507 BlockBackend **src;
1508 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001509 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001510 int64_t total_sectors;
1511 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001512 int64_t allocated_done;
1513 int64_t sector_num;
1514 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001515 enum ImgConvertBlockStatus status;
1516 int64_t sector_next_status;
1517 BlockBackend *target;
1518 bool has_zero_init;
1519 bool compressed;
1520 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001521 bool wr_in_order;
Kevin Wolf690c7302015-03-19 13:33:32 +01001522 int min_sparse;
1523 size_t cluster_sectors;
1524 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001525 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001526 int running_coroutines;
1527 Coroutine *co[MAX_COROUTINES];
1528 int64_t wait_sector_num[MAX_COROUTINES];
1529 CoMutex lock;
1530 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001531} ImgConvertState;
1532
Peter Lieven2d9187b2017-02-28 13:40:07 +01001533static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1534 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001535{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001536 *src_cur = 0;
1537 *src_cur_offset = 0;
1538 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1539 *src_cur_offset += s->src_sectors[*src_cur];
1540 (*src_cur)++;
1541 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001542 }
1543}
1544
1545static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1546{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001547 int64_t ret, src_cur_offset;
1548 int n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001549
Peter Lieven2d9187b2017-02-28 13:40:07 +01001550 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001551
1552 assert(s->total_sectors > sector_num);
1553 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1554
1555 if (s->sector_next_status <= sector_num) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08001556 BlockDriverState *file;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001557 ret = bdrv_get_block_status(blk_bs(s->src[src_cur]),
1558 sector_num - src_cur_offset,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001559 n, &n, &file);
Kevin Wolf690c7302015-03-19 13:33:32 +01001560 if (ret < 0) {
1561 return ret;
1562 }
1563
1564 if (ret & BDRV_BLOCK_ZERO) {
1565 s->status = BLK_ZERO;
1566 } else if (ret & BDRV_BLOCK_DATA) {
1567 s->status = BLK_DATA;
1568 } else if (!s->target_has_backing) {
1569 /* Without a target backing file we must copy over the contents of
1570 * the backing file as well. */
Ren Kimura263a6f42016-04-28 01:04:58 +09001571 /* Check block status of the backing file chain to avoid
Kevin Wolf690c7302015-03-19 13:33:32 +01001572 * needlessly reading zeroes and limiting the iteration to the
1573 * buffer size */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001574 ret = bdrv_get_block_status_above(blk_bs(s->src[src_cur]), NULL,
1575 sector_num - src_cur_offset,
Ren Kimura263a6f42016-04-28 01:04:58 +09001576 n, &n, &file);
1577 if (ret < 0) {
1578 return ret;
1579 }
1580
1581 if (ret & BDRV_BLOCK_ZERO) {
1582 s->status = BLK_ZERO;
1583 } else {
1584 s->status = BLK_DATA;
1585 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001586 } else {
1587 s->status = BLK_BACKING_FILE;
1588 }
1589
1590 s->sector_next_status = sector_num + n;
1591 }
1592
1593 n = MIN(n, s->sector_next_status - sector_num);
1594 if (s->status == BLK_DATA) {
1595 n = MIN(n, s->buf_sectors);
1596 }
1597
1598 /* We need to write complete clusters for compressed images, so if an
1599 * unallocated area is shorter than that, we must consider the whole
1600 * cluster allocated. */
1601 if (s->compressed) {
1602 if (n < s->cluster_sectors) {
1603 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1604 s->status = BLK_DATA;
1605 } else {
1606 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1607 }
1608 }
1609
1610 return n;
1611}
1612
Peter Lieven2d9187b2017-02-28 13:40:07 +01001613static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1614 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001615{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001616 int n, ret;
1617 QEMUIOVector qiov;
1618 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001619
Kevin Wolf690c7302015-03-19 13:33:32 +01001620 assert(nb_sectors <= s->buf_sectors);
1621 while (nb_sectors > 0) {
1622 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001623 int src_cur;
1624 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001625
1626 /* In the case of compression with multiple source files, we can get a
1627 * nb_sectors that spreads into the next part. So we must be able to
1628 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001629 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1630 blk = s->src[src_cur];
1631 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001632
Peter Lieven2d9187b2017-02-28 13:40:07 +01001633 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1634 iov.iov_base = buf;
1635 iov.iov_len = n << BDRV_SECTOR_BITS;
1636 qemu_iovec_init_external(&qiov, &iov, 1);
1637
1638 ret = blk_co_preadv(
1639 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1640 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001641 if (ret < 0) {
1642 return ret;
1643 }
1644
1645 sector_num += n;
1646 nb_sectors -= n;
1647 buf += n * BDRV_SECTOR_SIZE;
1648 }
1649
1650 return 0;
1651}
1652
Peter Lieven2d9187b2017-02-28 13:40:07 +01001653
1654static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1655 int nb_sectors, uint8_t *buf,
1656 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001657{
1658 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001659 QEMUIOVector qiov;
1660 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001661
1662 while (nb_sectors > 0) {
1663 int n = nb_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001664 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001665 case BLK_BACKING_FILE:
1666 /* If we have a backing file, leave clusters unallocated that are
1667 * unallocated in the source image, so that the backing file is
1668 * visible at the respective offset. */
1669 assert(s->target_has_backing);
1670 break;
1671
1672 case BLK_DATA:
1673 /* We must always write compressed clusters as a whole, so don't
1674 * try to find zeroed parts in the buffer. We can only save the
1675 * write if the buffer is completely zeroed and we're allowed to
1676 * keep the target sparse. */
1677 if (s->compressed) {
1678 if (s->has_zero_init && s->min_sparse &&
1679 buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))
1680 {
1681 assert(!s->target_has_backing);
1682 break;
1683 }
1684
Peter Lieven2d9187b2017-02-28 13:40:07 +01001685 iov.iov_base = buf;
1686 iov.iov_len = n << BDRV_SECTOR_BITS;
1687 qemu_iovec_init_external(&qiov, &iov, 1);
1688
1689 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
1690 n << BDRV_SECTOR_BITS, &qiov,
1691 BDRV_REQ_WRITE_COMPRESSED);
Kevin Wolf690c7302015-03-19 13:33:32 +01001692 if (ret < 0) {
1693 return ret;
1694 }
1695 break;
1696 }
1697
1698 /* If there is real non-zero data or we're told to keep the target
1699 * fully allocated (-S 0), we must write it. Otherwise we can treat
1700 * it as zero sectors. */
1701 if (!s->min_sparse ||
1702 is_allocated_sectors_min(buf, n, &n, s->min_sparse))
1703 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001704 iov.iov_base = buf;
1705 iov.iov_len = n << BDRV_SECTOR_BITS;
1706 qemu_iovec_init_external(&qiov, &iov, 1);
1707
1708 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
1709 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001710 if (ret < 0) {
1711 return ret;
1712 }
1713 break;
1714 }
1715 /* fall-through */
1716
1717 case BLK_ZERO:
1718 if (s->has_zero_init) {
1719 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;
bellardea2384d2004-08-01 21:59:26 +00001933
Peter Lieven9fd77f92017-04-21 11:11:55 +02001934 ImgConvertState s = (ImgConvertState) {
1935 /* Need at least 4k of zeros for sparse detection */
1936 .min_sparse = 8,
1937 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1938 .wr_in_order = true,
1939 .num_coroutines = 8,
1940 };
1941
bellardea2384d2004-08-01 21:59:26 +00001942 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001943 static const struct option long_options[] = {
1944 {"help", no_argument, 0, 'h'},
1945 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001946 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001947 {0, 0, 0, 0}
1948 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001949 c = getopt_long(argc, argv, ":hf:O:B:ce6o:s:l:S:pt:T:qnm:W",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001950 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001951 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001952 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001953 }
bellardea2384d2004-08-01 21:59:26 +00001954 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001955 case ':':
1956 missing_argument(argv[optind - 1]);
1957 break;
Jes Sorensenef873942010-12-06 15:25:40 +01001958 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001959 unrecognized_option(argv[optind - 1]);
1960 break;
bellardea2384d2004-08-01 21:59:26 +00001961 case 'h':
1962 help();
1963 break;
1964 case 'f':
1965 fmt = optarg;
1966 break;
1967 case 'O':
1968 out_fmt = optarg;
1969 break;
thsf58c7b32008-06-05 21:53:49 +00001970 case 'B':
1971 out_baseimg = optarg;
1972 break;
bellardea2384d2004-08-01 21:59:26 +00001973 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02001974 s.compressed = true;
bellardea2384d2004-08-01 21:59:26 +00001975 break;
1976 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001977 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001978 "encryption\' instead!");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001979 goto fail_getopt;
thsec36ba12007-09-16 21:59:02 +00001980 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001981 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001982 "compat6\' instead!");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001983 goto fail_getopt;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001984 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001985 if (!is_valid_option_list(optarg)) {
1986 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001987 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001988 }
1989 if (!options) {
1990 options = g_strdup(optarg);
1991 } else {
1992 char *old_options = options;
1993 options = g_strdup_printf("%s,%s", options, optarg);
1994 g_free(old_options);
1995 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001996 break;
edison51ef6722010-09-21 19:58:41 -07001997 case 's':
1998 snapshot_name = optarg;
1999 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08002000 case 'l':
2001 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01002002 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2003 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08002004 if (!sn_opts) {
2005 error_report("Failed in parsing snapshot param '%s'",
2006 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002007 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002008 }
2009 } else {
2010 snapshot_name = optarg;
2011 }
2012 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002013 case 'S':
2014 {
2015 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002016
2017 sval = cvtnum(optarg);
2018 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02002019 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002020 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002021 }
2022
Peter Lieven9fd77f92017-04-21 11:11:55 +02002023 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002024 break;
2025 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002026 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002027 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002028 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002029 case 't':
2030 cache = optarg;
2031 break;
Max Reitz40055952014-07-22 22:58:42 +02002032 case 'T':
2033 src_cache = optarg;
2034 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002035 case 'q':
2036 quiet = true;
2037 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002038 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002039 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002040 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002041 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002042 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2043 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002044 error_report("Invalid number of coroutines. Allowed number of"
2045 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002046 goto fail_getopt;
2047 }
2048 break;
2049 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002050 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002051 break;
Max Reitz3258b912017-04-26 15:46:47 +02002052 case OPTION_OBJECT: {
2053 QemuOpts *object_opts;
2054 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2055 optarg, true);
2056 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002057 goto fail_getopt;
2058 }
2059 break;
Max Reitz3258b912017-04-26 15:46:47 +02002060 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002061 case OPTION_IMAGE_OPTS:
2062 image_opts = true;
2063 break;
bellardea2384d2004-08-01 21:59:26 +00002064 }
2065 }
ths3b46e622007-09-17 08:09:54 +00002066
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002067 if (qemu_opts_foreach(&qemu_object_opts,
2068 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002069 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002070 goto fail_getopt;
2071 }
2072
Peter Lieven9fd77f92017-04-21 11:11:55 +02002073 if (!s.wr_in_order && s.compressed) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002074 error_report("Out of order write and compress are mutually exclusive");
Peter Lieven9fd77f92017-04-21 11:11:55 +02002075 goto fail_getopt;
2076 }
2077
2078 s.src_num = argc - optind - 1;
2079 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2080
2081 if (options && has_help_option(options)) {
2082 ret = print_block_option_help(out_filename, out_fmt);
2083 goto fail_getopt;
2084 }
2085
2086 if (s.src_num < 1) {
2087 error_report("Must specify image file name");
2088 goto fail_getopt;
2089 }
2090
2091
Peter Lieven9fd77f92017-04-21 11:11:55 +02002092 /* ret is still -EINVAL until here */
2093 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2094 if (ret < 0) {
2095 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002096 goto fail_getopt;
2097 }
2098
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002099 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002100 if (quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002101 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002102 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002103 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002104 qemu_progress_print(0, 100);
2105
Peter Lieven9fd77f92017-04-21 11:11:55 +02002106 s.src = g_new0(BlockBackend *, s.src_num);
2107 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002108
Peter Lieven9fd77f92017-04-21 11:11:55 +02002109 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2110 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
2111 fmt, src_flags, src_writethrough, quiet);
2112 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002113 ret = -1;
2114 goto out;
2115 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002116 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2117 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002118 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002119 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002120 ret = -1;
2121 goto out;
2122 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002123 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002124 }
bellardea2384d2004-08-01 21:59:26 +00002125
Wenchao Xiaef806542013-12-04 17:10:57 +08002126 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002127 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002128 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2129 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2130 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002131 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002132 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002133 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002134 ret = -1;
2135 goto out;
2136 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002137
Peter Lieven9fd77f92017-04-21 11:11:55 +02002138 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2139 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002140 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002141 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002142 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002143 ret = -1;
2144 goto out;
edison51ef6722010-09-21 19:58:41 -07002145 }
2146
Kevin Wolfefa84d42009-05-18 16:42:12 +02002147 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +00002148 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002149 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002150 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002151 ret = -1;
2152 goto out;
2153 }
balrog926c2d22007-10-31 01:11:44 +00002154
Max Reitzb65a5e12015-02-05 13:58:12 -05002155 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002156 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +01002157 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002158 ret = -1;
2159 goto out;
2160 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09002161
Max Reitz2e024cd2015-02-11 09:58:46 -05002162 if (!skip_create) {
2163 if (!drv->create_opts) {
2164 error_report("Format driver '%s' does not support image creation",
2165 drv->format_name);
2166 ret = -1;
2167 goto out;
2168 }
Max Reitzf75613c2014-12-02 18:32:46 +01002169
Max Reitz2e024cd2015-02-11 09:58:46 -05002170 if (!proto_drv->create_opts) {
2171 error_report("Protocol driver '%s' does not support image creation",
2172 proto_drv->format_name);
2173 ret = -1;
2174 goto out;
2175 }
Max Reitzf75613c2014-12-02 18:32:46 +01002176
Max Reitz2e024cd2015-02-11 09:58:46 -05002177 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2178 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002179
Max Reitz2e024cd2015-02-11 09:58:46 -05002180 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002181 if (options) {
2182 qemu_opts_do_parse(opts, options, NULL, &local_err);
2183 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002184 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002185 ret = -1;
2186 goto out;
2187 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002188 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002189
Peter Lieven9fd77f92017-04-21 11:11:55 +02002190 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002191 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002192 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2193 if (ret < 0) {
2194 goto out;
2195 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002196 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002197
Kevin Wolfa18953f2010-10-14 15:46:04 +02002198 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002199 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002200 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002201 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002202 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002203 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002204
Max Reitz48758a82017-04-26 15:46:48 +02002205 if (s.src_num > 1 && out_baseimg) {
2206 error_report("Having a backing file for the target makes no sense when "
2207 "concatenating multiple input images");
2208 ret = -1;
2209 goto out;
2210 }
2211
Kevin Wolfefa84d42009-05-18 16:42:12 +02002212 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002213 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002214 bool encryption =
2215 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
2216 const char *preallocation =
2217 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002218
Pavel Butsykin35fadca2016-07-22 11:17:48 +03002219 if (!drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002220 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002221 ret = -1;
2222 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002223 }
2224
Chunyan Liu83d05212014-06-05 17:20:51 +08002225 if (encryption) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002226 error_report("Compression and encryption not supported at "
2227 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002228 ret = -1;
2229 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002230 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002231
Chunyan Liu83d05212014-06-05 17:20:51 +08002232 if (preallocation
2233 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002234 {
2235 error_report("Compression and preallocation not supported at "
2236 "the same time");
2237 ret = -1;
2238 goto out;
2239 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002240 }
2241
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002242 if (!skip_create) {
2243 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002244 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002245 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002246 error_reportf_err(local_err, "%s: error while converting %s: ",
2247 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002248 goto out;
bellardea2384d2004-08-01 21:59:26 +00002249 }
2250 }
ths3b46e622007-09-17 08:09:54 +00002251
Peter Lieven9fd77f92017-04-21 11:11:55 +02002252 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002253 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002254 if (ret < 0) {
2255 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002256 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002257 }
2258
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002259 /* XXX we should allow --image-opts to trigger use of
2260 * img_open() here, but then we have trouble with
2261 * the bdrv_create() call which takes different params.
2262 * Not critical right now, so fix can wait...
2263 */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002264 s.target = img_open_file(out_filename, out_fmt, flags, writethrough, quiet);
2265 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002266 ret = -1;
2267 goto out;
2268 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002269 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002270
Eric Blake5def6b82016-06-23 16:37:19 -06002271 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002272 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2273 * as maximum. */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002274 s.buf_sectors = MIN(32768,
2275 MAX(s.buf_sectors,
2276 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2277 out_bs->bl.pdiscard_alignment >>
2278 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002279
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002280 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002281 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002282 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002283 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002284 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002285 ret = -1;
2286 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002287 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002288 error_report("output file is smaller than input file");
2289 ret = -1;
2290 goto out;
2291 }
2292 }
2293
Peter Lieven24f833c2013-11-27 11:07:07 +01002294 ret = bdrv_get_info(out_bs, &bdi);
2295 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002296 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002297 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002298 goto out;
2299 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002300 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002301 s.compressed = s.compressed || bdi.needs_compressed_writes;
2302 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Peter Lieven24f833c2013-11-27 11:07:07 +01002303 }
2304
Peter Lieven9fd77f92017-04-21 11:11:55 +02002305 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002306out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002307 if (!ret) {
2308 qemu_progress_print(100, 0);
2309 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002310 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002311 qemu_opts_del(opts);
2312 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002313 qemu_opts_del(sn_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002314 blk_unref(s.target);
2315 if (s.src) {
2316 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2317 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002318 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002319 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002320 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002321 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002322fail_getopt:
2323 g_free(options);
2324
Peter Lieven9fd77f92017-04-21 11:11:55 +02002325 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002326}
2327
bellard57d1a2b2004-08-03 21:15:11 +00002328
bellardfaea38e2006-08-05 21:31:00 +00002329static void dump_snapshots(BlockDriverState *bs)
2330{
2331 QEMUSnapshotInfo *sn_tab, *sn;
2332 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002333
2334 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2335 if (nb_sns <= 0)
2336 return;
2337 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002338 bdrv_snapshot_dump(fprintf, stdout, NULL);
2339 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002340 for(i = 0; i < nb_sns; i++) {
2341 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002342 bdrv_snapshot_dump(fprintf, stdout, sn);
2343 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002344 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002345 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002346}
2347
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002348static void dump_json_image_info_list(ImageInfoList *list)
2349{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002350 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002351 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002352 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002353
2354 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2355 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002356 str = qobject_to_json_pretty(obj);
2357 assert(str != NULL);
2358 printf("%s\n", qstring_get_str(str));
2359 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002360 visit_free(v);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002361 QDECREF(str);
2362}
2363
Benoît Canetc054b3f2012-09-05 13:09:02 +02002364static void dump_json_image_info(ImageInfo *info)
2365{
Benoît Canetc054b3f2012-09-05 13:09:02 +02002366 QString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002367 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002368 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002369
2370 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2371 visit_complete(v, &obj);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002372 str = qobject_to_json_pretty(obj);
2373 assert(str != NULL);
2374 printf("%s\n", qstring_get_str(str));
2375 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002376 visit_free(v);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002377 QDECREF(str);
2378}
2379
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002380static void dump_human_image_info_list(ImageInfoList *list)
2381{
2382 ImageInfoList *elem;
2383 bool delim = false;
2384
2385 for (elem = list; elem; elem = elem->next) {
2386 if (delim) {
2387 printf("\n");
2388 }
2389 delim = true;
2390
Wenchao Xia5b917042013-05-25 11:09:45 +08002391 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002392 }
2393}
2394
2395static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2396{
2397 return strcmp(a, b) == 0;
2398}
2399
2400/**
2401 * Open an image file chain and return an ImageInfoList
2402 *
2403 * @filename: topmost image filename
2404 * @fmt: topmost image format (may be NULL to autodetect)
2405 * @chain: true - enumerate entire backing file chain
2406 * false - only topmost image file
2407 *
2408 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2409 * image file. If there was an error a message will have been printed to
2410 * stderr.
2411 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002412static ImageInfoList *collect_image_info_list(bool image_opts,
2413 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002414 const char *fmt,
2415 bool chain)
2416{
2417 ImageInfoList *head = NULL;
2418 ImageInfoList **last = &head;
2419 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002420 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002421
2422 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2423
2424 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002425 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002426 BlockDriverState *bs;
2427 ImageInfo *info;
2428 ImageInfoList *elem;
2429
2430 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2431 error_report("Backing file '%s' creates an infinite loop.",
2432 filename);
2433 goto err;
2434 }
2435 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2436
Max Reitzefaa7c42016-03-16 19:54:38 +01002437 blk = img_open(image_opts, filename, fmt,
Kevin Wolfce099542016-03-15 13:01:04 +01002438 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002439 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002440 goto err;
2441 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002442 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002443
Wenchao Xia43526ec2013-06-06 12:27:58 +08002444 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002445 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002446 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002447 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002448 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002449 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002450
2451 elem = g_new0(ImageInfoList, 1);
2452 elem->value = info;
2453 *last = elem;
2454 last = &elem->next;
2455
Markus Armbruster26f54e92014-10-07 13:59:04 +02002456 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002457
2458 filename = fmt = NULL;
2459 if (chain) {
2460 if (info->has_full_backing_filename) {
2461 filename = info->full_backing_filename;
2462 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002463 error_report("Could not determine absolute backing filename,"
2464 " but backing filename '%s' present",
2465 info->backing_filename);
2466 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002467 }
2468 if (info->has_backing_filename_format) {
2469 fmt = info->backing_filename_format;
2470 }
2471 }
2472 }
2473 g_hash_table_destroy(filenames);
2474 return head;
2475
2476err:
2477 qapi_free_ImageInfoList(head);
2478 g_hash_table_destroy(filenames);
2479 return NULL;
2480}
2481
Benoît Canetc054b3f2012-09-05 13:09:02 +02002482static int img_info(int argc, char **argv)
2483{
2484 int c;
2485 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002486 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002487 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002488 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002489 bool image_opts = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002490
bellardea2384d2004-08-01 21:59:26 +00002491 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002492 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002493 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002494 int option_index = 0;
2495 static const struct option long_options[] = {
2496 {"help", no_argument, 0, 'h'},
2497 {"format", required_argument, 0, 'f'},
2498 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002499 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002500 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002501 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002502 {0, 0, 0, 0}
2503 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002504 c = getopt_long(argc, argv, ":f:h",
Benoît Canetc054b3f2012-09-05 13:09:02 +02002505 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002506 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002507 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002508 }
bellardea2384d2004-08-01 21:59:26 +00002509 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002510 case ':':
2511 missing_argument(argv[optind - 1]);
2512 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002513 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002514 unrecognized_option(argv[optind - 1]);
2515 break;
bellardea2384d2004-08-01 21:59:26 +00002516 case 'h':
2517 help();
2518 break;
2519 case 'f':
2520 fmt = optarg;
2521 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002522 case OPTION_OUTPUT:
2523 output = optarg;
2524 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002525 case OPTION_BACKING_CHAIN:
2526 chain = true;
2527 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002528 case OPTION_OBJECT: {
2529 QemuOpts *opts;
2530 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2531 optarg, true);
2532 if (!opts) {
2533 return 1;
2534 }
2535 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002536 case OPTION_IMAGE_OPTS:
2537 image_opts = true;
2538 break;
bellardea2384d2004-08-01 21:59:26 +00002539 }
2540 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002541 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002542 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002543 }
bellardea2384d2004-08-01 21:59:26 +00002544 filename = argv[optind++];
2545
Benoît Canetc054b3f2012-09-05 13:09:02 +02002546 if (output && !strcmp(output, "json")) {
2547 output_format = OFORMAT_JSON;
2548 } else if (output && !strcmp(output, "human")) {
2549 output_format = OFORMAT_HUMAN;
2550 } else if (output) {
2551 error_report("--output must be used with human or json as argument.");
2552 return 1;
2553 }
2554
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002555 if (qemu_opts_foreach(&qemu_object_opts,
2556 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002557 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002558 return 1;
2559 }
2560
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002561 list = collect_image_info_list(image_opts, filename, fmt, chain);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002562 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002563 return 1;
2564 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002565
Benoît Canetc054b3f2012-09-05 13:09:02 +02002566 switch (output_format) {
2567 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002568 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002569 break;
2570 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002571 if (chain) {
2572 dump_json_image_info_list(list);
2573 } else {
2574 dump_json_image_info(list->value);
2575 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002576 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002577 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002578
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002579 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002580 return 0;
2581}
2582
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002583static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2584 MapEntry *next)
2585{
2586 switch (output_format) {
2587 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002588 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002589 error_report("File contains external, encrypted or compressed clusters.");
2590 exit(1);
2591 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002592 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002593 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002594 e->start, e->length,
2595 e->has_offset ? e->offset : 0,
2596 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002597 }
2598 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2599 * Modify the flags here to allow more coalescing.
2600 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002601 if (next && (!next->data || next->zero)) {
2602 next->data = false;
2603 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002604 }
2605 break;
2606 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002607 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2608 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002609 (e->start == 0 ? "[" : ",\n"),
2610 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002611 e->zero ? "true" : "false",
2612 e->data ? "true" : "false");
2613 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002614 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002615 }
2616 putchar('}');
2617
2618 if (!next) {
2619 printf("]\n");
2620 }
2621 break;
2622 }
2623}
2624
2625static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2626 int nb_sectors, MapEntry *e)
2627{
2628 int64_t ret;
2629 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002630 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002631 bool has_offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002632
2633 /* As an optimization, we could cache the current range of unallocated
2634 * clusters in each file of the chain, and avoid querying the same
2635 * range repeatedly.
2636 */
2637
2638 depth = 0;
2639 for (;;) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08002640 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2641 &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002642 if (ret < 0) {
2643 return ret;
2644 }
2645 assert(nb_sectors);
2646 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2647 break;
2648 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002649 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002650 if (bs == NULL) {
2651 ret = 0;
2652 break;
2653 }
2654
2655 depth++;
2656 }
2657
John Snow28756452016-02-05 13:12:33 -05002658 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2659
2660 *e = (MapEntry) {
2661 .start = sector_num * BDRV_SECTOR_SIZE,
2662 .length = nb_sectors * BDRV_SECTOR_SIZE,
2663 .data = !!(ret & BDRV_BLOCK_DATA),
2664 .zero = !!(ret & BDRV_BLOCK_ZERO),
2665 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2666 .has_offset = has_offset,
2667 .depth = depth,
2668 .has_filename = file && has_offset,
2669 .filename = file && has_offset ? file->filename : NULL,
2670 };
2671
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002672 return 0;
2673}
2674
Fam Zheng16b0d552016-01-26 11:59:02 +08002675static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2676{
2677 if (curr->length == 0) {
2678 return false;
2679 }
2680 if (curr->zero != next->zero ||
2681 curr->data != next->data ||
2682 curr->depth != next->depth ||
2683 curr->has_filename != next->has_filename ||
2684 curr->has_offset != next->has_offset) {
2685 return false;
2686 }
2687 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2688 return false;
2689 }
2690 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2691 return false;
2692 }
2693 return true;
2694}
2695
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002696static int img_map(int argc, char **argv)
2697{
2698 int c;
2699 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002700 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002701 BlockDriverState *bs;
2702 const char *filename, *fmt, *output;
2703 int64_t length;
2704 MapEntry curr = { .length = 0 }, next;
2705 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002706 bool image_opts = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002707
2708 fmt = NULL;
2709 output = NULL;
2710 for (;;) {
2711 int option_index = 0;
2712 static const struct option long_options[] = {
2713 {"help", no_argument, 0, 'h'},
2714 {"format", required_argument, 0, 'f'},
2715 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002716 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002717 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002718 {0, 0, 0, 0}
2719 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002720 c = getopt_long(argc, argv, ":f:h",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002721 long_options, &option_index);
2722 if (c == -1) {
2723 break;
2724 }
2725 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002726 case ':':
2727 missing_argument(argv[optind - 1]);
2728 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002729 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002730 unrecognized_option(argv[optind - 1]);
2731 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002732 case 'h':
2733 help();
2734 break;
2735 case 'f':
2736 fmt = optarg;
2737 break;
2738 case OPTION_OUTPUT:
2739 output = optarg;
2740 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002741 case OPTION_OBJECT: {
2742 QemuOpts *opts;
2743 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2744 optarg, true);
2745 if (!opts) {
2746 return 1;
2747 }
2748 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002749 case OPTION_IMAGE_OPTS:
2750 image_opts = true;
2751 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002752 }
2753 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002754 if (optind != argc - 1) {
2755 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002756 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002757 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002758
2759 if (output && !strcmp(output, "json")) {
2760 output_format = OFORMAT_JSON;
2761 } else if (output && !strcmp(output, "human")) {
2762 output_format = OFORMAT_HUMAN;
2763 } else if (output) {
2764 error_report("--output must be used with human or json as argument.");
2765 return 1;
2766 }
2767
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002768 if (qemu_opts_foreach(&qemu_object_opts,
2769 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002770 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002771 return 1;
2772 }
2773
Kevin Wolfce099542016-03-15 13:01:04 +01002774 blk = img_open(image_opts, filename, fmt, 0, false, false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002775 if (!blk) {
2776 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002777 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002778 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002779
2780 if (output_format == OFORMAT_HUMAN) {
2781 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2782 }
2783
Max Reitzf1d3cd72015-02-05 13:58:18 -05002784 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002785 while (curr.start + curr.length < length) {
2786 int64_t nsectors_left;
2787 int64_t sector_num;
2788 int n;
2789
2790 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2791
2792 /* Probe up to 1 GiB at a time. */
2793 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2794 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2795 ret = get_block_status(bs, sector_num, n, &next);
2796
2797 if (ret < 0) {
2798 error_report("Could not read file metadata: %s", strerror(-ret));
2799 goto out;
2800 }
2801
Fam Zheng16b0d552016-01-26 11:59:02 +08002802 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002803 curr.length += next.length;
2804 continue;
2805 }
2806
2807 if (curr.length > 0) {
2808 dump_map_entry(output_format, &curr, &next);
2809 }
2810 curr = next;
2811 }
2812
2813 dump_map_entry(output_format, &curr, NULL);
2814
2815out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002816 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002817 return ret < 0;
2818}
2819
aliguorif7b4a942009-01-07 17:40:15 +00002820#define SNAPSHOT_LIST 1
2821#define SNAPSHOT_CREATE 2
2822#define SNAPSHOT_APPLY 3
2823#define SNAPSHOT_DELETE 4
2824
Stuart Brady153859b2009-06-07 00:42:17 +01002825static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002826{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002827 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002828 BlockDriverState *bs;
2829 QEMUSnapshotInfo sn;
2830 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002831 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002832 int action = 0;
2833 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002834 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002835 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002836 bool image_opts = false;
aliguorif7b4a942009-01-07 17:40:15 +00002837
Kevin Wolfce099542016-03-15 13:01:04 +01002838 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002839 /* Parse commandline parameters */
2840 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002841 static const struct option long_options[] = {
2842 {"help", no_argument, 0, 'h'},
2843 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002844 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002845 {0, 0, 0, 0}
2846 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002847 c = getopt_long(argc, argv, ":la:c:d:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002848 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002849 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002850 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002851 }
aliguorif7b4a942009-01-07 17:40:15 +00002852 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002853 case ':':
2854 missing_argument(argv[optind - 1]);
2855 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002856 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002857 unrecognized_option(argv[optind - 1]);
2858 break;
aliguorif7b4a942009-01-07 17:40:15 +00002859 case 'h':
2860 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002861 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002862 case 'l':
2863 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002864 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002865 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002866 }
2867 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002868 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002869 break;
2870 case 'a':
2871 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002872 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002873 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002874 }
2875 action = SNAPSHOT_APPLY;
2876 snapshot_name = optarg;
2877 break;
2878 case 'c':
2879 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002880 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002881 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002882 }
2883 action = SNAPSHOT_CREATE;
2884 snapshot_name = optarg;
2885 break;
2886 case 'd':
2887 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002888 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002889 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002890 }
2891 action = SNAPSHOT_DELETE;
2892 snapshot_name = optarg;
2893 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002894 case 'q':
2895 quiet = true;
2896 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002897 case OPTION_OBJECT: {
2898 QemuOpts *opts;
2899 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2900 optarg, true);
2901 if (!opts) {
2902 return 1;
2903 }
2904 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002905 case OPTION_IMAGE_OPTS:
2906 image_opts = true;
2907 break;
aliguorif7b4a942009-01-07 17:40:15 +00002908 }
2909 }
2910
Kevin Wolffc11eb22013-08-05 10:53:04 +02002911 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002912 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002913 }
aliguorif7b4a942009-01-07 17:40:15 +00002914 filename = argv[optind++];
2915
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002916 if (qemu_opts_foreach(&qemu_object_opts,
2917 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002918 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002919 return 1;
2920 }
2921
aliguorif7b4a942009-01-07 17:40:15 +00002922 /* Open the image */
Kevin Wolfce099542016-03-15 13:01:04 +01002923 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002924 if (!blk) {
2925 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002926 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002927 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002928
2929 /* Perform the requested action */
2930 switch(action) {
2931 case SNAPSHOT_LIST:
2932 dump_snapshots(bs);
2933 break;
2934
2935 case SNAPSHOT_CREATE:
2936 memset(&sn, 0, sizeof(sn));
2937 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2938
2939 qemu_gettimeofday(&tv);
2940 sn.date_sec = tv.tv_sec;
2941 sn.date_nsec = tv.tv_usec * 1000;
2942
2943 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002944 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002945 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002946 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002947 }
aliguorif7b4a942009-01-07 17:40:15 +00002948 break;
2949
2950 case SNAPSHOT_APPLY:
2951 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002952 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002953 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002954 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002955 }
aliguorif7b4a942009-01-07 17:40:15 +00002956 break;
2957
2958 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002959 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002960 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002961 error_reportf_err(err, "Could not delete snapshot '%s': ",
2962 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002963 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002964 }
aliguorif7b4a942009-01-07 17:40:15 +00002965 break;
2966 }
2967
2968 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02002969 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002970 if (ret) {
2971 return 1;
2972 }
Stuart Brady153859b2009-06-07 00:42:17 +01002973 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002974}
2975
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002976static int img_rebase(int argc, char **argv)
2977{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002978 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01002979 uint8_t *buf_old = NULL;
2980 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05002981 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002982 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02002983 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
2984 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01002985 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002986 int unsafe = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002987 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002988 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02002989 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002990 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002991
2992 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002993 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002994 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02002995 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002996 out_baseimg = NULL;
2997 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002998 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002999 static const struct option long_options[] = {
3000 {"help", no_argument, 0, 'h'},
3001 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003002 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003003 {0, 0, 0, 0}
3004 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003005 c = getopt_long(argc, argv, ":hf:F:b:upt:T:q",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003006 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003007 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003008 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003009 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003010 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003011 case ':':
3012 missing_argument(argv[optind - 1]);
3013 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003014 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003015 unrecognized_option(argv[optind - 1]);
3016 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003017 case 'h':
3018 help();
3019 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003020 case 'f':
3021 fmt = optarg;
3022 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003023 case 'F':
3024 out_basefmt = optarg;
3025 break;
3026 case 'b':
3027 out_baseimg = optarg;
3028 break;
3029 case 'u':
3030 unsafe = 1;
3031 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003032 case 'p':
3033 progress = 1;
3034 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003035 case 't':
3036 cache = optarg;
3037 break;
Max Reitz40055952014-07-22 22:58:42 +02003038 case 'T':
3039 src_cache = optarg;
3040 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003041 case 'q':
3042 quiet = true;
3043 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003044 case OPTION_OBJECT: {
3045 QemuOpts *opts;
3046 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3047 optarg, true);
3048 if (!opts) {
3049 return 1;
3050 }
3051 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003052 case OPTION_IMAGE_OPTS:
3053 image_opts = true;
3054 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003055 }
3056 }
3057
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003058 if (quiet) {
3059 progress = 0;
3060 }
3061
Fam Zhengac1307a2014-04-22 13:36:11 +08003062 if (optind != argc - 1) {
3063 error_exit("Expecting one image file name");
3064 }
3065 if (!unsafe && !out_baseimg) {
3066 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003067 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003068 filename = argv[optind++];
3069
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003070 if (qemu_opts_foreach(&qemu_object_opts,
3071 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003072 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003073 return 1;
3074 }
3075
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003076 qemu_progress_init(progress, 2.0);
3077 qemu_progress_print(0, 100);
3078
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003079 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003080 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003081 if (ret < 0) {
3082 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003083 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003084 }
3085
Kevin Wolfce099542016-03-15 13:01:04 +01003086 src_flags = 0;
3087 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003088 if (ret < 0) {
3089 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003090 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003091 }
3092
Kevin Wolfce099542016-03-15 13:01:04 +01003093 /* The source files are opened read-only, don't care about WCE */
3094 assert((src_flags & BDRV_O_RDWR) == 0);
3095 (void) src_writethrough;
3096
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003097 /*
3098 * Open the images.
3099 *
3100 * Ignore the old backing file for unsafe rebase in case we want to correct
3101 * the reference to a renamed or moved backing file.
3102 */
Kevin Wolfce099542016-03-15 13:01:04 +01003103 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003104 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003105 ret = -1;
3106 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003107 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003108 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003109
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003110 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003111 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003112 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003113 ret = -1;
3114 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003115 }
3116 }
3117
3118 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003119 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003120 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003121 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003122
Max Reitz644483d2015-02-05 13:58:17 -05003123 if (bs->backing_format[0] != '\0') {
3124 options = qdict_new();
3125 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
3126 }
3127
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003128 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003129 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003130 options, src_flags, &local_err);
3131 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003132 error_reportf_err(local_err,
3133 "Could not open old backing file '%s': ",
3134 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003135 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003136 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003137 }
Max Reitz644483d2015-02-05 13:58:17 -05003138
Alex Bligha6166732012-10-16 13:46:18 +01003139 if (out_baseimg[0]) {
Max Reitz644483d2015-02-05 13:58:17 -05003140 if (out_basefmt) {
3141 options = qdict_new();
3142 qdict_put(options, "driver", qstring_from_str(out_basefmt));
3143 } else {
3144 options = NULL;
3145 }
3146
Max Reitzefaa7c42016-03-16 19:54:38 +01003147 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003148 options, src_flags, &local_err);
3149 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003150 error_reportf_err(local_err,
3151 "Could not open new backing file '%s': ",
3152 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003153 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003154 goto out;
3155 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003156 }
3157 }
3158
3159 /*
3160 * Check each unallocated cluster in the COW file. If it is unallocated,
3161 * accesses go to the backing file. We must therefore compare this cluster
3162 * in the old and new backing file, and if they differ we need to copy it
3163 * from the old backing file into the COW file.
3164 *
3165 * If qemu-img crashes during this step, no harm is done. The content of
3166 * the image is the same as the original one at any time.
3167 */
3168 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003169 int64_t num_sectors;
3170 int64_t old_backing_num_sectors;
3171 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003172 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003173 int n;
Kevin Wolf1f710492012-10-12 14:29:18 +02003174 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003175
Max Reitzf1d3cd72015-02-05 13:58:18 -05003176 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3177 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003178
Max Reitzf1d3cd72015-02-05 13:58:18 -05003179 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003180 if (num_sectors < 0) {
3181 error_report("Could not get size of '%s': %s",
3182 filename, strerror(-num_sectors));
3183 ret = -1;
3184 goto out;
3185 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003186 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003187 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003188 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003189
3190 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3191 error_report("Could not get size of '%s': %s",
3192 backing_name, strerror(-old_backing_num_sectors));
3193 ret = -1;
3194 goto out;
3195 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003196 if (blk_new_backing) {
3197 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003198 if (new_backing_num_sectors < 0) {
3199 error_report("Could not get size of '%s': %s",
3200 out_baseimg, strerror(-new_backing_num_sectors));
3201 ret = -1;
3202 goto out;
3203 }
Alex Bligha6166732012-10-16 13:46:18 +01003204 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003205
Kevin Wolf1f710492012-10-12 14:29:18 +02003206 if (num_sectors != 0) {
3207 local_progress = (float)100 /
3208 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
3209 }
3210
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003211 for (sector = 0; sector < num_sectors; sector += n) {
3212
3213 /* How many sectors can we handle with the next read? */
3214 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
3215 n = (IO_BUF_SIZE / 512);
3216 } else {
3217 n = num_sectors - sector;
3218 }
3219
3220 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02003221 ret = bdrv_is_allocated(bs, sector, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003222 if (ret < 0) {
3223 error_report("error while reading image metadata: %s",
3224 strerror(-ret));
3225 goto out;
3226 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003227 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003228 continue;
3229 }
3230
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003231 /*
3232 * Read old and new backing file and take into consideration that
3233 * backing files may be smaller than the COW image.
3234 */
3235 if (sector >= old_backing_num_sectors) {
3236 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3237 } else {
3238 if (sector + n > old_backing_num_sectors) {
3239 n = old_backing_num_sectors - sector;
3240 }
3241
Eric Blake91669202016-05-06 10:26:43 -06003242 ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS,
3243 buf_old, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003244 if (ret < 0) {
3245 error_report("error while reading from old backing file");
3246 goto out;
3247 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003248 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003249
Max Reitzf1d3cd72015-02-05 13:58:18 -05003250 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003251 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3252 } else {
3253 if (sector + n > new_backing_num_sectors) {
3254 n = new_backing_num_sectors - sector;
3255 }
3256
Eric Blake91669202016-05-06 10:26:43 -06003257 ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS,
3258 buf_new, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003259 if (ret < 0) {
3260 error_report("error while reading from new backing file");
3261 goto out;
3262 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003263 }
3264
3265 /* If they differ, we need to write to the COW file */
3266 uint64_t written = 0;
3267
3268 while (written < n) {
3269 int pnum;
3270
3271 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003272 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003273 {
Eric Blake91669202016-05-06 10:26:43 -06003274 ret = blk_pwrite(blk,
3275 (sector + written) << BDRV_SECTOR_BITS,
3276 buf_old + written * 512,
3277 pnum << BDRV_SECTOR_BITS, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003278 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003279 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003280 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003281 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003282 }
3283 }
3284
3285 written += pnum;
3286 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003287 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003288 }
3289 }
3290
3291 /*
3292 * Change the backing file. All clusters that are different from the old
3293 * backing file are overwritten in the COW file now, so the visible content
3294 * doesn't change when we switch the backing file.
3295 */
Alex Bligha6166732012-10-16 13:46:18 +01003296 if (out_baseimg && *out_baseimg) {
3297 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3298 } else {
3299 ret = bdrv_change_backing_file(bs, NULL, NULL);
3300 }
3301
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003302 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003303 error_report("Could not change the backing file to '%s': No "
3304 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003305 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003306 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003307 out_baseimg, strerror(-ret));
3308 }
3309
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003310 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003311 /*
3312 * TODO At this point it is possible to check if any clusters that are
3313 * allocated in the COW file are the same in the backing file. If so, they
3314 * could be dropped from the COW file. Don't do this before switching the
3315 * backing file, in case of a crash this would lead to corruption.
3316 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003317out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003318 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003319 /* Cleanup */
3320 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003321 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003322 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003323 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003324 qemu_vfree(buf_old);
3325 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003326
Markus Armbruster26f54e92014-10-07 13:59:04 +02003327 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003328 if (ret) {
3329 return 1;
3330 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003331 return 0;
3332}
3333
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003334static int img_resize(int argc, char **argv)
3335{
Markus Armbruster6750e792015-02-12 17:43:08 +01003336 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003337 int c, ret, relative;
3338 const char *filename, *fmt, *size;
3339 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003340 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003341 BlockBackend *blk = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003342 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003343
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003344 static QemuOptsList resize_options = {
3345 .name = "resize_options",
3346 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3347 .desc = {
3348 {
3349 .name = BLOCK_OPT_SIZE,
3350 .type = QEMU_OPT_SIZE,
3351 .help = "Virtual disk size"
3352 }, {
3353 /* end of list */
3354 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003355 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003356 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003357 bool image_opts = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003358
Kevin Wolfe80fec72011-04-29 10:58:12 +02003359 /* Remove size from argv manually so that negative numbers are not treated
3360 * as options by getopt. */
3361 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003362 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003363 return 1;
3364 }
3365
3366 size = argv[--argc];
3367
3368 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003369 fmt = NULL;
3370 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003371 static const struct option long_options[] = {
3372 {"help", no_argument, 0, 'h'},
3373 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003374 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003375 {0, 0, 0, 0}
3376 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003377 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003378 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003379 if (c == -1) {
3380 break;
3381 }
3382 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003383 case ':':
3384 missing_argument(argv[optind - 1]);
3385 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003386 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003387 unrecognized_option(argv[optind - 1]);
3388 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003389 case 'h':
3390 help();
3391 break;
3392 case 'f':
3393 fmt = optarg;
3394 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003395 case 'q':
3396 quiet = true;
3397 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003398 case OPTION_OBJECT: {
3399 QemuOpts *opts;
3400 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3401 optarg, true);
3402 if (!opts) {
3403 return 1;
3404 }
3405 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003406 case OPTION_IMAGE_OPTS:
3407 image_opts = true;
3408 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003409 }
3410 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003411 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003412 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003413 }
3414 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003415
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003416 if (qemu_opts_foreach(&qemu_object_opts,
3417 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003418 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003419 return 1;
3420 }
3421
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003422 /* Choose grow, shrink, or absolute resize mode */
3423 switch (size[0]) {
3424 case '+':
3425 relative = 1;
3426 size++;
3427 break;
3428 case '-':
3429 relative = -1;
3430 size++;
3431 break;
3432 default:
3433 relative = 0;
3434 break;
3435 }
3436
3437 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003438 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003439 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003440 if (err) {
3441 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003442 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003443 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003444 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003445 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003446 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3447 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003448
Max Reitzefaa7c42016-03-16 19:54:38 +01003449 blk = img_open(image_opts, filename, fmt,
Kevin Wolf55880602017-02-17 15:07:38 +01003450 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003451 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003452 ret = -1;
3453 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003454 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003455
3456 if (relative) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003457 total_size = blk_getlength(blk) + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003458 } else {
3459 total_size = n;
3460 }
3461 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003462 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003463 ret = -1;
3464 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003465 }
3466
Max Reitzf1d3cd72015-02-05 13:58:18 -05003467 ret = blk_truncate(blk, total_size);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003468 switch (ret) {
3469 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003470 qprintf(quiet, "Image resized.\n");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003471 break;
3472 case -ENOTSUP:
Kevin Wolf259b2172012-03-06 12:44:45 +01003473 error_report("This image does not support resize");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003474 break;
3475 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +01003476 error_report("Image is read-only");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003477 break;
3478 default:
Max Reitzbcf23482016-06-15 17:36:29 +02003479 error_report("Error resizing image: %s", strerror(-ret));
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003480 break;
3481 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003482out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003483 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003484 if (ret) {
3485 return 1;
3486 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003487 return 0;
3488}
3489
Max Reitz76a3a342014-10-27 11:12:51 +01003490static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003491 int64_t offset, int64_t total_work_size,
3492 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003493{
3494 qemu_progress_print(100.f * offset / total_work_size, 0);
3495}
3496
Max Reitz6f176b42013-09-03 10:09:50 +02003497static int img_amend(int argc, char **argv)
3498{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003499 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003500 int c, ret = 0;
3501 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003502 QemuOptsList *create_opts = NULL;
3503 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003504 const char *fmt = NULL, *filename, *cache;
3505 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003506 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003507 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003508 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003509 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003510 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003511
Max Reitzbd39e6e2014-07-22 22:58:43 +02003512 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003513 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003514 static const struct option long_options[] = {
3515 {"help", no_argument, 0, 'h'},
3516 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003517 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003518 {0, 0, 0, 0}
3519 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003520 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003521 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003522 if (c == -1) {
3523 break;
3524 }
3525
3526 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003527 case ':':
3528 missing_argument(argv[optind - 1]);
3529 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003530 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003531 unrecognized_option(argv[optind - 1]);
3532 break;
3533 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003534 help();
3535 break;
3536 case 'o':
3537 if (!is_valid_option_list(optarg)) {
3538 error_report("Invalid option list: %s", optarg);
3539 ret = -1;
3540 goto out_no_progress;
3541 }
3542 if (!options) {
3543 options = g_strdup(optarg);
3544 } else {
3545 char *old_options = options;
3546 options = g_strdup_printf("%s,%s", options, optarg);
3547 g_free(old_options);
3548 }
3549 break;
3550 case 'f':
3551 fmt = optarg;
3552 break;
3553 case 't':
3554 cache = optarg;
3555 break;
3556 case 'p':
3557 progress = true;
3558 break;
3559 case 'q':
3560 quiet = true;
3561 break;
3562 case OPTION_OBJECT:
3563 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3564 optarg, true);
3565 if (!opts) {
3566 ret = -1;
3567 goto out_no_progress;
3568 }
3569 break;
3570 case OPTION_IMAGE_OPTS:
3571 image_opts = true;
3572 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003573 }
3574 }
3575
Max Reitz6f176b42013-09-03 10:09:50 +02003576 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003577 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003578 }
3579
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003580 if (qemu_opts_foreach(&qemu_object_opts,
3581 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003582 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003583 ret = -1;
3584 goto out_no_progress;
3585 }
3586
Max Reitz76a3a342014-10-27 11:12:51 +01003587 if (quiet) {
3588 progress = false;
3589 }
3590 qemu_progress_init(progress, 1.0);
3591
Kevin Wolfa283cb62014-02-21 16:24:07 +01003592 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3593 if (fmt && has_help_option(options)) {
3594 /* If a format is explicitly specified (and possibly no filename is
3595 * given), print option help here */
3596 ret = print_block_option_help(filename, fmt);
3597 goto out;
3598 }
3599
3600 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003601 error_report("Expecting one image file name");
3602 ret = -1;
3603 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003604 }
Max Reitz6f176b42013-09-03 10:09:50 +02003605
Kevin Wolfce099542016-03-15 13:01:04 +01003606 flags = BDRV_O_RDWR;
3607 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003608 if (ret < 0) {
3609 error_report("Invalid cache option: %s", cache);
3610 goto out;
3611 }
3612
Kevin Wolfce099542016-03-15 13:01:04 +01003613 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003614 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003615 ret = -1;
3616 goto out;
3617 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003618 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003619
3620 fmt = bs->drv->format_name;
3621
Kevin Wolf626f84f2014-02-21 16:24:06 +01003622 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003623 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003624 ret = print_block_option_help(filename, fmt);
3625 goto out;
3626 }
3627
Max Reitzb2439d22014-12-02 18:32:47 +01003628 if (!bs->drv->create_opts) {
3629 error_report("Format driver '%s' does not support any options to amend",
3630 fmt);
3631 ret = -1;
3632 goto out;
3633 }
3634
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003635 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003636 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003637 qemu_opts_do_parse(opts, options, NULL, &err);
3638 if (err) {
3639 error_report_err(err);
3640 ret = -1;
3641 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003642 }
3643
Max Reitz76a3a342014-10-27 11:12:51 +01003644 /* In case the driver does not call amend_status_cb() */
3645 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003646 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003647 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003648 if (ret < 0) {
3649 error_report("Error while amending options: %s", strerror(-ret));
3650 goto out;
3651 }
3652
3653out:
Max Reitz76a3a342014-10-27 11:12:51 +01003654 qemu_progress_end();
3655
Max Reitze814dff2015-08-20 16:00:38 -07003656out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003657 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003658 qemu_opts_del(opts);
3659 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003660 g_free(options);
3661
Max Reitz6f176b42013-09-03 10:09:50 +02003662 if (ret) {
3663 return 1;
3664 }
3665 return 0;
3666}
3667
Kevin Wolfb6133b82014-08-05 14:17:13 +02003668typedef struct BenchData {
3669 BlockBackend *blk;
3670 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003671 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003672 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003673 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003674 int nrreq;
3675 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003676 int flush_interval;
3677 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003678 uint8_t *buf;
3679 QEMUIOVector *qiov;
3680
3681 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003682 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003683 uint64_t offset;
3684} BenchData;
3685
Kevin Wolf55d539c2016-06-03 13:59:41 +02003686static void bench_undrained_flush_cb(void *opaque, int ret)
3687{
3688 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003689 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003690 exit(EXIT_FAILURE);
3691 }
3692}
3693
Kevin Wolfb6133b82014-08-05 14:17:13 +02003694static void bench_cb(void *opaque, int ret)
3695{
3696 BenchData *b = opaque;
3697 BlockAIOCB *acb;
3698
3699 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003700 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003701 exit(EXIT_FAILURE);
3702 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003703
3704 if (b->in_flush) {
3705 /* Just finished a flush with drained queue: Start next requests */
3706 assert(b->in_flight == 0);
3707 b->in_flush = false;
3708 } else if (b->in_flight > 0) {
3709 int remaining = b->n - b->in_flight;
3710
Kevin Wolfb6133b82014-08-05 14:17:13 +02003711 b->n--;
3712 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003713
3714 /* Time for flush? Drain queue if requested, then flush */
3715 if (b->flush_interval && remaining % b->flush_interval == 0) {
3716 if (!b->in_flight || !b->drain_on_flush) {
3717 BlockCompletionFunc *cb;
3718
3719 if (b->drain_on_flush) {
3720 b->in_flush = true;
3721 cb = bench_cb;
3722 } else {
3723 cb = bench_undrained_flush_cb;
3724 }
3725
3726 acb = blk_aio_flush(b->blk, cb, b);
3727 if (!acb) {
3728 error_report("Failed to issue flush request");
3729 exit(EXIT_FAILURE);
3730 }
3731 }
3732 if (b->drain_on_flush) {
3733 return;
3734 }
3735 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003736 }
3737
3738 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003739 int64_t offset = b->offset;
3740 /* blk_aio_* might look for completed I/Os and kick bench_cb
3741 * again, so make sure this operation is counted by in_flight
3742 * and b->offset is ready for the next submission.
3743 */
3744 b->in_flight++;
3745 b->offset += b->step;
3746 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003747 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003748 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003749 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003750 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003751 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003752 if (!acb) {
3753 error_report("Failed to issue request");
3754 exit(EXIT_FAILURE);
3755 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003756 }
3757}
3758
3759static int img_bench(int argc, char **argv)
3760{
3761 int c, ret = 0;
3762 const char *fmt = NULL, *filename;
3763 bool quiet = false;
3764 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003765 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003766 int count = 75000;
3767 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003768 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003769 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003770 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003771 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003772 int flush_interval = 0;
3773 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003774 int64_t image_size;
3775 BlockBackend *blk = NULL;
3776 BenchData data = {};
3777 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003778 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003779 struct timeval t1, t2;
3780 int i;
3781
3782 for (;;) {
3783 static const struct option long_options[] = {
3784 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003785 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003786 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003787 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003788 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003789 {0, 0, 0, 0}
3790 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003791 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:w", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003792 if (c == -1) {
3793 break;
3794 }
3795
3796 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003797 case ':':
3798 missing_argument(argv[optind - 1]);
3799 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003800 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003801 unrecognized_option(argv[optind - 1]);
3802 break;
3803 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02003804 help();
3805 break;
3806 case 'c':
3807 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003808 unsigned long res;
3809
3810 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003811 error_report("Invalid request count specified");
3812 return 1;
3813 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003814 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003815 break;
3816 }
3817 case 'd':
3818 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003819 unsigned long res;
3820
3821 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003822 error_report("Invalid queue depth specified");
3823 return 1;
3824 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003825 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003826 break;
3827 }
3828 case 'f':
3829 fmt = optarg;
3830 break;
3831 case 'n':
3832 flags |= BDRV_O_NATIVE_AIO;
3833 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003834 case 'o':
3835 {
Markus Armbruster606caa02017-02-21 21:14:04 +01003836 offset = cvtnum(optarg);
3837 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02003838 error_report("Invalid offset specified");
3839 return 1;
3840 }
3841 break;
3842 }
3843 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003844 case 'q':
3845 quiet = true;
3846 break;
3847 case 's':
3848 {
3849 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003850
Markus Armbruster606caa02017-02-21 21:14:04 +01003851 sval = cvtnum(optarg);
3852 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003853 error_report("Invalid buffer size specified");
3854 return 1;
3855 }
3856
3857 bufsize = sval;
3858 break;
3859 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003860 case 'S':
3861 {
3862 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003863
Markus Armbruster606caa02017-02-21 21:14:04 +01003864 sval = cvtnum(optarg);
3865 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02003866 error_report("Invalid step size specified");
3867 return 1;
3868 }
3869
3870 step = sval;
3871 break;
3872 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003873 case 't':
3874 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3875 if (ret < 0) {
3876 error_report("Invalid cache mode");
3877 ret = -1;
3878 goto out;
3879 }
3880 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003881 case 'w':
3882 flags |= BDRV_O_RDWR;
3883 is_write = true;
3884 break;
3885 case OPTION_PATTERN:
3886 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003887 unsigned long res;
3888
3889 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003890 error_report("Invalid pattern byte specified");
3891 return 1;
3892 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003893 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003894 break;
3895 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003896 case OPTION_FLUSH_INTERVAL:
3897 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003898 unsigned long res;
3899
3900 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02003901 error_report("Invalid flush interval specified");
3902 return 1;
3903 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003904 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003905 break;
3906 }
3907 case OPTION_NO_DRAIN:
3908 drain_on_flush = false;
3909 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003910 case OPTION_IMAGE_OPTS:
3911 image_opts = true;
3912 break;
3913 }
3914 }
3915
3916 if (optind != argc - 1) {
3917 error_exit("Expecting one image file name");
3918 }
3919 filename = argv[argc - 1];
3920
Kevin Wolf55d539c2016-06-03 13:59:41 +02003921 if (!is_write && flush_interval) {
3922 error_report("--flush-interval is only available in write tests");
3923 ret = -1;
3924 goto out;
3925 }
3926 if (flush_interval && flush_interval < depth) {
3927 error_report("Flush interval can't be smaller than depth");
3928 ret = -1;
3929 goto out;
3930 }
3931
Kevin Wolfb6133b82014-08-05 14:17:13 +02003932 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
3933 if (!blk) {
3934 ret = -1;
3935 goto out;
3936 }
3937
3938 image_size = blk_getlength(blk);
3939 if (image_size < 0) {
3940 ret = image_size;
3941 goto out;
3942 }
3943
3944 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02003945 .blk = blk,
3946 .image_size = image_size,
3947 .bufsize = bufsize,
3948 .step = step ?: bufsize,
3949 .nrreq = depth,
3950 .n = count,
3951 .offset = offset,
3952 .write = is_write,
3953 .flush_interval = flush_interval,
3954 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02003955 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02003956 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02003957 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02003958 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02003959 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02003960 if (flush_interval) {
3961 printf("Sending flush every %d requests\n", flush_interval);
3962 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003963
3964 data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003965 memset(data.buf, pattern, data.nrreq * data.bufsize);
3966
Kevin Wolfb6133b82014-08-05 14:17:13 +02003967 data.qiov = g_new(QEMUIOVector, data.nrreq);
3968 for (i = 0; i < data.nrreq; i++) {
3969 qemu_iovec_init(&data.qiov[i], 1);
3970 qemu_iovec_add(&data.qiov[i],
3971 data.buf + i * data.bufsize, data.bufsize);
3972 }
3973
3974 gettimeofday(&t1, NULL);
3975 bench_cb(&data, 0);
3976
3977 while (data.n > 0) {
3978 main_loop_wait(false);
3979 }
3980 gettimeofday(&t2, NULL);
3981
3982 printf("Run completed in %3.3f seconds.\n",
3983 (t2.tv_sec - t1.tv_sec)
3984 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
3985
3986out:
3987 qemu_vfree(data.buf);
3988 blk_unref(blk);
3989
3990 if (ret) {
3991 return 1;
3992 }
3993 return 0;
3994}
3995
Reda Sallahi86ce1f62016-08-10 04:43:12 +02003996#define C_BS 01
3997#define C_COUNT 02
3998#define C_IF 04
3999#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004000#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004001
4002struct DdInfo {
4003 unsigned int flags;
4004 int64_t count;
4005};
4006
4007struct DdIo {
4008 int bsz; /* Block size */
4009 char *filename;
4010 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004011 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004012};
4013
4014struct DdOpts {
4015 const char *name;
4016 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4017 unsigned int flag;
4018};
4019
4020static int img_dd_bs(const char *arg,
4021 struct DdIo *in, struct DdIo *out,
4022 struct DdInfo *dd)
4023{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004024 int64_t res;
4025
Markus Armbruster606caa02017-02-21 21:14:04 +01004026 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004027
Markus Armbruster606caa02017-02-21 21:14:04 +01004028 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004029 error_report("invalid number: '%s'", arg);
4030 return 1;
4031 }
4032 in->bsz = out->bsz = res;
4033
4034 return 0;
4035}
4036
4037static int img_dd_count(const char *arg,
4038 struct DdIo *in, struct DdIo *out,
4039 struct DdInfo *dd)
4040{
Markus Armbruster606caa02017-02-21 21:14:04 +01004041 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004042
Markus Armbruster606caa02017-02-21 21:14:04 +01004043 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004044 error_report("invalid number: '%s'", arg);
4045 return 1;
4046 }
4047
4048 return 0;
4049}
4050
4051static int img_dd_if(const char *arg,
4052 struct DdIo *in, struct DdIo *out,
4053 struct DdInfo *dd)
4054{
4055 in->filename = g_strdup(arg);
4056
4057 return 0;
4058}
4059
4060static int img_dd_of(const char *arg,
4061 struct DdIo *in, struct DdIo *out,
4062 struct DdInfo *dd)
4063{
4064 out->filename = g_strdup(arg);
4065
4066 return 0;
4067}
4068
Reda Sallahif7c15532016-08-10 16:16:09 +02004069static int img_dd_skip(const char *arg,
4070 struct DdIo *in, struct DdIo *out,
4071 struct DdInfo *dd)
4072{
Markus Armbruster606caa02017-02-21 21:14:04 +01004073 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004074
Markus Armbruster606caa02017-02-21 21:14:04 +01004075 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004076 error_report("invalid number: '%s'", arg);
4077 return 1;
4078 }
4079
4080 return 0;
4081}
4082
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004083static int img_dd(int argc, char **argv)
4084{
4085 int ret = 0;
4086 char *arg = NULL;
4087 char *tmp;
4088 BlockDriver *drv = NULL, *proto_drv = NULL;
4089 BlockBackend *blk1 = NULL, *blk2 = NULL;
4090 QemuOpts *opts = NULL;
4091 QemuOptsList *create_opts = NULL;
4092 Error *local_err = NULL;
4093 bool image_opts = false;
4094 int c, i;
4095 const char *out_fmt = "raw";
4096 const char *fmt = NULL;
4097 int64_t size = 0;
4098 int64_t block_count = 0, out_pos, in_pos;
4099 struct DdInfo dd = {
4100 .flags = 0,
4101 .count = 0,
4102 };
4103 struct DdIo in = {
4104 .bsz = 512, /* Block size is by default 512 bytes */
4105 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004106 .buf = NULL,
4107 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004108 };
4109 struct DdIo out = {
4110 .bsz = 512,
4111 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004112 .buf = NULL,
4113 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004114 };
4115
4116 const struct DdOpts options[] = {
4117 { "bs", img_dd_bs, C_BS },
4118 { "count", img_dd_count, C_COUNT },
4119 { "if", img_dd_if, C_IF },
4120 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004121 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004122 { NULL, NULL, 0 }
4123 };
4124 const struct option long_options[] = {
4125 { "help", no_argument, 0, 'h'},
4126 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4127 { 0, 0, 0, 0 }
4128 };
4129
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004130 while ((c = getopt_long(argc, argv, ":hf:O:", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004131 if (c == EOF) {
4132 break;
4133 }
4134 switch (c) {
4135 case 'O':
4136 out_fmt = optarg;
4137 break;
4138 case 'f':
4139 fmt = optarg;
4140 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004141 case ':':
4142 missing_argument(argv[optind - 1]);
4143 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004144 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004145 unrecognized_option(argv[optind - 1]);
4146 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004147 case 'h':
4148 help();
4149 break;
4150 case OPTION_IMAGE_OPTS:
4151 image_opts = true;
4152 break;
4153 }
4154 }
4155
4156 for (i = optind; i < argc; i++) {
4157 int j;
4158 arg = g_strdup(argv[i]);
4159
4160 tmp = strchr(arg, '=');
4161 if (tmp == NULL) {
4162 error_report("unrecognized operand %s", arg);
4163 ret = -1;
4164 goto out;
4165 }
4166
4167 *tmp++ = '\0';
4168
4169 for (j = 0; options[j].name != NULL; j++) {
4170 if (!strcmp(arg, options[j].name)) {
4171 break;
4172 }
4173 }
4174 if (options[j].name == NULL) {
4175 error_report("unrecognized operand %s", arg);
4176 ret = -1;
4177 goto out;
4178 }
4179
4180 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4181 ret = -1;
4182 goto out;
4183 }
4184 dd.flags |= options[j].flag;
4185 g_free(arg);
4186 arg = NULL;
4187 }
4188
4189 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4190 error_report("Must specify both input and output files");
4191 ret = -1;
4192 goto out;
4193 }
4194 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false);
4195
4196 if (!blk1) {
4197 ret = -1;
4198 goto out;
4199 }
4200
4201 drv = bdrv_find_format(out_fmt);
4202 if (!drv) {
4203 error_report("Unknown file format");
4204 ret = -1;
4205 goto out;
4206 }
4207 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4208
4209 if (!proto_drv) {
4210 error_report_err(local_err);
4211 ret = -1;
4212 goto out;
4213 }
4214 if (!drv->create_opts) {
4215 error_report("Format driver '%s' does not support image creation",
4216 drv->format_name);
4217 ret = -1;
4218 goto out;
4219 }
4220 if (!proto_drv->create_opts) {
4221 error_report("Protocol driver '%s' does not support image creation",
4222 proto_drv->format_name);
4223 ret = -1;
4224 goto out;
4225 }
4226 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4227 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4228
4229 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4230
4231 size = blk_getlength(blk1);
4232 if (size < 0) {
4233 error_report("Failed to get size for '%s'", in.filename);
4234 ret = -1;
4235 goto out;
4236 }
4237
4238 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4239 dd.count * in.bsz < size) {
4240 size = dd.count * in.bsz;
4241 }
4242
Reda Sallahif7c15532016-08-10 16:16:09 +02004243 /* Overflow means the specified offset is beyond input image's size */
4244 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4245 size < in.bsz * in.offset)) {
4246 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4247 } else {
4248 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4249 size - in.bsz * in.offset, &error_abort);
4250 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004251
4252 ret = bdrv_create(drv, out.filename, opts, &local_err);
4253 if (ret < 0) {
4254 error_reportf_err(local_err,
4255 "%s: error while creating output image: ",
4256 out.filename);
4257 ret = -1;
4258 goto out;
4259 }
4260
4261 blk2 = img_open(image_opts, out.filename, out_fmt, BDRV_O_RDWR,
4262 false, false);
4263
4264 if (!blk2) {
4265 ret = -1;
4266 goto out;
4267 }
4268
Reda Sallahif7c15532016-08-10 16:16:09 +02004269 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4270 size < in.offset * in.bsz)) {
4271 /* We give a warning if the skip option is bigger than the input
4272 * size and create an empty output disk image (i.e. like dd(1)).
4273 */
4274 error_report("%s: cannot skip to specified offset", in.filename);
4275 in_pos = size;
4276 } else {
4277 in_pos = in.offset * in.bsz;
4278 }
4279
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004280 in.buf = g_new(uint8_t, in.bsz);
4281
Reda Sallahif7c15532016-08-10 16:16:09 +02004282 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004283 int in_ret, out_ret;
4284
4285 if (in_pos + in.bsz > size) {
4286 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4287 } else {
4288 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4289 }
4290 if (in_ret < 0) {
4291 error_report("error while reading from input image file: %s",
4292 strerror(-in_ret));
4293 ret = -1;
4294 goto out;
4295 }
4296 in_pos += in_ret;
4297
4298 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4299
4300 if (out_ret < 0) {
4301 error_report("error while writing to output image file: %s",
4302 strerror(-out_ret));
4303 ret = -1;
4304 goto out;
4305 }
4306 out_pos += out_ret;
4307 }
4308
4309out:
4310 g_free(arg);
4311 qemu_opts_del(opts);
4312 qemu_opts_free(create_opts);
4313 blk_unref(blk1);
4314 blk_unref(blk2);
4315 g_free(in.filename);
4316 g_free(out.filename);
4317 g_free(in.buf);
4318 g_free(out.buf);
4319
4320 if (ret) {
4321 return 1;
4322 }
4323 return 0;
4324}
4325
Kevin Wolfb6133b82014-08-05 14:17:13 +02004326
Anthony Liguoric227f092009-10-01 16:12:16 -05004327static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004328#define DEF(option, callback, arg_string) \
4329 { option, callback },
4330#include "qemu-img-cmds.h"
4331#undef DEF
4332#undef GEN_DOCS
4333 { NULL, NULL, },
4334};
4335
bellardea2384d2004-08-01 21:59:26 +00004336int main(int argc, char **argv)
4337{
Anthony Liguoric227f092009-10-01 16:12:16 -05004338 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004339 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004340 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004341 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004342 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004343 static const struct option long_options[] = {
4344 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004345 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004346 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004347 {0, 0, 0, 0}
4348 };
bellardea2384d2004-08-01 21:59:26 +00004349
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004350#ifdef CONFIG_POSIX
4351 signal(SIGPIPE, SIG_IGN);
4352#endif
4353
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004354 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004355 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004356 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004357
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004358 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004359 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004360 exit(EXIT_FAILURE);
4361 }
4362
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004363 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004364
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004365 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004366 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004367 if (argc < 2) {
4368 error_exit("Not enough arguments");
4369 }
Stuart Brady153859b2009-06-07 00:42:17 +01004370
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004371 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004372 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004373 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004374
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004375 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004376 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004377 case ':':
4378 missing_argument(argv[optind - 1]);
4379 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08004380 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004381 unrecognized_option(argv[optind - 1]);
4382 return 0;
4383 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03004384 help();
4385 return 0;
4386 case 'V':
4387 printf(QEMU_IMG_VERSION);
4388 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004389 case 'T':
4390 g_free(trace_file);
4391 trace_file = trace_opt_parse(optarg);
4392 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004393 }
bellardea2384d2004-08-01 21:59:26 +00004394 }
Stuart Brady153859b2009-06-07 00:42:17 +01004395
Denis V. Lunev10985132016-06-17 17:44:13 +03004396 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004397
Denis V. Lunev10985132016-06-17 17:44:13 +03004398 /* reset getopt_long scanning */
4399 argc -= optind;
4400 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004401 return 0;
4402 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004403 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004404 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004405
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004406 if (!trace_init_backends()) {
4407 exit(1);
4408 }
4409 trace_init_file(trace_file);
4410 qemu_set_log(LOG_TRACE);
4411
Denis V. Lunev10985132016-06-17 17:44:13 +03004412 /* find the command */
4413 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4414 if (!strcmp(cmdname, cmd->name)) {
4415 return cmd->handler(argc, argv);
4416 }
4417 }
Jeff Cody7db16892014-04-25 17:02:32 -04004418
Stuart Brady153859b2009-06-07 00:42:17 +01004419 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004420 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004421}