blob: 28022145d56ed45b9de3d4effef10e3f63e0aa51 [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"
Max Reitzdc5f6902017-06-13 22:20:55 +020027#include "qapi/util.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020028#include "qapi-visit.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010029#include "qapi/qobject-output-visitor.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010030#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010031#include "qapi/qmp/qjson.h"
Fam Zheng335e9932017-05-03 00:35:39 +080032#include "qapi/qmp/qbool.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020033#include "qemu/cutils.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000034#include "qemu/config-file.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010035#include "qemu/option.h"
36#include "qemu/error-report.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030037#include "qemu/log.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000038#include "qom/object_interfaces.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010039#include "sysemu/sysemu.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020040#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010041#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020042#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080043#include "block/qapi.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010044#include "crypto/init.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030045#include "trace/control.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020046#include <getopt.h>
bellarde8445332006-06-14 15:32:10 +000047
Don Slutz61979a62015-01-09 10:17:35 -050048#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
Thomas Huth0781dd62016-10-05 11:54:44 +020049 "\n" QEMU_COPYRIGHT "\n"
Jeff Cody5f6979c2014-04-28 14:37:18 -040050
Anthony Liguoric227f092009-10-01 16:12:16 -050051typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010052 const char *name;
53 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050054} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010055
Federico Simoncelli8599ea42013-01-28 06:59:47 -050056enum {
57 OPTION_OUTPUT = 256,
58 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000059 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000060 OPTION_IMAGE_OPTS = 259,
Kevin Wolfb6495fa2015-07-10 18:09:18 +020061 OPTION_PATTERN = 260,
Kevin Wolf55d539c2016-06-03 13:59:41 +020062 OPTION_FLUSH_INTERVAL = 261,
63 OPTION_NO_DRAIN = 262,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +010064 OPTION_TARGET_IMAGE_OPTS = 263,
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +010065 OPTION_SIZE = 264,
Max Reitzdc5f6902017-06-13 22:20:55 +020066 OPTION_PREALLOCATION = 265,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050067};
68
69typedef enum OutputFormat {
70 OFORMAT_JSON,
71 OFORMAT_HUMAN,
72} OutputFormat;
73
Kevin Wolfe6996142016-03-15 13:03:11 +010074/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040075#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000076
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010077static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000078{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010079 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000080}
81
Fam Zhengac1307a2014-04-22 13:36:11 +080082static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
83{
84 va_list ap;
85
86 error_printf("qemu-img: ");
87
88 va_start(ap, fmt);
89 error_vprintf(fmt, ap);
90 va_end(ap);
91
92 error_printf("\nTry 'qemu-img --help' for more information\n");
93 exit(EXIT_FAILURE);
94}
95
Stefan Hajnoczic9192972017-03-17 18:45:41 +080096static void QEMU_NORETURN missing_argument(const char *option)
97{
98 error_exit("missing argument for option '%s'", option);
99}
100
101static void QEMU_NORETURN unrecognized_option(const char *option)
102{
103 error_exit("unrecognized option '%s'", option);
104}
105
blueswir1d2c639d2009-01-24 18:19:25 +0000106/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +0800107static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +0000108{
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100109 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -0400110 QEMU_IMG_VERSION
Denis V. Lunev10985132016-06-17 17:44:13 +0300111 "usage: qemu-img [standard options] command [command options]\n"
malc3f020d72010-02-08 12:04:56 +0300112 "QEMU disk image utility\n"
113 "\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300114 " '-h', '--help' display this help and exit\n"
115 " '-V', '--version' output version information and exit\n"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +0300116 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
117 " specify tracing options\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300118 "\n"
malc3f020d72010-02-08 12:04:56 +0300119 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100120#define DEF(option, callback, arg_string) \
121 " " arg_string "\n"
122#include "qemu-img-cmds.h"
123#undef DEF
124#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +0300125 "\n"
126 "Command parameters:\n"
127 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000128 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
129 " manual page for a description of the object properties. The most common\n"
130 " object type is a 'secret', which is used to supply passwords and/or\n"
131 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300132 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400133 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800134 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
135 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100136 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
137 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300138 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200139 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
140 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
141 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300142 " 'output_filename' is the destination disk image filename\n"
143 " 'output_fmt' is the destination format\n"
144 " 'options' is a comma separated list of format specific options in a\n"
145 " name=value format. Use -o ? for an overview of the options supported by the\n"
146 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800147 " 'snapshot_param' is param used for internal snapshot, format\n"
148 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
149 " '[ID_OR_NAME]'\n"
150 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
151 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300152 " '-c' indicates that target image must be compressed (qcow format only)\n"
153 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
154 " match exactly. The image doesn't need a working backing file before\n"
155 " rebasing in this case (useful for renaming the backing file)\n"
156 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200157 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100158 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200159 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
160 " contain only zeros for qemu-img to create a sparse image during\n"
161 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
162 " unallocated or zero sectors, and the destination image will always be\n"
163 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200164 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100165 " '-n' skips the target volume creation (useful if the volume is created\n"
166 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300167 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200168 "Parameters to check subcommand:\n"
169 " '-r' tries to repair any inconsistencies that are found during the check.\n"
170 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
171 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200172 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200173 "\n"
Peter Lieven2d9187b2017-02-28 13:40:07 +0100174 "Parameters to convert subcommand:\n"
175 " '-m' specifies how many coroutines work in parallel during the convert\n"
176 " process (defaults to 8)\n"
177 " '-W' allow to write to the target out of order rather than sequential\n"
178 "\n"
malc3f020d72010-02-08 12:04:56 +0300179 "Parameters to snapshot subcommand:\n"
180 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
181 " '-a' applies a snapshot (revert disk to saved state)\n"
182 " '-c' creates a snapshot\n"
183 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100184 " '-l' lists all snapshots in the given image\n"
185 "\n"
186 "Parameters to compare subcommand:\n"
187 " '-f' first image format\n"
188 " '-F' second image format\n"
Reda Sallahi86ce1f62016-08-10 04:43:12 +0200189 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
190 "\n"
191 "Parameters to dd subcommand:\n"
192 " 'bs=BYTES' read and write up to BYTES bytes at a time "
193 "(default: 512)\n"
194 " 'count=N' copy only N input blocks\n"
195 " 'if=FILE' read from FILE\n"
Reda Sallahif7c15532016-08-10 16:16:09 +0200196 " 'of=FILE' write to FILE\n"
197 " 'skip=N' skip N bs-sized blocks at the start of input\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100198
199 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100200 bdrv_iterate_format(format_print, NULL);
bellardea2384d2004-08-01 21:59:26 +0000201 printf("\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800202 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000203}
204
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000205static QemuOptsList qemu_object_opts = {
206 .name = "object",
207 .implied_opt_name = "qom-type",
208 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
209 .desc = {
210 { }
211 },
212};
213
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000214static QemuOptsList qemu_source_opts = {
215 .name = "source",
216 .implied_opt_name = "file",
217 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
218 .desc = {
219 { }
220 },
221};
222
Stefan Weil7c30f652013-06-16 17:01:05 +0200223static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100224{
225 int ret = 0;
226 if (!quiet) {
227 va_list args;
228 va_start(args, fmt);
229 ret = vprintf(fmt, args);
230 va_end(args);
231 }
232 return ret;
233}
234
bellardea2384d2004-08-01 21:59:26 +0000235
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100236static int print_block_option_help(const char *filename, const char *fmt)
237{
238 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800239 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500240 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100241
242 /* Find driver and parse its options */
243 drv = bdrv_find_format(fmt);
244 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100245 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100246 return 1;
247 }
248
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800249 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100250 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500251 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100252 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100253 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800254 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100255 return 1;
256 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800257 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100258 }
259
Chunyan Liu83d05212014-06-05 17:20:51 +0800260 qemu_opts_print_help(create_opts);
261 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100262 return 0;
263}
264
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000265
Max Reitzefaa7c42016-03-16 19:54:38 +0100266static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100267 QemuOpts *opts, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800268 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000269{
270 QDict *options;
271 Error *local_err = NULL;
272 BlockBackend *blk;
273 options = qemu_opts_to_qdict(opts, NULL);
Fam Zheng335e9932017-05-03 00:35:39 +0800274 if (force_share) {
275 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
276 && !qdict_get_bool(options, BDRV_OPT_FORCE_SHARE)) {
277 error_report("--force-share/-U conflicts with image options");
Fam Zhengadb998c2017-05-15 22:10:14 +0800278 QDECREF(options);
Fam Zheng335e9932017-05-03 00:35:39 +0800279 return NULL;
280 }
Eric Blake579cf1d2017-05-15 14:54:39 -0500281 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800282 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100283 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000284 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100285 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000286 return NULL;
287 }
Kevin Wolfce099542016-03-15 13:01:04 +0100288 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000289
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000290 return blk;
291}
292
Max Reitzefaa7c42016-03-16 19:54:38 +0100293static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100294 QDict *options,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000295 const char *fmt, int flags,
Fam Zheng335e9932017-05-03 00:35:39 +0800296 bool writethrough, bool quiet,
297 bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000298{
299 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200300 Error *local_err = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100301
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100302 if (!options) {
303 options = qdict_new();
304 }
bellard75c23802004-08-27 21:28:58 +0000305 if (fmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500306 qdict_put_str(options, "driver", fmt);
bellard75c23802004-08-27 21:28:58 +0000307 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100308
Fam Zheng335e9932017-05-03 00:35:39 +0800309 if (force_share) {
Eric Blake579cf1d2017-05-15 14:54:39 -0500310 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800311 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100312 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500313 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100314 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000315 return NULL;
bellard75c23802004-08-27 21:28:58 +0000316 }
Kevin Wolfce099542016-03-15 13:01:04 +0100317 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100318
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200319 return blk;
bellard75c23802004-08-27 21:28:58 +0000320}
321
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000322
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100323static int img_add_key_secrets(void *opaque,
324 const char *name, const char *value,
325 Error **errp)
326{
327 QDict *options = opaque;
328
329 if (g_str_has_suffix(name, "key-secret")) {
330 qdict_put(options, name, qstring_from_str(value));
331 }
332
333 return 0;
334}
335
336static BlockBackend *img_open_new_file(const char *filename,
337 QemuOpts *create_opts,
338 const char *fmt, int flags,
339 bool writethrough, bool quiet,
340 bool force_share)
341{
342 QDict *options = NULL;
343
344 options = qdict_new();
345 qemu_opt_foreach(create_opts, img_add_key_secrets, options, &error_abort);
346
347 return img_open_file(filename, options, fmt, flags, writethrough, quiet,
348 force_share);
349}
350
351
Max Reitzefaa7c42016-03-16 19:54:38 +0100352static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000353 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100354 const char *fmt, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800355 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000356{
357 BlockBackend *blk;
358 if (image_opts) {
359 QemuOpts *opts;
360 if (fmt) {
361 error_report("--image-opts and --format are mutually exclusive");
362 return NULL;
363 }
364 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
365 filename, true);
366 if (!opts) {
367 return NULL;
368 }
Fam Zheng335e9932017-05-03 00:35:39 +0800369 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
370 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000371 } else {
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100372 blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
Fam Zheng335e9932017-05-03 00:35:39 +0800373 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000374 }
375 return blk;
376}
377
378
Chunyan Liu83d05212014-06-05 17:20:51 +0800379static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100380 const char *base_filename,
381 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200382{
Markus Armbruster6750e792015-02-12 17:43:08 +0100383 Error *err = NULL;
384
Kevin Wolfefa84d42009-05-18 16:42:12 +0200385 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100386 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100387 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100388 error_report("Backing file not supported for file format '%s'",
389 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100390 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900391 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200392 }
393 }
394 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100395 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100396 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100397 error_report("Backing file format not supported for file "
398 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100399 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900400 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200401 }
402 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900403 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200404}
405
Markus Armbruster606caa02017-02-21 21:14:04 +0100406static int64_t cvtnum(const char *s)
407{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100408 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100409 uint64_t value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100410
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100411 err = qemu_strtosz(s, NULL, &value);
412 if (err < 0) {
413 return err;
414 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100415 if (value > INT64_MAX) {
416 return -ERANGE;
417 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100418 return value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100419}
420
bellardea2384d2004-08-01 21:59:26 +0000421static int img_create(int argc, char **argv)
422{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200423 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100424 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000425 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000426 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000427 const char *filename;
428 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200429 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200430 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100431 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000432
bellardea2384d2004-08-01 21:59:26 +0000433 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000434 static const struct option long_options[] = {
435 {"help", no_argument, 0, 'h'},
436 {"object", required_argument, 0, OPTION_OBJECT},
437 {0, 0, 0, 0}
438 };
Daniel P. Berrange6b4df542017-07-04 13:30:09 +0100439 c = getopt_long(argc, argv, ":F:b:f:ho:q",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000440 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100441 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000442 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100443 }
bellardea2384d2004-08-01 21:59:26 +0000444 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800445 case ':':
446 missing_argument(argv[optind - 1]);
447 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100448 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800449 unrecognized_option(argv[optind - 1]);
450 break;
bellardea2384d2004-08-01 21:59:26 +0000451 case 'h':
452 help();
453 break;
aliguori9230eaf2009-03-28 17:55:19 +0000454 case 'F':
455 base_fmt = optarg;
456 break;
bellardea2384d2004-08-01 21:59:26 +0000457 case 'b':
458 base_filename = optarg;
459 break;
460 case 'f':
461 fmt = optarg;
462 break;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200463 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100464 if (!is_valid_option_list(optarg)) {
465 error_report("Invalid option list: %s", optarg);
466 goto fail;
467 }
468 if (!options) {
469 options = g_strdup(optarg);
470 } else {
471 char *old_options = options;
472 options = g_strdup_printf("%s,%s", options, optarg);
473 g_free(old_options);
474 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200475 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100476 case 'q':
477 quiet = true;
478 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000479 case OPTION_OBJECT: {
480 QemuOpts *opts;
481 opts = qemu_opts_parse_noisily(&qemu_object_opts,
482 optarg, true);
483 if (!opts) {
484 goto fail;
485 }
486 } break;
bellardea2384d2004-08-01 21:59:26 +0000487 }
488 }
aliguori9230eaf2009-03-28 17:55:19 +0000489
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900490 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100491 filename = (optind < argc) ? argv[optind] : NULL;
492 if (options && has_help_option(options)) {
493 g_free(options);
494 return print_block_option_help(filename, fmt);
495 }
496
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100497 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800498 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100499 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100500 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900501
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000502 if (qemu_opts_foreach(&qemu_object_opts,
503 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200504 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000505 goto fail;
506 }
507
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100508 /* Get image size, if specified */
509 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100510 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100511
512 sval = cvtnum(argv[optind++]);
513 if (sval < 0) {
liguang79443392012-12-17 09:49:23 +0800514 if (sval == -ERANGE) {
515 error_report("Image size must be less than 8 EiB!");
516 } else {
517 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200518 "G, T, P or E suffixes for ");
519 error_report("kilobytes, megabytes, gigabytes, terabytes, "
520 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800521 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100522 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100523 }
524 img_size = (uint64_t)sval;
525 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200526 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800527 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200528 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100529
Luiz Capitulino9b375252012-11-30 10:52:05 -0200530 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Fam Zheng92172832017-04-21 20:27:01 +0800531 options, img_size, 0, quiet, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100532 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100533 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100534 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900535 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200536
Kevin Wolf77386bf2014-02-21 16:24:04 +0100537 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000538 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100539
540fail:
541 g_free(options);
542 return 1;
bellardea2384d2004-08-01 21:59:26 +0000543}
544
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100545static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500546{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500547 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500548 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100549 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600550
551 visit_type_ImageCheck(v, NULL, &check, &error_abort);
552 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500553 str = qobject_to_json_pretty(obj);
554 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100555 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500556 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600557 visit_free(v);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500558 QDECREF(str);
559}
560
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100561static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500562{
563 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100564 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500565 } else {
566 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100567 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
568 "Data may be corrupted, or further writes to the image "
569 "may corrupt it.\n",
570 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500571 }
572
573 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100574 qprintf(quiet,
575 "\n%" PRId64 " leaked clusters were found on the image.\n"
576 "This means waste of disk space, but no harm to data.\n",
577 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500578 }
579
580 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100581 qprintf(quiet,
582 "\n%" PRId64
583 " internal errors have occurred during the check.\n",
584 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500585 }
586 }
587
588 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100589 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
590 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
591 check->allocated_clusters, check->total_clusters,
592 check->allocated_clusters * 100.0 / check->total_clusters,
593 check->fragmented_clusters * 100.0 / check->allocated_clusters,
594 check->compressed_clusters * 100.0 /
595 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500596 }
597
598 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100599 qprintf(quiet,
600 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500601 }
602}
603
604static int collect_image_check(BlockDriverState *bs,
605 ImageCheck *check,
606 const char *filename,
607 const char *fmt,
608 int fix)
609{
610 int ret;
611 BdrvCheckResult result;
612
613 ret = bdrv_check(bs, &result, fix);
614 if (ret < 0) {
615 return ret;
616 }
617
618 check->filename = g_strdup(filename);
619 check->format = g_strdup(bdrv_get_format_name(bs));
620 check->check_errors = result.check_errors;
621 check->corruptions = result.corruptions;
622 check->has_corruptions = result.corruptions != 0;
623 check->leaks = result.leaks;
624 check->has_leaks = result.leaks != 0;
625 check->corruptions_fixed = result.corruptions_fixed;
626 check->has_corruptions_fixed = result.corruptions != 0;
627 check->leaks_fixed = result.leaks_fixed;
628 check->has_leaks_fixed = result.leaks != 0;
629 check->image_end_offset = result.image_end_offset;
630 check->has_image_end_offset = result.image_end_offset != 0;
631 check->total_clusters = result.bfi.total_clusters;
632 check->has_total_clusters = result.bfi.total_clusters != 0;
633 check->allocated_clusters = result.bfi.allocated_clusters;
634 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
635 check->fragmented_clusters = result.bfi.fragmented_clusters;
636 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100637 check->compressed_clusters = result.bfi.compressed_clusters;
638 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500639
640 return 0;
641}
642
Kevin Wolfe076f332010-06-29 11:43:13 +0200643/*
644 * Checks an image for consistency. Exit codes:
645 *
Max Reitzd6635c42014-06-02 22:15:21 +0200646 * 0 - Check completed, image is good
647 * 1 - Check not completed because of internal errors
648 * 2 - Check completed, image is corrupted
649 * 3 - Check completed, image has leaked clusters, but is good otherwise
650 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200651 */
aliguori15859692009-04-21 23:11:53 +0000652static int img_check(int argc, char **argv)
653{
654 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500655 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200656 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200657 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000658 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200659 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100660 int flags = BDRV_O_CHECK;
661 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200662 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100663 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000664 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +0800665 bool force_share = false;
aliguori15859692009-04-21 23:11:53 +0000666
667 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500668 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200669 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100670
aliguori15859692009-04-21 23:11:53 +0000671 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500672 int option_index = 0;
673 static const struct option long_options[] = {
674 {"help", no_argument, 0, 'h'},
675 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530676 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500677 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000678 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000679 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +0800680 {"force-share", no_argument, 0, 'U'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500681 {0, 0, 0, 0}
682 };
Fam Zheng335e9932017-05-03 00:35:39 +0800683 c = getopt_long(argc, argv, ":hf:r:T:qU",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500684 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100685 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000686 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100687 }
aliguori15859692009-04-21 23:11:53 +0000688 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800689 case ':':
690 missing_argument(argv[optind - 1]);
691 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100692 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800693 unrecognized_option(argv[optind - 1]);
694 break;
aliguori15859692009-04-21 23:11:53 +0000695 case 'h':
696 help();
697 break;
698 case 'f':
699 fmt = optarg;
700 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200701 case 'r':
702 flags |= BDRV_O_RDWR;
703
704 if (!strcmp(optarg, "leaks")) {
705 fix = BDRV_FIX_LEAKS;
706 } else if (!strcmp(optarg, "all")) {
707 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
708 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800709 error_exit("Unknown option value for -r "
710 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200711 }
712 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500713 case OPTION_OUTPUT:
714 output = optarg;
715 break;
Max Reitz40055952014-07-22 22:58:42 +0200716 case 'T':
717 cache = optarg;
718 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100719 case 'q':
720 quiet = true;
721 break;
Fam Zheng335e9932017-05-03 00:35:39 +0800722 case 'U':
723 force_share = true;
724 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000725 case OPTION_OBJECT: {
726 QemuOpts *opts;
727 opts = qemu_opts_parse_noisily(&qemu_object_opts,
728 optarg, true);
729 if (!opts) {
730 return 1;
731 }
732 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000733 case OPTION_IMAGE_OPTS:
734 image_opts = true;
735 break;
aliguori15859692009-04-21 23:11:53 +0000736 }
737 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200738 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800739 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100740 }
aliguori15859692009-04-21 23:11:53 +0000741 filename = argv[optind++];
742
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500743 if (output && !strcmp(output, "json")) {
744 output_format = OFORMAT_JSON;
745 } else if (output && !strcmp(output, "human")) {
746 output_format = OFORMAT_HUMAN;
747 } else if (output) {
748 error_report("--output must be used with human or json as argument.");
749 return 1;
750 }
751
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000752 if (qemu_opts_foreach(&qemu_object_opts,
753 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200754 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000755 return 1;
756 }
757
Kevin Wolfce099542016-03-15 13:01:04 +0100758 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200759 if (ret < 0) {
760 error_report("Invalid source cache option: %s", cache);
761 return 1;
762 }
763
Fam Zheng335e9932017-05-03 00:35:39 +0800764 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
765 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200766 if (!blk) {
767 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900768 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200769 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500770
771 check = g_new0(ImageCheck, 1);
772 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200773
774 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200775 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200776 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500777 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200778 }
779
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500780 if (check->corruptions_fixed || check->leaks_fixed) {
781 int corruptions_fixed, leaks_fixed;
782
783 leaks_fixed = check->leaks_fixed;
784 corruptions_fixed = check->corruptions_fixed;
785
786 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100787 qprintf(quiet,
788 "The following inconsistencies were found and repaired:\n\n"
789 " %" PRId64 " leaked clusters\n"
790 " %" PRId64 " corruptions\n\n"
791 "Double checking the fixed image now...\n",
792 check->leaks_fixed,
793 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500794 }
795
796 ret = collect_image_check(bs, check, filename, fmt, 0);
797
798 check->leaks_fixed = leaks_fixed;
799 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200800 }
801
Max Reitz832390a2014-10-23 15:29:12 +0200802 if (!ret) {
803 switch (output_format) {
804 case OFORMAT_HUMAN:
805 dump_human_image_check(check, quiet);
806 break;
807 case OFORMAT_JSON:
808 dump_json_image_check(check, quiet);
809 break;
810 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500811 }
812
813 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200814 if (ret) {
815 error_report("Check failed: %s", strerror(-ret));
816 } else {
817 error_report("Check failed");
818 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500819 ret = 1;
820 goto fail;
821 }
822
823 if (check->corruptions) {
824 ret = 2;
825 } else if (check->leaks) {
826 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200827 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500828 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000829 }
830
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500831fail:
832 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200833 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500834 return ret;
aliguori15859692009-04-21 23:11:53 +0000835}
836
Max Reitzd4a32382014-10-24 15:57:37 +0200837typedef struct CommonBlockJobCBInfo {
838 BlockDriverState *bs;
839 Error **errp;
840} CommonBlockJobCBInfo;
841
842static void common_block_job_cb(void *opaque, int ret)
843{
844 CommonBlockJobCBInfo *cbi = opaque;
845
846 if (ret < 0) {
847 error_setg_errno(cbi->errp, -ret, "Block job failed");
848 }
Max Reitzd4a32382014-10-24 15:57:37 +0200849}
850
851static void run_block_job(BlockJob *job, Error **errp)
852{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200853 AioContext *aio_context = blk_get_aio_context(job->blk);
sochin.jiang4172a002017-06-15 14:47:33 +0800854 int ret = 0;
Max Reitzd4a32382014-10-24 15:57:37 +0200855
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200856 aio_context_acquire(aio_context);
sochin.jiang4172a002017-06-15 14:47:33 +0800857 block_job_ref(job);
Max Reitzd4a32382014-10-24 15:57:37 +0200858 do {
859 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500860 qemu_progress_print(job->len ?
861 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
sochin.jiang4172a002017-06-15 14:47:33 +0800862 } while (!job->ready && !job->completed);
Max Reitzd4a32382014-10-24 15:57:37 +0200863
sochin.jiang4172a002017-06-15 14:47:33 +0800864 if (!job->completed) {
865 ret = block_job_complete_sync(job, errp);
866 } else {
867 ret = job->ret;
868 }
869 block_job_unref(job);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200870 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200871
sochin.jiang4172a002017-06-15 14:47:33 +0800872 /* publish completion progress only when success */
873 if (!ret) {
874 qemu_progress_print(100.f, 0);
875 }
Max Reitzd4a32382014-10-24 15:57:37 +0200876}
877
bellardea2384d2004-08-01 21:59:26 +0000878static int img_commit(int argc, char **argv)
879{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400880 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200881 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200882 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200883 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100884 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200885 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100886 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200887 Error *local_err = NULL;
888 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000889 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200890 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000891
892 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400893 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200894 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000895 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000896 static const struct option long_options[] = {
897 {"help", no_argument, 0, 'h'},
898 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000899 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000900 {0, 0, 0, 0}
901 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800902 c = getopt_long(argc, argv, ":f:ht:b:dpq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000903 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100904 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000905 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100906 }
bellardea2384d2004-08-01 21:59:26 +0000907 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800908 case ':':
909 missing_argument(argv[optind - 1]);
910 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100911 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800912 unrecognized_option(argv[optind - 1]);
913 break;
bellardea2384d2004-08-01 21:59:26 +0000914 case 'h':
915 help();
916 break;
917 case 'f':
918 fmt = optarg;
919 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400920 case 't':
921 cache = optarg;
922 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200923 case 'b':
924 base = optarg;
925 /* -b implies -d */
926 drop = true;
927 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200928 case 'd':
929 drop = true;
930 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200931 case 'p':
932 progress = true;
933 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100934 case 'q':
935 quiet = true;
936 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000937 case OPTION_OBJECT: {
938 QemuOpts *opts;
939 opts = qemu_opts_parse_noisily(&qemu_object_opts,
940 optarg, true);
941 if (!opts) {
942 return 1;
943 }
944 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000945 case OPTION_IMAGE_OPTS:
946 image_opts = true;
947 break;
bellardea2384d2004-08-01 21:59:26 +0000948 }
949 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200950
951 /* Progress is not shown in Quiet mode */
952 if (quiet) {
953 progress = false;
954 }
955
Kevin Wolffc11eb22013-08-05 10:53:04 +0200956 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800957 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100958 }
bellardea2384d2004-08-01 21:59:26 +0000959 filename = argv[optind++];
960
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000961 if (qemu_opts_foreach(&qemu_object_opts,
962 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200963 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000964 return 1;
965 }
966
Max Reitz9a86fe42014-10-24 15:57:38 +0200967 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100968 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400969 if (ret < 0) {
970 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100971 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400972 }
973
Fam Zheng335e9932017-05-03 00:35:39 +0800974 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
975 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200976 if (!blk) {
977 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900978 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200979 bs = blk_bs(blk);
980
Max Reitz687fa1d2014-10-24 15:57:39 +0200981 qemu_progress_init(progress, 1.f);
982 qemu_progress_print(0.f, 100);
983
Max Reitz1b22bff2014-10-24 15:57:40 +0200984 if (base) {
985 base_bs = bdrv_find_backing_image(bs, base);
986 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +0100987 error_setg(&local_err,
988 "Did not find '%s' in the backing chain of '%s'",
989 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +0200990 goto done;
991 }
992 } else {
993 /* This is different from QMP, which by default uses the deepest file in
994 * the backing chain (i.e., the very base); however, the traditional
995 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +0200996 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +0200997 if (!base_bs) {
998 error_setg(&local_err, "Image does not have a backing file");
999 goto done;
1000 }
bellardea2384d2004-08-01 21:59:26 +00001001 }
1002
Max Reitzd4a32382014-10-24 15:57:37 +02001003 cbi = (CommonBlockJobCBInfo){
1004 .errp = &local_err,
1005 .bs = bs,
1006 };
1007
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001008 aio_context = bdrv_get_aio_context(bs);
1009 aio_context_acquire(aio_context);
John Snow47970df2016-10-27 12:06:57 -04001010 commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
Kevin Wolf0db832f2017-02-20 18:10:05 +01001011 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +08001012 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001013 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +02001014 if (local_err) {
1015 goto done;
1016 }
1017
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001018 /* When the block job completes, the BlockBackend reference will point to
1019 * the old backing file. In order to avoid that the top image is already
1020 * deleted, so we can still empty it afterwards, increment the reference
1021 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +02001022 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001023 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001024 }
1025
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001026 job = block_job_get("commit");
1027 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001028 if (local_err) {
1029 goto unref_backing;
1030 }
1031
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001032 if (!drop && bs->drv->bdrv_make_empty) {
1033 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001034 if (ret) {
1035 error_setg_errno(&local_err, -ret, "Could not empty %s",
1036 filename);
1037 goto unref_backing;
1038 }
1039 }
1040
1041unref_backing:
1042 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001043 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001044 }
Max Reitzd4a32382014-10-24 15:57:37 +02001045
1046done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001047 qemu_progress_end();
1048
Markus Armbruster26f54e92014-10-07 13:59:04 +02001049 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001050
1051 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001052 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001053 return 1;
1054 }
Max Reitzd4a32382014-10-24 15:57:37 +02001055
1056 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001057 return 0;
1058}
1059
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001060/*
thsf58c7b32008-06-05 21:53:49 +00001061 * Returns true iff the first sector pointed to by 'buf' contains at least
1062 * a non-NUL byte.
1063 *
1064 * 'pnum' is set to the number of sectors (including and immediately following
1065 * the first one) that are known to be in the same allocated/unallocated state.
1066 */
bellardea2384d2004-08-01 21:59:26 +00001067static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1068{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001069 bool is_zero;
1070 int i;
bellardea2384d2004-08-01 21:59:26 +00001071
1072 if (n <= 0) {
1073 *pnum = 0;
1074 return 0;
1075 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001076 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001077 for(i = 1; i < n; i++) {
1078 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001079 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001080 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001081 }
bellardea2384d2004-08-01 21:59:26 +00001082 }
1083 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001084 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001085}
1086
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001087/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001088 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1089 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1090 * breaking up write requests for only small sparse areas.
1091 */
1092static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1093 int min)
1094{
1095 int ret;
1096 int num_checked, num_used;
1097
1098 if (n < min) {
1099 min = n;
1100 }
1101
1102 ret = is_allocated_sectors(buf, n, pnum);
1103 if (!ret) {
1104 return ret;
1105 }
1106
1107 num_used = *pnum;
1108 buf += BDRV_SECTOR_SIZE * *pnum;
1109 n -= *pnum;
1110 num_checked = num_used;
1111
1112 while (n > 0) {
1113 ret = is_allocated_sectors(buf, n, pnum);
1114
1115 buf += BDRV_SECTOR_SIZE * *pnum;
1116 n -= *pnum;
1117 num_checked += *pnum;
1118 if (ret) {
1119 num_used = num_checked;
1120 } else if (*pnum >= min) {
1121 break;
1122 }
1123 }
1124
1125 *pnum = num_used;
1126 return 1;
1127}
1128
1129/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001130 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1131 * buffers matches, non-zero otherwise.
1132 *
1133 * pnum is set to the number of sectors (including and immediately following
1134 * the first one) that are known to have the same comparison result
1135 */
1136static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1137 int *pnum)
1138{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001139 bool res;
1140 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001141
1142 if (n <= 0) {
1143 *pnum = 0;
1144 return 0;
1145 }
1146
1147 res = !!memcmp(buf1, buf2, 512);
1148 for(i = 1; i < n; i++) {
1149 buf1 += 512;
1150 buf2 += 512;
1151
1152 if (!!memcmp(buf1, buf2, 512) != res) {
1153 break;
1154 }
1155 }
1156
1157 *pnum = i;
1158 return res;
1159}
1160
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001161#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001162
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001163static int64_t sectors_to_bytes(int64_t sectors)
1164{
1165 return sectors << BDRV_SECTOR_BITS;
1166}
1167
1168static int64_t sectors_to_process(int64_t total, int64_t from)
1169{
1170 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1171}
1172
1173/*
1174 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1175 *
1176 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1177 * data and negative value on error.
1178 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001179 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001180 * @param sect_num: Number of first sector to check
1181 * @param sect_count: Number of sectors to check
1182 * @param filename: Name of disk file we are checking (logging purpose)
1183 * @param buffer: Allocated buffer for storing read data
1184 * @param quiet: Flag for quiet mode
1185 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001186static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001187 int sect_count, const char *filename,
1188 uint8_t *buffer, bool quiet)
1189{
1190 int pnum, ret = 0;
Eric Blake91669202016-05-06 10:26:43 -06001191 ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer,
1192 sect_count << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001193 if (ret < 0) {
1194 error_report("Error while reading offset %" PRId64 " of %s: %s",
1195 sectors_to_bytes(sect_num), filename, strerror(-ret));
1196 return ret;
1197 }
1198 ret = is_allocated_sectors(buffer, sect_count, &pnum);
1199 if (ret || pnum != sect_count) {
1200 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1201 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1202 return 1;
1203 }
1204
1205 return 0;
1206}
1207
1208/*
1209 * Compares two images. Exit codes:
1210 *
1211 * 0 - Images are identical
1212 * 1 - Images differ
1213 * >1 - Error occurred
1214 */
1215static int img_compare(int argc, char **argv)
1216{
Max Reitz40055952014-07-22 22:58:42 +02001217 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001218 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001219 BlockDriverState *bs1, *bs2;
1220 int64_t total_sectors1, total_sectors2;
1221 uint8_t *buf1 = NULL, *buf2 = NULL;
1222 int pnum1, pnum2;
1223 int allocated1, allocated2;
1224 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1225 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001226 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001227 bool writethrough;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001228 int64_t total_sectors;
1229 int64_t sector_num = 0;
1230 int64_t nb_sectors;
1231 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001232 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001233 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001234 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001235
Max Reitz40055952014-07-22 22:58:42 +02001236 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001237 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001238 static const struct option long_options[] = {
1239 {"help", no_argument, 0, 'h'},
1240 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001241 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001242 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001243 {0, 0, 0, 0}
1244 };
Fam Zheng335e9932017-05-03 00:35:39 +08001245 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001246 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001247 if (c == -1) {
1248 break;
1249 }
1250 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001251 case ':':
1252 missing_argument(argv[optind - 1]);
1253 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001254 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001255 unrecognized_option(argv[optind - 1]);
1256 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001257 case 'h':
1258 help();
1259 break;
1260 case 'f':
1261 fmt1 = optarg;
1262 break;
1263 case 'F':
1264 fmt2 = optarg;
1265 break;
Max Reitz40055952014-07-22 22:58:42 +02001266 case 'T':
1267 cache = optarg;
1268 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001269 case 'p':
1270 progress = true;
1271 break;
1272 case 'q':
1273 quiet = true;
1274 break;
1275 case 's':
1276 strict = true;
1277 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001278 case 'U':
1279 force_share = true;
1280 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001281 case OPTION_OBJECT: {
1282 QemuOpts *opts;
1283 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1284 optarg, true);
1285 if (!opts) {
1286 ret = 2;
1287 goto out4;
1288 }
1289 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001290 case OPTION_IMAGE_OPTS:
1291 image_opts = true;
1292 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001293 }
1294 }
1295
1296 /* Progress is not shown in Quiet mode */
1297 if (quiet) {
1298 progress = false;
1299 }
1300
1301
Kevin Wolffc11eb22013-08-05 10:53:04 +02001302 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001303 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001304 }
1305 filename1 = argv[optind++];
1306 filename2 = argv[optind++];
1307
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001308 if (qemu_opts_foreach(&qemu_object_opts,
1309 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001310 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001311 ret = 2;
1312 goto out4;
1313 }
1314
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001315 /* Initialize before goto out */
1316 qemu_progress_init(progress, 2.0);
1317
Kevin Wolfce099542016-03-15 13:01:04 +01001318 flags = 0;
1319 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001320 if (ret < 0) {
1321 error_report("Invalid source cache option: %s", cache);
1322 ret = 2;
1323 goto out3;
1324 }
1325
Fam Zheng335e9932017-05-03 00:35:39 +08001326 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1327 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001328 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001329 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001330 goto out3;
1331 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001332
Fam Zheng335e9932017-05-03 00:35:39 +08001333 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1334 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001335 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001336 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001337 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001338 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001339 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001340 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001341
Max Reitzf1d3cd72015-02-05 13:58:18 -05001342 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1343 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1344 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001345 if (total_sectors1 < 0) {
1346 error_report("Can't get size of %s: %s",
1347 filename1, strerror(-total_sectors1));
1348 ret = 4;
1349 goto out;
1350 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001351 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001352 if (total_sectors2 < 0) {
1353 error_report("Can't get size of %s: %s",
1354 filename2, strerror(-total_sectors2));
1355 ret = 4;
1356 goto out;
1357 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001358 total_sectors = MIN(total_sectors1, total_sectors2);
1359 progress_base = MAX(total_sectors1, total_sectors2);
1360
1361 qemu_progress_print(0, 100);
1362
1363 if (strict && total_sectors1 != total_sectors2) {
1364 ret = 1;
1365 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1366 goto out;
1367 }
1368
1369 for (;;) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001370 int64_t status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001371 BlockDriverState *file;
1372
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001373 nb_sectors = sectors_to_process(total_sectors, sector_num);
1374 if (nb_sectors <= 0) {
1375 break;
1376 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001377 status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1378 total_sectors1 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001379 &pnum1, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001380 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001381 ret = 3;
1382 error_report("Sector allocation test failed for %s", filename1);
1383 goto out;
1384 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001385 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001386
Fam Zheng25ad8e62016-01-13 16:37:41 +08001387 status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1388 total_sectors2 - sector_num,
Fam Zheng67a0fd22016-01-26 11:58:48 +08001389 &pnum2, &file);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001390 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001391 ret = 3;
1392 error_report("Sector allocation test failed for %s", filename2);
1393 goto out;
1394 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001395 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1396 if (pnum1) {
1397 nb_sectors = MIN(nb_sectors, pnum1);
1398 }
1399 if (pnum2) {
1400 nb_sectors = MIN(nb_sectors, pnum2);
1401 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001402
Fam Zheng25ad8e62016-01-13 16:37:41 +08001403 if (strict) {
1404 if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1405 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1406 ret = 1;
1407 qprintf(quiet, "Strict mode: Offset %" PRId64
1408 " block status mismatch!\n",
1409 sectors_to_bytes(sector_num));
1410 goto out;
1411 }
1412 }
1413 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1414 nb_sectors = MIN(pnum1, pnum2);
1415 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001416 if (allocated1) {
Eric Blake91669202016-05-06 10:26:43 -06001417 ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1,
1418 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001419 if (ret < 0) {
1420 error_report("Error while reading offset %" PRId64 " of %s:"
1421 " %s", sectors_to_bytes(sector_num), filename1,
1422 strerror(-ret));
1423 ret = 4;
1424 goto out;
1425 }
Eric Blake91669202016-05-06 10:26:43 -06001426 ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2,
1427 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001428 if (ret < 0) {
1429 error_report("Error while reading offset %" PRId64
1430 " of %s: %s", sectors_to_bytes(sector_num),
1431 filename2, strerror(-ret));
1432 ret = 4;
1433 goto out;
1434 }
1435 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1436 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001437 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1438 sectors_to_bytes(
1439 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001440 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001441 goto out;
1442 }
1443 }
1444 } else {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001445
1446 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001447 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001448 filename1, buf1, quiet);
1449 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001450 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001451 filename2, buf1, quiet);
1452 }
1453 if (ret) {
1454 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001455 error_report("Error while reading offset %" PRId64 ": %s",
1456 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001457 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001458 }
1459 goto out;
1460 }
1461 }
1462 sector_num += nb_sectors;
1463 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1464 }
1465
1466 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001467 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001468 int64_t total_sectors_over;
1469 const char *filename_over;
1470
1471 qprintf(quiet, "Warning: Image size mismatch!\n");
1472 if (total_sectors1 > total_sectors2) {
1473 total_sectors_over = total_sectors1;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001474 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001475 filename_over = filename1;
1476 } else {
1477 total_sectors_over = total_sectors2;
Max Reitzf1d3cd72015-02-05 13:58:18 -05001478 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001479 filename_over = filename2;
1480 }
1481
1482 for (;;) {
Eric Blake51b0a482017-07-07 07:44:59 -05001483 int64_t count;
1484
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001485 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1486 if (nb_sectors <= 0) {
1487 break;
1488 }
Eric Blake51b0a482017-07-07 07:44:59 -05001489 ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL,
1490 sector_num * BDRV_SECTOR_SIZE,
1491 nb_sectors * BDRV_SECTOR_SIZE,
1492 &count);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001493 if (ret < 0) {
1494 ret = 3;
1495 error_report("Sector allocation test failed for %s",
1496 filename_over);
1497 goto out;
1498
1499 }
Eric Blake51b0a482017-07-07 07:44:59 -05001500 /* TODO relax this once bdrv_is_allocated_above does not enforce
1501 * sector alignment */
1502 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
1503 nb_sectors = count >> BDRV_SECTOR_BITS;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001504 if (ret) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001505 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001506 filename_over, buf1, quiet);
1507 if (ret) {
1508 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001509 error_report("Error while reading offset %" PRId64
1510 " of %s: %s", sectors_to_bytes(sector_num),
1511 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001512 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001513 }
1514 goto out;
1515 }
1516 }
1517 sector_num += nb_sectors;
1518 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1519 }
1520 }
1521
1522 qprintf(quiet, "Images are identical.\n");
1523 ret = 0;
1524
1525out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001526 qemu_vfree(buf1);
1527 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001528 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001529out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001530 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001531out3:
1532 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001533out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001534 return ret;
1535}
1536
Kevin Wolf690c7302015-03-19 13:33:32 +01001537enum ImgConvertBlockStatus {
1538 BLK_DATA,
1539 BLK_ZERO,
1540 BLK_BACKING_FILE,
1541};
1542
Peter Lieven2d9187b2017-02-28 13:40:07 +01001543#define MAX_COROUTINES 16
1544
Kevin Wolf690c7302015-03-19 13:33:32 +01001545typedef struct ImgConvertState {
1546 BlockBackend **src;
1547 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001548 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001549 int64_t total_sectors;
1550 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001551 int64_t allocated_done;
1552 int64_t sector_num;
1553 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001554 enum ImgConvertBlockStatus status;
1555 int64_t sector_next_status;
1556 BlockBackend *target;
1557 bool has_zero_init;
1558 bool compressed;
1559 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001560 bool wr_in_order;
Kevin Wolf690c7302015-03-19 13:33:32 +01001561 int min_sparse;
1562 size_t cluster_sectors;
1563 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001564 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001565 int running_coroutines;
1566 Coroutine *co[MAX_COROUTINES];
1567 int64_t wait_sector_num[MAX_COROUTINES];
1568 CoMutex lock;
1569 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001570} ImgConvertState;
1571
Peter Lieven2d9187b2017-02-28 13:40:07 +01001572static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1573 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001574{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001575 *src_cur = 0;
1576 *src_cur_offset = 0;
1577 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1578 *src_cur_offset += s->src_sectors[*src_cur];
1579 (*src_cur)++;
1580 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001581 }
1582}
1583
1584static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1585{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001586 int64_t ret, src_cur_offset;
1587 int n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001588
Peter Lieven2d9187b2017-02-28 13:40:07 +01001589 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001590
1591 assert(s->total_sectors > sector_num);
1592 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1593
1594 if (s->sector_next_status <= sector_num) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08001595 BlockDriverState *file;
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001596 if (s->target_has_backing) {
1597 ret = bdrv_get_block_status(blk_bs(s->src[src_cur]),
1598 sector_num - src_cur_offset,
1599 n, &n, &file);
1600 } else {
1601 ret = bdrv_get_block_status_above(blk_bs(s->src[src_cur]), NULL,
1602 sector_num - src_cur_offset,
1603 n, &n, &file);
1604 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001605 if (ret < 0) {
1606 return ret;
1607 }
1608
1609 if (ret & BDRV_BLOCK_ZERO) {
1610 s->status = BLK_ZERO;
1611 } else if (ret & BDRV_BLOCK_DATA) {
1612 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001613 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001614 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001615 }
1616
1617 s->sector_next_status = sector_num + n;
1618 }
1619
1620 n = MIN(n, s->sector_next_status - sector_num);
1621 if (s->status == BLK_DATA) {
1622 n = MIN(n, s->buf_sectors);
1623 }
1624
1625 /* We need to write complete clusters for compressed images, so if an
1626 * unallocated area is shorter than that, we must consider the whole
1627 * cluster allocated. */
1628 if (s->compressed) {
1629 if (n < s->cluster_sectors) {
1630 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1631 s->status = BLK_DATA;
1632 } else {
1633 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1634 }
1635 }
1636
1637 return n;
1638}
1639
Peter Lieven2d9187b2017-02-28 13:40:07 +01001640static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1641 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001642{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001643 int n, ret;
1644 QEMUIOVector qiov;
1645 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001646
Kevin Wolf690c7302015-03-19 13:33:32 +01001647 assert(nb_sectors <= s->buf_sectors);
1648 while (nb_sectors > 0) {
1649 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001650 int src_cur;
1651 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001652
1653 /* In the case of compression with multiple source files, we can get a
1654 * nb_sectors that spreads into the next part. So we must be able to
1655 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001656 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1657 blk = s->src[src_cur];
1658 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001659
Peter Lieven2d9187b2017-02-28 13:40:07 +01001660 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1661 iov.iov_base = buf;
1662 iov.iov_len = n << BDRV_SECTOR_BITS;
1663 qemu_iovec_init_external(&qiov, &iov, 1);
1664
1665 ret = blk_co_preadv(
1666 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1667 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001668 if (ret < 0) {
1669 return ret;
1670 }
1671
1672 sector_num += n;
1673 nb_sectors -= n;
1674 buf += n * BDRV_SECTOR_SIZE;
1675 }
1676
1677 return 0;
1678}
1679
Peter Lieven2d9187b2017-02-28 13:40:07 +01001680
1681static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1682 int nb_sectors, uint8_t *buf,
1683 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001684{
1685 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001686 QEMUIOVector qiov;
1687 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001688
1689 while (nb_sectors > 0) {
1690 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001691 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1692
Peter Lieven2d9187b2017-02-28 13:40:07 +01001693 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001694 case BLK_BACKING_FILE:
1695 /* If we have a backing file, leave clusters unallocated that are
1696 * unallocated in the source image, so that the backing file is
1697 * visible at the respective offset. */
1698 assert(s->target_has_backing);
1699 break;
1700
1701 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001702 /* If we're told to keep the target fully allocated (-S 0) or there
1703 * is real non-zero data, we must write it. Otherwise we can treat
1704 * it as zero sectors.
1705 * Compressed clusters need to be written as a whole, so in that
1706 * case we can only save the write if the buffer is completely
1707 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001708 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001709 (!s->compressed &&
1710 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1711 (s->compressed &&
1712 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001713 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001714 iov.iov_base = buf;
1715 iov.iov_len = n << BDRV_SECTOR_BITS;
1716 qemu_iovec_init_external(&qiov, &iov, 1);
1717
1718 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
Lidong Chendb933fb2017-04-27 10:58:27 +08001719 n << BDRV_SECTOR_BITS, &qiov, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001720 if (ret < 0) {
1721 return ret;
1722 }
1723 break;
1724 }
1725 /* fall-through */
1726
1727 case BLK_ZERO:
1728 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001729 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001730 break;
1731 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001732 ret = blk_co_pwrite_zeroes(s->target,
1733 sector_num << BDRV_SECTOR_BITS,
1734 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001735 if (ret < 0) {
1736 return ret;
1737 }
1738 break;
1739 }
1740
1741 sector_num += n;
1742 nb_sectors -= n;
1743 buf += n * BDRV_SECTOR_SIZE;
1744 }
1745
1746 return 0;
1747}
1748
Peter Lieven2d9187b2017-02-28 13:40:07 +01001749static void coroutine_fn convert_co_do_copy(void *opaque)
1750{
1751 ImgConvertState *s = opaque;
1752 uint8_t *buf = NULL;
1753 int ret, i;
1754 int index = -1;
1755
1756 for (i = 0; i < s->num_coroutines; i++) {
1757 if (s->co[i] == qemu_coroutine_self()) {
1758 index = i;
1759 break;
1760 }
1761 }
1762 assert(index >= 0);
1763
1764 s->running_coroutines++;
1765 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1766
1767 while (1) {
1768 int n;
1769 int64_t sector_num;
1770 enum ImgConvertBlockStatus status;
1771
1772 qemu_co_mutex_lock(&s->lock);
1773 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1774 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001775 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001776 }
1777 n = convert_iteration_sectors(s, s->sector_num);
1778 if (n < 0) {
1779 qemu_co_mutex_unlock(&s->lock);
1780 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001781 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001782 }
1783 /* save current sector and allocation status to local variables */
1784 sector_num = s->sector_num;
1785 status = s->status;
1786 if (!s->min_sparse && s->status == BLK_ZERO) {
1787 n = MIN(n, s->buf_sectors);
1788 }
1789 /* increment global sector counter so that other coroutines can
1790 * already continue reading beyond this request */
1791 s->sector_num += n;
1792 qemu_co_mutex_unlock(&s->lock);
1793
1794 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1795 s->allocated_done += n;
1796 qemu_progress_print(100.0 * s->allocated_done /
1797 s->allocated_sectors, 0);
1798 }
1799
1800 if (status == BLK_DATA) {
1801 ret = convert_co_read(s, sector_num, n, buf);
1802 if (ret < 0) {
1803 error_report("error while reading sector %" PRId64
1804 ": %s", sector_num, strerror(-ret));
1805 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001806 }
1807 } else if (!s->min_sparse && status == BLK_ZERO) {
1808 status = BLK_DATA;
1809 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1810 }
1811
1812 if (s->wr_in_order) {
1813 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03001814 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001815 s->wait_sector_num[index] = sector_num;
1816 qemu_coroutine_yield();
1817 }
1818 s->wait_sector_num[index] = -1;
1819 }
1820
Anton Nefedovb91127e2017-04-26 11:33:15 +03001821 if (s->ret == -EINPROGRESS) {
1822 ret = convert_co_write(s, sector_num, n, buf, status);
1823 if (ret < 0) {
1824 error_report("error while writing sector %" PRId64
1825 ": %s", sector_num, strerror(-ret));
1826 s->ret = ret;
1827 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001828 }
1829
1830 if (s->wr_in_order) {
1831 /* reenter the coroutine that might have waited
1832 * for this write to complete */
1833 s->wr_offs = sector_num + n;
1834 for (i = 0; i < s->num_coroutines; i++) {
1835 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1836 /*
1837 * A -> B -> A cannot occur because A has
1838 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1839 * B will never enter A during this time window.
1840 */
1841 qemu_coroutine_enter(s->co[i]);
1842 break;
1843 }
1844 }
1845 }
1846 }
1847
Peter Lieven2d9187b2017-02-28 13:40:07 +01001848 qemu_vfree(buf);
1849 s->co[index] = NULL;
1850 s->running_coroutines--;
1851 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1852 /* the convert job finished successfully */
1853 s->ret = 0;
1854 }
1855}
1856
Kevin Wolf690c7302015-03-19 13:33:32 +01001857static int convert_do_copy(ImgConvertState *s)
1858{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001859 int ret, i, n;
1860 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001861
1862 /* Check whether we have zero initialisation or can get it efficiently */
1863 s->has_zero_init = s->min_sparse && !s->target_has_backing
1864 ? bdrv_has_zero_init(blk_bs(s->target))
1865 : false;
1866
1867 if (!s->has_zero_init && !s->target_has_backing &&
1868 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1869 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001870 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001871 if (ret == 0) {
1872 s->has_zero_init = true;
1873 }
1874 }
1875
1876 /* Allocate buffer for copied data. For compressed images, only one cluster
1877 * can be copied at a time. */
1878 if (s->compressed) {
1879 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1880 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001881 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001882 }
1883 s->buf_sectors = s->cluster_sectors;
1884 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001885
Kevin Wolf690c7302015-03-19 13:33:32 +01001886 while (sector_num < s->total_sectors) {
1887 n = convert_iteration_sectors(s, sector_num);
1888 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001889 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001890 }
Max Reitzaad15de2016-03-24 23:33:57 +01001891 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1892 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001893 s->allocated_sectors += n;
1894 }
1895 sector_num += n;
1896 }
1897
1898 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001899 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001900 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001901
Peter Lieven2d9187b2017-02-28 13:40:07 +01001902 qemu_co_mutex_init(&s->lock);
1903 for (i = 0; i < s->num_coroutines; i++) {
1904 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1905 s->wait_sector_num[i] = -1;
1906 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001907 }
1908
Anton Nefedovb91127e2017-04-26 11:33:15 +03001909 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001910 main_loop_wait(false);
1911 }
1912
1913 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001914 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001915 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001916 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001917 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001918 }
1919 }
1920
Peter Lieven2d9187b2017-02-28 13:40:07 +01001921 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001922}
1923
bellardea2384d2004-08-01 21:59:26 +00001924static int img_convert(int argc, char **argv)
1925{
Peter Lieven9fd77f92017-04-21 11:11:55 +02001926 int c, bs_i, flags, src_flags = 0;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001927 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02001928 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1929 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001930 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00001931 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001932 BlockDriverState *out_bs;
1933 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08001934 QemuOptsList *create_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001935 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02001936 Error *local_err = NULL;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001937 bool writethrough, src_writethrough, quiet = false, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001938 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001939 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08001940 bool force_share = false;
bellardea2384d2004-08-01 21:59:26 +00001941
Peter Lieven9fd77f92017-04-21 11:11:55 +02001942 ImgConvertState s = (ImgConvertState) {
1943 /* Need at least 4k of zeros for sparse detection */
1944 .min_sparse = 8,
1945 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1946 .wr_in_order = true,
1947 .num_coroutines = 8,
1948 };
1949
bellardea2384d2004-08-01 21:59:26 +00001950 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001951 static const struct option long_options[] = {
1952 {"help", no_argument, 0, 'h'},
1953 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001954 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001955 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001956 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001957 {0, 0, 0, 0}
1958 };
Daniel P. Berrange6b4df542017-07-04 13:30:09 +01001959 c = getopt_long(argc, argv, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001960 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001961 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001962 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001963 }
bellardea2384d2004-08-01 21:59:26 +00001964 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001965 case ':':
1966 missing_argument(argv[optind - 1]);
1967 break;
Jes Sorensenef873942010-12-06 15:25:40 +01001968 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001969 unrecognized_option(argv[optind - 1]);
1970 break;
bellardea2384d2004-08-01 21:59:26 +00001971 case 'h':
1972 help();
1973 break;
1974 case 'f':
1975 fmt = optarg;
1976 break;
1977 case 'O':
1978 out_fmt = optarg;
1979 break;
thsf58c7b32008-06-05 21:53:49 +00001980 case 'B':
1981 out_baseimg = optarg;
1982 break;
bellardea2384d2004-08-01 21:59:26 +00001983 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02001984 s.compressed = true;
bellardea2384d2004-08-01 21:59:26 +00001985 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001986 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001987 if (!is_valid_option_list(optarg)) {
1988 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001989 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001990 }
1991 if (!options) {
1992 options = g_strdup(optarg);
1993 } else {
1994 char *old_options = options;
1995 options = g_strdup_printf("%s,%s", options, optarg);
1996 g_free(old_options);
1997 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001998 break;
edison51ef6722010-09-21 19:58:41 -07001999 case 's':
2000 snapshot_name = optarg;
2001 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08002002 case 'l':
2003 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01002004 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2005 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08002006 if (!sn_opts) {
2007 error_report("Failed in parsing snapshot param '%s'",
2008 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002009 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002010 }
2011 } else {
2012 snapshot_name = optarg;
2013 }
2014 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002015 case 'S':
2016 {
2017 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002018
2019 sval = cvtnum(optarg);
2020 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02002021 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002022 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002023 }
2024
Peter Lieven9fd77f92017-04-21 11:11:55 +02002025 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002026 break;
2027 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002028 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002029 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002030 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002031 case 't':
2032 cache = optarg;
2033 break;
Max Reitz40055952014-07-22 22:58:42 +02002034 case 'T':
2035 src_cache = optarg;
2036 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002037 case 'q':
2038 quiet = true;
2039 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002040 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002041 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002042 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002043 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002044 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2045 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002046 error_report("Invalid number of coroutines. Allowed number of"
2047 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002048 goto fail_getopt;
2049 }
2050 break;
2051 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002052 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002053 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002054 case 'U':
2055 force_share = true;
2056 break;
Max Reitz3258b912017-04-26 15:46:47 +02002057 case OPTION_OBJECT: {
2058 QemuOpts *object_opts;
2059 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2060 optarg, true);
2061 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002062 goto fail_getopt;
2063 }
2064 break;
Max Reitz3258b912017-04-26 15:46:47 +02002065 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002066 case OPTION_IMAGE_OPTS:
2067 image_opts = true;
2068 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002069 case OPTION_TARGET_IMAGE_OPTS:
2070 tgt_image_opts = true;
2071 break;
bellardea2384d2004-08-01 21:59:26 +00002072 }
2073 }
ths3b46e622007-09-17 08:09:54 +00002074
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002075 if (!out_fmt && !tgt_image_opts) {
2076 out_fmt = "raw";
2077 }
2078
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002079 if (qemu_opts_foreach(&qemu_object_opts,
2080 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002081 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002082 goto fail_getopt;
2083 }
2084
Peter Lieven9fd77f92017-04-21 11:11:55 +02002085 if (!s.wr_in_order && s.compressed) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002086 error_report("Out of order write and compress are mutually exclusive");
Peter Lieven9fd77f92017-04-21 11:11:55 +02002087 goto fail_getopt;
2088 }
2089
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002090 if (tgt_image_opts && !skip_create) {
2091 error_report("--target-image-opts requires use of -n flag");
2092 goto fail_getopt;
2093 }
2094
Peter Lieven9fd77f92017-04-21 11:11:55 +02002095 s.src_num = argc - optind - 1;
2096 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2097
2098 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002099 if (out_fmt) {
2100 ret = print_block_option_help(out_filename, out_fmt);
2101 goto fail_getopt;
2102 } else {
2103 error_report("Option help requires a format be specified");
2104 goto fail_getopt;
2105 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002106 }
2107
2108 if (s.src_num < 1) {
2109 error_report("Must specify image file name");
2110 goto fail_getopt;
2111 }
2112
2113
Peter Lieven9fd77f92017-04-21 11:11:55 +02002114 /* ret is still -EINVAL until here */
2115 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2116 if (ret < 0) {
2117 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002118 goto fail_getopt;
2119 }
2120
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002121 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002122 if (quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002123 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002124 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002125 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002126 qemu_progress_print(0, 100);
2127
Peter Lieven9fd77f92017-04-21 11:11:55 +02002128 s.src = g_new0(BlockBackend *, s.src_num);
2129 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002130
Peter Lieven9fd77f92017-04-21 11:11:55 +02002131 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2132 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Fam Zheng335e9932017-05-03 00:35:39 +08002133 fmt, src_flags, src_writethrough, quiet,
2134 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002135 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002136 ret = -1;
2137 goto out;
2138 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002139 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2140 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002141 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002142 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002143 ret = -1;
2144 goto out;
2145 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002146 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002147 }
bellardea2384d2004-08-01 21:59:26 +00002148
Wenchao Xiaef806542013-12-04 17:10:57 +08002149 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002150 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002151 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2152 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2153 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002154 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002155 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002156 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002157 ret = -1;
2158 goto out;
2159 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002160
Peter Lieven9fd77f92017-04-21 11:11:55 +02002161 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2162 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002163 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002164 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002165 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002166 ret = -1;
2167 goto out;
edison51ef6722010-09-21 19:58:41 -07002168 }
2169
Max Reitz2e024cd2015-02-11 09:58:46 -05002170 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002171 /* Find driver and parse its options */
2172 drv = bdrv_find_format(out_fmt);
2173 if (!drv) {
2174 error_report("Unknown file format '%s'", out_fmt);
2175 ret = -1;
2176 goto out;
2177 }
2178
2179 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2180 if (!proto_drv) {
2181 error_report_err(local_err);
2182 ret = -1;
2183 goto out;
2184 }
2185
Max Reitz2e024cd2015-02-11 09:58:46 -05002186 if (!drv->create_opts) {
2187 error_report("Format driver '%s' does not support image creation",
2188 drv->format_name);
2189 ret = -1;
2190 goto out;
2191 }
Max Reitzf75613c2014-12-02 18:32:46 +01002192
Max Reitz2e024cd2015-02-11 09:58:46 -05002193 if (!proto_drv->create_opts) {
2194 error_report("Protocol driver '%s' does not support image creation",
2195 proto_drv->format_name);
2196 ret = -1;
2197 goto out;
2198 }
Max Reitzf75613c2014-12-02 18:32:46 +01002199
Max Reitz2e024cd2015-02-11 09:58:46 -05002200 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2201 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002202
Max Reitz2e024cd2015-02-11 09:58:46 -05002203 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002204 if (options) {
2205 qemu_opts_do_parse(opts, options, NULL, &local_err);
2206 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002207 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002208 ret = -1;
2209 goto out;
2210 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002211 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002212
Peter Lieven9fd77f92017-04-21 11:11:55 +02002213 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002214 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002215 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2216 if (ret < 0) {
2217 goto out;
2218 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002219 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002220
Kevin Wolfa18953f2010-10-14 15:46:04 +02002221 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002222 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002223 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002224 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002225 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002226 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002227
Max Reitz48758a82017-04-26 15:46:48 +02002228 if (s.src_num > 1 && out_baseimg) {
2229 error_report("Having a backing file for the target makes no sense when "
2230 "concatenating multiple input images");
2231 ret = -1;
2232 goto out;
2233 }
2234
Kevin Wolfefa84d42009-05-18 16:42:12 +02002235 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002236 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002237 bool encryption =
2238 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002239 const char *encryptfmt =
2240 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
Chunyan Liu83d05212014-06-05 17:20:51 +08002241 const char *preallocation =
2242 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002243
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002244 if (drv && !drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002245 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002246 ret = -1;
2247 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002248 }
2249
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002250 if (encryption || encryptfmt) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002251 error_report("Compression and encryption not supported at "
2252 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002253 ret = -1;
2254 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002255 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002256
Chunyan Liu83d05212014-06-05 17:20:51 +08002257 if (preallocation
2258 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002259 {
2260 error_report("Compression and preallocation not supported at "
2261 "the same time");
2262 ret = -1;
2263 goto out;
2264 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002265 }
2266
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002267 if (!skip_create) {
2268 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002269 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002270 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002271 error_reportf_err(local_err, "%s: error while converting %s: ",
2272 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002273 goto out;
bellardea2384d2004-08-01 21:59:26 +00002274 }
2275 }
ths3b46e622007-09-17 08:09:54 +00002276
Peter Lieven9fd77f92017-04-21 11:11:55 +02002277 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002278 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002279 if (ret < 0) {
2280 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002281 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002282 }
2283
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002284 if (skip_create) {
2285 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2286 flags, writethrough, quiet, false);
2287 } else {
2288 /* TODO ultimately we should allow --target-image-opts
2289 * to be used even when -n is not given.
2290 * That has to wait for bdrv_create to be improved
2291 * to allow filenames in option syntax
2292 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01002293 s.target = img_open_new_file(out_filename, opts, out_fmt,
2294 flags, writethrough, quiet, false);
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002295 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002296 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002297 ret = -1;
2298 goto out;
2299 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002300 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002301
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002302 if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2303 error_report("Compression not supported for this file format");
2304 ret = -1;
2305 goto out;
2306 }
2307
Eric Blake5def6b82016-06-23 16:37:19 -06002308 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002309 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2310 * as maximum. */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002311 s.buf_sectors = MIN(32768,
2312 MAX(s.buf_sectors,
2313 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2314 out_bs->bl.pdiscard_alignment >>
2315 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002316
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002317 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002318 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002319 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002320 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002321 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002322 ret = -1;
2323 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002324 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002325 error_report("output file is smaller than input file");
2326 ret = -1;
2327 goto out;
2328 }
2329 }
2330
Peter Lieven24f833c2013-11-27 11:07:07 +01002331 ret = bdrv_get_info(out_bs, &bdi);
2332 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002333 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002334 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002335 goto out;
2336 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002337 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002338 s.compressed = s.compressed || bdi.needs_compressed_writes;
2339 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Peter Lieven24f833c2013-11-27 11:07:07 +01002340 }
2341
Peter Lieven9fd77f92017-04-21 11:11:55 +02002342 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002343out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002344 if (!ret) {
2345 qemu_progress_print(100, 0);
2346 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002347 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002348 qemu_opts_del(opts);
2349 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002350 qemu_opts_del(sn_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002351 blk_unref(s.target);
2352 if (s.src) {
2353 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2354 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002355 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002356 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002357 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002358 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002359fail_getopt:
2360 g_free(options);
2361
Peter Lieven9fd77f92017-04-21 11:11:55 +02002362 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002363}
2364
bellard57d1a2b2004-08-03 21:15:11 +00002365
bellardfaea38e2006-08-05 21:31:00 +00002366static void dump_snapshots(BlockDriverState *bs)
2367{
2368 QEMUSnapshotInfo *sn_tab, *sn;
2369 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002370
2371 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2372 if (nb_sns <= 0)
2373 return;
2374 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002375 bdrv_snapshot_dump(fprintf, stdout, NULL);
2376 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002377 for(i = 0; i < nb_sns; i++) {
2378 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002379 bdrv_snapshot_dump(fprintf, stdout, sn);
2380 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002381 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002382 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002383}
2384
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002385static void dump_json_image_info_list(ImageInfoList *list)
2386{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002387 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002388 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002389 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002390
2391 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2392 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002393 str = qobject_to_json_pretty(obj);
2394 assert(str != NULL);
2395 printf("%s\n", qstring_get_str(str));
2396 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002397 visit_free(v);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002398 QDECREF(str);
2399}
2400
Benoît Canetc054b3f2012-09-05 13:09:02 +02002401static void dump_json_image_info(ImageInfo *info)
2402{
Benoît Canetc054b3f2012-09-05 13:09:02 +02002403 QString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002404 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002405 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002406
2407 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2408 visit_complete(v, &obj);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002409 str = qobject_to_json_pretty(obj);
2410 assert(str != NULL);
2411 printf("%s\n", qstring_get_str(str));
2412 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002413 visit_free(v);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002414 QDECREF(str);
2415}
2416
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002417static void dump_human_image_info_list(ImageInfoList *list)
2418{
2419 ImageInfoList *elem;
2420 bool delim = false;
2421
2422 for (elem = list; elem; elem = elem->next) {
2423 if (delim) {
2424 printf("\n");
2425 }
2426 delim = true;
2427
Wenchao Xia5b917042013-05-25 11:09:45 +08002428 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002429 }
2430}
2431
2432static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2433{
2434 return strcmp(a, b) == 0;
2435}
2436
2437/**
2438 * Open an image file chain and return an ImageInfoList
2439 *
2440 * @filename: topmost image filename
2441 * @fmt: topmost image format (may be NULL to autodetect)
2442 * @chain: true - enumerate entire backing file chain
2443 * false - only topmost image file
2444 *
2445 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2446 * image file. If there was an error a message will have been printed to
2447 * stderr.
2448 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002449static ImageInfoList *collect_image_info_list(bool image_opts,
2450 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002451 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002452 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002453{
2454 ImageInfoList *head = NULL;
2455 ImageInfoList **last = &head;
2456 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002457 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002458
2459 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2460
2461 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002462 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002463 BlockDriverState *bs;
2464 ImageInfo *info;
2465 ImageInfoList *elem;
2466
2467 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2468 error_report("Backing file '%s' creates an infinite loop.",
2469 filename);
2470 goto err;
2471 }
2472 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2473
Max Reitzefaa7c42016-03-16 19:54:38 +01002474 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002475 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2476 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002477 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002478 goto err;
2479 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002480 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002481
Wenchao Xia43526ec2013-06-06 12:27:58 +08002482 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002483 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002484 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002485 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002486 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002487 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002488
2489 elem = g_new0(ImageInfoList, 1);
2490 elem->value = info;
2491 *last = elem;
2492 last = &elem->next;
2493
Markus Armbruster26f54e92014-10-07 13:59:04 +02002494 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002495
2496 filename = fmt = NULL;
2497 if (chain) {
2498 if (info->has_full_backing_filename) {
2499 filename = info->full_backing_filename;
2500 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002501 error_report("Could not determine absolute backing filename,"
2502 " but backing filename '%s' present",
2503 info->backing_filename);
2504 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002505 }
2506 if (info->has_backing_filename_format) {
2507 fmt = info->backing_filename_format;
2508 }
2509 }
2510 }
2511 g_hash_table_destroy(filenames);
2512 return head;
2513
2514err:
2515 qapi_free_ImageInfoList(head);
2516 g_hash_table_destroy(filenames);
2517 return NULL;
2518}
2519
Benoît Canetc054b3f2012-09-05 13:09:02 +02002520static int img_info(int argc, char **argv)
2521{
2522 int c;
2523 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002524 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002525 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002526 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002527 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002528 bool force_share = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002529
bellardea2384d2004-08-01 21:59:26 +00002530 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002531 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002532 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002533 int option_index = 0;
2534 static const struct option long_options[] = {
2535 {"help", no_argument, 0, 'h'},
2536 {"format", required_argument, 0, 'f'},
2537 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002538 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002539 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002540 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002541 {"force-share", no_argument, 0, 'U'},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002542 {0, 0, 0, 0}
2543 };
Fam Zheng335e9932017-05-03 00:35:39 +08002544 c = getopt_long(argc, argv, ":f:hU",
Benoît Canetc054b3f2012-09-05 13:09:02 +02002545 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002546 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002547 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002548 }
bellardea2384d2004-08-01 21:59:26 +00002549 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002550 case ':':
2551 missing_argument(argv[optind - 1]);
2552 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002553 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002554 unrecognized_option(argv[optind - 1]);
2555 break;
bellardea2384d2004-08-01 21:59:26 +00002556 case 'h':
2557 help();
2558 break;
2559 case 'f':
2560 fmt = optarg;
2561 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002562 case 'U':
2563 force_share = true;
2564 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002565 case OPTION_OUTPUT:
2566 output = optarg;
2567 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002568 case OPTION_BACKING_CHAIN:
2569 chain = true;
2570 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002571 case OPTION_OBJECT: {
2572 QemuOpts *opts;
2573 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2574 optarg, true);
2575 if (!opts) {
2576 return 1;
2577 }
2578 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002579 case OPTION_IMAGE_OPTS:
2580 image_opts = true;
2581 break;
bellardea2384d2004-08-01 21:59:26 +00002582 }
2583 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002584 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002585 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002586 }
bellardea2384d2004-08-01 21:59:26 +00002587 filename = argv[optind++];
2588
Benoît Canetc054b3f2012-09-05 13:09:02 +02002589 if (output && !strcmp(output, "json")) {
2590 output_format = OFORMAT_JSON;
2591 } else if (output && !strcmp(output, "human")) {
2592 output_format = OFORMAT_HUMAN;
2593 } else if (output) {
2594 error_report("--output must be used with human or json as argument.");
2595 return 1;
2596 }
2597
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002598 if (qemu_opts_foreach(&qemu_object_opts,
2599 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002600 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002601 return 1;
2602 }
2603
Fam Zheng335e9932017-05-03 00:35:39 +08002604 list = collect_image_info_list(image_opts, filename, fmt, chain,
2605 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002606 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002607 return 1;
2608 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002609
Benoît Canetc054b3f2012-09-05 13:09:02 +02002610 switch (output_format) {
2611 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002612 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002613 break;
2614 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002615 if (chain) {
2616 dump_json_image_info_list(list);
2617 } else {
2618 dump_json_image_info(list->value);
2619 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002620 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002621 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002622
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002623 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002624 return 0;
2625}
2626
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002627static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2628 MapEntry *next)
2629{
2630 switch (output_format) {
2631 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002632 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002633 error_report("File contains external, encrypted or compressed clusters.");
2634 exit(1);
2635 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002636 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002637 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002638 e->start, e->length,
2639 e->has_offset ? e->offset : 0,
2640 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002641 }
2642 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2643 * Modify the flags here to allow more coalescing.
2644 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002645 if (next && (!next->data || next->zero)) {
2646 next->data = false;
2647 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002648 }
2649 break;
2650 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002651 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2652 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002653 (e->start == 0 ? "[" : ",\n"),
2654 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002655 e->zero ? "true" : "false",
2656 e->data ? "true" : "false");
2657 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002658 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002659 }
2660 putchar('}');
2661
2662 if (!next) {
2663 printf("]\n");
2664 }
2665 break;
2666 }
2667}
2668
2669static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2670 int nb_sectors, MapEntry *e)
2671{
2672 int64_t ret;
2673 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002674 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002675 bool has_offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002676
2677 /* As an optimization, we could cache the current range of unallocated
2678 * clusters in each file of the chain, and avoid querying the same
2679 * range repeatedly.
2680 */
2681
2682 depth = 0;
2683 for (;;) {
Fam Zheng67a0fd22016-01-26 11:58:48 +08002684 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2685 &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002686 if (ret < 0) {
2687 return ret;
2688 }
2689 assert(nb_sectors);
2690 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2691 break;
2692 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002693 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002694 if (bs == NULL) {
2695 ret = 0;
2696 break;
2697 }
2698
2699 depth++;
2700 }
2701
John Snow28756452016-02-05 13:12:33 -05002702 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2703
2704 *e = (MapEntry) {
2705 .start = sector_num * BDRV_SECTOR_SIZE,
2706 .length = nb_sectors * BDRV_SECTOR_SIZE,
2707 .data = !!(ret & BDRV_BLOCK_DATA),
2708 .zero = !!(ret & BDRV_BLOCK_ZERO),
2709 .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2710 .has_offset = has_offset,
2711 .depth = depth,
2712 .has_filename = file && has_offset,
2713 .filename = file && has_offset ? file->filename : NULL,
2714 };
2715
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002716 return 0;
2717}
2718
Fam Zheng16b0d552016-01-26 11:59:02 +08002719static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2720{
2721 if (curr->length == 0) {
2722 return false;
2723 }
2724 if (curr->zero != next->zero ||
2725 curr->data != next->data ||
2726 curr->depth != next->depth ||
2727 curr->has_filename != next->has_filename ||
2728 curr->has_offset != next->has_offset) {
2729 return false;
2730 }
2731 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2732 return false;
2733 }
2734 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2735 return false;
2736 }
2737 return true;
2738}
2739
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002740static int img_map(int argc, char **argv)
2741{
2742 int c;
2743 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002744 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002745 BlockDriverState *bs;
2746 const char *filename, *fmt, *output;
2747 int64_t length;
2748 MapEntry curr = { .length = 0 }, next;
2749 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002750 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002751 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002752
2753 fmt = NULL;
2754 output = NULL;
2755 for (;;) {
2756 int option_index = 0;
2757 static const struct option long_options[] = {
2758 {"help", no_argument, 0, 'h'},
2759 {"format", required_argument, 0, 'f'},
2760 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002761 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002762 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002763 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002764 {0, 0, 0, 0}
2765 };
Fam Zheng335e9932017-05-03 00:35:39 +08002766 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002767 long_options, &option_index);
2768 if (c == -1) {
2769 break;
2770 }
2771 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002772 case ':':
2773 missing_argument(argv[optind - 1]);
2774 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002775 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002776 unrecognized_option(argv[optind - 1]);
2777 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002778 case 'h':
2779 help();
2780 break;
2781 case 'f':
2782 fmt = optarg;
2783 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002784 case 'U':
2785 force_share = true;
2786 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002787 case OPTION_OUTPUT:
2788 output = optarg;
2789 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002790 case OPTION_OBJECT: {
2791 QemuOpts *opts;
2792 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2793 optarg, true);
2794 if (!opts) {
2795 return 1;
2796 }
2797 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002798 case OPTION_IMAGE_OPTS:
2799 image_opts = true;
2800 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002801 }
2802 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002803 if (optind != argc - 1) {
2804 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002805 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002806 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002807
2808 if (output && !strcmp(output, "json")) {
2809 output_format = OFORMAT_JSON;
2810 } else if (output && !strcmp(output, "human")) {
2811 output_format = OFORMAT_HUMAN;
2812 } else if (output) {
2813 error_report("--output must be used with human or json as argument.");
2814 return 1;
2815 }
2816
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002817 if (qemu_opts_foreach(&qemu_object_opts,
2818 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002819 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002820 return 1;
2821 }
2822
Fam Zheng335e9932017-05-03 00:35:39 +08002823 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002824 if (!blk) {
2825 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002826 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002827 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002828
2829 if (output_format == OFORMAT_HUMAN) {
2830 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2831 }
2832
Max Reitzf1d3cd72015-02-05 13:58:18 -05002833 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002834 while (curr.start + curr.length < length) {
2835 int64_t nsectors_left;
2836 int64_t sector_num;
2837 int n;
2838
2839 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2840
2841 /* Probe up to 1 GiB at a time. */
2842 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2843 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2844 ret = get_block_status(bs, sector_num, n, &next);
2845
2846 if (ret < 0) {
2847 error_report("Could not read file metadata: %s", strerror(-ret));
2848 goto out;
2849 }
2850
Fam Zheng16b0d552016-01-26 11:59:02 +08002851 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002852 curr.length += next.length;
2853 continue;
2854 }
2855
2856 if (curr.length > 0) {
2857 dump_map_entry(output_format, &curr, &next);
2858 }
2859 curr = next;
2860 }
2861
2862 dump_map_entry(output_format, &curr, NULL);
2863
2864out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002865 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002866 return ret < 0;
2867}
2868
aliguorif7b4a942009-01-07 17:40:15 +00002869#define SNAPSHOT_LIST 1
2870#define SNAPSHOT_CREATE 2
2871#define SNAPSHOT_APPLY 3
2872#define SNAPSHOT_DELETE 4
2873
Stuart Brady153859b2009-06-07 00:42:17 +01002874static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002875{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002876 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002877 BlockDriverState *bs;
2878 QEMUSnapshotInfo sn;
2879 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002880 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002881 int action = 0;
2882 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002883 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002884 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002885 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002886 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00002887
Kevin Wolfce099542016-03-15 13:01:04 +01002888 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002889 /* Parse commandline parameters */
2890 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002891 static const struct option long_options[] = {
2892 {"help", no_argument, 0, 'h'},
2893 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002894 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002895 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002896 {0, 0, 0, 0}
2897 };
Fam Zheng335e9932017-05-03 00:35:39 +08002898 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002899 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002900 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002901 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002902 }
aliguorif7b4a942009-01-07 17:40:15 +00002903 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002904 case ':':
2905 missing_argument(argv[optind - 1]);
2906 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002907 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002908 unrecognized_option(argv[optind - 1]);
2909 break;
aliguorif7b4a942009-01-07 17:40:15 +00002910 case 'h':
2911 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002912 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002913 case 'l':
2914 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002915 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002916 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002917 }
2918 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002919 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002920 break;
2921 case 'a':
2922 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002923 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002924 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002925 }
2926 action = SNAPSHOT_APPLY;
2927 snapshot_name = optarg;
2928 break;
2929 case 'c':
2930 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002931 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002932 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002933 }
2934 action = SNAPSHOT_CREATE;
2935 snapshot_name = optarg;
2936 break;
2937 case 'd':
2938 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002939 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002940 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002941 }
2942 action = SNAPSHOT_DELETE;
2943 snapshot_name = optarg;
2944 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002945 case 'q':
2946 quiet = true;
2947 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002948 case 'U':
2949 force_share = true;
2950 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002951 case OPTION_OBJECT: {
2952 QemuOpts *opts;
2953 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2954 optarg, true);
2955 if (!opts) {
2956 return 1;
2957 }
2958 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002959 case OPTION_IMAGE_OPTS:
2960 image_opts = true;
2961 break;
aliguorif7b4a942009-01-07 17:40:15 +00002962 }
2963 }
2964
Kevin Wolffc11eb22013-08-05 10:53:04 +02002965 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002966 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002967 }
aliguorif7b4a942009-01-07 17:40:15 +00002968 filename = argv[optind++];
2969
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002970 if (qemu_opts_foreach(&qemu_object_opts,
2971 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002972 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002973 return 1;
2974 }
2975
aliguorif7b4a942009-01-07 17:40:15 +00002976 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08002977 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
2978 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002979 if (!blk) {
2980 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002981 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002982 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002983
2984 /* Perform the requested action */
2985 switch(action) {
2986 case SNAPSHOT_LIST:
2987 dump_snapshots(bs);
2988 break;
2989
2990 case SNAPSHOT_CREATE:
2991 memset(&sn, 0, sizeof(sn));
2992 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2993
2994 qemu_gettimeofday(&tv);
2995 sn.date_sec = tv.tv_sec;
2996 sn.date_nsec = tv.tv_usec * 1000;
2997
2998 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002999 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003000 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003001 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003002 }
aliguorif7b4a942009-01-07 17:40:15 +00003003 break;
3004
3005 case SNAPSHOT_APPLY:
3006 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003007 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003008 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003009 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003010 }
aliguorif7b4a942009-01-07 17:40:15 +00003011 break;
3012
3013 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003014 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003015 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003016 error_reportf_err(err, "Could not delete snapshot '%s': ",
3017 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003018 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003019 }
aliguorif7b4a942009-01-07 17:40:15 +00003020 break;
3021 }
3022
3023 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003024 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003025 if (ret) {
3026 return 1;
3027 }
Stuart Brady153859b2009-06-07 00:42:17 +01003028 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003029}
3030
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003031static int img_rebase(int argc, char **argv)
3032{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003033 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003034 uint8_t *buf_old = NULL;
3035 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05003036 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003037 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003038 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3039 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003040 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003041 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003042 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003043 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003044 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003045 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003046 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003047
3048 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003049 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003050 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003051 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003052 out_baseimg = NULL;
3053 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003054 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003055 static const struct option long_options[] = {
3056 {"help", no_argument, 0, 'h'},
3057 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003058 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003059 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003060 {0, 0, 0, 0}
3061 };
Fam Zheng335e9932017-05-03 00:35:39 +08003062 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003063 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003064 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003065 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003066 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003067 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003068 case ':':
3069 missing_argument(argv[optind - 1]);
3070 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003071 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003072 unrecognized_option(argv[optind - 1]);
3073 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003074 case 'h':
3075 help();
3076 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003077 case 'f':
3078 fmt = optarg;
3079 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003080 case 'F':
3081 out_basefmt = optarg;
3082 break;
3083 case 'b':
3084 out_baseimg = optarg;
3085 break;
3086 case 'u':
3087 unsafe = 1;
3088 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003089 case 'p':
3090 progress = 1;
3091 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003092 case 't':
3093 cache = optarg;
3094 break;
Max Reitz40055952014-07-22 22:58:42 +02003095 case 'T':
3096 src_cache = optarg;
3097 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003098 case 'q':
3099 quiet = true;
3100 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003101 case OPTION_OBJECT: {
3102 QemuOpts *opts;
3103 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3104 optarg, true);
3105 if (!opts) {
3106 return 1;
3107 }
3108 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003109 case OPTION_IMAGE_OPTS:
3110 image_opts = true;
3111 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003112 case 'U':
3113 force_share = true;
3114 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003115 }
3116 }
3117
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003118 if (quiet) {
3119 progress = 0;
3120 }
3121
Fam Zhengac1307a2014-04-22 13:36:11 +08003122 if (optind != argc - 1) {
3123 error_exit("Expecting one image file name");
3124 }
3125 if (!unsafe && !out_baseimg) {
3126 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003127 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003128 filename = argv[optind++];
3129
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003130 if (qemu_opts_foreach(&qemu_object_opts,
3131 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003132 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003133 return 1;
3134 }
3135
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003136 qemu_progress_init(progress, 2.0);
3137 qemu_progress_print(0, 100);
3138
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003139 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003140 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003141 if (ret < 0) {
3142 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003143 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003144 }
3145
Kevin Wolfce099542016-03-15 13:01:04 +01003146 src_flags = 0;
3147 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003148 if (ret < 0) {
3149 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003150 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003151 }
3152
Kevin Wolfce099542016-03-15 13:01:04 +01003153 /* The source files are opened read-only, don't care about WCE */
3154 assert((src_flags & BDRV_O_RDWR) == 0);
3155 (void) src_writethrough;
3156
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003157 /*
3158 * Open the images.
3159 *
3160 * Ignore the old backing file for unsafe rebase in case we want to correct
3161 * the reference to a renamed or moved backing file.
3162 */
Fam Zheng335e9932017-05-03 00:35:39 +08003163 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3164 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003165 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003166 ret = -1;
3167 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003168 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003169 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003170
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003171 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003172 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003173 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003174 ret = -1;
3175 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003176 }
3177 }
3178
3179 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003180 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003181 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003182 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003183
Max Reitz644483d2015-02-05 13:58:17 -05003184 if (bs->backing_format[0] != '\0') {
3185 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003186 qdict_put_str(options, "driver", bs->backing_format);
Max Reitz644483d2015-02-05 13:58:17 -05003187 }
3188
Fam Zheng335e9932017-05-03 00:35:39 +08003189 if (force_share) {
3190 if (!options) {
3191 options = qdict_new();
3192 }
Eric Blake579cf1d2017-05-15 14:54:39 -05003193 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +08003194 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003195 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003196 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003197 options, src_flags, &local_err);
3198 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003199 error_reportf_err(local_err,
3200 "Could not open old backing file '%s': ",
3201 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003202 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003203 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003204 }
Max Reitz644483d2015-02-05 13:58:17 -05003205
Alex Bligha6166732012-10-16 13:46:18 +01003206 if (out_baseimg[0]) {
Fam Zheng335e9932017-05-03 00:35:39 +08003207 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003208 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003209 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003210 }
3211 if (force_share) {
3212 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003213 }
3214
Max Reitzefaa7c42016-03-16 19:54:38 +01003215 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003216 options, src_flags, &local_err);
3217 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003218 error_reportf_err(local_err,
3219 "Could not open new backing file '%s': ",
3220 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003221 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003222 goto out;
3223 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003224 }
3225 }
3226
3227 /*
3228 * Check each unallocated cluster in the COW file. If it is unallocated,
3229 * accesses go to the backing file. We must therefore compare this cluster
3230 * in the old and new backing file, and if they differ we need to copy it
3231 * from the old backing file into the COW file.
3232 *
3233 * If qemu-img crashes during this step, no harm is done. The content of
3234 * the image is the same as the original one at any time.
3235 */
3236 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003237 int64_t num_sectors;
3238 int64_t old_backing_num_sectors;
3239 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003240 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003241 int n;
Eric Blaked6a644b2017-07-07 07:44:57 -05003242 int64_t count;
Kevin Wolf1f710492012-10-12 14:29:18 +02003243 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003244
Max Reitzf1d3cd72015-02-05 13:58:18 -05003245 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3246 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003247
Max Reitzf1d3cd72015-02-05 13:58:18 -05003248 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003249 if (num_sectors < 0) {
3250 error_report("Could not get size of '%s': %s",
3251 filename, strerror(-num_sectors));
3252 ret = -1;
3253 goto out;
3254 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003255 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003256 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003257 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003258
3259 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3260 error_report("Could not get size of '%s': %s",
3261 backing_name, strerror(-old_backing_num_sectors));
3262 ret = -1;
3263 goto out;
3264 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003265 if (blk_new_backing) {
3266 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003267 if (new_backing_num_sectors < 0) {
3268 error_report("Could not get size of '%s': %s",
3269 out_baseimg, strerror(-new_backing_num_sectors));
3270 ret = -1;
3271 goto out;
3272 }
Alex Bligha6166732012-10-16 13:46:18 +01003273 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003274
Kevin Wolf1f710492012-10-12 14:29:18 +02003275 if (num_sectors != 0) {
3276 local_progress = (float)100 /
3277 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
3278 }
3279
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003280 for (sector = 0; sector < num_sectors; sector += n) {
3281
3282 /* How many sectors can we handle with the next read? */
3283 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
3284 n = (IO_BUF_SIZE / 512);
3285 } else {
3286 n = num_sectors - sector;
3287 }
3288
3289 /* If the cluster is allocated, we don't need to take action */
Eric Blaked6a644b2017-07-07 07:44:57 -05003290 ret = bdrv_is_allocated(bs, sector << BDRV_SECTOR_BITS,
3291 n << BDRV_SECTOR_BITS, &count);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003292 if (ret < 0) {
3293 error_report("error while reading image metadata: %s",
3294 strerror(-ret));
3295 goto out;
3296 }
Eric Blaked6a644b2017-07-07 07:44:57 -05003297 /* TODO relax this once bdrv_is_allocated does not enforce
3298 * sector alignment */
3299 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
3300 n = count >> BDRV_SECTOR_BITS;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003301 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003302 continue;
3303 }
3304
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003305 /*
3306 * Read old and new backing file and take into consideration that
3307 * backing files may be smaller than the COW image.
3308 */
3309 if (sector >= old_backing_num_sectors) {
3310 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3311 } else {
3312 if (sector + n > old_backing_num_sectors) {
3313 n = old_backing_num_sectors - sector;
3314 }
3315
Eric Blake91669202016-05-06 10:26:43 -06003316 ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS,
3317 buf_old, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003318 if (ret < 0) {
3319 error_report("error while reading from old backing file");
3320 goto out;
3321 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003322 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003323
Max Reitzf1d3cd72015-02-05 13:58:18 -05003324 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003325 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3326 } else {
3327 if (sector + n > new_backing_num_sectors) {
3328 n = new_backing_num_sectors - sector;
3329 }
3330
Eric Blake91669202016-05-06 10:26:43 -06003331 ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS,
3332 buf_new, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003333 if (ret < 0) {
3334 error_report("error while reading from new backing file");
3335 goto out;
3336 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003337 }
3338
3339 /* If they differ, we need to write to the COW file */
3340 uint64_t written = 0;
3341
3342 while (written < n) {
3343 int pnum;
3344
3345 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003346 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003347 {
Eric Blake91669202016-05-06 10:26:43 -06003348 ret = blk_pwrite(blk,
3349 (sector + written) << BDRV_SECTOR_BITS,
3350 buf_old + written * 512,
3351 pnum << BDRV_SECTOR_BITS, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003352 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003353 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003354 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003355 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003356 }
3357 }
3358
3359 written += pnum;
3360 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003361 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003362 }
3363 }
3364
3365 /*
3366 * Change the backing file. All clusters that are different from the old
3367 * backing file are overwritten in the COW file now, so the visible content
3368 * doesn't change when we switch the backing file.
3369 */
Alex Bligha6166732012-10-16 13:46:18 +01003370 if (out_baseimg && *out_baseimg) {
3371 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3372 } else {
3373 ret = bdrv_change_backing_file(bs, NULL, NULL);
3374 }
3375
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003376 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003377 error_report("Could not change the backing file to '%s': No "
3378 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003379 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003380 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003381 out_baseimg, strerror(-ret));
3382 }
3383
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003384 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003385 /*
3386 * TODO At this point it is possible to check if any clusters that are
3387 * allocated in the COW file are the same in the backing file. If so, they
3388 * could be dropped from the COW file. Don't do this before switching the
3389 * backing file, in case of a crash this would lead to corruption.
3390 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003391out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003392 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003393 /* Cleanup */
3394 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003395 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003396 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003397 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003398 qemu_vfree(buf_old);
3399 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003400
Markus Armbruster26f54e92014-10-07 13:59:04 +02003401 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003402 if (ret) {
3403 return 1;
3404 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003405 return 0;
3406}
3407
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003408static int img_resize(int argc, char **argv)
3409{
Markus Armbruster6750e792015-02-12 17:43:08 +01003410 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003411 int c, ret, relative;
3412 const char *filename, *fmt, *size;
Max Reitzdc5f6902017-06-13 22:20:55 +02003413 int64_t n, total_size, current_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003414 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003415 BlockBackend *blk = NULL;
Max Reitzdc5f6902017-06-13 22:20:55 +02003416 PreallocMode prealloc = PREALLOC_MODE_OFF;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003417 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003418
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003419 static QemuOptsList resize_options = {
3420 .name = "resize_options",
3421 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3422 .desc = {
3423 {
3424 .name = BLOCK_OPT_SIZE,
3425 .type = QEMU_OPT_SIZE,
3426 .help = "Virtual disk size"
3427 }, {
3428 /* end of list */
3429 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003430 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003431 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003432 bool image_opts = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003433
Kevin Wolfe80fec72011-04-29 10:58:12 +02003434 /* Remove size from argv manually so that negative numbers are not treated
3435 * as options by getopt. */
3436 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003437 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003438 return 1;
3439 }
3440
3441 size = argv[--argc];
3442
3443 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003444 fmt = NULL;
3445 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003446 static const struct option long_options[] = {
3447 {"help", no_argument, 0, 'h'},
3448 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003449 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Max Reitzdc5f6902017-06-13 22:20:55 +02003450 {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003451 {0, 0, 0, 0}
3452 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003453 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003454 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003455 if (c == -1) {
3456 break;
3457 }
3458 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003459 case ':':
3460 missing_argument(argv[optind - 1]);
3461 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003462 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003463 unrecognized_option(argv[optind - 1]);
3464 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003465 case 'h':
3466 help();
3467 break;
3468 case 'f':
3469 fmt = optarg;
3470 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003471 case 'q':
3472 quiet = true;
3473 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003474 case OPTION_OBJECT: {
3475 QemuOpts *opts;
3476 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3477 optarg, true);
3478 if (!opts) {
3479 return 1;
3480 }
3481 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003482 case OPTION_IMAGE_OPTS:
3483 image_opts = true;
3484 break;
Max Reitzdc5f6902017-06-13 22:20:55 +02003485 case OPTION_PREALLOCATION:
3486 prealloc = qapi_enum_parse(PreallocMode_lookup, optarg,
3487 PREALLOC_MODE__MAX, PREALLOC_MODE__MAX,
3488 NULL);
3489 if (prealloc == PREALLOC_MODE__MAX) {
3490 error_report("Invalid preallocation mode '%s'", optarg);
3491 return 1;
3492 }
3493 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003494 }
3495 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003496 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003497 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003498 }
3499 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003500
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003501 if (qemu_opts_foreach(&qemu_object_opts,
3502 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003503 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003504 return 1;
3505 }
3506
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003507 /* Choose grow, shrink, or absolute resize mode */
3508 switch (size[0]) {
3509 case '+':
3510 relative = 1;
3511 size++;
3512 break;
3513 case '-':
3514 relative = -1;
3515 size++;
3516 break;
3517 default:
3518 relative = 0;
3519 break;
3520 }
3521
3522 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003523 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003524 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003525 if (err) {
3526 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003527 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003528 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003529 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003530 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003531 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3532 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003533
Max Reitzefaa7c42016-03-16 19:54:38 +01003534 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003535 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3536 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003537 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003538 ret = -1;
3539 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003540 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003541
Max Reitzdc5f6902017-06-13 22:20:55 +02003542 current_size = blk_getlength(blk);
3543 if (current_size < 0) {
3544 error_report("Failed to inquire current image length: %s",
3545 strerror(-current_size));
3546 ret = -1;
3547 goto out;
3548 }
3549
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003550 if (relative) {
Max Reitzdc5f6902017-06-13 22:20:55 +02003551 total_size = current_size + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003552 } else {
3553 total_size = n;
3554 }
3555 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003556 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003557 ret = -1;
3558 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003559 }
3560
Max Reitzdc5f6902017-06-13 22:20:55 +02003561 if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
3562 error_report("Preallocation can only be used for growing images");
3563 ret = -1;
3564 goto out;
3565 }
3566
3567 ret = blk_truncate(blk, total_size, prealloc, &err);
Max Reitzed3d2ec2017-03-28 22:51:27 +02003568 if (!ret) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003569 qprintf(quiet, "Image resized.\n");
Max Reitzed3d2ec2017-03-28 22:51:27 +02003570 } else {
3571 error_report_err(err);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003572 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003573out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003574 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003575 if (ret) {
3576 return 1;
3577 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003578 return 0;
3579}
3580
Max Reitz76a3a342014-10-27 11:12:51 +01003581static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003582 int64_t offset, int64_t total_work_size,
3583 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003584{
3585 qemu_progress_print(100.f * offset / total_work_size, 0);
3586}
3587
Max Reitz6f176b42013-09-03 10:09:50 +02003588static int img_amend(int argc, char **argv)
3589{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003590 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003591 int c, ret = 0;
3592 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003593 QemuOptsList *create_opts = NULL;
3594 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003595 const char *fmt = NULL, *filename, *cache;
3596 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003597 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003598 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003599 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003600 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003601 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003602
Max Reitzbd39e6e2014-07-22 22:58:43 +02003603 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003604 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003605 static const struct option long_options[] = {
3606 {"help", no_argument, 0, 'h'},
3607 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003608 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003609 {0, 0, 0, 0}
3610 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003611 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003612 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003613 if (c == -1) {
3614 break;
3615 }
3616
3617 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003618 case ':':
3619 missing_argument(argv[optind - 1]);
3620 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003621 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003622 unrecognized_option(argv[optind - 1]);
3623 break;
3624 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003625 help();
3626 break;
3627 case 'o':
3628 if (!is_valid_option_list(optarg)) {
3629 error_report("Invalid option list: %s", optarg);
3630 ret = -1;
3631 goto out_no_progress;
3632 }
3633 if (!options) {
3634 options = g_strdup(optarg);
3635 } else {
3636 char *old_options = options;
3637 options = g_strdup_printf("%s,%s", options, optarg);
3638 g_free(old_options);
3639 }
3640 break;
3641 case 'f':
3642 fmt = optarg;
3643 break;
3644 case 't':
3645 cache = optarg;
3646 break;
3647 case 'p':
3648 progress = true;
3649 break;
3650 case 'q':
3651 quiet = true;
3652 break;
3653 case OPTION_OBJECT:
3654 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3655 optarg, true);
3656 if (!opts) {
3657 ret = -1;
3658 goto out_no_progress;
3659 }
3660 break;
3661 case OPTION_IMAGE_OPTS:
3662 image_opts = true;
3663 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003664 }
3665 }
3666
Max Reitz6f176b42013-09-03 10:09:50 +02003667 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003668 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003669 }
3670
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003671 if (qemu_opts_foreach(&qemu_object_opts,
3672 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003673 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003674 ret = -1;
3675 goto out_no_progress;
3676 }
3677
Max Reitz76a3a342014-10-27 11:12:51 +01003678 if (quiet) {
3679 progress = false;
3680 }
3681 qemu_progress_init(progress, 1.0);
3682
Kevin Wolfa283cb62014-02-21 16:24:07 +01003683 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3684 if (fmt && has_help_option(options)) {
3685 /* If a format is explicitly specified (and possibly no filename is
3686 * given), print option help here */
3687 ret = print_block_option_help(filename, fmt);
3688 goto out;
3689 }
3690
3691 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003692 error_report("Expecting one image file name");
3693 ret = -1;
3694 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003695 }
Max Reitz6f176b42013-09-03 10:09:50 +02003696
Kevin Wolfce099542016-03-15 13:01:04 +01003697 flags = BDRV_O_RDWR;
3698 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003699 if (ret < 0) {
3700 error_report("Invalid cache option: %s", cache);
3701 goto out;
3702 }
3703
Fam Zheng335e9932017-05-03 00:35:39 +08003704 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3705 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003706 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003707 ret = -1;
3708 goto out;
3709 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003710 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003711
3712 fmt = bs->drv->format_name;
3713
Kevin Wolf626f84f2014-02-21 16:24:06 +01003714 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003715 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003716 ret = print_block_option_help(filename, fmt);
3717 goto out;
3718 }
3719
Max Reitzb2439d22014-12-02 18:32:47 +01003720 if (!bs->drv->create_opts) {
3721 error_report("Format driver '%s' does not support any options to amend",
3722 fmt);
3723 ret = -1;
3724 goto out;
3725 }
3726
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003727 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003728 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003729 qemu_opts_do_parse(opts, options, NULL, &err);
3730 if (err) {
3731 error_report_err(err);
3732 ret = -1;
3733 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003734 }
3735
Max Reitz76a3a342014-10-27 11:12:51 +01003736 /* In case the driver does not call amend_status_cb() */
3737 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003738 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003739 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003740 if (ret < 0) {
3741 error_report("Error while amending options: %s", strerror(-ret));
3742 goto out;
3743 }
3744
3745out:
Max Reitz76a3a342014-10-27 11:12:51 +01003746 qemu_progress_end();
3747
Max Reitze814dff2015-08-20 16:00:38 -07003748out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003749 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003750 qemu_opts_del(opts);
3751 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003752 g_free(options);
3753
Max Reitz6f176b42013-09-03 10:09:50 +02003754 if (ret) {
3755 return 1;
3756 }
3757 return 0;
3758}
3759
Kevin Wolfb6133b82014-08-05 14:17:13 +02003760typedef struct BenchData {
3761 BlockBackend *blk;
3762 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003763 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003764 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003765 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003766 int nrreq;
3767 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003768 int flush_interval;
3769 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003770 uint8_t *buf;
3771 QEMUIOVector *qiov;
3772
3773 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003774 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003775 uint64_t offset;
3776} BenchData;
3777
Kevin Wolf55d539c2016-06-03 13:59:41 +02003778static void bench_undrained_flush_cb(void *opaque, int ret)
3779{
3780 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003781 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003782 exit(EXIT_FAILURE);
3783 }
3784}
3785
Kevin Wolfb6133b82014-08-05 14:17:13 +02003786static void bench_cb(void *opaque, int ret)
3787{
3788 BenchData *b = opaque;
3789 BlockAIOCB *acb;
3790
3791 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003792 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003793 exit(EXIT_FAILURE);
3794 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003795
3796 if (b->in_flush) {
3797 /* Just finished a flush with drained queue: Start next requests */
3798 assert(b->in_flight == 0);
3799 b->in_flush = false;
3800 } else if (b->in_flight > 0) {
3801 int remaining = b->n - b->in_flight;
3802
Kevin Wolfb6133b82014-08-05 14:17:13 +02003803 b->n--;
3804 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003805
3806 /* Time for flush? Drain queue if requested, then flush */
3807 if (b->flush_interval && remaining % b->flush_interval == 0) {
3808 if (!b->in_flight || !b->drain_on_flush) {
3809 BlockCompletionFunc *cb;
3810
3811 if (b->drain_on_flush) {
3812 b->in_flush = true;
3813 cb = bench_cb;
3814 } else {
3815 cb = bench_undrained_flush_cb;
3816 }
3817
3818 acb = blk_aio_flush(b->blk, cb, b);
3819 if (!acb) {
3820 error_report("Failed to issue flush request");
3821 exit(EXIT_FAILURE);
3822 }
3823 }
3824 if (b->drain_on_flush) {
3825 return;
3826 }
3827 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003828 }
3829
3830 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003831 int64_t offset = b->offset;
3832 /* blk_aio_* might look for completed I/Os and kick bench_cb
3833 * again, so make sure this operation is counted by in_flight
3834 * and b->offset is ready for the next submission.
3835 */
3836 b->in_flight++;
3837 b->offset += b->step;
3838 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003839 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003840 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003841 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003842 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003843 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003844 if (!acb) {
3845 error_report("Failed to issue request");
3846 exit(EXIT_FAILURE);
3847 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003848 }
3849}
3850
3851static int img_bench(int argc, char **argv)
3852{
3853 int c, ret = 0;
3854 const char *fmt = NULL, *filename;
3855 bool quiet = false;
3856 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003857 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003858 int count = 75000;
3859 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003860 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003861 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003862 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003863 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003864 int flush_interval = 0;
3865 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003866 int64_t image_size;
3867 BlockBackend *blk = NULL;
3868 BenchData data = {};
3869 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003870 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003871 struct timeval t1, t2;
3872 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08003873 bool force_share = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003874
3875 for (;;) {
3876 static const struct option long_options[] = {
3877 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003878 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003879 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003880 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003881 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08003882 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003883 {0, 0, 0, 0}
3884 };
Fam Zheng335e9932017-05-03 00:35:39 +08003885 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003886 if (c == -1) {
3887 break;
3888 }
3889
3890 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003891 case ':':
3892 missing_argument(argv[optind - 1]);
3893 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003894 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003895 unrecognized_option(argv[optind - 1]);
3896 break;
3897 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02003898 help();
3899 break;
3900 case 'c':
3901 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003902 unsigned long res;
3903
3904 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003905 error_report("Invalid request count specified");
3906 return 1;
3907 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003908 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003909 break;
3910 }
3911 case 'd':
3912 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003913 unsigned long res;
3914
3915 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003916 error_report("Invalid queue depth specified");
3917 return 1;
3918 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003919 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003920 break;
3921 }
3922 case 'f':
3923 fmt = optarg;
3924 break;
3925 case 'n':
3926 flags |= BDRV_O_NATIVE_AIO;
3927 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003928 case 'o':
3929 {
Markus Armbruster606caa02017-02-21 21:14:04 +01003930 offset = cvtnum(optarg);
3931 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02003932 error_report("Invalid offset specified");
3933 return 1;
3934 }
3935 break;
3936 }
3937 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003938 case 'q':
3939 quiet = true;
3940 break;
3941 case 's':
3942 {
3943 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003944
Markus Armbruster606caa02017-02-21 21:14:04 +01003945 sval = cvtnum(optarg);
3946 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003947 error_report("Invalid buffer size specified");
3948 return 1;
3949 }
3950
3951 bufsize = sval;
3952 break;
3953 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003954 case 'S':
3955 {
3956 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003957
Markus Armbruster606caa02017-02-21 21:14:04 +01003958 sval = cvtnum(optarg);
3959 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02003960 error_report("Invalid step size specified");
3961 return 1;
3962 }
3963
3964 step = sval;
3965 break;
3966 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003967 case 't':
3968 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3969 if (ret < 0) {
3970 error_report("Invalid cache mode");
3971 ret = -1;
3972 goto out;
3973 }
3974 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003975 case 'w':
3976 flags |= BDRV_O_RDWR;
3977 is_write = true;
3978 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003979 case 'U':
3980 force_share = true;
3981 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003982 case OPTION_PATTERN:
3983 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003984 unsigned long res;
3985
3986 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003987 error_report("Invalid pattern byte specified");
3988 return 1;
3989 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003990 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003991 break;
3992 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003993 case OPTION_FLUSH_INTERVAL:
3994 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003995 unsigned long res;
3996
3997 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02003998 error_report("Invalid flush interval specified");
3999 return 1;
4000 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004001 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004002 break;
4003 }
4004 case OPTION_NO_DRAIN:
4005 drain_on_flush = false;
4006 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004007 case OPTION_IMAGE_OPTS:
4008 image_opts = true;
4009 break;
4010 }
4011 }
4012
4013 if (optind != argc - 1) {
4014 error_exit("Expecting one image file name");
4015 }
4016 filename = argv[argc - 1];
4017
Kevin Wolf55d539c2016-06-03 13:59:41 +02004018 if (!is_write && flush_interval) {
4019 error_report("--flush-interval is only available in write tests");
4020 ret = -1;
4021 goto out;
4022 }
4023 if (flush_interval && flush_interval < depth) {
4024 error_report("Flush interval can't be smaller than depth");
4025 ret = -1;
4026 goto out;
4027 }
4028
Fam Zheng335e9932017-05-03 00:35:39 +08004029 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4030 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004031 if (!blk) {
4032 ret = -1;
4033 goto out;
4034 }
4035
4036 image_size = blk_getlength(blk);
4037 if (image_size < 0) {
4038 ret = image_size;
4039 goto out;
4040 }
4041
4042 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004043 .blk = blk,
4044 .image_size = image_size,
4045 .bufsize = bufsize,
4046 .step = step ?: bufsize,
4047 .nrreq = depth,
4048 .n = count,
4049 .offset = offset,
4050 .write = is_write,
4051 .flush_interval = flush_interval,
4052 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004053 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004054 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004055 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004056 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004057 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004058 if (flush_interval) {
4059 printf("Sending flush every %d requests\n", flush_interval);
4060 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004061
4062 data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004063 memset(data.buf, pattern, data.nrreq * data.bufsize);
4064
Kevin Wolfb6133b82014-08-05 14:17:13 +02004065 data.qiov = g_new(QEMUIOVector, data.nrreq);
4066 for (i = 0; i < data.nrreq; i++) {
4067 qemu_iovec_init(&data.qiov[i], 1);
4068 qemu_iovec_add(&data.qiov[i],
4069 data.buf + i * data.bufsize, data.bufsize);
4070 }
4071
4072 gettimeofday(&t1, NULL);
4073 bench_cb(&data, 0);
4074
4075 while (data.n > 0) {
4076 main_loop_wait(false);
4077 }
4078 gettimeofday(&t2, NULL);
4079
4080 printf("Run completed in %3.3f seconds.\n",
4081 (t2.tv_sec - t1.tv_sec)
4082 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4083
4084out:
4085 qemu_vfree(data.buf);
4086 blk_unref(blk);
4087
4088 if (ret) {
4089 return 1;
4090 }
4091 return 0;
4092}
4093
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004094#define C_BS 01
4095#define C_COUNT 02
4096#define C_IF 04
4097#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004098#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004099
4100struct DdInfo {
4101 unsigned int flags;
4102 int64_t count;
4103};
4104
4105struct DdIo {
4106 int bsz; /* Block size */
4107 char *filename;
4108 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004109 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004110};
4111
4112struct DdOpts {
4113 const char *name;
4114 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4115 unsigned int flag;
4116};
4117
4118static int img_dd_bs(const char *arg,
4119 struct DdIo *in, struct DdIo *out,
4120 struct DdInfo *dd)
4121{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004122 int64_t res;
4123
Markus Armbruster606caa02017-02-21 21:14:04 +01004124 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004125
Markus Armbruster606caa02017-02-21 21:14:04 +01004126 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004127 error_report("invalid number: '%s'", arg);
4128 return 1;
4129 }
4130 in->bsz = out->bsz = res;
4131
4132 return 0;
4133}
4134
4135static int img_dd_count(const char *arg,
4136 struct DdIo *in, struct DdIo *out,
4137 struct DdInfo *dd)
4138{
Markus Armbruster606caa02017-02-21 21:14:04 +01004139 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004140
Markus Armbruster606caa02017-02-21 21:14:04 +01004141 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004142 error_report("invalid number: '%s'", arg);
4143 return 1;
4144 }
4145
4146 return 0;
4147}
4148
4149static int img_dd_if(const char *arg,
4150 struct DdIo *in, struct DdIo *out,
4151 struct DdInfo *dd)
4152{
4153 in->filename = g_strdup(arg);
4154
4155 return 0;
4156}
4157
4158static int img_dd_of(const char *arg,
4159 struct DdIo *in, struct DdIo *out,
4160 struct DdInfo *dd)
4161{
4162 out->filename = g_strdup(arg);
4163
4164 return 0;
4165}
4166
Reda Sallahif7c15532016-08-10 16:16:09 +02004167static int img_dd_skip(const char *arg,
4168 struct DdIo *in, struct DdIo *out,
4169 struct DdInfo *dd)
4170{
Markus Armbruster606caa02017-02-21 21:14:04 +01004171 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004172
Markus Armbruster606caa02017-02-21 21:14:04 +01004173 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004174 error_report("invalid number: '%s'", arg);
4175 return 1;
4176 }
4177
4178 return 0;
4179}
4180
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004181static int img_dd(int argc, char **argv)
4182{
4183 int ret = 0;
4184 char *arg = NULL;
4185 char *tmp;
4186 BlockDriver *drv = NULL, *proto_drv = NULL;
4187 BlockBackend *blk1 = NULL, *blk2 = NULL;
4188 QemuOpts *opts = NULL;
4189 QemuOptsList *create_opts = NULL;
4190 Error *local_err = NULL;
4191 bool image_opts = false;
4192 int c, i;
4193 const char *out_fmt = "raw";
4194 const char *fmt = NULL;
4195 int64_t size = 0;
4196 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004197 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004198 struct DdInfo dd = {
4199 .flags = 0,
4200 .count = 0,
4201 };
4202 struct DdIo in = {
4203 .bsz = 512, /* Block size is by default 512 bytes */
4204 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004205 .buf = NULL,
4206 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004207 };
4208 struct DdIo out = {
4209 .bsz = 512,
4210 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004211 .buf = NULL,
4212 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004213 };
4214
4215 const struct DdOpts options[] = {
4216 { "bs", img_dd_bs, C_BS },
4217 { "count", img_dd_count, C_COUNT },
4218 { "if", img_dd_if, C_IF },
4219 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004220 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004221 { NULL, NULL, 0 }
4222 };
4223 const struct option long_options[] = {
4224 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004225 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004226 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004227 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004228 { 0, 0, 0, 0 }
4229 };
4230
Fam Zheng335e9932017-05-03 00:35:39 +08004231 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004232 if (c == EOF) {
4233 break;
4234 }
4235 switch (c) {
4236 case 'O':
4237 out_fmt = optarg;
4238 break;
4239 case 'f':
4240 fmt = optarg;
4241 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004242 case ':':
4243 missing_argument(argv[optind - 1]);
4244 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004245 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004246 unrecognized_option(argv[optind - 1]);
4247 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004248 case 'h':
4249 help();
4250 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004251 case 'U':
4252 force_share = true;
4253 break;
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004254 case OPTION_OBJECT:
4255 if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004256 ret = -1;
4257 goto out;
4258 }
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004259 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004260 case OPTION_IMAGE_OPTS:
4261 image_opts = true;
4262 break;
4263 }
4264 }
4265
4266 for (i = optind; i < argc; i++) {
4267 int j;
4268 arg = g_strdup(argv[i]);
4269
4270 tmp = strchr(arg, '=');
4271 if (tmp == NULL) {
4272 error_report("unrecognized operand %s", arg);
4273 ret = -1;
4274 goto out;
4275 }
4276
4277 *tmp++ = '\0';
4278
4279 for (j = 0; options[j].name != NULL; j++) {
4280 if (!strcmp(arg, options[j].name)) {
4281 break;
4282 }
4283 }
4284 if (options[j].name == NULL) {
4285 error_report("unrecognized operand %s", arg);
4286 ret = -1;
4287 goto out;
4288 }
4289
4290 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4291 ret = -1;
4292 goto out;
4293 }
4294 dd.flags |= options[j].flag;
4295 g_free(arg);
4296 arg = NULL;
4297 }
4298
4299 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4300 error_report("Must specify both input and output files");
4301 ret = -1;
4302 goto out;
4303 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004304
4305 if (qemu_opts_foreach(&qemu_object_opts,
4306 user_creatable_add_opts_foreach,
4307 NULL, NULL)) {
4308 ret = -1;
4309 goto out;
4310 }
4311
Fam Zheng335e9932017-05-03 00:35:39 +08004312 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4313 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004314
4315 if (!blk1) {
4316 ret = -1;
4317 goto out;
4318 }
4319
4320 drv = bdrv_find_format(out_fmt);
4321 if (!drv) {
4322 error_report("Unknown file format");
4323 ret = -1;
4324 goto out;
4325 }
4326 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4327
4328 if (!proto_drv) {
4329 error_report_err(local_err);
4330 ret = -1;
4331 goto out;
4332 }
4333 if (!drv->create_opts) {
4334 error_report("Format driver '%s' does not support image creation",
4335 drv->format_name);
4336 ret = -1;
4337 goto out;
4338 }
4339 if (!proto_drv->create_opts) {
4340 error_report("Protocol driver '%s' does not support image creation",
4341 proto_drv->format_name);
4342 ret = -1;
4343 goto out;
4344 }
4345 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4346 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4347
4348 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4349
4350 size = blk_getlength(blk1);
4351 if (size < 0) {
4352 error_report("Failed to get size for '%s'", in.filename);
4353 ret = -1;
4354 goto out;
4355 }
4356
4357 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4358 dd.count * in.bsz < size) {
4359 size = dd.count * in.bsz;
4360 }
4361
Reda Sallahif7c15532016-08-10 16:16:09 +02004362 /* Overflow means the specified offset is beyond input image's size */
4363 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4364 size < in.bsz * in.offset)) {
4365 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4366 } else {
4367 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4368 size - in.bsz * in.offset, &error_abort);
4369 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004370
4371 ret = bdrv_create(drv, out.filename, opts, &local_err);
4372 if (ret < 0) {
4373 error_reportf_err(local_err,
4374 "%s: error while creating output image: ",
4375 out.filename);
4376 ret = -1;
4377 goto out;
4378 }
4379
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004380 /* TODO, we can't honour --image-opts for the target,
4381 * since it needs to be given in a format compatible
4382 * with the bdrv_create() call above which does not
4383 * support image-opts style.
4384 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01004385 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004386 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004387
4388 if (!blk2) {
4389 ret = -1;
4390 goto out;
4391 }
4392
Reda Sallahif7c15532016-08-10 16:16:09 +02004393 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4394 size < in.offset * in.bsz)) {
4395 /* We give a warning if the skip option is bigger than the input
4396 * size and create an empty output disk image (i.e. like dd(1)).
4397 */
4398 error_report("%s: cannot skip to specified offset", in.filename);
4399 in_pos = size;
4400 } else {
4401 in_pos = in.offset * in.bsz;
4402 }
4403
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004404 in.buf = g_new(uint8_t, in.bsz);
4405
Reda Sallahif7c15532016-08-10 16:16:09 +02004406 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004407 int in_ret, out_ret;
4408
4409 if (in_pos + in.bsz > size) {
4410 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4411 } else {
4412 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4413 }
4414 if (in_ret < 0) {
4415 error_report("error while reading from input image file: %s",
4416 strerror(-in_ret));
4417 ret = -1;
4418 goto out;
4419 }
4420 in_pos += in_ret;
4421
4422 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4423
4424 if (out_ret < 0) {
4425 error_report("error while writing to output image file: %s",
4426 strerror(-out_ret));
4427 ret = -1;
4428 goto out;
4429 }
4430 out_pos += out_ret;
4431 }
4432
4433out:
4434 g_free(arg);
4435 qemu_opts_del(opts);
4436 qemu_opts_free(create_opts);
4437 blk_unref(blk1);
4438 blk_unref(blk2);
4439 g_free(in.filename);
4440 g_free(out.filename);
4441 g_free(in.buf);
4442 g_free(out.buf);
4443
4444 if (ret) {
4445 return 1;
4446 }
4447 return 0;
4448}
4449
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004450static void dump_json_block_measure_info(BlockMeasureInfo *info)
4451{
4452 QString *str;
4453 QObject *obj;
4454 Visitor *v = qobject_output_visitor_new(&obj);
4455
4456 visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
4457 visit_complete(v, &obj);
4458 str = qobject_to_json_pretty(obj);
4459 assert(str != NULL);
4460 printf("%s\n", qstring_get_str(str));
4461 qobject_decref(obj);
4462 visit_free(v);
4463 QDECREF(str);
4464}
4465
4466static int img_measure(int argc, char **argv)
4467{
4468 static const struct option long_options[] = {
4469 {"help", no_argument, 0, 'h'},
4470 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4471 {"object", required_argument, 0, OPTION_OBJECT},
4472 {"output", required_argument, 0, OPTION_OUTPUT},
4473 {"size", required_argument, 0, OPTION_SIZE},
4474 {"force-share", no_argument, 0, 'U'},
4475 {0, 0, 0, 0}
4476 };
4477 OutputFormat output_format = OFORMAT_HUMAN;
4478 BlockBackend *in_blk = NULL;
4479 BlockDriver *drv;
4480 const char *filename = NULL;
4481 const char *fmt = NULL;
4482 const char *out_fmt = "raw";
4483 char *options = NULL;
4484 char *snapshot_name = NULL;
4485 bool force_share = false;
4486 QemuOpts *opts = NULL;
4487 QemuOpts *object_opts = NULL;
4488 QemuOpts *sn_opts = NULL;
4489 QemuOptsList *create_opts = NULL;
4490 bool image_opts = false;
4491 uint64_t img_size = UINT64_MAX;
4492 BlockMeasureInfo *info = NULL;
4493 Error *local_err = NULL;
4494 int ret = 1;
4495 int c;
4496
4497 while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
4498 long_options, NULL)) != -1) {
4499 switch (c) {
4500 case '?':
4501 case 'h':
4502 help();
4503 break;
4504 case 'f':
4505 fmt = optarg;
4506 break;
4507 case 'O':
4508 out_fmt = optarg;
4509 break;
4510 case 'o':
4511 if (!is_valid_option_list(optarg)) {
4512 error_report("Invalid option list: %s", optarg);
4513 goto out;
4514 }
4515 if (!options) {
4516 options = g_strdup(optarg);
4517 } else {
4518 char *old_options = options;
4519 options = g_strdup_printf("%s,%s", options, optarg);
4520 g_free(old_options);
4521 }
4522 break;
4523 case 'l':
4524 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
4525 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
4526 optarg, false);
4527 if (!sn_opts) {
4528 error_report("Failed in parsing snapshot param '%s'",
4529 optarg);
4530 goto out;
4531 }
4532 } else {
4533 snapshot_name = optarg;
4534 }
4535 break;
4536 case 'U':
4537 force_share = true;
4538 break;
4539 case OPTION_OBJECT:
4540 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
4541 optarg, true);
4542 if (!object_opts) {
4543 goto out;
4544 }
4545 break;
4546 case OPTION_IMAGE_OPTS:
4547 image_opts = true;
4548 break;
4549 case OPTION_OUTPUT:
4550 if (!strcmp(optarg, "json")) {
4551 output_format = OFORMAT_JSON;
4552 } else if (!strcmp(optarg, "human")) {
4553 output_format = OFORMAT_HUMAN;
4554 } else {
4555 error_report("--output must be used with human or json "
4556 "as argument.");
4557 goto out;
4558 }
4559 break;
4560 case OPTION_SIZE:
4561 {
4562 int64_t sval;
4563
4564 sval = cvtnum(optarg);
4565 if (sval < 0) {
4566 if (sval == -ERANGE) {
4567 error_report("Image size must be less than 8 EiB!");
4568 } else {
4569 error_report("Invalid image size specified! You may use "
4570 "k, M, G, T, P or E suffixes for ");
4571 error_report("kilobytes, megabytes, gigabytes, terabytes, "
4572 "petabytes and exabytes.");
4573 }
4574 goto out;
4575 }
4576 img_size = (uint64_t)sval;
4577 }
4578 break;
4579 }
4580 }
4581
4582 if (qemu_opts_foreach(&qemu_object_opts,
4583 user_creatable_add_opts_foreach,
4584 NULL, NULL)) {
4585 goto out;
4586 }
4587
4588 if (argc - optind > 1) {
4589 error_report("At most one filename argument is allowed.");
4590 goto out;
4591 } else if (argc - optind == 1) {
4592 filename = argv[optind];
4593 }
4594
4595 if (!filename &&
4596 (object_opts || image_opts || fmt || snapshot_name || sn_opts)) {
4597 error_report("--object, --image-opts, -f, and -l "
4598 "require a filename argument.");
4599 goto out;
4600 }
4601 if (filename && img_size != UINT64_MAX) {
4602 error_report("--size N cannot be used together with a filename.");
4603 goto out;
4604 }
4605 if (!filename && img_size == UINT64_MAX) {
4606 error_report("Either --size N or one filename must be specified.");
4607 goto out;
4608 }
4609
4610 if (filename) {
4611 in_blk = img_open(image_opts, filename, fmt, 0,
4612 false, false, force_share);
4613 if (!in_blk) {
4614 goto out;
4615 }
4616
4617 if (sn_opts) {
4618 bdrv_snapshot_load_tmp(blk_bs(in_blk),
4619 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
4620 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
4621 &local_err);
4622 } else if (snapshot_name != NULL) {
4623 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
4624 snapshot_name, &local_err);
4625 }
4626 if (local_err) {
4627 error_reportf_err(local_err, "Failed to load snapshot: ");
4628 goto out;
4629 }
4630 }
4631
4632 drv = bdrv_find_format(out_fmt);
4633 if (!drv) {
4634 error_report("Unknown file format '%s'", out_fmt);
4635 goto out;
4636 }
4637 if (!drv->create_opts) {
4638 error_report("Format driver '%s' does not support image creation",
4639 drv->format_name);
4640 goto out;
4641 }
4642
4643 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4644 create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
4645 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4646 if (options) {
4647 qemu_opts_do_parse(opts, options, NULL, &local_err);
4648 if (local_err) {
4649 error_report_err(local_err);
4650 error_report("Invalid options for file format '%s'", out_fmt);
4651 goto out;
4652 }
4653 }
4654 if (img_size != UINT64_MAX) {
4655 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
4656 }
4657
4658 info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
4659 if (local_err) {
4660 error_report_err(local_err);
4661 goto out;
4662 }
4663
4664 if (output_format == OFORMAT_HUMAN) {
4665 printf("required size: %" PRIu64 "\n", info->required);
4666 printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
4667 } else {
4668 dump_json_block_measure_info(info);
4669 }
4670
4671 ret = 0;
4672
4673out:
4674 qapi_free_BlockMeasureInfo(info);
4675 qemu_opts_del(object_opts);
4676 qemu_opts_del(opts);
4677 qemu_opts_del(sn_opts);
4678 qemu_opts_free(create_opts);
4679 g_free(options);
4680 blk_unref(in_blk);
4681 return ret;
4682}
Kevin Wolfb6133b82014-08-05 14:17:13 +02004683
Anthony Liguoric227f092009-10-01 16:12:16 -05004684static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004685#define DEF(option, callback, arg_string) \
4686 { option, callback },
4687#include "qemu-img-cmds.h"
4688#undef DEF
4689#undef GEN_DOCS
4690 { NULL, NULL, },
4691};
4692
bellardea2384d2004-08-01 21:59:26 +00004693int main(int argc, char **argv)
4694{
Anthony Liguoric227f092009-10-01 16:12:16 -05004695 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004696 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004697 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004698 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004699 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004700 static const struct option long_options[] = {
4701 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004702 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004703 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004704 {0, 0, 0, 0}
4705 };
bellardea2384d2004-08-01 21:59:26 +00004706
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004707#ifdef CONFIG_POSIX
4708 signal(SIGPIPE, SIG_IGN);
4709#endif
4710
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004711 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004712 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004713 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004714
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004715 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004716 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004717 exit(EXIT_FAILURE);
4718 }
4719
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004720 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004721
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004722 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004723 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004724 if (argc < 2) {
4725 error_exit("Not enough arguments");
4726 }
Stuart Brady153859b2009-06-07 00:42:17 +01004727
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004728 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004729 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004730 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004731
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004732 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004733 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004734 case ':':
4735 missing_argument(argv[optind - 1]);
4736 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08004737 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004738 unrecognized_option(argv[optind - 1]);
4739 return 0;
4740 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03004741 help();
4742 return 0;
4743 case 'V':
4744 printf(QEMU_IMG_VERSION);
4745 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004746 case 'T':
4747 g_free(trace_file);
4748 trace_file = trace_opt_parse(optarg);
4749 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004750 }
bellardea2384d2004-08-01 21:59:26 +00004751 }
Stuart Brady153859b2009-06-07 00:42:17 +01004752
Denis V. Lunev10985132016-06-17 17:44:13 +03004753 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004754
Denis V. Lunev10985132016-06-17 17:44:13 +03004755 /* reset getopt_long scanning */
4756 argc -= optind;
4757 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004758 return 0;
4759 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004760 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004761 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004762
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004763 if (!trace_init_backends()) {
4764 exit(1);
4765 }
4766 trace_init_file(trace_file);
4767 qemu_set_log(LOG_TRACE);
4768
Denis V. Lunev10985132016-06-17 17:44:13 +03004769 /* find the command */
4770 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4771 if (!strcmp(cmdname, cmd->name)) {
4772 return cmd->handler(argc, argv);
4773 }
4774 }
Jeff Cody7db16892014-04-25 17:02:32 -04004775
Stuart Brady153859b2009-06-07 00:42:17 +01004776 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004777 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004778}