blob: 5aef8ef0476ebb07a39225851a9151efad276ac2 [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 }
301 qdict_put(options, BDRV_OPT_FORCE_SHARE, qbool_from_bool(true));
302 }
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) {
334 qdict_put(options, BDRV_OPT_FORCE_SHARE, qbool_from_bool(true));
335 }
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 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800467 c = getopt_long(argc, argv, ":F:b:f:he6o: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;
491 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200492 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100493 "encryption\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100494 goto fail;
thsd8871c52007-10-24 16:11:42 +0000495 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200496 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100497 "compat6\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100498 goto fail;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200499 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100500 if (!is_valid_option_list(optarg)) {
501 error_report("Invalid option list: %s", optarg);
502 goto fail;
503 }
504 if (!options) {
505 options = g_strdup(optarg);
506 } else {
507 char *old_options = options;
508 options = g_strdup_printf("%s,%s", options, optarg);
509 g_free(old_options);
510 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200511 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100512 case 'q':
513 quiet = true;
514 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000515 case OPTION_OBJECT: {
516 QemuOpts *opts;
517 opts = qemu_opts_parse_noisily(&qemu_object_opts,
518 optarg, true);
519 if (!opts) {
520 goto fail;
521 }
522 } break;
bellardea2384d2004-08-01 21:59:26 +0000523 }
524 }
aliguori9230eaf2009-03-28 17:55:19 +0000525
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900526 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100527 filename = (optind < argc) ? argv[optind] : NULL;
528 if (options && has_help_option(options)) {
529 g_free(options);
530 return print_block_option_help(filename, fmt);
531 }
532
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100533 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800534 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100535 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100536 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900537
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000538 if (qemu_opts_foreach(&qemu_object_opts,
539 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200540 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000541 goto fail;
542 }
543
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100544 /* Get image size, if specified */
545 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100546 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100547
548 sval = cvtnum(argv[optind++]);
549 if (sval < 0) {
liguang79443392012-12-17 09:49:23 +0800550 if (sval == -ERANGE) {
551 error_report("Image size must be less than 8 EiB!");
552 } else {
553 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200554 "G, T, P or E suffixes for ");
555 error_report("kilobytes, megabytes, gigabytes, terabytes, "
556 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800557 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100558 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100559 }
560 img_size = (uint64_t)sval;
561 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200562 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800563 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200564 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100565
Luiz Capitulino9b375252012-11-30 10:52:05 -0200566 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Fam Zheng92172832017-04-21 20:27:01 +0800567 options, img_size, 0, quiet, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100568 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100569 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100570 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900571 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200572
Kevin Wolf77386bf2014-02-21 16:24:04 +0100573 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000574 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100575
576fail:
577 g_free(options);
578 return 1;
bellardea2384d2004-08-01 21:59:26 +0000579}
580
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100581static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500582{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500583 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500584 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100585 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600586
587 visit_type_ImageCheck(v, NULL, &check, &error_abort);
588 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500589 str = qobject_to_json_pretty(obj);
590 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100591 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500592 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600593 visit_free(v);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500594 QDECREF(str);
595}
596
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100597static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500598{
599 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100600 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500601 } else {
602 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100603 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
604 "Data may be corrupted, or further writes to the image "
605 "may corrupt it.\n",
606 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500607 }
608
609 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100610 qprintf(quiet,
611 "\n%" PRId64 " leaked clusters were found on the image.\n"
612 "This means waste of disk space, but no harm to data.\n",
613 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500614 }
615
616 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100617 qprintf(quiet,
618 "\n%" PRId64
619 " internal errors have occurred during the check.\n",
620 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500621 }
622 }
623
624 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100625 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
626 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
627 check->allocated_clusters, check->total_clusters,
628 check->allocated_clusters * 100.0 / check->total_clusters,
629 check->fragmented_clusters * 100.0 / check->allocated_clusters,
630 check->compressed_clusters * 100.0 /
631 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500632 }
633
634 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100635 qprintf(quiet,
636 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500637 }
638}
639
640static int collect_image_check(BlockDriverState *bs,
641 ImageCheck *check,
642 const char *filename,
643 const char *fmt,
644 int fix)
645{
646 int ret;
647 BdrvCheckResult result;
648
649 ret = bdrv_check(bs, &result, fix);
650 if (ret < 0) {
651 return ret;
652 }
653
654 check->filename = g_strdup(filename);
655 check->format = g_strdup(bdrv_get_format_name(bs));
656 check->check_errors = result.check_errors;
657 check->corruptions = result.corruptions;
658 check->has_corruptions = result.corruptions != 0;
659 check->leaks = result.leaks;
660 check->has_leaks = result.leaks != 0;
661 check->corruptions_fixed = result.corruptions_fixed;
662 check->has_corruptions_fixed = result.corruptions != 0;
663 check->leaks_fixed = result.leaks_fixed;
664 check->has_leaks_fixed = result.leaks != 0;
665 check->image_end_offset = result.image_end_offset;
666 check->has_image_end_offset = result.image_end_offset != 0;
667 check->total_clusters = result.bfi.total_clusters;
668 check->has_total_clusters = result.bfi.total_clusters != 0;
669 check->allocated_clusters = result.bfi.allocated_clusters;
670 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
671 check->fragmented_clusters = result.bfi.fragmented_clusters;
672 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100673 check->compressed_clusters = result.bfi.compressed_clusters;
674 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500675
676 return 0;
677}
678
Kevin Wolfe076f332010-06-29 11:43:13 +0200679/*
680 * Checks an image for consistency. Exit codes:
681 *
Max Reitzd6635c42014-06-02 22:15:21 +0200682 * 0 - Check completed, image is good
683 * 1 - Check not completed because of internal errors
684 * 2 - Check completed, image is corrupted
685 * 3 - Check completed, image has leaked clusters, but is good otherwise
686 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200687 */
aliguori15859692009-04-21 23:11:53 +0000688static int img_check(int argc, char **argv)
689{
690 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500691 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200692 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200693 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000694 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200695 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100696 int flags = BDRV_O_CHECK;
697 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200698 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100699 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000700 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +0800701 bool force_share = false;
aliguori15859692009-04-21 23:11:53 +0000702
703 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500704 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200705 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100706
aliguori15859692009-04-21 23:11:53 +0000707 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500708 int option_index = 0;
709 static const struct option long_options[] = {
710 {"help", no_argument, 0, 'h'},
711 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530712 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500713 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000714 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000715 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +0800716 {"force-share", no_argument, 0, 'U'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500717 {0, 0, 0, 0}
718 };
Fam Zheng335e9932017-05-03 00:35:39 +0800719 c = getopt_long(argc, argv, ":hf:r:T:qU",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500720 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100721 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000722 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100723 }
aliguori15859692009-04-21 23:11:53 +0000724 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800725 case ':':
726 missing_argument(argv[optind - 1]);
727 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100728 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800729 unrecognized_option(argv[optind - 1]);
730 break;
aliguori15859692009-04-21 23:11:53 +0000731 case 'h':
732 help();
733 break;
734 case 'f':
735 fmt = optarg;
736 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200737 case 'r':
738 flags |= BDRV_O_RDWR;
739
740 if (!strcmp(optarg, "leaks")) {
741 fix = BDRV_FIX_LEAKS;
742 } else if (!strcmp(optarg, "all")) {
743 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
744 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800745 error_exit("Unknown option value for -r "
746 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200747 }
748 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500749 case OPTION_OUTPUT:
750 output = optarg;
751 break;
Max Reitz40055952014-07-22 22:58:42 +0200752 case 'T':
753 cache = optarg;
754 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100755 case 'q':
756 quiet = true;
757 break;
Fam Zheng335e9932017-05-03 00:35:39 +0800758 case 'U':
759 force_share = true;
760 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000761 case OPTION_OBJECT: {
762 QemuOpts *opts;
763 opts = qemu_opts_parse_noisily(&qemu_object_opts,
764 optarg, true);
765 if (!opts) {
766 return 1;
767 }
768 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000769 case OPTION_IMAGE_OPTS:
770 image_opts = true;
771 break;
aliguori15859692009-04-21 23:11:53 +0000772 }
773 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200774 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800775 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100776 }
aliguori15859692009-04-21 23:11:53 +0000777 filename = argv[optind++];
778
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500779 if (output && !strcmp(output, "json")) {
780 output_format = OFORMAT_JSON;
781 } else if (output && !strcmp(output, "human")) {
782 output_format = OFORMAT_HUMAN;
783 } else if (output) {
784 error_report("--output must be used with human or json as argument.");
785 return 1;
786 }
787
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000788 if (qemu_opts_foreach(&qemu_object_opts,
789 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200790 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000791 return 1;
792 }
793
Kevin Wolfce099542016-03-15 13:01:04 +0100794 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200795 if (ret < 0) {
796 error_report("Invalid source cache option: %s", cache);
797 return 1;
798 }
799
Fam Zheng335e9932017-05-03 00:35:39 +0800800 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
801 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200802 if (!blk) {
803 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900804 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200805 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500806
807 check = g_new0(ImageCheck, 1);
808 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200809
810 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200811 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200812 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500813 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200814 }
815
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500816 if (check->corruptions_fixed || check->leaks_fixed) {
817 int corruptions_fixed, leaks_fixed;
818
819 leaks_fixed = check->leaks_fixed;
820 corruptions_fixed = check->corruptions_fixed;
821
822 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100823 qprintf(quiet,
824 "The following inconsistencies were found and repaired:\n\n"
825 " %" PRId64 " leaked clusters\n"
826 " %" PRId64 " corruptions\n\n"
827 "Double checking the fixed image now...\n",
828 check->leaks_fixed,
829 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500830 }
831
832 ret = collect_image_check(bs, check, filename, fmt, 0);
833
834 check->leaks_fixed = leaks_fixed;
835 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200836 }
837
Max Reitz832390a2014-10-23 15:29:12 +0200838 if (!ret) {
839 switch (output_format) {
840 case OFORMAT_HUMAN:
841 dump_human_image_check(check, quiet);
842 break;
843 case OFORMAT_JSON:
844 dump_json_image_check(check, quiet);
845 break;
846 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500847 }
848
849 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200850 if (ret) {
851 error_report("Check failed: %s", strerror(-ret));
852 } else {
853 error_report("Check failed");
854 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500855 ret = 1;
856 goto fail;
857 }
858
859 if (check->corruptions) {
860 ret = 2;
861 } else if (check->leaks) {
862 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200863 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500864 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000865 }
866
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500867fail:
868 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200869 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500870 return ret;
aliguori15859692009-04-21 23:11:53 +0000871}
872
Max Reitzd4a32382014-10-24 15:57:37 +0200873typedef struct CommonBlockJobCBInfo {
874 BlockDriverState *bs;
875 Error **errp;
876} CommonBlockJobCBInfo;
877
878static void common_block_job_cb(void *opaque, int ret)
879{
880 CommonBlockJobCBInfo *cbi = opaque;
881
882 if (ret < 0) {
883 error_setg_errno(cbi->errp, -ret, "Block job failed");
884 }
Max Reitzd4a32382014-10-24 15:57:37 +0200885}
886
887static void run_block_job(BlockJob *job, Error **errp)
888{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200889 AioContext *aio_context = blk_get_aio_context(job->blk);
Max Reitzd4a32382014-10-24 15:57:37 +0200890
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100891 /* FIXME In error cases, the job simply goes away and we access a dangling
892 * pointer below. */
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200893 aio_context_acquire(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +0200894 do {
895 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500896 qemu_progress_print(job->len ?
897 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200898 } while (!job->ready);
899
900 block_job_complete_sync(job, errp);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200901 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200902
903 /* A block job may finish instantaneously without publishing any progress,
904 * so just signal completion here */
905 qemu_progress_print(100.f, 0);
Max Reitzd4a32382014-10-24 15:57:37 +0200906}
907
bellardea2384d2004-08-01 21:59:26 +0000908static int img_commit(int argc, char **argv)
909{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400910 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200911 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200912 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200913 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100914 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200915 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100916 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200917 Error *local_err = NULL;
918 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000919 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200920 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000921
922 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400923 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200924 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000925 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000926 static const struct option long_options[] = {
927 {"help", no_argument, 0, 'h'},
928 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000929 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000930 {0, 0, 0, 0}
931 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800932 c = getopt_long(argc, argv, ":f:ht:b:dpq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000933 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100934 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000935 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100936 }
bellardea2384d2004-08-01 21:59:26 +0000937 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800938 case ':':
939 missing_argument(argv[optind - 1]);
940 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100941 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800942 unrecognized_option(argv[optind - 1]);
943 break;
bellardea2384d2004-08-01 21:59:26 +0000944 case 'h':
945 help();
946 break;
947 case 'f':
948 fmt = optarg;
949 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400950 case 't':
951 cache = optarg;
952 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200953 case 'b':
954 base = optarg;
955 /* -b implies -d */
956 drop = true;
957 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200958 case 'd':
959 drop = true;
960 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200961 case 'p':
962 progress = true;
963 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100964 case 'q':
965 quiet = true;
966 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000967 case OPTION_OBJECT: {
968 QemuOpts *opts;
969 opts = qemu_opts_parse_noisily(&qemu_object_opts,
970 optarg, true);
971 if (!opts) {
972 return 1;
973 }
974 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000975 case OPTION_IMAGE_OPTS:
976 image_opts = true;
977 break;
bellardea2384d2004-08-01 21:59:26 +0000978 }
979 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200980
981 /* Progress is not shown in Quiet mode */
982 if (quiet) {
983 progress = false;
984 }
985
Kevin Wolffc11eb22013-08-05 10:53:04 +0200986 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800987 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100988 }
bellardea2384d2004-08-01 21:59:26 +0000989 filename = argv[optind++];
990
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000991 if (qemu_opts_foreach(&qemu_object_opts,
992 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200993 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000994 return 1;
995 }
996
Max Reitz9a86fe42014-10-24 15:57:38 +0200997 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100998 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400999 if (ret < 0) {
1000 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +01001001 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001002 }
1003
Fam Zheng335e9932017-05-03 00:35:39 +08001004 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
1005 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001006 if (!blk) {
1007 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001008 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001009 bs = blk_bs(blk);
1010
Max Reitz687fa1d2014-10-24 15:57:39 +02001011 qemu_progress_init(progress, 1.f);
1012 qemu_progress_print(0.f, 100);
1013
Max Reitz1b22bff2014-10-24 15:57:40 +02001014 if (base) {
1015 base_bs = bdrv_find_backing_image(bs, base);
1016 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +01001017 error_setg(&local_err,
1018 "Did not find '%s' in the backing chain of '%s'",
1019 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +02001020 goto done;
1021 }
1022 } else {
1023 /* This is different from QMP, which by default uses the deepest file in
1024 * the backing chain (i.e., the very base); however, the traditional
1025 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +02001026 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +02001027 if (!base_bs) {
1028 error_setg(&local_err, "Image does not have a backing file");
1029 goto done;
1030 }
bellardea2384d2004-08-01 21:59:26 +00001031 }
1032
Max Reitzd4a32382014-10-24 15:57:37 +02001033 cbi = (CommonBlockJobCBInfo){
1034 .errp = &local_err,
1035 .bs = bs,
1036 };
1037
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001038 aio_context = bdrv_get_aio_context(bs);
1039 aio_context_acquire(aio_context);
John Snow47970df2016-10-27 12:06:57 -04001040 commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
Kevin Wolf0db832f2017-02-20 18:10:05 +01001041 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +08001042 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001043 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +02001044 if (local_err) {
1045 goto done;
1046 }
1047
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001048 /* When the block job completes, the BlockBackend reference will point to
1049 * the old backing file. In order to avoid that the top image is already
1050 * deleted, so we can still empty it afterwards, increment the reference
1051 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +02001052 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001053 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001054 }
1055
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001056 job = block_job_get("commit");
1057 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001058 if (local_err) {
1059 goto unref_backing;
1060 }
1061
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001062 if (!drop && bs->drv->bdrv_make_empty) {
1063 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001064 if (ret) {
1065 error_setg_errno(&local_err, -ret, "Could not empty %s",
1066 filename);
1067 goto unref_backing;
1068 }
1069 }
1070
1071unref_backing:
1072 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001073 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001074 }
Max Reitzd4a32382014-10-24 15:57:37 +02001075
1076done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001077 qemu_progress_end();
1078
Markus Armbruster26f54e92014-10-07 13:59:04 +02001079 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001080
1081 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001082 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001083 return 1;
1084 }
Max Reitzd4a32382014-10-24 15:57:37 +02001085
1086 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001087 return 0;
1088}
1089
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001090/*
thsf58c7b32008-06-05 21:53:49 +00001091 * Returns true iff the first sector pointed to by 'buf' contains at least
1092 * a non-NUL byte.
1093 *
1094 * 'pnum' is set to the number of sectors (including and immediately following
1095 * the first one) that are known to be in the same allocated/unallocated state.
1096 */
bellardea2384d2004-08-01 21:59:26 +00001097static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1098{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001099 bool is_zero;
1100 int i;
bellardea2384d2004-08-01 21:59:26 +00001101
1102 if (n <= 0) {
1103 *pnum = 0;
1104 return 0;
1105 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001106 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001107 for(i = 1; i < n; i++) {
1108 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001109 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001110 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001111 }
bellardea2384d2004-08-01 21:59:26 +00001112 }
1113 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001114 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001115}
1116
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001117/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001118 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1119 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1120 * breaking up write requests for only small sparse areas.
1121 */
1122static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1123 int min)
1124{
1125 int ret;
1126 int num_checked, num_used;
1127
1128 if (n < min) {
1129 min = n;
1130 }
1131
1132 ret = is_allocated_sectors(buf, n, pnum);
1133 if (!ret) {
1134 return ret;
1135 }
1136
1137 num_used = *pnum;
1138 buf += BDRV_SECTOR_SIZE * *pnum;
1139 n -= *pnum;
1140 num_checked = num_used;
1141
1142 while (n > 0) {
1143 ret = is_allocated_sectors(buf, n, pnum);
1144
1145 buf += BDRV_SECTOR_SIZE * *pnum;
1146 n -= *pnum;
1147 num_checked += *pnum;
1148 if (ret) {
1149 num_used = num_checked;
1150 } else if (*pnum >= min) {
1151 break;
1152 }
1153 }
1154
1155 *pnum = num_used;
1156 return 1;
1157}
1158
1159/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001160 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1161 * buffers matches, non-zero otherwise.
1162 *
1163 * pnum is set to the number of sectors (including and immediately following
1164 * the first one) that are known to have the same comparison result
1165 */
1166static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1167 int *pnum)
1168{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001169 bool res;
1170 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001171
1172 if (n <= 0) {
1173 *pnum = 0;
1174 return 0;
1175 }
1176
1177 res = !!memcmp(buf1, buf2, 512);
1178 for(i = 1; i < n; i++) {
1179 buf1 += 512;
1180 buf2 += 512;
1181
1182 if (!!memcmp(buf1, buf2, 512) != res) {
1183 break;
1184 }
1185 }
1186
1187 *pnum = i;
1188 return res;
1189}
1190
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001191#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001192
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001193static int64_t sectors_to_bytes(int64_t sectors)
1194{
1195 return sectors << BDRV_SECTOR_BITS;
1196}
1197
1198static int64_t sectors_to_process(int64_t total, int64_t from)
1199{
1200 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1201}
1202
1203/*
1204 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1205 *
1206 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1207 * data and negative value on error.
1208 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001209 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001210 * @param sect_num: Number of first sector to check
1211 * @param sect_count: Number of sectors to check
1212 * @param filename: Name of disk file we are checking (logging purpose)
1213 * @param buffer: Allocated buffer for storing read data
1214 * @param quiet: Flag for quiet mode
1215 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001216static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001217 int sect_count, const char *filename,
1218 uint8_t *buffer, bool quiet)
1219{
1220 int pnum, ret = 0;
Eric Blake91669202016-05-06 10:26:43 -06001221 ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer,
1222 sect_count << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001223 if (ret < 0) {
1224 error_report("Error while reading offset %" PRId64 " of %s: %s",
1225 sectors_to_bytes(sect_num), filename, strerror(-ret));
1226 return ret;
1227 }
1228 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1229 if (ret || pnum != sect_count) {
1230 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1231 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1232 return 1;
1233 }
1234
1235 return 0;
1236}
1237
1238/*
1239 * Compares two images. Exit codes:
1240 *
1241 * 0 - Images are identical
1242 * 1 - Images differ
1243 * >1 - Error occurred
1244 */
1245static int img_compare(int argc, char **argv)
1246{
Max Reitz40055952014-07-22 22:58:42 +02001247 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001248 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001249 BlockDriverState *bs1, *bs2;
1250 int64_t total_sectors1, total_sectors2;
1251 uint8_t *buf1 = NULL, *buf2 = NULL;
1252 int pnum1, pnum2;
1253 int allocated1, allocated2;
1254 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1255 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001256 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001257 bool writethrough;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001258 int64_t total_sectors;
1259 int64_t sector_num = 0;
1260 int64_t nb_sectors;
1261 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001262 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001263 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001264 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001265
Max Reitz40055952014-07-22 22:58:42 +02001266 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001267 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001268 static const struct option long_options[] = {
1269 {"help", no_argument, 0, 'h'},
1270 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001271 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001272 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001273 {0, 0, 0, 0}
1274 };
Fam Zheng335e9932017-05-03 00:35:39 +08001275 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001276 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001277 if (c == -1) {
1278 break;
1279 }
1280 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001281 case ':':
1282 missing_argument(argv[optind - 1]);
1283 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001284 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001285 unrecognized_option(argv[optind - 1]);
1286 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001287 case 'h':
1288 help();
1289 break;
1290 case 'f':
1291 fmt1 = optarg;
1292 break;
1293 case 'F':
1294 fmt2 = optarg;
1295 break;
Max Reitz40055952014-07-22 22:58:42 +02001296 case 'T':
1297 cache = optarg;
1298 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001299 case 'p':
1300 progress = true;
1301 break;
1302 case 'q':
1303 quiet = true;
1304 break;
1305 case 's':
1306 strict = true;
1307 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001308 case 'U':
1309 force_share = true;
1310 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001311 case OPTION_OBJECT: {
1312 QemuOpts *opts;
1313 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1314 optarg, true);
1315 if (!opts) {
1316 ret = 2;
1317 goto out4;
1318 }
1319 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001320 case OPTION_IMAGE_OPTS:
1321 image_opts = true;
1322 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001323 }
1324 }
1325
1326 /* Progress is not shown in Quiet mode */
1327 if (quiet) {
1328 progress = false;
1329 }
1330
1331
Kevin Wolffc11eb22013-08-05 10:53:04 +02001332 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001333 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001334 }
1335 filename1 = argv[optind++];
1336 filename2 = argv[optind++];
1337
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001338 if (qemu_opts_foreach(&qemu_object_opts,
1339 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001340 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001341 ret = 2;
1342 goto out4;
1343 }
1344
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001345 /* Initialize before goto out */
1346 qemu_progress_init(progress, 2.0);
1347
Kevin Wolfce099542016-03-15 13:01:04 +01001348 flags = 0;
1349 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001350 if (ret < 0) {
1351 error_report("Invalid source cache option: %s", cache);
1352 ret = 2;
1353 goto out3;
1354 }
1355
Fam Zheng335e9932017-05-03 00:35:39 +08001356 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1357 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001358 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001359 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001360 goto out3;
1361 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001362
Fam Zheng335e9932017-05-03 00:35:39 +08001363 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1364 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001365 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001366 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001367 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001368 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001369 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001370 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001371
Max Reitzf1d3cd72015-02-05 13:58:18 -05001372 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1373 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1374 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001375 if (total_sectors1 < 0) {
1376 error_report("Can't get size of %s: %s",
1377 filename1, strerror(-total_sectors1));
1378 ret = 4;
1379 goto out;
1380 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001381 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001382 if (total_sectors2 < 0) {
1383 error_report("Can't get size of %s: %s",
1384 filename2, strerror(-total_sectors2));
1385 ret = 4;
1386 goto out;
1387 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001388 total_sectors = MIN(total_sectors1, total_sectors2);
1389 progress_base = MAX(total_sectors1, total_sectors2);
1390
1391 qemu_progress_print(0, 100);
1392
1393 if (strict && total_sectors1 != total_sectors2) {
1394 ret = 1;
1395 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1396 goto out;
1397 }
1398
1399 for (;;) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001400 int64_t status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001401 BlockDriverState *file;
1402
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001403 nb_sectors = sectors_to_process(total_sectors, sector_num);
1404 if (nb_sectors <= 0) {
1405 break;
1406 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001407 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1408 total_sectors1 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001409 &pnum1, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001410 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001411 ret = 3;
1412 error_report("Sector allocation test failed for %s", filename1);
1413 goto out;
1414 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001415 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001416
Fam Zheng25ad8e62016-01-13 16:37:41 +08001417 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1418 total_sectors2 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001419 &pnum2, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001420 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001421 ret = 3;
1422 error_report("Sector allocation test failed for %s", filename2);
1423 goto out;
1424 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001425 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1426 if (pnum1) {
1427 nb_sectors = MIN(nb_sectors, pnum1);
1428 }
1429 if (pnum2) {
1430 nb_sectors = MIN(nb_sectors, pnum2);
1431 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001432
Fam Zheng25ad8e62016-01-13 16:37:41 +08001433 if (strict) {
1434 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1435 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1436 ret = 1;
1437 qprintf(quiet, "Strict mode: Offset %" PRId64
1438 " block status mismatch!\n",
1439 sectors_to_bytes(sector_num));
1440 goto out;
1441 }
1442 }
1443 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1444 nb_sectors = MIN(pnum1, pnum2);
1445 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001446 if (allocated1) {
Eric Blake91669202016-05-06 10:26:43 -06001447 ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1,
1448 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001449 if (ret < 0) {
1450 error_report("Error while reading offset %" PRId64 " of %s:"
1451 " %s", sectors_to_bytes(sector_num), filename1,
1452 strerror(-ret));
1453 ret = 4;
1454 goto out;
1455 }
Eric Blake91669202016-05-06 10:26:43 -06001456 ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2,
1457 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001458 if (ret < 0) {
1459 error_report("Error while reading offset %" PRId64
1460 " of %s: %s", sectors_to_bytes(sector_num),
1461 filename2, strerror(-ret));
1462 ret = 4;
1463 goto out;
1464 }
1465 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1466 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001467 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1468 sectors_to_bytes(
1469 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001470 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001471 goto out;
1472 }
1473 }
1474 } else {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001475
1476 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001477 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001478 filename1, buf1, quiet);
1479 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001480 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001481 filename2, buf1, quiet);
1482 }
1483 if (ret) {
1484 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001485 error_report("Error while reading offset %" PRId64 ": %s",
1486 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001487 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001488 }
1489 goto out;
1490 }
1491 }
1492 sector_num += nb_sectors;
1493 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1494 }
1495
1496 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001497 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001498 int64_t total_sectors_over;
1499 const char *filename_over;
1500
1501 qprintf(quiet, "Warning: Image size mismatch!\n");
1502 if (total_sectors1 > total_sectors2) {
1503 total_sectors_over = total_sectors1;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001504 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001505 filename_over = filename1;
1506 } else {
1507 total_sectors_over = total_sectors2;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001508 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001509 filename_over = filename2;
1510 }
1511
1512 for (;;) {
1513 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1514 if (nb_sectors <= 0) {
1515 break;
1516 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001517 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001518 nb_sectors, &pnum);
1519 if (ret < 0) {
1520 ret = 3;
1521 error_report("Sector allocation test failed for %s",
1522 filename_over);
1523 goto out;
1524
1525 }
1526 nb_sectors = pnum;
1527 if (ret) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001528 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001529 filename_over, buf1, quiet);
1530 if (ret) {
1531 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001532 error_report("Error while reading offset %" PRId64
1533 " of %s: %s", sectors_to_bytes(sector_num),
1534 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001535 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001536 }
1537 goto out;
1538 }
1539 }
1540 sector_num += nb_sectors;
1541 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1542 }
1543 }
1544
1545 qprintf(quiet, "Images are identical.\n");
1546 ret = 0;
1547
1548out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001549 qemu_vfree(buf1);
1550 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001551 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001552out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001553 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001554out3:
1555 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001556out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001557 return ret;
1558}
1559
Kevin Wolf690c7302015-03-19 13:33:32 +01001560enum ImgConvertBlockStatus {
1561 BLK_DATA,
1562 BLK_ZERO,
1563 BLK_BACKING_FILE,
1564};
1565
Peter Lieven2d9187b2017-02-28 13:40:07 +01001566#define MAX_COROUTINES 16
1567
Kevin Wolf690c7302015-03-19 13:33:32 +01001568typedef struct ImgConvertState {
1569 BlockBackend **src;
1570 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001571 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001572 int64_t total_sectors;
1573 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001574 int64_t allocated_done;
1575 int64_t sector_num;
1576 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001577 enum ImgConvertBlockStatus status;
1578 int64_t sector_next_status;
1579 BlockBackend *target;
1580 bool has_zero_init;
1581 bool compressed;
1582 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001583 bool wr_in_order;
Kevin Wolf690c7302015-03-19 13:33:32 +01001584 int min_sparse;
1585 size_t cluster_sectors;
1586 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001587 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001588 int running_coroutines;
1589 Coroutine *co[MAX_COROUTINES];
1590 int64_t wait_sector_num[MAX_COROUTINES];
1591 CoMutex lock;
1592 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001593} ImgConvertState;
1594
Peter Lieven2d9187b2017-02-28 13:40:07 +01001595static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1596 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001597{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001598 *src_cur = 0;
1599 *src_cur_offset = 0;
1600 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1601 *src_cur_offset += s->src_sectors[*src_cur];
1602 (*src_cur)++;
1603 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001604 }
1605}
1606
1607static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1608{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001609 int64_t ret, src_cur_offset;
1610 int n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001611
Peter Lieven2d9187b2017-02-28 13:40:07 +01001612 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001613
1614 assert(s->total_sectors > sector_num);
1615 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1616
1617 if (s->sector_next_status <= sector_num) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08001618 BlockDriverState *file;
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001619 if (s->target_has_backing) {
1620 ret = bdrv_get_block_status(blk_bs(s->src[src_cur]),
1621 sector_num - src_cur_offset,
1622 n, &n, &file);
1623 } else {
1624 ret = bdrv_get_block_status_above(blk_bs(s->src[src_cur]), NULL,
1625 sector_num - src_cur_offset,
1626 n, &n, &file);
1627 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001628 if (ret < 0) {
1629 return ret;
1630 }
1631
1632 if (ret & BDRV_BLOCK_ZERO) {
1633 s->status = BLK_ZERO;
1634 } else if (ret & BDRV_BLOCK_DATA) {
1635 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001636 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001637 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001638 }
1639
1640 s->sector_next_status = sector_num + n;
1641 }
1642
1643 n = MIN(n, s->sector_next_status - sector_num);
1644 if (s->status == BLK_DATA) {
1645 n = MIN(n, s->buf_sectors);
1646 }
1647
1648 /* We need to write complete clusters for compressed images, so if an
1649 * unallocated area is shorter than that, we must consider the whole
1650 * cluster allocated. */
1651 if (s->compressed) {
1652 if (n < s->cluster_sectors) {
1653 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1654 s->status = BLK_DATA;
1655 } else {
1656 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1657 }
1658 }
1659
1660 return n;
1661}
1662
Peter Lieven2d9187b2017-02-28 13:40:07 +01001663static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1664 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001665{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001666 int n, ret;
1667 QEMUIOVector qiov;
1668 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001669
Kevin Wolf690c7302015-03-19 13:33:32 +01001670 assert(nb_sectors <= s->buf_sectors);
1671 while (nb_sectors > 0) {
1672 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001673 int src_cur;
1674 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001675
1676 /* In the case of compression with multiple source files, we can get a
1677 * nb_sectors that spreads into the next part. So we must be able to
1678 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001679 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1680 blk = s->src[src_cur];
1681 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001682
Peter Lieven2d9187b2017-02-28 13:40:07 +01001683 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1684 iov.iov_base = buf;
1685 iov.iov_len = n << BDRV_SECTOR_BITS;
1686 qemu_iovec_init_external(&qiov, &iov, 1);
1687
1688 ret = blk_co_preadv(
1689 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1690 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001691 if (ret < 0) {
1692 return ret;
1693 }
1694
1695 sector_num += n;
1696 nb_sectors -= n;
1697 buf += n * BDRV_SECTOR_SIZE;
1698 }
1699
1700 return 0;
1701}
1702
Peter Lieven2d9187b2017-02-28 13:40:07 +01001703
1704static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1705 int nb_sectors, uint8_t *buf,
1706 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001707{
1708 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001709 QEMUIOVector qiov;
1710 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001711
1712 while (nb_sectors > 0) {
1713 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001714 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1715
Peter Lieven2d9187b2017-02-28 13:40:07 +01001716 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001717 case BLK_BACKING_FILE:
1718 /* If we have a backing file, leave clusters unallocated that are
1719 * unallocated in the source image, so that the backing file is
1720 * visible at the respective offset. */
1721 assert(s->target_has_backing);
1722 break;
1723
1724 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001725 /* If we're told to keep the target fully allocated (-S 0) or there
1726 * is real non-zero data, we must write it. Otherwise we can treat
1727 * it as zero sectors.
1728 * Compressed clusters need to be written as a whole, so in that
1729 * case we can only save the write if the buffer is completely
1730 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001731 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001732 (!s->compressed &&
1733 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1734 (s->compressed &&
1735 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001736 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001737 iov.iov_base = buf;
1738 iov.iov_len = n << BDRV_SECTOR_BITS;
1739 qemu_iovec_init_external(&qiov, &iov, 1);
1740
1741 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
Lidong Chendb933fb2017-04-27 10:58:27 +08001742 n << BDRV_SECTOR_BITS, &qiov, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001743 if (ret < 0) {
1744 return ret;
1745 }
1746 break;
1747 }
1748 /* fall-through */
1749
1750 case BLK_ZERO:
1751 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001752 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001753 break;
1754 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001755 ret = blk_co_pwrite_zeroes(s->target,
1756 sector_num << BDRV_SECTOR_BITS,
1757 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001758 if (ret < 0) {
1759 return ret;
1760 }
1761 break;
1762 }
1763
1764 sector_num += n;
1765 nb_sectors -= n;
1766 buf += n * BDRV_SECTOR_SIZE;
1767 }
1768
1769 return 0;
1770}
1771
Peter Lieven2d9187b2017-02-28 13:40:07 +01001772static void coroutine_fn convert_co_do_copy(void *opaque)
1773{
1774 ImgConvertState *s = opaque;
1775 uint8_t *buf = NULL;
1776 int ret, i;
1777 int index = -1;
1778
1779 for (i = 0; i < s->num_coroutines; i++) {
1780 if (s->co[i] == qemu_coroutine_self()) {
1781 index = i;
1782 break;
1783 }
1784 }
1785 assert(index >= 0);
1786
1787 s->running_coroutines++;
1788 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1789
1790 while (1) {
1791 int n;
1792 int64_t sector_num;
1793 enum ImgConvertBlockStatus status;
1794
1795 qemu_co_mutex_lock(&s->lock);
1796 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1797 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001798 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001799 }
1800 n = convert_iteration_sectors(s, s->sector_num);
1801 if (n < 0) {
1802 qemu_co_mutex_unlock(&s->lock);
1803 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001804 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001805 }
1806 /* save current sector and allocation status to local variables */
1807 sector_num = s->sector_num;
1808 status = s->status;
1809 if (!s->min_sparse && s->status == BLK_ZERO) {
1810 n = MIN(n, s->buf_sectors);
1811 }
1812 /* increment global sector counter so that other coroutines can
1813 * already continue reading beyond this request */
1814 s->sector_num += n;
1815 qemu_co_mutex_unlock(&s->lock);
1816
1817 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1818 s->allocated_done += n;
1819 qemu_progress_print(100.0 * s->allocated_done /
1820 s->allocated_sectors, 0);
1821 }
1822
1823 if (status == BLK_DATA) {
1824 ret = convert_co_read(s, sector_num, n, buf);
1825 if (ret < 0) {
1826 error_report("error while reading sector %" PRId64
1827 ": %s", sector_num, strerror(-ret));
1828 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001829 }
1830 } else if (!s->min_sparse && status == BLK_ZERO) {
1831 status = BLK_DATA;
1832 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1833 }
1834
1835 if (s->wr_in_order) {
1836 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03001837 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001838 s->wait_sector_num[index] = sector_num;
1839 qemu_coroutine_yield();
1840 }
1841 s->wait_sector_num[index] = -1;
1842 }
1843
Anton Nefedovb91127e2017-04-26 11:33:15 +03001844 if (s->ret == -EINPROGRESS) {
1845 ret = convert_co_write(s, sector_num, n, buf, status);
1846 if (ret < 0) {
1847 error_report("error while writing sector %" PRId64
1848 ": %s", sector_num, strerror(-ret));
1849 s->ret = ret;
1850 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001851 }
1852
1853 if (s->wr_in_order) {
1854 /* reenter the coroutine that might have waited
1855 * for this write to complete */
1856 s->wr_offs = sector_num + n;
1857 for (i = 0; i < s->num_coroutines; i++) {
1858 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1859 /*
1860 * A -> B -> A cannot occur because A has
1861 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1862 * B will never enter A during this time window.
1863 */
1864 qemu_coroutine_enter(s->co[i]);
1865 break;
1866 }
1867 }
1868 }
1869 }
1870
Peter Lieven2d9187b2017-02-28 13:40:07 +01001871 qemu_vfree(buf);
1872 s->co[index] = NULL;
1873 s->running_coroutines--;
1874 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1875 /* the convert job finished successfully */
1876 s->ret = 0;
1877 }
1878}
1879
Kevin Wolf690c7302015-03-19 13:33:32 +01001880static int convert_do_copy(ImgConvertState *s)
1881{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001882 int ret, i, n;
1883 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001884
1885 /* Check whether we have zero initialisation or can get it efficiently */
1886 s->has_zero_init = s->min_sparse && !s->target_has_backing
1887 ? bdrv_has_zero_init(blk_bs(s->target))
1888 : false;
1889
1890 if (!s->has_zero_init && !s->target_has_backing &&
1891 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1892 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001893 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001894 if (ret == 0) {
1895 s->has_zero_init = true;
1896 }
1897 }
1898
1899 /* Allocate buffer for copied data. For compressed images, only one cluster
1900 * can be copied at a time. */
1901 if (s->compressed) {
1902 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1903 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001904 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001905 }
1906 s->buf_sectors = s->cluster_sectors;
1907 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001908
Kevin Wolf690c7302015-03-19 13:33:32 +01001909 while (sector_num < s->total_sectors) {
1910 n = convert_iteration_sectors(s, sector_num);
1911 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001912 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001913 }
Max Reitzaad15de2016-03-24 23:33:57 +01001914 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1915 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001916 s->allocated_sectors += n;
1917 }
1918 sector_num += n;
1919 }
1920
1921 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001922 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001923 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001924
Peter Lieven2d9187b2017-02-28 13:40:07 +01001925 qemu_co_mutex_init(&s->lock);
1926 for (i = 0; i < s->num_coroutines; i++) {
1927 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1928 s->wait_sector_num[i] = -1;
1929 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001930 }
1931
Anton Nefedovb91127e2017-04-26 11:33:15 +03001932 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001933 main_loop_wait(false);
1934 }
1935
1936 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001937 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001938 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001939 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001940 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001941 }
1942 }
1943
Peter Lieven2d9187b2017-02-28 13:40:07 +01001944 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001945}
1946
bellardea2384d2004-08-01 21:59:26 +00001947static int img_convert(int argc, char **argv)
1948{
Peter Lieven9fd77f92017-04-21 11:11:55 +02001949 int c, bs_i, flags, src_flags = 0;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001950 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02001951 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1952 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001953 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00001954 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001955 BlockDriverState *out_bs;
1956 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08001957 QemuOptsList *create_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001958 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02001959 Error *local_err = NULL;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001960 bool writethrough, src_writethrough, quiet = false, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001961 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001962 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08001963 bool force_share = false;
bellardea2384d2004-08-01 21:59:26 +00001964
Peter Lieven9fd77f92017-04-21 11:11:55 +02001965 ImgConvertState s = (ImgConvertState) {
1966 /* Need at least 4k of zeros for sparse detection */
1967 .min_sparse = 8,
1968 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1969 .wr_in_order = true,
1970 .num_coroutines = 8,
1971 };
1972
bellardea2384d2004-08-01 21:59:26 +00001973 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001974 static const struct option long_options[] = {
1975 {"help", no_argument, 0, 'h'},
1976 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001977 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001978 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001979 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001980 {0, 0, 0, 0}
1981 };
Fam Zheng335e9932017-05-03 00:35:39 +08001982 c = getopt_long(argc, argv, ":hf:O:B:ce6o:s:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001983 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001984 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001985 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001986 }
bellardea2384d2004-08-01 21:59:26 +00001987 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001988 case ':':
1989 missing_argument(argv[optind - 1]);
1990 break;
Jes Sorensenef873942010-12-06 15:25:40 +01001991 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001992 unrecognized_option(argv[optind - 1]);
1993 break;
bellardea2384d2004-08-01 21:59:26 +00001994 case 'h':
1995 help();
1996 break;
1997 case 'f':
1998 fmt = optarg;
1999 break;
2000 case 'O':
2001 out_fmt = optarg;
2002 break;
thsf58c7b32008-06-05 21:53:49 +00002003 case 'B':
2004 out_baseimg = optarg;
2005 break;
bellardea2384d2004-08-01 21:59:26 +00002006 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002007 s.compressed = true;
bellardea2384d2004-08-01 21:59:26 +00002008 break;
2009 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02002010 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01002011 "encryption\' instead!");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002012 goto fail_getopt;
thsec36ba12007-09-16 21:59:02 +00002013 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02002014 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01002015 "compat6\' instead!");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002016 goto fail_getopt;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002017 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01002018 if (!is_valid_option_list(optarg)) {
2019 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002020 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01002021 }
2022 if (!options) {
2023 options = g_strdup(optarg);
2024 } else {
2025 char *old_options = options;
2026 options = g_strdup_printf("%s,%s", options, optarg);
2027 g_free(old_options);
2028 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002029 break;
edison51ef6722010-09-21 19:58:41 -07002030 case 's':
2031 snapshot_name = optarg;
2032 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08002033 case 'l':
2034 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01002035 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2036 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08002037 if (!sn_opts) {
2038 error_report("Failed in parsing snapshot param '%s'",
2039 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002040 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002041 }
2042 } else {
2043 snapshot_name = optarg;
2044 }
2045 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002046 case 'S':
2047 {
2048 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002049
2050 sval = cvtnum(optarg);
2051 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02002052 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002053 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002054 }
2055
Peter Lieven9fd77f92017-04-21 11:11:55 +02002056 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002057 break;
2058 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002059 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002060 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002061 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002062 case 't':
2063 cache = optarg;
2064 break;
Max Reitz40055952014-07-22 22:58:42 +02002065 case 'T':
2066 src_cache = optarg;
2067 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002068 case 'q':
2069 quiet = true;
2070 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002071 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002072 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002073 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002074 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002075 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2076 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002077 error_report("Invalid number of coroutines. Allowed number of"
2078 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002079 goto fail_getopt;
2080 }
2081 break;
2082 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002083 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002084 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002085 case 'U':
2086 force_share = true;
2087 break;
Max Reitz3258b912017-04-26 15:46:47 +02002088 case OPTION_OBJECT: {
2089 QemuOpts *object_opts;
2090 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2091 optarg, true);
2092 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002093 goto fail_getopt;
2094 }
2095 break;
Max Reitz3258b912017-04-26 15:46:47 +02002096 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002097 case OPTION_IMAGE_OPTS:
2098 image_opts = true;
2099 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002100 case OPTION_TARGET_IMAGE_OPTS:
2101 tgt_image_opts = true;
2102 break;
bellardea2384d2004-08-01 21:59:26 +00002103 }
2104 }
ths3b46e622007-09-17 08:09:54 +00002105
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002106 if (!out_fmt && !tgt_image_opts) {
2107 out_fmt = "raw";
2108 }
2109
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002110 if (qemu_opts_foreach(&qemu_object_opts,
2111 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002112 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002113 goto fail_getopt;
2114 }
2115
Peter Lieven9fd77f92017-04-21 11:11:55 +02002116 if (!s.wr_in_order && s.compressed) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002117 error_report("Out of order write and compress are mutually exclusive");
Peter Lieven9fd77f92017-04-21 11:11:55 +02002118 goto fail_getopt;
2119 }
2120
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002121 if (tgt_image_opts && !skip_create) {
2122 error_report("--target-image-opts requires use of -n flag");
2123 goto fail_getopt;
2124 }
2125
Peter Lieven9fd77f92017-04-21 11:11:55 +02002126 s.src_num = argc - optind - 1;
2127 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2128
2129 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002130 if (out_fmt) {
2131 ret = print_block_option_help(out_filename, out_fmt);
2132 goto fail_getopt;
2133 } else {
2134 error_report("Option help requires a format be specified");
2135 goto fail_getopt;
2136 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002137 }
2138
2139 if (s.src_num < 1) {
2140 error_report("Must specify image file name");
2141 goto fail_getopt;
2142 }
2143
2144
Peter Lieven9fd77f92017-04-21 11:11:55 +02002145 /* ret is still -EINVAL until here */
2146 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2147 if (ret < 0) {
2148 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002149 goto fail_getopt;
2150 }
2151
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002152 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002153 if (quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002154 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002155 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002156 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002157 qemu_progress_print(0, 100);
2158
Peter Lieven9fd77f92017-04-21 11:11:55 +02002159 s.src = g_new0(BlockBackend *, s.src_num);
2160 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002161
Peter Lieven9fd77f92017-04-21 11:11:55 +02002162 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2163 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Fam Zheng335e9932017-05-03 00:35:39 +08002164 fmt, src_flags, src_writethrough, quiet,
2165 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002166 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002167 ret = -1;
2168 goto out;
2169 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002170 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2171 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002172 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002173 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002174 ret = -1;
2175 goto out;
2176 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002177 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002178 }
bellardea2384d2004-08-01 21:59:26 +00002179
Wenchao Xiaef806542013-12-04 17:10:57 +08002180 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002181 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002182 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2183 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2184 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002185 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002186 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002187 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002188 ret = -1;
2189 goto out;
2190 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002191
Peter Lieven9fd77f92017-04-21 11:11:55 +02002192 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2193 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002194 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002195 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002196 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002197 ret = -1;
2198 goto out;
edison51ef6722010-09-21 19:58:41 -07002199 }
2200
Max Reitz2e024cd2015-02-11 09:58:46 -05002201 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002202 /* Find driver and parse its options */
2203 drv = bdrv_find_format(out_fmt);
2204 if (!drv) {
2205 error_report("Unknown file format '%s'", out_fmt);
2206 ret = -1;
2207 goto out;
2208 }
2209
2210 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2211 if (!proto_drv) {
2212 error_report_err(local_err);
2213 ret = -1;
2214 goto out;
2215 }
2216
Max Reitz2e024cd2015-02-11 09:58:46 -05002217 if (!drv->create_opts) {
2218 error_report("Format driver '%s' does not support image creation",
2219 drv->format_name);
2220 ret = -1;
2221 goto out;
2222 }
Max Reitzf75613c2014-12-02 18:32:46 +01002223
Max Reitz2e024cd2015-02-11 09:58:46 -05002224 if (!proto_drv->create_opts) {
2225 error_report("Protocol driver '%s' does not support image creation",
2226 proto_drv->format_name);
2227 ret = -1;
2228 goto out;
2229 }
Max Reitzf75613c2014-12-02 18:32:46 +01002230
Max Reitz2e024cd2015-02-11 09:58:46 -05002231 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2232 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002233
Max Reitz2e024cd2015-02-11 09:58:46 -05002234 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002235 if (options) {
2236 qemu_opts_do_parse(opts, options, NULL, &local_err);
2237 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002238 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002239 ret = -1;
2240 goto out;
2241 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002242 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002243
Peter Lieven9fd77f92017-04-21 11:11:55 +02002244 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002245 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002246 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2247 if (ret < 0) {
2248 goto out;
2249 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002250 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002251
Kevin Wolfa18953f2010-10-14 15:46:04 +02002252 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002253 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002254 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002255 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002256 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002257 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002258
Max Reitz48758a82017-04-26 15:46:48 +02002259 if (s.src_num > 1 && out_baseimg) {
2260 error_report("Having a backing file for the target makes no sense when "
2261 "concatenating multiple input images");
2262 ret = -1;
2263 goto out;
2264 }
2265
Kevin Wolfefa84d42009-05-18 16:42:12 +02002266 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002267 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002268 bool encryption =
2269 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
2270 const char *preallocation =
2271 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002272
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002273 if (drv && !drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002274 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002275 ret = -1;
2276 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002277 }
2278
Chunyan Liu83d05212014-06-05 17:20:51 +08002279 if (encryption) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002280 error_report("Compression and encryption not supported at "
2281 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002282 ret = -1;
2283 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002284 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002285
Chunyan Liu83d05212014-06-05 17:20:51 +08002286 if (preallocation
2287 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002288 {
2289 error_report("Compression and preallocation not supported at "
2290 "the same time");
2291 ret = -1;
2292 goto out;
2293 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002294 }
2295
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002296 if (!skip_create) {
2297 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002298 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002299 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002300 error_reportf_err(local_err, "%s: error while converting %s: ",
2301 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002302 goto out;
bellardea2384d2004-08-01 21:59:26 +00002303 }
2304 }
ths3b46e622007-09-17 08:09:54 +00002305
Peter Lieven9fd77f92017-04-21 11:11:55 +02002306 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002307 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002308 if (ret < 0) {
2309 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002310 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002311 }
2312
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002313 if (skip_create) {
2314 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2315 flags, writethrough, quiet, false);
2316 } else {
2317 /* TODO ultimately we should allow --target-image-opts
2318 * to be used even when -n is not given.
2319 * That has to wait for bdrv_create to be improved
2320 * to allow filenames in option syntax
2321 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01002322 s.target = img_open_new_file(out_filename, opts, out_fmt,
2323 flags, writethrough, quiet, false);
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002324 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002325 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002326 ret = -1;
2327 goto out;
2328 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002329 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002330
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002331 if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2332 error_report("Compression not supported for this file format");
2333 ret = -1;
2334 goto out;
2335 }
2336
Eric Blake5def6b82016-06-23 16:37:19 -06002337 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002338 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2339 * as maximum. */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002340 s.buf_sectors = MIN(32768,
2341 MAX(s.buf_sectors,
2342 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2343 out_bs->bl.pdiscard_alignment >>
2344 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002345
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002346 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002347 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002348 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002349 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002350 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002351 ret = -1;
2352 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002353 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002354 error_report("output file is smaller than input file");
2355 ret = -1;
2356 goto out;
2357 }
2358 }
2359
Peter Lieven24f833c2013-11-27 11:07:07 +01002360 ret = bdrv_get_info(out_bs, &bdi);
2361 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002362 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002363 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002364 goto out;
2365 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002366 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002367 s.compressed = s.compressed || bdi.needs_compressed_writes;
2368 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Peter Lieven24f833c2013-11-27 11:07:07 +01002369 }
2370
Peter Lieven9fd77f92017-04-21 11:11:55 +02002371 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002372out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002373 if (!ret) {
2374 qemu_progress_print(100, 0);
2375 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002376 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002377 qemu_opts_del(opts);
2378 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002379 qemu_opts_del(sn_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002380 blk_unref(s.target);
2381 if (s.src) {
2382 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2383 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002384 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002385 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002386 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002387 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002388fail_getopt:
2389 g_free(options);
2390
Peter Lieven9fd77f92017-04-21 11:11:55 +02002391 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002392}
2393
bellard57d1a2b2004-08-03 21:15:11 +00002394
bellardfaea38e2006-08-05 21:31:00 +00002395static void dump_snapshots(BlockDriverState *bs)
2396{
2397 QEMUSnapshotInfo *sn_tab, *sn;
2398 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002399
2400 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2401 if (nb_sns <= 0)
2402 return;
2403 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002404 bdrv_snapshot_dump(fprintf, stdout, NULL);
2405 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002406 for(i = 0; i < nb_sns; i++) {
2407 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002408 bdrv_snapshot_dump(fprintf, stdout, sn);
2409 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002410 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002411 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002412}
2413
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002414static void dump_json_image_info_list(ImageInfoList *list)
2415{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002416 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002417 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002418 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002419
2420 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2421 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002422 str = qobject_to_json_pretty(obj);
2423 assert(str != NULL);
2424 printf("%s\n", qstring_get_str(str));
2425 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002426 visit_free(v);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002427 QDECREF(str);
2428}
2429
Benoît Canetc054b3f2012-09-05 13:09:02 +02002430static void dump_json_image_info(ImageInfo *info)
2431{
Benoît Canetc054b3f2012-09-05 13:09:02 +02002432 QString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002433 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002434 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002435
2436 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2437 visit_complete(v, &obj);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002438 str = qobject_to_json_pretty(obj);
2439 assert(str != NULL);
2440 printf("%s\n", qstring_get_str(str));
2441 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002442 visit_free(v);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002443 QDECREF(str);
2444}
2445
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002446static void dump_human_image_info_list(ImageInfoList *list)
2447{
2448 ImageInfoList *elem;
2449 bool delim = false;
2450
2451 for (elem = list; elem; elem = elem->next) {
2452 if (delim) {
2453 printf("\n");
2454 }
2455 delim = true;
2456
Wenchao Xia5b917042013-05-25 11:09:45 +08002457 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002458 }
2459}
2460
2461static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2462{
2463 return strcmp(a, b) == 0;
2464}
2465
2466/**
2467 * Open an image file chain and return an ImageInfoList
2468 *
2469 * @filename: topmost image filename
2470 * @fmt: topmost image format (may be NULL to autodetect)
2471 * @chain: true - enumerate entire backing file chain
2472 * false - only topmost image file
2473 *
2474 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2475 * image file. If there was an error a message will have been printed to
2476 * stderr.
2477 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002478static ImageInfoList *collect_image_info_list(bool image_opts,
2479 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002480 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002481 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002482{
2483 ImageInfoList *head = NULL;
2484 ImageInfoList **last = &head;
2485 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002486 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002487
2488 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2489
2490 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002491 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002492 BlockDriverState *bs;
2493 ImageInfo *info;
2494 ImageInfoList *elem;
2495
2496 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2497 error_report("Backing file '%s' creates an infinite loop.",
2498 filename);
2499 goto err;
2500 }
2501 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2502
Max Reitzefaa7c42016-03-16 19:54:38 +01002503 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002504 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2505 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002506 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002507 goto err;
2508 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002509 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002510
Wenchao Xia43526ec2013-06-06 12:27:58 +08002511 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002512 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002513 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002514 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002515 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002516 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002517
2518 elem = g_new0(ImageInfoList, 1);
2519 elem->value = info;
2520 *last = elem;
2521 last = &elem->next;
2522
Markus Armbruster26f54e92014-10-07 13:59:04 +02002523 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002524
2525 filename = fmt = NULL;
2526 if (chain) {
2527 if (info->has_full_backing_filename) {
2528 filename = info->full_backing_filename;
2529 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002530 error_report("Could not determine absolute backing filename,"
2531 " but backing filename '%s' present",
2532 info->backing_filename);
2533 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002534 }
2535 if (info->has_backing_filename_format) {
2536 fmt = info->backing_filename_format;
2537 }
2538 }
2539 }
2540 g_hash_table_destroy(filenames);
2541 return head;
2542
2543err:
2544 qapi_free_ImageInfoList(head);
2545 g_hash_table_destroy(filenames);
2546 return NULL;
2547}
2548
Benoît Canetc054b3f2012-09-05 13:09:02 +02002549static int img_info(int argc, char **argv)
2550{
2551 int c;
2552 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002553 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002554 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002555 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002556 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002557 bool force_share = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002558
bellardea2384d2004-08-01 21:59:26 +00002559 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002560 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002561 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002562 int option_index = 0;
2563 static const struct option long_options[] = {
2564 {"help", no_argument, 0, 'h'},
2565 {"format", required_argument, 0, 'f'},
2566 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002567 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002568 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002569 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002570 {"force-share", no_argument, 0, 'U'},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002571 {0, 0, 0, 0}
2572 };
Fam Zheng335e9932017-05-03 00:35:39 +08002573 c = getopt_long(argc, argv, ":f:hU",
Benoît Canetc054b3f2012-09-05 13:09:02 +02002574 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002575 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002576 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002577 }
bellardea2384d2004-08-01 21:59:26 +00002578 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002579 case ':':
2580 missing_argument(argv[optind - 1]);
2581 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002582 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002583 unrecognized_option(argv[optind - 1]);
2584 break;
bellardea2384d2004-08-01 21:59:26 +00002585 case 'h':
2586 help();
2587 break;
2588 case 'f':
2589 fmt = optarg;
2590 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002591 case 'U':
2592 force_share = true;
2593 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002594 case OPTION_OUTPUT:
2595 output = optarg;
2596 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002597 case OPTION_BACKING_CHAIN:
2598 chain = true;
2599 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002600 case OPTION_OBJECT: {
2601 QemuOpts *opts;
2602 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2603 optarg, true);
2604 if (!opts) {
2605 return 1;
2606 }
2607 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002608 case OPTION_IMAGE_OPTS:
2609 image_opts = true;
2610 break;
bellardea2384d2004-08-01 21:59:26 +00002611 }
2612 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002613 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002614 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002615 }
bellardea2384d2004-08-01 21:59:26 +00002616 filename = argv[optind++];
2617
Benoît Canetc054b3f2012-09-05 13:09:02 +02002618 if (output && !strcmp(output, "json")) {
2619 output_format = OFORMAT_JSON;
2620 } else if (output && !strcmp(output, "human")) {
2621 output_format = OFORMAT_HUMAN;
2622 } else if (output) {
2623 error_report("--output must be used with human or json as argument.");
2624 return 1;
2625 }
2626
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002627 if (qemu_opts_foreach(&qemu_object_opts,
2628 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002629 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002630 return 1;
2631 }
2632
Fam Zheng335e9932017-05-03 00:35:39 +08002633 list = collect_image_info_list(image_opts, filename, fmt, chain,
2634 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002635 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002636 return 1;
2637 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002638
Benoît Canetc054b3f2012-09-05 13:09:02 +02002639 switch (output_format) {
2640 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002641 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002642 break;
2643 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002644 if (chain) {
2645 dump_json_image_info_list(list);
2646 } else {
2647 dump_json_image_info(list->value);
2648 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002649 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002650 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002651
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002652 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002653 return 0;
2654}
2655
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002656static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2657 MapEntry *next)
2658{
2659 switch (output_format) {
2660 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002661 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002662 error_report("File contains external, encrypted or compressed clusters.");
2663 exit(1);
2664 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002665 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002666 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002667 e->start, e->length,
2668 e->has_offset ? e->offset : 0,
2669 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002670 }
2671 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2672 * Modify the flags here to allow more coalescing.
2673 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002674 if (next && (!next->data || next->zero)) {
2675 next->data = false;
2676 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002677 }
2678 break;
2679 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002680 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2681 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002682 (e->start == 0 ? "[" : ",\n"),
2683 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002684 e->zero ? "true" : "false",
2685 e->data ? "true" : "false");
2686 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002687 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002688 }
2689 putchar('}');
2690
2691 if (!next) {
2692 printf("]\n");
2693 }
2694 break;
2695 }
2696}
2697
2698static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2699 int nb_sectors, MapEntry *e)
2700{
2701 int64_t ret;
2702 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002703 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002704 bool has_offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002705
2706 /* As an optimization, we could cache the current range of unallocated
2707 * clusters in each file of the chain, and avoid querying the same
2708 * range repeatedly.
2709 */
2710
2711 depth = 0;
2712 for (;;) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08002713 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2714 &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002715 if (ret < 0) {
2716 return ret;
2717 }
2718 assert(nb_sectors);
2719 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2720 break;
2721 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002722 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002723 if (bs == NULL) {
2724 ret = 0;
2725 break;
2726 }
2727
2728 depth++;
2729 }
2730
John Snow28756452016-02-05 13:12:33 -05002731 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2732
2733 *e = (MapEntry) {
2734 .start = sector_num * BDRV_SECTOR_SIZE,
2735 .length = nb_sectors * BDRV_SECTOR_SIZE,
2736 .data = !!(ret & BDRV_BLOCK_DATA),
2737 .zero = !!(ret & BDRV_BLOCK_ZERO),
2738 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2739 .has_offset = has_offset,
2740 .depth = depth,
2741 .has_filename = file && has_offset,
2742 .filename = file && has_offset ? file->filename : NULL,
2743 };
2744
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002745 return 0;
2746}
2747
Fam Zheng16b0d552016-01-26 11:59:02 +08002748static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2749{
2750 if (curr->length == 0) {
2751 return false;
2752 }
2753 if (curr->zero != next->zero ||
2754 curr->data != next->data ||
2755 curr->depth != next->depth ||
2756 curr->has_filename != next->has_filename ||
2757 curr->has_offset != next->has_offset) {
2758 return false;
2759 }
2760 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2761 return false;
2762 }
2763 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2764 return false;
2765 }
2766 return true;
2767}
2768
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002769static int img_map(int argc, char **argv)
2770{
2771 int c;
2772 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002773 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002774 BlockDriverState *bs;
2775 const char *filename, *fmt, *output;
2776 int64_t length;
2777 MapEntry curr = { .length = 0 }, next;
2778 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002779 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002780 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002781
2782 fmt = NULL;
2783 output = NULL;
2784 for (;;) {
2785 int option_index = 0;
2786 static const struct option long_options[] = {
2787 {"help", no_argument, 0, 'h'},
2788 {"format", required_argument, 0, 'f'},
2789 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002790 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002791 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002792 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002793 {0, 0, 0, 0}
2794 };
Fam Zheng335e9932017-05-03 00:35:39 +08002795 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002796 long_options, &option_index);
2797 if (c == -1) {
2798 break;
2799 }
2800 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002801 case ':':
2802 missing_argument(argv[optind - 1]);
2803 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002804 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002805 unrecognized_option(argv[optind - 1]);
2806 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002807 case 'h':
2808 help();
2809 break;
2810 case 'f':
2811 fmt = optarg;
2812 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002813 case 'U':
2814 force_share = true;
2815 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002816 case OPTION_OUTPUT:
2817 output = optarg;
2818 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002819 case OPTION_OBJECT: {
2820 QemuOpts *opts;
2821 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2822 optarg, true);
2823 if (!opts) {
2824 return 1;
2825 }
2826 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002827 case OPTION_IMAGE_OPTS:
2828 image_opts = true;
2829 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002830 }
2831 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002832 if (optind != argc - 1) {
2833 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002834 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002835 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002836
2837 if (output && !strcmp(output, "json")) {
2838 output_format = OFORMAT_JSON;
2839 } else if (output && !strcmp(output, "human")) {
2840 output_format = OFORMAT_HUMAN;
2841 } else if (output) {
2842 error_report("--output must be used with human or json as argument.");
2843 return 1;
2844 }
2845
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002846 if (qemu_opts_foreach(&qemu_object_opts,
2847 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002848 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002849 return 1;
2850 }
2851
Fam Zheng335e9932017-05-03 00:35:39 +08002852 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002853 if (!blk) {
2854 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002855 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002856 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002857
2858 if (output_format == OFORMAT_HUMAN) {
2859 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2860 }
2861
Max Reitzf1d3cd72015-02-05 13:58:18 -05002862 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002863 while (curr.start + curr.length < length) {
2864 int64_t nsectors_left;
2865 int64_t sector_num;
2866 int n;
2867
2868 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2869
2870 /* Probe up to 1 GiB at a time. */
2871 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2872 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2873 ret = get_block_status(bs, sector_num, n, &next);
2874
2875 if (ret < 0) {
2876 error_report("Could not read file metadata: %s", strerror(-ret));
2877 goto out;
2878 }
2879
Fam Zheng16b0d552016-01-26 11:59:02 +08002880 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002881 curr.length += next.length;
2882 continue;
2883 }
2884
2885 if (curr.length > 0) {
2886 dump_map_entry(output_format, &curr, &next);
2887 }
2888 curr = next;
2889 }
2890
2891 dump_map_entry(output_format, &curr, NULL);
2892
2893out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002894 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002895 return ret < 0;
2896}
2897
aliguorif7b4a942009-01-07 17:40:15 +00002898#define SNAPSHOT_LIST 1
2899#define SNAPSHOT_CREATE 2
2900#define SNAPSHOT_APPLY 3
2901#define SNAPSHOT_DELETE 4
2902
Stuart Brady153859b2009-06-07 00:42:17 +01002903static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002904{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002905 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002906 BlockDriverState *bs;
2907 QEMUSnapshotInfo sn;
2908 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002909 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002910 int action = 0;
2911 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002912 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002913 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002914 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002915 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00002916
Kevin Wolfce099542016-03-15 13:01:04 +01002917 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002918 /* Parse commandline parameters */
2919 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002920 static const struct option long_options[] = {
2921 {"help", no_argument, 0, 'h'},
2922 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002923 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002924 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002925 {0, 0, 0, 0}
2926 };
Fam Zheng335e9932017-05-03 00:35:39 +08002927 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002928 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002929 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002930 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002931 }
aliguorif7b4a942009-01-07 17:40:15 +00002932 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002933 case ':':
2934 missing_argument(argv[optind - 1]);
2935 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002936 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002937 unrecognized_option(argv[optind - 1]);
2938 break;
aliguorif7b4a942009-01-07 17:40:15 +00002939 case 'h':
2940 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002941 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002942 case 'l':
2943 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002944 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002945 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002946 }
2947 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002948 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002949 break;
2950 case 'a':
2951 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002952 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002953 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002954 }
2955 action = SNAPSHOT_APPLY;
2956 snapshot_name = optarg;
2957 break;
2958 case 'c':
2959 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002960 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002961 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002962 }
2963 action = SNAPSHOT_CREATE;
2964 snapshot_name = optarg;
2965 break;
2966 case 'd':
2967 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002968 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002969 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002970 }
2971 action = SNAPSHOT_DELETE;
2972 snapshot_name = optarg;
2973 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002974 case 'q':
2975 quiet = true;
2976 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002977 case 'U':
2978 force_share = true;
2979 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002980 case OPTION_OBJECT: {
2981 QemuOpts *opts;
2982 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2983 optarg, true);
2984 if (!opts) {
2985 return 1;
2986 }
2987 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002988 case OPTION_IMAGE_OPTS:
2989 image_opts = true;
2990 break;
aliguorif7b4a942009-01-07 17:40:15 +00002991 }
2992 }
2993
Kevin Wolffc11eb22013-08-05 10:53:04 +02002994 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002995 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002996 }
aliguorif7b4a942009-01-07 17:40:15 +00002997 filename = argv[optind++];
2998
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002999 if (qemu_opts_foreach(&qemu_object_opts,
3000 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003001 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003002 return 1;
3003 }
3004
aliguorif7b4a942009-01-07 17:40:15 +00003005 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08003006 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
3007 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003008 if (!blk) {
3009 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003010 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003011 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00003012
3013 /* Perform the requested action */
3014 switch(action) {
3015 case SNAPSHOT_LIST:
3016 dump_snapshots(bs);
3017 break;
3018
3019 case SNAPSHOT_CREATE:
3020 memset(&sn, 0, sizeof(sn));
3021 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
3022
3023 qemu_gettimeofday(&tv);
3024 sn.date_sec = tv.tv_sec;
3025 sn.date_nsec = tv.tv_usec * 1000;
3026
3027 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003028 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003029 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003030 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003031 }
aliguorif7b4a942009-01-07 17:40:15 +00003032 break;
3033
3034 case SNAPSHOT_APPLY:
3035 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003036 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003037 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003038 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003039 }
aliguorif7b4a942009-01-07 17:40:15 +00003040 break;
3041
3042 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003043 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003044 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003045 error_reportf_err(err, "Could not delete snapshot '%s': ",
3046 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003047 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003048 }
aliguorif7b4a942009-01-07 17:40:15 +00003049 break;
3050 }
3051
3052 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003053 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003054 if (ret) {
3055 return 1;
3056 }
Stuart Brady153859b2009-06-07 00:42:17 +01003057 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003058}
3059
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003060static int img_rebase(int argc, char **argv)
3061{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003062 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003063 uint8_t *buf_old = NULL;
3064 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05003065 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003066 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003067 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3068 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003069 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003070 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003071 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003072 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003073 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003074 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003075 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003076
3077 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003078 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003079 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003080 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003081 out_baseimg = NULL;
3082 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003083 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003084 static const struct option long_options[] = {
3085 {"help", no_argument, 0, 'h'},
3086 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003087 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003088 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003089 {0, 0, 0, 0}
3090 };
Fam Zheng335e9932017-05-03 00:35:39 +08003091 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003092 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003093 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003094 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003095 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003096 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003097 case ':':
3098 missing_argument(argv[optind - 1]);
3099 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003100 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003101 unrecognized_option(argv[optind - 1]);
3102 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003103 case 'h':
3104 help();
3105 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003106 case 'f':
3107 fmt = optarg;
3108 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003109 case 'F':
3110 out_basefmt = optarg;
3111 break;
3112 case 'b':
3113 out_baseimg = optarg;
3114 break;
3115 case 'u':
3116 unsafe = 1;
3117 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003118 case 'p':
3119 progress = 1;
3120 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003121 case 't':
3122 cache = optarg;
3123 break;
Max Reitz40055952014-07-22 22:58:42 +02003124 case 'T':
3125 src_cache = optarg;
3126 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003127 case 'q':
3128 quiet = true;
3129 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003130 case OPTION_OBJECT: {
3131 QemuOpts *opts;
3132 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3133 optarg, true);
3134 if (!opts) {
3135 return 1;
3136 }
3137 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003138 case OPTION_IMAGE_OPTS:
3139 image_opts = true;
3140 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003141 case 'U':
3142 force_share = true;
3143 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003144 }
3145 }
3146
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003147 if (quiet) {
3148 progress = 0;
3149 }
3150
Fam Zhengac1307a2014-04-22 13:36:11 +08003151 if (optind != argc - 1) {
3152 error_exit("Expecting one image file name");
3153 }
3154 if (!unsafe && !out_baseimg) {
3155 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003156 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003157 filename = argv[optind++];
3158
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003159 if (qemu_opts_foreach(&qemu_object_opts,
3160 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003161 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003162 return 1;
3163 }
3164
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003165 qemu_progress_init(progress, 2.0);
3166 qemu_progress_print(0, 100);
3167
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003168 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003169 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003170 if (ret < 0) {
3171 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003172 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003173 }
3174
Kevin Wolfce099542016-03-15 13:01:04 +01003175 src_flags = 0;
3176 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003177 if (ret < 0) {
3178 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003179 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003180 }
3181
Kevin Wolfce099542016-03-15 13:01:04 +01003182 /* The source files are opened read-only, don't care about WCE */
3183 assert((src_flags & BDRV_O_RDWR) == 0);
3184 (void) src_writethrough;
3185
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003186 /*
3187 * Open the images.
3188 *
3189 * Ignore the old backing file for unsafe rebase in case we want to correct
3190 * the reference to a renamed or moved backing file.
3191 */
Fam Zheng335e9932017-05-03 00:35:39 +08003192 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3193 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003194 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003195 ret = -1;
3196 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003197 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003198 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003199
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003200 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003201 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003202 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003203 ret = -1;
3204 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003205 }
3206 }
3207
3208 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003209 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003210 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003211 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003212
Max Reitz644483d2015-02-05 13:58:17 -05003213 if (bs->backing_format[0] != '\0') {
3214 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003215 qdict_put_str(options, "driver", bs->backing_format);
Max Reitz644483d2015-02-05 13:58:17 -05003216 }
3217
Fam Zheng335e9932017-05-03 00:35:39 +08003218 if (force_share) {
3219 if (!options) {
3220 options = qdict_new();
3221 }
3222 qdict_put(options, BDRV_OPT_FORCE_SHARE,
3223 qbool_from_bool(true));
3224 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003225 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003226 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003227 options, src_flags, &local_err);
3228 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003229 error_reportf_err(local_err,
3230 "Could not open old backing file '%s': ",
3231 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003232 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003233 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003234 }
Max Reitz644483d2015-02-05 13:58:17 -05003235
Alex Bligha6166732012-10-16 13:46:18 +01003236 if (out_baseimg[0]) {
Fam Zheng335e9932017-05-03 00:35:39 +08003237 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003238 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003239 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003240 }
3241 if (force_share) {
3242 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003243 }
3244
Max Reitzefaa7c42016-03-16 19:54:38 +01003245 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003246 options, src_flags, &local_err);
3247 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003248 error_reportf_err(local_err,
3249 "Could not open new backing file '%s': ",
3250 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003251 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003252 goto out;
3253 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003254 }
3255 }
3256
3257 /*
3258 * Check each unallocated cluster in the COW file. If it is unallocated,
3259 * accesses go to the backing file. We must therefore compare this cluster
3260 * in the old and new backing file, and if they differ we need to copy it
3261 * from the old backing file into the COW file.
3262 *
3263 * If qemu-img crashes during this step, no harm is done. The content of
3264 * the image is the same as the original one at any time.
3265 */
3266 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003267 int64_t num_sectors;
3268 int64_t old_backing_num_sectors;
3269 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003270 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003271 int n;
Kevin Wolf1f710492012-10-12 14:29:18 +02003272 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003273
Max Reitzf1d3cd72015-02-05 13:58:18 -05003274 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3275 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003276
Max Reitzf1d3cd72015-02-05 13:58:18 -05003277 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003278 if (num_sectors < 0) {
3279 error_report("Could not get size of '%s': %s",
3280 filename, strerror(-num_sectors));
3281 ret = -1;
3282 goto out;
3283 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003284 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003285 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003286 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003287
3288 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3289 error_report("Could not get size of '%s': %s",
3290 backing_name, strerror(-old_backing_num_sectors));
3291 ret = -1;
3292 goto out;
3293 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003294 if (blk_new_backing) {
3295 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003296 if (new_backing_num_sectors < 0) {
3297 error_report("Could not get size of '%s': %s",
3298 out_baseimg, strerror(-new_backing_num_sectors));
3299 ret = -1;
3300 goto out;
3301 }
Alex Bligha6166732012-10-16 13:46:18 +01003302 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003303
Kevin Wolf1f710492012-10-12 14:29:18 +02003304 if (num_sectors != 0) {
3305 local_progress = (float)100 /
3306 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
3307 }
3308
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003309 for (sector = 0; sector < num_sectors; sector += n) {
3310
3311 /* How many sectors can we handle with the next read? */
3312 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
3313 n = (IO_BUF_SIZE / 512);
3314 } else {
3315 n = num_sectors - sector;
3316 }
3317
3318 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02003319 ret = bdrv_is_allocated(bs, sector, n, &n);
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 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003325 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003326 continue;
3327 }
3328
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003329 /*
3330 * Read old and new backing file and take into consideration that
3331 * backing files may be smaller than the COW image.
3332 */
3333 if (sector >= old_backing_num_sectors) {
3334 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3335 } else {
3336 if (sector + n > old_backing_num_sectors) {
3337 n = old_backing_num_sectors - sector;
3338 }
3339
Eric Blake91669202016-05-06 10:26:43 -06003340 ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS,
3341 buf_old, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003342 if (ret < 0) {
3343 error_report("error while reading from old backing file");
3344 goto out;
3345 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003346 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003347
Max Reitzf1d3cd72015-02-05 13:58:18 -05003348 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003349 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3350 } else {
3351 if (sector + n > new_backing_num_sectors) {
3352 n = new_backing_num_sectors - sector;
3353 }
3354
Eric Blake91669202016-05-06 10:26:43 -06003355 ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS,
3356 buf_new, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003357 if (ret < 0) {
3358 error_report("error while reading from new backing file");
3359 goto out;
3360 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003361 }
3362
3363 /* If they differ, we need to write to the COW file */
3364 uint64_t written = 0;
3365
3366 while (written < n) {
3367 int pnum;
3368
3369 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003370 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003371 {
Eric Blake91669202016-05-06 10:26:43 -06003372 ret = blk_pwrite(blk,
3373 (sector + written) << BDRV_SECTOR_BITS,
3374 buf_old + written * 512,
3375 pnum << BDRV_SECTOR_BITS, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003376 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003377 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003378 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003379 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003380 }
3381 }
3382
3383 written += pnum;
3384 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003385 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003386 }
3387 }
3388
3389 /*
3390 * Change the backing file. All clusters that are different from the old
3391 * backing file are overwritten in the COW file now, so the visible content
3392 * doesn't change when we switch the backing file.
3393 */
Alex Bligha6166732012-10-16 13:46:18 +01003394 if (out_baseimg && *out_baseimg) {
3395 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3396 } else {
3397 ret = bdrv_change_backing_file(bs, NULL, NULL);
3398 }
3399
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003400 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003401 error_report("Could not change the backing file to '%s': No "
3402 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003403 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003404 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003405 out_baseimg, strerror(-ret));
3406 }
3407
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003408 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003409 /*
3410 * TODO At this point it is possible to check if any clusters that are
3411 * allocated in the COW file are the same in the backing file. If so, they
3412 * could be dropped from the COW file. Don't do this before switching the
3413 * backing file, in case of a crash this would lead to corruption.
3414 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003415out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003416 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003417 /* Cleanup */
3418 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003419 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003420 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003421 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003422 qemu_vfree(buf_old);
3423 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003424
Markus Armbruster26f54e92014-10-07 13:59:04 +02003425 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003426 if (ret) {
3427 return 1;
3428 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003429 return 0;
3430}
3431
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003432static int img_resize(int argc, char **argv)
3433{
Markus Armbruster6750e792015-02-12 17:43:08 +01003434 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003435 int c, ret, relative;
3436 const char *filename, *fmt, *size;
3437 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003438 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003439 BlockBackend *blk = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003440 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003441
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003442 static QemuOptsList resize_options = {
3443 .name = "resize_options",
3444 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3445 .desc = {
3446 {
3447 .name = BLOCK_OPT_SIZE,
3448 .type = QEMU_OPT_SIZE,
3449 .help = "Virtual disk size"
3450 }, {
3451 /* end of list */
3452 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003453 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003454 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003455 bool image_opts = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003456
Kevin Wolfe80fec72011-04-29 10:58:12 +02003457 /* Remove size from argv manually so that negative numbers are not treated
3458 * as options by getopt. */
3459 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003460 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003461 return 1;
3462 }
3463
3464 size = argv[--argc];
3465
3466 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003467 fmt = NULL;
3468 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003469 static const struct option long_options[] = {
3470 {"help", no_argument, 0, 'h'},
3471 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003472 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003473 {0, 0, 0, 0}
3474 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003475 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003476 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003477 if (c == -1) {
3478 break;
3479 }
3480 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003481 case ':':
3482 missing_argument(argv[optind - 1]);
3483 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003484 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003485 unrecognized_option(argv[optind - 1]);
3486 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003487 case 'h':
3488 help();
3489 break;
3490 case 'f':
3491 fmt = optarg;
3492 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003493 case 'q':
3494 quiet = true;
3495 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003496 case OPTION_OBJECT: {
3497 QemuOpts *opts;
3498 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3499 optarg, true);
3500 if (!opts) {
3501 return 1;
3502 }
3503 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003504 case OPTION_IMAGE_OPTS:
3505 image_opts = true;
3506 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003507 }
3508 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003509 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003510 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003511 }
3512 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003513
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003514 if (qemu_opts_foreach(&qemu_object_opts,
3515 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003516 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003517 return 1;
3518 }
3519
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003520 /* Choose grow, shrink, or absolute resize mode */
3521 switch (size[0]) {
3522 case '+':
3523 relative = 1;
3524 size++;
3525 break;
3526 case '-':
3527 relative = -1;
3528 size++;
3529 break;
3530 default:
3531 relative = 0;
3532 break;
3533 }
3534
3535 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003536 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003537 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003538 if (err) {
3539 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003540 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003541 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003542 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003543 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003544 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3545 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003546
Max Reitzefaa7c42016-03-16 19:54:38 +01003547 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003548 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3549 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003550 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003551 ret = -1;
3552 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003553 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003554
3555 if (relative) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003556 total_size = blk_getlength(blk) + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003557 } else {
3558 total_size = n;
3559 }
3560 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003561 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003562 ret = -1;
3563 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003564 }
3565
Max Reitzed3d2ec2017-03-28 22:51:27 +02003566 ret = blk_truncate(blk, total_size, &err);
3567 if (!ret) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003568 qprintf(quiet, "Image resized.\n");
Max Reitzed3d2ec2017-03-28 22:51:27 +02003569 } else {
3570 error_report_err(err);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003571 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003572out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003573 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003574 if (ret) {
3575 return 1;
3576 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003577 return 0;
3578}
3579
Max Reitz76a3a342014-10-27 11:12:51 +01003580static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003581 int64_t offset, int64_t total_work_size,
3582 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003583{
3584 qemu_progress_print(100.f * offset / total_work_size, 0);
3585}
3586
Max Reitz6f176b42013-09-03 10:09:50 +02003587static int img_amend(int argc, char **argv)
3588{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003589 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003590 int c, ret = 0;
3591 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003592 QemuOptsList *create_opts = NULL;
3593 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003594 const char *fmt = NULL, *filename, *cache;
3595 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003596 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003597 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003598 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003599 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003600 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003601
Max Reitzbd39e6e2014-07-22 22:58:43 +02003602 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003603 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003604 static const struct option long_options[] = {
3605 {"help", no_argument, 0, 'h'},
3606 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003607 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003608 {0, 0, 0, 0}
3609 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003610 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003611 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003612 if (c == -1) {
3613 break;
3614 }
3615
3616 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003617 case ':':
3618 missing_argument(argv[optind - 1]);
3619 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003620 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003621 unrecognized_option(argv[optind - 1]);
3622 break;
3623 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003624 help();
3625 break;
3626 case 'o':
3627 if (!is_valid_option_list(optarg)) {
3628 error_report("Invalid option list: %s", optarg);
3629 ret = -1;
3630 goto out_no_progress;
3631 }
3632 if (!options) {
3633 options = g_strdup(optarg);
3634 } else {
3635 char *old_options = options;
3636 options = g_strdup_printf("%s,%s", options, optarg);
3637 g_free(old_options);
3638 }
3639 break;
3640 case 'f':
3641 fmt = optarg;
3642 break;
3643 case 't':
3644 cache = optarg;
3645 break;
3646 case 'p':
3647 progress = true;
3648 break;
3649 case 'q':
3650 quiet = true;
3651 break;
3652 case OPTION_OBJECT:
3653 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3654 optarg, true);
3655 if (!opts) {
3656 ret = -1;
3657 goto out_no_progress;
3658 }
3659 break;
3660 case OPTION_IMAGE_OPTS:
3661 image_opts = true;
3662 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003663 }
3664 }
3665
Max Reitz6f176b42013-09-03 10:09:50 +02003666 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003667 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003668 }
3669
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003670 if (qemu_opts_foreach(&qemu_object_opts,
3671 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003672 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003673 ret = -1;
3674 goto out_no_progress;
3675 }
3676
Max Reitz76a3a342014-10-27 11:12:51 +01003677 if (quiet) {
3678 progress = false;
3679 }
3680 qemu_progress_init(progress, 1.0);
3681
Kevin Wolfa283cb62014-02-21 16:24:07 +01003682 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3683 if (fmt && has_help_option(options)) {
3684 /* If a format is explicitly specified (and possibly no filename is
3685 * given), print option help here */
3686 ret = print_block_option_help(filename, fmt);
3687 goto out;
3688 }
3689
3690 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003691 error_report("Expecting one image file name");
3692 ret = -1;
3693 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003694 }
Max Reitz6f176b42013-09-03 10:09:50 +02003695
Kevin Wolfce099542016-03-15 13:01:04 +01003696 flags = BDRV_O_RDWR;
3697 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003698 if (ret < 0) {
3699 error_report("Invalid cache option: %s", cache);
3700 goto out;
3701 }
3702
Fam Zheng335e9932017-05-03 00:35:39 +08003703 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3704 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003705 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003706 ret = -1;
3707 goto out;
3708 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003709 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003710
3711 fmt = bs->drv->format_name;
3712
Kevin Wolf626f84f2014-02-21 16:24:06 +01003713 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003714 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003715 ret = print_block_option_help(filename, fmt);
3716 goto out;
3717 }
3718
Max Reitzb2439d22014-12-02 18:32:47 +01003719 if (!bs->drv->create_opts) {
3720 error_report("Format driver '%s' does not support any options to amend",
3721 fmt);
3722 ret = -1;
3723 goto out;
3724 }
3725
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003726 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003727 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003728 qemu_opts_do_parse(opts, options, NULL, &err);
3729 if (err) {
3730 error_report_err(err);
3731 ret = -1;
3732 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003733 }
3734
Max Reitz76a3a342014-10-27 11:12:51 +01003735 /* In case the driver does not call amend_status_cb() */
3736 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003737 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003738 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003739 if (ret < 0) {
3740 error_report("Error while amending options: %s", strerror(-ret));
3741 goto out;
3742 }
3743
3744out:
Max Reitz76a3a342014-10-27 11:12:51 +01003745 qemu_progress_end();
3746
Max Reitze814dff2015-08-20 16:00:38 -07003747out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003748 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003749 qemu_opts_del(opts);
3750 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003751 g_free(options);
3752
Max Reitz6f176b42013-09-03 10:09:50 +02003753 if (ret) {
3754 return 1;
3755 }
3756 return 0;
3757}
3758
Kevin Wolfb6133b82014-08-05 14:17:13 +02003759typedef struct BenchData {
3760 BlockBackend *blk;
3761 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003762 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003763 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003764 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003765 int nrreq;
3766 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003767 int flush_interval;
3768 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003769 uint8_t *buf;
3770 QEMUIOVector *qiov;
3771
3772 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003773 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003774 uint64_t offset;
3775} BenchData;
3776
Kevin Wolf55d539c2016-06-03 13:59:41 +02003777static void bench_undrained_flush_cb(void *opaque, int ret)
3778{
3779 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003780 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003781 exit(EXIT_FAILURE);
3782 }
3783}
3784
Kevin Wolfb6133b82014-08-05 14:17:13 +02003785static void bench_cb(void *opaque, int ret)
3786{
3787 BenchData *b = opaque;
3788 BlockAIOCB *acb;
3789
3790 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003791 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003792 exit(EXIT_FAILURE);
3793 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003794
3795 if (b->in_flush) {
3796 /* Just finished a flush with drained queue: Start next requests */
3797 assert(b->in_flight == 0);
3798 b->in_flush = false;
3799 } else if (b->in_flight > 0) {
3800 int remaining = b->n - b->in_flight;
3801
Kevin Wolfb6133b82014-08-05 14:17:13 +02003802 b->n--;
3803 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003804
3805 /* Time for flush? Drain queue if requested, then flush */
3806 if (b->flush_interval && remaining % b->flush_interval == 0) {
3807 if (!b->in_flight || !b->drain_on_flush) {
3808 BlockCompletionFunc *cb;
3809
3810 if (b->drain_on_flush) {
3811 b->in_flush = true;
3812 cb = bench_cb;
3813 } else {
3814 cb = bench_undrained_flush_cb;
3815 }
3816
3817 acb = blk_aio_flush(b->blk, cb, b);
3818 if (!acb) {
3819 error_report("Failed to issue flush request");
3820 exit(EXIT_FAILURE);
3821 }
3822 }
3823 if (b->drain_on_flush) {
3824 return;
3825 }
3826 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003827 }
3828
3829 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003830 int64_t offset = b->offset;
3831 /* blk_aio_* might look for completed I/Os and kick bench_cb
3832 * again, so make sure this operation is counted by in_flight
3833 * and b->offset is ready for the next submission.
3834 */
3835 b->in_flight++;
3836 b->offset += b->step;
3837 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003838 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003839 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003840 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003841 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003842 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003843 if (!acb) {
3844 error_report("Failed to issue request");
3845 exit(EXIT_FAILURE);
3846 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003847 }
3848}
3849
3850static int img_bench(int argc, char **argv)
3851{
3852 int c, ret = 0;
3853 const char *fmt = NULL, *filename;
3854 bool quiet = false;
3855 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003856 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003857 int count = 75000;
3858 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003859 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003860 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003861 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003862 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003863 int flush_interval = 0;
3864 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003865 int64_t image_size;
3866 BlockBackend *blk = NULL;
3867 BenchData data = {};
3868 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003869 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003870 struct timeval t1, t2;
3871 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08003872 bool force_share = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003873
3874 for (;;) {
3875 static const struct option long_options[] = {
3876 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003877 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003878 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003879 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003880 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08003881 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003882 {0, 0, 0, 0}
3883 };
Fam Zheng335e9932017-05-03 00:35:39 +08003884 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003885 if (c == -1) {
3886 break;
3887 }
3888
3889 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003890 case ':':
3891 missing_argument(argv[optind - 1]);
3892 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003893 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003894 unrecognized_option(argv[optind - 1]);
3895 break;
3896 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02003897 help();
3898 break;
3899 case 'c':
3900 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003901 unsigned long res;
3902
3903 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003904 error_report("Invalid request count specified");
3905 return 1;
3906 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003907 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003908 break;
3909 }
3910 case 'd':
3911 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003912 unsigned long res;
3913
3914 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003915 error_report("Invalid queue depth specified");
3916 return 1;
3917 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003918 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003919 break;
3920 }
3921 case 'f':
3922 fmt = optarg;
3923 break;
3924 case 'n':
3925 flags |= BDRV_O_NATIVE_AIO;
3926 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003927 case 'o':
3928 {
Markus Armbruster606caa02017-02-21 21:14:04 +01003929 offset = cvtnum(optarg);
3930 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02003931 error_report("Invalid offset specified");
3932 return 1;
3933 }
3934 break;
3935 }
3936 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003937 case 'q':
3938 quiet = true;
3939 break;
3940 case 's':
3941 {
3942 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003943
Markus Armbruster606caa02017-02-21 21:14:04 +01003944 sval = cvtnum(optarg);
3945 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003946 error_report("Invalid buffer size specified");
3947 return 1;
3948 }
3949
3950 bufsize = sval;
3951 break;
3952 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003953 case 'S':
3954 {
3955 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003956
Markus Armbruster606caa02017-02-21 21:14:04 +01003957 sval = cvtnum(optarg);
3958 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02003959 error_report("Invalid step size specified");
3960 return 1;
3961 }
3962
3963 step = sval;
3964 break;
3965 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003966 case 't':
3967 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3968 if (ret < 0) {
3969 error_report("Invalid cache mode");
3970 ret = -1;
3971 goto out;
3972 }
3973 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003974 case 'w':
3975 flags |= BDRV_O_RDWR;
3976 is_write = true;
3977 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003978 case 'U':
3979 force_share = true;
3980 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003981 case OPTION_PATTERN:
3982 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003983 unsigned long res;
3984
3985 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003986 error_report("Invalid pattern byte specified");
3987 return 1;
3988 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003989 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003990 break;
3991 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003992 case OPTION_FLUSH_INTERVAL:
3993 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003994 unsigned long res;
3995
3996 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02003997 error_report("Invalid flush interval specified");
3998 return 1;
3999 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004000 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004001 break;
4002 }
4003 case OPTION_NO_DRAIN:
4004 drain_on_flush = false;
4005 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004006 case OPTION_IMAGE_OPTS:
4007 image_opts = true;
4008 break;
4009 }
4010 }
4011
4012 if (optind != argc - 1) {
4013 error_exit("Expecting one image file name");
4014 }
4015 filename = argv[argc - 1];
4016
Kevin Wolf55d539c2016-06-03 13:59:41 +02004017 if (!is_write && flush_interval) {
4018 error_report("--flush-interval is only available in write tests");
4019 ret = -1;
4020 goto out;
4021 }
4022 if (flush_interval && flush_interval < depth) {
4023 error_report("Flush interval can't be smaller than depth");
4024 ret = -1;
4025 goto out;
4026 }
4027
Fam Zheng335e9932017-05-03 00:35:39 +08004028 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4029 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004030 if (!blk) {
4031 ret = -1;
4032 goto out;
4033 }
4034
4035 image_size = blk_getlength(blk);
4036 if (image_size < 0) {
4037 ret = image_size;
4038 goto out;
4039 }
4040
4041 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004042 .blk = blk,
4043 .image_size = image_size,
4044 .bufsize = bufsize,
4045 .step = step ?: bufsize,
4046 .nrreq = depth,
4047 .n = count,
4048 .offset = offset,
4049 .write = is_write,
4050 .flush_interval = flush_interval,
4051 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004052 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004053 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004054 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004055 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004056 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004057 if (flush_interval) {
4058 printf("Sending flush every %d requests\n", flush_interval);
4059 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004060
4061 data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004062 memset(data.buf, pattern, data.nrreq * data.bufsize);
4063
Kevin Wolfb6133b82014-08-05 14:17:13 +02004064 data.qiov = g_new(QEMUIOVector, data.nrreq);
4065 for (i = 0; i < data.nrreq; i++) {
4066 qemu_iovec_init(&data.qiov[i], 1);
4067 qemu_iovec_add(&data.qiov[i],
4068 data.buf + i * data.bufsize, data.bufsize);
4069 }
4070
4071 gettimeofday(&t1, NULL);
4072 bench_cb(&data, 0);
4073
4074 while (data.n > 0) {
4075 main_loop_wait(false);
4076 }
4077 gettimeofday(&t2, NULL);
4078
4079 printf("Run completed in %3.3f seconds.\n",
4080 (t2.tv_sec - t1.tv_sec)
4081 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4082
4083out:
4084 qemu_vfree(data.buf);
4085 blk_unref(blk);
4086
4087 if (ret) {
4088 return 1;
4089 }
4090 return 0;
4091}
4092
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004093#define C_BS 01
4094#define C_COUNT 02
4095#define C_IF 04
4096#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004097#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004098
4099struct DdInfo {
4100 unsigned int flags;
4101 int64_t count;
4102};
4103
4104struct DdIo {
4105 int bsz; /* Block size */
4106 char *filename;
4107 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004108 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004109};
4110
4111struct DdOpts {
4112 const char *name;
4113 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4114 unsigned int flag;
4115};
4116
4117static int img_dd_bs(const char *arg,
4118 struct DdIo *in, struct DdIo *out,
4119 struct DdInfo *dd)
4120{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004121 int64_t res;
4122
Markus Armbruster606caa02017-02-21 21:14:04 +01004123 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004124
Markus Armbruster606caa02017-02-21 21:14:04 +01004125 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004126 error_report("invalid number: '%s'", arg);
4127 return 1;
4128 }
4129 in->bsz = out->bsz = res;
4130
4131 return 0;
4132}
4133
4134static int img_dd_count(const char *arg,
4135 struct DdIo *in, struct DdIo *out,
4136 struct DdInfo *dd)
4137{
Markus Armbruster606caa02017-02-21 21:14:04 +01004138 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004139
Markus Armbruster606caa02017-02-21 21:14:04 +01004140 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004141 error_report("invalid number: '%s'", arg);
4142 return 1;
4143 }
4144
4145 return 0;
4146}
4147
4148static int img_dd_if(const char *arg,
4149 struct DdIo *in, struct DdIo *out,
4150 struct DdInfo *dd)
4151{
4152 in->filename = g_strdup(arg);
4153
4154 return 0;
4155}
4156
4157static int img_dd_of(const char *arg,
4158 struct DdIo *in, struct DdIo *out,
4159 struct DdInfo *dd)
4160{
4161 out->filename = g_strdup(arg);
4162
4163 return 0;
4164}
4165
Reda Sallahif7c15532016-08-10 16:16:09 +02004166static int img_dd_skip(const char *arg,
4167 struct DdIo *in, struct DdIo *out,
4168 struct DdInfo *dd)
4169{
Markus Armbruster606caa02017-02-21 21:14:04 +01004170 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004171
Markus Armbruster606caa02017-02-21 21:14:04 +01004172 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004173 error_report("invalid number: '%s'", arg);
4174 return 1;
4175 }
4176
4177 return 0;
4178}
4179
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004180static int img_dd(int argc, char **argv)
4181{
4182 int ret = 0;
4183 char *arg = NULL;
4184 char *tmp;
4185 BlockDriver *drv = NULL, *proto_drv = NULL;
4186 BlockBackend *blk1 = NULL, *blk2 = NULL;
4187 QemuOpts *opts = NULL;
4188 QemuOptsList *create_opts = NULL;
4189 Error *local_err = NULL;
4190 bool image_opts = false;
4191 int c, i;
4192 const char *out_fmt = "raw";
4193 const char *fmt = NULL;
4194 int64_t size = 0;
4195 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004196 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004197 struct DdInfo dd = {
4198 .flags = 0,
4199 .count = 0,
4200 };
4201 struct DdIo in = {
4202 .bsz = 512, /* Block size is by default 512 bytes */
4203 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004204 .buf = NULL,
4205 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004206 };
4207 struct DdIo out = {
4208 .bsz = 512,
4209 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004210 .buf = NULL,
4211 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004212 };
4213
4214 const struct DdOpts options[] = {
4215 { "bs", img_dd_bs, C_BS },
4216 { "count", img_dd_count, C_COUNT },
4217 { "if", img_dd_if, C_IF },
4218 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004219 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004220 { NULL, NULL, 0 }
4221 };
4222 const struct option long_options[] = {
4223 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004224 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004225 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004226 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004227 { 0, 0, 0, 0 }
4228 };
4229
Fam Zheng335e9932017-05-03 00:35:39 +08004230 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004231 if (c == EOF) {
4232 break;
4233 }
4234 switch (c) {
4235 case 'O':
4236 out_fmt = optarg;
4237 break;
4238 case 'f':
4239 fmt = optarg;
4240 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004241 case ':':
4242 missing_argument(argv[optind - 1]);
4243 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004244 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004245 unrecognized_option(argv[optind - 1]);
4246 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004247 case 'h':
4248 help();
4249 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004250 case 'U':
4251 force_share = true;
4252 break;
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004253 case OPTION_OBJECT: {
4254 QemuOpts *opts;
4255 opts = qemu_opts_parse_noisily(&qemu_object_opts,
4256 optarg, true);
4257 if (!opts) {
4258 ret = -1;
4259 goto out;
4260 }
4261 } break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004262 case OPTION_IMAGE_OPTS:
4263 image_opts = true;
4264 break;
4265 }
4266 }
4267
4268 for (i = optind; i < argc; i++) {
4269 int j;
4270 arg = g_strdup(argv[i]);
4271
4272 tmp = strchr(arg, '=');
4273 if (tmp == NULL) {
4274 error_report("unrecognized operand %s", arg);
4275 ret = -1;
4276 goto out;
4277 }
4278
4279 *tmp++ = '\0';
4280
4281 for (j = 0; options[j].name != NULL; j++) {
4282 if (!strcmp(arg, options[j].name)) {
4283 break;
4284 }
4285 }
4286 if (options[j].name == NULL) {
4287 error_report("unrecognized operand %s", arg);
4288 ret = -1;
4289 goto out;
4290 }
4291
4292 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4293 ret = -1;
4294 goto out;
4295 }
4296 dd.flags |= options[j].flag;
4297 g_free(arg);
4298 arg = NULL;
4299 }
4300
4301 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4302 error_report("Must specify both input and output files");
4303 ret = -1;
4304 goto out;
4305 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004306
4307 if (qemu_opts_foreach(&qemu_object_opts,
4308 user_creatable_add_opts_foreach,
4309 NULL, NULL)) {
4310 ret = -1;
4311 goto out;
4312 }
4313
Fam Zheng335e9932017-05-03 00:35:39 +08004314 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4315 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004316
4317 if (!blk1) {
4318 ret = -1;
4319 goto out;
4320 }
4321
4322 drv = bdrv_find_format(out_fmt);
4323 if (!drv) {
4324 error_report("Unknown file format");
4325 ret = -1;
4326 goto out;
4327 }
4328 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4329
4330 if (!proto_drv) {
4331 error_report_err(local_err);
4332 ret = -1;
4333 goto out;
4334 }
4335 if (!drv->create_opts) {
4336 error_report("Format driver '%s' does not support image creation",
4337 drv->format_name);
4338 ret = -1;
4339 goto out;
4340 }
4341 if (!proto_drv->create_opts) {
4342 error_report("Protocol driver '%s' does not support image creation",
4343 proto_drv->format_name);
4344 ret = -1;
4345 goto out;
4346 }
4347 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4348 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4349
4350 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4351
4352 size = blk_getlength(blk1);
4353 if (size < 0) {
4354 error_report("Failed to get size for '%s'", in.filename);
4355 ret = -1;
4356 goto out;
4357 }
4358
4359 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4360 dd.count * in.bsz < size) {
4361 size = dd.count * in.bsz;
4362 }
4363
Reda Sallahif7c15532016-08-10 16:16:09 +02004364 /* Overflow means the specified offset is beyond input image's size */
4365 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4366 size < in.bsz * in.offset)) {
4367 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4368 } else {
4369 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4370 size - in.bsz * in.offset, &error_abort);
4371 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004372
4373 ret = bdrv_create(drv, out.filename, opts, &local_err);
4374 if (ret < 0) {
4375 error_reportf_err(local_err,
4376 "%s: error while creating output image: ",
4377 out.filename);
4378 ret = -1;
4379 goto out;
4380 }
4381
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004382 /* TODO, we can't honour --image-opts for the target,
4383 * since it needs to be given in a format compatible
4384 * with the bdrv_create() call above which does not
4385 * support image-opts style.
4386 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01004387 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004388 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004389
4390 if (!blk2) {
4391 ret = -1;
4392 goto out;
4393 }
4394
Reda Sallahif7c15532016-08-10 16:16:09 +02004395 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4396 size < in.offset * in.bsz)) {
4397 /* We give a warning if the skip option is bigger than the input
4398 * size and create an empty output disk image (i.e. like dd(1)).
4399 */
4400 error_report("%s: cannot skip to specified offset", in.filename);
4401 in_pos = size;
4402 } else {
4403 in_pos = in.offset * in.bsz;
4404 }
4405
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004406 in.buf = g_new(uint8_t, in.bsz);
4407
Reda Sallahif7c15532016-08-10 16:16:09 +02004408 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004409 int in_ret, out_ret;
4410
4411 if (in_pos + in.bsz > size) {
4412 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4413 } else {
4414 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4415 }
4416 if (in_ret < 0) {
4417 error_report("error while reading from input image file: %s",
4418 strerror(-in_ret));
4419 ret = -1;
4420 goto out;
4421 }
4422 in_pos += in_ret;
4423
4424 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4425
4426 if (out_ret < 0) {
4427 error_report("error while writing to output image file: %s",
4428 strerror(-out_ret));
4429 ret = -1;
4430 goto out;
4431 }
4432 out_pos += out_ret;
4433 }
4434
4435out:
4436 g_free(arg);
4437 qemu_opts_del(opts);
4438 qemu_opts_free(create_opts);
4439 blk_unref(blk1);
4440 blk_unref(blk2);
4441 g_free(in.filename);
4442 g_free(out.filename);
4443 g_free(in.buf);
4444 g_free(out.buf);
4445
4446 if (ret) {
4447 return 1;
4448 }
4449 return 0;
4450}
4451
Kevin Wolfb6133b82014-08-05 14:17:13 +02004452
Anthony Liguoric227f092009-10-01 16:12:16 -05004453static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004454#define DEF(option, callback, arg_string) \
4455 { option, callback },
4456#include "qemu-img-cmds.h"
4457#undef DEF
4458#undef GEN_DOCS
4459 { NULL, NULL, },
4460};
4461
bellardea2384d2004-08-01 21:59:26 +00004462int main(int argc, char **argv)
4463{
Anthony Liguoric227f092009-10-01 16:12:16 -05004464 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004465 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004466 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004467 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004468 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004469 static const struct option long_options[] = {
4470 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004471 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004472 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004473 {0, 0, 0, 0}
4474 };
bellardea2384d2004-08-01 21:59:26 +00004475
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004476#ifdef CONFIG_POSIX
4477 signal(SIGPIPE, SIG_IGN);
4478#endif
4479
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004480 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004481 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004482 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004483
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004484 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004485 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004486 exit(EXIT_FAILURE);
4487 }
4488
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004489 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004490
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004491 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004492 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004493 if (argc < 2) {
4494 error_exit("Not enough arguments");
4495 }
Stuart Brady153859b2009-06-07 00:42:17 +01004496
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004497 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004498 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004499 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004500
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004501 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004502 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004503 case ':':
4504 missing_argument(argv[optind - 1]);
4505 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08004506 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004507 unrecognized_option(argv[optind - 1]);
4508 return 0;
4509 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03004510 help();
4511 return 0;
4512 case 'V':
4513 printf(QEMU_IMG_VERSION);
4514 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004515 case 'T':
4516 g_free(trace_file);
4517 trace_file = trace_opt_parse(optarg);
4518 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004519 }
bellardea2384d2004-08-01 21:59:26 +00004520 }
Stuart Brady153859b2009-06-07 00:42:17 +01004521
Denis V. Lunev10985132016-06-17 17:44:13 +03004522 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004523
Denis V. Lunev10985132016-06-17 17:44:13 +03004524 /* reset getopt_long scanning */
4525 argc -= optind;
4526 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004527 return 0;
4528 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004529 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004530 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004531
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004532 if (!trace_init_backends()) {
4533 exit(1);
4534 }
4535 trace_init_file(trace_file);
4536 qemu_set_log(LOG_TRACE);
4537
Denis V. Lunev10985132016-06-17 17:44:13 +03004538 /* find the command */
4539 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4540 if (!strcmp(cmdname, cmd->name)) {
4541 return cmd->handler(argc, argv);
4542 }
4543 }
Jeff Cody7db16892014-04-25 17:02:32 -04004544
Stuart Brady153859b2009-06-07 00:42:17 +01004545 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004546 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004547}