blob: 7f1de74c130cf301573b69a1194f8996ebf8d268 [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
Max Reitzefaa7c42016-03-16 19:54:38 +0100263static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100264 QemuOpts *opts, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800265 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000266{
267 QDict *options;
268 Error *local_err = NULL;
269 BlockBackend *blk;
270 options = qemu_opts_to_qdict(opts, NULL);
Fam Zheng335e9932017-05-03 00:35:39 +0800271 if (force_share) {
272 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
273 && !qdict_get_bool(options, BDRV_OPT_FORCE_SHARE)) {
274 error_report("--force-share/-U conflicts with image options");
Fam Zhengadb998c2017-05-15 22:10:14 +0800275 QDECREF(options);
Fam Zheng335e9932017-05-03 00:35:39 +0800276 return NULL;
277 }
Eric Blake579cf1d2017-05-15 14:54:39 -0500278 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800279 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100280 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000281 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100282 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000283 return NULL;
284 }
Kevin Wolfce099542016-03-15 13:01:04 +0100285 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000286
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000287 return blk;
288}
289
Max Reitzefaa7c42016-03-16 19:54:38 +0100290static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100291 QDict *options,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000292 const char *fmt, int flags,
Fam Zheng335e9932017-05-03 00:35:39 +0800293 bool writethrough, bool quiet,
294 bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000295{
296 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200297 Error *local_err = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100298
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100299 if (!options) {
300 options = qdict_new();
301 }
bellard75c23802004-08-27 21:28:58 +0000302 if (fmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500303 qdict_put_str(options, "driver", fmt);
bellard75c23802004-08-27 21:28:58 +0000304 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100305
Fam Zheng335e9932017-05-03 00:35:39 +0800306 if (force_share) {
Eric Blake579cf1d2017-05-15 14:54:39 -0500307 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800308 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100309 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500310 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100311 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000312 return NULL;
bellard75c23802004-08-27 21:28:58 +0000313 }
Kevin Wolfce099542016-03-15 13:01:04 +0100314 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100315
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200316 return blk;
bellard75c23802004-08-27 21:28:58 +0000317}
318
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000319
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100320static int img_add_key_secrets(void *opaque,
321 const char *name, const char *value,
322 Error **errp)
323{
324 QDict *options = opaque;
325
326 if (g_str_has_suffix(name, "key-secret")) {
327 qdict_put(options, name, qstring_from_str(value));
328 }
329
330 return 0;
331}
332
333static BlockBackend *img_open_new_file(const char *filename,
334 QemuOpts *create_opts,
335 const char *fmt, int flags,
336 bool writethrough, bool quiet,
337 bool force_share)
338{
339 QDict *options = NULL;
340
341 options = qdict_new();
342 qemu_opt_foreach(create_opts, img_add_key_secrets, options, &error_abort);
343
344 return img_open_file(filename, options, fmt, flags, writethrough, quiet,
345 force_share);
346}
347
348
Max Reitzefaa7c42016-03-16 19:54:38 +0100349static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000350 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100351 const char *fmt, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800352 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000353{
354 BlockBackend *blk;
355 if (image_opts) {
356 QemuOpts *opts;
357 if (fmt) {
358 error_report("--image-opts and --format are mutually exclusive");
359 return NULL;
360 }
361 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
362 filename, true);
363 if (!opts) {
364 return NULL;
365 }
Fam Zheng335e9932017-05-03 00:35:39 +0800366 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
367 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000368 } else {
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100369 blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
Fam Zheng335e9932017-05-03 00:35:39 +0800370 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000371 }
372 return blk;
373}
374
375
Chunyan Liu83d05212014-06-05 17:20:51 +0800376static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100377 const char *base_filename,
378 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200379{
Markus Armbruster6750e792015-02-12 17:43:08 +0100380 Error *err = NULL;
381
Kevin Wolfefa84d42009-05-18 16:42:12 +0200382 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100383 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100384 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100385 error_report("Backing file not supported for file format '%s'",
386 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100387 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900388 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200389 }
390 }
391 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100392 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100393 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100394 error_report("Backing file format not supported for file "
395 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100396 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900397 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200398 }
399 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900400 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200401}
402
Markus Armbruster606caa02017-02-21 21:14:04 +0100403static int64_t cvtnum(const char *s)
404{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100405 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100406 uint64_t value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100407
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100408 err = qemu_strtosz(s, NULL, &value);
409 if (err < 0) {
410 return err;
411 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100412 if (value > INT64_MAX) {
413 return -ERANGE;
414 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100415 return value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100416}
417
bellardea2384d2004-08-01 21:59:26 +0000418static int img_create(int argc, char **argv)
419{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200420 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100421 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000422 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000423 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000424 const char *filename;
425 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200426 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200427 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100428 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000429
bellardea2384d2004-08-01 21:59:26 +0000430 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000431 static const struct option long_options[] = {
432 {"help", no_argument, 0, 'h'},
433 {"object", required_argument, 0, OPTION_OBJECT},
434 {0, 0, 0, 0}
435 };
Daniel P. Berrange6b4df542017-07-04 13:30:09 +0100436 c = getopt_long(argc, argv, ":F:b:f:ho:q",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000437 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100438 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000439 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100440 }
bellardea2384d2004-08-01 21:59:26 +0000441 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800442 case ':':
443 missing_argument(argv[optind - 1]);
444 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100445 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800446 unrecognized_option(argv[optind - 1]);
447 break;
bellardea2384d2004-08-01 21:59:26 +0000448 case 'h':
449 help();
450 break;
aliguori9230eaf2009-03-28 17:55:19 +0000451 case 'F':
452 base_fmt = optarg;
453 break;
bellardea2384d2004-08-01 21:59:26 +0000454 case 'b':
455 base_filename = optarg;
456 break;
457 case 'f':
458 fmt = optarg;
459 break;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200460 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100461 if (!is_valid_option_list(optarg)) {
462 error_report("Invalid option list: %s", optarg);
463 goto fail;
464 }
465 if (!options) {
466 options = g_strdup(optarg);
467 } else {
468 char *old_options = options;
469 options = g_strdup_printf("%s,%s", options, optarg);
470 g_free(old_options);
471 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200472 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100473 case 'q':
474 quiet = true;
475 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000476 case OPTION_OBJECT: {
477 QemuOpts *opts;
478 opts = qemu_opts_parse_noisily(&qemu_object_opts,
479 optarg, true);
480 if (!opts) {
481 goto fail;
482 }
483 } break;
bellardea2384d2004-08-01 21:59:26 +0000484 }
485 }
aliguori9230eaf2009-03-28 17:55:19 +0000486
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900487 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100488 filename = (optind < argc) ? argv[optind] : NULL;
489 if (options && has_help_option(options)) {
490 g_free(options);
491 return print_block_option_help(filename, fmt);
492 }
493
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100494 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800495 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100496 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100497 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900498
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000499 if (qemu_opts_foreach(&qemu_object_opts,
500 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200501 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000502 goto fail;
503 }
504
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100505 /* Get image size, if specified */
506 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100507 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100508
509 sval = cvtnum(argv[optind++]);
510 if (sval < 0) {
liguang79443392012-12-17 09:49:23 +0800511 if (sval == -ERANGE) {
512 error_report("Image size must be less than 8 EiB!");
513 } else {
514 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200515 "G, T, P or E suffixes for ");
516 error_report("kilobytes, megabytes, gigabytes, terabytes, "
517 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800518 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100519 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100520 }
521 img_size = (uint64_t)sval;
522 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200523 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800524 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200525 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100526
Luiz Capitulino9b375252012-11-30 10:52:05 -0200527 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Fam Zheng92172832017-04-21 20:27:01 +0800528 options, img_size, 0, quiet, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100529 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100530 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100531 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900532 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200533
Kevin Wolf77386bf2014-02-21 16:24:04 +0100534 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000535 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100536
537fail:
538 g_free(options);
539 return 1;
bellardea2384d2004-08-01 21:59:26 +0000540}
541
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100542static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500543{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500544 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500545 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100546 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600547
548 visit_type_ImageCheck(v, NULL, &check, &error_abort);
549 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500550 str = qobject_to_json_pretty(obj);
551 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100552 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500553 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600554 visit_free(v);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500555 QDECREF(str);
556}
557
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100558static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500559{
560 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100561 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500562 } else {
563 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100564 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
565 "Data may be corrupted, or further writes to the image "
566 "may corrupt it.\n",
567 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500568 }
569
570 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100571 qprintf(quiet,
572 "\n%" PRId64 " leaked clusters were found on the image.\n"
573 "This means waste of disk space, but no harm to data.\n",
574 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500575 }
576
577 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100578 qprintf(quiet,
579 "\n%" PRId64
580 " internal errors have occurred during the check.\n",
581 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500582 }
583 }
584
585 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100586 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
587 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
588 check->allocated_clusters, check->total_clusters,
589 check->allocated_clusters * 100.0 / check->total_clusters,
590 check->fragmented_clusters * 100.0 / check->allocated_clusters,
591 check->compressed_clusters * 100.0 /
592 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500593 }
594
595 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100596 qprintf(quiet,
597 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500598 }
599}
600
601static int collect_image_check(BlockDriverState *bs,
602 ImageCheck *check,
603 const char *filename,
604 const char *fmt,
605 int fix)
606{
607 int ret;
608 BdrvCheckResult result;
609
610 ret = bdrv_check(bs, &result, fix);
611 if (ret < 0) {
612 return ret;
613 }
614
615 check->filename = g_strdup(filename);
616 check->format = g_strdup(bdrv_get_format_name(bs));
617 check->check_errors = result.check_errors;
618 check->corruptions = result.corruptions;
619 check->has_corruptions = result.corruptions != 0;
620 check->leaks = result.leaks;
621 check->has_leaks = result.leaks != 0;
622 check->corruptions_fixed = result.corruptions_fixed;
623 check->has_corruptions_fixed = result.corruptions != 0;
624 check->leaks_fixed = result.leaks_fixed;
625 check->has_leaks_fixed = result.leaks != 0;
626 check->image_end_offset = result.image_end_offset;
627 check->has_image_end_offset = result.image_end_offset != 0;
628 check->total_clusters = result.bfi.total_clusters;
629 check->has_total_clusters = result.bfi.total_clusters != 0;
630 check->allocated_clusters = result.bfi.allocated_clusters;
631 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
632 check->fragmented_clusters = result.bfi.fragmented_clusters;
633 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100634 check->compressed_clusters = result.bfi.compressed_clusters;
635 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500636
637 return 0;
638}
639
Kevin Wolfe076f332010-06-29 11:43:13 +0200640/*
641 * Checks an image for consistency. Exit codes:
642 *
Max Reitzd6635c42014-06-02 22:15:21 +0200643 * 0 - Check completed, image is good
644 * 1 - Check not completed because of internal errors
645 * 2 - Check completed, image is corrupted
646 * 3 - Check completed, image has leaked clusters, but is good otherwise
647 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200648 */
aliguori15859692009-04-21 23:11:53 +0000649static int img_check(int argc, char **argv)
650{
651 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500652 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200653 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200654 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000655 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200656 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100657 int flags = BDRV_O_CHECK;
658 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200659 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100660 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000661 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +0800662 bool force_share = false;
aliguori15859692009-04-21 23:11:53 +0000663
664 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500665 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200666 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100667
aliguori15859692009-04-21 23:11:53 +0000668 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500669 int option_index = 0;
670 static const struct option long_options[] = {
671 {"help", no_argument, 0, 'h'},
672 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530673 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500674 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000675 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000676 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +0800677 {"force-share", no_argument, 0, 'U'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500678 {0, 0, 0, 0}
679 };
Fam Zheng335e9932017-05-03 00:35:39 +0800680 c = getopt_long(argc, argv, ":hf:r:T:qU",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500681 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100682 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000683 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100684 }
aliguori15859692009-04-21 23:11:53 +0000685 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800686 case ':':
687 missing_argument(argv[optind - 1]);
688 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100689 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800690 unrecognized_option(argv[optind - 1]);
691 break;
aliguori15859692009-04-21 23:11:53 +0000692 case 'h':
693 help();
694 break;
695 case 'f':
696 fmt = optarg;
697 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200698 case 'r':
699 flags |= BDRV_O_RDWR;
700
701 if (!strcmp(optarg, "leaks")) {
702 fix = BDRV_FIX_LEAKS;
703 } else if (!strcmp(optarg, "all")) {
704 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
705 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800706 error_exit("Unknown option value for -r "
707 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200708 }
709 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500710 case OPTION_OUTPUT:
711 output = optarg;
712 break;
Max Reitz40055952014-07-22 22:58:42 +0200713 case 'T':
714 cache = optarg;
715 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100716 case 'q':
717 quiet = true;
718 break;
Fam Zheng335e9932017-05-03 00:35:39 +0800719 case 'U':
720 force_share = true;
721 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000722 case OPTION_OBJECT: {
723 QemuOpts *opts;
724 opts = qemu_opts_parse_noisily(&qemu_object_opts,
725 optarg, true);
726 if (!opts) {
727 return 1;
728 }
729 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000730 case OPTION_IMAGE_OPTS:
731 image_opts = true;
732 break;
aliguori15859692009-04-21 23:11:53 +0000733 }
734 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200735 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800736 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100737 }
aliguori15859692009-04-21 23:11:53 +0000738 filename = argv[optind++];
739
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500740 if (output && !strcmp(output, "json")) {
741 output_format = OFORMAT_JSON;
742 } else if (output && !strcmp(output, "human")) {
743 output_format = OFORMAT_HUMAN;
744 } else if (output) {
745 error_report("--output must be used with human or json as argument.");
746 return 1;
747 }
748
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000749 if (qemu_opts_foreach(&qemu_object_opts,
750 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200751 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000752 return 1;
753 }
754
Kevin Wolfce099542016-03-15 13:01:04 +0100755 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200756 if (ret < 0) {
757 error_report("Invalid source cache option: %s", cache);
758 return 1;
759 }
760
Fam Zheng335e9932017-05-03 00:35:39 +0800761 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
762 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200763 if (!blk) {
764 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900765 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200766 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500767
768 check = g_new0(ImageCheck, 1);
769 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200770
771 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200772 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200773 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500774 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200775 }
776
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500777 if (check->corruptions_fixed || check->leaks_fixed) {
778 int corruptions_fixed, leaks_fixed;
779
780 leaks_fixed = check->leaks_fixed;
781 corruptions_fixed = check->corruptions_fixed;
782
783 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100784 qprintf(quiet,
785 "The following inconsistencies were found and repaired:\n\n"
786 " %" PRId64 " leaked clusters\n"
787 " %" PRId64 " corruptions\n\n"
788 "Double checking the fixed image now...\n",
789 check->leaks_fixed,
790 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500791 }
792
793 ret = collect_image_check(bs, check, filename, fmt, 0);
794
795 check->leaks_fixed = leaks_fixed;
796 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200797 }
798
Max Reitz832390a2014-10-23 15:29:12 +0200799 if (!ret) {
800 switch (output_format) {
801 case OFORMAT_HUMAN:
802 dump_human_image_check(check, quiet);
803 break;
804 case OFORMAT_JSON:
805 dump_json_image_check(check, quiet);
806 break;
807 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500808 }
809
810 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200811 if (ret) {
812 error_report("Check failed: %s", strerror(-ret));
813 } else {
814 error_report("Check failed");
815 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500816 ret = 1;
817 goto fail;
818 }
819
820 if (check->corruptions) {
821 ret = 2;
822 } else if (check->leaks) {
823 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200824 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500825 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000826 }
827
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500828fail:
829 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200830 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500831 return ret;
aliguori15859692009-04-21 23:11:53 +0000832}
833
Max Reitzd4a32382014-10-24 15:57:37 +0200834typedef struct CommonBlockJobCBInfo {
835 BlockDriverState *bs;
836 Error **errp;
837} CommonBlockJobCBInfo;
838
839static void common_block_job_cb(void *opaque, int ret)
840{
841 CommonBlockJobCBInfo *cbi = opaque;
842
843 if (ret < 0) {
844 error_setg_errno(cbi->errp, -ret, "Block job failed");
845 }
Max Reitzd4a32382014-10-24 15:57:37 +0200846}
847
848static void run_block_job(BlockJob *job, Error **errp)
849{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200850 AioContext *aio_context = blk_get_aio_context(job->blk);
sochin.jiang4172a002017-06-15 14:47:33 +0800851 int ret = 0;
Max Reitzd4a32382014-10-24 15:57:37 +0200852
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200853 aio_context_acquire(aio_context);
sochin.jiang4172a002017-06-15 14:47:33 +0800854 block_job_ref(job);
Max Reitzd4a32382014-10-24 15:57:37 +0200855 do {
856 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500857 qemu_progress_print(job->len ?
858 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
sochin.jiang4172a002017-06-15 14:47:33 +0800859 } while (!job->ready && !job->completed);
Max Reitzd4a32382014-10-24 15:57:37 +0200860
sochin.jiang4172a002017-06-15 14:47:33 +0800861 if (!job->completed) {
862 ret = block_job_complete_sync(job, errp);
863 } else {
864 ret = job->ret;
865 }
866 block_job_unref(job);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200867 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200868
sochin.jiang4172a002017-06-15 14:47:33 +0800869 /* publish completion progress only when success */
870 if (!ret) {
871 qemu_progress_print(100.f, 0);
872 }
Max Reitzd4a32382014-10-24 15:57:37 +0200873}
874
bellardea2384d2004-08-01 21:59:26 +0000875static int img_commit(int argc, char **argv)
876{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400877 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200878 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200879 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200880 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100881 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200882 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100883 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200884 Error *local_err = NULL;
885 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000886 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200887 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000888
889 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400890 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200891 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000892 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000893 static const struct option long_options[] = {
894 {"help", no_argument, 0, 'h'},
895 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000896 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000897 {0, 0, 0, 0}
898 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800899 c = getopt_long(argc, argv, ":f:ht:b:dpq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000900 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100901 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000902 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100903 }
bellardea2384d2004-08-01 21:59:26 +0000904 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800905 case ':':
906 missing_argument(argv[optind - 1]);
907 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100908 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800909 unrecognized_option(argv[optind - 1]);
910 break;
bellardea2384d2004-08-01 21:59:26 +0000911 case 'h':
912 help();
913 break;
914 case 'f':
915 fmt = optarg;
916 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400917 case 't':
918 cache = optarg;
919 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200920 case 'b':
921 base = optarg;
922 /* -b implies -d */
923 drop = true;
924 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200925 case 'd':
926 drop = true;
927 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200928 case 'p':
929 progress = true;
930 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100931 case 'q':
932 quiet = true;
933 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000934 case OPTION_OBJECT: {
935 QemuOpts *opts;
936 opts = qemu_opts_parse_noisily(&qemu_object_opts,
937 optarg, true);
938 if (!opts) {
939 return 1;
940 }
941 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000942 case OPTION_IMAGE_OPTS:
943 image_opts = true;
944 break;
bellardea2384d2004-08-01 21:59:26 +0000945 }
946 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200947
948 /* Progress is not shown in Quiet mode */
949 if (quiet) {
950 progress = false;
951 }
952
Kevin Wolffc11eb22013-08-05 10:53:04 +0200953 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800954 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100955 }
bellardea2384d2004-08-01 21:59:26 +0000956 filename = argv[optind++];
957
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000958 if (qemu_opts_foreach(&qemu_object_opts,
959 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200960 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000961 return 1;
962 }
963
Max Reitz9a86fe42014-10-24 15:57:38 +0200964 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100965 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400966 if (ret < 0) {
967 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100968 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400969 }
970
Fam Zheng335e9932017-05-03 00:35:39 +0800971 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
972 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200973 if (!blk) {
974 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900975 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200976 bs = blk_bs(blk);
977
Max Reitz687fa1d2014-10-24 15:57:39 +0200978 qemu_progress_init(progress, 1.f);
979 qemu_progress_print(0.f, 100);
980
Max Reitz1b22bff2014-10-24 15:57:40 +0200981 if (base) {
982 base_bs = bdrv_find_backing_image(bs, base);
983 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +0100984 error_setg(&local_err,
985 "Did not find '%s' in the backing chain of '%s'",
986 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +0200987 goto done;
988 }
989 } else {
990 /* This is different from QMP, which by default uses the deepest file in
991 * the backing chain (i.e., the very base); however, the traditional
992 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +0200993 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +0200994 if (!base_bs) {
995 error_setg(&local_err, "Image does not have a backing file");
996 goto done;
997 }
bellardea2384d2004-08-01 21:59:26 +0000998 }
999
Max Reitzd4a32382014-10-24 15:57:37 +02001000 cbi = (CommonBlockJobCBInfo){
1001 .errp = &local_err,
1002 .bs = bs,
1003 };
1004
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001005 aio_context = bdrv_get_aio_context(bs);
1006 aio_context_acquire(aio_context);
John Snow47970df2016-10-27 12:06:57 -04001007 commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
Kevin Wolf0db832f2017-02-20 18:10:05 +01001008 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +08001009 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001010 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +02001011 if (local_err) {
1012 goto done;
1013 }
1014
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001015 /* When the block job completes, the BlockBackend reference will point to
1016 * the old backing file. In order to avoid that the top image is already
1017 * deleted, so we can still empty it afterwards, increment the reference
1018 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +02001019 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001020 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001021 }
1022
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001023 job = block_job_get("commit");
1024 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001025 if (local_err) {
1026 goto unref_backing;
1027 }
1028
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001029 if (!drop && bs->drv->bdrv_make_empty) {
1030 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001031 if (ret) {
1032 error_setg_errno(&local_err, -ret, "Could not empty %s",
1033 filename);
1034 goto unref_backing;
1035 }
1036 }
1037
1038unref_backing:
1039 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001040 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001041 }
Max Reitzd4a32382014-10-24 15:57:37 +02001042
1043done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001044 qemu_progress_end();
1045
Markus Armbruster26f54e92014-10-07 13:59:04 +02001046 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001047
1048 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001049 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001050 return 1;
1051 }
Max Reitzd4a32382014-10-24 15:57:37 +02001052
1053 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001054 return 0;
1055}
1056
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001057/*
thsf58c7b32008-06-05 21:53:49 +00001058 * Returns true iff the first sector pointed to by 'buf' contains at least
1059 * a non-NUL byte.
1060 *
1061 * 'pnum' is set to the number of sectors (including and immediately following
1062 * the first one) that are known to be in the same allocated/unallocated state.
1063 */
bellardea2384d2004-08-01 21:59:26 +00001064static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1065{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001066 bool is_zero;
1067 int i;
bellardea2384d2004-08-01 21:59:26 +00001068
1069 if (n <= 0) {
1070 *pnum = 0;
1071 return 0;
1072 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001073 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001074 for(i = 1; i < n; i++) {
1075 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001076 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001077 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001078 }
bellardea2384d2004-08-01 21:59:26 +00001079 }
1080 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001081 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001082}
1083
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001084/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001085 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1086 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1087 * breaking up write requests for only small sparse areas.
1088 */
1089static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1090 int min)
1091{
1092 int ret;
1093 int num_checked, num_used;
1094
1095 if (n < min) {
1096 min = n;
1097 }
1098
1099 ret = is_allocated_sectors(buf, n, pnum);
1100 if (!ret) {
1101 return ret;
1102 }
1103
1104 num_used = *pnum;
1105 buf += BDRV_SECTOR_SIZE * *pnum;
1106 n -= *pnum;
1107 num_checked = num_used;
1108
1109 while (n > 0) {
1110 ret = is_allocated_sectors(buf, n, pnum);
1111
1112 buf += BDRV_SECTOR_SIZE * *pnum;
1113 n -= *pnum;
1114 num_checked += *pnum;
1115 if (ret) {
1116 num_used = num_checked;
1117 } else if (*pnum >= min) {
1118 break;
1119 }
1120 }
1121
1122 *pnum = num_used;
1123 return 1;
1124}
1125
1126/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001127 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1128 * buffers matches, non-zero otherwise.
1129 *
1130 * pnum is set to the number of sectors (including and immediately following
1131 * the first one) that are known to have the same comparison result
1132 */
1133static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1134 int *pnum)
1135{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001136 bool res;
1137 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001138
1139 if (n <= 0) {
1140 *pnum = 0;
1141 return 0;
1142 }
1143
1144 res = !!memcmp(buf1, buf2, 512);
1145 for(i = 1; i < n; i++) {
1146 buf1 += 512;
1147 buf2 += 512;
1148
1149 if (!!memcmp(buf1, buf2, 512) != res) {
1150 break;
1151 }
1152 }
1153
1154 *pnum = i;
1155 return res;
1156}
1157
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001158#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001159
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001160static int64_t sectors_to_bytes(int64_t sectors)
1161{
1162 return sectors << BDRV_SECTOR_BITS;
1163}
1164
1165static int64_t sectors_to_process(int64_t total, int64_t from)
1166{
1167 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1168}
1169
1170/*
1171 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1172 *
1173 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1174 * data and negative value on error.
1175 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001176 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001177 * @param sect_num: Number of first sector to check
1178 * @param sect_count: Number of sectors to check
1179 * @param filename: Name of disk file we are checking (logging purpose)
1180 * @param buffer: Allocated buffer for storing read data
1181 * @param quiet: Flag for quiet mode
1182 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001183static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001184 int sect_count, const char *filename,
1185 uint8_t *buffer, bool quiet)
1186{
1187 int pnum, ret = 0;
Eric Blake91669202016-05-06 10:26:43 -06001188 ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer,
1189 sect_count << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001190 if (ret < 0) {
1191 error_report("Error while reading offset %" PRId64 " of %s: %s",
1192 sectors_to_bytes(sect_num), filename, strerror(-ret));
1193 return ret;
1194 }
1195 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1196 if (ret || pnum != sect_count) {
1197 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1198 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1199 return 1;
1200 }
1201
1202 return 0;
1203}
1204
1205/*
1206 * Compares two images. Exit codes:
1207 *
1208 * 0 - Images are identical
1209 * 1 - Images differ
1210 * >1 - Error occurred
1211 */
1212static int img_compare(int argc, char **argv)
1213{
Max Reitz40055952014-07-22 22:58:42 +02001214 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001215 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001216 BlockDriverState *bs1, *bs2;
1217 int64_t total_sectors1, total_sectors2;
1218 uint8_t *buf1 = NULL, *buf2 = NULL;
1219 int pnum1, pnum2;
1220 int allocated1, allocated2;
1221 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1222 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001223 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001224 bool writethrough;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001225 int64_t total_sectors;
1226 int64_t sector_num = 0;
1227 int64_t nb_sectors;
1228 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001229 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001230 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001231 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001232
Max Reitz40055952014-07-22 22:58:42 +02001233 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001234 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001235 static const struct option long_options[] = {
1236 {"help", no_argument, 0, 'h'},
1237 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001238 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001239 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001240 {0, 0, 0, 0}
1241 };
Fam Zheng335e9932017-05-03 00:35:39 +08001242 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001243 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001244 if (c == -1) {
1245 break;
1246 }
1247 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001248 case ':':
1249 missing_argument(argv[optind - 1]);
1250 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001251 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001252 unrecognized_option(argv[optind - 1]);
1253 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001254 case 'h':
1255 help();
1256 break;
1257 case 'f':
1258 fmt1 = optarg;
1259 break;
1260 case 'F':
1261 fmt2 = optarg;
1262 break;
Max Reitz40055952014-07-22 22:58:42 +02001263 case 'T':
1264 cache = optarg;
1265 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001266 case 'p':
1267 progress = true;
1268 break;
1269 case 'q':
1270 quiet = true;
1271 break;
1272 case 's':
1273 strict = true;
1274 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001275 case 'U':
1276 force_share = true;
1277 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001278 case OPTION_OBJECT: {
1279 QemuOpts *opts;
1280 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1281 optarg, true);
1282 if (!opts) {
1283 ret = 2;
1284 goto out4;
1285 }
1286 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001287 case OPTION_IMAGE_OPTS:
1288 image_opts = true;
1289 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001290 }
1291 }
1292
1293 /* Progress is not shown in Quiet mode */
1294 if (quiet) {
1295 progress = false;
1296 }
1297
1298
Kevin Wolffc11eb22013-08-05 10:53:04 +02001299 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001300 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001301 }
1302 filename1 = argv[optind++];
1303 filename2 = argv[optind++];
1304
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001305 if (qemu_opts_foreach(&qemu_object_opts,
1306 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001307 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001308 ret = 2;
1309 goto out4;
1310 }
1311
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001312 /* Initialize before goto out */
1313 qemu_progress_init(progress, 2.0);
1314
Kevin Wolfce099542016-03-15 13:01:04 +01001315 flags = 0;
1316 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001317 if (ret < 0) {
1318 error_report("Invalid source cache option: %s", cache);
1319 ret = 2;
1320 goto out3;
1321 }
1322
Fam Zheng335e9932017-05-03 00:35:39 +08001323 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1324 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001325 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001326 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001327 goto out3;
1328 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001329
Fam Zheng335e9932017-05-03 00:35:39 +08001330 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1331 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001332 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001333 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001334 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001335 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001336 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001337 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001338
Max Reitzf1d3cd72015-02-05 13:58:18 -05001339 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1340 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1341 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001342 if (total_sectors1 < 0) {
1343 error_report("Can't get size of %s: %s",
1344 filename1, strerror(-total_sectors1));
1345 ret = 4;
1346 goto out;
1347 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001348 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001349 if (total_sectors2 < 0) {
1350 error_report("Can't get size of %s: %s",
1351 filename2, strerror(-total_sectors2));
1352 ret = 4;
1353 goto out;
1354 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001355 total_sectors = MIN(total_sectors1, total_sectors2);
1356 progress_base = MAX(total_sectors1, total_sectors2);
1357
1358 qemu_progress_print(0, 100);
1359
1360 if (strict && total_sectors1 != total_sectors2) {
1361 ret = 1;
1362 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1363 goto out;
1364 }
1365
1366 for (;;) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001367 int64_t status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001368 BlockDriverState *file;
1369
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001370 nb_sectors = sectors_to_process(total_sectors, sector_num);
1371 if (nb_sectors <= 0) {
1372 break;
1373 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001374 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1375 total_sectors1 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001376 &pnum1, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001377 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001378 ret = 3;
1379 error_report("Sector allocation test failed for %s", filename1);
1380 goto out;
1381 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001382 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001383
Fam Zheng25ad8e62016-01-13 16:37:41 +08001384 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1385 total_sectors2 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001386 &pnum2, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001387 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001388 ret = 3;
1389 error_report("Sector allocation test failed for %s", filename2);
1390 goto out;
1391 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001392 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1393 if (pnum1) {
1394 nb_sectors = MIN(nb_sectors, pnum1);
1395 }
1396 if (pnum2) {
1397 nb_sectors = MIN(nb_sectors, pnum2);
1398 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001399
Fam Zheng25ad8e62016-01-13 16:37:41 +08001400 if (strict) {
1401 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1402 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1403 ret = 1;
1404 qprintf(quiet, "Strict mode: Offset %" PRId64
1405 " block status mismatch!\n",
1406 sectors_to_bytes(sector_num));
1407 goto out;
1408 }
1409 }
1410 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1411 nb_sectors = MIN(pnum1, pnum2);
1412 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001413 if (allocated1) {
Eric Blake91669202016-05-06 10:26:43 -06001414 ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1,
1415 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001416 if (ret < 0) {
1417 error_report("Error while reading offset %" PRId64 " of %s:"
1418 " %s", sectors_to_bytes(sector_num), filename1,
1419 strerror(-ret));
1420 ret = 4;
1421 goto out;
1422 }
Eric Blake91669202016-05-06 10:26:43 -06001423 ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2,
1424 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001425 if (ret < 0) {
1426 error_report("Error while reading offset %" PRId64
1427 " of %s: %s", sectors_to_bytes(sector_num),
1428 filename2, strerror(-ret));
1429 ret = 4;
1430 goto out;
1431 }
1432 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1433 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001434 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1435 sectors_to_bytes(
1436 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001437 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001438 goto out;
1439 }
1440 }
1441 } else {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001442
1443 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001444 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001445 filename1, buf1, quiet);
1446 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001447 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001448 filename2, buf1, quiet);
1449 }
1450 if (ret) {
1451 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001452 error_report("Error while reading offset %" PRId64 ": %s",
1453 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001454 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001455 }
1456 goto out;
1457 }
1458 }
1459 sector_num += nb_sectors;
1460 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1461 }
1462
1463 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001464 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001465 int64_t total_sectors_over;
1466 const char *filename_over;
1467
1468 qprintf(quiet, "Warning: Image size mismatch!\n");
1469 if (total_sectors1 > total_sectors2) {
1470 total_sectors_over = total_sectors1;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001471 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001472 filename_over = filename1;
1473 } else {
1474 total_sectors_over = total_sectors2;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001475 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001476 filename_over = filename2;
1477 }
1478
1479 for (;;) {
Eric Blake51b0a482017-07-07 07:44:59 -05001480 int64_t count;
1481
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001482 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1483 if (nb_sectors <= 0) {
1484 break;
1485 }
Eric Blake51b0a482017-07-07 07:44:59 -05001486 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL,
1487 sector_num * BDRV_SECTOR_SIZE,
1488 nb_sectors * BDRV_SECTOR_SIZE,
1489 &count);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001490 if (ret < 0) {
1491 ret = 3;
1492 error_report("Sector allocation test failed for %s",
1493 filename_over);
1494 goto out;
1495
1496 }
Eric Blake51b0a482017-07-07 07:44:59 -05001497 /* TODO relax this once bdrv_is_allocated_above does not enforce
1498 * sector alignment */
1499 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
1500 nb_sectors = count >> BDRV_SECTOR_BITS;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001501 if (ret) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001502 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001503 filename_over, buf1, quiet);
1504 if (ret) {
1505 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001506 error_report("Error while reading offset %" PRId64
1507 " of %s: %s", sectors_to_bytes(sector_num),
1508 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001509 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001510 }
1511 goto out;
1512 }
1513 }
1514 sector_num += nb_sectors;
1515 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1516 }
1517 }
1518
1519 qprintf(quiet, "Images are identical.\n");
1520 ret = 0;
1521
1522out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001523 qemu_vfree(buf1);
1524 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001525 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001526out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001527 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001528out3:
1529 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001530out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001531 return ret;
1532}
1533
Kevin Wolf690c7302015-03-19 13:33:32 +01001534enum ImgConvertBlockStatus {
1535 BLK_DATA,
1536 BLK_ZERO,
1537 BLK_BACKING_FILE,
1538};
1539
Peter Lieven2d9187b2017-02-28 13:40:07 +01001540#define MAX_COROUTINES 16
1541
Kevin Wolf690c7302015-03-19 13:33:32 +01001542typedef struct ImgConvertState {
1543 BlockBackend **src;
1544 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001545 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001546 int64_t total_sectors;
1547 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001548 int64_t allocated_done;
1549 int64_t sector_num;
1550 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001551 enum ImgConvertBlockStatus status;
1552 int64_t sector_next_status;
1553 BlockBackend *target;
1554 bool has_zero_init;
1555 bool compressed;
1556 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001557 bool wr_in_order;
Kevin Wolf690c7302015-03-19 13:33:32 +01001558 int min_sparse;
1559 size_t cluster_sectors;
1560 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001561 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001562 int running_coroutines;
1563 Coroutine *co[MAX_COROUTINES];
1564 int64_t wait_sector_num[MAX_COROUTINES];
1565 CoMutex lock;
1566 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001567} ImgConvertState;
1568
Peter Lieven2d9187b2017-02-28 13:40:07 +01001569static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1570 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001571{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001572 *src_cur = 0;
1573 *src_cur_offset = 0;
1574 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1575 *src_cur_offset += s->src_sectors[*src_cur];
1576 (*src_cur)++;
1577 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001578 }
1579}
1580
1581static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1582{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001583 int64_t ret, src_cur_offset;
1584 int n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001585
Peter Lieven2d9187b2017-02-28 13:40:07 +01001586 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001587
1588 assert(s->total_sectors > sector_num);
1589 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1590
1591 if (s->sector_next_status <= sector_num) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08001592 BlockDriverState *file;
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001593 if (s->target_has_backing) {
1594 ret = bdrv_get_block_status(blk_bs(s->src[src_cur]),
1595 sector_num - src_cur_offset,
1596 n, &n, &file);
1597 } else {
1598 ret = bdrv_get_block_status_above(blk_bs(s->src[src_cur]), NULL,
1599 sector_num - src_cur_offset,
1600 n, &n, &file);
1601 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001602 if (ret < 0) {
1603 return ret;
1604 }
1605
1606 if (ret & BDRV_BLOCK_ZERO) {
1607 s->status = BLK_ZERO;
1608 } else if (ret & BDRV_BLOCK_DATA) {
1609 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001610 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001611 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001612 }
1613
1614 s->sector_next_status = sector_num + n;
1615 }
1616
1617 n = MIN(n, s->sector_next_status - sector_num);
1618 if (s->status == BLK_DATA) {
1619 n = MIN(n, s->buf_sectors);
1620 }
1621
1622 /* We need to write complete clusters for compressed images, so if an
1623 * unallocated area is shorter than that, we must consider the whole
1624 * cluster allocated. */
1625 if (s->compressed) {
1626 if (n < s->cluster_sectors) {
1627 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1628 s->status = BLK_DATA;
1629 } else {
1630 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1631 }
1632 }
1633
1634 return n;
1635}
1636
Peter Lieven2d9187b2017-02-28 13:40:07 +01001637static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1638 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001639{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001640 int n, ret;
1641 QEMUIOVector qiov;
1642 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001643
Kevin Wolf690c7302015-03-19 13:33:32 +01001644 assert(nb_sectors <= s->buf_sectors);
1645 while (nb_sectors > 0) {
1646 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001647 int src_cur;
1648 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001649
1650 /* In the case of compression with multiple source files, we can get a
1651 * nb_sectors that spreads into the next part. So we must be able to
1652 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001653 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1654 blk = s->src[src_cur];
1655 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001656
Peter Lieven2d9187b2017-02-28 13:40:07 +01001657 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1658 iov.iov_base = buf;
1659 iov.iov_len = n << BDRV_SECTOR_BITS;
1660 qemu_iovec_init_external(&qiov, &iov, 1);
1661
1662 ret = blk_co_preadv(
1663 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1664 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001665 if (ret < 0) {
1666 return ret;
1667 }
1668
1669 sector_num += n;
1670 nb_sectors -= n;
1671 buf += n * BDRV_SECTOR_SIZE;
1672 }
1673
1674 return 0;
1675}
1676
Peter Lieven2d9187b2017-02-28 13:40:07 +01001677
1678static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1679 int nb_sectors, uint8_t *buf,
1680 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001681{
1682 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001683 QEMUIOVector qiov;
1684 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001685
1686 while (nb_sectors > 0) {
1687 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001688 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1689
Peter Lieven2d9187b2017-02-28 13:40:07 +01001690 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001691 case BLK_BACKING_FILE:
1692 /* If we have a backing file, leave clusters unallocated that are
1693 * unallocated in the source image, so that the backing file is
1694 * visible at the respective offset. */
1695 assert(s->target_has_backing);
1696 break;
1697
1698 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001699 /* If we're told to keep the target fully allocated (-S 0) or there
1700 * is real non-zero data, we must write it. Otherwise we can treat
1701 * it as zero sectors.
1702 * Compressed clusters need to be written as a whole, so in that
1703 * case we can only save the write if the buffer is completely
1704 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001705 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001706 (!s->compressed &&
1707 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1708 (s->compressed &&
1709 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001710 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001711 iov.iov_base = buf;
1712 iov.iov_len = n << BDRV_SECTOR_BITS;
1713 qemu_iovec_init_external(&qiov, &iov, 1);
1714
1715 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
Lidong Chendb933fb2017-04-27 10:58:27 +08001716 n << BDRV_SECTOR_BITS, &qiov, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001717 if (ret < 0) {
1718 return ret;
1719 }
1720 break;
1721 }
1722 /* fall-through */
1723
1724 case BLK_ZERO:
1725 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001726 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001727 break;
1728 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001729 ret = blk_co_pwrite_zeroes(s->target,
1730 sector_num << BDRV_SECTOR_BITS,
1731 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001732 if (ret < 0) {
1733 return ret;
1734 }
1735 break;
1736 }
1737
1738 sector_num += n;
1739 nb_sectors -= n;
1740 buf += n * BDRV_SECTOR_SIZE;
1741 }
1742
1743 return 0;
1744}
1745
Peter Lieven2d9187b2017-02-28 13:40:07 +01001746static void coroutine_fn convert_co_do_copy(void *opaque)
1747{
1748 ImgConvertState *s = opaque;
1749 uint8_t *buf = NULL;
1750 int ret, i;
1751 int index = -1;
1752
1753 for (i = 0; i < s->num_coroutines; i++) {
1754 if (s->co[i] == qemu_coroutine_self()) {
1755 index = i;
1756 break;
1757 }
1758 }
1759 assert(index >= 0);
1760
1761 s->running_coroutines++;
1762 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1763
1764 while (1) {
1765 int n;
1766 int64_t sector_num;
1767 enum ImgConvertBlockStatus status;
1768
1769 qemu_co_mutex_lock(&s->lock);
1770 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1771 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001772 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001773 }
1774 n = convert_iteration_sectors(s, s->sector_num);
1775 if (n < 0) {
1776 qemu_co_mutex_unlock(&s->lock);
1777 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001778 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001779 }
1780 /* save current sector and allocation status to local variables */
1781 sector_num = s->sector_num;
1782 status = s->status;
1783 if (!s->min_sparse && s->status == BLK_ZERO) {
1784 n = MIN(n, s->buf_sectors);
1785 }
1786 /* increment global sector counter so that other coroutines can
1787 * already continue reading beyond this request */
1788 s->sector_num += n;
1789 qemu_co_mutex_unlock(&s->lock);
1790
1791 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1792 s->allocated_done += n;
1793 qemu_progress_print(100.0 * s->allocated_done /
1794 s->allocated_sectors, 0);
1795 }
1796
1797 if (status == BLK_DATA) {
1798 ret = convert_co_read(s, sector_num, n, buf);
1799 if (ret < 0) {
1800 error_report("error while reading sector %" PRId64
1801 ": %s", sector_num, strerror(-ret));
1802 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001803 }
1804 } else if (!s->min_sparse && status == BLK_ZERO) {
1805 status = BLK_DATA;
1806 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1807 }
1808
1809 if (s->wr_in_order) {
1810 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03001811 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001812 s->wait_sector_num[index] = sector_num;
1813 qemu_coroutine_yield();
1814 }
1815 s->wait_sector_num[index] = -1;
1816 }
1817
Anton Nefedovb91127e2017-04-26 11:33:15 +03001818 if (s->ret == -EINPROGRESS) {
1819 ret = convert_co_write(s, sector_num, n, buf, status);
1820 if (ret < 0) {
1821 error_report("error while writing sector %" PRId64
1822 ": %s", sector_num, strerror(-ret));
1823 s->ret = ret;
1824 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001825 }
1826
1827 if (s->wr_in_order) {
1828 /* reenter the coroutine that might have waited
1829 * for this write to complete */
1830 s->wr_offs = sector_num + n;
1831 for (i = 0; i < s->num_coroutines; i++) {
1832 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1833 /*
1834 * A -> B -> A cannot occur because A has
1835 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1836 * B will never enter A during this time window.
1837 */
1838 qemu_coroutine_enter(s->co[i]);
1839 break;
1840 }
1841 }
1842 }
1843 }
1844
Peter Lieven2d9187b2017-02-28 13:40:07 +01001845 qemu_vfree(buf);
1846 s->co[index] = NULL;
1847 s->running_coroutines--;
1848 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1849 /* the convert job finished successfully */
1850 s->ret = 0;
1851 }
1852}
1853
Kevin Wolf690c7302015-03-19 13:33:32 +01001854static int convert_do_copy(ImgConvertState *s)
1855{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001856 int ret, i, n;
1857 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001858
1859 /* Check whether we have zero initialisation or can get it efficiently */
1860 s->has_zero_init = s->min_sparse && !s->target_has_backing
1861 ? bdrv_has_zero_init(blk_bs(s->target))
1862 : false;
1863
1864 if (!s->has_zero_init && !s->target_has_backing &&
1865 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1866 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001867 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001868 if (ret == 0) {
1869 s->has_zero_init = true;
1870 }
1871 }
1872
1873 /* Allocate buffer for copied data. For compressed images, only one cluster
1874 * can be copied at a time. */
1875 if (s->compressed) {
1876 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1877 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001878 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001879 }
1880 s->buf_sectors = s->cluster_sectors;
1881 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001882
Kevin Wolf690c7302015-03-19 13:33:32 +01001883 while (sector_num < s->total_sectors) {
1884 n = convert_iteration_sectors(s, sector_num);
1885 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001886 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001887 }
Max Reitzaad15de2016-03-24 23:33:57 +01001888 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1889 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001890 s->allocated_sectors += n;
1891 }
1892 sector_num += n;
1893 }
1894
1895 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001896 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001897 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001898
Peter Lieven2d9187b2017-02-28 13:40:07 +01001899 qemu_co_mutex_init(&s->lock);
1900 for (i = 0; i < s->num_coroutines; i++) {
1901 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1902 s->wait_sector_num[i] = -1;
1903 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001904 }
1905
Anton Nefedovb91127e2017-04-26 11:33:15 +03001906 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001907 main_loop_wait(false);
1908 }
1909
1910 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001911 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001912 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001913 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001914 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001915 }
1916 }
1917
Peter Lieven2d9187b2017-02-28 13:40:07 +01001918 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001919}
1920
bellardea2384d2004-08-01 21:59:26 +00001921static int img_convert(int argc, char **argv)
1922{
Peter Lieven9fd77f92017-04-21 11:11:55 +02001923 int c, bs_i, flags, src_flags = 0;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001924 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02001925 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1926 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001927 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00001928 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001929 BlockDriverState *out_bs;
1930 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08001931 QemuOptsList *create_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001932 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02001933 Error *local_err = NULL;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001934 bool writethrough, src_writethrough, quiet = false, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001935 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001936 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08001937 bool force_share = false;
bellardea2384d2004-08-01 21:59:26 +00001938
Peter Lieven9fd77f92017-04-21 11:11:55 +02001939 ImgConvertState s = (ImgConvertState) {
1940 /* Need at least 4k of zeros for sparse detection */
1941 .min_sparse = 8,
1942 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1943 .wr_in_order = true,
1944 .num_coroutines = 8,
1945 };
1946
bellardea2384d2004-08-01 21:59:26 +00001947 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001948 static const struct option long_options[] = {
1949 {"help", no_argument, 0, 'h'},
1950 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001951 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001952 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001953 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001954 {0, 0, 0, 0}
1955 };
Daniel P. Berrange6b4df542017-07-04 13:30:09 +01001956 c = getopt_long(argc, argv, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001957 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001958 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001959 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001960 }
bellardea2384d2004-08-01 21:59:26 +00001961 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001962 case ':':
1963 missing_argument(argv[optind - 1]);
1964 break;
Jes Sorensenef873942010-12-06 15:25:40 +01001965 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001966 unrecognized_option(argv[optind - 1]);
1967 break;
bellardea2384d2004-08-01 21:59:26 +00001968 case 'h':
1969 help();
1970 break;
1971 case 'f':
1972 fmt = optarg;
1973 break;
1974 case 'O':
1975 out_fmt = optarg;
1976 break;
thsf58c7b32008-06-05 21:53:49 +00001977 case 'B':
1978 out_baseimg = optarg;
1979 break;
bellardea2384d2004-08-01 21:59:26 +00001980 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02001981 s.compressed = true;
bellardea2384d2004-08-01 21:59:26 +00001982 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001983 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001984 if (!is_valid_option_list(optarg)) {
1985 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001986 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001987 }
1988 if (!options) {
1989 options = g_strdup(optarg);
1990 } else {
1991 char *old_options = options;
1992 options = g_strdup_printf("%s,%s", options, optarg);
1993 g_free(old_options);
1994 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001995 break;
edison51ef6722010-09-21 19:58:41 -07001996 case 's':
1997 snapshot_name = optarg;
1998 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001999 case 'l':
2000 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01002001 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2002 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08002003 if (!sn_opts) {
2004 error_report("Failed in parsing snapshot param '%s'",
2005 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002006 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002007 }
2008 } else {
2009 snapshot_name = optarg;
2010 }
2011 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002012 case 'S':
2013 {
2014 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002015
2016 sval = cvtnum(optarg);
2017 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02002018 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002019 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002020 }
2021
Peter Lieven9fd77f92017-04-21 11:11:55 +02002022 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002023 break;
2024 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002025 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002026 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002027 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002028 case 't':
2029 cache = optarg;
2030 break;
Max Reitz40055952014-07-22 22:58:42 +02002031 case 'T':
2032 src_cache = optarg;
2033 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002034 case 'q':
2035 quiet = true;
2036 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002037 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002038 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002039 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002040 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002041 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2042 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002043 error_report("Invalid number of coroutines. Allowed number of"
2044 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002045 goto fail_getopt;
2046 }
2047 break;
2048 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002049 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002050 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002051 case 'U':
2052 force_share = true;
2053 break;
Max Reitz3258b912017-04-26 15:46:47 +02002054 case OPTION_OBJECT: {
2055 QemuOpts *object_opts;
2056 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2057 optarg, true);
2058 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002059 goto fail_getopt;
2060 }
2061 break;
Max Reitz3258b912017-04-26 15:46:47 +02002062 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002063 case OPTION_IMAGE_OPTS:
2064 image_opts = true;
2065 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002066 case OPTION_TARGET_IMAGE_OPTS:
2067 tgt_image_opts = true;
2068 break;
bellardea2384d2004-08-01 21:59:26 +00002069 }
2070 }
ths3b46e622007-09-17 08:09:54 +00002071
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002072 if (!out_fmt && !tgt_image_opts) {
2073 out_fmt = "raw";
2074 }
2075
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002076 if (qemu_opts_foreach(&qemu_object_opts,
2077 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002078 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002079 goto fail_getopt;
2080 }
2081
Peter Lieven9fd77f92017-04-21 11:11:55 +02002082 if (!s.wr_in_order && s.compressed) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002083 error_report("Out of order write and compress are mutually exclusive");
Peter Lieven9fd77f92017-04-21 11:11:55 +02002084 goto fail_getopt;
2085 }
2086
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002087 if (tgt_image_opts && !skip_create) {
2088 error_report("--target-image-opts requires use of -n flag");
2089 goto fail_getopt;
2090 }
2091
Peter Lieven9fd77f92017-04-21 11:11:55 +02002092 s.src_num = argc - optind - 1;
2093 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2094
2095 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002096 if (out_fmt) {
2097 ret = print_block_option_help(out_filename, out_fmt);
2098 goto fail_getopt;
2099 } else {
2100 error_report("Option help requires a format be specified");
2101 goto fail_getopt;
2102 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002103 }
2104
2105 if (s.src_num < 1) {
2106 error_report("Must specify image file name");
2107 goto fail_getopt;
2108 }
2109
2110
Peter Lieven9fd77f92017-04-21 11:11:55 +02002111 /* ret is still -EINVAL until here */
2112 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2113 if (ret < 0) {
2114 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002115 goto fail_getopt;
2116 }
2117
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002118 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002119 if (quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002120 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002121 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002122 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002123 qemu_progress_print(0, 100);
2124
Peter Lieven9fd77f92017-04-21 11:11:55 +02002125 s.src = g_new0(BlockBackend *, s.src_num);
2126 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002127
Peter Lieven9fd77f92017-04-21 11:11:55 +02002128 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2129 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Fam Zheng335e9932017-05-03 00:35:39 +08002130 fmt, src_flags, src_writethrough, quiet,
2131 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002132 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002133 ret = -1;
2134 goto out;
2135 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002136 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2137 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002138 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002139 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002140 ret = -1;
2141 goto out;
2142 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002143 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002144 }
bellardea2384d2004-08-01 21:59:26 +00002145
Wenchao Xiaef806542013-12-04 17:10:57 +08002146 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002147 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002148 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2149 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2150 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002151 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002152 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002153 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002154 ret = -1;
2155 goto out;
2156 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002157
Peter Lieven9fd77f92017-04-21 11:11:55 +02002158 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2159 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002160 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002161 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002162 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002163 ret = -1;
2164 goto out;
edison51ef6722010-09-21 19:58:41 -07002165 }
2166
Max Reitz2e024cd2015-02-11 09:58:46 -05002167 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002168 /* Find driver and parse its options */
2169 drv = bdrv_find_format(out_fmt);
2170 if (!drv) {
2171 error_report("Unknown file format '%s'", out_fmt);
2172 ret = -1;
2173 goto out;
2174 }
2175
2176 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2177 if (!proto_drv) {
2178 error_report_err(local_err);
2179 ret = -1;
2180 goto out;
2181 }
2182
Max Reitz2e024cd2015-02-11 09:58:46 -05002183 if (!drv->create_opts) {
2184 error_report("Format driver '%s' does not support image creation",
2185 drv->format_name);
2186 ret = -1;
2187 goto out;
2188 }
Max Reitzf75613c2014-12-02 18:32:46 +01002189
Max Reitz2e024cd2015-02-11 09:58:46 -05002190 if (!proto_drv->create_opts) {
2191 error_report("Protocol driver '%s' does not support image creation",
2192 proto_drv->format_name);
2193 ret = -1;
2194 goto out;
2195 }
Max Reitzf75613c2014-12-02 18:32:46 +01002196
Max Reitz2e024cd2015-02-11 09:58:46 -05002197 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2198 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002199
Max Reitz2e024cd2015-02-11 09:58:46 -05002200 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002201 if (options) {
2202 qemu_opts_do_parse(opts, options, NULL, &local_err);
2203 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002204 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002205 ret = -1;
2206 goto out;
2207 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002208 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002209
Peter Lieven9fd77f92017-04-21 11:11:55 +02002210 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002211 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002212 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2213 if (ret < 0) {
2214 goto out;
2215 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002216 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002217
Kevin Wolfa18953f2010-10-14 15:46:04 +02002218 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002219 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002220 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002221 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002222 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002223 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002224
Max Reitz48758a82017-04-26 15:46:48 +02002225 if (s.src_num > 1 && out_baseimg) {
2226 error_report("Having a backing file for the target makes no sense when "
2227 "concatenating multiple input images");
2228 ret = -1;
2229 goto out;
2230 }
2231
Kevin Wolfefa84d42009-05-18 16:42:12 +02002232 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002233 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002234 bool encryption =
2235 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002236 const char *encryptfmt =
2237 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
Chunyan Liu83d05212014-06-05 17:20:51 +08002238 const char *preallocation =
2239 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002240
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002241 if (drv && !drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002242 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002243 ret = -1;
2244 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002245 }
2246
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002247 if (encryption || encryptfmt) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002248 error_report("Compression and encryption not supported at "
2249 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002250 ret = -1;
2251 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002252 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002253
Chunyan Liu83d05212014-06-05 17:20:51 +08002254 if (preallocation
2255 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002256 {
2257 error_report("Compression and preallocation not supported at "
2258 "the same time");
2259 ret = -1;
2260 goto out;
2261 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002262 }
2263
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002264 if (!skip_create) {
2265 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002266 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002267 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002268 error_reportf_err(local_err, "%s: error while converting %s: ",
2269 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002270 goto out;
bellardea2384d2004-08-01 21:59:26 +00002271 }
2272 }
ths3b46e622007-09-17 08:09:54 +00002273
Peter Lieven9fd77f92017-04-21 11:11:55 +02002274 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002275 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002276 if (ret < 0) {
2277 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002278 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002279 }
2280
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002281 if (skip_create) {
2282 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2283 flags, writethrough, quiet, false);
2284 } else {
2285 /* TODO ultimately we should allow --target-image-opts
2286 * to be used even when -n is not given.
2287 * That has to wait for bdrv_create to be improved
2288 * to allow filenames in option syntax
2289 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01002290 s.target = img_open_new_file(out_filename, opts, out_fmt,
2291 flags, writethrough, quiet, false);
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002292 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002293 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002294 ret = -1;
2295 goto out;
2296 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002297 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002298
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002299 if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2300 error_report("Compression not supported for this file format");
2301 ret = -1;
2302 goto out;
2303 }
2304
Eric Blake5def6b82016-06-23 16:37:19 -06002305 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002306 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2307 * as maximum. */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002308 s.buf_sectors = MIN(32768,
2309 MAX(s.buf_sectors,
2310 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2311 out_bs->bl.pdiscard_alignment >>
2312 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002313
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002314 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002315 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002316 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002317 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002318 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002319 ret = -1;
2320 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002321 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002322 error_report("output file is smaller than input file");
2323 ret = -1;
2324 goto out;
2325 }
2326 }
2327
Peter Lieven24f833c2013-11-27 11:07:07 +01002328 ret = bdrv_get_info(out_bs, &bdi);
2329 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002330 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002331 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002332 goto out;
2333 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002334 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002335 s.compressed = s.compressed || bdi.needs_compressed_writes;
2336 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Peter Lieven24f833c2013-11-27 11:07:07 +01002337 }
2338
Peter Lieven9fd77f92017-04-21 11:11:55 +02002339 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002340out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002341 if (!ret) {
2342 qemu_progress_print(100, 0);
2343 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002344 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002345 qemu_opts_del(opts);
2346 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002347 qemu_opts_del(sn_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002348 blk_unref(s.target);
2349 if (s.src) {
2350 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2351 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002352 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002353 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002354 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002355 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002356fail_getopt:
2357 g_free(options);
2358
Peter Lieven9fd77f92017-04-21 11:11:55 +02002359 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002360}
2361
bellard57d1a2b2004-08-03 21:15:11 +00002362
bellardfaea38e2006-08-05 21:31:00 +00002363static void dump_snapshots(BlockDriverState *bs)
2364{
2365 QEMUSnapshotInfo *sn_tab, *sn;
2366 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002367
2368 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2369 if (nb_sns <= 0)
2370 return;
2371 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002372 bdrv_snapshot_dump(fprintf, stdout, NULL);
2373 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002374 for(i = 0; i < nb_sns; i++) {
2375 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002376 bdrv_snapshot_dump(fprintf, stdout, sn);
2377 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002378 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002379 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002380}
2381
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002382static void dump_json_image_info_list(ImageInfoList *list)
2383{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002384 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002385 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002386 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002387
2388 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2389 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002390 str = qobject_to_json_pretty(obj);
2391 assert(str != NULL);
2392 printf("%s\n", qstring_get_str(str));
2393 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002394 visit_free(v);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002395 QDECREF(str);
2396}
2397
Benoît Canetc054b3f2012-09-05 13:09:02 +02002398static void dump_json_image_info(ImageInfo *info)
2399{
Benoît Canetc054b3f2012-09-05 13:09:02 +02002400 QString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002401 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002402 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002403
2404 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2405 visit_complete(v, &obj);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002406 str = qobject_to_json_pretty(obj);
2407 assert(str != NULL);
2408 printf("%s\n", qstring_get_str(str));
2409 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002410 visit_free(v);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002411 QDECREF(str);
2412}
2413
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002414static void dump_human_image_info_list(ImageInfoList *list)
2415{
2416 ImageInfoList *elem;
2417 bool delim = false;
2418
2419 for (elem = list; elem; elem = elem->next) {
2420 if (delim) {
2421 printf("\n");
2422 }
2423 delim = true;
2424
Wenchao Xia5b917042013-05-25 11:09:45 +08002425 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002426 }
2427}
2428
2429static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2430{
2431 return strcmp(a, b) == 0;
2432}
2433
2434/**
2435 * Open an image file chain and return an ImageInfoList
2436 *
2437 * @filename: topmost image filename
2438 * @fmt: topmost image format (may be NULL to autodetect)
2439 * @chain: true - enumerate entire backing file chain
2440 * false - only topmost image file
2441 *
2442 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2443 * image file. If there was an error a message will have been printed to
2444 * stderr.
2445 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002446static ImageInfoList *collect_image_info_list(bool image_opts,
2447 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002448 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002449 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002450{
2451 ImageInfoList *head = NULL;
2452 ImageInfoList **last = &head;
2453 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002454 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002455
2456 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2457
2458 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002459 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002460 BlockDriverState *bs;
2461 ImageInfo *info;
2462 ImageInfoList *elem;
2463
2464 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2465 error_report("Backing file '%s' creates an infinite loop.",
2466 filename);
2467 goto err;
2468 }
2469 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2470
Max Reitzefaa7c42016-03-16 19:54:38 +01002471 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002472 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2473 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002474 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002475 goto err;
2476 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002477 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002478
Wenchao Xia43526ec2013-06-06 12:27:58 +08002479 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002480 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002481 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002482 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002483 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002484 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002485
2486 elem = g_new0(ImageInfoList, 1);
2487 elem->value = info;
2488 *last = elem;
2489 last = &elem->next;
2490
Markus Armbruster26f54e92014-10-07 13:59:04 +02002491 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002492
2493 filename = fmt = NULL;
2494 if (chain) {
2495 if (info->has_full_backing_filename) {
2496 filename = info->full_backing_filename;
2497 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002498 error_report("Could not determine absolute backing filename,"
2499 " but backing filename '%s' present",
2500 info->backing_filename);
2501 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002502 }
2503 if (info->has_backing_filename_format) {
2504 fmt = info->backing_filename_format;
2505 }
2506 }
2507 }
2508 g_hash_table_destroy(filenames);
2509 return head;
2510
2511err:
2512 qapi_free_ImageInfoList(head);
2513 g_hash_table_destroy(filenames);
2514 return NULL;
2515}
2516
Benoît Canetc054b3f2012-09-05 13:09:02 +02002517static int img_info(int argc, char **argv)
2518{
2519 int c;
2520 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002521 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002522 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002523 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002524 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002525 bool force_share = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002526
bellardea2384d2004-08-01 21:59:26 +00002527 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002528 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002529 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002530 int option_index = 0;
2531 static const struct option long_options[] = {
2532 {"help", no_argument, 0, 'h'},
2533 {"format", required_argument, 0, 'f'},
2534 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002535 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002536 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002537 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002538 {"force-share", no_argument, 0, 'U'},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002539 {0, 0, 0, 0}
2540 };
Fam Zheng335e9932017-05-03 00:35:39 +08002541 c = getopt_long(argc, argv, ":f:hU",
Benoît Canetc054b3f2012-09-05 13:09:02 +02002542 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002543 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002544 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002545 }
bellardea2384d2004-08-01 21:59:26 +00002546 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002547 case ':':
2548 missing_argument(argv[optind - 1]);
2549 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002550 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002551 unrecognized_option(argv[optind - 1]);
2552 break;
bellardea2384d2004-08-01 21:59:26 +00002553 case 'h':
2554 help();
2555 break;
2556 case 'f':
2557 fmt = optarg;
2558 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002559 case 'U':
2560 force_share = true;
2561 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002562 case OPTION_OUTPUT:
2563 output = optarg;
2564 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002565 case OPTION_BACKING_CHAIN:
2566 chain = true;
2567 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002568 case OPTION_OBJECT: {
2569 QemuOpts *opts;
2570 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2571 optarg, true);
2572 if (!opts) {
2573 return 1;
2574 }
2575 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002576 case OPTION_IMAGE_OPTS:
2577 image_opts = true;
2578 break;
bellardea2384d2004-08-01 21:59:26 +00002579 }
2580 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002581 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002582 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002583 }
bellardea2384d2004-08-01 21:59:26 +00002584 filename = argv[optind++];
2585
Benoît Canetc054b3f2012-09-05 13:09:02 +02002586 if (output && !strcmp(output, "json")) {
2587 output_format = OFORMAT_JSON;
2588 } else if (output && !strcmp(output, "human")) {
2589 output_format = OFORMAT_HUMAN;
2590 } else if (output) {
2591 error_report("--output must be used with human or json as argument.");
2592 return 1;
2593 }
2594
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002595 if (qemu_opts_foreach(&qemu_object_opts,
2596 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002597 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002598 return 1;
2599 }
2600
Fam Zheng335e9932017-05-03 00:35:39 +08002601 list = collect_image_info_list(image_opts, filename, fmt, chain,
2602 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002603 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002604 return 1;
2605 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002606
Benoît Canetc054b3f2012-09-05 13:09:02 +02002607 switch (output_format) {
2608 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002609 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002610 break;
2611 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002612 if (chain) {
2613 dump_json_image_info_list(list);
2614 } else {
2615 dump_json_image_info(list->value);
2616 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002617 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002618 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002619
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002620 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002621 return 0;
2622}
2623
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002624static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2625 MapEntry *next)
2626{
2627 switch (output_format) {
2628 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002629 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002630 error_report("File contains external, encrypted or compressed clusters.");
2631 exit(1);
2632 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002633 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002634 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002635 e->start, e->length,
2636 e->has_offset ? e->offset : 0,
2637 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002638 }
2639 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2640 * Modify the flags here to allow more coalescing.
2641 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002642 if (next && (!next->data || next->zero)) {
2643 next->data = false;
2644 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002645 }
2646 break;
2647 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002648 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2649 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002650 (e->start == 0 ? "[" : ",\n"),
2651 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002652 e->zero ? "true" : "false",
2653 e->data ? "true" : "false");
2654 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002655 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002656 }
2657 putchar('}');
2658
2659 if (!next) {
2660 printf("]\n");
2661 }
2662 break;
2663 }
2664}
2665
2666static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2667 int nb_sectors, MapEntry *e)
2668{
2669 int64_t ret;
2670 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002671 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002672 bool has_offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002673
2674 /* As an optimization, we could cache the current range of unallocated
2675 * clusters in each file of the chain, and avoid querying the same
2676 * range repeatedly.
2677 */
2678
2679 depth = 0;
2680 for (;;) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08002681 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2682 &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002683 if (ret < 0) {
2684 return ret;
2685 }
2686 assert(nb_sectors);
2687 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2688 break;
2689 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002690 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002691 if (bs == NULL) {
2692 ret = 0;
2693 break;
2694 }
2695
2696 depth++;
2697 }
2698
John Snow28756452016-02-05 13:12:33 -05002699 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2700
2701 *e = (MapEntry) {
2702 .start = sector_num * BDRV_SECTOR_SIZE,
2703 .length = nb_sectors * BDRV_SECTOR_SIZE,
2704 .data = !!(ret & BDRV_BLOCK_DATA),
2705 .zero = !!(ret & BDRV_BLOCK_ZERO),
2706 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2707 .has_offset = has_offset,
2708 .depth = depth,
2709 .has_filename = file && has_offset,
2710 .filename = file && has_offset ? file->filename : NULL,
2711 };
2712
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002713 return 0;
2714}
2715
Fam Zheng16b0d552016-01-26 11:59:02 +08002716static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2717{
2718 if (curr->length == 0) {
2719 return false;
2720 }
2721 if (curr->zero != next->zero ||
2722 curr->data != next->data ||
2723 curr->depth != next->depth ||
2724 curr->has_filename != next->has_filename ||
2725 curr->has_offset != next->has_offset) {
2726 return false;
2727 }
2728 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2729 return false;
2730 }
2731 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2732 return false;
2733 }
2734 return true;
2735}
2736
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002737static int img_map(int argc, char **argv)
2738{
2739 int c;
2740 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002741 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002742 BlockDriverState *bs;
2743 const char *filename, *fmt, *output;
2744 int64_t length;
2745 MapEntry curr = { .length = 0 }, next;
2746 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002747 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002748 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002749
2750 fmt = NULL;
2751 output = NULL;
2752 for (;;) {
2753 int option_index = 0;
2754 static const struct option long_options[] = {
2755 {"help", no_argument, 0, 'h'},
2756 {"format", required_argument, 0, 'f'},
2757 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002758 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002759 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002760 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002761 {0, 0, 0, 0}
2762 };
Fam Zheng335e9932017-05-03 00:35:39 +08002763 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002764 long_options, &option_index);
2765 if (c == -1) {
2766 break;
2767 }
2768 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002769 case ':':
2770 missing_argument(argv[optind - 1]);
2771 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002772 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002773 unrecognized_option(argv[optind - 1]);
2774 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002775 case 'h':
2776 help();
2777 break;
2778 case 'f':
2779 fmt = optarg;
2780 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002781 case 'U':
2782 force_share = true;
2783 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002784 case OPTION_OUTPUT:
2785 output = optarg;
2786 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002787 case OPTION_OBJECT: {
2788 QemuOpts *opts;
2789 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2790 optarg, true);
2791 if (!opts) {
2792 return 1;
2793 }
2794 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002795 case OPTION_IMAGE_OPTS:
2796 image_opts = true;
2797 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002798 }
2799 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002800 if (optind != argc - 1) {
2801 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002802 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002803 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002804
2805 if (output && !strcmp(output, "json")) {
2806 output_format = OFORMAT_JSON;
2807 } else if (output && !strcmp(output, "human")) {
2808 output_format = OFORMAT_HUMAN;
2809 } else if (output) {
2810 error_report("--output must be used with human or json as argument.");
2811 return 1;
2812 }
2813
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002814 if (qemu_opts_foreach(&qemu_object_opts,
2815 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002816 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002817 return 1;
2818 }
2819
Fam Zheng335e9932017-05-03 00:35:39 +08002820 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002821 if (!blk) {
2822 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002823 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002824 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002825
2826 if (output_format == OFORMAT_HUMAN) {
2827 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2828 }
2829
Max Reitzf1d3cd72015-02-05 13:58:18 -05002830 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002831 while (curr.start + curr.length < length) {
2832 int64_t nsectors_left;
2833 int64_t sector_num;
2834 int n;
2835
2836 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2837
2838 /* Probe up to 1 GiB at a time. */
2839 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2840 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2841 ret = get_block_status(bs, sector_num, n, &next);
2842
2843 if (ret < 0) {
2844 error_report("Could not read file metadata: %s", strerror(-ret));
2845 goto out;
2846 }
2847
Fam Zheng16b0d552016-01-26 11:59:02 +08002848 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002849 curr.length += next.length;
2850 continue;
2851 }
2852
2853 if (curr.length > 0) {
2854 dump_map_entry(output_format, &curr, &next);
2855 }
2856 curr = next;
2857 }
2858
2859 dump_map_entry(output_format, &curr, NULL);
2860
2861out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002862 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002863 return ret < 0;
2864}
2865
aliguorif7b4a942009-01-07 17:40:15 +00002866#define SNAPSHOT_LIST 1
2867#define SNAPSHOT_CREATE 2
2868#define SNAPSHOT_APPLY 3
2869#define SNAPSHOT_DELETE 4
2870
Stuart Brady153859b2009-06-07 00:42:17 +01002871static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002872{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002873 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002874 BlockDriverState *bs;
2875 QEMUSnapshotInfo sn;
2876 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002877 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002878 int action = 0;
2879 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002880 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002881 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002882 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002883 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00002884
Kevin Wolfce099542016-03-15 13:01:04 +01002885 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002886 /* Parse commandline parameters */
2887 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002888 static const struct option long_options[] = {
2889 {"help", no_argument, 0, 'h'},
2890 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002891 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002892 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002893 {0, 0, 0, 0}
2894 };
Fam Zheng335e9932017-05-03 00:35:39 +08002895 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002896 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002897 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002898 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002899 }
aliguorif7b4a942009-01-07 17:40:15 +00002900 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002901 case ':':
2902 missing_argument(argv[optind - 1]);
2903 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002904 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002905 unrecognized_option(argv[optind - 1]);
2906 break;
aliguorif7b4a942009-01-07 17:40:15 +00002907 case 'h':
2908 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002909 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002910 case 'l':
2911 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002912 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002913 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002914 }
2915 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002916 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002917 break;
2918 case 'a':
2919 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002920 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002921 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002922 }
2923 action = SNAPSHOT_APPLY;
2924 snapshot_name = optarg;
2925 break;
2926 case 'c':
2927 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002928 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002929 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002930 }
2931 action = SNAPSHOT_CREATE;
2932 snapshot_name = optarg;
2933 break;
2934 case 'd':
2935 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002936 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002937 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002938 }
2939 action = SNAPSHOT_DELETE;
2940 snapshot_name = optarg;
2941 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002942 case 'q':
2943 quiet = true;
2944 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002945 case 'U':
2946 force_share = true;
2947 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002948 case OPTION_OBJECT: {
2949 QemuOpts *opts;
2950 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2951 optarg, true);
2952 if (!opts) {
2953 return 1;
2954 }
2955 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002956 case OPTION_IMAGE_OPTS:
2957 image_opts = true;
2958 break;
aliguorif7b4a942009-01-07 17:40:15 +00002959 }
2960 }
2961
Kevin Wolffc11eb22013-08-05 10:53:04 +02002962 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002963 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002964 }
aliguorif7b4a942009-01-07 17:40:15 +00002965 filename = argv[optind++];
2966
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002967 if (qemu_opts_foreach(&qemu_object_opts,
2968 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002969 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002970 return 1;
2971 }
2972
aliguorif7b4a942009-01-07 17:40:15 +00002973 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08002974 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
2975 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002976 if (!blk) {
2977 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002978 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002979 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002980
2981 /* Perform the requested action */
2982 switch(action) {
2983 case SNAPSHOT_LIST:
2984 dump_snapshots(bs);
2985 break;
2986
2987 case SNAPSHOT_CREATE:
2988 memset(&sn, 0, sizeof(sn));
2989 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2990
2991 qemu_gettimeofday(&tv);
2992 sn.date_sec = tv.tv_sec;
2993 sn.date_nsec = tv.tv_usec * 1000;
2994
2995 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002996 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002997 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002998 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002999 }
aliguorif7b4a942009-01-07 17:40:15 +00003000 break;
3001
3002 case SNAPSHOT_APPLY:
3003 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003004 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003005 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003006 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003007 }
aliguorif7b4a942009-01-07 17:40:15 +00003008 break;
3009
3010 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003011 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003012 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003013 error_reportf_err(err, "Could not delete snapshot '%s': ",
3014 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003015 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003016 }
aliguorif7b4a942009-01-07 17:40:15 +00003017 break;
3018 }
3019
3020 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003021 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003022 if (ret) {
3023 return 1;
3024 }
Stuart Brady153859b2009-06-07 00:42:17 +01003025 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003026}
3027
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003028static int img_rebase(int argc, char **argv)
3029{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003030 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003031 uint8_t *buf_old = NULL;
3032 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05003033 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003034 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003035 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3036 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003037 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003038 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003039 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003040 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003041 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003042 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003043 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003044
3045 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003046 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003047 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003048 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003049 out_baseimg = NULL;
3050 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003051 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003052 static const struct option long_options[] = {
3053 {"help", no_argument, 0, 'h'},
3054 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003055 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003056 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003057 {0, 0, 0, 0}
3058 };
Fam Zheng335e9932017-05-03 00:35:39 +08003059 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003060 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003061 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003062 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003063 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003064 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003065 case ':':
3066 missing_argument(argv[optind - 1]);
3067 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003068 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003069 unrecognized_option(argv[optind - 1]);
3070 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003071 case 'h':
3072 help();
3073 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003074 case 'f':
3075 fmt = optarg;
3076 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003077 case 'F':
3078 out_basefmt = optarg;
3079 break;
3080 case 'b':
3081 out_baseimg = optarg;
3082 break;
3083 case 'u':
3084 unsafe = 1;
3085 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003086 case 'p':
3087 progress = 1;
3088 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003089 case 't':
3090 cache = optarg;
3091 break;
Max Reitz40055952014-07-22 22:58:42 +02003092 case 'T':
3093 src_cache = optarg;
3094 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003095 case 'q':
3096 quiet = true;
3097 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003098 case OPTION_OBJECT: {
3099 QemuOpts *opts;
3100 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3101 optarg, true);
3102 if (!opts) {
3103 return 1;
3104 }
3105 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003106 case OPTION_IMAGE_OPTS:
3107 image_opts = true;
3108 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003109 case 'U':
3110 force_share = true;
3111 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003112 }
3113 }
3114
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003115 if (quiet) {
3116 progress = 0;
3117 }
3118
Fam Zhengac1307a2014-04-22 13:36:11 +08003119 if (optind != argc - 1) {
3120 error_exit("Expecting one image file name");
3121 }
3122 if (!unsafe && !out_baseimg) {
3123 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003124 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003125 filename = argv[optind++];
3126
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003127 if (qemu_opts_foreach(&qemu_object_opts,
3128 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003129 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003130 return 1;
3131 }
3132
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003133 qemu_progress_init(progress, 2.0);
3134 qemu_progress_print(0, 100);
3135
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003136 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003137 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003138 if (ret < 0) {
3139 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003140 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003141 }
3142
Kevin Wolfce099542016-03-15 13:01:04 +01003143 src_flags = 0;
3144 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003145 if (ret < 0) {
3146 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003147 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003148 }
3149
Kevin Wolfce099542016-03-15 13:01:04 +01003150 /* The source files are opened read-only, don't care about WCE */
3151 assert((src_flags & BDRV_O_RDWR) == 0);
3152 (void) src_writethrough;
3153
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003154 /*
3155 * Open the images.
3156 *
3157 * Ignore the old backing file for unsafe rebase in case we want to correct
3158 * the reference to a renamed or moved backing file.
3159 */
Fam Zheng335e9932017-05-03 00:35:39 +08003160 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3161 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003162 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003163 ret = -1;
3164 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003165 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003166 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003167
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003168 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003169 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003170 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003171 ret = -1;
3172 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003173 }
3174 }
3175
3176 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003177 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003178 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003179 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003180
Max Reitz644483d2015-02-05 13:58:17 -05003181 if (bs->backing_format[0] != '\0') {
3182 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003183 qdict_put_str(options, "driver", bs->backing_format);
Max Reitz644483d2015-02-05 13:58:17 -05003184 }
3185
Fam Zheng335e9932017-05-03 00:35:39 +08003186 if (force_share) {
3187 if (!options) {
3188 options = qdict_new();
3189 }
Eric Blake579cf1d2017-05-15 14:54:39 -05003190 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +08003191 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003192 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003193 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003194 options, src_flags, &local_err);
3195 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003196 error_reportf_err(local_err,
3197 "Could not open old backing file '%s': ",
3198 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003199 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003200 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003201 }
Max Reitz644483d2015-02-05 13:58:17 -05003202
Alex Bligha6166732012-10-16 13:46:18 +01003203 if (out_baseimg[0]) {
Fam Zheng335e9932017-05-03 00:35:39 +08003204 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003205 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003206 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003207 }
3208 if (force_share) {
3209 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003210 }
3211
Max Reitzefaa7c42016-03-16 19:54:38 +01003212 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003213 options, src_flags, &local_err);
3214 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003215 error_reportf_err(local_err,
3216 "Could not open new backing file '%s': ",
3217 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003218 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003219 goto out;
3220 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003221 }
3222 }
3223
3224 /*
3225 * Check each unallocated cluster in the COW file. If it is unallocated,
3226 * accesses go to the backing file. We must therefore compare this cluster
3227 * in the old and new backing file, and if they differ we need to copy it
3228 * from the old backing file into the COW file.
3229 *
3230 * If qemu-img crashes during this step, no harm is done. The content of
3231 * the image is the same as the original one at any time.
3232 */
3233 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003234 int64_t num_sectors;
3235 int64_t old_backing_num_sectors;
3236 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003237 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003238 int n;
Eric Blaked6a644b2017-07-07 07:44:57 -05003239 int64_t count;
Kevin Wolf1f710492012-10-12 14:29:18 +02003240 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003241
Max Reitzf1d3cd72015-02-05 13:58:18 -05003242 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3243 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003244
Max Reitzf1d3cd72015-02-05 13:58:18 -05003245 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003246 if (num_sectors < 0) {
3247 error_report("Could not get size of '%s': %s",
3248 filename, strerror(-num_sectors));
3249 ret = -1;
3250 goto out;
3251 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003252 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003253 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003254 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003255
3256 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3257 error_report("Could not get size of '%s': %s",
3258 backing_name, strerror(-old_backing_num_sectors));
3259 ret = -1;
3260 goto out;
3261 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003262 if (blk_new_backing) {
3263 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003264 if (new_backing_num_sectors < 0) {
3265 error_report("Could not get size of '%s': %s",
3266 out_baseimg, strerror(-new_backing_num_sectors));
3267 ret = -1;
3268 goto out;
3269 }
Alex Bligha6166732012-10-16 13:46:18 +01003270 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003271
Kevin Wolf1f710492012-10-12 14:29:18 +02003272 if (num_sectors != 0) {
3273 local_progress = (float)100 /
3274 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
3275 }
3276
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003277 for (sector = 0; sector < num_sectors; sector += n) {
3278
3279 /* How many sectors can we handle with the next read? */
3280 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
3281 n = (IO_BUF_SIZE / 512);
3282 } else {
3283 n = num_sectors - sector;
3284 }
3285
3286 /* If the cluster is allocated, we don't need to take action */
Eric Blaked6a644b2017-07-07 07:44:57 -05003287 ret = bdrv_is_allocated(bs, sector << BDRV_SECTOR_BITS,
3288 n << BDRV_SECTOR_BITS, &count);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003289 if (ret < 0) {
3290 error_report("error while reading image metadata: %s",
3291 strerror(-ret));
3292 goto out;
3293 }
Eric Blaked6a644b2017-07-07 07:44:57 -05003294 /* TODO relax this once bdrv_is_allocated does not enforce
3295 * sector alignment */
3296 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
3297 n = count >> BDRV_SECTOR_BITS;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003298 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003299 continue;
3300 }
3301
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003302 /*
3303 * Read old and new backing file and take into consideration that
3304 * backing files may be smaller than the COW image.
3305 */
3306 if (sector >= old_backing_num_sectors) {
3307 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3308 } else {
3309 if (sector + n > old_backing_num_sectors) {
3310 n = old_backing_num_sectors - sector;
3311 }
3312
Eric Blake91669202016-05-06 10:26:43 -06003313 ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS,
3314 buf_old, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003315 if (ret < 0) {
3316 error_report("error while reading from old backing file");
3317 goto out;
3318 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003319 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003320
Max Reitzf1d3cd72015-02-05 13:58:18 -05003321 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003322 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3323 } else {
3324 if (sector + n > new_backing_num_sectors) {
3325 n = new_backing_num_sectors - sector;
3326 }
3327
Eric Blake91669202016-05-06 10:26:43 -06003328 ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS,
3329 buf_new, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003330 if (ret < 0) {
3331 error_report("error while reading from new backing file");
3332 goto out;
3333 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003334 }
3335
3336 /* If they differ, we need to write to the COW file */
3337 uint64_t written = 0;
3338
3339 while (written < n) {
3340 int pnum;
3341
3342 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003343 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003344 {
Eric Blake91669202016-05-06 10:26:43 -06003345 ret = blk_pwrite(blk,
3346 (sector + written) << BDRV_SECTOR_BITS,
3347 buf_old + written * 512,
3348 pnum << BDRV_SECTOR_BITS, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003349 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003350 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003351 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003352 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003353 }
3354 }
3355
3356 written += pnum;
3357 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003358 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003359 }
3360 }
3361
3362 /*
3363 * Change the backing file. All clusters that are different from the old
3364 * backing file are overwritten in the COW file now, so the visible content
3365 * doesn't change when we switch the backing file.
3366 */
Alex Bligha6166732012-10-16 13:46:18 +01003367 if (out_baseimg && *out_baseimg) {
3368 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3369 } else {
3370 ret = bdrv_change_backing_file(bs, NULL, NULL);
3371 }
3372
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003373 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003374 error_report("Could not change the backing file to '%s': No "
3375 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003376 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003377 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003378 out_baseimg, strerror(-ret));
3379 }
3380
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003381 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003382 /*
3383 * TODO At this point it is possible to check if any clusters that are
3384 * allocated in the COW file are the same in the backing file. If so, they
3385 * could be dropped from the COW file. Don't do this before switching the
3386 * backing file, in case of a crash this would lead to corruption.
3387 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003388out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003389 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003390 /* Cleanup */
3391 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003392 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003393 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003394 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003395 qemu_vfree(buf_old);
3396 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003397
Markus Armbruster26f54e92014-10-07 13:59:04 +02003398 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003399 if (ret) {
3400 return 1;
3401 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003402 return 0;
3403}
3404
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003405static int img_resize(int argc, char **argv)
3406{
Markus Armbruster6750e792015-02-12 17:43:08 +01003407 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003408 int c, ret, relative;
3409 const char *filename, *fmt, *size;
3410 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003411 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003412 BlockBackend *blk = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003413 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003414
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003415 static QemuOptsList resize_options = {
3416 .name = "resize_options",
3417 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3418 .desc = {
3419 {
3420 .name = BLOCK_OPT_SIZE,
3421 .type = QEMU_OPT_SIZE,
3422 .help = "Virtual disk size"
3423 }, {
3424 /* end of list */
3425 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003426 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003427 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003428 bool image_opts = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003429
Kevin Wolfe80fec72011-04-29 10:58:12 +02003430 /* Remove size from argv manually so that negative numbers are not treated
3431 * as options by getopt. */
3432 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003433 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003434 return 1;
3435 }
3436
3437 size = argv[--argc];
3438
3439 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003440 fmt = NULL;
3441 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003442 static const struct option long_options[] = {
3443 {"help", no_argument, 0, 'h'},
3444 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003445 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003446 {0, 0, 0, 0}
3447 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003448 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003449 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003450 if (c == -1) {
3451 break;
3452 }
3453 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003454 case ':':
3455 missing_argument(argv[optind - 1]);
3456 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003457 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003458 unrecognized_option(argv[optind - 1]);
3459 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003460 case 'h':
3461 help();
3462 break;
3463 case 'f':
3464 fmt = optarg;
3465 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003466 case 'q':
3467 quiet = true;
3468 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003469 case OPTION_OBJECT: {
3470 QemuOpts *opts;
3471 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3472 optarg, true);
3473 if (!opts) {
3474 return 1;
3475 }
3476 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003477 case OPTION_IMAGE_OPTS:
3478 image_opts = true;
3479 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003480 }
3481 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003482 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003483 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003484 }
3485 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003486
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003487 if (qemu_opts_foreach(&qemu_object_opts,
3488 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003489 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003490 return 1;
3491 }
3492
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003493 /* Choose grow, shrink, or absolute resize mode */
3494 switch (size[0]) {
3495 case '+':
3496 relative = 1;
3497 size++;
3498 break;
3499 case '-':
3500 relative = -1;
3501 size++;
3502 break;
3503 default:
3504 relative = 0;
3505 break;
3506 }
3507
3508 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003509 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003510 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003511 if (err) {
3512 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003513 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003514 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003515 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003516 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003517 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3518 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003519
Max Reitzefaa7c42016-03-16 19:54:38 +01003520 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003521 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3522 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003523 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003524 ret = -1;
3525 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003526 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003527
3528 if (relative) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05003529 total_size = blk_getlength(blk) + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003530 } else {
3531 total_size = n;
3532 }
3533 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003534 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003535 ret = -1;
3536 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003537 }
3538
Max Reitzed3d2ec2017-03-28 22:51:27 +02003539 ret = blk_truncate(blk, total_size, &err);
3540 if (!ret) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003541 qprintf(quiet, "Image resized.\n");
Max Reitzed3d2ec2017-03-28 22:51:27 +02003542 } else {
3543 error_report_err(err);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003544 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003545out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003546 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003547 if (ret) {
3548 return 1;
3549 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003550 return 0;
3551}
3552
Max Reitz76a3a342014-10-27 11:12:51 +01003553static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003554 int64_t offset, int64_t total_work_size,
3555 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003556{
3557 qemu_progress_print(100.f * offset / total_work_size, 0);
3558}
3559
Max Reitz6f176b42013-09-03 10:09:50 +02003560static int img_amend(int argc, char **argv)
3561{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003562 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003563 int c, ret = 0;
3564 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003565 QemuOptsList *create_opts = NULL;
3566 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003567 const char *fmt = NULL, *filename, *cache;
3568 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003569 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003570 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003571 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003572 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003573 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003574
Max Reitzbd39e6e2014-07-22 22:58:43 +02003575 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003576 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003577 static const struct option long_options[] = {
3578 {"help", no_argument, 0, 'h'},
3579 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003580 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003581 {0, 0, 0, 0}
3582 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003583 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003584 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003585 if (c == -1) {
3586 break;
3587 }
3588
3589 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003590 case ':':
3591 missing_argument(argv[optind - 1]);
3592 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003593 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003594 unrecognized_option(argv[optind - 1]);
3595 break;
3596 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003597 help();
3598 break;
3599 case 'o':
3600 if (!is_valid_option_list(optarg)) {
3601 error_report("Invalid option list: %s", optarg);
3602 ret = -1;
3603 goto out_no_progress;
3604 }
3605 if (!options) {
3606 options = g_strdup(optarg);
3607 } else {
3608 char *old_options = options;
3609 options = g_strdup_printf("%s,%s", options, optarg);
3610 g_free(old_options);
3611 }
3612 break;
3613 case 'f':
3614 fmt = optarg;
3615 break;
3616 case 't':
3617 cache = optarg;
3618 break;
3619 case 'p':
3620 progress = true;
3621 break;
3622 case 'q':
3623 quiet = true;
3624 break;
3625 case OPTION_OBJECT:
3626 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3627 optarg, true);
3628 if (!opts) {
3629 ret = -1;
3630 goto out_no_progress;
3631 }
3632 break;
3633 case OPTION_IMAGE_OPTS:
3634 image_opts = true;
3635 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003636 }
3637 }
3638
Max Reitz6f176b42013-09-03 10:09:50 +02003639 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003640 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003641 }
3642
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003643 if (qemu_opts_foreach(&qemu_object_opts,
3644 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003645 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003646 ret = -1;
3647 goto out_no_progress;
3648 }
3649
Max Reitz76a3a342014-10-27 11:12:51 +01003650 if (quiet) {
3651 progress = false;
3652 }
3653 qemu_progress_init(progress, 1.0);
3654
Kevin Wolfa283cb62014-02-21 16:24:07 +01003655 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3656 if (fmt && has_help_option(options)) {
3657 /* If a format is explicitly specified (and possibly no filename is
3658 * given), print option help here */
3659 ret = print_block_option_help(filename, fmt);
3660 goto out;
3661 }
3662
3663 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003664 error_report("Expecting one image file name");
3665 ret = -1;
3666 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003667 }
Max Reitz6f176b42013-09-03 10:09:50 +02003668
Kevin Wolfce099542016-03-15 13:01:04 +01003669 flags = BDRV_O_RDWR;
3670 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003671 if (ret < 0) {
3672 error_report("Invalid cache option: %s", cache);
3673 goto out;
3674 }
3675
Fam Zheng335e9932017-05-03 00:35:39 +08003676 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3677 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003678 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003679 ret = -1;
3680 goto out;
3681 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003682 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003683
3684 fmt = bs->drv->format_name;
3685
Kevin Wolf626f84f2014-02-21 16:24:06 +01003686 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003687 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003688 ret = print_block_option_help(filename, fmt);
3689 goto out;
3690 }
3691
Max Reitzb2439d22014-12-02 18:32:47 +01003692 if (!bs->drv->create_opts) {
3693 error_report("Format driver '%s' does not support any options to amend",
3694 fmt);
3695 ret = -1;
3696 goto out;
3697 }
3698
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003699 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003700 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003701 qemu_opts_do_parse(opts, options, NULL, &err);
3702 if (err) {
3703 error_report_err(err);
3704 ret = -1;
3705 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003706 }
3707
Max Reitz76a3a342014-10-27 11:12:51 +01003708 /* In case the driver does not call amend_status_cb() */
3709 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003710 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003711 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003712 if (ret < 0) {
3713 error_report("Error while amending options: %s", strerror(-ret));
3714 goto out;
3715 }
3716
3717out:
Max Reitz76a3a342014-10-27 11:12:51 +01003718 qemu_progress_end();
3719
Max Reitze814dff2015-08-20 16:00:38 -07003720out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003721 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003722 qemu_opts_del(opts);
3723 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003724 g_free(options);
3725
Max Reitz6f176b42013-09-03 10:09:50 +02003726 if (ret) {
3727 return 1;
3728 }
3729 return 0;
3730}
3731
Kevin Wolfb6133b82014-08-05 14:17:13 +02003732typedef struct BenchData {
3733 BlockBackend *blk;
3734 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003735 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003736 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003737 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003738 int nrreq;
3739 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003740 int flush_interval;
3741 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003742 uint8_t *buf;
3743 QEMUIOVector *qiov;
3744
3745 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003746 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003747 uint64_t offset;
3748} BenchData;
3749
Kevin Wolf55d539c2016-06-03 13:59:41 +02003750static void bench_undrained_flush_cb(void *opaque, int ret)
3751{
3752 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003753 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003754 exit(EXIT_FAILURE);
3755 }
3756}
3757
Kevin Wolfb6133b82014-08-05 14:17:13 +02003758static void bench_cb(void *opaque, int ret)
3759{
3760 BenchData *b = opaque;
3761 BlockAIOCB *acb;
3762
3763 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003764 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003765 exit(EXIT_FAILURE);
3766 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003767
3768 if (b->in_flush) {
3769 /* Just finished a flush with drained queue: Start next requests */
3770 assert(b->in_flight == 0);
3771 b->in_flush = false;
3772 } else if (b->in_flight > 0) {
3773 int remaining = b->n - b->in_flight;
3774
Kevin Wolfb6133b82014-08-05 14:17:13 +02003775 b->n--;
3776 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003777
3778 /* Time for flush? Drain queue if requested, then flush */
3779 if (b->flush_interval && remaining % b->flush_interval == 0) {
3780 if (!b->in_flight || !b->drain_on_flush) {
3781 BlockCompletionFunc *cb;
3782
3783 if (b->drain_on_flush) {
3784 b->in_flush = true;
3785 cb = bench_cb;
3786 } else {
3787 cb = bench_undrained_flush_cb;
3788 }
3789
3790 acb = blk_aio_flush(b->blk, cb, b);
3791 if (!acb) {
3792 error_report("Failed to issue flush request");
3793 exit(EXIT_FAILURE);
3794 }
3795 }
3796 if (b->drain_on_flush) {
3797 return;
3798 }
3799 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003800 }
3801
3802 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003803 int64_t offset = b->offset;
3804 /* blk_aio_* might look for completed I/Os and kick bench_cb
3805 * again, so make sure this operation is counted by in_flight
3806 * and b->offset is ready for the next submission.
3807 */
3808 b->in_flight++;
3809 b->offset += b->step;
3810 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003811 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003812 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003813 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003814 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003815 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003816 if (!acb) {
3817 error_report("Failed to issue request");
3818 exit(EXIT_FAILURE);
3819 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003820 }
3821}
3822
3823static int img_bench(int argc, char **argv)
3824{
3825 int c, ret = 0;
3826 const char *fmt = NULL, *filename;
3827 bool quiet = false;
3828 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003829 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003830 int count = 75000;
3831 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003832 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003833 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003834 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003835 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003836 int flush_interval = 0;
3837 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003838 int64_t image_size;
3839 BlockBackend *blk = NULL;
3840 BenchData data = {};
3841 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003842 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003843 struct timeval t1, t2;
3844 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08003845 bool force_share = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003846
3847 for (;;) {
3848 static const struct option long_options[] = {
3849 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003850 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003851 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003852 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003853 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08003854 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003855 {0, 0, 0, 0}
3856 };
Fam Zheng335e9932017-05-03 00:35:39 +08003857 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003858 if (c == -1) {
3859 break;
3860 }
3861
3862 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003863 case ':':
3864 missing_argument(argv[optind - 1]);
3865 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003866 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003867 unrecognized_option(argv[optind - 1]);
3868 break;
3869 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02003870 help();
3871 break;
3872 case 'c':
3873 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003874 unsigned long res;
3875
3876 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003877 error_report("Invalid request count specified");
3878 return 1;
3879 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003880 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003881 break;
3882 }
3883 case 'd':
3884 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003885 unsigned long res;
3886
3887 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003888 error_report("Invalid queue depth specified");
3889 return 1;
3890 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003891 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003892 break;
3893 }
3894 case 'f':
3895 fmt = optarg;
3896 break;
3897 case 'n':
3898 flags |= BDRV_O_NATIVE_AIO;
3899 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003900 case 'o':
3901 {
Markus Armbruster606caa02017-02-21 21:14:04 +01003902 offset = cvtnum(optarg);
3903 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02003904 error_report("Invalid offset specified");
3905 return 1;
3906 }
3907 break;
3908 }
3909 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003910 case 'q':
3911 quiet = true;
3912 break;
3913 case 's':
3914 {
3915 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003916
Markus Armbruster606caa02017-02-21 21:14:04 +01003917 sval = cvtnum(optarg);
3918 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003919 error_report("Invalid buffer size specified");
3920 return 1;
3921 }
3922
3923 bufsize = sval;
3924 break;
3925 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003926 case 'S':
3927 {
3928 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003929
Markus Armbruster606caa02017-02-21 21:14:04 +01003930 sval = cvtnum(optarg);
3931 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02003932 error_report("Invalid step size specified");
3933 return 1;
3934 }
3935
3936 step = sval;
3937 break;
3938 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003939 case 't':
3940 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3941 if (ret < 0) {
3942 error_report("Invalid cache mode");
3943 ret = -1;
3944 goto out;
3945 }
3946 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003947 case 'w':
3948 flags |= BDRV_O_RDWR;
3949 is_write = true;
3950 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003951 case 'U':
3952 force_share = true;
3953 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003954 case OPTION_PATTERN:
3955 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003956 unsigned long res;
3957
3958 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003959 error_report("Invalid pattern byte specified");
3960 return 1;
3961 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003962 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003963 break;
3964 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003965 case OPTION_FLUSH_INTERVAL:
3966 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003967 unsigned long res;
3968
3969 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02003970 error_report("Invalid flush interval specified");
3971 return 1;
3972 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003973 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003974 break;
3975 }
3976 case OPTION_NO_DRAIN:
3977 drain_on_flush = false;
3978 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003979 case OPTION_IMAGE_OPTS:
3980 image_opts = true;
3981 break;
3982 }
3983 }
3984
3985 if (optind != argc - 1) {
3986 error_exit("Expecting one image file name");
3987 }
3988 filename = argv[argc - 1];
3989
Kevin Wolf55d539c2016-06-03 13:59:41 +02003990 if (!is_write && flush_interval) {
3991 error_report("--flush-interval is only available in write tests");
3992 ret = -1;
3993 goto out;
3994 }
3995 if (flush_interval && flush_interval < depth) {
3996 error_report("Flush interval can't be smaller than depth");
3997 ret = -1;
3998 goto out;
3999 }
4000
Fam Zheng335e9932017-05-03 00:35:39 +08004001 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4002 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004003 if (!blk) {
4004 ret = -1;
4005 goto out;
4006 }
4007
4008 image_size = blk_getlength(blk);
4009 if (image_size < 0) {
4010 ret = image_size;
4011 goto out;
4012 }
4013
4014 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004015 .blk = blk,
4016 .image_size = image_size,
4017 .bufsize = bufsize,
4018 .step = step ?: bufsize,
4019 .nrreq = depth,
4020 .n = count,
4021 .offset = offset,
4022 .write = is_write,
4023 .flush_interval = flush_interval,
4024 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004025 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004026 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004027 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004028 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004029 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004030 if (flush_interval) {
4031 printf("Sending flush every %d requests\n", flush_interval);
4032 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004033
4034 data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004035 memset(data.buf, pattern, data.nrreq * data.bufsize);
4036
Kevin Wolfb6133b82014-08-05 14:17:13 +02004037 data.qiov = g_new(QEMUIOVector, data.nrreq);
4038 for (i = 0; i < data.nrreq; i++) {
4039 qemu_iovec_init(&data.qiov[i], 1);
4040 qemu_iovec_add(&data.qiov[i],
4041 data.buf + i * data.bufsize, data.bufsize);
4042 }
4043
4044 gettimeofday(&t1, NULL);
4045 bench_cb(&data, 0);
4046
4047 while (data.n > 0) {
4048 main_loop_wait(false);
4049 }
4050 gettimeofday(&t2, NULL);
4051
4052 printf("Run completed in %3.3f seconds.\n",
4053 (t2.tv_sec - t1.tv_sec)
4054 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4055
4056out:
4057 qemu_vfree(data.buf);
4058 blk_unref(blk);
4059
4060 if (ret) {
4061 return 1;
4062 }
4063 return 0;
4064}
4065
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004066#define C_BS 01
4067#define C_COUNT 02
4068#define C_IF 04
4069#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004070#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004071
4072struct DdInfo {
4073 unsigned int flags;
4074 int64_t count;
4075};
4076
4077struct DdIo {
4078 int bsz; /* Block size */
4079 char *filename;
4080 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004081 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004082};
4083
4084struct DdOpts {
4085 const char *name;
4086 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4087 unsigned int flag;
4088};
4089
4090static int img_dd_bs(const char *arg,
4091 struct DdIo *in, struct DdIo *out,
4092 struct DdInfo *dd)
4093{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004094 int64_t res;
4095
Markus Armbruster606caa02017-02-21 21:14:04 +01004096 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004097
Markus Armbruster606caa02017-02-21 21:14:04 +01004098 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004099 error_report("invalid number: '%s'", arg);
4100 return 1;
4101 }
4102 in->bsz = out->bsz = res;
4103
4104 return 0;
4105}
4106
4107static int img_dd_count(const char *arg,
4108 struct DdIo *in, struct DdIo *out,
4109 struct DdInfo *dd)
4110{
Markus Armbruster606caa02017-02-21 21:14:04 +01004111 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004112
Markus Armbruster606caa02017-02-21 21:14:04 +01004113 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004114 error_report("invalid number: '%s'", arg);
4115 return 1;
4116 }
4117
4118 return 0;
4119}
4120
4121static int img_dd_if(const char *arg,
4122 struct DdIo *in, struct DdIo *out,
4123 struct DdInfo *dd)
4124{
4125 in->filename = g_strdup(arg);
4126
4127 return 0;
4128}
4129
4130static int img_dd_of(const char *arg,
4131 struct DdIo *in, struct DdIo *out,
4132 struct DdInfo *dd)
4133{
4134 out->filename = g_strdup(arg);
4135
4136 return 0;
4137}
4138
Reda Sallahif7c15532016-08-10 16:16:09 +02004139static int img_dd_skip(const char *arg,
4140 struct DdIo *in, struct DdIo *out,
4141 struct DdInfo *dd)
4142{
Markus Armbruster606caa02017-02-21 21:14:04 +01004143 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004144
Markus Armbruster606caa02017-02-21 21:14:04 +01004145 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004146 error_report("invalid number: '%s'", arg);
4147 return 1;
4148 }
4149
4150 return 0;
4151}
4152
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004153static int img_dd(int argc, char **argv)
4154{
4155 int ret = 0;
4156 char *arg = NULL;
4157 char *tmp;
4158 BlockDriver *drv = NULL, *proto_drv = NULL;
4159 BlockBackend *blk1 = NULL, *blk2 = NULL;
4160 QemuOpts *opts = NULL;
4161 QemuOptsList *create_opts = NULL;
4162 Error *local_err = NULL;
4163 bool image_opts = false;
4164 int c, i;
4165 const char *out_fmt = "raw";
4166 const char *fmt = NULL;
4167 int64_t size = 0;
4168 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004169 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004170 struct DdInfo dd = {
4171 .flags = 0,
4172 .count = 0,
4173 };
4174 struct DdIo in = {
4175 .bsz = 512, /* Block size is by default 512 bytes */
4176 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004177 .buf = NULL,
4178 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004179 };
4180 struct DdIo out = {
4181 .bsz = 512,
4182 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004183 .buf = NULL,
4184 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004185 };
4186
4187 const struct DdOpts options[] = {
4188 { "bs", img_dd_bs, C_BS },
4189 { "count", img_dd_count, C_COUNT },
4190 { "if", img_dd_if, C_IF },
4191 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004192 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004193 { NULL, NULL, 0 }
4194 };
4195 const struct option long_options[] = {
4196 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004197 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004198 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004199 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004200 { 0, 0, 0, 0 }
4201 };
4202
Fam Zheng335e9932017-05-03 00:35:39 +08004203 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004204 if (c == EOF) {
4205 break;
4206 }
4207 switch (c) {
4208 case 'O':
4209 out_fmt = optarg;
4210 break;
4211 case 'f':
4212 fmt = optarg;
4213 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004214 case ':':
4215 missing_argument(argv[optind - 1]);
4216 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004217 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004218 unrecognized_option(argv[optind - 1]);
4219 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004220 case 'h':
4221 help();
4222 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004223 case 'U':
4224 force_share = true;
4225 break;
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004226 case OPTION_OBJECT:
4227 if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004228 ret = -1;
4229 goto out;
4230 }
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004231 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004232 case OPTION_IMAGE_OPTS:
4233 image_opts = true;
4234 break;
4235 }
4236 }
4237
4238 for (i = optind; i < argc; i++) {
4239 int j;
4240 arg = g_strdup(argv[i]);
4241
4242 tmp = strchr(arg, '=');
4243 if (tmp == NULL) {
4244 error_report("unrecognized operand %s", arg);
4245 ret = -1;
4246 goto out;
4247 }
4248
4249 *tmp++ = '\0';
4250
4251 for (j = 0; options[j].name != NULL; j++) {
4252 if (!strcmp(arg, options[j].name)) {
4253 break;
4254 }
4255 }
4256 if (options[j].name == NULL) {
4257 error_report("unrecognized operand %s", arg);
4258 ret = -1;
4259 goto out;
4260 }
4261
4262 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4263 ret = -1;
4264 goto out;
4265 }
4266 dd.flags |= options[j].flag;
4267 g_free(arg);
4268 arg = NULL;
4269 }
4270
4271 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4272 error_report("Must specify both input and output files");
4273 ret = -1;
4274 goto out;
4275 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004276
4277 if (qemu_opts_foreach(&qemu_object_opts,
4278 user_creatable_add_opts_foreach,
4279 NULL, NULL)) {
4280 ret = -1;
4281 goto out;
4282 }
4283
Fam Zheng335e9932017-05-03 00:35:39 +08004284 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4285 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004286
4287 if (!blk1) {
4288 ret = -1;
4289 goto out;
4290 }
4291
4292 drv = bdrv_find_format(out_fmt);
4293 if (!drv) {
4294 error_report("Unknown file format");
4295 ret = -1;
4296 goto out;
4297 }
4298 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4299
4300 if (!proto_drv) {
4301 error_report_err(local_err);
4302 ret = -1;
4303 goto out;
4304 }
4305 if (!drv->create_opts) {
4306 error_report("Format driver '%s' does not support image creation",
4307 drv->format_name);
4308 ret = -1;
4309 goto out;
4310 }
4311 if (!proto_drv->create_opts) {
4312 error_report("Protocol driver '%s' does not support image creation",
4313 proto_drv->format_name);
4314 ret = -1;
4315 goto out;
4316 }
4317 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4318 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4319
4320 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4321
4322 size = blk_getlength(blk1);
4323 if (size < 0) {
4324 error_report("Failed to get size for '%s'", in.filename);
4325 ret = -1;
4326 goto out;
4327 }
4328
4329 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4330 dd.count * in.bsz < size) {
4331 size = dd.count * in.bsz;
4332 }
4333
Reda Sallahif7c15532016-08-10 16:16:09 +02004334 /* Overflow means the specified offset is beyond input image's size */
4335 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4336 size < in.bsz * in.offset)) {
4337 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4338 } else {
4339 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4340 size - in.bsz * in.offset, &error_abort);
4341 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004342
4343 ret = bdrv_create(drv, out.filename, opts, &local_err);
4344 if (ret < 0) {
4345 error_reportf_err(local_err,
4346 "%s: error while creating output image: ",
4347 out.filename);
4348 ret = -1;
4349 goto out;
4350 }
4351
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004352 /* TODO, we can't honour --image-opts for the target,
4353 * since it needs to be given in a format compatible
4354 * with the bdrv_create() call above which does not
4355 * support image-opts style.
4356 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01004357 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004358 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004359
4360 if (!blk2) {
4361 ret = -1;
4362 goto out;
4363 }
4364
Reda Sallahif7c15532016-08-10 16:16:09 +02004365 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4366 size < in.offset * in.bsz)) {
4367 /* We give a warning if the skip option is bigger than the input
4368 * size and create an empty output disk image (i.e. like dd(1)).
4369 */
4370 error_report("%s: cannot skip to specified offset", in.filename);
4371 in_pos = size;
4372 } else {
4373 in_pos = in.offset * in.bsz;
4374 }
4375
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004376 in.buf = g_new(uint8_t, in.bsz);
4377
Reda Sallahif7c15532016-08-10 16:16:09 +02004378 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004379 int in_ret, out_ret;
4380
4381 if (in_pos + in.bsz > size) {
4382 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4383 } else {
4384 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4385 }
4386 if (in_ret < 0) {
4387 error_report("error while reading from input image file: %s",
4388 strerror(-in_ret));
4389 ret = -1;
4390 goto out;
4391 }
4392 in_pos += in_ret;
4393
4394 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4395
4396 if (out_ret < 0) {
4397 error_report("error while writing to output image file: %s",
4398 strerror(-out_ret));
4399 ret = -1;
4400 goto out;
4401 }
4402 out_pos += out_ret;
4403 }
4404
4405out:
4406 g_free(arg);
4407 qemu_opts_del(opts);
4408 qemu_opts_free(create_opts);
4409 blk_unref(blk1);
4410 blk_unref(blk2);
4411 g_free(in.filename);
4412 g_free(out.filename);
4413 g_free(in.buf);
4414 g_free(out.buf);
4415
4416 if (ret) {
4417 return 1;
4418 }
4419 return 0;
4420}
4421
Kevin Wolfb6133b82014-08-05 14:17:13 +02004422
Anthony Liguoric227f092009-10-01 16:12:16 -05004423static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004424#define DEF(option, callback, arg_string) \
4425 { option, callback },
4426#include "qemu-img-cmds.h"
4427#undef DEF
4428#undef GEN_DOCS
4429 { NULL, NULL, },
4430};
4431
bellardea2384d2004-08-01 21:59:26 +00004432int main(int argc, char **argv)
4433{
Anthony Liguoric227f092009-10-01 16:12:16 -05004434 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004435 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004436 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004437 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004438 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004439 static const struct option long_options[] = {
4440 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004441 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004442 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004443 {0, 0, 0, 0}
4444 };
bellardea2384d2004-08-01 21:59:26 +00004445
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004446#ifdef CONFIG_POSIX
4447 signal(SIGPIPE, SIG_IGN);
4448#endif
4449
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004450 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004451 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004452 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004453
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004454 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004455 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004456 exit(EXIT_FAILURE);
4457 }
4458
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004459 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004460
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004461 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004462 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004463 if (argc < 2) {
4464 error_exit("Not enough arguments");
4465 }
Stuart Brady153859b2009-06-07 00:42:17 +01004466
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004467 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004468 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004469 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004470
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004471 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004472 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004473 case ':':
4474 missing_argument(argv[optind - 1]);
4475 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08004476 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004477 unrecognized_option(argv[optind - 1]);
4478 return 0;
4479 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03004480 help();
4481 return 0;
4482 case 'V':
4483 printf(QEMU_IMG_VERSION);
4484 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004485 case 'T':
4486 g_free(trace_file);
4487 trace_file = trace_opt_parse(optarg);
4488 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004489 }
bellardea2384d2004-08-01 21:59:26 +00004490 }
Stuart Brady153859b2009-06-07 00:42:17 +01004491
Denis V. Lunev10985132016-06-17 17:44:13 +03004492 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004493
Denis V. Lunev10985132016-06-17 17:44:13 +03004494 /* reset getopt_long scanning */
4495 argc -= optind;
4496 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004497 return 0;
4498 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004499 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004500 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004501
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004502 if (!trace_init_backends()) {
4503 exit(1);
4504 }
4505 trace_init_file(trace_file);
4506 qemu_set_log(LOG_TRACE);
4507
Denis V. Lunev10985132016-06-17 17:44:13 +03004508 /* find the command */
4509 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4510 if (!strcmp(cmdname, cmd->name)) {
4511 return cmd->handler(argc, argv);
4512 }
4513 }
Jeff Cody7db16892014-04-25 17:02:32 -04004514
Stuart Brady153859b2009-06-07 00:42:17 +01004515 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004516 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004517}