blob: ae4fe6d83af769c7418577aa6592507ad3c2aeee [file] [log] [blame]
bellardea2384d2004-08-01 21:59:26 +00001/*
bellardfb43f4d2006-08-07 21:34:46 +00002 * QEMU disk image utility
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardea2384d2004-08-01 21:59:26 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Peter Maydell80c71a22016-01-18 18:01:42 +000024#include "qemu/osdep.h"
Fam Zheng67a1de02016-06-01 17:44:21 +080025#include "qemu-version.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010026#include "qapi/error.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020027#include "qapi-visit.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010028#include "qapi/qobject-output-visitor.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010029#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010030#include "qapi/qmp/qjson.h"
Fam Zheng335e9932017-05-03 00:35:39 +080031#include "qapi/qmp/qbool.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020032#include "qemu/cutils.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000033#include "qemu/config-file.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010034#include "qemu/option.h"
35#include "qemu/error-report.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030036#include "qemu/log.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000037#include "qom/object_interfaces.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/sysemu.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020039#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010040#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020041#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080042#include "block/qapi.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010043#include "crypto/init.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030044#include "trace/control.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020045#include <getopt.h>
bellarde8445332006-06-14 15:32:10 +000046
Don Slutz61979a62015-01-09 10:17:35 -050047#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
Thomas Huth0781dd62016-10-05 11:54:44 +020048 "\n" QEMU_COPYRIGHT "\n"
Jeff Cody5f6979c2014-04-28 14:37:18 -040049
Anthony Liguoric227f092009-10-01 16:12:16 -050050typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010051 const char *name;
52 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050053} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010054
Federico Simoncelli8599ea42013-01-28 06:59:47 -050055enum {
56 OPTION_OUTPUT = 256,
57 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000058 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000059 OPTION_IMAGE_OPTS = 259,
Kevin Wolfb6495fa2015-07-10 18:09:18 +020060 OPTION_PATTERN = 260,
Kevin Wolf55d539c2016-06-03 13:59:41 +020061 OPTION_FLUSH_INTERVAL = 261,
62 OPTION_NO_DRAIN = 262,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +010063 OPTION_TARGET_IMAGE_OPTS = 263,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050064};
65
66typedef enum OutputFormat {
67 OFORMAT_JSON,
68 OFORMAT_HUMAN,
69} OutputFormat;
70
Kevin Wolfe6996142016-03-15 13:03:11 +010071/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040072#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000073
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010074static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000075{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010076 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000077}
78
Fam Zhengac1307a2014-04-22 13:36:11 +080079static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
80{
81 va_list ap;
82
83 error_printf("qemu-img: ");
84
85 va_start(ap, fmt);
86 error_vprintf(fmt, ap);
87 va_end(ap);
88
89 error_printf("\nTry 'qemu-img --help' for more information\n");
90 exit(EXIT_FAILURE);
91}
92
Stefan Hajnoczic9192972017-03-17 18:45:41 +080093static void QEMU_NORETURN missing_argument(const char *option)
94{
95 error_exit("missing argument for option '%s'", option);
96}
97
98static void QEMU_NORETURN unrecognized_option(const char *option)
99{
100 error_exit("unrecognized option '%s'", option);
101}
102
blueswir1d2c639d2009-01-24 18:19:25 +0000103/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +0800104static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +0000105{
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100106 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -0400107 QEMU_IMG_VERSION
Denis V. Lunev10985132016-06-17 17:44:13 +0300108 "usage: qemu-img [standard options] command [command options]\n"
malc3f020d72010-02-08 12:04:56 +0300109 "QEMU disk image utility\n"
110 "\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300111 " '-h', '--help' display this help and exit\n"
112 " '-V', '--version' output version information and exit\n"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +0300113 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
114 " specify tracing options\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300115 "\n"
malc3f020d72010-02-08 12:04:56 +0300116 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100117#define DEF(option, callback, arg_string) \
118 " " arg_string "\n"
119#include "qemu-img-cmds.h"
120#undef DEF
121#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +0300122 "\n"
123 "Command parameters:\n"
124 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000125 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
126 " manual page for a description of the object properties. The most common\n"
127 " object type is a 'secret', which is used to supply passwords and/or\n"
128 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300129 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400130 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800131 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
132 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100133 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
134 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300135 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200136 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
137 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
138 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300139 " 'output_filename' is the destination disk image filename\n"
140 " 'output_fmt' is the destination format\n"
141 " 'options' is a comma separated list of format specific options in a\n"
142 " name=value format. Use -o ? for an overview of the options supported by the\n"
143 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800144 " 'snapshot_param' is param used for internal snapshot, format\n"
145 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
146 " '[ID_OR_NAME]'\n"
147 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
148 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300149 " '-c' indicates that target image must be compressed (qcow format only)\n"
150 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
151 " match exactly. The image doesn't need a working backing file before\n"
152 " rebasing in this case (useful for renaming the backing file)\n"
153 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200154 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100155 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200156 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
157 " contain only zeros for qemu-img to create a sparse image during\n"
158 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
159 " unallocated or zero sectors, and the destination image will always be\n"
160 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200161 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100162 " '-n' skips the target volume creation (useful if the volume is created\n"
163 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300164 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200165 "Parameters to check subcommand:\n"
166 " '-r' tries to repair any inconsistencies that are found during the check.\n"
167 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
168 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200169 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200170 "\n"
Peter Lieven2d9187b2017-02-28 13:40:07 +0100171 "Parameters to convert subcommand:\n"
172 " '-m' specifies how many coroutines work in parallel during the convert\n"
173 " process (defaults to 8)\n"
174 " '-W' allow to write to the target out of order rather than sequential\n"
175 "\n"
malc3f020d72010-02-08 12:04:56 +0300176 "Parameters to snapshot subcommand:\n"
177 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
178 " '-a' applies a snapshot (revert disk to saved state)\n"
179 " '-c' creates a snapshot\n"
180 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100181 " '-l' lists all snapshots in the given image\n"
182 "\n"
183 "Parameters to compare subcommand:\n"
184 " '-f' first image format\n"
185 " '-F' second image format\n"
Reda Sallahi86ce1f62016-08-10 04:43:12 +0200186 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
187 "\n"
188 "Parameters to dd subcommand:\n"
189 " 'bs=BYTES' read and write up to BYTES bytes at a time "
190 "(default: 512)\n"
191 " 'count=N' copy only N input blocks\n"
192 " 'if=FILE' read from FILE\n"
Reda Sallahif7c15532016-08-10 16:16:09 +0200193 " 'of=FILE' write to FILE\n"
194 " 'skip=N' skip N bs-sized blocks at the start of input\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100195
196 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100197 bdrv_iterate_format(format_print, NULL);
bellardea2384d2004-08-01 21:59:26 +0000198 printf("\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800199 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000200}
201
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000202static QemuOptsList qemu_object_opts = {
203 .name = "object",
204 .implied_opt_name = "qom-type",
205 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
206 .desc = {
207 { }
208 },
209};
210
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000211static QemuOptsList qemu_source_opts = {
212 .name = "source",
213 .implied_opt_name = "file",
214 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
215 .desc = {
216 { }
217 },
218};
219
Stefan Weil7c30f652013-06-16 17:01:05 +0200220static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100221{
222 int ret = 0;
223 if (!quiet) {
224 va_list args;
225 va_start(args, fmt);
226 ret = vprintf(fmt, args);
227 va_end(args);
228 }
229 return ret;
230}
231
bellardea2384d2004-08-01 21:59:26 +0000232
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100233static int print_block_option_help(const char *filename, const char *fmt)
234{
235 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800236 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500237 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100238
239 /* Find driver and parse its options */
240 drv = bdrv_find_format(fmt);
241 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100242 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100243 return 1;
244 }
245
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800246 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100247 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500248 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100249 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100250 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800251 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100252 return 1;
253 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800254 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100255 }
256
Chunyan Liu83d05212014-06-05 17:20:51 +0800257 qemu_opts_print_help(create_opts);
258 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100259 return 0;
260}
261
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000262
263static int img_open_password(BlockBackend *blk, const char *filename,
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000264 int flags, bool quiet)
bellard75c23802004-08-27 21:28:58 +0000265{
266 BlockDriverState *bs;
bellard75c23802004-08-27 21:28:58 +0000267 char password[256];
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000268
269 bs = blk_bs(blk);
Daniel P. Berrange4ef130f2016-03-21 14:11:43 +0000270 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs) &&
271 !(flags & BDRV_O_NO_IO)) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000272 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
273 if (qemu_read_password(password, sizeof(password)) < 0) {
274 error_report("No password given");
275 return -1;
276 }
277 if (bdrv_set_key(bs, password) < 0) {
278 error_report("invalid password");
279 return -1;
280 }
281 }
282 return 0;
283}
284
285
Max Reitzefaa7c42016-03-16 19:54:38 +0100286static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100287 QemuOpts *opts, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800288 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000289{
290 QDict *options;
291 Error *local_err = NULL;
292 BlockBackend *blk;
293 options = qemu_opts_to_qdict(opts, NULL);
Fam Zheng335e9932017-05-03 00:35:39 +0800294 if (force_share) {
295 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
296 && !qdict_get_bool(options, BDRV_OPT_FORCE_SHARE)) {
297 error_report("--force-share/-U conflicts with image options");
Fam Zhengadb998c2017-05-15 22:10:14 +0800298 QDECREF(options);
Fam Zheng335e9932017-05-03 00:35:39 +0800299 return NULL;
300 }
Eric Blake579cf1d2017-05-15 14:54:39 -0500301 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800302 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100303 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000304 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100305 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000306 return NULL;
307 }
Kevin Wolfce099542016-03-15 13:01:04 +0100308 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000309
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000310 if (img_open_password(blk, optstr, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000311 blk_unref(blk);
312 return NULL;
313 }
314 return blk;
315}
316
Max Reitzefaa7c42016-03-16 19:54:38 +0100317static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100318 QDict *options,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000319 const char *fmt, int flags,
Fam Zheng335e9932017-05-03 00:35:39 +0800320 bool writethrough, bool quiet,
321 bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000322{
323 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200324 Error *local_err = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100325
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100326 if (!options) {
327 options = qdict_new();
328 }
bellard75c23802004-08-27 21:28:58 +0000329 if (fmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500330 qdict_put_str(options, "driver", fmt);
bellard75c23802004-08-27 21:28:58 +0000331 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100332
Fam Zheng335e9932017-05-03 00:35:39 +0800333 if (force_share) {
Eric Blake579cf1d2017-05-15 14:54:39 -0500334 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800335 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100336 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500337 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100338 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000339 return NULL;
bellard75c23802004-08-27 21:28:58 +0000340 }
Kevin Wolfce099542016-03-15 13:01:04 +0100341 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100342
Daniel P. Berrangeabb06c52016-03-21 14:11:42 +0000343 if (img_open_password(blk, filename, flags, quiet) < 0) {
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000344 blk_unref(blk);
345 return NULL;
bellard75c23802004-08-27 21:28:58 +0000346 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200347 return blk;
bellard75c23802004-08-27 21:28:58 +0000348}
349
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000350
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100351static int img_add_key_secrets(void *opaque,
352 const char *name, const char *value,
353 Error **errp)
354{
355 QDict *options = opaque;
356
357 if (g_str_has_suffix(name, "key-secret")) {
358 qdict_put(options, name, qstring_from_str(value));
359 }
360
361 return 0;
362}
363
364static BlockBackend *img_open_new_file(const char *filename,
365 QemuOpts *create_opts,
366 const char *fmt, int flags,
367 bool writethrough, bool quiet,
368 bool force_share)
369{
370 QDict *options = NULL;
371
372 options = qdict_new();
373 qemu_opt_foreach(create_opts, img_add_key_secrets, options, &error_abort);
374
375 return img_open_file(filename, options, fmt, flags, writethrough, quiet,
376 force_share);
377}
378
379
Max Reitzefaa7c42016-03-16 19:54:38 +0100380static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000381 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100382 const char *fmt, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800383 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000384{
385 BlockBackend *blk;
386 if (image_opts) {
387 QemuOpts *opts;
388 if (fmt) {
389 error_report("--image-opts and --format are mutually exclusive");
390 return NULL;
391 }
392 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
393 filename, true);
394 if (!opts) {
395 return NULL;
396 }
Fam Zheng335e9932017-05-03 00:35:39 +0800397 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
398 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000399 } else {
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100400 blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
Fam Zheng335e9932017-05-03 00:35:39 +0800401 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000402 }
403 return blk;
404}
405
406
Chunyan Liu83d05212014-06-05 17:20:51 +0800407static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100408 const char *base_filename,
409 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200410{
Markus Armbruster6750e792015-02-12 17:43:08 +0100411 Error *err = NULL;
412
Kevin Wolfefa84d42009-05-18 16:42:12 +0200413 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100414 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100415 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100416 error_report("Backing file not supported for file format '%s'",
417 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100418 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900419 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200420 }
421 }
422 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100423 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100424 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100425 error_report("Backing file format not supported for file "
426 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100427 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900428 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200429 }
430 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900431 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200432}
433
Markus Armbruster606caa02017-02-21 21:14:04 +0100434static int64_t cvtnum(const char *s)
435{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100436 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100437 uint64_t value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100438
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100439 err = qemu_strtosz(s, NULL, &value);
440 if (err < 0) {
441 return err;
442 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100443 if (value > INT64_MAX) {
444 return -ERANGE;
445 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100446 return value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100447}
448
bellardea2384d2004-08-01 21:59:26 +0000449static int img_create(int argc, char **argv)
450{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200451 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100452 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000453 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000454 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000455 const char *filename;
456 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200457 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200458 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100459 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000460
bellardea2384d2004-08-01 21:59:26 +0000461 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000462 static const struct option long_options[] = {
463 {"help", no_argument, 0, 'h'},
464 {"object", required_argument, 0, OPTION_OBJECT},
465 {0, 0, 0, 0}
466 };
Daniel P. Berrange6b4df542017-07-04 13:30:09 +0100467 c = getopt_long(argc, argv, ":F:b:f:ho:q",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000468 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100469 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000470 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100471 }
bellardea2384d2004-08-01 21:59:26 +0000472 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800473 case ':':
474 missing_argument(argv[optind - 1]);
475 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100476 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800477 unrecognized_option(argv[optind - 1]);
478 break;
bellardea2384d2004-08-01 21:59:26 +0000479 case 'h':
480 help();
481 break;
aliguori9230eaf2009-03-28 17:55:19 +0000482 case 'F':
483 base_fmt = optarg;
484 break;
bellardea2384d2004-08-01 21:59:26 +0000485 case 'b':
486 base_filename = optarg;
487 break;
488 case 'f':
489 fmt = optarg;
490 break;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200491 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100492 if (!is_valid_option_list(optarg)) {
493 error_report("Invalid option list: %s", optarg);
494 goto fail;
495 }
496 if (!options) {
497 options = g_strdup(optarg);
498 } else {
499 char *old_options = options;
500 options = g_strdup_printf("%s,%s", options, optarg);
501 g_free(old_options);
502 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200503 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100504 case 'q':
505 quiet = true;
506 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000507 case OPTION_OBJECT: {
508 QemuOpts *opts;
509 opts = qemu_opts_parse_noisily(&qemu_object_opts,
510 optarg, true);
511 if (!opts) {
512 goto fail;
513 }
514 } break;
bellardea2384d2004-08-01 21:59:26 +0000515 }
516 }
aliguori9230eaf2009-03-28 17:55:19 +0000517
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900518 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100519 filename = (optind < argc) ? argv[optind] : NULL;
520 if (options && has_help_option(options)) {
521 g_free(options);
522 return print_block_option_help(filename, fmt);
523 }
524
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100525 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800526 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100527 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100528 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900529
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000530 if (qemu_opts_foreach(&qemu_object_opts,
531 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200532 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000533 goto fail;
534 }
535
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100536 /* Get image size, if specified */
537 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100538 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100539
540 sval = cvtnum(argv[optind++]);
541 if (sval < 0) {
liguang79443392012-12-17 09:49:23 +0800542 if (sval == -ERANGE) {
543 error_report("Image size must be less than 8 EiB!");
544 } else {
545 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200546 "G, T, P or E suffixes for ");
547 error_report("kilobytes, megabytes, gigabytes, terabytes, "
548 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800549 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100550 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100551 }
552 img_size = (uint64_t)sval;
553 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200554 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800555 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200556 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100557
Luiz Capitulino9b375252012-11-30 10:52:05 -0200558 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Fam Zheng92172832017-04-21 20:27:01 +0800559 options, img_size, 0, quiet, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100560 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100561 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100562 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900563 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200564
Kevin Wolf77386bf2014-02-21 16:24:04 +0100565 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000566 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100567
568fail:
569 g_free(options);
570 return 1;
bellardea2384d2004-08-01 21:59:26 +0000571}
572
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100573static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500574{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500575 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500576 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100577 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600578
579 visit_type_ImageCheck(v, NULL, &check, &error_abort);
580 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500581 str = qobject_to_json_pretty(obj);
582 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100583 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500584 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600585 visit_free(v);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500586 QDECREF(str);
587}
588
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100589static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500590{
591 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100592 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500593 } else {
594 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100595 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
596 "Data may be corrupted, or further writes to the image "
597 "may corrupt it.\n",
598 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500599 }
600
601 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100602 qprintf(quiet,
603 "\n%" PRId64 " leaked clusters were found on the image.\n"
604 "This means waste of disk space, but no harm to data.\n",
605 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500606 }
607
608 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100609 qprintf(quiet,
610 "\n%" PRId64
611 " internal errors have occurred during the check.\n",
612 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500613 }
614 }
615
616 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100617 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
618 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
619 check->allocated_clusters, check->total_clusters,
620 check->allocated_clusters * 100.0 / check->total_clusters,
621 check->fragmented_clusters * 100.0 / check->allocated_clusters,
622 check->compressed_clusters * 100.0 /
623 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500624 }
625
626 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100627 qprintf(quiet,
628 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500629 }
630}
631
632static int collect_image_check(BlockDriverState *bs,
633 ImageCheck *check,
634 const char *filename,
635 const char *fmt,
636 int fix)
637{
638 int ret;
639 BdrvCheckResult result;
640
641 ret = bdrv_check(bs, &result, fix);
642 if (ret < 0) {
643 return ret;
644 }
645
646 check->filename = g_strdup(filename);
647 check->format = g_strdup(bdrv_get_format_name(bs));
648 check->check_errors = result.check_errors;
649 check->corruptions = result.corruptions;
650 check->has_corruptions = result.corruptions != 0;
651 check->leaks = result.leaks;
652 check->has_leaks = result.leaks != 0;
653 check->corruptions_fixed = result.corruptions_fixed;
654 check->has_corruptions_fixed = result.corruptions != 0;
655 check->leaks_fixed = result.leaks_fixed;
656 check->has_leaks_fixed = result.leaks != 0;
657 check->image_end_offset = result.image_end_offset;
658 check->has_image_end_offset = result.image_end_offset != 0;
659 check->total_clusters = result.bfi.total_clusters;
660 check->has_total_clusters = result.bfi.total_clusters != 0;
661 check->allocated_clusters = result.bfi.allocated_clusters;
662 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
663 check->fragmented_clusters = result.bfi.fragmented_clusters;
664 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100665 check->compressed_clusters = result.bfi.compressed_clusters;
666 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500667
668 return 0;
669}
670
Kevin Wolfe076f332010-06-29 11:43:13 +0200671/*
672 * Checks an image for consistency. Exit codes:
673 *
Max Reitzd6635c42014-06-02 22:15:21 +0200674 * 0 - Check completed, image is good
675 * 1 - Check not completed because of internal errors
676 * 2 - Check completed, image is corrupted
677 * 3 - Check completed, image has leaked clusters, but is good otherwise
678 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200679 */
aliguori15859692009-04-21 23:11:53 +0000680static int img_check(int argc, char **argv)
681{
682 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500683 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200684 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200685 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000686 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200687 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100688 int flags = BDRV_O_CHECK;
689 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200690 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100691 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000692 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +0800693 bool force_share = false;
aliguori15859692009-04-21 23:11:53 +0000694
695 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500696 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200697 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100698
aliguori15859692009-04-21 23:11:53 +0000699 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500700 int option_index = 0;
701 static const struct option long_options[] = {
702 {"help", no_argument, 0, 'h'},
703 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530704 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500705 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000706 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000707 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +0800708 {"force-share", no_argument, 0, 'U'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500709 {0, 0, 0, 0}
710 };
Fam Zheng335e9932017-05-03 00:35:39 +0800711 c = getopt_long(argc, argv, ":hf:r:T:qU",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500712 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100713 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000714 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100715 }
aliguori15859692009-04-21 23:11:53 +0000716 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800717 case ':':
718 missing_argument(argv[optind - 1]);
719 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100720 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800721 unrecognized_option(argv[optind - 1]);
722 break;
aliguori15859692009-04-21 23:11:53 +0000723 case 'h':
724 help();
725 break;
726 case 'f':
727 fmt = optarg;
728 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200729 case 'r':
730 flags |= BDRV_O_RDWR;
731
732 if (!strcmp(optarg, "leaks")) {
733 fix = BDRV_FIX_LEAKS;
734 } else if (!strcmp(optarg, "all")) {
735 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
736 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800737 error_exit("Unknown option value for -r "
738 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200739 }
740 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500741 case OPTION_OUTPUT:
742 output = optarg;
743 break;
Max Reitz40055952014-07-22 22:58:42 +0200744 case 'T':
745 cache = optarg;
746 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100747 case 'q':
748 quiet = true;
749 break;
Fam Zheng335e9932017-05-03 00:35:39 +0800750 case 'U':
751 force_share = true;
752 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000753 case OPTION_OBJECT: {
754 QemuOpts *opts;
755 opts = qemu_opts_parse_noisily(&qemu_object_opts,
756 optarg, true);
757 if (!opts) {
758 return 1;
759 }
760 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000761 case OPTION_IMAGE_OPTS:
762 image_opts = true;
763 break;
aliguori15859692009-04-21 23:11:53 +0000764 }
765 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200766 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800767 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100768 }
aliguori15859692009-04-21 23:11:53 +0000769 filename = argv[optind++];
770
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500771 if (output && !strcmp(output, "json")) {
772 output_format = OFORMAT_JSON;
773 } else if (output && !strcmp(output, "human")) {
774 output_format = OFORMAT_HUMAN;
775 } else if (output) {
776 error_report("--output must be used with human or json as argument.");
777 return 1;
778 }
779
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000780 if (qemu_opts_foreach(&qemu_object_opts,
781 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200782 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000783 return 1;
784 }
785
Kevin Wolfce099542016-03-15 13:01:04 +0100786 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200787 if (ret < 0) {
788 error_report("Invalid source cache option: %s", cache);
789 return 1;
790 }
791
Fam Zheng335e9932017-05-03 00:35:39 +0800792 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
793 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200794 if (!blk) {
795 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900796 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200797 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500798
799 check = g_new0(ImageCheck, 1);
800 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200801
802 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200803 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200804 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500805 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200806 }
807
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500808 if (check->corruptions_fixed || check->leaks_fixed) {
809 int corruptions_fixed, leaks_fixed;
810
811 leaks_fixed = check->leaks_fixed;
812 corruptions_fixed = check->corruptions_fixed;
813
814 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100815 qprintf(quiet,
816 "The following inconsistencies were found and repaired:\n\n"
817 " %" PRId64 " leaked clusters\n"
818 " %" PRId64 " corruptions\n\n"
819 "Double checking the fixed image now...\n",
820 check->leaks_fixed,
821 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500822 }
823
824 ret = collect_image_check(bs, check, filename, fmt, 0);
825
826 check->leaks_fixed = leaks_fixed;
827 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200828 }
829
Max Reitz832390a2014-10-23 15:29:12 +0200830 if (!ret) {
831 switch (output_format) {
832 case OFORMAT_HUMAN:
833 dump_human_image_check(check, quiet);
834 break;
835 case OFORMAT_JSON:
836 dump_json_image_check(check, quiet);
837 break;
838 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500839 }
840
841 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200842 if (ret) {
843 error_report("Check failed: %s", strerror(-ret));
844 } else {
845 error_report("Check failed");
846 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500847 ret = 1;
848 goto fail;
849 }
850
851 if (check->corruptions) {
852 ret = 2;
853 } else if (check->leaks) {
854 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200855 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500856 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000857 }
858
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500859fail:
860 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200861 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500862 return ret;
aliguori15859692009-04-21 23:11:53 +0000863}
864
Max Reitzd4a32382014-10-24 15:57:37 +0200865typedef struct CommonBlockJobCBInfo {
866 BlockDriverState *bs;
867 Error **errp;
868} CommonBlockJobCBInfo;
869
870static void common_block_job_cb(void *opaque, int ret)
871{
872 CommonBlockJobCBInfo *cbi = opaque;
873
874 if (ret < 0) {
875 error_setg_errno(cbi->errp, -ret, "Block job failed");
876 }
Max Reitzd4a32382014-10-24 15:57:37 +0200877}
878
879static void run_block_job(BlockJob *job, Error **errp)
880{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200881 AioContext *aio_context = blk_get_aio_context(job->blk);
sochin.jiang4172a002017-06-15 14:47:33 +0800882 int ret = 0;
Max Reitzd4a32382014-10-24 15:57:37 +0200883
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200884 aio_context_acquire(aio_context);
sochin.jiang4172a002017-06-15 14:47:33 +0800885 block_job_ref(job);
Max Reitzd4a32382014-10-24 15:57:37 +0200886 do {
887 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500888 qemu_progress_print(job->len ?
889 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
sochin.jiang4172a002017-06-15 14:47:33 +0800890 } while (!job->ready && !job->completed);
Max Reitzd4a32382014-10-24 15:57:37 +0200891
sochin.jiang4172a002017-06-15 14:47:33 +0800892 if (!job->completed) {
893 ret = block_job_complete_sync(job, errp);
894 } else {
895 ret = job->ret;
896 }
897 block_job_unref(job);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200898 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200899
sochin.jiang4172a002017-06-15 14:47:33 +0800900 /* publish completion progress only when success */
901 if (!ret) {
902 qemu_progress_print(100.f, 0);
903 }
Max Reitzd4a32382014-10-24 15:57:37 +0200904}
905
bellardea2384d2004-08-01 21:59:26 +0000906static int img_commit(int argc, char **argv)
907{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400908 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200909 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200910 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200911 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100912 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200913 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100914 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200915 Error *local_err = NULL;
916 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000917 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200918 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000919
920 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400921 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200922 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000923 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000924 static const struct option long_options[] = {
925 {"help", no_argument, 0, 'h'},
926 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000927 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000928 {0, 0, 0, 0}
929 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800930 c = getopt_long(argc, argv, ":f:ht:b:dpq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000931 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100932 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000933 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100934 }
bellardea2384d2004-08-01 21:59:26 +0000935 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800936 case ':':
937 missing_argument(argv[optind - 1]);
938 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100939 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800940 unrecognized_option(argv[optind - 1]);
941 break;
bellardea2384d2004-08-01 21:59:26 +0000942 case 'h':
943 help();
944 break;
945 case 'f':
946 fmt = optarg;
947 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400948 case 't':
949 cache = optarg;
950 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200951 case 'b':
952 base = optarg;
953 /* -b implies -d */
954 drop = true;
955 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200956 case 'd':
957 drop = true;
958 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200959 case 'p':
960 progress = true;
961 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100962 case 'q':
963 quiet = true;
964 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000965 case OPTION_OBJECT: {
966 QemuOpts *opts;
967 opts = qemu_opts_parse_noisily(&qemu_object_opts,
968 optarg, true);
969 if (!opts) {
970 return 1;
971 }
972 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000973 case OPTION_IMAGE_OPTS:
974 image_opts = true;
975 break;
bellardea2384d2004-08-01 21:59:26 +0000976 }
977 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200978
979 /* Progress is not shown in Quiet mode */
980 if (quiet) {
981 progress = false;
982 }
983
Kevin Wolffc11eb22013-08-05 10:53:04 +0200984 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800985 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100986 }
bellardea2384d2004-08-01 21:59:26 +0000987 filename = argv[optind++];
988
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000989 if (qemu_opts_foreach(&qemu_object_opts,
990 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200991 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000992 return 1;
993 }
994
Max Reitz9a86fe42014-10-24 15:57:38 +0200995 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100996 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400997 if (ret < 0) {
998 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100999 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001000 }
1001
Fam Zheng335e9932017-05-03 00:35:39 +08001002 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
1003 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001004 if (!blk) {
1005 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001006 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001007 bs = blk_bs(blk);
1008
Max Reitz687fa1d2014-10-24 15:57:39 +02001009 qemu_progress_init(progress, 1.f);
1010 qemu_progress_print(0.f, 100);
1011
Max Reitz1b22bff2014-10-24 15:57:40 +02001012 if (base) {
1013 base_bs = bdrv_find_backing_image(bs, base);
1014 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +01001015 error_setg(&local_err,
1016 "Did not find '%s' in the backing chain of '%s'",
1017 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +02001018 goto done;
1019 }
1020 } else {
1021 /* This is different from QMP, which by default uses the deepest file in
1022 * the backing chain (i.e., the very base); however, the traditional
1023 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +02001024 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +02001025 if (!base_bs) {
1026 error_setg(&local_err, "Image does not have a backing file");
1027 goto done;
1028 }
bellardea2384d2004-08-01 21:59:26 +00001029 }
1030
Max Reitzd4a32382014-10-24 15:57:37 +02001031 cbi = (CommonBlockJobCBInfo){
1032 .errp = &local_err,
1033 .bs = bs,
1034 };
1035
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001036 aio_context = bdrv_get_aio_context(bs);
1037 aio_context_acquire(aio_context);
John Snow47970df2016-10-27 12:06:57 -04001038 commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
Kevin Wolf0db832f2017-02-20 18:10:05 +01001039 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +08001040 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001041 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +02001042 if (local_err) {
1043 goto done;
1044 }
1045
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001046 /* When the block job completes, the BlockBackend reference will point to
1047 * the old backing file. In order to avoid that the top image is already
1048 * deleted, so we can still empty it afterwards, increment the reference
1049 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +02001050 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001051 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001052 }
1053
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001054 job = block_job_get("commit");
1055 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001056 if (local_err) {
1057 goto unref_backing;
1058 }
1059
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001060 if (!drop && bs->drv->bdrv_make_empty) {
1061 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001062 if (ret) {
1063 error_setg_errno(&local_err, -ret, "Could not empty %s",
1064 filename);
1065 goto unref_backing;
1066 }
1067 }
1068
1069unref_backing:
1070 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001071 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001072 }
Max Reitzd4a32382014-10-24 15:57:37 +02001073
1074done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001075 qemu_progress_end();
1076
Markus Armbruster26f54e92014-10-07 13:59:04 +02001077 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001078
1079 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001080 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001081 return 1;
1082 }
Max Reitzd4a32382014-10-24 15:57:37 +02001083
1084 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001085 return 0;
1086}
1087
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001088/*
thsf58c7b32008-06-05 21:53:49 +00001089 * Returns true iff the first sector pointed to by 'buf' contains at least
1090 * a non-NUL byte.
1091 *
1092 * 'pnum' is set to the number of sectors (including and immediately following
1093 * the first one) that are known to be in the same allocated/unallocated state.
1094 */
bellardea2384d2004-08-01 21:59:26 +00001095static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1096{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001097 bool is_zero;
1098 int i;
bellardea2384d2004-08-01 21:59:26 +00001099
1100 if (n <= 0) {
1101 *pnum = 0;
1102 return 0;
1103 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001104 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001105 for(i = 1; i < n; i++) {
1106 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001107 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001108 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001109 }
bellardea2384d2004-08-01 21:59:26 +00001110 }
1111 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001112 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001113}
1114
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001115/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001116 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1117 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1118 * breaking up write requests for only small sparse areas.
1119 */
1120static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1121 int min)
1122{
1123 int ret;
1124 int num_checked, num_used;
1125
1126 if (n < min) {
1127 min = n;
1128 }
1129
1130 ret = is_allocated_sectors(buf, n, pnum);
1131 if (!ret) {
1132 return ret;
1133 }
1134
1135 num_used = *pnum;
1136 buf += BDRV_SECTOR_SIZE * *pnum;
1137 n -= *pnum;
1138 num_checked = num_used;
1139
1140 while (n > 0) {
1141 ret = is_allocated_sectors(buf, n, pnum);
1142
1143 buf += BDRV_SECTOR_SIZE * *pnum;
1144 n -= *pnum;
1145 num_checked += *pnum;
1146 if (ret) {
1147 num_used = num_checked;
1148 } else if (*pnum >= min) {
1149 break;
1150 }
1151 }
1152
1153 *pnum = num_used;
1154 return 1;
1155}
1156
1157/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001158 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1159 * buffers matches, non-zero otherwise.
1160 *
1161 * pnum is set to the number of sectors (including and immediately following
1162 * the first one) that are known to have the same comparison result
1163 */
1164static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1165 int *pnum)
1166{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001167 bool res;
1168 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001169
1170 if (n <= 0) {
1171 *pnum = 0;
1172 return 0;
1173 }
1174
1175 res = !!memcmp(buf1, buf2, 512);
1176 for(i = 1; i < n; i++) {
1177 buf1 += 512;
1178 buf2 += 512;
1179
1180 if (!!memcmp(buf1, buf2, 512) != res) {
1181 break;
1182 }
1183 }
1184
1185 *pnum = i;
1186 return res;
1187}
1188
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001189#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001190
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001191static int64_t sectors_to_bytes(int64_t sectors)
1192{
1193 return sectors << BDRV_SECTOR_BITS;
1194}
1195
1196static int64_t sectors_to_process(int64_t total, int64_t from)
1197{
1198 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1199}
1200
1201/*
1202 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1203 *
1204 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1205 * data and negative value on error.
1206 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001207 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001208 * @param sect_num: Number of first sector to check
1209 * @param sect_count: Number of sectors to check
1210 * @param filename: Name of disk file we are checking (logging purpose)
1211 * @param buffer: Allocated buffer for storing read data
1212 * @param quiet: Flag for quiet mode
1213 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001214static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001215 int sect_count, const char *filename,
1216 uint8_t *buffer, bool quiet)
1217{
1218 int pnum, ret = 0;
Eric Blake91669202016-05-06 10:26:43 -06001219 ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer,
1220 sect_count << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001221 if (ret < 0) {
1222 error_report("Error while reading offset %" PRId64 " of %s: %s",
1223 sectors_to_bytes(sect_num), filename, strerror(-ret));
1224 return ret;
1225 }
1226 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1227 if (ret || pnum != sect_count) {
1228 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1229 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1230 return 1;
1231 }
1232
1233 return 0;
1234}
1235
1236/*
1237 * Compares two images. Exit codes:
1238 *
1239 * 0 - Images are identical
1240 * 1 - Images differ
1241 * >1 - Error occurred
1242 */
1243static int img_compare(int argc, char **argv)
1244{
Max Reitz40055952014-07-22 22:58:42 +02001245 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001246 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001247 BlockDriverState *bs1, *bs2;
1248 int64_t total_sectors1, total_sectors2;
1249 uint8_t *buf1 = NULL, *buf2 = NULL;
1250 int pnum1, pnum2;
1251 int allocated1, allocated2;
1252 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1253 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001254 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001255 bool writethrough;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001256 int64_t total_sectors;
1257 int64_t sector_num = 0;
1258 int64_t nb_sectors;
1259 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001260 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001261 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001262 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001263
Max Reitz40055952014-07-22 22:58:42 +02001264 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001265 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001266 static const struct option long_options[] = {
1267 {"help", no_argument, 0, 'h'},
1268 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001269 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001270 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001271 {0, 0, 0, 0}
1272 };
Fam Zheng335e9932017-05-03 00:35:39 +08001273 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001274 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001275 if (c == -1) {
1276 break;
1277 }
1278 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001279 case ':':
1280 missing_argument(argv[optind - 1]);
1281 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001282 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001283 unrecognized_option(argv[optind - 1]);
1284 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001285 case 'h':
1286 help();
1287 break;
1288 case 'f':
1289 fmt1 = optarg;
1290 break;
1291 case 'F':
1292 fmt2 = optarg;
1293 break;
Max Reitz40055952014-07-22 22:58:42 +02001294 case 'T':
1295 cache = optarg;
1296 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001297 case 'p':
1298 progress = true;
1299 break;
1300 case 'q':
1301 quiet = true;
1302 break;
1303 case 's':
1304 strict = true;
1305 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001306 case 'U':
1307 force_share = true;
1308 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001309 case OPTION_OBJECT: {
1310 QemuOpts *opts;
1311 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1312 optarg, true);
1313 if (!opts) {
1314 ret = 2;
1315 goto out4;
1316 }
1317 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001318 case OPTION_IMAGE_OPTS:
1319 image_opts = true;
1320 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001321 }
1322 }
1323
1324 /* Progress is not shown in Quiet mode */
1325 if (quiet) {
1326 progress = false;
1327 }
1328
1329
Kevin Wolffc11eb22013-08-05 10:53:04 +02001330 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001331 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001332 }
1333 filename1 = argv[optind++];
1334 filename2 = argv[optind++];
1335
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001336 if (qemu_opts_foreach(&qemu_object_opts,
1337 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001338 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001339 ret = 2;
1340 goto out4;
1341 }
1342
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001343 /* Initialize before goto out */
1344 qemu_progress_init(progress, 2.0);
1345
Kevin Wolfce099542016-03-15 13:01:04 +01001346 flags = 0;
1347 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001348 if (ret < 0) {
1349 error_report("Invalid source cache option: %s", cache);
1350 ret = 2;
1351 goto out3;
1352 }
1353
Fam Zheng335e9932017-05-03 00:35:39 +08001354 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1355 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001356 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001357 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001358 goto out3;
1359 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001360
Fam Zheng335e9932017-05-03 00:35:39 +08001361 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1362 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001363 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001364 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001365 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001366 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001367 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001368 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001369
Max Reitzf1d3cd72015-02-05 13:58:18 -05001370 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1371 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1372 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001373 if (total_sectors1 < 0) {
1374 error_report("Can't get size of %s: %s",
1375 filename1, strerror(-total_sectors1));
1376 ret = 4;
1377 goto out;
1378 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001379 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001380 if (total_sectors2 < 0) {
1381 error_report("Can't get size of %s: %s",
1382 filename2, strerror(-total_sectors2));
1383 ret = 4;
1384 goto out;
1385 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001386 total_sectors = MIN(total_sectors1, total_sectors2);
1387 progress_base = MAX(total_sectors1, total_sectors2);
1388
1389 qemu_progress_print(0, 100);
1390
1391 if (strict && total_sectors1 != total_sectors2) {
1392 ret = 1;
1393 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1394 goto out;
1395 }
1396
1397 for (;;) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001398 int64_t status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001399 BlockDriverState *file;
1400
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001401 nb_sectors = sectors_to_process(total_sectors, sector_num);
1402 if (nb_sectors <= 0) {
1403 break;
1404 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001405 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1406 total_sectors1 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001407 &pnum1, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001408 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001409 ret = 3;
1410 error_report("Sector allocation test failed for %s", filename1);
1411 goto out;
1412 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001413 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001414
Fam Zheng25ad8e62016-01-13 16:37:41 +08001415 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1416 total_sectors2 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001417 &pnum2, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001418 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001419 ret = 3;
1420 error_report("Sector allocation test failed for %s", filename2);
1421 goto out;
1422 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001423 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1424 if (pnum1) {
1425 nb_sectors = MIN(nb_sectors, pnum1);
1426 }
1427 if (pnum2) {
1428 nb_sectors = MIN(nb_sectors, pnum2);
1429 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001430
Fam Zheng25ad8e62016-01-13 16:37:41 +08001431 if (strict) {
1432 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1433 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1434 ret = 1;
1435 qprintf(quiet, "Strict mode: Offset %" PRId64
1436 " block status mismatch!\n",
1437 sectors_to_bytes(sector_num));
1438 goto out;
1439 }
1440 }
1441 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1442 nb_sectors = MIN(pnum1, pnum2);
1443 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001444 if (allocated1) {
Eric Blake91669202016-05-06 10:26:43 -06001445 ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1,
1446 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001447 if (ret < 0) {
1448 error_report("Error while reading offset %" PRId64 " of %s:"
1449 " %s", sectors_to_bytes(sector_num), filename1,
1450 strerror(-ret));
1451 ret = 4;
1452 goto out;
1453 }
Eric Blake91669202016-05-06 10:26:43 -06001454 ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2,
1455 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001456 if (ret < 0) {
1457 error_report("Error while reading offset %" PRId64
1458 " of %s: %s", sectors_to_bytes(sector_num),
1459 filename2, strerror(-ret));
1460 ret = 4;
1461 goto out;
1462 }
1463 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1464 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001465 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1466 sectors_to_bytes(
1467 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001468 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001469 goto out;
1470 }
1471 }
1472 } else {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001473
1474 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001475 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001476 filename1, buf1, quiet);
1477 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001478 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001479 filename2, buf1, quiet);
1480 }
1481 if (ret) {
1482 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001483 error_report("Error while reading offset %" PRId64 ": %s",
1484 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001485 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001486 }
1487 goto out;
1488 }
1489 }
1490 sector_num += nb_sectors;
1491 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1492 }
1493
1494 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001495 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001496 int64_t total_sectors_over;
1497 const char *filename_over;
1498
1499 qprintf(quiet, "Warning: Image size mismatch!\n");
1500 if (total_sectors1 > total_sectors2) {
1501 total_sectors_over = total_sectors1;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001502 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001503 filename_over = filename1;
1504 } else {
1505 total_sectors_over = total_sectors2;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001506 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001507 filename_over = filename2;
1508 }
1509
1510 for (;;) {
Eric Blake51b0a482017-07-07 07:44:59 -05001511 int64_t count;
1512
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001513 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1514 if (nb_sectors <= 0) {
1515 break;
1516 }
Eric Blake51b0a482017-07-07 07:44:59 -05001517 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL,
1518 sector_num * BDRV_SECTOR_SIZE,
1519 nb_sectors * BDRV_SECTOR_SIZE,
1520 &count);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001521 if (ret < 0) {
1522 ret = 3;
1523 error_report("Sector allocation test failed for %s",
1524 filename_over);
1525 goto out;
1526
1527 }
Eric Blake51b0a482017-07-07 07:44:59 -05001528 /* TODO relax this once bdrv_is_allocated_above does not enforce
1529 * sector alignment */
1530 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
1531 nb_sectors = count >> BDRV_SECTOR_BITS;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001532 if (ret) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001533 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001534 filename_over, buf1, quiet);
1535 if (ret) {
1536 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001537 error_report("Error while reading offset %" PRId64
1538 " of %s: %s", sectors_to_bytes(sector_num),
1539 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001540 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001541 }
1542 goto out;
1543 }
1544 }
1545 sector_num += nb_sectors;
1546 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1547 }
1548 }
1549
1550 qprintf(quiet, "Images are identical.\n");
1551 ret = 0;
1552
1553out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001554 qemu_vfree(buf1);
1555 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001556 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001557out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001558 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001559out3:
1560 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001561out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001562 return ret;
1563}
1564
Kevin Wolf690c7302015-03-19 13:33:32 +01001565enum ImgConvertBlockStatus {
1566 BLK_DATA,
1567 BLK_ZERO,
1568 BLK_BACKING_FILE,
1569};
1570
Peter Lieven2d9187b2017-02-28 13:40:07 +01001571#define MAX_COROUTINES 16
1572
Kevin Wolf690c7302015-03-19 13:33:32 +01001573typedef struct ImgConvertState {
1574 BlockBackend **src;
1575 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001576 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001577 int64_t total_sectors;
1578 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001579 int64_t allocated_done;
1580 int64_t sector_num;
1581 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001582 enum ImgConvertBlockStatus status;
1583 int64_t sector_next_status;
1584 BlockBackend *target;
1585 bool has_zero_init;
1586 bool compressed;
1587 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001588 bool wr_in_order;
Kevin Wolf690c7302015-03-19 13:33:32 +01001589 int min_sparse;
1590 size_t cluster_sectors;
1591 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001592 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001593 int running_coroutines;
1594 Coroutine *co[MAX_COROUTINES];
1595 int64_t wait_sector_num[MAX_COROUTINES];
1596 CoMutex lock;
1597 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001598} ImgConvertState;
1599
Peter Lieven2d9187b2017-02-28 13:40:07 +01001600static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1601 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001602{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001603 *src_cur = 0;
1604 *src_cur_offset = 0;
1605 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1606 *src_cur_offset += s->src_sectors[*src_cur];
1607 (*src_cur)++;
1608 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001609 }
1610}
1611
1612static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1613{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001614 int64_t ret, src_cur_offset;
1615 int n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001616
Peter Lieven2d9187b2017-02-28 13:40:07 +01001617 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001618
1619 assert(s->total_sectors > sector_num);
1620 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1621
1622 if (s->sector_next_status <= sector_num) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08001623 BlockDriverState *file;
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001624 if (s->target_has_backing) {
1625 ret = bdrv_get_block_status(blk_bs(s->src[src_cur]),
1626 sector_num - src_cur_offset,
1627 n, &n, &file);
1628 } else {
1629 ret = bdrv_get_block_status_above(blk_bs(s->src[src_cur]), NULL,
1630 sector_num - src_cur_offset,
1631 n, &n, &file);
1632 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001633 if (ret < 0) {
1634 return ret;
1635 }
1636
1637 if (ret & BDRV_BLOCK_ZERO) {
1638 s->status = BLK_ZERO;
1639 } else if (ret & BDRV_BLOCK_DATA) {
1640 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001641 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001642 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001643 }
1644
1645 s->sector_next_status = sector_num + n;
1646 }
1647
1648 n = MIN(n, s->sector_next_status - sector_num);
1649 if (s->status == BLK_DATA) {
1650 n = MIN(n, s->buf_sectors);
1651 }
1652
1653 /* We need to write complete clusters for compressed images, so if an
1654 * unallocated area is shorter than that, we must consider the whole
1655 * cluster allocated. */
1656 if (s->compressed) {
1657 if (n < s->cluster_sectors) {
1658 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1659 s->status = BLK_DATA;
1660 } else {
1661 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1662 }
1663 }
1664
1665 return n;
1666}
1667
Peter Lieven2d9187b2017-02-28 13:40:07 +01001668static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1669 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001670{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001671 int n, ret;
1672 QEMUIOVector qiov;
1673 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001674
Kevin Wolf690c7302015-03-19 13:33:32 +01001675 assert(nb_sectors <= s->buf_sectors);
1676 while (nb_sectors > 0) {
1677 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001678 int src_cur;
1679 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001680
1681 /* In the case of compression with multiple source files, we can get a
1682 * nb_sectors that spreads into the next part. So we must be able to
1683 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001684 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1685 blk = s->src[src_cur];
1686 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001687
Peter Lieven2d9187b2017-02-28 13:40:07 +01001688 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1689 iov.iov_base = buf;
1690 iov.iov_len = n << BDRV_SECTOR_BITS;
1691 qemu_iovec_init_external(&qiov, &iov, 1);
1692
1693 ret = blk_co_preadv(
1694 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1695 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001696 if (ret < 0) {
1697 return ret;
1698 }
1699
1700 sector_num += n;
1701 nb_sectors -= n;
1702 buf += n * BDRV_SECTOR_SIZE;
1703 }
1704
1705 return 0;
1706}
1707
Peter Lieven2d9187b2017-02-28 13:40:07 +01001708
1709static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1710 int nb_sectors, uint8_t *buf,
1711 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001712{
1713 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001714 QEMUIOVector qiov;
1715 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001716
1717 while (nb_sectors > 0) {
1718 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001719 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1720
Peter Lieven2d9187b2017-02-28 13:40:07 +01001721 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001722 case BLK_BACKING_FILE:
1723 /* If we have a backing file, leave clusters unallocated that are
1724 * unallocated in the source image, so that the backing file is
1725 * visible at the respective offset. */
1726 assert(s->target_has_backing);
1727 break;
1728
1729 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001730 /* If we're told to keep the target fully allocated (-S 0) or there
1731 * is real non-zero data, we must write it. Otherwise we can treat
1732 * it as zero sectors.
1733 * Compressed clusters need to be written as a whole, so in that
1734 * case we can only save the write if the buffer is completely
1735 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001736 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001737 (!s->compressed &&
1738 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1739 (s->compressed &&
1740 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001741 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001742 iov.iov_base = buf;
1743 iov.iov_len = n << BDRV_SECTOR_BITS;
1744 qemu_iovec_init_external(&qiov, &iov, 1);
1745
1746 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
Lidong Chendb933fb2017-04-27 10:58:27 +08001747 n << BDRV_SECTOR_BITS, &qiov, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001748 if (ret < 0) {
1749 return ret;
1750 }
1751 break;
1752 }
1753 /* fall-through */
1754
1755 case BLK_ZERO:
1756 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001757 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001758 break;
1759 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001760 ret = blk_co_pwrite_zeroes(s->target,
1761 sector_num << BDRV_SECTOR_BITS,
1762 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001763 if (ret < 0) {
1764 return ret;
1765 }
1766 break;
1767 }
1768
1769 sector_num += n;
1770 nb_sectors -= n;
1771 buf += n * BDRV_SECTOR_SIZE;
1772 }
1773
1774 return 0;
1775}
1776
Peter Lieven2d9187b2017-02-28 13:40:07 +01001777static void coroutine_fn convert_co_do_copy(void *opaque)
1778{
1779 ImgConvertState *s = opaque;
1780 uint8_t *buf = NULL;
1781 int ret, i;
1782 int index = -1;
1783
1784 for (i = 0; i < s->num_coroutines; i++) {
1785 if (s->co[i] == qemu_coroutine_self()) {
1786 index = i;
1787 break;
1788 }
1789 }
1790 assert(index >= 0);
1791
1792 s->running_coroutines++;
1793 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1794
1795 while (1) {
1796 int n;
1797 int64_t sector_num;
1798 enum ImgConvertBlockStatus status;
1799
1800 qemu_co_mutex_lock(&s->lock);
1801 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1802 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001803 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001804 }
1805 n = convert_iteration_sectors(s, s->sector_num);
1806 if (n < 0) {
1807 qemu_co_mutex_unlock(&s->lock);
1808 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001809 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001810 }
1811 /* save current sector and allocation status to local variables */
1812 sector_num = s->sector_num;
1813 status = s->status;
1814 if (!s->min_sparse && s->status == BLK_ZERO) {
1815 n = MIN(n, s->buf_sectors);
1816 }
1817 /* increment global sector counter so that other coroutines can
1818 * already continue reading beyond this request */
1819 s->sector_num += n;
1820 qemu_co_mutex_unlock(&s->lock);
1821
1822 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1823 s->allocated_done += n;
1824 qemu_progress_print(100.0 * s->allocated_done /
1825 s->allocated_sectors, 0);
1826 }
1827
1828 if (status == BLK_DATA) {
1829 ret = convert_co_read(s, sector_num, n, buf);
1830 if (ret < 0) {
1831 error_report("error while reading sector %" PRId64
1832 ": %s", sector_num, strerror(-ret));
1833 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001834 }
1835 } else if (!s->min_sparse && status == BLK_ZERO) {
1836 status = BLK_DATA;
1837 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1838 }
1839
1840 if (s->wr_in_order) {
1841 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03001842 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001843 s->wait_sector_num[index] = sector_num;
1844 qemu_coroutine_yield();
1845 }
1846 s->wait_sector_num[index] = -1;
1847 }
1848
Anton Nefedovb91127e2017-04-26 11:33:15 +03001849 if (s->ret == -EINPROGRESS) {
1850 ret = convert_co_write(s, sector_num, n, buf, status);
1851 if (ret < 0) {
1852 error_report("error while writing sector %" PRId64
1853 ": %s", sector_num, strerror(-ret));
1854 s->ret = ret;
1855 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001856 }
1857
1858 if (s->wr_in_order) {
1859 /* reenter the coroutine that might have waited
1860 * for this write to complete */
1861 s->wr_offs = sector_num + n;
1862 for (i = 0; i < s->num_coroutines; i++) {
1863 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1864 /*
1865 * A -> B -> A cannot occur because A has
1866 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1867 * B will never enter A during this time window.
1868 */
1869 qemu_coroutine_enter(s->co[i]);
1870 break;
1871 }
1872 }
1873 }
1874 }
1875
Peter Lieven2d9187b2017-02-28 13:40:07 +01001876 qemu_vfree(buf);
1877 s->co[index] = NULL;
1878 s->running_coroutines--;
1879 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1880 /* the convert job finished successfully */
1881 s->ret = 0;
1882 }
1883}
1884
Kevin Wolf690c7302015-03-19 13:33:32 +01001885static int convert_do_copy(ImgConvertState *s)
1886{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001887 int ret, i, n;
1888 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001889
1890 /* Check whether we have zero initialisation or can get it efficiently */
1891 s->has_zero_init = s->min_sparse && !s->target_has_backing
1892 ? bdrv_has_zero_init(blk_bs(s->target))
1893 : false;
1894
1895 if (!s->has_zero_init && !s->target_has_backing &&
1896 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1897 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001898 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001899 if (ret == 0) {
1900 s->has_zero_init = true;
1901 }
1902 }
1903
1904 /* Allocate buffer for copied data. For compressed images, only one cluster
1905 * can be copied at a time. */
1906 if (s->compressed) {
1907 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1908 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001909 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001910 }
1911 s->buf_sectors = s->cluster_sectors;
1912 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001913
Kevin Wolf690c7302015-03-19 13:33:32 +01001914 while (sector_num < s->total_sectors) {
1915 n = convert_iteration_sectors(s, sector_num);
1916 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001917 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001918 }
Max Reitzaad15de2016-03-24 23:33:57 +01001919 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1920 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001921 s->allocated_sectors += n;
1922 }
1923 sector_num += n;
1924 }
1925
1926 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001927 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001928 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001929
Peter Lieven2d9187b2017-02-28 13:40:07 +01001930 qemu_co_mutex_init(&s->lock);
1931 for (i = 0; i < s->num_coroutines; i++) {
1932 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1933 s->wait_sector_num[i] = -1;
1934 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001935 }
1936
Anton Nefedovb91127e2017-04-26 11:33:15 +03001937 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001938 main_loop_wait(false);
1939 }
1940
1941 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001942 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001943 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001944 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001945 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001946 }
1947 }
1948
Peter Lieven2d9187b2017-02-28 13:40:07 +01001949 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001950}
1951
bellardea2384d2004-08-01 21:59:26 +00001952static int img_convert(int argc, char **argv)
1953{
Peter Lieven9fd77f92017-04-21 11:11:55 +02001954 int c, bs_i, flags, src_flags = 0;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001955 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02001956 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1957 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001958 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00001959 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001960 BlockDriverState *out_bs;
1961 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08001962 QemuOptsList *create_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001963 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02001964 Error *local_err = NULL;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001965 bool writethrough, src_writethrough, quiet = false, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001966 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001967 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08001968 bool force_share = false;
bellardea2384d2004-08-01 21:59:26 +00001969
Peter Lieven9fd77f92017-04-21 11:11:55 +02001970 ImgConvertState s = (ImgConvertState) {
1971 /* Need at least 4k of zeros for sparse detection */
1972 .min_sparse = 8,
1973 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1974 .wr_in_order = true,
1975 .num_coroutines = 8,
1976 };
1977
bellardea2384d2004-08-01 21:59:26 +00001978 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001979 static const struct option long_options[] = {
1980 {"help", no_argument, 0, 'h'},
1981 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001982 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001983 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001984 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001985 {0, 0, 0, 0}
1986 };
Daniel P. Berrange6b4df542017-07-04 13:30:09 +01001987 c = getopt_long(argc, argv, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001988 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001989 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001990 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001991 }
bellardea2384d2004-08-01 21:59:26 +00001992 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001993 case ':':
1994 missing_argument(argv[optind - 1]);
1995 break;
Jes Sorensenef873942010-12-06 15:25:40 +01001996 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001997 unrecognized_option(argv[optind - 1]);
1998 break;
bellardea2384d2004-08-01 21:59:26 +00001999 case 'h':
2000 help();
2001 break;
2002 case 'f':
2003 fmt = optarg;
2004 break;
2005 case 'O':
2006 out_fmt = optarg;
2007 break;
thsf58c7b32008-06-05 21:53:49 +00002008 case 'B':
2009 out_baseimg = optarg;
2010 break;
bellardea2384d2004-08-01 21:59:26 +00002011 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002012 s.compressed = true;
bellardea2384d2004-08-01 21:59:26 +00002013 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002014 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01002015 if (!is_valid_option_list(optarg)) {
2016 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002017 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01002018 }
2019 if (!options) {
2020 options = g_strdup(optarg);
2021 } else {
2022 char *old_options = options;
2023 options = g_strdup_printf("%s,%s", options, optarg);
2024 g_free(old_options);
2025 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002026 break;
edison51ef6722010-09-21 19:58:41 -07002027 case 's':
2028 snapshot_name = optarg;
2029 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08002030 case 'l':
2031 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01002032 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2033 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08002034 if (!sn_opts) {
2035 error_report("Failed in parsing snapshot param '%s'",
2036 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002037 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002038 }
2039 } else {
2040 snapshot_name = optarg;
2041 }
2042 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002043 case 'S':
2044 {
2045 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002046
2047 sval = cvtnum(optarg);
2048 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02002049 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002050 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002051 }
2052
Peter Lieven9fd77f92017-04-21 11:11:55 +02002053 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002054 break;
2055 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002056 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002057 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002058 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002059 case 't':
2060 cache = optarg;
2061 break;
Max Reitz40055952014-07-22 22:58:42 +02002062 case 'T':
2063 src_cache = optarg;
2064 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002065 case 'q':
2066 quiet = true;
2067 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002068 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002069 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002070 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002071 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002072 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2073 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002074 error_report("Invalid number of coroutines. Allowed number of"
2075 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002076 goto fail_getopt;
2077 }
2078 break;
2079 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002080 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002081 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002082 case 'U':
2083 force_share = true;
2084 break;
Max Reitz3258b912017-04-26 15:46:47 +02002085 case OPTION_OBJECT: {
2086 QemuOpts *object_opts;
2087 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2088 optarg, true);
2089 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002090 goto fail_getopt;
2091 }
2092 break;
Max Reitz3258b912017-04-26 15:46:47 +02002093 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002094 case OPTION_IMAGE_OPTS:
2095 image_opts = true;
2096 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002097 case OPTION_TARGET_IMAGE_OPTS:
2098 tgt_image_opts = true;
2099 break;
bellardea2384d2004-08-01 21:59:26 +00002100 }
2101 }
ths3b46e622007-09-17 08:09:54 +00002102
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002103 if (!out_fmt && !tgt_image_opts) {
2104 out_fmt = "raw";
2105 }
2106
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002107 if (qemu_opts_foreach(&qemu_object_opts,
2108 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002109 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002110 goto fail_getopt;
2111 }
2112
Peter Lieven9fd77f92017-04-21 11:11:55 +02002113 if (!s.wr_in_order && s.compressed) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002114 error_report("Out of order write and compress are mutually exclusive");
Peter Lieven9fd77f92017-04-21 11:11:55 +02002115 goto fail_getopt;
2116 }
2117
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002118 if (tgt_image_opts && !skip_create) {
2119 error_report("--target-image-opts requires use of -n flag");
2120 goto fail_getopt;
2121 }
2122
Peter Lieven9fd77f92017-04-21 11:11:55 +02002123 s.src_num = argc - optind - 1;
2124 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2125
2126 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002127 if (out_fmt) {
2128 ret = print_block_option_help(out_filename, out_fmt);
2129 goto fail_getopt;
2130 } else {
2131 error_report("Option help requires a format be specified");
2132 goto fail_getopt;
2133 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002134 }
2135
2136 if (s.src_num < 1) {
2137 error_report("Must specify image file name");
2138 goto fail_getopt;
2139 }
2140
2141
Peter Lieven9fd77f92017-04-21 11:11:55 +02002142 /* ret is still -EINVAL until here */
2143 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2144 if (ret < 0) {
2145 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002146 goto fail_getopt;
2147 }
2148
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002149 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002150 if (quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002151 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002152 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002153 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002154 qemu_progress_print(0, 100);
2155
Peter Lieven9fd77f92017-04-21 11:11:55 +02002156 s.src = g_new0(BlockBackend *, s.src_num);
2157 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002158
Peter Lieven9fd77f92017-04-21 11:11:55 +02002159 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2160 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Fam Zheng335e9932017-05-03 00:35:39 +08002161 fmt, src_flags, src_writethrough, quiet,
2162 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002163 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002164 ret = -1;
2165 goto out;
2166 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002167 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2168 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002169 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002170 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002171 ret = -1;
2172 goto out;
2173 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002174 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002175 }
bellardea2384d2004-08-01 21:59:26 +00002176
Wenchao Xiaef806542013-12-04 17:10:57 +08002177 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002178 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002179 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2180 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2181 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002182 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002183 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002184 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002185 ret = -1;
2186 goto out;
2187 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002188
Peter Lieven9fd77f92017-04-21 11:11:55 +02002189 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2190 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002191 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002192 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002193 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002194 ret = -1;
2195 goto out;
edison51ef6722010-09-21 19:58:41 -07002196 }
2197
Max Reitz2e024cd2015-02-11 09:58:46 -05002198 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002199 /* Find driver and parse its options */
2200 drv = bdrv_find_format(out_fmt);
2201 if (!drv) {
2202 error_report("Unknown file format '%s'", out_fmt);
2203 ret = -1;
2204 goto out;
2205 }
2206
2207 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2208 if (!proto_drv) {
2209 error_report_err(local_err);
2210 ret = -1;
2211 goto out;
2212 }
2213
Max Reitz2e024cd2015-02-11 09:58:46 -05002214 if (!drv->create_opts) {
2215 error_report("Format driver '%s' does not support image creation",
2216 drv->format_name);
2217 ret = -1;
2218 goto out;
2219 }
Max Reitzf75613c2014-12-02 18:32:46 +01002220
Max Reitz2e024cd2015-02-11 09:58:46 -05002221 if (!proto_drv->create_opts) {
2222 error_report("Protocol driver '%s' does not support image creation",
2223 proto_drv->format_name);
2224 ret = -1;
2225 goto out;
2226 }
Max Reitzf75613c2014-12-02 18:32:46 +01002227
Max Reitz2e024cd2015-02-11 09:58:46 -05002228 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2229 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002230
Max Reitz2e024cd2015-02-11 09:58:46 -05002231 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002232 if (options) {
2233 qemu_opts_do_parse(opts, options, NULL, &local_err);
2234 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002235 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002236 ret = -1;
2237 goto out;
2238 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002239 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002240
Peter Lieven9fd77f92017-04-21 11:11:55 +02002241 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002242 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002243 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2244 if (ret < 0) {
2245 goto out;
2246 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002247 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002248
Kevin Wolfa18953f2010-10-14 15:46:04 +02002249 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002250 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002251 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002252 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002253 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002254 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002255
Max Reitz48758a82017-04-26 15:46:48 +02002256 if (s.src_num > 1 && out_baseimg) {
2257 error_report("Having a backing file for the target makes no sense when "
2258 "concatenating multiple input images");
2259 ret = -1;
2260 goto out;
2261 }
2262
Kevin Wolfefa84d42009-05-18 16:42:12 +02002263 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002264 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002265 bool encryption =
2266 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002267 const char *encryptfmt =
2268 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
Chunyan Liu83d05212014-06-05 17:20:51 +08002269 const char *preallocation =
2270 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002271
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002272 if (drv && !drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002273 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002274 ret = -1;
2275 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002276 }
2277
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002278 if (encryption || encryptfmt) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002279 error_report("Compression and encryption not supported at "
2280 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002281 ret = -1;
2282 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002283 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002284
Chunyan Liu83d05212014-06-05 17:20:51 +08002285 if (preallocation
2286 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002287 {
2288 error_report("Compression and preallocation not supported at "
2289 "the same time");
2290 ret = -1;
2291 goto out;
2292 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002293 }
2294
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002295 if (!skip_create) {
2296 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002297 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002298 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002299 error_reportf_err(local_err, "%s: error while converting %s: ",
2300 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002301 goto out;
bellardea2384d2004-08-01 21:59:26 +00002302 }
2303 }
ths3b46e622007-09-17 08:09:54 +00002304
Peter Lieven9fd77f92017-04-21 11:11:55 +02002305 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002306 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002307 if (ret < 0) {
2308 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002309 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002310 }
2311
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002312 if (skip_create) {
2313 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2314 flags, writethrough, quiet, false);
2315 } else {
2316 /* TODO ultimately we should allow --target-image-opts
2317 * to be used even when -n is not given.
2318 * That has to wait for bdrv_create to be improved
2319 * to allow filenames in option syntax
2320 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01002321 s.target = img_open_new_file(out_filename, opts, out_fmt,
2322 flags, writethrough, quiet, false);
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002323 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002324 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002325 ret = -1;
2326 goto out;
2327 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002328 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002329
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002330 if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2331 error_report("Compression not supported for this file format");
2332 ret = -1;
2333 goto out;
2334 }
2335
Eric Blake5def6b82016-06-23 16:37:19 -06002336 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002337 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2338 * as maximum. */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002339 s.buf_sectors = MIN(32768,
2340 MAX(s.buf_sectors,
2341 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2342 out_bs->bl.pdiscard_alignment >>
2343 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002344
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002345 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002346 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002347 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002348 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002349 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002350 ret = -1;
2351 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002352 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002353 error_report("output file is smaller than input file");
2354 ret = -1;
2355 goto out;
2356 }
2357 }
2358
Peter Lieven24f833c2013-11-27 11:07:07 +01002359 ret = bdrv_get_info(out_bs, &bdi);
2360 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002361 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002362 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002363 goto out;
2364 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002365 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002366 s.compressed = s.compressed || bdi.needs_compressed_writes;
2367 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Peter Lieven24f833c2013-11-27 11:07:07 +01002368 }
2369
Peter Lieven9fd77f92017-04-21 11:11:55 +02002370 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002371out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002372 if (!ret) {
2373 qemu_progress_print(100, 0);
2374 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002375 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002376 qemu_opts_del(opts);
2377 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002378 qemu_opts_del(sn_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002379 blk_unref(s.target);
2380 if (s.src) {
2381 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2382 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002383 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002384 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002385 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002386 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002387fail_getopt:
2388 g_free(options);
2389
Peter Lieven9fd77f92017-04-21 11:11:55 +02002390 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002391}
2392
bellard57d1a2b2004-08-03 21:15:11 +00002393
bellardfaea38e2006-08-05 21:31:00 +00002394static void dump_snapshots(BlockDriverState *bs)
2395{
2396 QEMUSnapshotInfo *sn_tab, *sn;
2397 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002398
2399 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2400 if (nb_sns <= 0)
2401 return;
2402 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002403 bdrv_snapshot_dump(fprintf, stdout, NULL);
2404 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002405 for(i = 0; i < nb_sns; i++) {
2406 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002407 bdrv_snapshot_dump(fprintf, stdout, sn);
2408 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002409 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002410 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002411}
2412
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002413static void dump_json_image_info_list(ImageInfoList *list)
2414{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002415 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002416 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002417 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002418
2419 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2420 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002421 str = qobject_to_json_pretty(obj);
2422 assert(str != NULL);
2423 printf("%s\n", qstring_get_str(str));
2424 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002425 visit_free(v);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002426 QDECREF(str);
2427}
2428
Benoît Canetc054b3f2012-09-05 13:09:02 +02002429static void dump_json_image_info(ImageInfo *info)
2430{
Benoît Canetc054b3f2012-09-05 13:09:02 +02002431 QString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002432 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002433 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002434
2435 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2436 visit_complete(v, &obj);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002437 str = qobject_to_json_pretty(obj);
2438 assert(str != NULL);
2439 printf("%s\n", qstring_get_str(str));
2440 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002441 visit_free(v);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002442 QDECREF(str);
2443}
2444
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002445static void dump_human_image_info_list(ImageInfoList *list)
2446{
2447 ImageInfoList *elem;
2448 bool delim = false;
2449
2450 for (elem = list; elem; elem = elem->next) {
2451 if (delim) {
2452 printf("\n");
2453 }
2454 delim = true;
2455
Wenchao Xia5b917042013-05-25 11:09:45 +08002456 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002457 }
2458}
2459
2460static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2461{
2462 return strcmp(a, b) == 0;
2463}
2464
2465/**
2466 * Open an image file chain and return an ImageInfoList
2467 *
2468 * @filename: topmost image filename
2469 * @fmt: topmost image format (may be NULL to autodetect)
2470 * @chain: true - enumerate entire backing file chain
2471 * false - only topmost image file
2472 *
2473 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2474 * image file. If there was an error a message will have been printed to
2475 * stderr.
2476 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002477static ImageInfoList *collect_image_info_list(bool image_opts,
2478 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002479 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002480 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002481{
2482 ImageInfoList *head = NULL;
2483 ImageInfoList **last = &head;
2484 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002485 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002486
2487 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2488
2489 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002490 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002491 BlockDriverState *bs;
2492 ImageInfo *info;
2493 ImageInfoList *elem;
2494
2495 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2496 error_report("Backing file '%s' creates an infinite loop.",
2497 filename);
2498 goto err;
2499 }
2500 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2501
Max Reitzefaa7c42016-03-16 19:54:38 +01002502 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002503 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2504 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002505 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002506 goto err;
2507 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002508 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002509
Wenchao Xia43526ec2013-06-06 12:27:58 +08002510 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002511 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002512 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002513 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002514 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002515 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002516
2517 elem = g_new0(ImageInfoList, 1);
2518 elem->value = info;
2519 *last = elem;
2520 last = &elem->next;
2521
Markus Armbruster26f54e92014-10-07 13:59:04 +02002522 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002523
2524 filename = fmt = NULL;
2525 if (chain) {
2526 if (info->has_full_backing_filename) {
2527 filename = info->full_backing_filename;
2528 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002529 error_report("Could not determine absolute backing filename,"
2530 " but backing filename '%s' present",
2531 info->backing_filename);
2532 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002533 }
2534 if (info->has_backing_filename_format) {
2535 fmt = info->backing_filename_format;
2536 }
2537 }
2538 }
2539 g_hash_table_destroy(filenames);
2540 return head;
2541
2542err:
2543 qapi_free_ImageInfoList(head);
2544 g_hash_table_destroy(filenames);
2545 return NULL;
2546}
2547
Benoît Canetc054b3f2012-09-05 13:09:02 +02002548static int img_info(int argc, char **argv)
2549{
2550 int c;
2551 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002552 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002553 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002554 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002555 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002556 bool force_share = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002557
bellardea2384d2004-08-01 21:59:26 +00002558 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002559 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002560 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002561 int option_index = 0;
2562 static const struct option long_options[] = {
2563 {"help", no_argument, 0, 'h'},
2564 {"format", required_argument, 0, 'f'},
2565 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002566 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002567 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002568 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002569 {"force-share", no_argument, 0, 'U'},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002570 {0, 0, 0, 0}
2571 };
Fam Zheng335e9932017-05-03 00:35:39 +08002572 c = getopt_long(argc, argv, ":f:hU",
Benoît Canetc054b3f2012-09-05 13:09:02 +02002573 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002574 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002575 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002576 }
bellardea2384d2004-08-01 21:59:26 +00002577 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002578 case ':':
2579 missing_argument(argv[optind - 1]);
2580 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002581 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002582 unrecognized_option(argv[optind - 1]);
2583 break;
bellardea2384d2004-08-01 21:59:26 +00002584 case 'h':
2585 help();
2586 break;
2587 case 'f':
2588 fmt = optarg;
2589 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002590 case 'U':
2591 force_share = true;
2592 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002593 case OPTION_OUTPUT:
2594 output = optarg;
2595 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002596 case OPTION_BACKING_CHAIN:
2597 chain = true;
2598 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002599 case OPTION_OBJECT: {
2600 QemuOpts *opts;
2601 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2602 optarg, true);
2603 if (!opts) {
2604 return 1;
2605 }
2606 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002607 case OPTION_IMAGE_OPTS:
2608 image_opts = true;
2609 break;
bellardea2384d2004-08-01 21:59:26 +00002610 }
2611 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002612 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002613 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002614 }
bellardea2384d2004-08-01 21:59:26 +00002615 filename = argv[optind++];
2616
Benoît Canetc054b3f2012-09-05 13:09:02 +02002617 if (output && !strcmp(output, "json")) {
2618 output_format = OFORMAT_JSON;
2619 } else if (output && !strcmp(output, "human")) {
2620 output_format = OFORMAT_HUMAN;
2621 } else if (output) {
2622 error_report("--output must be used with human or json as argument.");
2623 return 1;
2624 }
2625
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002626 if (qemu_opts_foreach(&qemu_object_opts,
2627 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002628 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002629 return 1;
2630 }
2631
Fam Zheng335e9932017-05-03 00:35:39 +08002632 list = collect_image_info_list(image_opts, filename, fmt, chain,
2633 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002634 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002635 return 1;
2636 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002637
Benoît Canetc054b3f2012-09-05 13:09:02 +02002638 switch (output_format) {
2639 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002640 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002641 break;
2642 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002643 if (chain) {
2644 dump_json_image_info_list(list);
2645 } else {
2646 dump_json_image_info(list->value);
2647 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002648 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002649 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002650
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002651 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002652 return 0;
2653}
2654
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002655static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2656 MapEntry *next)
2657{
2658 switch (output_format) {
2659 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002660 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002661 error_report("File contains external, encrypted or compressed clusters.");
2662 exit(1);
2663 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002664 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002665 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002666 e->start, e->length,
2667 e->has_offset ? e->offset : 0,
2668 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002669 }
2670 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2671 * Modify the flags here to allow more coalescing.
2672 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002673 if (next && (!next->data || next->zero)) {
2674 next->data = false;
2675 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002676 }
2677 break;
2678 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002679 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2680 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002681 (e->start == 0 ? "[" : ",\n"),
2682 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002683 e->zero ? "true" : "false",
2684 e->data ? "true" : "false");
2685 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002686 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002687 }
2688 putchar('}');
2689
2690 if (!next) {
2691 printf("]\n");
2692 }
2693 break;
2694 }
2695}
2696
2697static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2698 int nb_sectors, MapEntry *e)
2699{
2700 int64_t ret;
2701 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002702 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002703 bool has_offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002704
2705 /* As an optimization, we could cache the current range of unallocated
2706 * clusters in each file of the chain, and avoid querying the same
2707 * range repeatedly.
2708 */
2709
2710 depth = 0;
2711 for (;;) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08002712 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2713 &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002714 if (ret < 0) {
2715 return ret;
2716 }
2717 assert(nb_sectors);
2718 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2719 break;
2720 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002721 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002722 if (bs == NULL) {
2723 ret = 0;
2724 break;
2725 }
2726
2727 depth++;
2728 }
2729
John Snow28756452016-02-05 13:12:33 -05002730 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2731
2732 *e = (MapEntry) {
2733 .start = sector_num * BDRV_SECTOR_SIZE,
2734 .length = nb_sectors * BDRV_SECTOR_SIZE,
2735 .data = !!(ret & BDRV_BLOCK_DATA),
2736 .zero = !!(ret & BDRV_BLOCK_ZERO),
2737 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2738 .has_offset = has_offset,
2739 .depth = depth,
2740 .has_filename = file && has_offset,
2741 .filename = file && has_offset ? file->filename : NULL,
2742 };
2743
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002744 return 0;
2745}
2746
Fam Zheng16b0d552016-01-26 11:59:02 +08002747static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2748{
2749 if (curr->length == 0) {
2750 return false;
2751 }
2752 if (curr->zero != next->zero ||
2753 curr->data != next->data ||
2754 curr->depth != next->depth ||
2755 curr->has_filename != next->has_filename ||
2756 curr->has_offset != next->has_offset) {
2757 return false;
2758 }
2759 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2760 return false;
2761 }
2762 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2763 return false;
2764 }
2765 return true;
2766}
2767
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002768static int img_map(int argc, char **argv)
2769{
2770 int c;
2771 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002772 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002773 BlockDriverState *bs;
2774 const char *filename, *fmt, *output;
2775 int64_t length;
2776 MapEntry curr = { .length = 0 }, next;
2777 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002778 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002779 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002780
2781 fmt = NULL;
2782 output = NULL;
2783 for (;;) {
2784 int option_index = 0;
2785 static const struct option long_options[] = {
2786 {"help", no_argument, 0, 'h'},
2787 {"format", required_argument, 0, 'f'},
2788 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002789 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002790 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002791 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002792 {0, 0, 0, 0}
2793 };
Fam Zheng335e9932017-05-03 00:35:39 +08002794 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002795 long_options, &option_index);
2796 if (c == -1) {
2797 break;
2798 }
2799 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002800 case ':':
2801 missing_argument(argv[optind - 1]);
2802 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002803 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002804 unrecognized_option(argv[optind - 1]);
2805 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002806 case 'h':
2807 help();
2808 break;
2809 case 'f':
2810 fmt = optarg;
2811 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002812 case 'U':
2813 force_share = true;
2814 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002815 case OPTION_OUTPUT:
2816 output = optarg;
2817 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002818 case OPTION_OBJECT: {
2819 QemuOpts *opts;
2820 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2821 optarg, true);
2822 if (!opts) {
2823 return 1;
2824 }
2825 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002826 case OPTION_IMAGE_OPTS:
2827 image_opts = true;
2828 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002829 }
2830 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002831 if (optind != argc - 1) {
2832 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002833 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002834 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002835
2836 if (output && !strcmp(output, "json")) {
2837 output_format = OFORMAT_JSON;
2838 } else if (output && !strcmp(output, "human")) {
2839 output_format = OFORMAT_HUMAN;
2840 } else if (output) {
2841 error_report("--output must be used with human or json as argument.");
2842 return 1;
2843 }
2844
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002845 if (qemu_opts_foreach(&qemu_object_opts,
2846 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002847 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002848 return 1;
2849 }
2850
Fam Zheng335e9932017-05-03 00:35:39 +08002851 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002852 if (!blk) {
2853 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002854 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002855 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002856
2857 if (output_format == OFORMAT_HUMAN) {
2858 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2859 }
2860
Max Reitzf1d3cd72015-02-05 13:58:18 -05002861 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002862 while (curr.start + curr.length < length) {
2863 int64_t nsectors_left;
2864 int64_t sector_num;
2865 int n;
2866
2867 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2868
2869 /* Probe up to 1 GiB at a time. */
2870 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2871 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2872 ret = get_block_status(bs, sector_num, n, &next);
2873
2874 if (ret < 0) {
2875 error_report("Could not read file metadata: %s", strerror(-ret));
2876 goto out;
2877 }
2878
Fam Zheng16b0d552016-01-26 11:59:02 +08002879 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002880 curr.length += next.length;
2881 continue;
2882 }
2883
2884 if (curr.length > 0) {
2885 dump_map_entry(output_format, &curr, &next);
2886 }
2887 curr = next;
2888 }
2889
2890 dump_map_entry(output_format, &curr, NULL);
2891
2892out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002893 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002894 return ret < 0;
2895}
2896
aliguorif7b4a942009-01-07 17:40:15 +00002897#define SNAPSHOT_LIST 1
2898#define SNAPSHOT_CREATE 2
2899#define SNAPSHOT_APPLY 3
2900#define SNAPSHOT_DELETE 4
2901
Stuart Brady153859b2009-06-07 00:42:17 +01002902static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002903{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002904 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002905 BlockDriverState *bs;
2906 QEMUSnapshotInfo sn;
2907 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002908 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002909 int action = 0;
2910 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002911 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002912 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002913 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002914 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00002915
Kevin Wolfce099542016-03-15 13:01:04 +01002916 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002917 /* Parse commandline parameters */
2918 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002919 static const struct option long_options[] = {
2920 {"help", no_argument, 0, 'h'},
2921 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002922 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002923 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002924 {0, 0, 0, 0}
2925 };
Fam Zheng335e9932017-05-03 00:35:39 +08002926 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002927 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002928 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002929 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002930 }
aliguorif7b4a942009-01-07 17:40:15 +00002931 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002932 case ':':
2933 missing_argument(argv[optind - 1]);
2934 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002935 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002936 unrecognized_option(argv[optind - 1]);
2937 break;
aliguorif7b4a942009-01-07 17:40:15 +00002938 case 'h':
2939 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002940 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002941 case 'l':
2942 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002943 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002944 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002945 }
2946 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002947 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002948 break;
2949 case 'a':
2950 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002951 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002952 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002953 }
2954 action = SNAPSHOT_APPLY;
2955 snapshot_name = optarg;
2956 break;
2957 case 'c':
2958 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002959 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002960 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002961 }
2962 action = SNAPSHOT_CREATE;
2963 snapshot_name = optarg;
2964 break;
2965 case 'd':
2966 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002967 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002968 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002969 }
2970 action = SNAPSHOT_DELETE;
2971 snapshot_name = optarg;
2972 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002973 case 'q':
2974 quiet = true;
2975 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002976 case 'U':
2977 force_share = true;
2978 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002979 case OPTION_OBJECT: {
2980 QemuOpts *opts;
2981 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2982 optarg, true);
2983 if (!opts) {
2984 return 1;
2985 }
2986 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002987 case OPTION_IMAGE_OPTS:
2988 image_opts = true;
2989 break;
aliguorif7b4a942009-01-07 17:40:15 +00002990 }
2991 }
2992
Kevin Wolffc11eb22013-08-05 10:53:04 +02002993 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002994 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002995 }
aliguorif7b4a942009-01-07 17:40:15 +00002996 filename = argv[optind++];
2997
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002998 if (qemu_opts_foreach(&qemu_object_opts,
2999 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003000 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003001 return 1;
3002 }
3003
aliguorif7b4a942009-01-07 17:40:15 +00003004 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08003005 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
3006 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003007 if (!blk) {
3008 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003009 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003010 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00003011
3012 /* Perform the requested action */
3013 switch(action) {
3014 case SNAPSHOT_LIST:
3015 dump_snapshots(bs);
3016 break;
3017
3018 case SNAPSHOT_CREATE:
3019 memset(&sn, 0, sizeof(sn));
3020 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
3021
3022 qemu_gettimeofday(&tv);
3023 sn.date_sec = tv.tv_sec;
3024 sn.date_nsec = tv.tv_usec * 1000;
3025
3026 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003027 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003028 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003029 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003030 }
aliguorif7b4a942009-01-07 17:40:15 +00003031 break;
3032
3033 case SNAPSHOT_APPLY:
3034 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003035 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003036 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003037 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003038 }
aliguorif7b4a942009-01-07 17:40:15 +00003039 break;
3040
3041 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003042 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003043 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003044 error_reportf_err(err, "Could not delete snapshot '%s': ",
3045 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003046 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003047 }
aliguorif7b4a942009-01-07 17:40:15 +00003048 break;
3049 }
3050
3051 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003052 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003053 if (ret) {
3054 return 1;
3055 }
Stuart Brady153859b2009-06-07 00:42:17 +01003056 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003057}
3058
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003059static int img_rebase(int argc, char **argv)
3060{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003061 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003062 uint8_t *buf_old = NULL;
3063 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05003064 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003065 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003066 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3067 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003068 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003069 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003070 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003071 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003072 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003073 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003074 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003075
3076 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003077 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003078 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003079 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003080 out_baseimg = NULL;
3081 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003082 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003083 static const struct option long_options[] = {
3084 {"help", no_argument, 0, 'h'},
3085 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003086 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003087 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003088 {0, 0, 0, 0}
3089 };
Fam Zheng335e9932017-05-03 00:35:39 +08003090 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003091 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003092 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003093 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003094 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003095 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003096 case ':':
3097 missing_argument(argv[optind - 1]);
3098 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003099 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003100 unrecognized_option(argv[optind - 1]);
3101 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003102 case 'h':
3103 help();
3104 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003105 case 'f':
3106 fmt = optarg;
3107 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003108 case 'F':
3109 out_basefmt = optarg;
3110 break;
3111 case 'b':
3112 out_baseimg = optarg;
3113 break;
3114 case 'u':
3115 unsafe = 1;
3116 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003117 case 'p':
3118 progress = 1;
3119 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003120 case 't':
3121 cache = optarg;
3122 break;
Max Reitz40055952014-07-22 22:58:42 +02003123 case 'T':
3124 src_cache = optarg;
3125 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003126 case 'q':
3127 quiet = true;
3128 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003129 case OPTION_OBJECT: {
3130 QemuOpts *opts;
3131 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3132 optarg, true);
3133 if (!opts) {
3134 return 1;
3135 }
3136 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003137 case OPTION_IMAGE_OPTS:
3138 image_opts = true;
3139 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003140 case 'U':
3141 force_share = true;
3142 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003143 }
3144 }
3145
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003146 if (quiet) {
3147 progress = 0;
3148 }
3149
Fam Zhengac1307a2014-04-22 13:36:11 +08003150 if (optind != argc - 1) {
3151 error_exit("Expecting one image file name");
3152 }
3153 if (!unsafe && !out_baseimg) {
3154 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003155 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003156 filename = argv[optind++];
3157
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003158 if (qemu_opts_foreach(&qemu_object_opts,
3159 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003160 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003161 return 1;
3162 }
3163
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003164 qemu_progress_init(progress, 2.0);
3165 qemu_progress_print(0, 100);
3166
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003167 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003168 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003169 if (ret < 0) {
3170 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003171 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003172 }
3173
Kevin Wolfce099542016-03-15 13:01:04 +01003174 src_flags = 0;
3175 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003176 if (ret < 0) {
3177 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003178 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003179 }
3180
Kevin Wolfce099542016-03-15 13:01:04 +01003181 /* The source files are opened read-only, don't care about WCE */
3182 assert((src_flags & BDRV_O_RDWR) == 0);
3183 (void) src_writethrough;
3184
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003185 /*
3186 * Open the images.
3187 *
3188 * Ignore the old backing file for unsafe rebase in case we want to correct
3189 * the reference to a renamed or moved backing file.
3190 */
Fam Zheng335e9932017-05-03 00:35:39 +08003191 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3192 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003193 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003194 ret = -1;
3195 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003196 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003197 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003198
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003199 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003200 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003201 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003202 ret = -1;
3203 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003204 }
3205 }
3206
3207 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003208 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003209 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003210 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003211
Max Reitz644483d2015-02-05 13:58:17 -05003212 if (bs->backing_format[0] != '\0') {
3213 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003214 qdict_put_str(options, "driver", bs->backing_format);
Max Reitz644483d2015-02-05 13:58:17 -05003215 }
3216
Fam Zheng335e9932017-05-03 00:35:39 +08003217 if (force_share) {
3218 if (!options) {
3219 options = qdict_new();
3220 }
Eric Blake579cf1d2017-05-15 14:54:39 -05003221 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +08003222 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003223 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003224 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003225 options, src_flags, &local_err);
3226 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003227 error_reportf_err(local_err,
3228 "Could not open old backing file '%s': ",
3229 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003230 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003231 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003232 }
Max Reitz644483d2015-02-05 13:58:17 -05003233
Alex Bligha6166732012-10-16 13:46:18 +01003234 if (out_baseimg[0]) {
Fam Zheng335e9932017-05-03 00:35:39 +08003235 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003236 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003237 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003238 }
3239 if (force_share) {
3240 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003241 }
3242
Max Reitzefaa7c42016-03-16 19:54:38 +01003243 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003244 options, src_flags, &local_err);
3245 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003246 error_reportf_err(local_err,
3247 "Could not open new backing file '%s': ",
3248 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003249 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003250 goto out;
3251 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003252 }
3253 }
3254
3255 /*
3256 * Check each unallocated cluster in the COW file. If it is unallocated,
3257 * accesses go to the backing file. We must therefore compare this cluster
3258 * in the old and new backing file, and if they differ we need to copy it
3259 * from the old backing file into the COW file.
3260 *
3261 * If qemu-img crashes during this step, no harm is done. The content of
3262 * the image is the same as the original one at any time.
3263 */
3264 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003265 int64_t num_sectors;
3266 int64_t old_backing_num_sectors;
3267 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003268 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003269 int n;
Eric Blaked6a644b2017-07-07 07:44:57 -05003270 int64_t count;
Kevin Wolf1f710492012-10-12 14:29:18 +02003271 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003272
Max Reitzf1d3cd72015-02-05 13:58:18 -05003273 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3274 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003275
Max Reitzf1d3cd72015-02-05 13:58:18 -05003276 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003277 if (num_sectors < 0) {
3278 error_report("Could not get size of '%s': %s",
3279 filename, strerror(-num_sectors));
3280 ret = -1;
3281 goto out;
3282 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003283 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003284 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003285 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003286
3287 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3288 error_report("Could not get size of '%s': %s",
3289 backing_name, strerror(-old_backing_num_sectors));
3290 ret = -1;
3291 goto out;
3292 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003293 if (blk_new_backing) {
3294 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003295 if (new_backing_num_sectors < 0) {
3296 error_report("Could not get size of '%s': %s",
3297 out_baseimg, strerror(-new_backing_num_sectors));
3298 ret = -1;
3299 goto out;
3300 }
Alex Bligha6166732012-10-16 13:46:18 +01003301 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003302
Kevin Wolf1f710492012-10-12 14:29:18 +02003303 if (num_sectors != 0) {
3304 local_progress = (float)100 /
3305 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
3306 }
3307
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003308 for (sector = 0; sector < num_sectors; sector += n) {
3309
3310 /* How many sectors can we handle with the next read? */
3311 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
3312 n = (IO_BUF_SIZE / 512);
3313 } else {
3314 n = num_sectors - sector;
3315 }
3316
3317 /* If the cluster is allocated, we don't need to take action */
Eric Blaked6a644b2017-07-07 07:44:57 -05003318 ret = bdrv_is_allocated(bs, sector << BDRV_SECTOR_BITS,
3319 n << BDRV_SECTOR_BITS, &count);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003320 if (ret < 0) {
3321 error_report("error while reading image metadata: %s",
3322 strerror(-ret));
3323 goto out;
3324 }
Eric Blaked6a644b2017-07-07 07:44:57 -05003325 /* TODO relax this once bdrv_is_allocated does not enforce
3326 * sector alignment */
3327 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
3328 n = count >> BDRV_SECTOR_BITS;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003329 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003330 continue;
3331 }
3332
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003333 /*
3334 * Read old and new backing file and take into consideration that
3335 * backing files may be smaller than the COW image.
3336 */
3337 if (sector >= old_backing_num_sectors) {
3338 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3339 } else {
3340 if (sector + n > old_backing_num_sectors) {
3341 n = old_backing_num_sectors - sector;
3342 }
3343
Eric Blake91669202016-05-06 10:26:43 -06003344 ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS,
3345 buf_old, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003346 if (ret < 0) {
3347 error_report("error while reading from old backing file");
3348 goto out;
3349 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003350 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003351
Max Reitzf1d3cd72015-02-05 13:58:18 -05003352 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003353 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3354 } else {
3355 if (sector + n > new_backing_num_sectors) {
3356 n = new_backing_num_sectors - sector;
3357 }
3358
Eric Blake91669202016-05-06 10:26:43 -06003359 ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS,
3360 buf_new, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003361 if (ret < 0) {
3362 error_report("error while reading from new backing file");
3363 goto out;
3364 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003365 }
3366
3367 /* If they differ, we need to write to the COW file */
3368 uint64_t written = 0;
3369
3370 while (written < n) {
3371 int pnum;
3372
3373 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003374 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003375 {
Eric Blake91669202016-05-06 10:26:43 -06003376 ret = blk_pwrite(blk,
3377 (sector + written) << BDRV_SECTOR_BITS,
3378 buf_old + written * 512,
3379 pnum << BDRV_SECTOR_BITS, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003380 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003381 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003382 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003383 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003384 }
3385 }
3386
3387 written += pnum;
3388 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003389 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003390 }
3391 }
3392
3393 /*
3394 * Change the backing file. All clusters that are different from the old
3395 * backing file are overwritten in the COW file now, so the visible content
3396 * doesn't change when we switch the backing file.
3397 */
Alex Bligha6166732012-10-16 13:46:18 +01003398 if (out_baseimg && *out_baseimg) {
3399 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3400 } else {
3401 ret = bdrv_change_backing_file(bs, NULL, NULL);
3402 }
3403
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003404 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003405 error_report("Could not change the backing file to '%s': No "
3406 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003407 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003408 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003409 out_baseimg, strerror(-ret));
3410 }
3411
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003412 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003413 /*
3414 * TODO At this point it is possible to check if any clusters that are
3415 * allocated in the COW file are the same in the backing file. If so, they
3416 * could be dropped from the COW file. Don't do this before switching the
3417 * backing file, in case of a crash this would lead to corruption.
3418 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003419out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003420 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003421 /* Cleanup */
3422 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003423 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003424 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003425 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003426 qemu_vfree(buf_old);
3427 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003428
Markus Armbruster26f54e92014-10-07 13:59:04 +02003429 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003430 if (ret) {
3431 return 1;
3432 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003433 return 0;
3434}
3435
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003436static int img_resize(int argc, char **argv)
3437{
Markus Armbruster6750e792015-02-12 17:43:08 +01003438 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003439 int c, ret, relative;
3440 const char *filename, *fmt, *size;
3441 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003442 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003443 BlockBackend *blk = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003444 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003445
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003446 static QemuOptsList resize_options = {
3447 .name = "resize_options",
3448 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3449 .desc = {
3450 {
3451 .name = BLOCK_OPT_SIZE,
3452 .type = QEMU_OPT_SIZE,
3453 .help = "Virtual disk size"
3454 }, {
3455 /* end of list */
3456 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003457 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003458 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003459 bool image_opts = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003460
Kevin Wolfe80fec72011-04-29 10:58:12 +02003461 /* Remove size from argv manually so that negative numbers are not treated
3462 * as options by getopt. */
3463 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003464 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003465 return 1;
3466 }
3467
3468 size = argv[--argc];
3469
3470 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003471 fmt = NULL;
3472 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003473 static const struct option long_options[] = {
3474 {"help", no_argument, 0, 'h'},
3475 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003476 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003477 {0, 0, 0, 0}
3478 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003479 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003480 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003481 if (c == -1) {
3482 break;
3483 }
3484 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003485 case ':':
3486 missing_argument(argv[optind - 1]);
3487 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003488 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003489 unrecognized_option(argv[optind - 1]);
3490 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003491 case 'h':
3492 help();
3493 break;
3494 case 'f':
3495 fmt = optarg;
3496 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003497 case 'q':
3498 quiet = true;
3499 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003500 case OPTION_OBJECT: {
3501 QemuOpts *opts;
3502 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3503 optarg, true);
3504 if (!opts) {
3505 return 1;
3506 }
3507 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003508 case OPTION_IMAGE_OPTS:
3509 image_opts = true;
3510 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003511 }
3512 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003513 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003514 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003515 }
3516 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003517
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003518 if (qemu_opts_foreach(&qemu_object_opts,
3519 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003520 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003521 return 1;
3522 }
3523
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003524 /* Choose grow, shrink, or absolute resize mode */
3525 switch (size[0]) {
3526 case '+':
3527 relative = 1;
3528 size++;
3529 break;
3530 case '-':
3531 relative = -1;
3532 size++;
3533 break;
3534 default:
3535 relative = 0;
3536 break;
3537 }
3538
3539 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003540 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003541 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003542 if (err) {
3543 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003544 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003545 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003546 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003547 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003548 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3549 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003550
Max Reitzefaa7c42016-03-16 19:54:38 +01003551 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003552 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3553 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003554 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003555 ret = -1;
3556 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003557 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003558
3559 if (relative) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003560 total_size = blk_getlength(blk) + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003561 } else {
3562 total_size = n;
3563 }
3564 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003565 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003566 ret = -1;
3567 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003568 }
3569
Max Reitzed3d2ec2017-03-28 22:51:27 +02003570 ret = blk_truncate(blk, total_size, &err);
3571 if (!ret) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003572 qprintf(quiet, "Image resized.\n");
Max Reitzed3d2ec2017-03-28 22:51:27 +02003573 } else {
3574 error_report_err(err);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003575 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003576out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003577 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003578 if (ret) {
3579 return 1;
3580 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003581 return 0;
3582}
3583
Max Reitz76a3a342014-10-27 11:12:51 +01003584static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003585 int64_t offset, int64_t total_work_size,
3586 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003587{
3588 qemu_progress_print(100.f * offset / total_work_size, 0);
3589}
3590
Max Reitz6f176b42013-09-03 10:09:50 +02003591static int img_amend(int argc, char **argv)
3592{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003593 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003594 int c, ret = 0;
3595 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003596 QemuOptsList *create_opts = NULL;
3597 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003598 const char *fmt = NULL, *filename, *cache;
3599 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003600 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003601 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003602 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003603 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003604 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003605
Max Reitzbd39e6e2014-07-22 22:58:43 +02003606 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003607 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003608 static const struct option long_options[] = {
3609 {"help", no_argument, 0, 'h'},
3610 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003611 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003612 {0, 0, 0, 0}
3613 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003614 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003615 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003616 if (c == -1) {
3617 break;
3618 }
3619
3620 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003621 case ':':
3622 missing_argument(argv[optind - 1]);
3623 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003624 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003625 unrecognized_option(argv[optind - 1]);
3626 break;
3627 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003628 help();
3629 break;
3630 case 'o':
3631 if (!is_valid_option_list(optarg)) {
3632 error_report("Invalid option list: %s", optarg);
3633 ret = -1;
3634 goto out_no_progress;
3635 }
3636 if (!options) {
3637 options = g_strdup(optarg);
3638 } else {
3639 char *old_options = options;
3640 options = g_strdup_printf("%s,%s", options, optarg);
3641 g_free(old_options);
3642 }
3643 break;
3644 case 'f':
3645 fmt = optarg;
3646 break;
3647 case 't':
3648 cache = optarg;
3649 break;
3650 case 'p':
3651 progress = true;
3652 break;
3653 case 'q':
3654 quiet = true;
3655 break;
3656 case OPTION_OBJECT:
3657 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3658 optarg, true);
3659 if (!opts) {
3660 ret = -1;
3661 goto out_no_progress;
3662 }
3663 break;
3664 case OPTION_IMAGE_OPTS:
3665 image_opts = true;
3666 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003667 }
3668 }
3669
Max Reitz6f176b42013-09-03 10:09:50 +02003670 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003671 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003672 }
3673
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003674 if (qemu_opts_foreach(&qemu_object_opts,
3675 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003676 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003677 ret = -1;
3678 goto out_no_progress;
3679 }
3680
Max Reitz76a3a342014-10-27 11:12:51 +01003681 if (quiet) {
3682 progress = false;
3683 }
3684 qemu_progress_init(progress, 1.0);
3685
Kevin Wolfa283cb62014-02-21 16:24:07 +01003686 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3687 if (fmt && has_help_option(options)) {
3688 /* If a format is explicitly specified (and possibly no filename is
3689 * given), print option help here */
3690 ret = print_block_option_help(filename, fmt);
3691 goto out;
3692 }
3693
3694 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003695 error_report("Expecting one image file name");
3696 ret = -1;
3697 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003698 }
Max Reitz6f176b42013-09-03 10:09:50 +02003699
Kevin Wolfce099542016-03-15 13:01:04 +01003700 flags = BDRV_O_RDWR;
3701 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003702 if (ret < 0) {
3703 error_report("Invalid cache option: %s", cache);
3704 goto out;
3705 }
3706
Fam Zheng335e9932017-05-03 00:35:39 +08003707 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3708 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003709 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003710 ret = -1;
3711 goto out;
3712 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003713 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003714
3715 fmt = bs->drv->format_name;
3716
Kevin Wolf626f84f2014-02-21 16:24:06 +01003717 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003718 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003719 ret = print_block_option_help(filename, fmt);
3720 goto out;
3721 }
3722
Max Reitzb2439d22014-12-02 18:32:47 +01003723 if (!bs->drv->create_opts) {
3724 error_report("Format driver '%s' does not support any options to amend",
3725 fmt);
3726 ret = -1;
3727 goto out;
3728 }
3729
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003730 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003731 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003732 qemu_opts_do_parse(opts, options, NULL, &err);
3733 if (err) {
3734 error_report_err(err);
3735 ret = -1;
3736 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003737 }
3738
Max Reitz76a3a342014-10-27 11:12:51 +01003739 /* In case the driver does not call amend_status_cb() */
3740 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003741 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003742 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003743 if (ret < 0) {
3744 error_report("Error while amending options: %s", strerror(-ret));
3745 goto out;
3746 }
3747
3748out:
Max Reitz76a3a342014-10-27 11:12:51 +01003749 qemu_progress_end();
3750
Max Reitze814dff2015-08-20 16:00:38 -07003751out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003752 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003753 qemu_opts_del(opts);
3754 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003755 g_free(options);
3756
Max Reitz6f176b42013-09-03 10:09:50 +02003757 if (ret) {
3758 return 1;
3759 }
3760 return 0;
3761}
3762
Kevin Wolfb6133b82014-08-05 14:17:13 +02003763typedef struct BenchData {
3764 BlockBackend *blk;
3765 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003766 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003767 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003768 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003769 int nrreq;
3770 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003771 int flush_interval;
3772 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003773 uint8_t *buf;
3774 QEMUIOVector *qiov;
3775
3776 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003777 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003778 uint64_t offset;
3779} BenchData;
3780
Kevin Wolf55d539c2016-06-03 13:59:41 +02003781static void bench_undrained_flush_cb(void *opaque, int ret)
3782{
3783 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003784 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003785 exit(EXIT_FAILURE);
3786 }
3787}
3788
Kevin Wolfb6133b82014-08-05 14:17:13 +02003789static void bench_cb(void *opaque, int ret)
3790{
3791 BenchData *b = opaque;
3792 BlockAIOCB *acb;
3793
3794 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003795 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003796 exit(EXIT_FAILURE);
3797 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003798
3799 if (b->in_flush) {
3800 /* Just finished a flush with drained queue: Start next requests */
3801 assert(b->in_flight == 0);
3802 b->in_flush = false;
3803 } else if (b->in_flight > 0) {
3804 int remaining = b->n - b->in_flight;
3805
Kevin Wolfb6133b82014-08-05 14:17:13 +02003806 b->n--;
3807 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003808
3809 /* Time for flush? Drain queue if requested, then flush */
3810 if (b->flush_interval && remaining % b->flush_interval == 0) {
3811 if (!b->in_flight || !b->drain_on_flush) {
3812 BlockCompletionFunc *cb;
3813
3814 if (b->drain_on_flush) {
3815 b->in_flush = true;
3816 cb = bench_cb;
3817 } else {
3818 cb = bench_undrained_flush_cb;
3819 }
3820
3821 acb = blk_aio_flush(b->blk, cb, b);
3822 if (!acb) {
3823 error_report("Failed to issue flush request");
3824 exit(EXIT_FAILURE);
3825 }
3826 }
3827 if (b->drain_on_flush) {
3828 return;
3829 }
3830 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003831 }
3832
3833 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003834 int64_t offset = b->offset;
3835 /* blk_aio_* might look for completed I/Os and kick bench_cb
3836 * again, so make sure this operation is counted by in_flight
3837 * and b->offset is ready for the next submission.
3838 */
3839 b->in_flight++;
3840 b->offset += b->step;
3841 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003842 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003843 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003844 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003845 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003846 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003847 if (!acb) {
3848 error_report("Failed to issue request");
3849 exit(EXIT_FAILURE);
3850 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003851 }
3852}
3853
3854static int img_bench(int argc, char **argv)
3855{
3856 int c, ret = 0;
3857 const char *fmt = NULL, *filename;
3858 bool quiet = false;
3859 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003860 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003861 int count = 75000;
3862 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003863 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003864 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003865 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003866 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003867 int flush_interval = 0;
3868 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003869 int64_t image_size;
3870 BlockBackend *blk = NULL;
3871 BenchData data = {};
3872 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003873 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003874 struct timeval t1, t2;
3875 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08003876 bool force_share = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003877
3878 for (;;) {
3879 static const struct option long_options[] = {
3880 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003881 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003882 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003883 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003884 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08003885 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003886 {0, 0, 0, 0}
3887 };
Fam Zheng335e9932017-05-03 00:35:39 +08003888 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003889 if (c == -1) {
3890 break;
3891 }
3892
3893 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003894 case ':':
3895 missing_argument(argv[optind - 1]);
3896 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003897 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003898 unrecognized_option(argv[optind - 1]);
3899 break;
3900 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02003901 help();
3902 break;
3903 case 'c':
3904 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003905 unsigned long res;
3906
3907 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003908 error_report("Invalid request count specified");
3909 return 1;
3910 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003911 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003912 break;
3913 }
3914 case 'd':
3915 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003916 unsigned long res;
3917
3918 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003919 error_report("Invalid queue depth specified");
3920 return 1;
3921 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003922 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003923 break;
3924 }
3925 case 'f':
3926 fmt = optarg;
3927 break;
3928 case 'n':
3929 flags |= BDRV_O_NATIVE_AIO;
3930 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003931 case 'o':
3932 {
Markus Armbruster606caa02017-02-21 21:14:04 +01003933 offset = cvtnum(optarg);
3934 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02003935 error_report("Invalid offset specified");
3936 return 1;
3937 }
3938 break;
3939 }
3940 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003941 case 'q':
3942 quiet = true;
3943 break;
3944 case 's':
3945 {
3946 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003947
Markus Armbruster606caa02017-02-21 21:14:04 +01003948 sval = cvtnum(optarg);
3949 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003950 error_report("Invalid buffer size specified");
3951 return 1;
3952 }
3953
3954 bufsize = sval;
3955 break;
3956 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003957 case 'S':
3958 {
3959 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003960
Markus Armbruster606caa02017-02-21 21:14:04 +01003961 sval = cvtnum(optarg);
3962 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02003963 error_report("Invalid step size specified");
3964 return 1;
3965 }
3966
3967 step = sval;
3968 break;
3969 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003970 case 't':
3971 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3972 if (ret < 0) {
3973 error_report("Invalid cache mode");
3974 ret = -1;
3975 goto out;
3976 }
3977 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003978 case 'w':
3979 flags |= BDRV_O_RDWR;
3980 is_write = true;
3981 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003982 case 'U':
3983 force_share = true;
3984 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003985 case OPTION_PATTERN:
3986 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003987 unsigned long res;
3988
3989 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003990 error_report("Invalid pattern byte specified");
3991 return 1;
3992 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003993 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003994 break;
3995 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003996 case OPTION_FLUSH_INTERVAL:
3997 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003998 unsigned long res;
3999
4000 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004001 error_report("Invalid flush interval specified");
4002 return 1;
4003 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004004 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004005 break;
4006 }
4007 case OPTION_NO_DRAIN:
4008 drain_on_flush = false;
4009 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004010 case OPTION_IMAGE_OPTS:
4011 image_opts = true;
4012 break;
4013 }
4014 }
4015
4016 if (optind != argc - 1) {
4017 error_exit("Expecting one image file name");
4018 }
4019 filename = argv[argc - 1];
4020
Kevin Wolf55d539c2016-06-03 13:59:41 +02004021 if (!is_write && flush_interval) {
4022 error_report("--flush-interval is only available in write tests");
4023 ret = -1;
4024 goto out;
4025 }
4026 if (flush_interval && flush_interval < depth) {
4027 error_report("Flush interval can't be smaller than depth");
4028 ret = -1;
4029 goto out;
4030 }
4031
Fam Zheng335e9932017-05-03 00:35:39 +08004032 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4033 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004034 if (!blk) {
4035 ret = -1;
4036 goto out;
4037 }
4038
4039 image_size = blk_getlength(blk);
4040 if (image_size < 0) {
4041 ret = image_size;
4042 goto out;
4043 }
4044
4045 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004046 .blk = blk,
4047 .image_size = image_size,
4048 .bufsize = bufsize,
4049 .step = step ?: bufsize,
4050 .nrreq = depth,
4051 .n = count,
4052 .offset = offset,
4053 .write = is_write,
4054 .flush_interval = flush_interval,
4055 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004056 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004057 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004058 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004059 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004060 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004061 if (flush_interval) {
4062 printf("Sending flush every %d requests\n", flush_interval);
4063 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004064
4065 data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004066 memset(data.buf, pattern, data.nrreq * data.bufsize);
4067
Kevin Wolfb6133b82014-08-05 14:17:13 +02004068 data.qiov = g_new(QEMUIOVector, data.nrreq);
4069 for (i = 0; i < data.nrreq; i++) {
4070 qemu_iovec_init(&data.qiov[i], 1);
4071 qemu_iovec_add(&data.qiov[i],
4072 data.buf + i * data.bufsize, data.bufsize);
4073 }
4074
4075 gettimeofday(&t1, NULL);
4076 bench_cb(&data, 0);
4077
4078 while (data.n > 0) {
4079 main_loop_wait(false);
4080 }
4081 gettimeofday(&t2, NULL);
4082
4083 printf("Run completed in %3.3f seconds.\n",
4084 (t2.tv_sec - t1.tv_sec)
4085 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4086
4087out:
4088 qemu_vfree(data.buf);
4089 blk_unref(blk);
4090
4091 if (ret) {
4092 return 1;
4093 }
4094 return 0;
4095}
4096
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004097#define C_BS 01
4098#define C_COUNT 02
4099#define C_IF 04
4100#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004101#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004102
4103struct DdInfo {
4104 unsigned int flags;
4105 int64_t count;
4106};
4107
4108struct DdIo {
4109 int bsz; /* Block size */
4110 char *filename;
4111 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004112 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004113};
4114
4115struct DdOpts {
4116 const char *name;
4117 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4118 unsigned int flag;
4119};
4120
4121static int img_dd_bs(const char *arg,
4122 struct DdIo *in, struct DdIo *out,
4123 struct DdInfo *dd)
4124{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004125 int64_t res;
4126
Markus Armbruster606caa02017-02-21 21:14:04 +01004127 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004128
Markus Armbruster606caa02017-02-21 21:14:04 +01004129 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004130 error_report("invalid number: '%s'", arg);
4131 return 1;
4132 }
4133 in->bsz = out->bsz = res;
4134
4135 return 0;
4136}
4137
4138static int img_dd_count(const char *arg,
4139 struct DdIo *in, struct DdIo *out,
4140 struct DdInfo *dd)
4141{
Markus Armbruster606caa02017-02-21 21:14:04 +01004142 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004143
Markus Armbruster606caa02017-02-21 21:14:04 +01004144 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004145 error_report("invalid number: '%s'", arg);
4146 return 1;
4147 }
4148
4149 return 0;
4150}
4151
4152static int img_dd_if(const char *arg,
4153 struct DdIo *in, struct DdIo *out,
4154 struct DdInfo *dd)
4155{
4156 in->filename = g_strdup(arg);
4157
4158 return 0;
4159}
4160
4161static int img_dd_of(const char *arg,
4162 struct DdIo *in, struct DdIo *out,
4163 struct DdInfo *dd)
4164{
4165 out->filename = g_strdup(arg);
4166
4167 return 0;
4168}
4169
Reda Sallahif7c15532016-08-10 16:16:09 +02004170static int img_dd_skip(const char *arg,
4171 struct DdIo *in, struct DdIo *out,
4172 struct DdInfo *dd)
4173{
Markus Armbruster606caa02017-02-21 21:14:04 +01004174 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004175
Markus Armbruster606caa02017-02-21 21:14:04 +01004176 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004177 error_report("invalid number: '%s'", arg);
4178 return 1;
4179 }
4180
4181 return 0;
4182}
4183
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004184static int img_dd(int argc, char **argv)
4185{
4186 int ret = 0;
4187 char *arg = NULL;
4188 char *tmp;
4189 BlockDriver *drv = NULL, *proto_drv = NULL;
4190 BlockBackend *blk1 = NULL, *blk2 = NULL;
4191 QemuOpts *opts = NULL;
4192 QemuOptsList *create_opts = NULL;
4193 Error *local_err = NULL;
4194 bool image_opts = false;
4195 int c, i;
4196 const char *out_fmt = "raw";
4197 const char *fmt = NULL;
4198 int64_t size = 0;
4199 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004200 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004201 struct DdInfo dd = {
4202 .flags = 0,
4203 .count = 0,
4204 };
4205 struct DdIo in = {
4206 .bsz = 512, /* Block size is by default 512 bytes */
4207 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004208 .buf = NULL,
4209 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004210 };
4211 struct DdIo out = {
4212 .bsz = 512,
4213 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004214 .buf = NULL,
4215 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004216 };
4217
4218 const struct DdOpts options[] = {
4219 { "bs", img_dd_bs, C_BS },
4220 { "count", img_dd_count, C_COUNT },
4221 { "if", img_dd_if, C_IF },
4222 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004223 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004224 { NULL, NULL, 0 }
4225 };
4226 const struct option long_options[] = {
4227 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004228 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004229 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004230 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004231 { 0, 0, 0, 0 }
4232 };
4233
Fam Zheng335e9932017-05-03 00:35:39 +08004234 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004235 if (c == EOF) {
4236 break;
4237 }
4238 switch (c) {
4239 case 'O':
4240 out_fmt = optarg;
4241 break;
4242 case 'f':
4243 fmt = optarg;
4244 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004245 case ':':
4246 missing_argument(argv[optind - 1]);
4247 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004248 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004249 unrecognized_option(argv[optind - 1]);
4250 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004251 case 'h':
4252 help();
4253 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004254 case 'U':
4255 force_share = true;
4256 break;
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004257 case OPTION_OBJECT:
4258 if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004259 ret = -1;
4260 goto out;
4261 }
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004262 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004263 case OPTION_IMAGE_OPTS:
4264 image_opts = true;
4265 break;
4266 }
4267 }
4268
4269 for (i = optind; i < argc; i++) {
4270 int j;
4271 arg = g_strdup(argv[i]);
4272
4273 tmp = strchr(arg, '=');
4274 if (tmp == NULL) {
4275 error_report("unrecognized operand %s", arg);
4276 ret = -1;
4277 goto out;
4278 }
4279
4280 *tmp++ = '\0';
4281
4282 for (j = 0; options[j].name != NULL; j++) {
4283 if (!strcmp(arg, options[j].name)) {
4284 break;
4285 }
4286 }
4287 if (options[j].name == NULL) {
4288 error_report("unrecognized operand %s", arg);
4289 ret = -1;
4290 goto out;
4291 }
4292
4293 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4294 ret = -1;
4295 goto out;
4296 }
4297 dd.flags |= options[j].flag;
4298 g_free(arg);
4299 arg = NULL;
4300 }
4301
4302 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4303 error_report("Must specify both input and output files");
4304 ret = -1;
4305 goto out;
4306 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004307
4308 if (qemu_opts_foreach(&qemu_object_opts,
4309 user_creatable_add_opts_foreach,
4310 NULL, NULL)) {
4311 ret = -1;
4312 goto out;
4313 }
4314
Fam Zheng335e9932017-05-03 00:35:39 +08004315 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4316 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004317
4318 if (!blk1) {
4319 ret = -1;
4320 goto out;
4321 }
4322
4323 drv = bdrv_find_format(out_fmt);
4324 if (!drv) {
4325 error_report("Unknown file format");
4326 ret = -1;
4327 goto out;
4328 }
4329 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4330
4331 if (!proto_drv) {
4332 error_report_err(local_err);
4333 ret = -1;
4334 goto out;
4335 }
4336 if (!drv->create_opts) {
4337 error_report("Format driver '%s' does not support image creation",
4338 drv->format_name);
4339 ret = -1;
4340 goto out;
4341 }
4342 if (!proto_drv->create_opts) {
4343 error_report("Protocol driver '%s' does not support image creation",
4344 proto_drv->format_name);
4345 ret = -1;
4346 goto out;
4347 }
4348 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4349 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4350
4351 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4352
4353 size = blk_getlength(blk1);
4354 if (size < 0) {
4355 error_report("Failed to get size for '%s'", in.filename);
4356 ret = -1;
4357 goto out;
4358 }
4359
4360 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4361 dd.count * in.bsz < size) {
4362 size = dd.count * in.bsz;
4363 }
4364
Reda Sallahif7c15532016-08-10 16:16:09 +02004365 /* Overflow means the specified offset is beyond input image's size */
4366 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4367 size < in.bsz * in.offset)) {
4368 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4369 } else {
4370 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4371 size - in.bsz * in.offset, &error_abort);
4372 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004373
4374 ret = bdrv_create(drv, out.filename, opts, &local_err);
4375 if (ret < 0) {
4376 error_reportf_err(local_err,
4377 "%s: error while creating output image: ",
4378 out.filename);
4379 ret = -1;
4380 goto out;
4381 }
4382
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004383 /* TODO, we can't honour --image-opts for the target,
4384 * since it needs to be given in a format compatible
4385 * with the bdrv_create() call above which does not
4386 * support image-opts style.
4387 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01004388 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004389 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004390
4391 if (!blk2) {
4392 ret = -1;
4393 goto out;
4394 }
4395
Reda Sallahif7c15532016-08-10 16:16:09 +02004396 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4397 size < in.offset * in.bsz)) {
4398 /* We give a warning if the skip option is bigger than the input
4399 * size and create an empty output disk image (i.e. like dd(1)).
4400 */
4401 error_report("%s: cannot skip to specified offset", in.filename);
4402 in_pos = size;
4403 } else {
4404 in_pos = in.offset * in.bsz;
4405 }
4406
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004407 in.buf = g_new(uint8_t, in.bsz);
4408
Reda Sallahif7c15532016-08-10 16:16:09 +02004409 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004410 int in_ret, out_ret;
4411
4412 if (in_pos + in.bsz > size) {
4413 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4414 } else {
4415 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4416 }
4417 if (in_ret < 0) {
4418 error_report("error while reading from input image file: %s",
4419 strerror(-in_ret));
4420 ret = -1;
4421 goto out;
4422 }
4423 in_pos += in_ret;
4424
4425 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4426
4427 if (out_ret < 0) {
4428 error_report("error while writing to output image file: %s",
4429 strerror(-out_ret));
4430 ret = -1;
4431 goto out;
4432 }
4433 out_pos += out_ret;
4434 }
4435
4436out:
4437 g_free(arg);
4438 qemu_opts_del(opts);
4439 qemu_opts_free(create_opts);
4440 blk_unref(blk1);
4441 blk_unref(blk2);
4442 g_free(in.filename);
4443 g_free(out.filename);
4444 g_free(in.buf);
4445 g_free(out.buf);
4446
4447 if (ret) {
4448 return 1;
4449 }
4450 return 0;
4451}
4452
Kevin Wolfb6133b82014-08-05 14:17:13 +02004453
Anthony Liguoric227f092009-10-01 16:12:16 -05004454static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004455#define DEF(option, callback, arg_string) \
4456 { option, callback },
4457#include "qemu-img-cmds.h"
4458#undef DEF
4459#undef GEN_DOCS
4460 { NULL, NULL, },
4461};
4462
bellardea2384d2004-08-01 21:59:26 +00004463int main(int argc, char **argv)
4464{
Anthony Liguoric227f092009-10-01 16:12:16 -05004465 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004466 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004467 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004468 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004469 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004470 static const struct option long_options[] = {
4471 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004472 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004473 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004474 {0, 0, 0, 0}
4475 };
bellardea2384d2004-08-01 21:59:26 +00004476
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004477#ifdef CONFIG_POSIX
4478 signal(SIGPIPE, SIG_IGN);
4479#endif
4480
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004481 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004482 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004483 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004484
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004485 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004486 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004487 exit(EXIT_FAILURE);
4488 }
4489
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004490 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004491
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004492 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004493 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004494 if (argc < 2) {
4495 error_exit("Not enough arguments");
4496 }
Stuart Brady153859b2009-06-07 00:42:17 +01004497
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004498 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004499 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004500 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004501
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004502 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004503 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004504 case ':':
4505 missing_argument(argv[optind - 1]);
4506 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08004507 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004508 unrecognized_option(argv[optind - 1]);
4509 return 0;
4510 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03004511 help();
4512 return 0;
4513 case 'V':
4514 printf(QEMU_IMG_VERSION);
4515 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004516 case 'T':
4517 g_free(trace_file);
4518 trace_file = trace_opt_parse(optarg);
4519 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004520 }
bellardea2384d2004-08-01 21:59:26 +00004521 }
Stuart Brady153859b2009-06-07 00:42:17 +01004522
Denis V. Lunev10985132016-06-17 17:44:13 +03004523 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004524
Denis V. Lunev10985132016-06-17 17:44:13 +03004525 /* reset getopt_long scanning */
4526 argc -= optind;
4527 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004528 return 0;
4529 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004530 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004531 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004532
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004533 if (!trace_init_backends()) {
4534 exit(1);
4535 }
4536 trace_init_file(trace_file);
4537 qemu_set_log(LOG_TRACE);
4538
Denis V. Lunev10985132016-06-17 17:44:13 +03004539 /* find the command */
4540 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4541 if (!strcmp(cmdname, cmd->name)) {
4542 return cmd->handler(argc, argv);
4543 }
4544 }
Jeff Cody7db16892014-04-25 17:02:32 -04004545
Stuart Brady153859b2009-06-07 00:42:17 +01004546 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004547 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004548}