blob: 0c76d4caa7a8050d5fdf680e610f11c36771a64d [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
blueswir1d2c639d2009-01-24 18:19:25 +000091/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +080092static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +000093{
Paolo Bonzinie00291c2010-02-04 16:49:56 +010094 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -040095 QEMU_IMG_VERSION
Denis V. Lunev10985132016-06-17 17:44:13 +030096 "usage: qemu-img [standard options] command [command options]\n"
malc3f020d72010-02-08 12:04:56 +030097 "QEMU disk image utility\n"
98 "\n"
Denis V. Lunev10985132016-06-17 17:44:13 +030099 " '-h', '--help' display this help and exit\n"
100 " '-V', '--version' output version information and exit\n"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +0300101 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
102 " specify tracing options\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300103 "\n"
malc3f020d72010-02-08 12:04:56 +0300104 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100105#define DEF(option, callback, arg_string) \
106 " " arg_string "\n"
107#include "qemu-img-cmds.h"
108#undef DEF
109#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +0300110 "\n"
111 "Command parameters:\n"
112 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000113 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
114 " manual page for a description of the object properties. The most common\n"
115 " object type is a 'secret', which is used to supply passwords and/or\n"
116 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300117 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400118 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800119 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
120 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100121 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
122 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300123 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200124 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
125 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
126 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300127 " 'output_filename' is the destination disk image filename\n"
128 " 'output_fmt' is the destination format\n"
129 " 'options' is a comma separated list of format specific options in a\n"
130 " name=value format. Use -o ? for an overview of the options supported by the\n"
131 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800132 " 'snapshot_param' is param used for internal snapshot, format\n"
133 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
134 " '[ID_OR_NAME]'\n"
135 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
136 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300137 " '-c' indicates that target image must be compressed (qcow format only)\n"
138 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
139 " match exactly. The image doesn't need a working backing file before\n"
140 " rebasing in this case (useful for renaming the backing file)\n"
141 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200142 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100143 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200144 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
145 " contain only zeros for qemu-img to create a sparse image during\n"
146 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
147 " unallocated or zero sectors, and the destination image will always be\n"
148 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200149 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100150 " '-n' skips the target volume creation (useful if the volume is created\n"
151 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300152 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200153 "Parameters to check subcommand:\n"
154 " '-r' tries to repair any inconsistencies that are found during the check.\n"
155 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
156 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200157 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200158 "\n"
Peter Lieven2d9187b2017-02-28 13:40:07 +0100159 "Parameters to convert subcommand:\n"
160 " '-m' specifies how many coroutines work in parallel during the convert\n"
161 " process (defaults to 8)\n"
162 " '-W' allow to write to the target out of order rather than sequential\n"
163 "\n"
malc3f020d72010-02-08 12:04:56 +0300164 "Parameters to snapshot subcommand:\n"
165 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
166 " '-a' applies a snapshot (revert disk to saved state)\n"
167 " '-c' creates a snapshot\n"
168 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100169 " '-l' lists all snapshots in the given image\n"
170 "\n"
171 "Parameters to compare subcommand:\n"
172 " '-f' first image format\n"
173 " '-F' second image format\n"
Reda Sallahi86ce1f62016-08-10 04:43:12 +0200174 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
175 "\n"
176 "Parameters to dd subcommand:\n"
177 " 'bs=BYTES' read and write up to BYTES bytes at a time "
178 "(default: 512)\n"
179 " 'count=N' copy only N input blocks\n"
180 " 'if=FILE' read from FILE\n"
Reda Sallahif7c15532016-08-10 16:16:09 +0200181 " 'of=FILE' write to FILE\n"
182 " 'skip=N' skip N bs-sized blocks at the start of input\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100183
184 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100185 bdrv_iterate_format(format_print, NULL);
bellardea2384d2004-08-01 21:59:26 +0000186 printf("\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800187 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000188}
189
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000190static QemuOptsList qemu_object_opts = {
191 .name = "object",
192 .implied_opt_name = "qom-type",
193 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
194 .desc = {
195 { }
196 },
197};
198
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000199static QemuOptsList qemu_source_opts = {
200 .name = "source",
201 .implied_opt_name = "file",
202 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
203 .desc = {
204 { }
205 },
206};
207
Stefan Weil7c30f652013-06-16 17:01:05 +0200208static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100209{
210 int ret = 0;
211 if (!quiet) {
212 va_list args;
213 va_start(args, fmt);
214 ret = vprintf(fmt, args);
215 va_end(args);
216 }
217 return ret;
218}
219
bellardea2384d2004-08-01 21:59:26 +0000220
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100221static int print_block_option_help(const char *filename, const char *fmt)
222{
223 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800224 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500225 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100226
227 /* Find driver and parse its options */
228 drv = bdrv_find_format(fmt);
229 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100230 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100231 return 1;
232 }
233
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800234 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100235 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500236 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100237 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100238 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800239 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100240 return 1;
241 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800242 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100243 }
244
Chunyan Liu83d05212014-06-05 17:20:51 +0800245 qemu_opts_print_help(create_opts);
246 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100247 return 0;
248}
249
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000250
251static int img_open_password(BlockBackend *blk, const char *filename,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000252 int flags, bool quiet)
bellard75c23802004-08-27 21:28:58 +0000253{
254 BlockDriverState *bs;
bellard75c23802004-08-27 21:28:58 +0000255 char password[256];
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000256
257 bs = blk_bs(blk);
Daniel P. Berrange4ef130f2016-03-21 14:11:43 +0000258 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs) &&
259 !(flags & BDRV_O_NO_IO)) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000260 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
261 if (qemu_read_password(password, sizeof(password)) < 0) {
262 error_report("No password given");
263 return -1;
264 }
265 if (bdrv_set_key(bs, password) < 0) {
266 error_report("invalid password");
267 return -1;
268 }
269 }
270 return 0;
271}
272
273
Max Reitzefaa7c42016-03-16 19:54:38 +0100274static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100275 QemuOpts *opts, int flags, bool writethrough,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000276 bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000277{
278 QDict *options;
279 Error *local_err = NULL;
280 BlockBackend *blk;
281 options = qemu_opts_to_qdict(opts, NULL);
Max Reitzefaa7c42016-03-16 19:54:38 +0100282 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000283 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100284 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000285 return NULL;
286 }
Kevin Wolfce099542016-03-15 13:01:04 +0100287 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000288
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000289 if (img_open_password(blk, optstr, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000290 blk_unref(blk);
291 return NULL;
292 }
293 return blk;
294}
295
Max Reitzefaa7c42016-03-16 19:54:38 +0100296static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000297 const char *fmt, int flags,
Kevin Wolfce099542016-03-15 13:01:04 +0100298 bool writethrough, bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000299{
300 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200301 Error *local_err = NULL;
Max Reitz5bd31322015-02-05 13:58:16 -0500302 QDict *options = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100303
bellard75c23802004-08-27 21:28:58 +0000304 if (fmt) {
Max Reitz5bd31322015-02-05 13:58:16 -0500305 options = qdict_new();
306 qdict_put(options, "driver", qstring_from_str(fmt));
bellard75c23802004-08-27 21:28:58 +0000307 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100308
Max Reitzefaa7c42016-03-16 19:54:38 +0100309 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500310 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100311 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000312 return NULL;
bellard75c23802004-08-27 21:28:58 +0000313 }
Kevin Wolfce099542016-03-15 13:01:04 +0100314 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100315
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000316 if (img_open_password(blk, filename, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000317 blk_unref(blk);
318 return NULL;
bellard75c23802004-08-27 21:28:58 +0000319 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200320 return blk;
bellard75c23802004-08-27 21:28:58 +0000321}
322
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000323
Max Reitzefaa7c42016-03-16 19:54:38 +0100324static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000325 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100326 const char *fmt, int flags, bool writethrough,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000327 bool quiet)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000328{
329 BlockBackend *blk;
330 if (image_opts) {
331 QemuOpts *opts;
332 if (fmt) {
333 error_report("--image-opts and --format are mutually exclusive");
334 return NULL;
335 }
336 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
337 filename, true);
338 if (!opts) {
339 return NULL;
340 }
Kevin Wolfce099542016-03-15 13:01:04 +0100341 blk = img_open_opts(filename, opts, flags, writethrough, quiet);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000342 } else {
Kevin Wolfce099542016-03-15 13:01:04 +0100343 blk = img_open_file(filename, fmt, flags, writethrough, quiet);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000344 }
345 return blk;
346}
347
348
Chunyan Liu83d05212014-06-05 17:20:51 +0800349static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100350 const char *base_filename,
351 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200352{
Markus Armbruster6750e792015-02-12 17:43:08 +0100353 Error *err = NULL;
354
Kevin Wolfefa84d42009-05-18 16:42:12 +0200355 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100356 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100357 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100358 error_report("Backing file not supported for file format '%s'",
359 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100360 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900361 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200362 }
363 }
364 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100365 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100366 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100367 error_report("Backing file format not supported for file "
368 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100369 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900370 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200371 }
372 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900373 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200374}
375
Markus Armbruster606caa02017-02-21 21:14:04 +0100376static int64_t cvtnum(const char *s)
377{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100378 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100379 uint64_t value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100380
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100381 err = qemu_strtosz(s, NULL, &value);
382 if (err < 0) {
383 return err;
384 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100385 if (value > INT64_MAX) {
386 return -ERANGE;
387 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100388 return value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100389}
390
bellardea2384d2004-08-01 21:59:26 +0000391static int img_create(int argc, char **argv)
392{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200393 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100394 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000395 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000396 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000397 const char *filename;
398 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200399 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200400 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100401 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000402
bellardea2384d2004-08-01 21:59:26 +0000403 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000404 static const struct option long_options[] = {
405 {"help", no_argument, 0, 'h'},
406 {"object", required_argument, 0, OPTION_OBJECT},
407 {0, 0, 0, 0}
408 };
409 c = getopt_long(argc, argv, "F:b:f:he6o:q",
410 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100411 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000412 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100413 }
bellardea2384d2004-08-01 21:59:26 +0000414 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100415 case '?':
bellardea2384d2004-08-01 21:59:26 +0000416 case 'h':
417 help();
418 break;
aliguori9230eaf2009-03-28 17:55:19 +0000419 case 'F':
420 base_fmt = optarg;
421 break;
bellardea2384d2004-08-01 21:59:26 +0000422 case 'b':
423 base_filename = optarg;
424 break;
425 case 'f':
426 fmt = optarg;
427 break;
428 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200429 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100430 "encryption\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100431 goto fail;
thsd8871c52007-10-24 16:11:42 +0000432 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200433 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100434 "compat6\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100435 goto fail;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200436 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100437 if (!is_valid_option_list(optarg)) {
438 error_report("Invalid option list: %s", optarg);
439 goto fail;
440 }
441 if (!options) {
442 options = g_strdup(optarg);
443 } else {
444 char *old_options = options;
445 options = g_strdup_printf("%s,%s", options, optarg);
446 g_free(old_options);
447 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200448 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100449 case 'q':
450 quiet = true;
451 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000452 case OPTION_OBJECT: {
453 QemuOpts *opts;
454 opts = qemu_opts_parse_noisily(&qemu_object_opts,
455 optarg, true);
456 if (!opts) {
457 goto fail;
458 }
459 } break;
bellardea2384d2004-08-01 21:59:26 +0000460 }
461 }
aliguori9230eaf2009-03-28 17:55:19 +0000462
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900463 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100464 filename = (optind < argc) ? argv[optind] : NULL;
465 if (options && has_help_option(options)) {
466 g_free(options);
467 return print_block_option_help(filename, fmt);
468 }
469
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100470 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800471 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100472 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100473 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900474
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000475 if (qemu_opts_foreach(&qemu_object_opts,
476 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200477 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000478 goto fail;
479 }
480
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100481 /* Get image size, if specified */
482 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100483 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100484
485 sval = cvtnum(argv[optind++]);
486 if (sval < 0) {
liguang79443392012-12-17 09:49:23 +0800487 if (sval == -ERANGE) {
488 error_report("Image size must be less than 8 EiB!");
489 } else {
490 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200491 "G, T, P or E suffixes for ");
492 error_report("kilobytes, megabytes, gigabytes, terabytes, "
493 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800494 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100495 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100496 }
497 img_size = (uint64_t)sval;
498 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200499 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800500 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200501 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100502
Luiz Capitulino9b375252012-11-30 10:52:05 -0200503 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Kevin Wolf61de4c62016-03-18 17:46:45 +0100504 options, img_size, 0, &local_err, quiet);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100505 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100506 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100507 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900508 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200509
Kevin Wolf77386bf2014-02-21 16:24:04 +0100510 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000511 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100512
513fail:
514 g_free(options);
515 return 1;
bellardea2384d2004-08-01 21:59:26 +0000516}
517
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100518static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500519{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500520 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500521 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100522 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600523
524 visit_type_ImageCheck(v, NULL, &check, &error_abort);
525 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500526 str = qobject_to_json_pretty(obj);
527 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100528 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500529 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600530 visit_free(v);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500531 QDECREF(str);
532}
533
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100534static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500535{
536 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100537 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500538 } else {
539 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100540 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
541 "Data may be corrupted, or further writes to the image "
542 "may corrupt it.\n",
543 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500544 }
545
546 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100547 qprintf(quiet,
548 "\n%" PRId64 " leaked clusters were found on the image.\n"
549 "This means waste of disk space, but no harm to data.\n",
550 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500551 }
552
553 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100554 qprintf(quiet,
555 "\n%" PRId64
556 " internal errors have occurred during the check.\n",
557 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500558 }
559 }
560
561 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100562 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
563 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
564 check->allocated_clusters, check->total_clusters,
565 check->allocated_clusters * 100.0 / check->total_clusters,
566 check->fragmented_clusters * 100.0 / check->allocated_clusters,
567 check->compressed_clusters * 100.0 /
568 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500569 }
570
571 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100572 qprintf(quiet,
573 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500574 }
575}
576
577static int collect_image_check(BlockDriverState *bs,
578 ImageCheck *check,
579 const char *filename,
580 const char *fmt,
581 int fix)
582{
583 int ret;
584 BdrvCheckResult result;
585
586 ret = bdrv_check(bs, &result, fix);
587 if (ret < 0) {
588 return ret;
589 }
590
591 check->filename = g_strdup(filename);
592 check->format = g_strdup(bdrv_get_format_name(bs));
593 check->check_errors = result.check_errors;
594 check->corruptions = result.corruptions;
595 check->has_corruptions = result.corruptions != 0;
596 check->leaks = result.leaks;
597 check->has_leaks = result.leaks != 0;
598 check->corruptions_fixed = result.corruptions_fixed;
599 check->has_corruptions_fixed = result.corruptions != 0;
600 check->leaks_fixed = result.leaks_fixed;
601 check->has_leaks_fixed = result.leaks != 0;
602 check->image_end_offset = result.image_end_offset;
603 check->has_image_end_offset = result.image_end_offset != 0;
604 check->total_clusters = result.bfi.total_clusters;
605 check->has_total_clusters = result.bfi.total_clusters != 0;
606 check->allocated_clusters = result.bfi.allocated_clusters;
607 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
608 check->fragmented_clusters = result.bfi.fragmented_clusters;
609 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100610 check->compressed_clusters = result.bfi.compressed_clusters;
611 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500612
613 return 0;
614}
615
Kevin Wolfe076f332010-06-29 11:43:13 +0200616/*
617 * Checks an image for consistency. Exit codes:
618 *
Max Reitzd6635c42014-06-02 22:15:21 +0200619 * 0 - Check completed, image is good
620 * 1 - Check not completed because of internal errors
621 * 2 - Check completed, image is corrupted
622 * 3 - Check completed, image has leaked clusters, but is good otherwise
623 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200624 */
aliguori15859692009-04-21 23:11:53 +0000625static int img_check(int argc, char **argv)
626{
627 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500628 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200629 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200630 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000631 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200632 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100633 int flags = BDRV_O_CHECK;
634 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200635 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100636 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000637 bool image_opts = false;
aliguori15859692009-04-21 23:11:53 +0000638
639 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500640 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200641 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100642
aliguori15859692009-04-21 23:11:53 +0000643 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500644 int option_index = 0;
645 static const struct option long_options[] = {
646 {"help", no_argument, 0, 'h'},
647 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530648 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500649 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000650 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000651 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500652 {0, 0, 0, 0}
653 };
Max Reitz40055952014-07-22 22:58:42 +0200654 c = getopt_long(argc, argv, "hf:r:T:q",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500655 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100656 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000657 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100658 }
aliguori15859692009-04-21 23:11:53 +0000659 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100660 case '?':
aliguori15859692009-04-21 23:11:53 +0000661 case 'h':
662 help();
663 break;
664 case 'f':
665 fmt = optarg;
666 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200667 case 'r':
668 flags |= BDRV_O_RDWR;
669
670 if (!strcmp(optarg, "leaks")) {
671 fix = BDRV_FIX_LEAKS;
672 } else if (!strcmp(optarg, "all")) {
673 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
674 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800675 error_exit("Unknown option value for -r "
676 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200677 }
678 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500679 case OPTION_OUTPUT:
680 output = optarg;
681 break;
Max Reitz40055952014-07-22 22:58:42 +0200682 case 'T':
683 cache = optarg;
684 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100685 case 'q':
686 quiet = true;
687 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000688 case OPTION_OBJECT: {
689 QemuOpts *opts;
690 opts = qemu_opts_parse_noisily(&qemu_object_opts,
691 optarg, true);
692 if (!opts) {
693 return 1;
694 }
695 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000696 case OPTION_IMAGE_OPTS:
697 image_opts = true;
698 break;
aliguori15859692009-04-21 23:11:53 +0000699 }
700 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200701 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800702 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100703 }
aliguori15859692009-04-21 23:11:53 +0000704 filename = argv[optind++];
705
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500706 if (output && !strcmp(output, "json")) {
707 output_format = OFORMAT_JSON;
708 } else if (output && !strcmp(output, "human")) {
709 output_format = OFORMAT_HUMAN;
710 } else if (output) {
711 error_report("--output must be used with human or json as argument.");
712 return 1;
713 }
714
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000715 if (qemu_opts_foreach(&qemu_object_opts,
716 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200717 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000718 return 1;
719 }
720
Kevin Wolfce099542016-03-15 13:01:04 +0100721 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200722 if (ret < 0) {
723 error_report("Invalid source cache option: %s", cache);
724 return 1;
725 }
726
Kevin Wolfce099542016-03-15 13:01:04 +0100727 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200728 if (!blk) {
729 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900730 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200731 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500732
733 check = g_new0(ImageCheck, 1);
734 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200735
736 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200737 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200738 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500739 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200740 }
741
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500742 if (check->corruptions_fixed || check->leaks_fixed) {
743 int corruptions_fixed, leaks_fixed;
744
745 leaks_fixed = check->leaks_fixed;
746 corruptions_fixed = check->corruptions_fixed;
747
748 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100749 qprintf(quiet,
750 "The following inconsistencies were found and repaired:\n\n"
751 " %" PRId64 " leaked clusters\n"
752 " %" PRId64 " corruptions\n\n"
753 "Double checking the fixed image now...\n",
754 check->leaks_fixed,
755 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500756 }
757
758 ret = collect_image_check(bs, check, filename, fmt, 0);
759
760 check->leaks_fixed = leaks_fixed;
761 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200762 }
763
Max Reitz832390a2014-10-23 15:29:12 +0200764 if (!ret) {
765 switch (output_format) {
766 case OFORMAT_HUMAN:
767 dump_human_image_check(check, quiet);
768 break;
769 case OFORMAT_JSON:
770 dump_json_image_check(check, quiet);
771 break;
772 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500773 }
774
775 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200776 if (ret) {
777 error_report("Check failed: %s", strerror(-ret));
778 } else {
779 error_report("Check failed");
780 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500781 ret = 1;
782 goto fail;
783 }
784
785 if (check->corruptions) {
786 ret = 2;
787 } else if (check->leaks) {
788 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200789 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500790 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000791 }
792
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500793fail:
794 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200795 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500796 return ret;
aliguori15859692009-04-21 23:11:53 +0000797}
798
Max Reitzd4a32382014-10-24 15:57:37 +0200799typedef struct CommonBlockJobCBInfo {
800 BlockDriverState *bs;
801 Error **errp;
802} CommonBlockJobCBInfo;
803
804static void common_block_job_cb(void *opaque, int ret)
805{
806 CommonBlockJobCBInfo *cbi = opaque;
807
808 if (ret < 0) {
809 error_setg_errno(cbi->errp, -ret, "Block job failed");
810 }
Max Reitzd4a32382014-10-24 15:57:37 +0200811}
812
813static void run_block_job(BlockJob *job, Error **errp)
814{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200815 AioContext *aio_context = blk_get_aio_context(job->blk);
Max Reitzd4a32382014-10-24 15:57:37 +0200816
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100817 /* FIXME In error cases, the job simply goes away and we access a dangling
818 * pointer below. */
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200819 aio_context_acquire(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +0200820 do {
821 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500822 qemu_progress_print(job->len ?
823 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200824 } while (!job->ready);
825
826 block_job_complete_sync(job, errp);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200827 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200828
829 /* A block job may finish instantaneously without publishing any progress,
830 * so just signal completion here */
831 qemu_progress_print(100.f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200832}
833
bellardea2384d2004-08-01 21:59:26 +0000834static int img_commit(int argc, char **argv)
835{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400836 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200837 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200838 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200839 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100840 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200841 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100842 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200843 Error *local_err = NULL;
844 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000845 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200846 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000847
848 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400849 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200850 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000851 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000852 static const struct option long_options[] = {
853 {"help", no_argument, 0, 'h'},
854 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000855 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000856 {0, 0, 0, 0}
857 };
858 c = getopt_long(argc, argv, "f:ht:b:dpq",
859 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100860 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000861 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100862 }
bellardea2384d2004-08-01 21:59:26 +0000863 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100864 case '?':
bellardea2384d2004-08-01 21:59:26 +0000865 case 'h':
866 help();
867 break;
868 case 'f':
869 fmt = optarg;
870 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400871 case 't':
872 cache = optarg;
873 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200874 case 'b':
875 base = optarg;
876 /* -b implies -d */
877 drop = true;
878 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200879 case 'd':
880 drop = true;
881 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200882 case 'p':
883 progress = true;
884 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100885 case 'q':
886 quiet = true;
887 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000888 case OPTION_OBJECT: {
889 QemuOpts *opts;
890 opts = qemu_opts_parse_noisily(&qemu_object_opts,
891 optarg, true);
892 if (!opts) {
893 return 1;
894 }
895 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000896 case OPTION_IMAGE_OPTS:
897 image_opts = true;
898 break;
bellardea2384d2004-08-01 21:59:26 +0000899 }
900 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200901
902 /* Progress is not shown in Quiet mode */
903 if (quiet) {
904 progress = false;
905 }
906
Kevin Wolffc11eb22013-08-05 10:53:04 +0200907 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800908 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100909 }
bellardea2384d2004-08-01 21:59:26 +0000910 filename = argv[optind++];
911
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000912 if (qemu_opts_foreach(&qemu_object_opts,
913 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200914 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000915 return 1;
916 }
917
Max Reitz9a86fe42014-10-24 15:57:38 +0200918 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100919 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400920 if (ret < 0) {
921 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100922 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400923 }
924
Kevin Wolfce099542016-03-15 13:01:04 +0100925 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200926 if (!blk) {
927 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900928 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200929 bs = blk_bs(blk);
930
Max Reitz687fa1d2014-10-24 15:57:39 +0200931 qemu_progress_init(progress, 1.f);
932 qemu_progress_print(0.f, 100);
933
Max Reitz1b22bff2014-10-24 15:57:40 +0200934 if (base) {
935 base_bs = bdrv_find_backing_image(bs, base);
936 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +0100937 error_setg(&local_err,
938 "Did not find '%s' in the backing chain of '%s'",
939 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +0200940 goto done;
941 }
942 } else {
943 /* This is different from QMP, which by default uses the deepest file in
944 * the backing chain (i.e., the very base); however, the traditional
945 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +0200946 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +0200947 if (!base_bs) {
948 error_setg(&local_err, "Image does not have a backing file");
949 goto done;
950 }
bellardea2384d2004-08-01 21:59:26 +0000951 }
952
Max Reitzd4a32382014-10-24 15:57:37 +0200953 cbi = (CommonBlockJobCBInfo){
954 .errp = &local_err,
955 .bs = bs,
956 };
957
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200958 aio_context = bdrv_get_aio_context(bs);
959 aio_context_acquire(aio_context);
John Snow47970df2016-10-27 12:06:57 -0400960 commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
961 BLOCKDEV_ON_ERROR_REPORT, common_block_job_cb, &cbi,
962 &local_err, false);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200963 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +0200964 if (local_err) {
965 goto done;
966 }
967
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200968 /* When the block job completes, the BlockBackend reference will point to
969 * the old backing file. In order to avoid that the top image is already
970 * deleted, so we can still empty it afterwards, increment the reference
971 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +0200972 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200973 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200974 }
975
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100976 job = block_job_get("commit");
977 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +0200978 if (local_err) {
979 goto unref_backing;
980 }
981
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200982 if (!drop && bs->drv->bdrv_make_empty) {
983 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200984 if (ret) {
985 error_setg_errno(&local_err, -ret, "Could not empty %s",
986 filename);
987 goto unref_backing;
988 }
989 }
990
991unref_backing:
992 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200993 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200994 }
Max Reitzd4a32382014-10-24 15:57:37 +0200995
996done:
Max Reitz687fa1d2014-10-24 15:57:39 +0200997 qemu_progress_end();
998
Markus Armbruster26f54e92014-10-07 13:59:04 +0200999 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001000
1001 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001002 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001003 return 1;
1004 }
Max Reitzd4a32382014-10-24 15:57:37 +02001005
1006 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001007 return 0;
1008}
1009
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001010/*
thsf58c7b32008-06-05 21:53:49 +00001011 * Returns true iff the first sector pointed to by 'buf' contains at least
1012 * a non-NUL byte.
1013 *
1014 * 'pnum' is set to the number of sectors (including and immediately following
1015 * the first one) that are known to be in the same allocated/unallocated state.
1016 */
bellardea2384d2004-08-01 21:59:26 +00001017static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1018{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001019 bool is_zero;
1020 int i;
bellardea2384d2004-08-01 21:59:26 +00001021
1022 if (n <= 0) {
1023 *pnum = 0;
1024 return 0;
1025 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001026 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001027 for(i = 1; i < n; i++) {
1028 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001029 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001030 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001031 }
bellardea2384d2004-08-01 21:59:26 +00001032 }
1033 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001034 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001035}
1036
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001037/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001038 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1039 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1040 * breaking up write requests for only small sparse areas.
1041 */
1042static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1043 int min)
1044{
1045 int ret;
1046 int num_checked, num_used;
1047
1048 if (n < min) {
1049 min = n;
1050 }
1051
1052 ret = is_allocated_sectors(buf, n, pnum);
1053 if (!ret) {
1054 return ret;
1055 }
1056
1057 num_used = *pnum;
1058 buf += BDRV_SECTOR_SIZE * *pnum;
1059 n -= *pnum;
1060 num_checked = num_used;
1061
1062 while (n > 0) {
1063 ret = is_allocated_sectors(buf, n, pnum);
1064
1065 buf += BDRV_SECTOR_SIZE * *pnum;
1066 n -= *pnum;
1067 num_checked += *pnum;
1068 if (ret) {
1069 num_used = num_checked;
1070 } else if (*pnum >= min) {
1071 break;
1072 }
1073 }
1074
1075 *pnum = num_used;
1076 return 1;
1077}
1078
1079/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001080 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1081 * buffers matches, non-zero otherwise.
1082 *
1083 * pnum is set to the number of sectors (including and immediately following
1084 * the first one) that are known to have the same comparison result
1085 */
1086static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1087 int *pnum)
1088{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001089 bool res;
1090 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001091
1092 if (n <= 0) {
1093 *pnum = 0;
1094 return 0;
1095 }
1096
1097 res = !!memcmp(buf1, buf2, 512);
1098 for(i = 1; i < n; i++) {
1099 buf1 += 512;
1100 buf2 += 512;
1101
1102 if (!!memcmp(buf1, buf2, 512) != res) {
1103 break;
1104 }
1105 }
1106
1107 *pnum = i;
1108 return res;
1109}
1110
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001111#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001112
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001113static int64_t sectors_to_bytes(int64_t sectors)
1114{
1115 return sectors << BDRV_SECTOR_BITS;
1116}
1117
1118static int64_t sectors_to_process(int64_t total, int64_t from)
1119{
1120 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1121}
1122
1123/*
1124 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1125 *
1126 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1127 * data and negative value on error.
1128 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001129 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001130 * @param sect_num: Number of first sector to check
1131 * @param sect_count: Number of sectors to check
1132 * @param filename: Name of disk file we are checking (logging purpose)
1133 * @param buffer: Allocated buffer for storing read data
1134 * @param quiet: Flag for quiet mode
1135 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001136static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001137 int sect_count, const char *filename,
1138 uint8_t *buffer, bool quiet)
1139{
1140 int pnum, ret = 0;
Eric Blake91669202016-05-06 10:26:43 -06001141 ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer,
1142 sect_count << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001143 if (ret < 0) {
1144 error_report("Error while reading offset %" PRId64 " of %s: %s",
1145 sectors_to_bytes(sect_num), filename, strerror(-ret));
1146 return ret;
1147 }
1148 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1149 if (ret || pnum != sect_count) {
1150 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1151 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1152 return 1;
1153 }
1154
1155 return 0;
1156}
1157
1158/*
1159 * Compares two images. Exit codes:
1160 *
1161 * 0 - Images are identical
1162 * 1 - Images differ
1163 * >1 - Error occurred
1164 */
1165static int img_compare(int argc, char **argv)
1166{
Max Reitz40055952014-07-22 22:58:42 +02001167 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001168 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001169 BlockDriverState *bs1, *bs2;
1170 int64_t total_sectors1, total_sectors2;
1171 uint8_t *buf1 = NULL, *buf2 = NULL;
1172 int pnum1, pnum2;
1173 int allocated1, allocated2;
1174 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1175 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001176 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001177 bool writethrough;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001178 int64_t total_sectors;
1179 int64_t sector_num = 0;
1180 int64_t nb_sectors;
1181 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001182 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001183 bool image_opts = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001184
Max Reitz40055952014-07-22 22:58:42 +02001185 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001186 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001187 static const struct option long_options[] = {
1188 {"help", no_argument, 0, 'h'},
1189 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001190 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001191 {0, 0, 0, 0}
1192 };
1193 c = getopt_long(argc, argv, "hf:F:T:pqs",
1194 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001195 if (c == -1) {
1196 break;
1197 }
1198 switch (c) {
1199 case '?':
1200 case 'h':
1201 help();
1202 break;
1203 case 'f':
1204 fmt1 = optarg;
1205 break;
1206 case 'F':
1207 fmt2 = optarg;
1208 break;
Max Reitz40055952014-07-22 22:58:42 +02001209 case 'T':
1210 cache = optarg;
1211 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001212 case 'p':
1213 progress = true;
1214 break;
1215 case 'q':
1216 quiet = true;
1217 break;
1218 case 's':
1219 strict = true;
1220 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001221 case OPTION_OBJECT: {
1222 QemuOpts *opts;
1223 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1224 optarg, true);
1225 if (!opts) {
1226 ret = 2;
1227 goto out4;
1228 }
1229 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001230 case OPTION_IMAGE_OPTS:
1231 image_opts = true;
1232 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001233 }
1234 }
1235
1236 /* Progress is not shown in Quiet mode */
1237 if (quiet) {
1238 progress = false;
1239 }
1240
1241
Kevin Wolffc11eb22013-08-05 10:53:04 +02001242 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001243 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001244 }
1245 filename1 = argv[optind++];
1246 filename2 = argv[optind++];
1247
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001248 if (qemu_opts_foreach(&qemu_object_opts,
1249 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001250 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001251 ret = 2;
1252 goto out4;
1253 }
1254
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001255 /* Initialize before goto out */
1256 qemu_progress_init(progress, 2.0);
1257
Kevin Wolfce099542016-03-15 13:01:04 +01001258 flags = 0;
1259 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001260 if (ret < 0) {
1261 error_report("Invalid source cache option: %s", cache);
1262 ret = 2;
1263 goto out3;
1264 }
1265
Kevin Wolfce099542016-03-15 13:01:04 +01001266 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001267 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001268 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001269 goto out3;
1270 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001271
Kevin Wolfce099542016-03-15 13:01:04 +01001272 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001273 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001274 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001275 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001276 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001277 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001278 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001279
Max Reitzf1d3cd72015-02-05 13:58:18 -05001280 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1281 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1282 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001283 if (total_sectors1 < 0) {
1284 error_report("Can't get size of %s: %s",
1285 filename1, strerror(-total_sectors1));
1286 ret = 4;
1287 goto out;
1288 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001289 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001290 if (total_sectors2 < 0) {
1291 error_report("Can't get size of %s: %s",
1292 filename2, strerror(-total_sectors2));
1293 ret = 4;
1294 goto out;
1295 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001296 total_sectors = MIN(total_sectors1, total_sectors2);
1297 progress_base = MAX(total_sectors1, total_sectors2);
1298
1299 qemu_progress_print(0, 100);
1300
1301 if (strict && total_sectors1 != total_sectors2) {
1302 ret = 1;
1303 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1304 goto out;
1305 }
1306
1307 for (;;) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001308 int64_t status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001309 BlockDriverState *file;
1310
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001311 nb_sectors = sectors_to_process(total_sectors, sector_num);
1312 if (nb_sectors <= 0) {
1313 break;
1314 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001315 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1316 total_sectors1 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001317 &pnum1, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001318 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001319 ret = 3;
1320 error_report("Sector allocation test failed for %s", filename1);
1321 goto out;
1322 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001323 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001324
Fam Zheng25ad8e62016-01-13 16:37:41 +08001325 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1326 total_sectors2 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001327 &pnum2, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001328 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001329 ret = 3;
1330 error_report("Sector allocation test failed for %s", filename2);
1331 goto out;
1332 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001333 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1334 if (pnum1) {
1335 nb_sectors = MIN(nb_sectors, pnum1);
1336 }
1337 if (pnum2) {
1338 nb_sectors = MIN(nb_sectors, pnum2);
1339 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001340
Fam Zheng25ad8e62016-01-13 16:37:41 +08001341 if (strict) {
1342 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1343 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1344 ret = 1;
1345 qprintf(quiet, "Strict mode: Offset %" PRId64
1346 " block status mismatch!\n",
1347 sectors_to_bytes(sector_num));
1348 goto out;
1349 }
1350 }
1351 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1352 nb_sectors = MIN(pnum1, pnum2);
1353 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001354 if (allocated1) {
Eric Blake91669202016-05-06 10:26:43 -06001355 ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1,
1356 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001357 if (ret < 0) {
1358 error_report("Error while reading offset %" PRId64 " of %s:"
1359 " %s", sectors_to_bytes(sector_num), filename1,
1360 strerror(-ret));
1361 ret = 4;
1362 goto out;
1363 }
Eric Blake91669202016-05-06 10:26:43 -06001364 ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2,
1365 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001366 if (ret < 0) {
1367 error_report("Error while reading offset %" PRId64
1368 " of %s: %s", sectors_to_bytes(sector_num),
1369 filename2, strerror(-ret));
1370 ret = 4;
1371 goto out;
1372 }
1373 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1374 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001375 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1376 sectors_to_bytes(
1377 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001378 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001379 goto out;
1380 }
1381 }
1382 } else {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001383
1384 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001385 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001386 filename1, buf1, quiet);
1387 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001388 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001389 filename2, buf1, quiet);
1390 }
1391 if (ret) {
1392 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001393 error_report("Error while reading offset %" PRId64 ": %s",
1394 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001395 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001396 }
1397 goto out;
1398 }
1399 }
1400 sector_num += nb_sectors;
1401 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1402 }
1403
1404 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001405 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001406 int64_t total_sectors_over;
1407 const char *filename_over;
1408
1409 qprintf(quiet, "Warning: Image size mismatch!\n");
1410 if (total_sectors1 > total_sectors2) {
1411 total_sectors_over = total_sectors1;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001412 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001413 filename_over = filename1;
1414 } else {
1415 total_sectors_over = total_sectors2;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001416 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001417 filename_over = filename2;
1418 }
1419
1420 for (;;) {
1421 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1422 if (nb_sectors <= 0) {
1423 break;
1424 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001425 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001426 nb_sectors, &pnum);
1427 if (ret < 0) {
1428 ret = 3;
1429 error_report("Sector allocation test failed for %s",
1430 filename_over);
1431 goto out;
1432
1433 }
1434 nb_sectors = pnum;
1435 if (ret) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001436 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001437 filename_over, buf1, quiet);
1438 if (ret) {
1439 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001440 error_report("Error while reading offset %" PRId64
1441 " of %s: %s", sectors_to_bytes(sector_num),
1442 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001443 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001444 }
1445 goto out;
1446 }
1447 }
1448 sector_num += nb_sectors;
1449 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1450 }
1451 }
1452
1453 qprintf(quiet, "Images are identical.\n");
1454 ret = 0;
1455
1456out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001457 qemu_vfree(buf1);
1458 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001459 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001460out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001461 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001462out3:
1463 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001464out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001465 return ret;
1466}
1467
Kevin Wolf690c7302015-03-19 13:33:32 +01001468enum ImgConvertBlockStatus {
1469 BLK_DATA,
1470 BLK_ZERO,
1471 BLK_BACKING_FILE,
1472};
1473
Peter Lieven2d9187b2017-02-28 13:40:07 +01001474#define MAX_COROUTINES 16
1475
Kevin Wolf690c7302015-03-19 13:33:32 +01001476typedef struct ImgConvertState {
1477 BlockBackend **src;
1478 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001479 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001480 int64_t total_sectors;
1481 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001482 int64_t allocated_done;
1483 int64_t sector_num;
1484 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001485 enum ImgConvertBlockStatus status;
1486 int64_t sector_next_status;
1487 BlockBackend *target;
1488 bool has_zero_init;
1489 bool compressed;
1490 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001491 bool wr_in_order;
Kevin Wolf690c7302015-03-19 13:33:32 +01001492 int min_sparse;
1493 size_t cluster_sectors;
1494 size_t buf_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001495 int num_coroutines;
1496 int running_coroutines;
1497 Coroutine *co[MAX_COROUTINES];
1498 int64_t wait_sector_num[MAX_COROUTINES];
1499 CoMutex lock;
1500 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001501} ImgConvertState;
1502
Peter Lieven2d9187b2017-02-28 13:40:07 +01001503static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1504 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001505{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001506 *src_cur = 0;
1507 *src_cur_offset = 0;
1508 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1509 *src_cur_offset += s->src_sectors[*src_cur];
1510 (*src_cur)++;
1511 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001512 }
1513}
1514
1515static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1516{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001517 int64_t ret, src_cur_offset;
1518 int n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001519
Peter Lieven2d9187b2017-02-28 13:40:07 +01001520 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001521
1522 assert(s->total_sectors > sector_num);
1523 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1524
1525 if (s->sector_next_status <= sector_num) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08001526 BlockDriverState *file;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001527 ret = bdrv_get_block_status(blk_bs(s->src[src_cur]),
1528 sector_num - src_cur_offset,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001529 n, &n, &file);
Kevin Wolf690c7302015-03-19 13:33:32 +01001530 if (ret < 0) {
1531 return ret;
1532 }
1533
1534 if (ret & BDRV_BLOCK_ZERO) {
1535 s->status = BLK_ZERO;
1536 } else if (ret & BDRV_BLOCK_DATA) {
1537 s->status = BLK_DATA;
1538 } else if (!s->target_has_backing) {
1539 /* Without a target backing file we must copy over the contents of
1540 * the backing file as well. */
Ren Kimura263a6f42016-04-28 01:04:58 +09001541 /* Check block status of the backing file chain to avoid
Kevin Wolf690c7302015-03-19 13:33:32 +01001542 * needlessly reading zeroes and limiting the iteration to the
1543 * buffer size */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001544 ret = bdrv_get_block_status_above(blk_bs(s->src[src_cur]), NULL,
1545 sector_num - src_cur_offset,
Ren Kimura263a6f42016-04-28 01:04:58 +09001546 n, &n, &file);
1547 if (ret < 0) {
1548 return ret;
1549 }
1550
1551 if (ret & BDRV_BLOCK_ZERO) {
1552 s->status = BLK_ZERO;
1553 } else {
1554 s->status = BLK_DATA;
1555 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001556 } else {
1557 s->status = BLK_BACKING_FILE;
1558 }
1559
1560 s->sector_next_status = sector_num + n;
1561 }
1562
1563 n = MIN(n, s->sector_next_status - sector_num);
1564 if (s->status == BLK_DATA) {
1565 n = MIN(n, s->buf_sectors);
1566 }
1567
1568 /* We need to write complete clusters for compressed images, so if an
1569 * unallocated area is shorter than that, we must consider the whole
1570 * cluster allocated. */
1571 if (s->compressed) {
1572 if (n < s->cluster_sectors) {
1573 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1574 s->status = BLK_DATA;
1575 } else {
1576 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1577 }
1578 }
1579
1580 return n;
1581}
1582
Peter Lieven2d9187b2017-02-28 13:40:07 +01001583static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1584 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001585{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001586 int n, ret;
1587 QEMUIOVector qiov;
1588 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001589
Kevin Wolf690c7302015-03-19 13:33:32 +01001590 assert(nb_sectors <= s->buf_sectors);
1591 while (nb_sectors > 0) {
1592 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001593 int src_cur;
1594 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001595
1596 /* In the case of compression with multiple source files, we can get a
1597 * nb_sectors that spreads into the next part. So we must be able to
1598 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001599 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1600 blk = s->src[src_cur];
1601 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001602
Peter Lieven2d9187b2017-02-28 13:40:07 +01001603 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1604 iov.iov_base = buf;
1605 iov.iov_len = n << BDRV_SECTOR_BITS;
1606 qemu_iovec_init_external(&qiov, &iov, 1);
1607
1608 ret = blk_co_preadv(
1609 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1610 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001611 if (ret < 0) {
1612 return ret;
1613 }
1614
1615 sector_num += n;
1616 nb_sectors -= n;
1617 buf += n * BDRV_SECTOR_SIZE;
1618 }
1619
1620 return 0;
1621}
1622
Peter Lieven2d9187b2017-02-28 13:40:07 +01001623
1624static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1625 int nb_sectors, uint8_t *buf,
1626 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001627{
1628 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001629 QEMUIOVector qiov;
1630 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001631
1632 while (nb_sectors > 0) {
1633 int n = nb_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001634 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001635 case BLK_BACKING_FILE:
1636 /* If we have a backing file, leave clusters unallocated that are
1637 * unallocated in the source image, so that the backing file is
1638 * visible at the respective offset. */
1639 assert(s->target_has_backing);
1640 break;
1641
1642 case BLK_DATA:
1643 /* We must always write compressed clusters as a whole, so don't
1644 * try to find zeroed parts in the buffer. We can only save the
1645 * write if the buffer is completely zeroed and we're allowed to
1646 * keep the target sparse. */
1647 if (s->compressed) {
1648 if (s->has_zero_init && s->min_sparse &&
1649 buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))
1650 {
1651 assert(!s->target_has_backing);
1652 break;
1653 }
1654
Peter Lieven2d9187b2017-02-28 13:40:07 +01001655 iov.iov_base = buf;
1656 iov.iov_len = n << BDRV_SECTOR_BITS;
1657 qemu_iovec_init_external(&qiov, &iov, 1);
1658
1659 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
1660 n << BDRV_SECTOR_BITS, &qiov,
1661 BDRV_REQ_WRITE_COMPRESSED);
Kevin Wolf690c7302015-03-19 13:33:32 +01001662 if (ret < 0) {
1663 return ret;
1664 }
1665 break;
1666 }
1667
1668 /* If there is real non-zero data or we're told to keep the target
1669 * fully allocated (-S 0), we must write it. Otherwise we can treat
1670 * it as zero sectors. */
1671 if (!s->min_sparse ||
1672 is_allocated_sectors_min(buf, n, &n, s->min_sparse))
1673 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001674 iov.iov_base = buf;
1675 iov.iov_len = n << BDRV_SECTOR_BITS;
1676 qemu_iovec_init_external(&qiov, &iov, 1);
1677
1678 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
1679 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001680 if (ret < 0) {
1681 return ret;
1682 }
1683 break;
1684 }
1685 /* fall-through */
1686
1687 case BLK_ZERO:
1688 if (s->has_zero_init) {
1689 break;
1690 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001691 ret = blk_co_pwrite_zeroes(s->target,
1692 sector_num << BDRV_SECTOR_BITS,
1693 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001694 if (ret < 0) {
1695 return ret;
1696 }
1697 break;
1698 }
1699
1700 sector_num += n;
1701 nb_sectors -= n;
1702 buf += n * BDRV_SECTOR_SIZE;
1703 }
1704
1705 return 0;
1706}
1707
Peter Lieven2d9187b2017-02-28 13:40:07 +01001708static void coroutine_fn convert_co_do_copy(void *opaque)
1709{
1710 ImgConvertState *s = opaque;
1711 uint8_t *buf = NULL;
1712 int ret, i;
1713 int index = -1;
1714
1715 for (i = 0; i < s->num_coroutines; i++) {
1716 if (s->co[i] == qemu_coroutine_self()) {
1717 index = i;
1718 break;
1719 }
1720 }
1721 assert(index >= 0);
1722
1723 s->running_coroutines++;
1724 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1725
1726 while (1) {
1727 int n;
1728 int64_t sector_num;
1729 enum ImgConvertBlockStatus status;
1730
1731 qemu_co_mutex_lock(&s->lock);
1732 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1733 qemu_co_mutex_unlock(&s->lock);
1734 goto out;
1735 }
1736 n = convert_iteration_sectors(s, s->sector_num);
1737 if (n < 0) {
1738 qemu_co_mutex_unlock(&s->lock);
1739 s->ret = n;
1740 goto out;
1741 }
1742 /* save current sector and allocation status to local variables */
1743 sector_num = s->sector_num;
1744 status = s->status;
1745 if (!s->min_sparse && s->status == BLK_ZERO) {
1746 n = MIN(n, s->buf_sectors);
1747 }
1748 /* increment global sector counter so that other coroutines can
1749 * already continue reading beyond this request */
1750 s->sector_num += n;
1751 qemu_co_mutex_unlock(&s->lock);
1752
1753 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1754 s->allocated_done += n;
1755 qemu_progress_print(100.0 * s->allocated_done /
1756 s->allocated_sectors, 0);
1757 }
1758
1759 if (status == BLK_DATA) {
1760 ret = convert_co_read(s, sector_num, n, buf);
1761 if (ret < 0) {
1762 error_report("error while reading sector %" PRId64
1763 ": %s", sector_num, strerror(-ret));
1764 s->ret = ret;
1765 goto out;
1766 }
1767 } else if (!s->min_sparse && status == BLK_ZERO) {
1768 status = BLK_DATA;
1769 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1770 }
1771
1772 if (s->wr_in_order) {
1773 /* keep writes in order */
1774 while (s->wr_offs != sector_num) {
1775 if (s->ret != -EINPROGRESS) {
1776 goto out;
1777 }
1778 s->wait_sector_num[index] = sector_num;
1779 qemu_coroutine_yield();
1780 }
1781 s->wait_sector_num[index] = -1;
1782 }
1783
1784 ret = convert_co_write(s, sector_num, n, buf, status);
1785 if (ret < 0) {
1786 error_report("error while writing sector %" PRId64
1787 ": %s", sector_num, strerror(-ret));
1788 s->ret = ret;
1789 goto out;
1790 }
1791
1792 if (s->wr_in_order) {
1793 /* reenter the coroutine that might have waited
1794 * for this write to complete */
1795 s->wr_offs = sector_num + n;
1796 for (i = 0; i < s->num_coroutines; i++) {
1797 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1798 /*
1799 * A -> B -> A cannot occur because A has
1800 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1801 * B will never enter A during this time window.
1802 */
1803 qemu_coroutine_enter(s->co[i]);
1804 break;
1805 }
1806 }
1807 }
1808 }
1809
1810out:
1811 qemu_vfree(buf);
1812 s->co[index] = NULL;
1813 s->running_coroutines--;
1814 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1815 /* the convert job finished successfully */
1816 s->ret = 0;
1817 }
1818}
1819
Kevin Wolf690c7302015-03-19 13:33:32 +01001820static int convert_do_copy(ImgConvertState *s)
1821{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001822 int ret, i, n;
1823 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001824
1825 /* Check whether we have zero initialisation or can get it efficiently */
1826 s->has_zero_init = s->min_sparse && !s->target_has_backing
1827 ? bdrv_has_zero_init(blk_bs(s->target))
1828 : false;
1829
1830 if (!s->has_zero_init && !s->target_has_backing &&
1831 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1832 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001833 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001834 if (ret == 0) {
1835 s->has_zero_init = true;
1836 }
1837 }
1838
1839 /* Allocate buffer for copied data. For compressed images, only one cluster
1840 * can be copied at a time. */
1841 if (s->compressed) {
1842 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1843 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001844 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001845 }
1846 s->buf_sectors = s->cluster_sectors;
1847 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001848
Kevin Wolf690c7302015-03-19 13:33:32 +01001849 while (sector_num < s->total_sectors) {
1850 n = convert_iteration_sectors(s, sector_num);
1851 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001852 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001853 }
Max Reitzaad15de2016-03-24 23:33:57 +01001854 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1855 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001856 s->allocated_sectors += n;
1857 }
1858 sector_num += n;
1859 }
1860
1861 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001862 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001863 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001864
Peter Lieven2d9187b2017-02-28 13:40:07 +01001865 qemu_co_mutex_init(&s->lock);
1866 for (i = 0; i < s->num_coroutines; i++) {
1867 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1868 s->wait_sector_num[i] = -1;
1869 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001870 }
1871
Peter Lieven2d9187b2017-02-28 13:40:07 +01001872 while (s->ret == -EINPROGRESS) {
1873 main_loop_wait(false);
1874 }
1875
1876 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001877 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001878 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001879 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001880 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001881 }
1882 }
1883
Peter Lieven2d9187b2017-02-28 13:40:07 +01001884 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001885}
1886
bellardea2384d2004-08-01 21:59:26 +00001887static int img_convert(int argc, char **argv)
1888{
Kevin Wolf690c7302015-03-19 13:33:32 +01001889 int c, bs_n, bs_i, compress, cluster_sectors, skip_create;
Peter Lieven13c28af2013-11-27 11:07:01 +01001890 int64_t ret = 0;
Max Reitz40055952014-07-22 22:58:42 +02001891 int progress = 0, flags, src_flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001892 bool writethrough, src_writethrough;
Max Reitz40055952014-07-22 22:58:42 +02001893 const char *fmt, *out_fmt, *cache, *src_cache, *out_baseimg, *out_filename;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001894 BlockDriver *drv, *proto_drv;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001895 BlockBackend **blk = NULL, *out_blk = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001896 BlockDriverState **bs = NULL, *out_bs = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001897 int64_t total_sectors;
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001898 int64_t *bs_sectors = NULL;
Peter Lievenf2521c92013-11-27 11:07:06 +01001899 size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE;
bellardfaea38e2006-08-05 21:31:00 +00001900 BlockDriverInfo bdi;
Chunyan Liu83d05212014-06-05 17:20:51 +08001901 QemuOpts *opts = NULL;
1902 QemuOptsList *create_opts = NULL;
1903 const char *out_baseimg_param;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001904 char *options = NULL;
edison51ef6722010-09-21 19:58:41 -07001905 const char *snapshot_name = NULL;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001906 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001907 bool quiet = false;
Max Reitzcc84d902013-09-06 17:14:26 +02001908 Error *local_err = NULL;
Wenchao Xiaef806542013-12-04 17:10:57 +08001909 QemuOpts *sn_opts = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001910 ImgConvertState state;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001911 bool image_opts = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001912 bool wr_in_order = true;
1913 long num_coroutines = 8;
bellardea2384d2004-08-01 21:59:26 +00001914
1915 fmt = NULL;
1916 out_fmt = "raw";
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001917 cache = "unsafe";
Max Reitz40055952014-07-22 22:58:42 +02001918 src_cache = BDRV_DEFAULT_CACHE;
thsf58c7b32008-06-05 21:53:49 +00001919 out_baseimg = NULL;
Jes Sorenseneec77d92010-12-07 17:44:34 +01001920 compress = 0;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001921 skip_create = 0;
bellardea2384d2004-08-01 21:59:26 +00001922 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001923 static const struct option long_options[] = {
1924 {"help", no_argument, 0, 'h'},
1925 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001926 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001927 {0, 0, 0, 0}
1928 };
Peter Lieven2d9187b2017-02-28 13:40:07 +01001929 c = getopt_long(argc, argv, "hf:O:B:ce6o:s:l:S:pt:T:qnm:W",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001930 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001931 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001932 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001933 }
bellardea2384d2004-08-01 21:59:26 +00001934 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001935 case '?':
bellardea2384d2004-08-01 21:59:26 +00001936 case 'h':
1937 help();
1938 break;
1939 case 'f':
1940 fmt = optarg;
1941 break;
1942 case 'O':
1943 out_fmt = optarg;
1944 break;
thsf58c7b32008-06-05 21:53:49 +00001945 case 'B':
1946 out_baseimg = optarg;
1947 break;
bellardea2384d2004-08-01 21:59:26 +00001948 case 'c':
Jes Sorenseneec77d92010-12-07 17:44:34 +01001949 compress = 1;
bellardea2384d2004-08-01 21:59:26 +00001950 break;
1951 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001952 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001953 "encryption\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001954 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001955 goto fail_getopt;
thsec36ba12007-09-16 21:59:02 +00001956 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001957 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001958 "compat6\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001959 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001960 goto fail_getopt;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001961 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001962 if (!is_valid_option_list(optarg)) {
1963 error_report("Invalid option list: %s", optarg);
1964 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001965 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001966 }
1967 if (!options) {
1968 options = g_strdup(optarg);
1969 } else {
1970 char *old_options = options;
1971 options = g_strdup_printf("%s,%s", options, optarg);
1972 g_free(old_options);
1973 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001974 break;
edison51ef6722010-09-21 19:58:41 -07001975 case 's':
1976 snapshot_name = optarg;
1977 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001978 case 'l':
1979 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01001980 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
1981 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08001982 if (!sn_opts) {
1983 error_report("Failed in parsing snapshot param '%s'",
1984 optarg);
Kevin Wolf2dc83282014-02-21 16:24:05 +01001985 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001986 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08001987 }
1988 } else {
1989 snapshot_name = optarg;
1990 }
1991 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001992 case 'S':
1993 {
1994 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01001995
1996 sval = cvtnum(optarg);
1997 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02001998 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001999 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002000 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002001 }
2002
2003 min_sparse = sval / BDRV_SECTOR_SIZE;
2004 break;
2005 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002006 case 'p':
2007 progress = 1;
2008 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002009 case 't':
2010 cache = optarg;
2011 break;
Max Reitz40055952014-07-22 22:58:42 +02002012 case 'T':
2013 src_cache = optarg;
2014 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002015 case 'q':
2016 quiet = true;
2017 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002018 case 'n':
2019 skip_create = 1;
2020 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002021 case 'm':
2022 if (qemu_strtol(optarg, NULL, 0, &num_coroutines) ||
2023 num_coroutines < 1 || num_coroutines > MAX_COROUTINES) {
2024 error_report("Invalid number of coroutines. Allowed number of"
2025 " coroutines is between 1 and %d", MAX_COROUTINES);
2026 ret = -1;
2027 goto fail_getopt;
2028 }
2029 break;
2030 case 'W':
2031 wr_in_order = false;
2032 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002033 case OPTION_OBJECT:
2034 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2035 optarg, true);
2036 if (!opts) {
2037 goto fail_getopt;
2038 }
2039 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002040 case OPTION_IMAGE_OPTS:
2041 image_opts = true;
2042 break;
bellardea2384d2004-08-01 21:59:26 +00002043 }
2044 }
ths3b46e622007-09-17 08:09:54 +00002045
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002046 if (qemu_opts_foreach(&qemu_object_opts,
2047 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002048 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002049 goto fail_getopt;
2050 }
2051
Peter Lieven2d9187b2017-02-28 13:40:07 +01002052 if (!wr_in_order && compress) {
2053 error_report("Out of order write and compress are mutually exclusive");
2054 ret = -1;
2055 goto fail_getopt;
2056 }
2057
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002058 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002059 if (quiet) {
2060 progress = 0;
2061 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002062 qemu_progress_init(progress, 1.0);
2063
balrog926c2d22007-10-31 01:11:44 +00002064 bs_n = argc - optind - 1;
Kevin Wolfa283cb62014-02-21 16:24:07 +01002065 out_filename = bs_n >= 1 ? argv[argc - 1] : NULL;
thsf58c7b32008-06-05 21:53:49 +00002066
Kevin Wolf2dc83282014-02-21 16:24:05 +01002067 if (options && has_help_option(options)) {
Jes Sorensen4ac8aac2010-12-06 15:25:38 +01002068 ret = print_block_option_help(out_filename, out_fmt);
2069 goto out;
2070 }
2071
bellardea2384d2004-08-01 21:59:26 +00002072 if (bs_n < 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002073 error_exit("Must specify image file name");
ths5fafdf22007-09-16 21:08:06 +00002074 }
bellardea2384d2004-08-01 21:59:26 +00002075
thsf58c7b32008-06-05 21:53:49 +00002076
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002077 if (bs_n > 1 && out_baseimg) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002078 error_report("-B makes no sense when concatenating multiple input "
2079 "images");
Jes Sorensen31ca34b2010-12-06 15:25:36 +01002080 ret = -1;
2081 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002082 }
Dong Xu Wangf8111c22012-03-15 20:13:31 +08002083
Kevin Wolfce099542016-03-15 13:01:04 +01002084 src_flags = 0;
2085 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02002086 if (ret < 0) {
2087 error_report("Invalid source cache option: %s", src_cache);
2088 goto out;
2089 }
2090
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002091 qemu_progress_print(0, 100);
2092
Markus Armbruster26f54e92014-10-07 13:59:04 +02002093 blk = g_new0(BlockBackend *, bs_n);
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02002094 bs = g_new0(BlockDriverState *, bs_n);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002095 bs_sectors = g_new(int64_t, bs_n);
balrog926c2d22007-10-31 01:11:44 +00002096
2097 total_sectors = 0;
2098 for (bs_i = 0; bs_i < bs_n; bs_i++) {
Max Reitzefaa7c42016-03-16 19:54:38 +01002099 blk[bs_i] = img_open(image_opts, argv[optind + bs_i],
Kevin Wolfce099542016-03-15 13:01:04 +01002100 fmt, src_flags, src_writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002101 if (!blk[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002102 ret = -1;
2103 goto out;
2104 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002105 bs[bs_i] = blk_bs(blk[bs_i]);
Max Reitzf1d3cd72015-02-05 13:58:18 -05002106 bs_sectors[bs_i] = blk_nb_sectors(blk[bs_i]);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002107 if (bs_sectors[bs_i] < 0) {
2108 error_report("Could not get size of %s: %s",
2109 argv[optind + bs_i], strerror(-bs_sectors[bs_i]));
2110 ret = -1;
2111 goto out;
2112 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02002113 total_sectors += bs_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002114 }
bellardea2384d2004-08-01 21:59:26 +00002115
Wenchao Xiaef806542013-12-04 17:10:57 +08002116 if (sn_opts) {
Peter Maydell10d6eda2017-02-10 16:28:24 +00002117 bdrv_snapshot_load_tmp(bs[0],
2118 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2119 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2120 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002121 } else if (snapshot_name != NULL) {
edison51ef6722010-09-21 19:58:41 -07002122 if (bs_n > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002123 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002124 ret = -1;
2125 goto out;
2126 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002127
2128 bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002129 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002130 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002131 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002132 ret = -1;
2133 goto out;
edison51ef6722010-09-21 19:58:41 -07002134 }
2135
Kevin Wolfefa84d42009-05-18 16:42:12 +02002136 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +00002137 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002138 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002139 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002140 ret = -1;
2141 goto out;
2142 }
balrog926c2d22007-10-31 01:11:44 +00002143
Max Reitzb65a5e12015-02-05 13:58:12 -05002144 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002145 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +01002146 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002147 ret = -1;
2148 goto out;
2149 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09002150
Max Reitz2e024cd2015-02-11 09:58:46 -05002151 if (!skip_create) {
2152 if (!drv->create_opts) {
2153 error_report("Format driver '%s' does not support image creation",
2154 drv->format_name);
2155 ret = -1;
2156 goto out;
2157 }
Max Reitzf75613c2014-12-02 18:32:46 +01002158
Max Reitz2e024cd2015-02-11 09:58:46 -05002159 if (!proto_drv->create_opts) {
2160 error_report("Protocol driver '%s' does not support image creation",
2161 proto_drv->format_name);
2162 ret = -1;
2163 goto out;
2164 }
Max Reitzf75613c2014-12-02 18:32:46 +01002165
Max Reitz2e024cd2015-02-11 09:58:46 -05002166 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2167 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002168
Max Reitz2e024cd2015-02-11 09:58:46 -05002169 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002170 if (options) {
2171 qemu_opts_do_parse(opts, options, NULL, &local_err);
2172 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002173 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002174 ret = -1;
2175 goto out;
2176 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002177 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002178
Markus Armbruster39101f22015-02-12 16:46:36 +01002179 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_sectors * 512,
2180 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002181 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2182 if (ret < 0) {
2183 goto out;
2184 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002185 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002186
Kevin Wolfa18953f2010-10-14 15:46:04 +02002187 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002188 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002189 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002190 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002191 }
2192
Kevin Wolfefa84d42009-05-18 16:42:12 +02002193 /* Check if compression is supported */
Jes Sorenseneec77d92010-12-07 17:44:34 +01002194 if (compress) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002195 bool encryption =
2196 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
2197 const char *preallocation =
2198 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002199
Pavel Butsykin35fadca2016-07-22 11:17:48 +03002200 if (!drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002201 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002202 ret = -1;
2203 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002204 }
2205
Chunyan Liu83d05212014-06-05 17:20:51 +08002206 if (encryption) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002207 error_report("Compression and encryption not supported at "
2208 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002209 ret = -1;
2210 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002211 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002212
Chunyan Liu83d05212014-06-05 17:20:51 +08002213 if (preallocation
2214 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002215 {
2216 error_report("Compression and preallocation not supported at "
2217 "the same time");
2218 ret = -1;
2219 goto out;
2220 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002221 }
2222
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002223 if (!skip_create) {
2224 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002225 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002226 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002227 error_reportf_err(local_err, "%s: error while converting %s: ",
2228 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002229 goto out;
bellardea2384d2004-08-01 21:59:26 +00002230 }
2231 }
ths3b46e622007-09-17 08:09:54 +00002232
Peter Lieven5a37b602013-10-24 12:07:06 +02002233 flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002234 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002235 if (ret < 0) {
2236 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002237 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002238 }
2239
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002240 /* XXX we should allow --image-opts to trigger use of
2241 * img_open() here, but then we have trouble with
2242 * the bdrv_create() call which takes different params.
2243 * Not critical right now, so fix can wait...
2244 */
Kevin Wolfce099542016-03-15 13:01:04 +01002245 out_blk = img_open_file(out_filename, out_fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002246 if (!out_blk) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002247 ret = -1;
2248 goto out;
2249 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002250 out_bs = blk_bs(out_blk);
bellardea2384d2004-08-01 21:59:26 +00002251
Eric Blake5def6b82016-06-23 16:37:19 -06002252 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002253 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2254 * as maximum. */
2255 bufsectors = MIN(32768,
Eric Blake5def6b82016-06-23 16:37:19 -06002256 MAX(bufsectors,
2257 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
Eric Blakeb9f78552016-06-23 16:37:21 -06002258 out_bs->bl.pdiscard_alignment >>
2259 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002260
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002261 if (skip_create) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05002262 int64_t output_sectors = blk_nb_sectors(out_blk);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002263 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002264 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002265 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002266 ret = -1;
2267 goto out;
Markus Armbruster43716fa2014-06-26 13:23:21 +02002268 } else if (output_sectors < total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002269 error_report("output file is smaller than input file");
2270 ret = -1;
2271 goto out;
2272 }
2273 }
2274
Peter Lieven24f833c2013-11-27 11:07:07 +01002275 cluster_sectors = 0;
2276 ret = bdrv_get_info(out_bs, &bdi);
2277 if (ret < 0) {
2278 if (compress) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002279 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002280 goto out;
2281 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002282 } else {
Fam Zheng85f49ca2014-05-06 21:08:43 +08002283 compress = compress || bdi.needs_compressed_writes;
Peter Lieven24f833c2013-11-27 11:07:07 +01002284 cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
2285 }
2286
Kevin Wolf690c7302015-03-19 13:33:32 +01002287 state = (ImgConvertState) {
2288 .src = blk,
2289 .src_sectors = bs_sectors,
2290 .src_num = bs_n,
2291 .total_sectors = total_sectors,
2292 .target = out_blk,
2293 .compressed = compress,
2294 .target_has_backing = (bool) out_baseimg,
2295 .min_sparse = min_sparse,
2296 .cluster_sectors = cluster_sectors,
2297 .buf_sectors = bufsectors,
Peter Lieven2d9187b2017-02-28 13:40:07 +01002298 .wr_in_order = wr_in_order,
2299 .num_coroutines = num_coroutines,
Kevin Wolf690c7302015-03-19 13:33:32 +01002300 };
2301 ret = convert_do_copy(&state);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002302
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002303out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002304 if (!ret) {
2305 qemu_progress_print(100, 0);
2306 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002307 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002308 qemu_opts_del(opts);
2309 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002310 qemu_opts_del(sn_opts);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002311 blk_unref(out_blk);
Markus Armbruster9ba10c92014-10-07 13:59:08 +02002312 g_free(bs);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002313 if (blk) {
2314 for (bs_i = 0; bs_i < bs_n; bs_i++) {
2315 blk_unref(blk[bs_i]);
2316 }
2317 g_free(blk);
2318 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02002319 g_free(bs_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002320fail_getopt:
2321 g_free(options);
2322
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002323 if (ret) {
2324 return 1;
2325 }
bellardea2384d2004-08-01 21:59:26 +00002326 return 0;
2327}
2328
bellard57d1a2b2004-08-03 21:15:11 +00002329
bellardfaea38e2006-08-05 21:31:00 +00002330static void dump_snapshots(BlockDriverState *bs)
2331{
2332 QEMUSnapshotInfo *sn_tab, *sn;
2333 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002334
2335 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2336 if (nb_sns <= 0)
2337 return;
2338 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002339 bdrv_snapshot_dump(fprintf, stdout, NULL);
2340 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002341 for(i = 0; i < nb_sns; i++) {
2342 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002343 bdrv_snapshot_dump(fprintf, stdout, sn);
2344 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002345 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002346 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002347}
2348
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002349static void dump_json_image_info_list(ImageInfoList *list)
2350{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002351 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002352 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002353 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002354
2355 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2356 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002357 str = qobject_to_json_pretty(obj);
2358 assert(str != NULL);
2359 printf("%s\n", qstring_get_str(str));
2360 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002361 visit_free(v);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002362 QDECREF(str);
2363}
2364
Benoît Canetc054b3f2012-09-05 13:09:02 +02002365static void dump_json_image_info(ImageInfo *info)
2366{
Benoît Canetc054b3f2012-09-05 13:09:02 +02002367 QString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002368 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002369 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002370
2371 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2372 visit_complete(v, &obj);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002373 str = qobject_to_json_pretty(obj);
2374 assert(str != NULL);
2375 printf("%s\n", qstring_get_str(str));
2376 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002377 visit_free(v);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002378 QDECREF(str);
2379}
2380
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002381static void dump_human_image_info_list(ImageInfoList *list)
2382{
2383 ImageInfoList *elem;
2384 bool delim = false;
2385
2386 for (elem = list; elem; elem = elem->next) {
2387 if (delim) {
2388 printf("\n");
2389 }
2390 delim = true;
2391
Wenchao Xia5b917042013-05-25 11:09:45 +08002392 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002393 }
2394}
2395
2396static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2397{
2398 return strcmp(a, b) == 0;
2399}
2400
2401/**
2402 * Open an image file chain and return an ImageInfoList
2403 *
2404 * @filename: topmost image filename
2405 * @fmt: topmost image format (may be NULL to autodetect)
2406 * @chain: true - enumerate entire backing file chain
2407 * false - only topmost image file
2408 *
2409 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2410 * image file. If there was an error a message will have been printed to
2411 * stderr.
2412 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002413static ImageInfoList *collect_image_info_list(bool image_opts,
2414 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002415 const char *fmt,
2416 bool chain)
2417{
2418 ImageInfoList *head = NULL;
2419 ImageInfoList **last = &head;
2420 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002421 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002422
2423 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2424
2425 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002426 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002427 BlockDriverState *bs;
2428 ImageInfo *info;
2429 ImageInfoList *elem;
2430
2431 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2432 error_report("Backing file '%s' creates an infinite loop.",
2433 filename);
2434 goto err;
2435 }
2436 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2437
Max Reitzefaa7c42016-03-16 19:54:38 +01002438 blk = img_open(image_opts, filename, fmt,
Kevin Wolfce099542016-03-15 13:01:04 +01002439 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002440 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002441 goto err;
2442 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002443 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002444
Wenchao Xia43526ec2013-06-06 12:27:58 +08002445 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002446 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002447 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002448 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002449 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002450 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002451
2452 elem = g_new0(ImageInfoList, 1);
2453 elem->value = info;
2454 *last = elem;
2455 last = &elem->next;
2456
Markus Armbruster26f54e92014-10-07 13:59:04 +02002457 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002458
2459 filename = fmt = NULL;
2460 if (chain) {
2461 if (info->has_full_backing_filename) {
2462 filename = info->full_backing_filename;
2463 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002464 error_report("Could not determine absolute backing filename,"
2465 " but backing filename '%s' present",
2466 info->backing_filename);
2467 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002468 }
2469 if (info->has_backing_filename_format) {
2470 fmt = info->backing_filename_format;
2471 }
2472 }
2473 }
2474 g_hash_table_destroy(filenames);
2475 return head;
2476
2477err:
2478 qapi_free_ImageInfoList(head);
2479 g_hash_table_destroy(filenames);
2480 return NULL;
2481}
2482
Benoît Canetc054b3f2012-09-05 13:09:02 +02002483static int img_info(int argc, char **argv)
2484{
2485 int c;
2486 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002487 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002488 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002489 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002490 bool image_opts = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002491
bellardea2384d2004-08-01 21:59:26 +00002492 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002493 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002494 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002495 int option_index = 0;
2496 static const struct option long_options[] = {
2497 {"help", no_argument, 0, 'h'},
2498 {"format", required_argument, 0, 'f'},
2499 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002500 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002501 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002502 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002503 {0, 0, 0, 0}
2504 };
2505 c = getopt_long(argc, argv, "f:h",
2506 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002507 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002508 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002509 }
bellardea2384d2004-08-01 21:59:26 +00002510 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002511 case '?':
bellardea2384d2004-08-01 21:59:26 +00002512 case 'h':
2513 help();
2514 break;
2515 case 'f':
2516 fmt = optarg;
2517 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002518 case OPTION_OUTPUT:
2519 output = optarg;
2520 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002521 case OPTION_BACKING_CHAIN:
2522 chain = true;
2523 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002524 case OPTION_OBJECT: {
2525 QemuOpts *opts;
2526 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2527 optarg, true);
2528 if (!opts) {
2529 return 1;
2530 }
2531 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002532 case OPTION_IMAGE_OPTS:
2533 image_opts = true;
2534 break;
bellardea2384d2004-08-01 21:59:26 +00002535 }
2536 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002537 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002538 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002539 }
bellardea2384d2004-08-01 21:59:26 +00002540 filename = argv[optind++];
2541
Benoît Canetc054b3f2012-09-05 13:09:02 +02002542 if (output && !strcmp(output, "json")) {
2543 output_format = OFORMAT_JSON;
2544 } else if (output && !strcmp(output, "human")) {
2545 output_format = OFORMAT_HUMAN;
2546 } else if (output) {
2547 error_report("--output must be used with human or json as argument.");
2548 return 1;
2549 }
2550
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002551 if (qemu_opts_foreach(&qemu_object_opts,
2552 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002553 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002554 return 1;
2555 }
2556
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002557 list = collect_image_info_list(image_opts, filename, fmt, chain);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002558 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002559 return 1;
2560 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002561
Benoît Canetc054b3f2012-09-05 13:09:02 +02002562 switch (output_format) {
2563 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002564 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002565 break;
2566 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002567 if (chain) {
2568 dump_json_image_info_list(list);
2569 } else {
2570 dump_json_image_info(list->value);
2571 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002572 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002573 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002574
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002575 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002576 return 0;
2577}
2578
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002579static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2580 MapEntry *next)
2581{
2582 switch (output_format) {
2583 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002584 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002585 error_report("File contains external, encrypted or compressed clusters.");
2586 exit(1);
2587 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002588 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002589 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002590 e->start, e->length,
2591 e->has_offset ? e->offset : 0,
2592 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002593 }
2594 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2595 * Modify the flags here to allow more coalescing.
2596 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002597 if (next && (!next->data || next->zero)) {
2598 next->data = false;
2599 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002600 }
2601 break;
2602 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002603 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2604 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002605 (e->start == 0 ? "[" : ",\n"),
2606 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002607 e->zero ? "true" : "false",
2608 e->data ? "true" : "false");
2609 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002610 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002611 }
2612 putchar('}');
2613
2614 if (!next) {
2615 printf("]\n");
2616 }
2617 break;
2618 }
2619}
2620
2621static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2622 int nb_sectors, MapEntry *e)
2623{
2624 int64_t ret;
2625 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002626 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002627 bool has_offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002628
2629 /* As an optimization, we could cache the current range of unallocated
2630 * clusters in each file of the chain, and avoid querying the same
2631 * range repeatedly.
2632 */
2633
2634 depth = 0;
2635 for (;;) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08002636 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2637 &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002638 if (ret < 0) {
2639 return ret;
2640 }
2641 assert(nb_sectors);
2642 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2643 break;
2644 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002645 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002646 if (bs == NULL) {
2647 ret = 0;
2648 break;
2649 }
2650
2651 depth++;
2652 }
2653
John Snow28756452016-02-05 13:12:33 -05002654 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2655
2656 *e = (MapEntry) {
2657 .start = sector_num * BDRV_SECTOR_SIZE,
2658 .length = nb_sectors * BDRV_SECTOR_SIZE,
2659 .data = !!(ret & BDRV_BLOCK_DATA),
2660 .zero = !!(ret & BDRV_BLOCK_ZERO),
2661 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2662 .has_offset = has_offset,
2663 .depth = depth,
2664 .has_filename = file && has_offset,
2665 .filename = file && has_offset ? file->filename : NULL,
2666 };
2667
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002668 return 0;
2669}
2670
Fam Zheng16b0d552016-01-26 11:59:02 +08002671static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2672{
2673 if (curr->length == 0) {
2674 return false;
2675 }
2676 if (curr->zero != next->zero ||
2677 curr->data != next->data ||
2678 curr->depth != next->depth ||
2679 curr->has_filename != next->has_filename ||
2680 curr->has_offset != next->has_offset) {
2681 return false;
2682 }
2683 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2684 return false;
2685 }
2686 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2687 return false;
2688 }
2689 return true;
2690}
2691
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002692static int img_map(int argc, char **argv)
2693{
2694 int c;
2695 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002696 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002697 BlockDriverState *bs;
2698 const char *filename, *fmt, *output;
2699 int64_t length;
2700 MapEntry curr = { .length = 0 }, next;
2701 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002702 bool image_opts = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002703
2704 fmt = NULL;
2705 output = NULL;
2706 for (;;) {
2707 int option_index = 0;
2708 static const struct option long_options[] = {
2709 {"help", no_argument, 0, 'h'},
2710 {"format", required_argument, 0, 'f'},
2711 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002712 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002713 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002714 {0, 0, 0, 0}
2715 };
2716 c = getopt_long(argc, argv, "f:h",
2717 long_options, &option_index);
2718 if (c == -1) {
2719 break;
2720 }
2721 switch (c) {
2722 case '?':
2723 case 'h':
2724 help();
2725 break;
2726 case 'f':
2727 fmt = optarg;
2728 break;
2729 case OPTION_OUTPUT:
2730 output = optarg;
2731 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002732 case OPTION_OBJECT: {
2733 QemuOpts *opts;
2734 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2735 optarg, true);
2736 if (!opts) {
2737 return 1;
2738 }
2739 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002740 case OPTION_IMAGE_OPTS:
2741 image_opts = true;
2742 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002743 }
2744 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002745 if (optind != argc - 1) {
2746 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002747 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002748 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002749
2750 if (output && !strcmp(output, "json")) {
2751 output_format = OFORMAT_JSON;
2752 } else if (output && !strcmp(output, "human")) {
2753 output_format = OFORMAT_HUMAN;
2754 } else if (output) {
2755 error_report("--output must be used with human or json as argument.");
2756 return 1;
2757 }
2758
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002759 if (qemu_opts_foreach(&qemu_object_opts,
2760 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002761 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002762 return 1;
2763 }
2764
Kevin Wolfce099542016-03-15 13:01:04 +01002765 blk = img_open(image_opts, filename, fmt, 0, false, false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002766 if (!blk) {
2767 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002768 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002769 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002770
2771 if (output_format == OFORMAT_HUMAN) {
2772 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2773 }
2774
Max Reitzf1d3cd72015-02-05 13:58:18 -05002775 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002776 while (curr.start + curr.length < length) {
2777 int64_t nsectors_left;
2778 int64_t sector_num;
2779 int n;
2780
2781 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2782
2783 /* Probe up to 1 GiB at a time. */
2784 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2785 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2786 ret = get_block_status(bs, sector_num, n, &next);
2787
2788 if (ret < 0) {
2789 error_report("Could not read file metadata: %s", strerror(-ret));
2790 goto out;
2791 }
2792
Fam Zheng16b0d552016-01-26 11:59:02 +08002793 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002794 curr.length += next.length;
2795 continue;
2796 }
2797
2798 if (curr.length > 0) {
2799 dump_map_entry(output_format, &curr, &next);
2800 }
2801 curr = next;
2802 }
2803
2804 dump_map_entry(output_format, &curr, NULL);
2805
2806out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002807 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002808 return ret < 0;
2809}
2810
aliguorif7b4a942009-01-07 17:40:15 +00002811#define SNAPSHOT_LIST 1
2812#define SNAPSHOT_CREATE 2
2813#define SNAPSHOT_APPLY 3
2814#define SNAPSHOT_DELETE 4
2815
Stuart Brady153859b2009-06-07 00:42:17 +01002816static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002817{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002818 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002819 BlockDriverState *bs;
2820 QEMUSnapshotInfo sn;
2821 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002822 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002823 int action = 0;
2824 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002825 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002826 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002827 bool image_opts = false;
aliguorif7b4a942009-01-07 17:40:15 +00002828
Kevin Wolfce099542016-03-15 13:01:04 +01002829 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002830 /* Parse commandline parameters */
2831 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002832 static const struct option long_options[] = {
2833 {"help", no_argument, 0, 'h'},
2834 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002835 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002836 {0, 0, 0, 0}
2837 };
2838 c = getopt_long(argc, argv, "la:c:d:hq",
2839 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002840 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002841 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002842 }
aliguorif7b4a942009-01-07 17:40:15 +00002843 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002844 case '?':
aliguorif7b4a942009-01-07 17:40:15 +00002845 case 'h':
2846 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002847 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002848 case 'l':
2849 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002850 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002851 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002852 }
2853 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002854 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002855 break;
2856 case 'a':
2857 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002858 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002859 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002860 }
2861 action = SNAPSHOT_APPLY;
2862 snapshot_name = optarg;
2863 break;
2864 case 'c':
2865 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002866 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002867 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002868 }
2869 action = SNAPSHOT_CREATE;
2870 snapshot_name = optarg;
2871 break;
2872 case 'd':
2873 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002874 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002875 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002876 }
2877 action = SNAPSHOT_DELETE;
2878 snapshot_name = optarg;
2879 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002880 case 'q':
2881 quiet = true;
2882 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002883 case OPTION_OBJECT: {
2884 QemuOpts *opts;
2885 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2886 optarg, true);
2887 if (!opts) {
2888 return 1;
2889 }
2890 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002891 case OPTION_IMAGE_OPTS:
2892 image_opts = true;
2893 break;
aliguorif7b4a942009-01-07 17:40:15 +00002894 }
2895 }
2896
Kevin Wolffc11eb22013-08-05 10:53:04 +02002897 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002898 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002899 }
aliguorif7b4a942009-01-07 17:40:15 +00002900 filename = argv[optind++];
2901
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002902 if (qemu_opts_foreach(&qemu_object_opts,
2903 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002904 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002905 return 1;
2906 }
2907
aliguorif7b4a942009-01-07 17:40:15 +00002908 /* Open the image */
Kevin Wolfce099542016-03-15 13:01:04 +01002909 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002910 if (!blk) {
2911 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002912 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002913 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002914
2915 /* Perform the requested action */
2916 switch(action) {
2917 case SNAPSHOT_LIST:
2918 dump_snapshots(bs);
2919 break;
2920
2921 case SNAPSHOT_CREATE:
2922 memset(&sn, 0, sizeof(sn));
2923 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2924
2925 qemu_gettimeofday(&tv);
2926 sn.date_sec = tv.tv_sec;
2927 sn.date_nsec = tv.tv_usec * 1000;
2928
2929 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002930 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002931 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002932 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002933 }
aliguorif7b4a942009-01-07 17:40:15 +00002934 break;
2935
2936 case SNAPSHOT_APPLY:
2937 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002938 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002939 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002940 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002941 }
aliguorif7b4a942009-01-07 17:40:15 +00002942 break;
2943
2944 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002945 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002946 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002947 error_reportf_err(err, "Could not delete snapshot '%s': ",
2948 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002949 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002950 }
aliguorif7b4a942009-01-07 17:40:15 +00002951 break;
2952 }
2953
2954 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02002955 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002956 if (ret) {
2957 return 1;
2958 }
Stuart Brady153859b2009-06-07 00:42:17 +01002959 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002960}
2961
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002962static int img_rebase(int argc, char **argv)
2963{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002964 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01002965 uint8_t *buf_old = NULL;
2966 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05002967 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002968 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02002969 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
2970 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01002971 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002972 int unsafe = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002973 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002974 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02002975 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002976 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002977
2978 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002979 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002980 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02002981 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002982 out_baseimg = NULL;
2983 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002984 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002985 static const struct option long_options[] = {
2986 {"help", no_argument, 0, 'h'},
2987 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002988 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002989 {0, 0, 0, 0}
2990 };
2991 c = getopt_long(argc, argv, "hf:F:b:upt:T:q",
2992 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002993 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002994 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002995 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002996 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002997 case '?':
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002998 case 'h':
2999 help();
3000 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003001 case 'f':
3002 fmt = optarg;
3003 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003004 case 'F':
3005 out_basefmt = optarg;
3006 break;
3007 case 'b':
3008 out_baseimg = optarg;
3009 break;
3010 case 'u':
3011 unsafe = 1;
3012 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003013 case 'p':
3014 progress = 1;
3015 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003016 case 't':
3017 cache = optarg;
3018 break;
Max Reitz40055952014-07-22 22:58:42 +02003019 case 'T':
3020 src_cache = optarg;
3021 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003022 case 'q':
3023 quiet = true;
3024 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003025 case OPTION_OBJECT: {
3026 QemuOpts *opts;
3027 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3028 optarg, true);
3029 if (!opts) {
3030 return 1;
3031 }
3032 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003033 case OPTION_IMAGE_OPTS:
3034 image_opts = true;
3035 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003036 }
3037 }
3038
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003039 if (quiet) {
3040 progress = 0;
3041 }
3042
Fam Zhengac1307a2014-04-22 13:36:11 +08003043 if (optind != argc - 1) {
3044 error_exit("Expecting one image file name");
3045 }
3046 if (!unsafe && !out_baseimg) {
3047 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003048 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003049 filename = argv[optind++];
3050
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003051 if (qemu_opts_foreach(&qemu_object_opts,
3052 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003053 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003054 return 1;
3055 }
3056
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003057 qemu_progress_init(progress, 2.0);
3058 qemu_progress_print(0, 100);
3059
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003060 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003061 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003062 if (ret < 0) {
3063 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003064 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003065 }
3066
Kevin Wolfce099542016-03-15 13:01:04 +01003067 src_flags = 0;
3068 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003069 if (ret < 0) {
3070 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003071 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003072 }
3073
Kevin Wolfce099542016-03-15 13:01:04 +01003074 /* The source files are opened read-only, don't care about WCE */
3075 assert((src_flags & BDRV_O_RDWR) == 0);
3076 (void) src_writethrough;
3077
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003078 /*
3079 * Open the images.
3080 *
3081 * Ignore the old backing file for unsafe rebase in case we want to correct
3082 * the reference to a renamed or moved backing file.
3083 */
Kevin Wolfce099542016-03-15 13:01:04 +01003084 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003085 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003086 ret = -1;
3087 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003088 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003089 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003090
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003091 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003092 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003093 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003094 ret = -1;
3095 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003096 }
3097 }
3098
3099 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003100 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003101 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003102 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003103
Max Reitz644483d2015-02-05 13:58:17 -05003104 if (bs->backing_format[0] != '\0') {
3105 options = qdict_new();
3106 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
3107 }
3108
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003109 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003110 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003111 options, src_flags, &local_err);
3112 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003113 error_reportf_err(local_err,
3114 "Could not open old backing file '%s': ",
3115 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003116 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003117 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003118 }
Max Reitz644483d2015-02-05 13:58:17 -05003119
Alex Bligha6166732012-10-16 13:46:18 +01003120 if (out_baseimg[0]) {
Max Reitz644483d2015-02-05 13:58:17 -05003121 if (out_basefmt) {
3122 options = qdict_new();
3123 qdict_put(options, "driver", qstring_from_str(out_basefmt));
3124 } else {
3125 options = NULL;
3126 }
3127
Max Reitzefaa7c42016-03-16 19:54:38 +01003128 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003129 options, src_flags, &local_err);
3130 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003131 error_reportf_err(local_err,
3132 "Could not open new backing file '%s': ",
3133 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003134 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003135 goto out;
3136 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003137 }
3138 }
3139
3140 /*
3141 * Check each unallocated cluster in the COW file. If it is unallocated,
3142 * accesses go to the backing file. We must therefore compare this cluster
3143 * in the old and new backing file, and if they differ we need to copy it
3144 * from the old backing file into the COW file.
3145 *
3146 * If qemu-img crashes during this step, no harm is done. The content of
3147 * the image is the same as the original one at any time.
3148 */
3149 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003150 int64_t num_sectors;
3151 int64_t old_backing_num_sectors;
3152 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003153 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003154 int n;
Kevin Wolf1f710492012-10-12 14:29:18 +02003155 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003156
Max Reitzf1d3cd72015-02-05 13:58:18 -05003157 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3158 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003159
Max Reitzf1d3cd72015-02-05 13:58:18 -05003160 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003161 if (num_sectors < 0) {
3162 error_report("Could not get size of '%s': %s",
3163 filename, strerror(-num_sectors));
3164 ret = -1;
3165 goto out;
3166 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003167 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003168 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003169 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003170
3171 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3172 error_report("Could not get size of '%s': %s",
3173 backing_name, strerror(-old_backing_num_sectors));
3174 ret = -1;
3175 goto out;
3176 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003177 if (blk_new_backing) {
3178 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003179 if (new_backing_num_sectors < 0) {
3180 error_report("Could not get size of '%s': %s",
3181 out_baseimg, strerror(-new_backing_num_sectors));
3182 ret = -1;
3183 goto out;
3184 }
Alex Bligha6166732012-10-16 13:46:18 +01003185 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003186
Kevin Wolf1f710492012-10-12 14:29:18 +02003187 if (num_sectors != 0) {
3188 local_progress = (float)100 /
3189 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
3190 }
3191
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003192 for (sector = 0; sector < num_sectors; sector += n) {
3193
3194 /* How many sectors can we handle with the next read? */
3195 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
3196 n = (IO_BUF_SIZE / 512);
3197 } else {
3198 n = num_sectors - sector;
3199 }
3200
3201 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02003202 ret = bdrv_is_allocated(bs, sector, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003203 if (ret < 0) {
3204 error_report("error while reading image metadata: %s",
3205 strerror(-ret));
3206 goto out;
3207 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003208 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003209 continue;
3210 }
3211
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003212 /*
3213 * Read old and new backing file and take into consideration that
3214 * backing files may be smaller than the COW image.
3215 */
3216 if (sector >= old_backing_num_sectors) {
3217 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3218 } else {
3219 if (sector + n > old_backing_num_sectors) {
3220 n = old_backing_num_sectors - sector;
3221 }
3222
Eric Blake91669202016-05-06 10:26:43 -06003223 ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS,
3224 buf_old, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003225 if (ret < 0) {
3226 error_report("error while reading from old backing file");
3227 goto out;
3228 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003229 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003230
Max Reitzf1d3cd72015-02-05 13:58:18 -05003231 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003232 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3233 } else {
3234 if (sector + n > new_backing_num_sectors) {
3235 n = new_backing_num_sectors - sector;
3236 }
3237
Eric Blake91669202016-05-06 10:26:43 -06003238 ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS,
3239 buf_new, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003240 if (ret < 0) {
3241 error_report("error while reading from new backing file");
3242 goto out;
3243 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003244 }
3245
3246 /* If they differ, we need to write to the COW file */
3247 uint64_t written = 0;
3248
3249 while (written < n) {
3250 int pnum;
3251
3252 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003253 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003254 {
Eric Blake91669202016-05-06 10:26:43 -06003255 ret = blk_pwrite(blk,
3256 (sector + written) << BDRV_SECTOR_BITS,
3257 buf_old + written * 512,
3258 pnum << BDRV_SECTOR_BITS, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003259 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003260 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003261 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003262 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003263 }
3264 }
3265
3266 written += pnum;
3267 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003268 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003269 }
3270 }
3271
3272 /*
3273 * Change the backing file. All clusters that are different from the old
3274 * backing file are overwritten in the COW file now, so the visible content
3275 * doesn't change when we switch the backing file.
3276 */
Alex Bligha6166732012-10-16 13:46:18 +01003277 if (out_baseimg && *out_baseimg) {
3278 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3279 } else {
3280 ret = bdrv_change_backing_file(bs, NULL, NULL);
3281 }
3282
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003283 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003284 error_report("Could not change the backing file to '%s': No "
3285 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003286 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003287 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003288 out_baseimg, strerror(-ret));
3289 }
3290
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003291 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003292 /*
3293 * TODO At this point it is possible to check if any clusters that are
3294 * allocated in the COW file are the same in the backing file. If so, they
3295 * could be dropped from the COW file. Don't do this before switching the
3296 * backing file, in case of a crash this would lead to corruption.
3297 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003298out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003299 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003300 /* Cleanup */
3301 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003302 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003303 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003304 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003305 qemu_vfree(buf_old);
3306 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003307
Markus Armbruster26f54e92014-10-07 13:59:04 +02003308 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003309 if (ret) {
3310 return 1;
3311 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003312 return 0;
3313}
3314
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003315static int img_resize(int argc, char **argv)
3316{
Markus Armbruster6750e792015-02-12 17:43:08 +01003317 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003318 int c, ret, relative;
3319 const char *filename, *fmt, *size;
3320 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003321 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003322 BlockBackend *blk = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003323 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003324
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003325 static QemuOptsList resize_options = {
3326 .name = "resize_options",
3327 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3328 .desc = {
3329 {
3330 .name = BLOCK_OPT_SIZE,
3331 .type = QEMU_OPT_SIZE,
3332 .help = "Virtual disk size"
3333 }, {
3334 /* end of list */
3335 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003336 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003337 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003338 bool image_opts = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003339
Kevin Wolfe80fec72011-04-29 10:58:12 +02003340 /* Remove size from argv manually so that negative numbers are not treated
3341 * as options by getopt. */
3342 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003343 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003344 return 1;
3345 }
3346
3347 size = argv[--argc];
3348
3349 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003350 fmt = NULL;
3351 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003352 static const struct option long_options[] = {
3353 {"help", no_argument, 0, 'h'},
3354 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003355 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003356 {0, 0, 0, 0}
3357 };
3358 c = getopt_long(argc, argv, "f:hq",
3359 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003360 if (c == -1) {
3361 break;
3362 }
3363 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01003364 case '?':
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003365 case 'h':
3366 help();
3367 break;
3368 case 'f':
3369 fmt = optarg;
3370 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003371 case 'q':
3372 quiet = true;
3373 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003374 case OPTION_OBJECT: {
3375 QemuOpts *opts;
3376 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3377 optarg, true);
3378 if (!opts) {
3379 return 1;
3380 }
3381 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003382 case OPTION_IMAGE_OPTS:
3383 image_opts = true;
3384 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003385 }
3386 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003387 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003388 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003389 }
3390 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003391
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003392 if (qemu_opts_foreach(&qemu_object_opts,
3393 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003394 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003395 return 1;
3396 }
3397
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003398 /* Choose grow, shrink, or absolute resize mode */
3399 switch (size[0]) {
3400 case '+':
3401 relative = 1;
3402 size++;
3403 break;
3404 case '-':
3405 relative = -1;
3406 size++;
3407 break;
3408 default:
3409 relative = 0;
3410 break;
3411 }
3412
3413 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003414 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003415 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003416 if (err) {
3417 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003418 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003419 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003420 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003421 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003422 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3423 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003424
Max Reitzefaa7c42016-03-16 19:54:38 +01003425 blk = img_open(image_opts, filename, fmt,
Kevin Wolf55880602017-02-17 15:07:38 +01003426 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003427 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003428 ret = -1;
3429 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003430 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003431
3432 if (relative) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003433 total_size = blk_getlength(blk) + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003434 } else {
3435 total_size = n;
3436 }
3437 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003438 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003439 ret = -1;
3440 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003441 }
3442
Max Reitzf1d3cd72015-02-05 13:58:18 -05003443 ret = blk_truncate(blk, total_size);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003444 switch (ret) {
3445 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003446 qprintf(quiet, "Image resized.\n");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003447 break;
3448 case -ENOTSUP:
Kevin Wolf259b2172012-03-06 12:44:45 +01003449 error_report("This image does not support resize");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003450 break;
3451 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +01003452 error_report("Image is read-only");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003453 break;
3454 default:
Max Reitzbcf23482016-06-15 17:36:29 +02003455 error_report("Error resizing image: %s", strerror(-ret));
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003456 break;
3457 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003458out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003459 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003460 if (ret) {
3461 return 1;
3462 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003463 return 0;
3464}
3465
Max Reitz76a3a342014-10-27 11:12:51 +01003466static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003467 int64_t offset, int64_t total_work_size,
3468 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003469{
3470 qemu_progress_print(100.f * offset / total_work_size, 0);
3471}
3472
Max Reitz6f176b42013-09-03 10:09:50 +02003473static int img_amend(int argc, char **argv)
3474{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003475 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003476 int c, ret = 0;
3477 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003478 QemuOptsList *create_opts = NULL;
3479 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003480 const char *fmt = NULL, *filename, *cache;
3481 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003482 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003483 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003484 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003485 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003486 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003487
Max Reitzbd39e6e2014-07-22 22:58:43 +02003488 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003489 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003490 static const struct option long_options[] = {
3491 {"help", no_argument, 0, 'h'},
3492 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003493 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003494 {0, 0, 0, 0}
3495 };
3496 c = getopt_long(argc, argv, "ho:f:t:pq",
3497 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003498 if (c == -1) {
3499 break;
3500 }
3501
3502 switch (c) {
3503 case 'h':
3504 case '?':
3505 help();
3506 break;
3507 case 'o':
Kevin Wolf626f84f2014-02-21 16:24:06 +01003508 if (!is_valid_option_list(optarg)) {
3509 error_report("Invalid option list: %s", optarg);
3510 ret = -1;
Max Reitze814dff2015-08-20 16:00:38 -07003511 goto out_no_progress;
Kevin Wolf626f84f2014-02-21 16:24:06 +01003512 }
3513 if (!options) {
3514 options = g_strdup(optarg);
3515 } else {
3516 char *old_options = options;
3517 options = g_strdup_printf("%s,%s", options, optarg);
3518 g_free(old_options);
3519 }
Max Reitz6f176b42013-09-03 10:09:50 +02003520 break;
3521 case 'f':
3522 fmt = optarg;
3523 break;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003524 case 't':
3525 cache = optarg;
3526 break;
Max Reitz76a3a342014-10-27 11:12:51 +01003527 case 'p':
3528 progress = true;
3529 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003530 case 'q':
3531 quiet = true;
3532 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003533 case OPTION_OBJECT:
3534 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3535 optarg, true);
3536 if (!opts) {
3537 ret = -1;
3538 goto out_no_progress;
3539 }
3540 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003541 case OPTION_IMAGE_OPTS:
3542 image_opts = true;
3543 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003544 }
3545 }
3546
Max Reitz6f176b42013-09-03 10:09:50 +02003547 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003548 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003549 }
3550
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003551 if (qemu_opts_foreach(&qemu_object_opts,
3552 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003553 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003554 ret = -1;
3555 goto out_no_progress;
3556 }
3557
Max Reitz76a3a342014-10-27 11:12:51 +01003558 if (quiet) {
3559 progress = false;
3560 }
3561 qemu_progress_init(progress, 1.0);
3562
Kevin Wolfa283cb62014-02-21 16:24:07 +01003563 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3564 if (fmt && has_help_option(options)) {
3565 /* If a format is explicitly specified (and possibly no filename is
3566 * given), print option help here */
3567 ret = print_block_option_help(filename, fmt);
3568 goto out;
3569 }
3570
3571 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003572 error_report("Expecting one image file name");
3573 ret = -1;
3574 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003575 }
Max Reitz6f176b42013-09-03 10:09:50 +02003576
Kevin Wolfce099542016-03-15 13:01:04 +01003577 flags = BDRV_O_RDWR;
3578 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003579 if (ret < 0) {
3580 error_report("Invalid cache option: %s", cache);
3581 goto out;
3582 }
3583
Kevin Wolfce099542016-03-15 13:01:04 +01003584 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003585 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003586 ret = -1;
3587 goto out;
3588 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003589 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003590
3591 fmt = bs->drv->format_name;
3592
Kevin Wolf626f84f2014-02-21 16:24:06 +01003593 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003594 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003595 ret = print_block_option_help(filename, fmt);
3596 goto out;
3597 }
3598
Max Reitzb2439d22014-12-02 18:32:47 +01003599 if (!bs->drv->create_opts) {
3600 error_report("Format driver '%s' does not support any options to amend",
3601 fmt);
3602 ret = -1;
3603 goto out;
3604 }
3605
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003606 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003607 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003608 qemu_opts_do_parse(opts, options, NULL, &err);
3609 if (err) {
3610 error_report_err(err);
3611 ret = -1;
3612 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003613 }
3614
Max Reitz76a3a342014-10-27 11:12:51 +01003615 /* In case the driver does not call amend_status_cb() */
3616 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003617 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003618 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003619 if (ret < 0) {
3620 error_report("Error while amending options: %s", strerror(-ret));
3621 goto out;
3622 }
3623
3624out:
Max Reitz76a3a342014-10-27 11:12:51 +01003625 qemu_progress_end();
3626
Max Reitze814dff2015-08-20 16:00:38 -07003627out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003628 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003629 qemu_opts_del(opts);
3630 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003631 g_free(options);
3632
Max Reitz6f176b42013-09-03 10:09:50 +02003633 if (ret) {
3634 return 1;
3635 }
3636 return 0;
3637}
3638
Kevin Wolfb6133b82014-08-05 14:17:13 +02003639typedef struct BenchData {
3640 BlockBackend *blk;
3641 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003642 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003643 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003644 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003645 int nrreq;
3646 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003647 int flush_interval;
3648 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003649 uint8_t *buf;
3650 QEMUIOVector *qiov;
3651
3652 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003653 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003654 uint64_t offset;
3655} BenchData;
3656
Kevin Wolf55d539c2016-06-03 13:59:41 +02003657static void bench_undrained_flush_cb(void *opaque, int ret)
3658{
3659 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003660 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003661 exit(EXIT_FAILURE);
3662 }
3663}
3664
Kevin Wolfb6133b82014-08-05 14:17:13 +02003665static void bench_cb(void *opaque, int ret)
3666{
3667 BenchData *b = opaque;
3668 BlockAIOCB *acb;
3669
3670 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003671 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003672 exit(EXIT_FAILURE);
3673 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003674
3675 if (b->in_flush) {
3676 /* Just finished a flush with drained queue: Start next requests */
3677 assert(b->in_flight == 0);
3678 b->in_flush = false;
3679 } else if (b->in_flight > 0) {
3680 int remaining = b->n - b->in_flight;
3681
Kevin Wolfb6133b82014-08-05 14:17:13 +02003682 b->n--;
3683 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003684
3685 /* Time for flush? Drain queue if requested, then flush */
3686 if (b->flush_interval && remaining % b->flush_interval == 0) {
3687 if (!b->in_flight || !b->drain_on_flush) {
3688 BlockCompletionFunc *cb;
3689
3690 if (b->drain_on_flush) {
3691 b->in_flush = true;
3692 cb = bench_cb;
3693 } else {
3694 cb = bench_undrained_flush_cb;
3695 }
3696
3697 acb = blk_aio_flush(b->blk, cb, b);
3698 if (!acb) {
3699 error_report("Failed to issue flush request");
3700 exit(EXIT_FAILURE);
3701 }
3702 }
3703 if (b->drain_on_flush) {
3704 return;
3705 }
3706 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003707 }
3708
3709 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003710 int64_t offset = b->offset;
3711 /* blk_aio_* might look for completed I/Os and kick bench_cb
3712 * again, so make sure this operation is counted by in_flight
3713 * and b->offset is ready for the next submission.
3714 */
3715 b->in_flight++;
3716 b->offset += b->step;
3717 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003718 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003719 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003720 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003721 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003722 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003723 if (!acb) {
3724 error_report("Failed to issue request");
3725 exit(EXIT_FAILURE);
3726 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003727 }
3728}
3729
3730static int img_bench(int argc, char **argv)
3731{
3732 int c, ret = 0;
3733 const char *fmt = NULL, *filename;
3734 bool quiet = false;
3735 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003736 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003737 int count = 75000;
3738 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003739 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003740 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003741 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003742 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003743 int flush_interval = 0;
3744 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003745 int64_t image_size;
3746 BlockBackend *blk = NULL;
3747 BenchData data = {};
3748 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003749 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003750 struct timeval t1, t2;
3751 int i;
3752
3753 for (;;) {
3754 static const struct option long_options[] = {
3755 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003756 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003757 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003758 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003759 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003760 {0, 0, 0, 0}
3761 };
Kevin Wolf83de9be2015-07-13 13:13:17 +02003762 c = getopt_long(argc, argv, "hc:d:f:no:qs:S:t:w", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003763 if (c == -1) {
3764 break;
3765 }
3766
3767 switch (c) {
3768 case 'h':
3769 case '?':
3770 help();
3771 break;
3772 case 'c':
3773 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003774 unsigned long res;
3775
3776 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003777 error_report("Invalid request count specified");
3778 return 1;
3779 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003780 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003781 break;
3782 }
3783 case 'd':
3784 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003785 unsigned long res;
3786
3787 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003788 error_report("Invalid queue depth specified");
3789 return 1;
3790 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003791 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003792 break;
3793 }
3794 case 'f':
3795 fmt = optarg;
3796 break;
3797 case 'n':
3798 flags |= BDRV_O_NATIVE_AIO;
3799 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003800 case 'o':
3801 {
Markus Armbruster606caa02017-02-21 21:14:04 +01003802 offset = cvtnum(optarg);
3803 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02003804 error_report("Invalid offset specified");
3805 return 1;
3806 }
3807 break;
3808 }
3809 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003810 case 'q':
3811 quiet = true;
3812 break;
3813 case 's':
3814 {
3815 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003816
Markus Armbruster606caa02017-02-21 21:14:04 +01003817 sval = cvtnum(optarg);
3818 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003819 error_report("Invalid buffer size specified");
3820 return 1;
3821 }
3822
3823 bufsize = sval;
3824 break;
3825 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003826 case 'S':
3827 {
3828 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003829
Markus Armbruster606caa02017-02-21 21:14:04 +01003830 sval = cvtnum(optarg);
3831 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02003832 error_report("Invalid step size specified");
3833 return 1;
3834 }
3835
3836 step = sval;
3837 break;
3838 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003839 case 't':
3840 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3841 if (ret < 0) {
3842 error_report("Invalid cache mode");
3843 ret = -1;
3844 goto out;
3845 }
3846 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003847 case 'w':
3848 flags |= BDRV_O_RDWR;
3849 is_write = true;
3850 break;
3851 case OPTION_PATTERN:
3852 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003853 unsigned long res;
3854
3855 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003856 error_report("Invalid pattern byte specified");
3857 return 1;
3858 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003859 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003860 break;
3861 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003862 case OPTION_FLUSH_INTERVAL:
3863 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003864 unsigned long res;
3865
3866 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02003867 error_report("Invalid flush interval specified");
3868 return 1;
3869 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003870 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003871 break;
3872 }
3873 case OPTION_NO_DRAIN:
3874 drain_on_flush = false;
3875 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003876 case OPTION_IMAGE_OPTS:
3877 image_opts = true;
3878 break;
3879 }
3880 }
3881
3882 if (optind != argc - 1) {
3883 error_exit("Expecting one image file name");
3884 }
3885 filename = argv[argc - 1];
3886
Kevin Wolf55d539c2016-06-03 13:59:41 +02003887 if (!is_write && flush_interval) {
3888 error_report("--flush-interval is only available in write tests");
3889 ret = -1;
3890 goto out;
3891 }
3892 if (flush_interval && flush_interval < depth) {
3893 error_report("Flush interval can't be smaller than depth");
3894 ret = -1;
3895 goto out;
3896 }
3897
Kevin Wolfb6133b82014-08-05 14:17:13 +02003898 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
3899 if (!blk) {
3900 ret = -1;
3901 goto out;
3902 }
3903
3904 image_size = blk_getlength(blk);
3905 if (image_size < 0) {
3906 ret = image_size;
3907 goto out;
3908 }
3909
3910 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02003911 .blk = blk,
3912 .image_size = image_size,
3913 .bufsize = bufsize,
3914 .step = step ?: bufsize,
3915 .nrreq = depth,
3916 .n = count,
3917 .offset = offset,
3918 .write = is_write,
3919 .flush_interval = flush_interval,
3920 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02003921 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02003922 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02003923 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02003924 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02003925 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02003926 if (flush_interval) {
3927 printf("Sending flush every %d requests\n", flush_interval);
3928 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003929
3930 data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003931 memset(data.buf, pattern, data.nrreq * data.bufsize);
3932
Kevin Wolfb6133b82014-08-05 14:17:13 +02003933 data.qiov = g_new(QEMUIOVector, data.nrreq);
3934 for (i = 0; i < data.nrreq; i++) {
3935 qemu_iovec_init(&data.qiov[i], 1);
3936 qemu_iovec_add(&data.qiov[i],
3937 data.buf + i * data.bufsize, data.bufsize);
3938 }
3939
3940 gettimeofday(&t1, NULL);
3941 bench_cb(&data, 0);
3942
3943 while (data.n > 0) {
3944 main_loop_wait(false);
3945 }
3946 gettimeofday(&t2, NULL);
3947
3948 printf("Run completed in %3.3f seconds.\n",
3949 (t2.tv_sec - t1.tv_sec)
3950 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
3951
3952out:
3953 qemu_vfree(data.buf);
3954 blk_unref(blk);
3955
3956 if (ret) {
3957 return 1;
3958 }
3959 return 0;
3960}
3961
Reda Sallahi86ce1f62016-08-10 04:43:12 +02003962#define C_BS 01
3963#define C_COUNT 02
3964#define C_IF 04
3965#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02003966#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02003967
3968struct DdInfo {
3969 unsigned int flags;
3970 int64_t count;
3971};
3972
3973struct DdIo {
3974 int bsz; /* Block size */
3975 char *filename;
3976 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02003977 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02003978};
3979
3980struct DdOpts {
3981 const char *name;
3982 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
3983 unsigned int flag;
3984};
3985
3986static int img_dd_bs(const char *arg,
3987 struct DdIo *in, struct DdIo *out,
3988 struct DdInfo *dd)
3989{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02003990 int64_t res;
3991
Markus Armbruster606caa02017-02-21 21:14:04 +01003992 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02003993
Markus Armbruster606caa02017-02-21 21:14:04 +01003994 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02003995 error_report("invalid number: '%s'", arg);
3996 return 1;
3997 }
3998 in->bsz = out->bsz = res;
3999
4000 return 0;
4001}
4002
4003static int img_dd_count(const char *arg,
4004 struct DdIo *in, struct DdIo *out,
4005 struct DdInfo *dd)
4006{
Markus Armbruster606caa02017-02-21 21:14:04 +01004007 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004008
Markus Armbruster606caa02017-02-21 21:14:04 +01004009 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004010 error_report("invalid number: '%s'", arg);
4011 return 1;
4012 }
4013
4014 return 0;
4015}
4016
4017static int img_dd_if(const char *arg,
4018 struct DdIo *in, struct DdIo *out,
4019 struct DdInfo *dd)
4020{
4021 in->filename = g_strdup(arg);
4022
4023 return 0;
4024}
4025
4026static int img_dd_of(const char *arg,
4027 struct DdIo *in, struct DdIo *out,
4028 struct DdInfo *dd)
4029{
4030 out->filename = g_strdup(arg);
4031
4032 return 0;
4033}
4034
Reda Sallahif7c15532016-08-10 16:16:09 +02004035static int img_dd_skip(const char *arg,
4036 struct DdIo *in, struct DdIo *out,
4037 struct DdInfo *dd)
4038{
Markus Armbruster606caa02017-02-21 21:14:04 +01004039 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004040
Markus Armbruster606caa02017-02-21 21:14:04 +01004041 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004042 error_report("invalid number: '%s'", arg);
4043 return 1;
4044 }
4045
4046 return 0;
4047}
4048
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004049static int img_dd(int argc, char **argv)
4050{
4051 int ret = 0;
4052 char *arg = NULL;
4053 char *tmp;
4054 BlockDriver *drv = NULL, *proto_drv = NULL;
4055 BlockBackend *blk1 = NULL, *blk2 = NULL;
4056 QemuOpts *opts = NULL;
4057 QemuOptsList *create_opts = NULL;
4058 Error *local_err = NULL;
4059 bool image_opts = false;
4060 int c, i;
4061 const char *out_fmt = "raw";
4062 const char *fmt = NULL;
4063 int64_t size = 0;
4064 int64_t block_count = 0, out_pos, in_pos;
4065 struct DdInfo dd = {
4066 .flags = 0,
4067 .count = 0,
4068 };
4069 struct DdIo in = {
4070 .bsz = 512, /* Block size is by default 512 bytes */
4071 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004072 .buf = NULL,
4073 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004074 };
4075 struct DdIo out = {
4076 .bsz = 512,
4077 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004078 .buf = NULL,
4079 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004080 };
4081
4082 const struct DdOpts options[] = {
4083 { "bs", img_dd_bs, C_BS },
4084 { "count", img_dd_count, C_COUNT },
4085 { "if", img_dd_if, C_IF },
4086 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004087 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004088 { NULL, NULL, 0 }
4089 };
4090 const struct option long_options[] = {
4091 { "help", no_argument, 0, 'h'},
4092 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4093 { 0, 0, 0, 0 }
4094 };
4095
4096 while ((c = getopt_long(argc, argv, "hf:O:", long_options, NULL))) {
4097 if (c == EOF) {
4098 break;
4099 }
4100 switch (c) {
4101 case 'O':
4102 out_fmt = optarg;
4103 break;
4104 case 'f':
4105 fmt = optarg;
4106 break;
4107 case '?':
4108 error_report("Try 'qemu-img --help' for more information.");
4109 ret = -1;
4110 goto out;
4111 case 'h':
4112 help();
4113 break;
4114 case OPTION_IMAGE_OPTS:
4115 image_opts = true;
4116 break;
4117 }
4118 }
4119
4120 for (i = optind; i < argc; i++) {
4121 int j;
4122 arg = g_strdup(argv[i]);
4123
4124 tmp = strchr(arg, '=');
4125 if (tmp == NULL) {
4126 error_report("unrecognized operand %s", arg);
4127 ret = -1;
4128 goto out;
4129 }
4130
4131 *tmp++ = '\0';
4132
4133 for (j = 0; options[j].name != NULL; j++) {
4134 if (!strcmp(arg, options[j].name)) {
4135 break;
4136 }
4137 }
4138 if (options[j].name == NULL) {
4139 error_report("unrecognized operand %s", arg);
4140 ret = -1;
4141 goto out;
4142 }
4143
4144 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4145 ret = -1;
4146 goto out;
4147 }
4148 dd.flags |= options[j].flag;
4149 g_free(arg);
4150 arg = NULL;
4151 }
4152
4153 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4154 error_report("Must specify both input and output files");
4155 ret = -1;
4156 goto out;
4157 }
4158 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false);
4159
4160 if (!blk1) {
4161 ret = -1;
4162 goto out;
4163 }
4164
4165 drv = bdrv_find_format(out_fmt);
4166 if (!drv) {
4167 error_report("Unknown file format");
4168 ret = -1;
4169 goto out;
4170 }
4171 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4172
4173 if (!proto_drv) {
4174 error_report_err(local_err);
4175 ret = -1;
4176 goto out;
4177 }
4178 if (!drv->create_opts) {
4179 error_report("Format driver '%s' does not support image creation",
4180 drv->format_name);
4181 ret = -1;
4182 goto out;
4183 }
4184 if (!proto_drv->create_opts) {
4185 error_report("Protocol driver '%s' does not support image creation",
4186 proto_drv->format_name);
4187 ret = -1;
4188 goto out;
4189 }
4190 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4191 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4192
4193 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4194
4195 size = blk_getlength(blk1);
4196 if (size < 0) {
4197 error_report("Failed to get size for '%s'", in.filename);
4198 ret = -1;
4199 goto out;
4200 }
4201
4202 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4203 dd.count * in.bsz < size) {
4204 size = dd.count * in.bsz;
4205 }
4206
Reda Sallahif7c15532016-08-10 16:16:09 +02004207 /* Overflow means the specified offset is beyond input image's size */
4208 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4209 size < in.bsz * in.offset)) {
4210 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4211 } else {
4212 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4213 size - in.bsz * in.offset, &error_abort);
4214 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004215
4216 ret = bdrv_create(drv, out.filename, opts, &local_err);
4217 if (ret < 0) {
4218 error_reportf_err(local_err,
4219 "%s: error while creating output image: ",
4220 out.filename);
4221 ret = -1;
4222 goto out;
4223 }
4224
4225 blk2 = img_open(image_opts, out.filename, out_fmt, BDRV_O_RDWR,
4226 false, false);
4227
4228 if (!blk2) {
4229 ret = -1;
4230 goto out;
4231 }
4232
Reda Sallahif7c15532016-08-10 16:16:09 +02004233 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4234 size < in.offset * in.bsz)) {
4235 /* We give a warning if the skip option is bigger than the input
4236 * size and create an empty output disk image (i.e. like dd(1)).
4237 */
4238 error_report("%s: cannot skip to specified offset", in.filename);
4239 in_pos = size;
4240 } else {
4241 in_pos = in.offset * in.bsz;
4242 }
4243
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004244 in.buf = g_new(uint8_t, in.bsz);
4245
Reda Sallahif7c15532016-08-10 16:16:09 +02004246 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004247 int in_ret, out_ret;
4248
4249 if (in_pos + in.bsz > size) {
4250 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4251 } else {
4252 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4253 }
4254 if (in_ret < 0) {
4255 error_report("error while reading from input image file: %s",
4256 strerror(-in_ret));
4257 ret = -1;
4258 goto out;
4259 }
4260 in_pos += in_ret;
4261
4262 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4263
4264 if (out_ret < 0) {
4265 error_report("error while writing to output image file: %s",
4266 strerror(-out_ret));
4267 ret = -1;
4268 goto out;
4269 }
4270 out_pos += out_ret;
4271 }
4272
4273out:
4274 g_free(arg);
4275 qemu_opts_del(opts);
4276 qemu_opts_free(create_opts);
4277 blk_unref(blk1);
4278 blk_unref(blk2);
4279 g_free(in.filename);
4280 g_free(out.filename);
4281 g_free(in.buf);
4282 g_free(out.buf);
4283
4284 if (ret) {
4285 return 1;
4286 }
4287 return 0;
4288}
4289
Kevin Wolfb6133b82014-08-05 14:17:13 +02004290
Anthony Liguoric227f092009-10-01 16:12:16 -05004291static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004292#define DEF(option, callback, arg_string) \
4293 { option, callback },
4294#include "qemu-img-cmds.h"
4295#undef DEF
4296#undef GEN_DOCS
4297 { NULL, NULL, },
4298};
4299
bellardea2384d2004-08-01 21:59:26 +00004300int main(int argc, char **argv)
4301{
Anthony Liguoric227f092009-10-01 16:12:16 -05004302 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004303 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004304 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004305 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004306 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004307 static const struct option long_options[] = {
4308 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004309 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004310 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004311 {0, 0, 0, 0}
4312 };
bellardea2384d2004-08-01 21:59:26 +00004313
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004314#ifdef CONFIG_POSIX
4315 signal(SIGPIPE, SIG_IGN);
4316#endif
4317
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004318 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004319 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004320 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004321
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004322 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004323 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004324 exit(EXIT_FAILURE);
4325 }
4326
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004327 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004328
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004329 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004330 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004331 if (argc < 2) {
4332 error_exit("Not enough arguments");
4333 }
Stuart Brady153859b2009-06-07 00:42:17 +01004334
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004335 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004336 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004337 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004338
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004339 while ((c = getopt_long(argc, argv, "+hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004340 switch (c) {
4341 case 'h':
4342 help();
4343 return 0;
4344 case 'V':
4345 printf(QEMU_IMG_VERSION);
4346 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004347 case 'T':
4348 g_free(trace_file);
4349 trace_file = trace_opt_parse(optarg);
4350 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004351 }
bellardea2384d2004-08-01 21:59:26 +00004352 }
Stuart Brady153859b2009-06-07 00:42:17 +01004353
Denis V. Lunev10985132016-06-17 17:44:13 +03004354 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004355
Denis V. Lunev10985132016-06-17 17:44:13 +03004356 /* reset getopt_long scanning */
4357 argc -= optind;
4358 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004359 return 0;
4360 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004361 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004362 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004363
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004364 if (!trace_init_backends()) {
4365 exit(1);
4366 }
4367 trace_init_file(trace_file);
4368 qemu_set_log(LOG_TRACE);
4369
Denis V. Lunev10985132016-06-17 17:44:13 +03004370 /* find the command */
4371 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4372 if (!strcmp(cmdname, cmd->name)) {
4373 return cmd->handler(argc, argv);
4374 }
4375 }
Jeff Cody7db16892014-04-25 17:02:32 -04004376
Stuart Brady153859b2009-06-07 00:42:17 +01004377 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004378 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004379}