blob: a48a471042649d60cc1a579e2e102bf1bf448c17 [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
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200817 aio_context_acquire(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +0200818 do {
819 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500820 qemu_progress_print(job->len ?
821 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200822 } while (!job->ready);
823
824 block_job_complete_sync(job, errp);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200825 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200826
827 /* A block job may finish instantaneously without publishing any progress,
828 * so just signal completion here */
829 qemu_progress_print(100.f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200830}
831
bellardea2384d2004-08-01 21:59:26 +0000832static int img_commit(int argc, char **argv)
833{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400834 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200835 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200836 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200837 BlockDriverState *bs, *base_bs;
Max Reitz687fa1d2014-10-24 15:57:39 +0200838 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100839 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200840 Error *local_err = NULL;
841 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000842 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200843 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000844
845 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400846 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200847 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000848 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000849 static const struct option long_options[] = {
850 {"help", no_argument, 0, 'h'},
851 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000852 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000853 {0, 0, 0, 0}
854 };
855 c = getopt_long(argc, argv, "f:ht:b:dpq",
856 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100857 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000858 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100859 }
bellardea2384d2004-08-01 21:59:26 +0000860 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100861 case '?':
bellardea2384d2004-08-01 21:59:26 +0000862 case 'h':
863 help();
864 break;
865 case 'f':
866 fmt = optarg;
867 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400868 case 't':
869 cache = optarg;
870 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200871 case 'b':
872 base = optarg;
873 /* -b implies -d */
874 drop = true;
875 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200876 case 'd':
877 drop = true;
878 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200879 case 'p':
880 progress = true;
881 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100882 case 'q':
883 quiet = true;
884 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000885 case OPTION_OBJECT: {
886 QemuOpts *opts;
887 opts = qemu_opts_parse_noisily(&qemu_object_opts,
888 optarg, true);
889 if (!opts) {
890 return 1;
891 }
892 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000893 case OPTION_IMAGE_OPTS:
894 image_opts = true;
895 break;
bellardea2384d2004-08-01 21:59:26 +0000896 }
897 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200898
899 /* Progress is not shown in Quiet mode */
900 if (quiet) {
901 progress = false;
902 }
903
Kevin Wolffc11eb22013-08-05 10:53:04 +0200904 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800905 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100906 }
bellardea2384d2004-08-01 21:59:26 +0000907 filename = argv[optind++];
908
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000909 if (qemu_opts_foreach(&qemu_object_opts,
910 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200911 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000912 return 1;
913 }
914
Max Reitz9a86fe42014-10-24 15:57:38 +0200915 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100916 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400917 if (ret < 0) {
918 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100919 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400920 }
921
Kevin Wolfce099542016-03-15 13:01:04 +0100922 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200923 if (!blk) {
924 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900925 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200926 bs = blk_bs(blk);
927
Max Reitz687fa1d2014-10-24 15:57:39 +0200928 qemu_progress_init(progress, 1.f);
929 qemu_progress_print(0.f, 100);
930
Max Reitz1b22bff2014-10-24 15:57:40 +0200931 if (base) {
932 base_bs = bdrv_find_backing_image(bs, base);
933 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +0100934 error_setg(&local_err,
935 "Did not find '%s' in the backing chain of '%s'",
936 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +0200937 goto done;
938 }
939 } else {
940 /* This is different from QMP, which by default uses the deepest file in
941 * the backing chain (i.e., the very base); however, the traditional
942 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +0200943 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +0200944 if (!base_bs) {
945 error_setg(&local_err, "Image does not have a backing file");
946 goto done;
947 }
bellardea2384d2004-08-01 21:59:26 +0000948 }
949
Max Reitzd4a32382014-10-24 15:57:37 +0200950 cbi = (CommonBlockJobCBInfo){
951 .errp = &local_err,
952 .bs = bs,
953 };
954
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200955 aio_context = bdrv_get_aio_context(bs);
956 aio_context_acquire(aio_context);
John Snow47970df2016-10-27 12:06:57 -0400957 commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
958 BLOCKDEV_ON_ERROR_REPORT, common_block_job_cb, &cbi,
959 &local_err, false);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200960 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +0200961 if (local_err) {
962 goto done;
963 }
964
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200965 /* When the block job completes, the BlockBackend reference will point to
966 * the old backing file. In order to avoid that the top image is already
967 * deleted, so we can still empty it afterwards, increment the reference
968 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +0200969 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200970 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200971 }
972
Max Reitzd4a32382014-10-24 15:57:37 +0200973 run_block_job(bs->job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +0200974 if (local_err) {
975 goto unref_backing;
976 }
977
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200978 if (!drop && bs->drv->bdrv_make_empty) {
979 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200980 if (ret) {
981 error_setg_errno(&local_err, -ret, "Could not empty %s",
982 filename);
983 goto unref_backing;
984 }
985 }
986
987unref_backing:
988 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +0200989 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +0200990 }
Max Reitzd4a32382014-10-24 15:57:37 +0200991
992done:
Max Reitz687fa1d2014-10-24 15:57:39 +0200993 qemu_progress_end();
994
Markus Armbruster26f54e92014-10-07 13:59:04 +0200995 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +0200996
997 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +0100998 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900999 return 1;
1000 }
Max Reitzd4a32382014-10-24 15:57:37 +02001001
1002 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001003 return 0;
1004}
1005
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001006/*
thsf58c7b32008-06-05 21:53:49 +00001007 * Returns true iff the first sector pointed to by 'buf' contains at least
1008 * a non-NUL byte.
1009 *
1010 * 'pnum' is set to the number of sectors (including and immediately following
1011 * the first one) that are known to be in the same allocated/unallocated state.
1012 */
bellardea2384d2004-08-01 21:59:26 +00001013static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1014{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001015 bool is_zero;
1016 int i;
bellardea2384d2004-08-01 21:59:26 +00001017
1018 if (n <= 0) {
1019 *pnum = 0;
1020 return 0;
1021 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001022 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001023 for(i = 1; i < n; i++) {
1024 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001025 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001026 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001027 }
bellardea2384d2004-08-01 21:59:26 +00001028 }
1029 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001030 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001031}
1032
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001033/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001034 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1035 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1036 * breaking up write requests for only small sparse areas.
1037 */
1038static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1039 int min)
1040{
1041 int ret;
1042 int num_checked, num_used;
1043
1044 if (n < min) {
1045 min = n;
1046 }
1047
1048 ret = is_allocated_sectors(buf, n, pnum);
1049 if (!ret) {
1050 return ret;
1051 }
1052
1053 num_used = *pnum;
1054 buf += BDRV_SECTOR_SIZE * *pnum;
1055 n -= *pnum;
1056 num_checked = num_used;
1057
1058 while (n > 0) {
1059 ret = is_allocated_sectors(buf, n, pnum);
1060
1061 buf += BDRV_SECTOR_SIZE * *pnum;
1062 n -= *pnum;
1063 num_checked += *pnum;
1064 if (ret) {
1065 num_used = num_checked;
1066 } else if (*pnum >= min) {
1067 break;
1068 }
1069 }
1070
1071 *pnum = num_used;
1072 return 1;
1073}
1074
1075/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001076 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1077 * buffers matches, non-zero otherwise.
1078 *
1079 * pnum is set to the number of sectors (including and immediately following
1080 * the first one) that are known to have the same comparison result
1081 */
1082static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1083 int *pnum)
1084{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001085 bool res;
1086 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001087
1088 if (n <= 0) {
1089 *pnum = 0;
1090 return 0;
1091 }
1092
1093 res = !!memcmp(buf1, buf2, 512);
1094 for(i = 1; i < n; i++) {
1095 buf1 += 512;
1096 buf2 += 512;
1097
1098 if (!!memcmp(buf1, buf2, 512) != res) {
1099 break;
1100 }
1101 }
1102
1103 *pnum = i;
1104 return res;
1105}
1106
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001107#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001108
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001109static int64_t sectors_to_bytes(int64_t sectors)
1110{
1111 return sectors << BDRV_SECTOR_BITS;
1112}
1113
1114static int64_t sectors_to_process(int64_t total, int64_t from)
1115{
1116 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1117}
1118
1119/*
1120 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1121 *
1122 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1123 * data and negative value on error.
1124 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001125 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001126 * @param sect_num: Number of first sector to check
1127 * @param sect_count: Number of sectors to check
1128 * @param filename: Name of disk file we are checking (logging purpose)
1129 * @param buffer: Allocated buffer for storing read data
1130 * @param quiet: Flag for quiet mode
1131 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001132static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001133 int sect_count, const char *filename,
1134 uint8_t *buffer, bool quiet)
1135{
1136 int pnum, ret = 0;
Eric Blake91669202016-05-06 10:26:43 -06001137 ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer,
1138 sect_count << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001139 if (ret < 0) {
1140 error_report("Error while reading offset %" PRId64 " of %s: %s",
1141 sectors_to_bytes(sect_num), filename, strerror(-ret));
1142 return ret;
1143 }
1144 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1145 if (ret || pnum != sect_count) {
1146 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1147 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1148 return 1;
1149 }
1150
1151 return 0;
1152}
1153
1154/*
1155 * Compares two images. Exit codes:
1156 *
1157 * 0 - Images are identical
1158 * 1 - Images differ
1159 * >1 - Error occurred
1160 */
1161static int img_compare(int argc, char **argv)
1162{
Max Reitz40055952014-07-22 22:58:42 +02001163 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001164 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001165 BlockDriverState *bs1, *bs2;
1166 int64_t total_sectors1, total_sectors2;
1167 uint8_t *buf1 = NULL, *buf2 = NULL;
1168 int pnum1, pnum2;
1169 int allocated1, allocated2;
1170 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1171 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001172 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001173 bool writethrough;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001174 int64_t total_sectors;
1175 int64_t sector_num = 0;
1176 int64_t nb_sectors;
1177 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001178 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001179 bool image_opts = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001180
Max Reitz40055952014-07-22 22:58:42 +02001181 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001182 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001183 static const struct option long_options[] = {
1184 {"help", no_argument, 0, 'h'},
1185 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001186 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001187 {0, 0, 0, 0}
1188 };
1189 c = getopt_long(argc, argv, "hf:F:T:pqs",
1190 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001191 if (c == -1) {
1192 break;
1193 }
1194 switch (c) {
1195 case '?':
1196 case 'h':
1197 help();
1198 break;
1199 case 'f':
1200 fmt1 = optarg;
1201 break;
1202 case 'F':
1203 fmt2 = optarg;
1204 break;
Max Reitz40055952014-07-22 22:58:42 +02001205 case 'T':
1206 cache = optarg;
1207 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001208 case 'p':
1209 progress = true;
1210 break;
1211 case 'q':
1212 quiet = true;
1213 break;
1214 case 's':
1215 strict = true;
1216 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001217 case OPTION_OBJECT: {
1218 QemuOpts *opts;
1219 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1220 optarg, true);
1221 if (!opts) {
1222 ret = 2;
1223 goto out4;
1224 }
1225 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001226 case OPTION_IMAGE_OPTS:
1227 image_opts = true;
1228 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001229 }
1230 }
1231
1232 /* Progress is not shown in Quiet mode */
1233 if (quiet) {
1234 progress = false;
1235 }
1236
1237
Kevin Wolffc11eb22013-08-05 10:53:04 +02001238 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001239 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001240 }
1241 filename1 = argv[optind++];
1242 filename2 = argv[optind++];
1243
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001244 if (qemu_opts_foreach(&qemu_object_opts,
1245 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001246 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001247 ret = 2;
1248 goto out4;
1249 }
1250
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001251 /* Initialize before goto out */
1252 qemu_progress_init(progress, 2.0);
1253
Kevin Wolfce099542016-03-15 13:01:04 +01001254 flags = 0;
1255 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001256 if (ret < 0) {
1257 error_report("Invalid source cache option: %s", cache);
1258 ret = 2;
1259 goto out3;
1260 }
1261
Kevin Wolfce099542016-03-15 13:01:04 +01001262 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001263 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001264 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001265 goto out3;
1266 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001267
Kevin Wolfce099542016-03-15 13:01:04 +01001268 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001269 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001270 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001271 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001272 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001273 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001274 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001275
Max Reitzf1d3cd72015-02-05 13:58:18 -05001276 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1277 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1278 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001279 if (total_sectors1 < 0) {
1280 error_report("Can't get size of %s: %s",
1281 filename1, strerror(-total_sectors1));
1282 ret = 4;
1283 goto out;
1284 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001285 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001286 if (total_sectors2 < 0) {
1287 error_report("Can't get size of %s: %s",
1288 filename2, strerror(-total_sectors2));
1289 ret = 4;
1290 goto out;
1291 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001292 total_sectors = MIN(total_sectors1, total_sectors2);
1293 progress_base = MAX(total_sectors1, total_sectors2);
1294
1295 qemu_progress_print(0, 100);
1296
1297 if (strict && total_sectors1 != total_sectors2) {
1298 ret = 1;
1299 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1300 goto out;
1301 }
1302
1303 for (;;) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001304 int64_t status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001305 BlockDriverState *file;
1306
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001307 nb_sectors = sectors_to_process(total_sectors, sector_num);
1308 if (nb_sectors <= 0) {
1309 break;
1310 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001311 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1312 total_sectors1 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001313 &pnum1, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001314 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001315 ret = 3;
1316 error_report("Sector allocation test failed for %s", filename1);
1317 goto out;
1318 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001319 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001320
Fam Zheng25ad8e62016-01-13 16:37:41 +08001321 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1322 total_sectors2 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001323 &pnum2, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001324 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001325 ret = 3;
1326 error_report("Sector allocation test failed for %s", filename2);
1327 goto out;
1328 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001329 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1330 if (pnum1) {
1331 nb_sectors = MIN(nb_sectors, pnum1);
1332 }
1333 if (pnum2) {
1334 nb_sectors = MIN(nb_sectors, pnum2);
1335 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001336
Fam Zheng25ad8e62016-01-13 16:37:41 +08001337 if (strict) {
1338 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1339 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1340 ret = 1;
1341 qprintf(quiet, "Strict mode: Offset %" PRId64
1342 " block status mismatch!\n",
1343 sectors_to_bytes(sector_num));
1344 goto out;
1345 }
1346 }
1347 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1348 nb_sectors = MIN(pnum1, pnum2);
1349 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001350 if (allocated1) {
Eric Blake91669202016-05-06 10:26:43 -06001351 ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1,
1352 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001353 if (ret < 0) {
1354 error_report("Error while reading offset %" PRId64 " of %s:"
1355 " %s", sectors_to_bytes(sector_num), filename1,
1356 strerror(-ret));
1357 ret = 4;
1358 goto out;
1359 }
Eric Blake91669202016-05-06 10:26:43 -06001360 ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2,
1361 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001362 if (ret < 0) {
1363 error_report("Error while reading offset %" PRId64
1364 " of %s: %s", sectors_to_bytes(sector_num),
1365 filename2, strerror(-ret));
1366 ret = 4;
1367 goto out;
1368 }
1369 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1370 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001371 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1372 sectors_to_bytes(
1373 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001374 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001375 goto out;
1376 }
1377 }
1378 } else {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001379
1380 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001381 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001382 filename1, buf1, quiet);
1383 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001384 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001385 filename2, buf1, quiet);
1386 }
1387 if (ret) {
1388 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001389 error_report("Error while reading offset %" PRId64 ": %s",
1390 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001391 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001392 }
1393 goto out;
1394 }
1395 }
1396 sector_num += nb_sectors;
1397 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1398 }
1399
1400 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001401 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001402 int64_t total_sectors_over;
1403 const char *filename_over;
1404
1405 qprintf(quiet, "Warning: Image size mismatch!\n");
1406 if (total_sectors1 > total_sectors2) {
1407 total_sectors_over = total_sectors1;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001408 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001409 filename_over = filename1;
1410 } else {
1411 total_sectors_over = total_sectors2;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001412 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001413 filename_over = filename2;
1414 }
1415
1416 for (;;) {
1417 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1418 if (nb_sectors <= 0) {
1419 break;
1420 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001421 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001422 nb_sectors, &pnum);
1423 if (ret < 0) {
1424 ret = 3;
1425 error_report("Sector allocation test failed for %s",
1426 filename_over);
1427 goto out;
1428
1429 }
1430 nb_sectors = pnum;
1431 if (ret) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001432 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001433 filename_over, buf1, quiet);
1434 if (ret) {
1435 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001436 error_report("Error while reading offset %" PRId64
1437 " of %s: %s", sectors_to_bytes(sector_num),
1438 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001439 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001440 }
1441 goto out;
1442 }
1443 }
1444 sector_num += nb_sectors;
1445 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1446 }
1447 }
1448
1449 qprintf(quiet, "Images are identical.\n");
1450 ret = 0;
1451
1452out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001453 qemu_vfree(buf1);
1454 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001455 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001456out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001457 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001458out3:
1459 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001460out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001461 return ret;
1462}
1463
Kevin Wolf690c7302015-03-19 13:33:32 +01001464enum ImgConvertBlockStatus {
1465 BLK_DATA,
1466 BLK_ZERO,
1467 BLK_BACKING_FILE,
1468};
1469
Peter Lieven2d9187b2017-02-28 13:40:07 +01001470#define MAX_COROUTINES 16
1471
Kevin Wolf690c7302015-03-19 13:33:32 +01001472typedef struct ImgConvertState {
1473 BlockBackend **src;
1474 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001475 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001476 int64_t total_sectors;
1477 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001478 int64_t allocated_done;
1479 int64_t sector_num;
1480 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001481 enum ImgConvertBlockStatus status;
1482 int64_t sector_next_status;
1483 BlockBackend *target;
1484 bool has_zero_init;
1485 bool compressed;
1486 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001487 bool wr_in_order;
Kevin Wolf690c7302015-03-19 13:33:32 +01001488 int min_sparse;
1489 size_t cluster_sectors;
1490 size_t buf_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001491 int num_coroutines;
1492 int running_coroutines;
1493 Coroutine *co[MAX_COROUTINES];
1494 int64_t wait_sector_num[MAX_COROUTINES];
1495 CoMutex lock;
1496 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001497} ImgConvertState;
1498
Peter Lieven2d9187b2017-02-28 13:40:07 +01001499static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1500 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001501{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001502 *src_cur = 0;
1503 *src_cur_offset = 0;
1504 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1505 *src_cur_offset += s->src_sectors[*src_cur];
1506 (*src_cur)++;
1507 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001508 }
1509}
1510
1511static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1512{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001513 int64_t ret, src_cur_offset;
1514 int n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001515
Peter Lieven2d9187b2017-02-28 13:40:07 +01001516 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001517
1518 assert(s->total_sectors > sector_num);
1519 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1520
1521 if (s->sector_next_status <= sector_num) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08001522 BlockDriverState *file;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001523 ret = bdrv_get_block_status(blk_bs(s->src[src_cur]),
1524 sector_num - src_cur_offset,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001525 n, &n, &file);
Kevin Wolf690c7302015-03-19 13:33:32 +01001526 if (ret < 0) {
1527 return ret;
1528 }
1529
1530 if (ret & BDRV_BLOCK_ZERO) {
1531 s->status = BLK_ZERO;
1532 } else if (ret & BDRV_BLOCK_DATA) {
1533 s->status = BLK_DATA;
1534 } else if (!s->target_has_backing) {
1535 /* Without a target backing file we must copy over the contents of
1536 * the backing file as well. */
Ren Kimura263a6f42016-04-28 01:04:58 +09001537 /* Check block status of the backing file chain to avoid
Kevin Wolf690c7302015-03-19 13:33:32 +01001538 * needlessly reading zeroes and limiting the iteration to the
1539 * buffer size */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001540 ret = bdrv_get_block_status_above(blk_bs(s->src[src_cur]), NULL,
1541 sector_num - src_cur_offset,
Ren Kimura263a6f42016-04-28 01:04:58 +09001542 n, &n, &file);
1543 if (ret < 0) {
1544 return ret;
1545 }
1546
1547 if (ret & BDRV_BLOCK_ZERO) {
1548 s->status = BLK_ZERO;
1549 } else {
1550 s->status = BLK_DATA;
1551 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001552 } else {
1553 s->status = BLK_BACKING_FILE;
1554 }
1555
1556 s->sector_next_status = sector_num + n;
1557 }
1558
1559 n = MIN(n, s->sector_next_status - sector_num);
1560 if (s->status == BLK_DATA) {
1561 n = MIN(n, s->buf_sectors);
1562 }
1563
1564 /* We need to write complete clusters for compressed images, so if an
1565 * unallocated area is shorter than that, we must consider the whole
1566 * cluster allocated. */
1567 if (s->compressed) {
1568 if (n < s->cluster_sectors) {
1569 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1570 s->status = BLK_DATA;
1571 } else {
1572 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1573 }
1574 }
1575
1576 return n;
1577}
1578
Peter Lieven2d9187b2017-02-28 13:40:07 +01001579static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1580 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001581{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001582 int n, ret;
1583 QEMUIOVector qiov;
1584 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001585
Kevin Wolf690c7302015-03-19 13:33:32 +01001586 assert(nb_sectors <= s->buf_sectors);
1587 while (nb_sectors > 0) {
1588 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001589 int src_cur;
1590 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001591
1592 /* In the case of compression with multiple source files, we can get a
1593 * nb_sectors that spreads into the next part. So we must be able to
1594 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001595 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1596 blk = s->src[src_cur];
1597 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001598
Peter Lieven2d9187b2017-02-28 13:40:07 +01001599 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1600 iov.iov_base = buf;
1601 iov.iov_len = n << BDRV_SECTOR_BITS;
1602 qemu_iovec_init_external(&qiov, &iov, 1);
1603
1604 ret = blk_co_preadv(
1605 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1606 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001607 if (ret < 0) {
1608 return ret;
1609 }
1610
1611 sector_num += n;
1612 nb_sectors -= n;
1613 buf += n * BDRV_SECTOR_SIZE;
1614 }
1615
1616 return 0;
1617}
1618
Peter Lieven2d9187b2017-02-28 13:40:07 +01001619
1620static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1621 int nb_sectors, uint8_t *buf,
1622 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001623{
1624 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001625 QEMUIOVector qiov;
1626 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001627
1628 while (nb_sectors > 0) {
1629 int n = nb_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001630 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001631 case BLK_BACKING_FILE:
1632 /* If we have a backing file, leave clusters unallocated that are
1633 * unallocated in the source image, so that the backing file is
1634 * visible at the respective offset. */
1635 assert(s->target_has_backing);
1636 break;
1637
1638 case BLK_DATA:
1639 /* We must always write compressed clusters as a whole, so don't
1640 * try to find zeroed parts in the buffer. We can only save the
1641 * write if the buffer is completely zeroed and we're allowed to
1642 * keep the target sparse. */
1643 if (s->compressed) {
1644 if (s->has_zero_init && s->min_sparse &&
1645 buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))
1646 {
1647 assert(!s->target_has_backing);
1648 break;
1649 }
1650
Peter Lieven2d9187b2017-02-28 13:40:07 +01001651 iov.iov_base = buf;
1652 iov.iov_len = n << BDRV_SECTOR_BITS;
1653 qemu_iovec_init_external(&qiov, &iov, 1);
1654
1655 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
1656 n << BDRV_SECTOR_BITS, &qiov,
1657 BDRV_REQ_WRITE_COMPRESSED);
Kevin Wolf690c7302015-03-19 13:33:32 +01001658 if (ret < 0) {
1659 return ret;
1660 }
1661 break;
1662 }
1663
1664 /* If there is real non-zero data or we're told to keep the target
1665 * fully allocated (-S 0), we must write it. Otherwise we can treat
1666 * it as zero sectors. */
1667 if (!s->min_sparse ||
1668 is_allocated_sectors_min(buf, n, &n, s->min_sparse))
1669 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001670 iov.iov_base = buf;
1671 iov.iov_len = n << BDRV_SECTOR_BITS;
1672 qemu_iovec_init_external(&qiov, &iov, 1);
1673
1674 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
1675 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001676 if (ret < 0) {
1677 return ret;
1678 }
1679 break;
1680 }
1681 /* fall-through */
1682
1683 case BLK_ZERO:
1684 if (s->has_zero_init) {
1685 break;
1686 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001687 ret = blk_co_pwrite_zeroes(s->target,
1688 sector_num << BDRV_SECTOR_BITS,
1689 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001690 if (ret < 0) {
1691 return ret;
1692 }
1693 break;
1694 }
1695
1696 sector_num += n;
1697 nb_sectors -= n;
1698 buf += n * BDRV_SECTOR_SIZE;
1699 }
1700
1701 return 0;
1702}
1703
Peter Lieven2d9187b2017-02-28 13:40:07 +01001704static void coroutine_fn convert_co_do_copy(void *opaque)
1705{
1706 ImgConvertState *s = opaque;
1707 uint8_t *buf = NULL;
1708 int ret, i;
1709 int index = -1;
1710
1711 for (i = 0; i < s->num_coroutines; i++) {
1712 if (s->co[i] == qemu_coroutine_self()) {
1713 index = i;
1714 break;
1715 }
1716 }
1717 assert(index >= 0);
1718
1719 s->running_coroutines++;
1720 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1721
1722 while (1) {
1723 int n;
1724 int64_t sector_num;
1725 enum ImgConvertBlockStatus status;
1726
1727 qemu_co_mutex_lock(&s->lock);
1728 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1729 qemu_co_mutex_unlock(&s->lock);
1730 goto out;
1731 }
1732 n = convert_iteration_sectors(s, s->sector_num);
1733 if (n < 0) {
1734 qemu_co_mutex_unlock(&s->lock);
1735 s->ret = n;
1736 goto out;
1737 }
1738 /* save current sector and allocation status to local variables */
1739 sector_num = s->sector_num;
1740 status = s->status;
1741 if (!s->min_sparse && s->status == BLK_ZERO) {
1742 n = MIN(n, s->buf_sectors);
1743 }
1744 /* increment global sector counter so that other coroutines can
1745 * already continue reading beyond this request */
1746 s->sector_num += n;
1747 qemu_co_mutex_unlock(&s->lock);
1748
1749 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1750 s->allocated_done += n;
1751 qemu_progress_print(100.0 * s->allocated_done /
1752 s->allocated_sectors, 0);
1753 }
1754
1755 if (status == BLK_DATA) {
1756 ret = convert_co_read(s, sector_num, n, buf);
1757 if (ret < 0) {
1758 error_report("error while reading sector %" PRId64
1759 ": %s", sector_num, strerror(-ret));
1760 s->ret = ret;
1761 goto out;
1762 }
1763 } else if (!s->min_sparse && status == BLK_ZERO) {
1764 status = BLK_DATA;
1765 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1766 }
1767
1768 if (s->wr_in_order) {
1769 /* keep writes in order */
1770 while (s->wr_offs != sector_num) {
1771 if (s->ret != -EINPROGRESS) {
1772 goto out;
1773 }
1774 s->wait_sector_num[index] = sector_num;
1775 qemu_coroutine_yield();
1776 }
1777 s->wait_sector_num[index] = -1;
1778 }
1779
1780 ret = convert_co_write(s, sector_num, n, buf, status);
1781 if (ret < 0) {
1782 error_report("error while writing sector %" PRId64
1783 ": %s", sector_num, strerror(-ret));
1784 s->ret = ret;
1785 goto out;
1786 }
1787
1788 if (s->wr_in_order) {
1789 /* reenter the coroutine that might have waited
1790 * for this write to complete */
1791 s->wr_offs = sector_num + n;
1792 for (i = 0; i < s->num_coroutines; i++) {
1793 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1794 /*
1795 * A -> B -> A cannot occur because A has
1796 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1797 * B will never enter A during this time window.
1798 */
1799 qemu_coroutine_enter(s->co[i]);
1800 break;
1801 }
1802 }
1803 }
1804 }
1805
1806out:
1807 qemu_vfree(buf);
1808 s->co[index] = NULL;
1809 s->running_coroutines--;
1810 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1811 /* the convert job finished successfully */
1812 s->ret = 0;
1813 }
1814}
1815
Kevin Wolf690c7302015-03-19 13:33:32 +01001816static int convert_do_copy(ImgConvertState *s)
1817{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001818 int ret, i, n;
1819 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001820
1821 /* Check whether we have zero initialisation or can get it efficiently */
1822 s->has_zero_init = s->min_sparse && !s->target_has_backing
1823 ? bdrv_has_zero_init(blk_bs(s->target))
1824 : false;
1825
1826 if (!s->has_zero_init && !s->target_has_backing &&
1827 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1828 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001829 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001830 if (ret == 0) {
1831 s->has_zero_init = true;
1832 }
1833 }
1834
1835 /* Allocate buffer for copied data. For compressed images, only one cluster
1836 * can be copied at a time. */
1837 if (s->compressed) {
1838 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1839 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001840 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001841 }
1842 s->buf_sectors = s->cluster_sectors;
1843 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001844
Kevin Wolf690c7302015-03-19 13:33:32 +01001845 while (sector_num < s->total_sectors) {
1846 n = convert_iteration_sectors(s, sector_num);
1847 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001848 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001849 }
Max Reitzaad15de2016-03-24 23:33:57 +01001850 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1851 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001852 s->allocated_sectors += n;
1853 }
1854 sector_num += n;
1855 }
1856
1857 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001858 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001859 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001860
Peter Lieven2d9187b2017-02-28 13:40:07 +01001861 qemu_co_mutex_init(&s->lock);
1862 for (i = 0; i < s->num_coroutines; i++) {
1863 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1864 s->wait_sector_num[i] = -1;
1865 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001866 }
1867
Peter Lieven2d9187b2017-02-28 13:40:07 +01001868 while (s->ret == -EINPROGRESS) {
1869 main_loop_wait(false);
1870 }
1871
1872 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001873 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001874 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001875 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001876 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001877 }
1878 }
1879
Peter Lieven2d9187b2017-02-28 13:40:07 +01001880 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001881}
1882
bellardea2384d2004-08-01 21:59:26 +00001883static int img_convert(int argc, char **argv)
1884{
Kevin Wolf690c7302015-03-19 13:33:32 +01001885 int c, bs_n, bs_i, compress, cluster_sectors, skip_create;
Peter Lieven13c28af2013-11-27 11:07:01 +01001886 int64_t ret = 0;
Max Reitz40055952014-07-22 22:58:42 +02001887 int progress = 0, flags, src_flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001888 bool writethrough, src_writethrough;
Max Reitz40055952014-07-22 22:58:42 +02001889 const char *fmt, *out_fmt, *cache, *src_cache, *out_baseimg, *out_filename;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001890 BlockDriver *drv, *proto_drv;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001891 BlockBackend **blk = NULL, *out_blk = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001892 BlockDriverState **bs = NULL, *out_bs = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001893 int64_t total_sectors;
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001894 int64_t *bs_sectors = NULL;
Peter Lievenf2521c92013-11-27 11:07:06 +01001895 size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE;
bellardfaea38e2006-08-05 21:31:00 +00001896 BlockDriverInfo bdi;
Chunyan Liu83d05212014-06-05 17:20:51 +08001897 QemuOpts *opts = NULL;
1898 QemuOptsList *create_opts = NULL;
1899 const char *out_baseimg_param;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001900 char *options = NULL;
edison51ef6722010-09-21 19:58:41 -07001901 const char *snapshot_name = NULL;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001902 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001903 bool quiet = false;
Max Reitzcc84d902013-09-06 17:14:26 +02001904 Error *local_err = NULL;
Wenchao Xiaef806542013-12-04 17:10:57 +08001905 QemuOpts *sn_opts = NULL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001906 ImgConvertState state;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001907 bool image_opts = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001908 bool wr_in_order = true;
1909 long num_coroutines = 8;
bellardea2384d2004-08-01 21:59:26 +00001910
1911 fmt = NULL;
1912 out_fmt = "raw";
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001913 cache = "unsafe";
Max Reitz40055952014-07-22 22:58:42 +02001914 src_cache = BDRV_DEFAULT_CACHE;
thsf58c7b32008-06-05 21:53:49 +00001915 out_baseimg = NULL;
Jes Sorenseneec77d92010-12-07 17:44:34 +01001916 compress = 0;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001917 skip_create = 0;
bellardea2384d2004-08-01 21:59:26 +00001918 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001919 static const struct option long_options[] = {
1920 {"help", no_argument, 0, 'h'},
1921 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001922 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001923 {0, 0, 0, 0}
1924 };
Peter Lieven2d9187b2017-02-28 13:40:07 +01001925 c = getopt_long(argc, argv, "hf:O:B:ce6o:s:l:S:pt:T:qnm:W",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001926 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001927 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001928 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001929 }
bellardea2384d2004-08-01 21:59:26 +00001930 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001931 case '?':
bellardea2384d2004-08-01 21:59:26 +00001932 case 'h':
1933 help();
1934 break;
1935 case 'f':
1936 fmt = optarg;
1937 break;
1938 case 'O':
1939 out_fmt = optarg;
1940 break;
thsf58c7b32008-06-05 21:53:49 +00001941 case 'B':
1942 out_baseimg = optarg;
1943 break;
bellardea2384d2004-08-01 21:59:26 +00001944 case 'c':
Jes Sorenseneec77d92010-12-07 17:44:34 +01001945 compress = 1;
bellardea2384d2004-08-01 21:59:26 +00001946 break;
1947 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001948 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001949 "encryption\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001950 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001951 goto fail_getopt;
thsec36ba12007-09-16 21:59:02 +00001952 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001953 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001954 "compat6\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001955 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001956 goto fail_getopt;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001957 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001958 if (!is_valid_option_list(optarg)) {
1959 error_report("Invalid option list: %s", optarg);
1960 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001961 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001962 }
1963 if (!options) {
1964 options = g_strdup(optarg);
1965 } else {
1966 char *old_options = options;
1967 options = g_strdup_printf("%s,%s", options, optarg);
1968 g_free(old_options);
1969 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001970 break;
edison51ef6722010-09-21 19:58:41 -07001971 case 's':
1972 snapshot_name = optarg;
1973 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001974 case 'l':
1975 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01001976 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
1977 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08001978 if (!sn_opts) {
1979 error_report("Failed in parsing snapshot param '%s'",
1980 optarg);
Kevin Wolf2dc83282014-02-21 16:24:05 +01001981 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001982 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08001983 }
1984 } else {
1985 snapshot_name = optarg;
1986 }
1987 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001988 case 'S':
1989 {
1990 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01001991
1992 sval = cvtnum(optarg);
1993 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02001994 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001995 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001996 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001997 }
1998
1999 min_sparse = sval / BDRV_SECTOR_SIZE;
2000 break;
2001 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002002 case 'p':
2003 progress = 1;
2004 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002005 case 't':
2006 cache = optarg;
2007 break;
Max Reitz40055952014-07-22 22:58:42 +02002008 case 'T':
2009 src_cache = optarg;
2010 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002011 case 'q':
2012 quiet = true;
2013 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002014 case 'n':
2015 skip_create = 1;
2016 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002017 case 'm':
2018 if (qemu_strtol(optarg, NULL, 0, &num_coroutines) ||
2019 num_coroutines < 1 || num_coroutines > MAX_COROUTINES) {
2020 error_report("Invalid number of coroutines. Allowed number of"
2021 " coroutines is between 1 and %d", MAX_COROUTINES);
2022 ret = -1;
2023 goto fail_getopt;
2024 }
2025 break;
2026 case 'W':
2027 wr_in_order = false;
2028 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002029 case OPTION_OBJECT:
2030 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2031 optarg, true);
2032 if (!opts) {
2033 goto fail_getopt;
2034 }
2035 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002036 case OPTION_IMAGE_OPTS:
2037 image_opts = true;
2038 break;
bellardea2384d2004-08-01 21:59:26 +00002039 }
2040 }
ths3b46e622007-09-17 08:09:54 +00002041
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002042 if (qemu_opts_foreach(&qemu_object_opts,
2043 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002044 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002045 goto fail_getopt;
2046 }
2047
Peter Lieven2d9187b2017-02-28 13:40:07 +01002048 if (!wr_in_order && compress) {
2049 error_report("Out of order write and compress are mutually exclusive");
2050 ret = -1;
2051 goto fail_getopt;
2052 }
2053
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002054 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002055 if (quiet) {
2056 progress = 0;
2057 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002058 qemu_progress_init(progress, 1.0);
2059
balrog926c2d22007-10-31 01:11:44 +00002060 bs_n = argc - optind - 1;
Kevin Wolfa283cb62014-02-21 16:24:07 +01002061 out_filename = bs_n >= 1 ? argv[argc - 1] : NULL;
thsf58c7b32008-06-05 21:53:49 +00002062
Kevin Wolf2dc83282014-02-21 16:24:05 +01002063 if (options && has_help_option(options)) {
Jes Sorensen4ac8aac2010-12-06 15:25:38 +01002064 ret = print_block_option_help(out_filename, out_fmt);
2065 goto out;
2066 }
2067
bellardea2384d2004-08-01 21:59:26 +00002068 if (bs_n < 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002069 error_exit("Must specify image file name");
ths5fafdf22007-09-16 21:08:06 +00002070 }
bellardea2384d2004-08-01 21:59:26 +00002071
thsf58c7b32008-06-05 21:53:49 +00002072
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002073 if (bs_n > 1 && out_baseimg) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002074 error_report("-B makes no sense when concatenating multiple input "
2075 "images");
Jes Sorensen31ca34b2010-12-06 15:25:36 +01002076 ret = -1;
2077 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002078 }
Dong Xu Wangf8111c22012-03-15 20:13:31 +08002079
Kevin Wolfce099542016-03-15 13:01:04 +01002080 src_flags = 0;
2081 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02002082 if (ret < 0) {
2083 error_report("Invalid source cache option: %s", src_cache);
2084 goto out;
2085 }
2086
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002087 qemu_progress_print(0, 100);
2088
Markus Armbruster26f54e92014-10-07 13:59:04 +02002089 blk = g_new0(BlockBackend *, bs_n);
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02002090 bs = g_new0(BlockDriverState *, bs_n);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002091 bs_sectors = g_new(int64_t, bs_n);
balrog926c2d22007-10-31 01:11:44 +00002092
2093 total_sectors = 0;
2094 for (bs_i = 0; bs_i < bs_n; bs_i++) {
Max Reitzefaa7c42016-03-16 19:54:38 +01002095 blk[bs_i] = img_open(image_opts, argv[optind + bs_i],
Kevin Wolfce099542016-03-15 13:01:04 +01002096 fmt, src_flags, src_writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002097 if (!blk[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002098 ret = -1;
2099 goto out;
2100 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002101 bs[bs_i] = blk_bs(blk[bs_i]);
Max Reitzf1d3cd72015-02-05 13:58:18 -05002102 bs_sectors[bs_i] = blk_nb_sectors(blk[bs_i]);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002103 if (bs_sectors[bs_i] < 0) {
2104 error_report("Could not get size of %s: %s",
2105 argv[optind + bs_i], strerror(-bs_sectors[bs_i]));
2106 ret = -1;
2107 goto out;
2108 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02002109 total_sectors += bs_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002110 }
bellardea2384d2004-08-01 21:59:26 +00002111
Wenchao Xiaef806542013-12-04 17:10:57 +08002112 if (sn_opts) {
Peter Maydell10d6eda2017-02-10 16:28:24 +00002113 bdrv_snapshot_load_tmp(bs[0],
2114 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2115 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2116 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002117 } else if (snapshot_name != NULL) {
edison51ef6722010-09-21 19:58:41 -07002118 if (bs_n > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002119 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002120 ret = -1;
2121 goto out;
2122 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002123
2124 bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002125 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002126 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002127 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002128 ret = -1;
2129 goto out;
edison51ef6722010-09-21 19:58:41 -07002130 }
2131
Kevin Wolfefa84d42009-05-18 16:42:12 +02002132 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +00002133 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002134 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002135 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002136 ret = -1;
2137 goto out;
2138 }
balrog926c2d22007-10-31 01:11:44 +00002139
Max Reitzb65a5e12015-02-05 13:58:12 -05002140 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002141 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +01002142 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002143 ret = -1;
2144 goto out;
2145 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09002146
Max Reitz2e024cd2015-02-11 09:58:46 -05002147 if (!skip_create) {
2148 if (!drv->create_opts) {
2149 error_report("Format driver '%s' does not support image creation",
2150 drv->format_name);
2151 ret = -1;
2152 goto out;
2153 }
Max Reitzf75613c2014-12-02 18:32:46 +01002154
Max Reitz2e024cd2015-02-11 09:58:46 -05002155 if (!proto_drv->create_opts) {
2156 error_report("Protocol driver '%s' does not support image creation",
2157 proto_drv->format_name);
2158 ret = -1;
2159 goto out;
2160 }
Max Reitzf75613c2014-12-02 18:32:46 +01002161
Max Reitz2e024cd2015-02-11 09:58:46 -05002162 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2163 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002164
Max Reitz2e024cd2015-02-11 09:58:46 -05002165 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002166 if (options) {
2167 qemu_opts_do_parse(opts, options, NULL, &local_err);
2168 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002169 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002170 ret = -1;
2171 goto out;
2172 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002173 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002174
Markus Armbruster39101f22015-02-12 16:46:36 +01002175 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_sectors * 512,
2176 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002177 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2178 if (ret < 0) {
2179 goto out;
2180 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002181 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002182
Kevin Wolfa18953f2010-10-14 15:46:04 +02002183 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002184 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002185 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002186 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002187 }
2188
Kevin Wolfefa84d42009-05-18 16:42:12 +02002189 /* Check if compression is supported */
Jes Sorenseneec77d92010-12-07 17:44:34 +01002190 if (compress) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002191 bool encryption =
2192 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
2193 const char *preallocation =
2194 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002195
Pavel Butsykin35fadca2016-07-22 11:17:48 +03002196 if (!drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002197 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002198 ret = -1;
2199 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002200 }
2201
Chunyan Liu83d05212014-06-05 17:20:51 +08002202 if (encryption) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002203 error_report("Compression and encryption not supported at "
2204 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002205 ret = -1;
2206 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002207 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002208
Chunyan Liu83d05212014-06-05 17:20:51 +08002209 if (preallocation
2210 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002211 {
2212 error_report("Compression and preallocation not supported at "
2213 "the same time");
2214 ret = -1;
2215 goto out;
2216 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002217 }
2218
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002219 if (!skip_create) {
2220 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002221 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002222 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002223 error_reportf_err(local_err, "%s: error while converting %s: ",
2224 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002225 goto out;
bellardea2384d2004-08-01 21:59:26 +00002226 }
2227 }
ths3b46e622007-09-17 08:09:54 +00002228
Peter Lieven5a37b602013-10-24 12:07:06 +02002229 flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002230 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002231 if (ret < 0) {
2232 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002233 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002234 }
2235
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002236 /* XXX we should allow --image-opts to trigger use of
2237 * img_open() here, but then we have trouble with
2238 * the bdrv_create() call which takes different params.
2239 * Not critical right now, so fix can wait...
2240 */
Kevin Wolfce099542016-03-15 13:01:04 +01002241 out_blk = img_open_file(out_filename, out_fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002242 if (!out_blk) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002243 ret = -1;
2244 goto out;
2245 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002246 out_bs = blk_bs(out_blk);
bellardea2384d2004-08-01 21:59:26 +00002247
Eric Blake5def6b82016-06-23 16:37:19 -06002248 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002249 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2250 * as maximum. */
2251 bufsectors = MIN(32768,
Eric Blake5def6b82016-06-23 16:37:19 -06002252 MAX(bufsectors,
2253 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
Eric Blakeb9f78552016-06-23 16:37:21 -06002254 out_bs->bl.pdiscard_alignment >>
2255 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002256
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002257 if (skip_create) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05002258 int64_t output_sectors = blk_nb_sectors(out_blk);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002259 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002260 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002261 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002262 ret = -1;
2263 goto out;
Markus Armbruster43716fa2014-06-26 13:23:21 +02002264 } else if (output_sectors < total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002265 error_report("output file is smaller than input file");
2266 ret = -1;
2267 goto out;
2268 }
2269 }
2270
Peter Lieven24f833c2013-11-27 11:07:07 +01002271 cluster_sectors = 0;
2272 ret = bdrv_get_info(out_bs, &bdi);
2273 if (ret < 0) {
2274 if (compress) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002275 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002276 goto out;
2277 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002278 } else {
Fam Zheng85f49ca2014-05-06 21:08:43 +08002279 compress = compress || bdi.needs_compressed_writes;
Peter Lieven24f833c2013-11-27 11:07:07 +01002280 cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
2281 }
2282
Kevin Wolf690c7302015-03-19 13:33:32 +01002283 state = (ImgConvertState) {
2284 .src = blk,
2285 .src_sectors = bs_sectors,
2286 .src_num = bs_n,
2287 .total_sectors = total_sectors,
2288 .target = out_blk,
2289 .compressed = compress,
2290 .target_has_backing = (bool) out_baseimg,
2291 .min_sparse = min_sparse,
2292 .cluster_sectors = cluster_sectors,
2293 .buf_sectors = bufsectors,
Peter Lieven2d9187b2017-02-28 13:40:07 +01002294 .wr_in_order = wr_in_order,
2295 .num_coroutines = num_coroutines,
Kevin Wolf690c7302015-03-19 13:33:32 +01002296 };
2297 ret = convert_do_copy(&state);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002298
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002299out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002300 if (!ret) {
2301 qemu_progress_print(100, 0);
2302 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002303 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002304 qemu_opts_del(opts);
2305 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002306 qemu_opts_del(sn_opts);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002307 blk_unref(out_blk);
Markus Armbruster9ba10c92014-10-07 13:59:08 +02002308 g_free(bs);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002309 if (blk) {
2310 for (bs_i = 0; bs_i < bs_n; bs_i++) {
2311 blk_unref(blk[bs_i]);
2312 }
2313 g_free(blk);
2314 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02002315 g_free(bs_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002316fail_getopt:
2317 g_free(options);
2318
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002319 if (ret) {
2320 return 1;
2321 }
bellardea2384d2004-08-01 21:59:26 +00002322 return 0;
2323}
2324
bellard57d1a2b2004-08-03 21:15:11 +00002325
bellardfaea38e2006-08-05 21:31:00 +00002326static void dump_snapshots(BlockDriverState *bs)
2327{
2328 QEMUSnapshotInfo *sn_tab, *sn;
2329 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002330
2331 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2332 if (nb_sns <= 0)
2333 return;
2334 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002335 bdrv_snapshot_dump(fprintf, stdout, NULL);
2336 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002337 for(i = 0; i < nb_sns; i++) {
2338 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002339 bdrv_snapshot_dump(fprintf, stdout, sn);
2340 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002341 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002342 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002343}
2344
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002345static void dump_json_image_info_list(ImageInfoList *list)
2346{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002347 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002348 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002349 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002350
2351 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2352 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002353 str = qobject_to_json_pretty(obj);
2354 assert(str != NULL);
2355 printf("%s\n", qstring_get_str(str));
2356 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002357 visit_free(v);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002358 QDECREF(str);
2359}
2360
Benoît Canetc054b3f2012-09-05 13:09:02 +02002361static void dump_json_image_info(ImageInfo *info)
2362{
Benoît Canetc054b3f2012-09-05 13:09:02 +02002363 QString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002364 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002365 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002366
2367 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2368 visit_complete(v, &obj);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002369 str = qobject_to_json_pretty(obj);
2370 assert(str != NULL);
2371 printf("%s\n", qstring_get_str(str));
2372 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002373 visit_free(v);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002374 QDECREF(str);
2375}
2376
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002377static void dump_human_image_info_list(ImageInfoList *list)
2378{
2379 ImageInfoList *elem;
2380 bool delim = false;
2381
2382 for (elem = list; elem; elem = elem->next) {
2383 if (delim) {
2384 printf("\n");
2385 }
2386 delim = true;
2387
Wenchao Xia5b917042013-05-25 11:09:45 +08002388 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002389 }
2390}
2391
2392static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2393{
2394 return strcmp(a, b) == 0;
2395}
2396
2397/**
2398 * Open an image file chain and return an ImageInfoList
2399 *
2400 * @filename: topmost image filename
2401 * @fmt: topmost image format (may be NULL to autodetect)
2402 * @chain: true - enumerate entire backing file chain
2403 * false - only topmost image file
2404 *
2405 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2406 * image file. If there was an error a message will have been printed to
2407 * stderr.
2408 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002409static ImageInfoList *collect_image_info_list(bool image_opts,
2410 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002411 const char *fmt,
2412 bool chain)
2413{
2414 ImageInfoList *head = NULL;
2415 ImageInfoList **last = &head;
2416 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002417 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002418
2419 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2420
2421 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002422 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002423 BlockDriverState *bs;
2424 ImageInfo *info;
2425 ImageInfoList *elem;
2426
2427 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2428 error_report("Backing file '%s' creates an infinite loop.",
2429 filename);
2430 goto err;
2431 }
2432 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2433
Max Reitzefaa7c42016-03-16 19:54:38 +01002434 blk = img_open(image_opts, filename, fmt,
Kevin Wolfce099542016-03-15 13:01:04 +01002435 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002436 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002437 goto err;
2438 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002439 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002440
Wenchao Xia43526ec2013-06-06 12:27:58 +08002441 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002442 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002443 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002444 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002445 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002446 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002447
2448 elem = g_new0(ImageInfoList, 1);
2449 elem->value = info;
2450 *last = elem;
2451 last = &elem->next;
2452
Markus Armbruster26f54e92014-10-07 13:59:04 +02002453 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002454
2455 filename = fmt = NULL;
2456 if (chain) {
2457 if (info->has_full_backing_filename) {
2458 filename = info->full_backing_filename;
2459 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002460 error_report("Could not determine absolute backing filename,"
2461 " but backing filename '%s' present",
2462 info->backing_filename);
2463 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002464 }
2465 if (info->has_backing_filename_format) {
2466 fmt = info->backing_filename_format;
2467 }
2468 }
2469 }
2470 g_hash_table_destroy(filenames);
2471 return head;
2472
2473err:
2474 qapi_free_ImageInfoList(head);
2475 g_hash_table_destroy(filenames);
2476 return NULL;
2477}
2478
Benoît Canetc054b3f2012-09-05 13:09:02 +02002479static int img_info(int argc, char **argv)
2480{
2481 int c;
2482 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002483 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002484 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002485 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002486 bool image_opts = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002487
bellardea2384d2004-08-01 21:59:26 +00002488 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002489 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002490 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002491 int option_index = 0;
2492 static const struct option long_options[] = {
2493 {"help", no_argument, 0, 'h'},
2494 {"format", required_argument, 0, 'f'},
2495 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002496 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002497 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002498 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002499 {0, 0, 0, 0}
2500 };
2501 c = getopt_long(argc, argv, "f:h",
2502 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002503 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002504 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002505 }
bellardea2384d2004-08-01 21:59:26 +00002506 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002507 case '?':
bellardea2384d2004-08-01 21:59:26 +00002508 case 'h':
2509 help();
2510 break;
2511 case 'f':
2512 fmt = optarg;
2513 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002514 case OPTION_OUTPUT:
2515 output = optarg;
2516 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002517 case OPTION_BACKING_CHAIN:
2518 chain = true;
2519 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002520 case OPTION_OBJECT: {
2521 QemuOpts *opts;
2522 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2523 optarg, true);
2524 if (!opts) {
2525 return 1;
2526 }
2527 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002528 case OPTION_IMAGE_OPTS:
2529 image_opts = true;
2530 break;
bellardea2384d2004-08-01 21:59:26 +00002531 }
2532 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002533 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002534 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002535 }
bellardea2384d2004-08-01 21:59:26 +00002536 filename = argv[optind++];
2537
Benoît Canetc054b3f2012-09-05 13:09:02 +02002538 if (output && !strcmp(output, "json")) {
2539 output_format = OFORMAT_JSON;
2540 } else if (output && !strcmp(output, "human")) {
2541 output_format = OFORMAT_HUMAN;
2542 } else if (output) {
2543 error_report("--output must be used with human or json as argument.");
2544 return 1;
2545 }
2546
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002547 if (qemu_opts_foreach(&qemu_object_opts,
2548 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002549 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002550 return 1;
2551 }
2552
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002553 list = collect_image_info_list(image_opts, filename, fmt, chain);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002554 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002555 return 1;
2556 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002557
Benoît Canetc054b3f2012-09-05 13:09:02 +02002558 switch (output_format) {
2559 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002560 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002561 break;
2562 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002563 if (chain) {
2564 dump_json_image_info_list(list);
2565 } else {
2566 dump_json_image_info(list->value);
2567 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002568 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002569 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002570
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002571 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002572 return 0;
2573}
2574
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002575static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2576 MapEntry *next)
2577{
2578 switch (output_format) {
2579 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002580 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002581 error_report("File contains external, encrypted or compressed clusters.");
2582 exit(1);
2583 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002584 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002585 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002586 e->start, e->length,
2587 e->has_offset ? e->offset : 0,
2588 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002589 }
2590 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2591 * Modify the flags here to allow more coalescing.
2592 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002593 if (next && (!next->data || next->zero)) {
2594 next->data = false;
2595 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002596 }
2597 break;
2598 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002599 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2600 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002601 (e->start == 0 ? "[" : ",\n"),
2602 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002603 e->zero ? "true" : "false",
2604 e->data ? "true" : "false");
2605 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002606 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002607 }
2608 putchar('}');
2609
2610 if (!next) {
2611 printf("]\n");
2612 }
2613 break;
2614 }
2615}
2616
2617static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2618 int nb_sectors, MapEntry *e)
2619{
2620 int64_t ret;
2621 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002622 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002623 bool has_offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002624
2625 /* As an optimization, we could cache the current range of unallocated
2626 * clusters in each file of the chain, and avoid querying the same
2627 * range repeatedly.
2628 */
2629
2630 depth = 0;
2631 for (;;) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08002632 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2633 &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002634 if (ret < 0) {
2635 return ret;
2636 }
2637 assert(nb_sectors);
2638 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2639 break;
2640 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002641 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002642 if (bs == NULL) {
2643 ret = 0;
2644 break;
2645 }
2646
2647 depth++;
2648 }
2649
John Snow28756452016-02-05 13:12:33 -05002650 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2651
2652 *e = (MapEntry) {
2653 .start = sector_num * BDRV_SECTOR_SIZE,
2654 .length = nb_sectors * BDRV_SECTOR_SIZE,
2655 .data = !!(ret & BDRV_BLOCK_DATA),
2656 .zero = !!(ret & BDRV_BLOCK_ZERO),
2657 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2658 .has_offset = has_offset,
2659 .depth = depth,
2660 .has_filename = file && has_offset,
2661 .filename = file && has_offset ? file->filename : NULL,
2662 };
2663
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002664 return 0;
2665}
2666
Fam Zheng16b0d552016-01-26 11:59:02 +08002667static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2668{
2669 if (curr->length == 0) {
2670 return false;
2671 }
2672 if (curr->zero != next->zero ||
2673 curr->data != next->data ||
2674 curr->depth != next->depth ||
2675 curr->has_filename != next->has_filename ||
2676 curr->has_offset != next->has_offset) {
2677 return false;
2678 }
2679 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2680 return false;
2681 }
2682 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2683 return false;
2684 }
2685 return true;
2686}
2687
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002688static int img_map(int argc, char **argv)
2689{
2690 int c;
2691 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002692 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002693 BlockDriverState *bs;
2694 const char *filename, *fmt, *output;
2695 int64_t length;
2696 MapEntry curr = { .length = 0 }, next;
2697 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002698 bool image_opts = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002699
2700 fmt = NULL;
2701 output = NULL;
2702 for (;;) {
2703 int option_index = 0;
2704 static const struct option long_options[] = {
2705 {"help", no_argument, 0, 'h'},
2706 {"format", required_argument, 0, 'f'},
2707 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002708 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002709 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002710 {0, 0, 0, 0}
2711 };
2712 c = getopt_long(argc, argv, "f:h",
2713 long_options, &option_index);
2714 if (c == -1) {
2715 break;
2716 }
2717 switch (c) {
2718 case '?':
2719 case 'h':
2720 help();
2721 break;
2722 case 'f':
2723 fmt = optarg;
2724 break;
2725 case OPTION_OUTPUT:
2726 output = optarg;
2727 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002728 case OPTION_OBJECT: {
2729 QemuOpts *opts;
2730 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2731 optarg, true);
2732 if (!opts) {
2733 return 1;
2734 }
2735 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002736 case OPTION_IMAGE_OPTS:
2737 image_opts = true;
2738 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002739 }
2740 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002741 if (optind != argc - 1) {
2742 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002743 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002744 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002745
2746 if (output && !strcmp(output, "json")) {
2747 output_format = OFORMAT_JSON;
2748 } else if (output && !strcmp(output, "human")) {
2749 output_format = OFORMAT_HUMAN;
2750 } else if (output) {
2751 error_report("--output must be used with human or json as argument.");
2752 return 1;
2753 }
2754
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002755 if (qemu_opts_foreach(&qemu_object_opts,
2756 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002757 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002758 return 1;
2759 }
2760
Kevin Wolfce099542016-03-15 13:01:04 +01002761 blk = img_open(image_opts, filename, fmt, 0, false, false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002762 if (!blk) {
2763 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002764 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002765 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002766
2767 if (output_format == OFORMAT_HUMAN) {
2768 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2769 }
2770
Max Reitzf1d3cd72015-02-05 13:58:18 -05002771 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002772 while (curr.start + curr.length < length) {
2773 int64_t nsectors_left;
2774 int64_t sector_num;
2775 int n;
2776
2777 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2778
2779 /* Probe up to 1 GiB at a time. */
2780 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2781 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2782 ret = get_block_status(bs, sector_num, n, &next);
2783
2784 if (ret < 0) {
2785 error_report("Could not read file metadata: %s", strerror(-ret));
2786 goto out;
2787 }
2788
Fam Zheng16b0d552016-01-26 11:59:02 +08002789 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002790 curr.length += next.length;
2791 continue;
2792 }
2793
2794 if (curr.length > 0) {
2795 dump_map_entry(output_format, &curr, &next);
2796 }
2797 curr = next;
2798 }
2799
2800 dump_map_entry(output_format, &curr, NULL);
2801
2802out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002803 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002804 return ret < 0;
2805}
2806
aliguorif7b4a942009-01-07 17:40:15 +00002807#define SNAPSHOT_LIST 1
2808#define SNAPSHOT_CREATE 2
2809#define SNAPSHOT_APPLY 3
2810#define SNAPSHOT_DELETE 4
2811
Stuart Brady153859b2009-06-07 00:42:17 +01002812static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002813{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002814 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002815 BlockDriverState *bs;
2816 QEMUSnapshotInfo sn;
2817 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002818 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002819 int action = 0;
2820 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002821 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002822 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002823 bool image_opts = false;
aliguorif7b4a942009-01-07 17:40:15 +00002824
Kevin Wolfce099542016-03-15 13:01:04 +01002825 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002826 /* Parse commandline parameters */
2827 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002828 static const struct option long_options[] = {
2829 {"help", no_argument, 0, 'h'},
2830 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002831 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002832 {0, 0, 0, 0}
2833 };
2834 c = getopt_long(argc, argv, "la:c:d:hq",
2835 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002836 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002837 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002838 }
aliguorif7b4a942009-01-07 17:40:15 +00002839 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002840 case '?':
aliguorif7b4a942009-01-07 17:40:15 +00002841 case 'h':
2842 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002843 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002844 case 'l':
2845 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002846 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002847 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002848 }
2849 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002850 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002851 break;
2852 case 'a':
2853 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002854 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002855 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002856 }
2857 action = SNAPSHOT_APPLY;
2858 snapshot_name = optarg;
2859 break;
2860 case 'c':
2861 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002862 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002863 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002864 }
2865 action = SNAPSHOT_CREATE;
2866 snapshot_name = optarg;
2867 break;
2868 case 'd':
2869 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002870 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002871 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002872 }
2873 action = SNAPSHOT_DELETE;
2874 snapshot_name = optarg;
2875 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002876 case 'q':
2877 quiet = true;
2878 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002879 case OPTION_OBJECT: {
2880 QemuOpts *opts;
2881 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2882 optarg, true);
2883 if (!opts) {
2884 return 1;
2885 }
2886 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002887 case OPTION_IMAGE_OPTS:
2888 image_opts = true;
2889 break;
aliguorif7b4a942009-01-07 17:40:15 +00002890 }
2891 }
2892
Kevin Wolffc11eb22013-08-05 10:53:04 +02002893 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002894 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002895 }
aliguorif7b4a942009-01-07 17:40:15 +00002896 filename = argv[optind++];
2897
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002898 if (qemu_opts_foreach(&qemu_object_opts,
2899 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002900 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002901 return 1;
2902 }
2903
aliguorif7b4a942009-01-07 17:40:15 +00002904 /* Open the image */
Kevin Wolfce099542016-03-15 13:01:04 +01002905 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002906 if (!blk) {
2907 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002908 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002909 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002910
2911 /* Perform the requested action */
2912 switch(action) {
2913 case SNAPSHOT_LIST:
2914 dump_snapshots(bs);
2915 break;
2916
2917 case SNAPSHOT_CREATE:
2918 memset(&sn, 0, sizeof(sn));
2919 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2920
2921 qemu_gettimeofday(&tv);
2922 sn.date_sec = tv.tv_sec;
2923 sn.date_nsec = tv.tv_usec * 1000;
2924
2925 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002926 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002927 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002928 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002929 }
aliguorif7b4a942009-01-07 17:40:15 +00002930 break;
2931
2932 case SNAPSHOT_APPLY:
2933 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002934 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002935 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002936 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002937 }
aliguorif7b4a942009-01-07 17:40:15 +00002938 break;
2939
2940 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002941 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002942 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002943 error_reportf_err(err, "Could not delete snapshot '%s': ",
2944 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002945 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002946 }
aliguorif7b4a942009-01-07 17:40:15 +00002947 break;
2948 }
2949
2950 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02002951 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002952 if (ret) {
2953 return 1;
2954 }
Stuart Brady153859b2009-06-07 00:42:17 +01002955 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002956}
2957
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002958static int img_rebase(int argc, char **argv)
2959{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002960 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01002961 uint8_t *buf_old = NULL;
2962 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05002963 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002964 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02002965 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
2966 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01002967 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002968 int unsafe = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002969 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002970 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02002971 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002972 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002973
2974 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002975 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002976 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02002977 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002978 out_baseimg = NULL;
2979 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002980 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002981 static const struct option long_options[] = {
2982 {"help", no_argument, 0, 'h'},
2983 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002984 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002985 {0, 0, 0, 0}
2986 };
2987 c = getopt_long(argc, argv, "hf:F:b:upt:T:q",
2988 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002989 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002990 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002991 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002992 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002993 case '?':
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002994 case 'h':
2995 help();
2996 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002997 case 'f':
2998 fmt = optarg;
2999 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003000 case 'F':
3001 out_basefmt = optarg;
3002 break;
3003 case 'b':
3004 out_baseimg = optarg;
3005 break;
3006 case 'u':
3007 unsafe = 1;
3008 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003009 case 'p':
3010 progress = 1;
3011 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003012 case 't':
3013 cache = optarg;
3014 break;
Max Reitz40055952014-07-22 22:58:42 +02003015 case 'T':
3016 src_cache = optarg;
3017 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003018 case 'q':
3019 quiet = true;
3020 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003021 case OPTION_OBJECT: {
3022 QemuOpts *opts;
3023 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3024 optarg, true);
3025 if (!opts) {
3026 return 1;
3027 }
3028 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003029 case OPTION_IMAGE_OPTS:
3030 image_opts = true;
3031 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003032 }
3033 }
3034
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003035 if (quiet) {
3036 progress = 0;
3037 }
3038
Fam Zhengac1307a2014-04-22 13:36:11 +08003039 if (optind != argc - 1) {
3040 error_exit("Expecting one image file name");
3041 }
3042 if (!unsafe && !out_baseimg) {
3043 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003044 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003045 filename = argv[optind++];
3046
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003047 if (qemu_opts_foreach(&qemu_object_opts,
3048 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003049 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003050 return 1;
3051 }
3052
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003053 qemu_progress_init(progress, 2.0);
3054 qemu_progress_print(0, 100);
3055
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003056 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003057 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003058 if (ret < 0) {
3059 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003060 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003061 }
3062
Kevin Wolfce099542016-03-15 13:01:04 +01003063 src_flags = 0;
3064 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003065 if (ret < 0) {
3066 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003067 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003068 }
3069
Kevin Wolfce099542016-03-15 13:01:04 +01003070 /* The source files are opened read-only, don't care about WCE */
3071 assert((src_flags & BDRV_O_RDWR) == 0);
3072 (void) src_writethrough;
3073
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003074 /*
3075 * Open the images.
3076 *
3077 * Ignore the old backing file for unsafe rebase in case we want to correct
3078 * the reference to a renamed or moved backing file.
3079 */
Kevin Wolfce099542016-03-15 13:01:04 +01003080 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003081 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003082 ret = -1;
3083 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003084 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003085 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003086
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003087 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003088 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003089 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003090 ret = -1;
3091 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003092 }
3093 }
3094
3095 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003096 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003097 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003098 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003099
Max Reitz644483d2015-02-05 13:58:17 -05003100 if (bs->backing_format[0] != '\0') {
3101 options = qdict_new();
3102 qdict_put(options, "driver", qstring_from_str(bs->backing_format));
3103 }
3104
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003105 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003106 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003107 options, src_flags, &local_err);
3108 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003109 error_reportf_err(local_err,
3110 "Could not open old backing file '%s': ",
3111 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003112 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003113 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003114 }
Max Reitz644483d2015-02-05 13:58:17 -05003115
Alex Bligha6166732012-10-16 13:46:18 +01003116 if (out_baseimg[0]) {
Max Reitz644483d2015-02-05 13:58:17 -05003117 if (out_basefmt) {
3118 options = qdict_new();
3119 qdict_put(options, "driver", qstring_from_str(out_basefmt));
3120 } else {
3121 options = NULL;
3122 }
3123
Max Reitzefaa7c42016-03-16 19:54:38 +01003124 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003125 options, src_flags, &local_err);
3126 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003127 error_reportf_err(local_err,
3128 "Could not open new backing file '%s': ",
3129 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003130 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003131 goto out;
3132 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003133 }
3134 }
3135
3136 /*
3137 * Check each unallocated cluster in the COW file. If it is unallocated,
3138 * accesses go to the backing file. We must therefore compare this cluster
3139 * in the old and new backing file, and if they differ we need to copy it
3140 * from the old backing file into the COW file.
3141 *
3142 * If qemu-img crashes during this step, no harm is done. The content of
3143 * the image is the same as the original one at any time.
3144 */
3145 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003146 int64_t num_sectors;
3147 int64_t old_backing_num_sectors;
3148 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003149 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003150 int n;
Kevin Wolf1f710492012-10-12 14:29:18 +02003151 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003152
Max Reitzf1d3cd72015-02-05 13:58:18 -05003153 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3154 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003155
Max Reitzf1d3cd72015-02-05 13:58:18 -05003156 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003157 if (num_sectors < 0) {
3158 error_report("Could not get size of '%s': %s",
3159 filename, strerror(-num_sectors));
3160 ret = -1;
3161 goto out;
3162 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003163 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003164 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003165 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003166
3167 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3168 error_report("Could not get size of '%s': %s",
3169 backing_name, strerror(-old_backing_num_sectors));
3170 ret = -1;
3171 goto out;
3172 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003173 if (blk_new_backing) {
3174 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003175 if (new_backing_num_sectors < 0) {
3176 error_report("Could not get size of '%s': %s",
3177 out_baseimg, strerror(-new_backing_num_sectors));
3178 ret = -1;
3179 goto out;
3180 }
Alex Bligha6166732012-10-16 13:46:18 +01003181 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003182
Kevin Wolf1f710492012-10-12 14:29:18 +02003183 if (num_sectors != 0) {
3184 local_progress = (float)100 /
3185 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
3186 }
3187
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003188 for (sector = 0; sector < num_sectors; sector += n) {
3189
3190 /* How many sectors can we handle with the next read? */
3191 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
3192 n = (IO_BUF_SIZE / 512);
3193 } else {
3194 n = num_sectors - sector;
3195 }
3196
3197 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02003198 ret = bdrv_is_allocated(bs, sector, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003199 if (ret < 0) {
3200 error_report("error while reading image metadata: %s",
3201 strerror(-ret));
3202 goto out;
3203 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003204 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003205 continue;
3206 }
3207
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003208 /*
3209 * Read old and new backing file and take into consideration that
3210 * backing files may be smaller than the COW image.
3211 */
3212 if (sector >= old_backing_num_sectors) {
3213 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3214 } else {
3215 if (sector + n > old_backing_num_sectors) {
3216 n = old_backing_num_sectors - sector;
3217 }
3218
Eric Blake91669202016-05-06 10:26:43 -06003219 ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS,
3220 buf_old, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003221 if (ret < 0) {
3222 error_report("error while reading from old backing file");
3223 goto out;
3224 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003225 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003226
Max Reitzf1d3cd72015-02-05 13:58:18 -05003227 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003228 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3229 } else {
3230 if (sector + n > new_backing_num_sectors) {
3231 n = new_backing_num_sectors - sector;
3232 }
3233
Eric Blake91669202016-05-06 10:26:43 -06003234 ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS,
3235 buf_new, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003236 if (ret < 0) {
3237 error_report("error while reading from new backing file");
3238 goto out;
3239 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003240 }
3241
3242 /* If they differ, we need to write to the COW file */
3243 uint64_t written = 0;
3244
3245 while (written < n) {
3246 int pnum;
3247
3248 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003249 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003250 {
Eric Blake91669202016-05-06 10:26:43 -06003251 ret = blk_pwrite(blk,
3252 (sector + written) << BDRV_SECTOR_BITS,
3253 buf_old + written * 512,
3254 pnum << BDRV_SECTOR_BITS, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003255 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003256 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003257 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003258 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003259 }
3260 }
3261
3262 written += pnum;
3263 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003264 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003265 }
3266 }
3267
3268 /*
3269 * Change the backing file. All clusters that are different from the old
3270 * backing file are overwritten in the COW file now, so the visible content
3271 * doesn't change when we switch the backing file.
3272 */
Alex Bligha6166732012-10-16 13:46:18 +01003273 if (out_baseimg && *out_baseimg) {
3274 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3275 } else {
3276 ret = bdrv_change_backing_file(bs, NULL, NULL);
3277 }
3278
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003279 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003280 error_report("Could not change the backing file to '%s': No "
3281 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003282 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003283 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003284 out_baseimg, strerror(-ret));
3285 }
3286
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003287 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003288 /*
3289 * TODO At this point it is possible to check if any clusters that are
3290 * allocated in the COW file are the same in the backing file. If so, they
3291 * could be dropped from the COW file. Don't do this before switching the
3292 * backing file, in case of a crash this would lead to corruption.
3293 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003294out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003295 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003296 /* Cleanup */
3297 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003298 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003299 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003300 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003301 qemu_vfree(buf_old);
3302 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003303
Markus Armbruster26f54e92014-10-07 13:59:04 +02003304 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003305 if (ret) {
3306 return 1;
3307 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003308 return 0;
3309}
3310
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003311static int img_resize(int argc, char **argv)
3312{
Markus Armbruster6750e792015-02-12 17:43:08 +01003313 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003314 int c, ret, relative;
3315 const char *filename, *fmt, *size;
3316 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003317 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003318 BlockBackend *blk = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003319 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003320
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003321 static QemuOptsList resize_options = {
3322 .name = "resize_options",
3323 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3324 .desc = {
3325 {
3326 .name = BLOCK_OPT_SIZE,
3327 .type = QEMU_OPT_SIZE,
3328 .help = "Virtual disk size"
3329 }, {
3330 /* end of list */
3331 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003332 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003333 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003334 bool image_opts = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003335
Kevin Wolfe80fec72011-04-29 10:58:12 +02003336 /* Remove size from argv manually so that negative numbers are not treated
3337 * as options by getopt. */
3338 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003339 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003340 return 1;
3341 }
3342
3343 size = argv[--argc];
3344
3345 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003346 fmt = NULL;
3347 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003348 static const struct option long_options[] = {
3349 {"help", no_argument, 0, 'h'},
3350 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003351 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003352 {0, 0, 0, 0}
3353 };
3354 c = getopt_long(argc, argv, "f:hq",
3355 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003356 if (c == -1) {
3357 break;
3358 }
3359 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01003360 case '?':
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003361 case 'h':
3362 help();
3363 break;
3364 case 'f':
3365 fmt = optarg;
3366 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003367 case 'q':
3368 quiet = true;
3369 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003370 case OPTION_OBJECT: {
3371 QemuOpts *opts;
3372 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3373 optarg, true);
3374 if (!opts) {
3375 return 1;
3376 }
3377 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003378 case OPTION_IMAGE_OPTS:
3379 image_opts = true;
3380 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003381 }
3382 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003383 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003384 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003385 }
3386 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003387
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003388 if (qemu_opts_foreach(&qemu_object_opts,
3389 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003390 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003391 return 1;
3392 }
3393
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003394 /* Choose grow, shrink, or absolute resize mode */
3395 switch (size[0]) {
3396 case '+':
3397 relative = 1;
3398 size++;
3399 break;
3400 case '-':
3401 relative = -1;
3402 size++;
3403 break;
3404 default:
3405 relative = 0;
3406 break;
3407 }
3408
3409 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003410 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003411 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003412 if (err) {
3413 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003414 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003415 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003416 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003417 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003418 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3419 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003420
Max Reitzefaa7c42016-03-16 19:54:38 +01003421 blk = img_open(image_opts, filename, fmt,
Kevin Wolf55880602017-02-17 15:07:38 +01003422 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003423 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003424 ret = -1;
3425 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003426 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003427
3428 if (relative) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003429 total_size = blk_getlength(blk) + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003430 } else {
3431 total_size = n;
3432 }
3433 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003434 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003435 ret = -1;
3436 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003437 }
3438
Max Reitzf1d3cd72015-02-05 13:58:18 -05003439 ret = blk_truncate(blk, total_size);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003440 switch (ret) {
3441 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003442 qprintf(quiet, "Image resized.\n");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003443 break;
3444 case -ENOTSUP:
Kevin Wolf259b2172012-03-06 12:44:45 +01003445 error_report("This image does not support resize");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003446 break;
3447 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +01003448 error_report("Image is read-only");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003449 break;
3450 default:
Max Reitzbcf23482016-06-15 17:36:29 +02003451 error_report("Error resizing image: %s", strerror(-ret));
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003452 break;
3453 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003454out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003455 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003456 if (ret) {
3457 return 1;
3458 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003459 return 0;
3460}
3461
Max Reitz76a3a342014-10-27 11:12:51 +01003462static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003463 int64_t offset, int64_t total_work_size,
3464 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003465{
3466 qemu_progress_print(100.f * offset / total_work_size, 0);
3467}
3468
Max Reitz6f176b42013-09-03 10:09:50 +02003469static int img_amend(int argc, char **argv)
3470{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003471 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003472 int c, ret = 0;
3473 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003474 QemuOptsList *create_opts = NULL;
3475 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003476 const char *fmt = NULL, *filename, *cache;
3477 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003478 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003479 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003480 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003481 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003482 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003483
Max Reitzbd39e6e2014-07-22 22:58:43 +02003484 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003485 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003486 static const struct option long_options[] = {
3487 {"help", no_argument, 0, 'h'},
3488 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003489 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003490 {0, 0, 0, 0}
3491 };
3492 c = getopt_long(argc, argv, "ho:f:t:pq",
3493 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003494 if (c == -1) {
3495 break;
3496 }
3497
3498 switch (c) {
3499 case 'h':
3500 case '?':
3501 help();
3502 break;
3503 case 'o':
Kevin Wolf626f84f2014-02-21 16:24:06 +01003504 if (!is_valid_option_list(optarg)) {
3505 error_report("Invalid option list: %s", optarg);
3506 ret = -1;
Max Reitze814dff2015-08-20 16:00:38 -07003507 goto out_no_progress;
Kevin Wolf626f84f2014-02-21 16:24:06 +01003508 }
3509 if (!options) {
3510 options = g_strdup(optarg);
3511 } else {
3512 char *old_options = options;
3513 options = g_strdup_printf("%s,%s", options, optarg);
3514 g_free(old_options);
3515 }
Max Reitz6f176b42013-09-03 10:09:50 +02003516 break;
3517 case 'f':
3518 fmt = optarg;
3519 break;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003520 case 't':
3521 cache = optarg;
3522 break;
Max Reitz76a3a342014-10-27 11:12:51 +01003523 case 'p':
3524 progress = true;
3525 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003526 case 'q':
3527 quiet = true;
3528 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003529 case OPTION_OBJECT:
3530 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3531 optarg, true);
3532 if (!opts) {
3533 ret = -1;
3534 goto out_no_progress;
3535 }
3536 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003537 case OPTION_IMAGE_OPTS:
3538 image_opts = true;
3539 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003540 }
3541 }
3542
Max Reitz6f176b42013-09-03 10:09:50 +02003543 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003544 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003545 }
3546
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003547 if (qemu_opts_foreach(&qemu_object_opts,
3548 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003549 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003550 ret = -1;
3551 goto out_no_progress;
3552 }
3553
Max Reitz76a3a342014-10-27 11:12:51 +01003554 if (quiet) {
3555 progress = false;
3556 }
3557 qemu_progress_init(progress, 1.0);
3558
Kevin Wolfa283cb62014-02-21 16:24:07 +01003559 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3560 if (fmt && has_help_option(options)) {
3561 /* If a format is explicitly specified (and possibly no filename is
3562 * given), print option help here */
3563 ret = print_block_option_help(filename, fmt);
3564 goto out;
3565 }
3566
3567 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003568 error_report("Expecting one image file name");
3569 ret = -1;
3570 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003571 }
Max Reitz6f176b42013-09-03 10:09:50 +02003572
Kevin Wolfce099542016-03-15 13:01:04 +01003573 flags = BDRV_O_RDWR;
3574 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003575 if (ret < 0) {
3576 error_report("Invalid cache option: %s", cache);
3577 goto out;
3578 }
3579
Kevin Wolfce099542016-03-15 13:01:04 +01003580 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003581 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003582 ret = -1;
3583 goto out;
3584 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003585 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003586
3587 fmt = bs->drv->format_name;
3588
Kevin Wolf626f84f2014-02-21 16:24:06 +01003589 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003590 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003591 ret = print_block_option_help(filename, fmt);
3592 goto out;
3593 }
3594
Max Reitzb2439d22014-12-02 18:32:47 +01003595 if (!bs->drv->create_opts) {
3596 error_report("Format driver '%s' does not support any options to amend",
3597 fmt);
3598 ret = -1;
3599 goto out;
3600 }
3601
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003602 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003603 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003604 qemu_opts_do_parse(opts, options, NULL, &err);
3605 if (err) {
3606 error_report_err(err);
3607 ret = -1;
3608 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003609 }
3610
Max Reitz76a3a342014-10-27 11:12:51 +01003611 /* In case the driver does not call amend_status_cb() */
3612 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003613 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003614 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003615 if (ret < 0) {
3616 error_report("Error while amending options: %s", strerror(-ret));
3617 goto out;
3618 }
3619
3620out:
Max Reitz76a3a342014-10-27 11:12:51 +01003621 qemu_progress_end();
3622
Max Reitze814dff2015-08-20 16:00:38 -07003623out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003624 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003625 qemu_opts_del(opts);
3626 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003627 g_free(options);
3628
Max Reitz6f176b42013-09-03 10:09:50 +02003629 if (ret) {
3630 return 1;
3631 }
3632 return 0;
3633}
3634
Kevin Wolfb6133b82014-08-05 14:17:13 +02003635typedef struct BenchData {
3636 BlockBackend *blk;
3637 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003638 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003639 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003640 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003641 int nrreq;
3642 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003643 int flush_interval;
3644 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003645 uint8_t *buf;
3646 QEMUIOVector *qiov;
3647
3648 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003649 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003650 uint64_t offset;
3651} BenchData;
3652
Kevin Wolf55d539c2016-06-03 13:59:41 +02003653static void bench_undrained_flush_cb(void *opaque, int ret)
3654{
3655 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003656 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003657 exit(EXIT_FAILURE);
3658 }
3659}
3660
Kevin Wolfb6133b82014-08-05 14:17:13 +02003661static void bench_cb(void *opaque, int ret)
3662{
3663 BenchData *b = opaque;
3664 BlockAIOCB *acb;
3665
3666 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003667 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003668 exit(EXIT_FAILURE);
3669 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003670
3671 if (b->in_flush) {
3672 /* Just finished a flush with drained queue: Start next requests */
3673 assert(b->in_flight == 0);
3674 b->in_flush = false;
3675 } else if (b->in_flight > 0) {
3676 int remaining = b->n - b->in_flight;
3677
Kevin Wolfb6133b82014-08-05 14:17:13 +02003678 b->n--;
3679 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003680
3681 /* Time for flush? Drain queue if requested, then flush */
3682 if (b->flush_interval && remaining % b->flush_interval == 0) {
3683 if (!b->in_flight || !b->drain_on_flush) {
3684 BlockCompletionFunc *cb;
3685
3686 if (b->drain_on_flush) {
3687 b->in_flush = true;
3688 cb = bench_cb;
3689 } else {
3690 cb = bench_undrained_flush_cb;
3691 }
3692
3693 acb = blk_aio_flush(b->blk, cb, b);
3694 if (!acb) {
3695 error_report("Failed to issue flush request");
3696 exit(EXIT_FAILURE);
3697 }
3698 }
3699 if (b->drain_on_flush) {
3700 return;
3701 }
3702 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003703 }
3704
3705 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003706 int64_t offset = b->offset;
3707 /* blk_aio_* might look for completed I/Os and kick bench_cb
3708 * again, so make sure this operation is counted by in_flight
3709 * and b->offset is ready for the next submission.
3710 */
3711 b->in_flight++;
3712 b->offset += b->step;
3713 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003714 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003715 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003716 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003717 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003718 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003719 if (!acb) {
3720 error_report("Failed to issue request");
3721 exit(EXIT_FAILURE);
3722 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003723 }
3724}
3725
3726static int img_bench(int argc, char **argv)
3727{
3728 int c, ret = 0;
3729 const char *fmt = NULL, *filename;
3730 bool quiet = false;
3731 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003732 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003733 int count = 75000;
3734 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003735 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003736 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003737 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003738 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003739 int flush_interval = 0;
3740 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003741 int64_t image_size;
3742 BlockBackend *blk = NULL;
3743 BenchData data = {};
3744 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003745 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003746 struct timeval t1, t2;
3747 int i;
3748
3749 for (;;) {
3750 static const struct option long_options[] = {
3751 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003752 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003753 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003754 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003755 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003756 {0, 0, 0, 0}
3757 };
Kevin Wolf83de9be2015-07-13 13:13:17 +02003758 c = getopt_long(argc, argv, "hc:d:f:no:qs:S:t:w", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003759 if (c == -1) {
3760 break;
3761 }
3762
3763 switch (c) {
3764 case 'h':
3765 case '?':
3766 help();
3767 break;
3768 case 'c':
3769 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003770 unsigned long res;
3771
3772 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003773 error_report("Invalid request count specified");
3774 return 1;
3775 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003776 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003777 break;
3778 }
3779 case 'd':
3780 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003781 unsigned long res;
3782
3783 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003784 error_report("Invalid queue depth specified");
3785 return 1;
3786 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003787 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003788 break;
3789 }
3790 case 'f':
3791 fmt = optarg;
3792 break;
3793 case 'n':
3794 flags |= BDRV_O_NATIVE_AIO;
3795 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003796 case 'o':
3797 {
Markus Armbruster606caa02017-02-21 21:14:04 +01003798 offset = cvtnum(optarg);
3799 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02003800 error_report("Invalid offset specified");
3801 return 1;
3802 }
3803 break;
3804 }
3805 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003806 case 'q':
3807 quiet = true;
3808 break;
3809 case 's':
3810 {
3811 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003812
Markus Armbruster606caa02017-02-21 21:14:04 +01003813 sval = cvtnum(optarg);
3814 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003815 error_report("Invalid buffer size specified");
3816 return 1;
3817 }
3818
3819 bufsize = sval;
3820 break;
3821 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003822 case 'S':
3823 {
3824 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003825
Markus Armbruster606caa02017-02-21 21:14:04 +01003826 sval = cvtnum(optarg);
3827 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02003828 error_report("Invalid step size specified");
3829 return 1;
3830 }
3831
3832 step = sval;
3833 break;
3834 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003835 case 't':
3836 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3837 if (ret < 0) {
3838 error_report("Invalid cache mode");
3839 ret = -1;
3840 goto out;
3841 }
3842 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003843 case 'w':
3844 flags |= BDRV_O_RDWR;
3845 is_write = true;
3846 break;
3847 case OPTION_PATTERN:
3848 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003849 unsigned long res;
3850
3851 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003852 error_report("Invalid pattern byte specified");
3853 return 1;
3854 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003855 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003856 break;
3857 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003858 case OPTION_FLUSH_INTERVAL:
3859 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003860 unsigned long res;
3861
3862 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02003863 error_report("Invalid flush interval specified");
3864 return 1;
3865 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003866 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003867 break;
3868 }
3869 case OPTION_NO_DRAIN:
3870 drain_on_flush = false;
3871 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003872 case OPTION_IMAGE_OPTS:
3873 image_opts = true;
3874 break;
3875 }
3876 }
3877
3878 if (optind != argc - 1) {
3879 error_exit("Expecting one image file name");
3880 }
3881 filename = argv[argc - 1];
3882
Kevin Wolf55d539c2016-06-03 13:59:41 +02003883 if (!is_write && flush_interval) {
3884 error_report("--flush-interval is only available in write tests");
3885 ret = -1;
3886 goto out;
3887 }
3888 if (flush_interval && flush_interval < depth) {
3889 error_report("Flush interval can't be smaller than depth");
3890 ret = -1;
3891 goto out;
3892 }
3893
Kevin Wolfb6133b82014-08-05 14:17:13 +02003894 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet);
3895 if (!blk) {
3896 ret = -1;
3897 goto out;
3898 }
3899
3900 image_size = blk_getlength(blk);
3901 if (image_size < 0) {
3902 ret = image_size;
3903 goto out;
3904 }
3905
3906 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02003907 .blk = blk,
3908 .image_size = image_size,
3909 .bufsize = bufsize,
3910 .step = step ?: bufsize,
3911 .nrreq = depth,
3912 .n = count,
3913 .offset = offset,
3914 .write = is_write,
3915 .flush_interval = flush_interval,
3916 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02003917 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02003918 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02003919 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02003920 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02003921 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02003922 if (flush_interval) {
3923 printf("Sending flush every %d requests\n", flush_interval);
3924 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003925
3926 data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003927 memset(data.buf, pattern, data.nrreq * data.bufsize);
3928
Kevin Wolfb6133b82014-08-05 14:17:13 +02003929 data.qiov = g_new(QEMUIOVector, data.nrreq);
3930 for (i = 0; i < data.nrreq; i++) {
3931 qemu_iovec_init(&data.qiov[i], 1);
3932 qemu_iovec_add(&data.qiov[i],
3933 data.buf + i * data.bufsize, data.bufsize);
3934 }
3935
3936 gettimeofday(&t1, NULL);
3937 bench_cb(&data, 0);
3938
3939 while (data.n > 0) {
3940 main_loop_wait(false);
3941 }
3942 gettimeofday(&t2, NULL);
3943
3944 printf("Run completed in %3.3f seconds.\n",
3945 (t2.tv_sec - t1.tv_sec)
3946 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
3947
3948out:
3949 qemu_vfree(data.buf);
3950 blk_unref(blk);
3951
3952 if (ret) {
3953 return 1;
3954 }
3955 return 0;
3956}
3957
Reda Sallahi86ce1f62016-08-10 04:43:12 +02003958#define C_BS 01
3959#define C_COUNT 02
3960#define C_IF 04
3961#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02003962#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02003963
3964struct DdInfo {
3965 unsigned int flags;
3966 int64_t count;
3967};
3968
3969struct DdIo {
3970 int bsz; /* Block size */
3971 char *filename;
3972 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02003973 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02003974};
3975
3976struct DdOpts {
3977 const char *name;
3978 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
3979 unsigned int flag;
3980};
3981
3982static int img_dd_bs(const char *arg,
3983 struct DdIo *in, struct DdIo *out,
3984 struct DdInfo *dd)
3985{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02003986 int64_t res;
3987
Markus Armbruster606caa02017-02-21 21:14:04 +01003988 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02003989
Markus Armbruster606caa02017-02-21 21:14:04 +01003990 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02003991 error_report("invalid number: '%s'", arg);
3992 return 1;
3993 }
3994 in->bsz = out->bsz = res;
3995
3996 return 0;
3997}
3998
3999static int img_dd_count(const char *arg,
4000 struct DdIo *in, struct DdIo *out,
4001 struct DdInfo *dd)
4002{
Markus Armbruster606caa02017-02-21 21:14:04 +01004003 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004004
Markus Armbruster606caa02017-02-21 21:14:04 +01004005 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004006 error_report("invalid number: '%s'", arg);
4007 return 1;
4008 }
4009
4010 return 0;
4011}
4012
4013static int img_dd_if(const char *arg,
4014 struct DdIo *in, struct DdIo *out,
4015 struct DdInfo *dd)
4016{
4017 in->filename = g_strdup(arg);
4018
4019 return 0;
4020}
4021
4022static int img_dd_of(const char *arg,
4023 struct DdIo *in, struct DdIo *out,
4024 struct DdInfo *dd)
4025{
4026 out->filename = g_strdup(arg);
4027
4028 return 0;
4029}
4030
Reda Sallahif7c15532016-08-10 16:16:09 +02004031static int img_dd_skip(const char *arg,
4032 struct DdIo *in, struct DdIo *out,
4033 struct DdInfo *dd)
4034{
Markus Armbruster606caa02017-02-21 21:14:04 +01004035 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004036
Markus Armbruster606caa02017-02-21 21:14:04 +01004037 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004038 error_report("invalid number: '%s'", arg);
4039 return 1;
4040 }
4041
4042 return 0;
4043}
4044
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004045static int img_dd(int argc, char **argv)
4046{
4047 int ret = 0;
4048 char *arg = NULL;
4049 char *tmp;
4050 BlockDriver *drv = NULL, *proto_drv = NULL;
4051 BlockBackend *blk1 = NULL, *blk2 = NULL;
4052 QemuOpts *opts = NULL;
4053 QemuOptsList *create_opts = NULL;
4054 Error *local_err = NULL;
4055 bool image_opts = false;
4056 int c, i;
4057 const char *out_fmt = "raw";
4058 const char *fmt = NULL;
4059 int64_t size = 0;
4060 int64_t block_count = 0, out_pos, in_pos;
4061 struct DdInfo dd = {
4062 .flags = 0,
4063 .count = 0,
4064 };
4065 struct DdIo in = {
4066 .bsz = 512, /* Block size is by default 512 bytes */
4067 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004068 .buf = NULL,
4069 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004070 };
4071 struct DdIo out = {
4072 .bsz = 512,
4073 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004074 .buf = NULL,
4075 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004076 };
4077
4078 const struct DdOpts options[] = {
4079 { "bs", img_dd_bs, C_BS },
4080 { "count", img_dd_count, C_COUNT },
4081 { "if", img_dd_if, C_IF },
4082 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004083 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004084 { NULL, NULL, 0 }
4085 };
4086 const struct option long_options[] = {
4087 { "help", no_argument, 0, 'h'},
4088 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4089 { 0, 0, 0, 0 }
4090 };
4091
4092 while ((c = getopt_long(argc, argv, "hf:O:", long_options, NULL))) {
4093 if (c == EOF) {
4094 break;
4095 }
4096 switch (c) {
4097 case 'O':
4098 out_fmt = optarg;
4099 break;
4100 case 'f':
4101 fmt = optarg;
4102 break;
4103 case '?':
4104 error_report("Try 'qemu-img --help' for more information.");
4105 ret = -1;
4106 goto out;
4107 case 'h':
4108 help();
4109 break;
4110 case OPTION_IMAGE_OPTS:
4111 image_opts = true;
4112 break;
4113 }
4114 }
4115
4116 for (i = optind; i < argc; i++) {
4117 int j;
4118 arg = g_strdup(argv[i]);
4119
4120 tmp = strchr(arg, '=');
4121 if (tmp == NULL) {
4122 error_report("unrecognized operand %s", arg);
4123 ret = -1;
4124 goto out;
4125 }
4126
4127 *tmp++ = '\0';
4128
4129 for (j = 0; options[j].name != NULL; j++) {
4130 if (!strcmp(arg, options[j].name)) {
4131 break;
4132 }
4133 }
4134 if (options[j].name == NULL) {
4135 error_report("unrecognized operand %s", arg);
4136 ret = -1;
4137 goto out;
4138 }
4139
4140 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4141 ret = -1;
4142 goto out;
4143 }
4144 dd.flags |= options[j].flag;
4145 g_free(arg);
4146 arg = NULL;
4147 }
4148
4149 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4150 error_report("Must specify both input and output files");
4151 ret = -1;
4152 goto out;
4153 }
4154 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false);
4155
4156 if (!blk1) {
4157 ret = -1;
4158 goto out;
4159 }
4160
4161 drv = bdrv_find_format(out_fmt);
4162 if (!drv) {
4163 error_report("Unknown file format");
4164 ret = -1;
4165 goto out;
4166 }
4167 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4168
4169 if (!proto_drv) {
4170 error_report_err(local_err);
4171 ret = -1;
4172 goto out;
4173 }
4174 if (!drv->create_opts) {
4175 error_report("Format driver '%s' does not support image creation",
4176 drv->format_name);
4177 ret = -1;
4178 goto out;
4179 }
4180 if (!proto_drv->create_opts) {
4181 error_report("Protocol driver '%s' does not support image creation",
4182 proto_drv->format_name);
4183 ret = -1;
4184 goto out;
4185 }
4186 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4187 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4188
4189 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4190
4191 size = blk_getlength(blk1);
4192 if (size < 0) {
4193 error_report("Failed to get size for '%s'", in.filename);
4194 ret = -1;
4195 goto out;
4196 }
4197
4198 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4199 dd.count * in.bsz < size) {
4200 size = dd.count * in.bsz;
4201 }
4202
Reda Sallahif7c15532016-08-10 16:16:09 +02004203 /* Overflow means the specified offset is beyond input image's size */
4204 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4205 size < in.bsz * in.offset)) {
4206 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4207 } else {
4208 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4209 size - in.bsz * in.offset, &error_abort);
4210 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004211
4212 ret = bdrv_create(drv, out.filename, opts, &local_err);
4213 if (ret < 0) {
4214 error_reportf_err(local_err,
4215 "%s: error while creating output image: ",
4216 out.filename);
4217 ret = -1;
4218 goto out;
4219 }
4220
4221 blk2 = img_open(image_opts, out.filename, out_fmt, BDRV_O_RDWR,
4222 false, false);
4223
4224 if (!blk2) {
4225 ret = -1;
4226 goto out;
4227 }
4228
Reda Sallahif7c15532016-08-10 16:16:09 +02004229 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4230 size < in.offset * in.bsz)) {
4231 /* We give a warning if the skip option is bigger than the input
4232 * size and create an empty output disk image (i.e. like dd(1)).
4233 */
4234 error_report("%s: cannot skip to specified offset", in.filename);
4235 in_pos = size;
4236 } else {
4237 in_pos = in.offset * in.bsz;
4238 }
4239
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004240 in.buf = g_new(uint8_t, in.bsz);
4241
Reda Sallahif7c15532016-08-10 16:16:09 +02004242 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004243 int in_ret, out_ret;
4244
4245 if (in_pos + in.bsz > size) {
4246 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4247 } else {
4248 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4249 }
4250 if (in_ret < 0) {
4251 error_report("error while reading from input image file: %s",
4252 strerror(-in_ret));
4253 ret = -1;
4254 goto out;
4255 }
4256 in_pos += in_ret;
4257
4258 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4259
4260 if (out_ret < 0) {
4261 error_report("error while writing to output image file: %s",
4262 strerror(-out_ret));
4263 ret = -1;
4264 goto out;
4265 }
4266 out_pos += out_ret;
4267 }
4268
4269out:
4270 g_free(arg);
4271 qemu_opts_del(opts);
4272 qemu_opts_free(create_opts);
4273 blk_unref(blk1);
4274 blk_unref(blk2);
4275 g_free(in.filename);
4276 g_free(out.filename);
4277 g_free(in.buf);
4278 g_free(out.buf);
4279
4280 if (ret) {
4281 return 1;
4282 }
4283 return 0;
4284}
4285
Kevin Wolfb6133b82014-08-05 14:17:13 +02004286
Anthony Liguoric227f092009-10-01 16:12:16 -05004287static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004288#define DEF(option, callback, arg_string) \
4289 { option, callback },
4290#include "qemu-img-cmds.h"
4291#undef DEF
4292#undef GEN_DOCS
4293 { NULL, NULL, },
4294};
4295
bellardea2384d2004-08-01 21:59:26 +00004296int main(int argc, char **argv)
4297{
Anthony Liguoric227f092009-10-01 16:12:16 -05004298 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004299 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004300 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004301 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004302 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004303 static const struct option long_options[] = {
4304 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004305 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004306 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004307 {0, 0, 0, 0}
4308 };
bellardea2384d2004-08-01 21:59:26 +00004309
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004310#ifdef CONFIG_POSIX
4311 signal(SIGPIPE, SIG_IGN);
4312#endif
4313
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004314 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004315 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004316 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004317
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004318 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004319 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004320 exit(EXIT_FAILURE);
4321 }
4322
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004323 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004324
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004325 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004326 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004327 if (argc < 2) {
4328 error_exit("Not enough arguments");
4329 }
Stuart Brady153859b2009-06-07 00:42:17 +01004330
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004331 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004332 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004333 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004334
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004335 while ((c = getopt_long(argc, argv, "+hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004336 switch (c) {
4337 case 'h':
4338 help();
4339 return 0;
4340 case 'V':
4341 printf(QEMU_IMG_VERSION);
4342 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004343 case 'T':
4344 g_free(trace_file);
4345 trace_file = trace_opt_parse(optarg);
4346 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004347 }
bellardea2384d2004-08-01 21:59:26 +00004348 }
Stuart Brady153859b2009-06-07 00:42:17 +01004349
Denis V. Lunev10985132016-06-17 17:44:13 +03004350 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004351
Denis V. Lunev10985132016-06-17 17:44:13 +03004352 /* reset getopt_long scanning */
4353 argc -= optind;
4354 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004355 return 0;
4356 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004357 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004358 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004359
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004360 if (!trace_init_backends()) {
4361 exit(1);
4362 }
4363 trace_init_file(trace_file);
4364 qemu_set_log(LOG_TRACE);
4365
Denis V. Lunev10985132016-06-17 17:44:13 +03004366 /* find the command */
4367 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4368 if (!strcmp(cmdname, cmd->name)) {
4369 return cmd->handler(argc, argv);
4370 }
4371 }
Jeff Cody7db16892014-04-25 17:02:32 -04004372
Stuart Brady153859b2009-06-07 00:42:17 +01004373 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004374 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004375}