blob: 75f1610aa021aaf51cdb861cf884d0e41250b919 [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 */
Markus Armbruster452fcdb2018-02-01 12:18:39 +010024
Peter Maydell80c71a22016-01-18 18:01:42 +000025#include "qemu/osdep.h"
Eric Blakec2a3d7d2017-07-21 08:50:47 -050026#include <getopt.h>
27
Fam Zheng67a1de02016-06-01 17:44:21 +080028#include "qemu-version.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010029#include "qapi/error.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010030#include "qapi/qapi-visit-block-core.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010031#include "qapi/qobject-output-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010032#include "qapi/qmp/qjson.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010033#include "qapi/qmp/qdict.h"
Markus Armbrusterfc81fa12018-02-01 12:18:40 +010034#include "qapi/qmp/qstring.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020035#include "qemu/cutils.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000036#include "qemu/config-file.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010037#include "qemu/option.h"
38#include "qemu/error-report.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030039#include "qemu/log.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000040#include "qom/object_interfaces.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010041#include "sysemu/sysemu.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020042#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010043#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020044#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080045#include "block/qapi.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010046#include "crypto/init.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030047#include "trace/control.h"
bellarde8445332006-06-14 15:32:10 +000048
Thomas Huth7e563bf2018-02-15 12:06:47 +010049#define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \
Thomas Huth0781dd62016-10-05 11:54:44 +020050 "\n" QEMU_COPYRIGHT "\n"
Jeff Cody5f6979c2014-04-28 14:37:18 -040051
Anthony Liguoric227f092009-10-01 16:12:16 -050052typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010053 const char *name;
54 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050055} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010056
Federico Simoncelli8599ea42013-01-28 06:59:47 -050057enum {
58 OPTION_OUTPUT = 256,
59 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000060 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000061 OPTION_IMAGE_OPTS = 259,
Kevin Wolfb6495fa2015-07-10 18:09:18 +020062 OPTION_PATTERN = 260,
Kevin Wolf55d539c2016-06-03 13:59:41 +020063 OPTION_FLUSH_INTERVAL = 261,
64 OPTION_NO_DRAIN = 262,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +010065 OPTION_TARGET_IMAGE_OPTS = 263,
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +010066 OPTION_SIZE = 264,
Max Reitzdc5f6902017-06-13 22:20:55 +020067 OPTION_PREALLOCATION = 265,
Pavel Butsykin4ffca892017-09-18 15:42:27 +030068 OPTION_SHRINK = 266,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050069};
70
71typedef enum OutputFormat {
72 OFORMAT_JSON,
73 OFORMAT_HUMAN,
74} OutputFormat;
75
Kevin Wolfe6996142016-03-15 13:03:11 +010076/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040077#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000078
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010079static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000080{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010081 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000082}
83
Fam Zhengac1307a2014-04-22 13:36:11 +080084static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
85{
86 va_list ap;
87
88 error_printf("qemu-img: ");
89
90 va_start(ap, fmt);
91 error_vprintf(fmt, ap);
92 va_end(ap);
93
94 error_printf("\nTry 'qemu-img --help' for more information\n");
95 exit(EXIT_FAILURE);
96}
97
Stefan Hajnoczic9192972017-03-17 18:45:41 +080098static void QEMU_NORETURN missing_argument(const char *option)
99{
100 error_exit("missing argument for option '%s'", option);
101}
102
103static void QEMU_NORETURN unrecognized_option(const char *option)
104{
105 error_exit("unrecognized option '%s'", option);
106}
107
blueswir1d2c639d2009-01-24 18:19:25 +0000108/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +0800109static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +0000110{
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100111 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -0400112 QEMU_IMG_VERSION
Denis V. Lunev10985132016-06-17 17:44:13 +0300113 "usage: qemu-img [standard options] command [command options]\n"
malc3f020d72010-02-08 12:04:56 +0300114 "QEMU disk image utility\n"
115 "\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300116 " '-h', '--help' display this help and exit\n"
117 " '-V', '--version' output version information and exit\n"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +0300118 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
119 " specify tracing options\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300120 "\n"
malc3f020d72010-02-08 12:04:56 +0300121 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100122#define DEF(option, callback, arg_string) \
123 " " arg_string "\n"
124#include "qemu-img-cmds.h"
125#undef DEF
malc3f020d72010-02-08 12:04:56 +0300126 "\n"
127 "Command parameters:\n"
128 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000129 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
130 " manual page for a description of the object properties. The most common\n"
131 " object type is a 'secret', which is used to supply passwords and/or\n"
132 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300133 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400134 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800135 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
136 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100137 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
138 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300139 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200140 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
141 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
142 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300143 " 'output_filename' is the destination disk image filename\n"
144 " 'output_fmt' is the destination format\n"
145 " 'options' is a comma separated list of format specific options in a\n"
146 " name=value format. Use -o ? for an overview of the options supported by the\n"
147 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800148 " 'snapshot_param' is param used for internal snapshot, format\n"
149 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
150 " '[ID_OR_NAME]'\n"
151 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
152 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300153 " '-c' indicates that target image must be compressed (qcow format only)\n"
John Snow6e6e55f2017-07-17 20:34:22 -0400154 " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
155 " new backing file match exactly. The image doesn't need a working\n"
156 " backing file before rebasing in this case (useful for renaming the\n"
157 " backing file). For image creation, allow creating without attempting\n"
158 " to open the backing file.\n"
malc3f020d72010-02-08 12:04:56 +0300159 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200160 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100161 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200162 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
163 " contain only zeros for qemu-img to create a sparse image during\n"
164 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
165 " unallocated or zero sectors, and the destination image will always be\n"
166 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200167 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100168 " '-n' skips the target volume creation (useful if the volume is created\n"
169 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300170 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200171 "Parameters to check subcommand:\n"
172 " '-r' tries to repair any inconsistencies that are found during the check.\n"
173 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
174 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200175 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200176 "\n"
Peter Lieven2d9187b2017-02-28 13:40:07 +0100177 "Parameters to convert subcommand:\n"
178 " '-m' specifies how many coroutines work in parallel during the convert\n"
179 " process (defaults to 8)\n"
180 " '-W' allow to write to the target out of order rather than sequential\n"
181 "\n"
malc3f020d72010-02-08 12:04:56 +0300182 "Parameters to snapshot subcommand:\n"
183 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
184 " '-a' applies a snapshot (revert disk to saved state)\n"
185 " '-c' creates a snapshot\n"
186 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100187 " '-l' lists all snapshots in the given image\n"
188 "\n"
189 "Parameters to compare subcommand:\n"
190 " '-f' first image format\n"
191 " '-F' second image format\n"
Reda Sallahi86ce1f62016-08-10 04:43:12 +0200192 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
193 "\n"
194 "Parameters to dd subcommand:\n"
195 " 'bs=BYTES' read and write up to BYTES bytes at a time "
196 "(default: 512)\n"
197 " 'count=N' copy only N input blocks\n"
198 " 'if=FILE' read from FILE\n"
Reda Sallahif7c15532016-08-10 16:16:09 +0200199 " 'of=FILE' write to FILE\n"
200 " 'skip=N' skip N bs-sized blocks at the start of input\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100201
202 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100203 bdrv_iterate_format(format_print, NULL);
Eric Blakef5048cb2017-08-03 11:33:53 -0500204 printf("\n\n" QEMU_HELP_BOTTOM "\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800205 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000206}
207
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000208static QemuOptsList qemu_object_opts = {
209 .name = "object",
210 .implied_opt_name = "qom-type",
211 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
212 .desc = {
213 { }
214 },
215};
216
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000217static QemuOptsList qemu_source_opts = {
218 .name = "source",
219 .implied_opt_name = "file",
220 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
221 .desc = {
222 { }
223 },
224};
225
Stefan Weil7c30f652013-06-16 17:01:05 +0200226static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100227{
228 int ret = 0;
229 if (!quiet) {
230 va_list args;
231 va_start(args, fmt);
232 ret = vprintf(fmt, args);
233 va_end(args);
234 }
235 return ret;
236}
237
bellardea2384d2004-08-01 21:59:26 +0000238
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100239static int print_block_option_help(const char *filename, const char *fmt)
240{
241 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800242 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500243 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100244
245 /* Find driver and parse its options */
246 drv = bdrv_find_format(fmt);
247 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100248 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100249 return 1;
250 }
251
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800252 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100253 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500254 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100255 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100256 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800257 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100258 return 1;
259 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800260 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100261 }
262
Chunyan Liu83d05212014-06-05 17:20:51 +0800263 qemu_opts_print_help(create_opts);
264 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100265 return 0;
266}
267
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000268
Max Reitzefaa7c42016-03-16 19:54:38 +0100269static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100270 QemuOpts *opts, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800271 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000272{
273 QDict *options;
274 Error *local_err = NULL;
275 BlockBackend *blk;
276 options = qemu_opts_to_qdict(opts, NULL);
Fam Zheng335e9932017-05-03 00:35:39 +0800277 if (force_share) {
278 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
Max Reitz4615f872018-05-02 22:20:50 +0200279 && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) {
Fam Zheng335e9932017-05-03 00:35:39 +0800280 error_report("--force-share/-U conflicts with image options");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200281 qobject_unref(options);
Fam Zheng335e9932017-05-03 00:35:39 +0800282 return NULL;
283 }
Max Reitz4615f872018-05-02 22:20:50 +0200284 qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on");
Fam Zheng335e9932017-05-03 00:35:39 +0800285 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100286 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000287 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100288 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000289 return NULL;
290 }
Kevin Wolfce099542016-03-15 13:01:04 +0100291 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000292
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000293 return blk;
294}
295
Max Reitzefaa7c42016-03-16 19:54:38 +0100296static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100297 QDict *options,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000298 const char *fmt, int flags,
Fam Zheng335e9932017-05-03 00:35:39 +0800299 bool writethrough, bool quiet,
300 bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000301{
302 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200303 Error *local_err = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100304
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100305 if (!options) {
306 options = qdict_new();
307 }
bellard75c23802004-08-27 21:28:58 +0000308 if (fmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500309 qdict_put_str(options, "driver", fmt);
bellard75c23802004-08-27 21:28:58 +0000310 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100311
Fam Zheng335e9932017-05-03 00:35:39 +0800312 if (force_share) {
Eric Blake579cf1d2017-05-15 14:54:39 -0500313 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800314 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100315 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500316 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100317 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000318 return NULL;
bellard75c23802004-08-27 21:28:58 +0000319 }
Kevin Wolfce099542016-03-15 13:01:04 +0100320 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100321
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200322 return blk;
bellard75c23802004-08-27 21:28:58 +0000323}
324
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000325
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100326static int img_add_key_secrets(void *opaque,
327 const char *name, const char *value,
328 Error **errp)
329{
330 QDict *options = opaque;
331
332 if (g_str_has_suffix(name, "key-secret")) {
Eric Blake187f47e2017-06-24 12:10:07 -0600333 qdict_put_str(options, name, value);
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100334 }
335
336 return 0;
337}
338
339static BlockBackend *img_open_new_file(const char *filename,
340 QemuOpts *create_opts,
341 const char *fmt, int flags,
342 bool writethrough, bool quiet,
343 bool force_share)
344{
345 QDict *options = NULL;
346
347 options = qdict_new();
348 qemu_opt_foreach(create_opts, img_add_key_secrets, options, &error_abort);
349
350 return img_open_file(filename, options, fmt, flags, writethrough, quiet,
351 force_share);
352}
353
354
Max Reitzefaa7c42016-03-16 19:54:38 +0100355static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000356 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100357 const char *fmt, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800358 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000359{
360 BlockBackend *blk;
361 if (image_opts) {
362 QemuOpts *opts;
363 if (fmt) {
364 error_report("--image-opts and --format are mutually exclusive");
365 return NULL;
366 }
367 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
368 filename, true);
369 if (!opts) {
370 return NULL;
371 }
Fam Zheng335e9932017-05-03 00:35:39 +0800372 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
373 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000374 } else {
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100375 blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
Fam Zheng335e9932017-05-03 00:35:39 +0800376 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000377 }
378 return blk;
379}
380
381
Chunyan Liu83d05212014-06-05 17:20:51 +0800382static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100383 const char *base_filename,
384 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200385{
Markus Armbruster6750e792015-02-12 17:43:08 +0100386 Error *err = NULL;
387
Kevin Wolfefa84d42009-05-18 16:42:12 +0200388 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100389 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100390 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100391 error_report("Backing file not supported for file format '%s'",
392 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100393 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900394 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200395 }
396 }
397 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100398 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100399 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100400 error_report("Backing file format not supported for file "
401 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100402 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900403 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200404 }
405 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900406 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200407}
408
Markus Armbruster606caa02017-02-21 21:14:04 +0100409static int64_t cvtnum(const char *s)
410{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100411 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100412 uint64_t value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100413
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100414 err = qemu_strtosz(s, NULL, &value);
415 if (err < 0) {
416 return err;
417 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100418 if (value > INT64_MAX) {
419 return -ERANGE;
420 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100421 return value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100422}
423
bellardea2384d2004-08-01 21:59:26 +0000424static int img_create(int argc, char **argv)
425{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200426 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100427 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000428 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000429 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000430 const char *filename;
431 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200432 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200433 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100434 bool quiet = false;
John Snow6e6e55f2017-07-17 20:34:22 -0400435 int flags = 0;
ths3b46e622007-09-17 08:09:54 +0000436
bellardea2384d2004-08-01 21:59:26 +0000437 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000438 static const struct option long_options[] = {
439 {"help", no_argument, 0, 'h'},
440 {"object", required_argument, 0, OPTION_OBJECT},
441 {0, 0, 0, 0}
442 };
John Snow6e6e55f2017-07-17 20:34:22 -0400443 c = getopt_long(argc, argv, ":F:b:f:ho:qu",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000444 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100445 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000446 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100447 }
bellardea2384d2004-08-01 21:59:26 +0000448 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800449 case ':':
450 missing_argument(argv[optind - 1]);
451 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100452 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800453 unrecognized_option(argv[optind - 1]);
454 break;
bellardea2384d2004-08-01 21:59:26 +0000455 case 'h':
456 help();
457 break;
aliguori9230eaf2009-03-28 17:55:19 +0000458 case 'F':
459 base_fmt = optarg;
460 break;
bellardea2384d2004-08-01 21:59:26 +0000461 case 'b':
462 base_filename = optarg;
463 break;
464 case 'f':
465 fmt = optarg;
466 break;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200467 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100468 if (!is_valid_option_list(optarg)) {
469 error_report("Invalid option list: %s", optarg);
470 goto fail;
471 }
472 if (!options) {
473 options = g_strdup(optarg);
474 } else {
475 char *old_options = options;
476 options = g_strdup_printf("%s,%s", options, optarg);
477 g_free(old_options);
478 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200479 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100480 case 'q':
481 quiet = true;
482 break;
John Snow6e6e55f2017-07-17 20:34:22 -0400483 case 'u':
484 flags |= BDRV_O_NO_BACKING;
485 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000486 case OPTION_OBJECT: {
487 QemuOpts *opts;
488 opts = qemu_opts_parse_noisily(&qemu_object_opts,
489 optarg, true);
490 if (!opts) {
491 goto fail;
492 }
493 } break;
bellardea2384d2004-08-01 21:59:26 +0000494 }
495 }
aliguori9230eaf2009-03-28 17:55:19 +0000496
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900497 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100498 filename = (optind < argc) ? argv[optind] : NULL;
499 if (options && has_help_option(options)) {
500 g_free(options);
501 return print_block_option_help(filename, fmt);
502 }
503
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100504 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800505 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100506 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100507 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900508
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000509 if (qemu_opts_foreach(&qemu_object_opts,
510 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200511 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000512 goto fail;
513 }
514
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100515 /* Get image size, if specified */
516 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100517 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100518
519 sval = cvtnum(argv[optind++]);
520 if (sval < 0) {
liguang79443392012-12-17 09:49:23 +0800521 if (sval == -ERANGE) {
522 error_report("Image size must be less than 8 EiB!");
523 } else {
524 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200525 "G, T, P or E suffixes for ");
526 error_report("kilobytes, megabytes, gigabytes, terabytes, "
527 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800528 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100529 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100530 }
531 img_size = (uint64_t)sval;
532 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200533 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800534 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200535 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100536
Luiz Capitulino9b375252012-11-30 10:52:05 -0200537 bdrv_img_create(filename, fmt, base_filename, base_fmt,
John Snow6e6e55f2017-07-17 20:34:22 -0400538 options, img_size, flags, quiet, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100539 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100540 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100541 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900542 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200543
Kevin Wolf77386bf2014-02-21 16:24:04 +0100544 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000545 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100546
547fail:
548 g_free(options);
549 return 1;
bellardea2384d2004-08-01 21:59:26 +0000550}
551
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100552static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500553{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500554 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500555 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100556 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600557
558 visit_type_ImageCheck(v, NULL, &check, &error_abort);
559 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500560 str = qobject_to_json_pretty(obj);
561 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100562 qprintf(quiet, "%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200563 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600564 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200565 qobject_unref(str);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500566}
567
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100568static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500569{
570 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100571 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500572 } else {
573 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100574 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
575 "Data may be corrupted, or further writes to the image "
576 "may corrupt it.\n",
577 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500578 }
579
580 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100581 qprintf(quiet,
582 "\n%" PRId64 " leaked clusters were found on the image.\n"
583 "This means waste of disk space, but no harm to data.\n",
584 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500585 }
586
587 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100588 qprintf(quiet,
589 "\n%" PRId64
590 " internal errors have occurred during the check.\n",
591 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500592 }
593 }
594
595 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100596 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
597 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
598 check->allocated_clusters, check->total_clusters,
599 check->allocated_clusters * 100.0 / check->total_clusters,
600 check->fragmented_clusters * 100.0 / check->allocated_clusters,
601 check->compressed_clusters * 100.0 /
602 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500603 }
604
605 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100606 qprintf(quiet,
607 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500608 }
609}
610
611static int collect_image_check(BlockDriverState *bs,
612 ImageCheck *check,
613 const char *filename,
614 const char *fmt,
615 int fix)
616{
617 int ret;
618 BdrvCheckResult result;
619
620 ret = bdrv_check(bs, &result, fix);
621 if (ret < 0) {
622 return ret;
623 }
624
625 check->filename = g_strdup(filename);
626 check->format = g_strdup(bdrv_get_format_name(bs));
627 check->check_errors = result.check_errors;
628 check->corruptions = result.corruptions;
629 check->has_corruptions = result.corruptions != 0;
630 check->leaks = result.leaks;
631 check->has_leaks = result.leaks != 0;
632 check->corruptions_fixed = result.corruptions_fixed;
633 check->has_corruptions_fixed = result.corruptions != 0;
634 check->leaks_fixed = result.leaks_fixed;
635 check->has_leaks_fixed = result.leaks != 0;
636 check->image_end_offset = result.image_end_offset;
637 check->has_image_end_offset = result.image_end_offset != 0;
638 check->total_clusters = result.bfi.total_clusters;
639 check->has_total_clusters = result.bfi.total_clusters != 0;
640 check->allocated_clusters = result.bfi.allocated_clusters;
641 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
642 check->fragmented_clusters = result.bfi.fragmented_clusters;
643 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100644 check->compressed_clusters = result.bfi.compressed_clusters;
645 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500646
647 return 0;
648}
649
Kevin Wolfe076f332010-06-29 11:43:13 +0200650/*
651 * Checks an image for consistency. Exit codes:
652 *
Max Reitzd6635c42014-06-02 22:15:21 +0200653 * 0 - Check completed, image is good
654 * 1 - Check not completed because of internal errors
655 * 2 - Check completed, image is corrupted
656 * 3 - Check completed, image has leaked clusters, but is good otherwise
657 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200658 */
aliguori15859692009-04-21 23:11:53 +0000659static int img_check(int argc, char **argv)
660{
661 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500662 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200663 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200664 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000665 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200666 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100667 int flags = BDRV_O_CHECK;
668 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200669 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100670 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000671 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +0800672 bool force_share = false;
aliguori15859692009-04-21 23:11:53 +0000673
674 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500675 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200676 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100677
aliguori15859692009-04-21 23:11:53 +0000678 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500679 int option_index = 0;
680 static const struct option long_options[] = {
681 {"help", no_argument, 0, 'h'},
682 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530683 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500684 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000685 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000686 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +0800687 {"force-share", no_argument, 0, 'U'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500688 {0, 0, 0, 0}
689 };
Fam Zheng335e9932017-05-03 00:35:39 +0800690 c = getopt_long(argc, argv, ":hf:r:T:qU",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500691 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100692 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000693 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100694 }
aliguori15859692009-04-21 23:11:53 +0000695 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800696 case ':':
697 missing_argument(argv[optind - 1]);
698 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100699 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800700 unrecognized_option(argv[optind - 1]);
701 break;
aliguori15859692009-04-21 23:11:53 +0000702 case 'h':
703 help();
704 break;
705 case 'f':
706 fmt = optarg;
707 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200708 case 'r':
709 flags |= BDRV_O_RDWR;
710
711 if (!strcmp(optarg, "leaks")) {
712 fix = BDRV_FIX_LEAKS;
713 } else if (!strcmp(optarg, "all")) {
714 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
715 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800716 error_exit("Unknown option value for -r "
717 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200718 }
719 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500720 case OPTION_OUTPUT:
721 output = optarg;
722 break;
Max Reitz40055952014-07-22 22:58:42 +0200723 case 'T':
724 cache = optarg;
725 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100726 case 'q':
727 quiet = true;
728 break;
Fam Zheng335e9932017-05-03 00:35:39 +0800729 case 'U':
730 force_share = true;
731 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000732 case OPTION_OBJECT: {
733 QemuOpts *opts;
734 opts = qemu_opts_parse_noisily(&qemu_object_opts,
735 optarg, true);
736 if (!opts) {
737 return 1;
738 }
739 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000740 case OPTION_IMAGE_OPTS:
741 image_opts = true;
742 break;
aliguori15859692009-04-21 23:11:53 +0000743 }
744 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200745 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800746 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100747 }
aliguori15859692009-04-21 23:11:53 +0000748 filename = argv[optind++];
749
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500750 if (output && !strcmp(output, "json")) {
751 output_format = OFORMAT_JSON;
752 } else if (output && !strcmp(output, "human")) {
753 output_format = OFORMAT_HUMAN;
754 } else if (output) {
755 error_report("--output must be used with human or json as argument.");
756 return 1;
757 }
758
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000759 if (qemu_opts_foreach(&qemu_object_opts,
760 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200761 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000762 return 1;
763 }
764
Kevin Wolfce099542016-03-15 13:01:04 +0100765 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200766 if (ret < 0) {
767 error_report("Invalid source cache option: %s", cache);
768 return 1;
769 }
770
Fam Zheng335e9932017-05-03 00:35:39 +0800771 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
772 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200773 if (!blk) {
774 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900775 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200776 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500777
778 check = g_new0(ImageCheck, 1);
779 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200780
781 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200782 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200783 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500784 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200785 }
786
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500787 if (check->corruptions_fixed || check->leaks_fixed) {
788 int corruptions_fixed, leaks_fixed;
789
790 leaks_fixed = check->leaks_fixed;
791 corruptions_fixed = check->corruptions_fixed;
792
793 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100794 qprintf(quiet,
795 "The following inconsistencies were found and repaired:\n\n"
796 " %" PRId64 " leaked clusters\n"
797 " %" PRId64 " corruptions\n\n"
798 "Double checking the fixed image now...\n",
799 check->leaks_fixed,
800 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500801 }
802
803 ret = collect_image_check(bs, check, filename, fmt, 0);
804
805 check->leaks_fixed = leaks_fixed;
806 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200807 }
808
Max Reitz832390a2014-10-23 15:29:12 +0200809 if (!ret) {
810 switch (output_format) {
811 case OFORMAT_HUMAN:
812 dump_human_image_check(check, quiet);
813 break;
814 case OFORMAT_JSON:
815 dump_json_image_check(check, quiet);
816 break;
817 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500818 }
819
820 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200821 if (ret) {
822 error_report("Check failed: %s", strerror(-ret));
823 } else {
824 error_report("Check failed");
825 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500826 ret = 1;
827 goto fail;
828 }
829
830 if (check->corruptions) {
831 ret = 2;
832 } else if (check->leaks) {
833 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200834 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500835 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000836 }
837
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500838fail:
839 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200840 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500841 return ret;
aliguori15859692009-04-21 23:11:53 +0000842}
843
Max Reitzd4a32382014-10-24 15:57:37 +0200844typedef struct CommonBlockJobCBInfo {
845 BlockDriverState *bs;
846 Error **errp;
847} CommonBlockJobCBInfo;
848
849static void common_block_job_cb(void *opaque, int ret)
850{
851 CommonBlockJobCBInfo *cbi = opaque;
852
853 if (ret < 0) {
854 error_setg_errno(cbi->errp, -ret, "Block job failed");
855 }
Max Reitzd4a32382014-10-24 15:57:37 +0200856}
857
858static void run_block_job(BlockJob *job, Error **errp)
859{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200860 AioContext *aio_context = blk_get_aio_context(job->blk);
sochin.jiang4172a002017-06-15 14:47:33 +0800861 int ret = 0;
Max Reitzd4a32382014-10-24 15:57:37 +0200862
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200863 aio_context_acquire(aio_context);
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200864 job_ref(&job->job);
Max Reitzd4a32382014-10-24 15:57:37 +0200865 do {
Kevin Wolf30a5c882018-05-04 12:17:20 +0200866 float progress = 0.0f;
Max Reitzd4a32382014-10-24 15:57:37 +0200867 aio_poll(aio_context, true);
Kevin Wolf30a5c882018-05-04 12:17:20 +0200868 if (job->job.progress_total) {
869 progress = (float)job->job.progress_current /
870 job->job.progress_total * 100.f;
871 }
872 qemu_progress_print(progress, 0);
Kevin Wolfdf956ae2018-04-25 15:09:58 +0200873 } while (!job_is_ready(&job->job) && !job_is_completed(&job->job));
Max Reitzd4a32382014-10-24 15:57:37 +0200874
Kevin Wolfdbe5e6c2018-04-19 13:04:01 +0200875 if (!job_is_completed(&job->job)) {
Kevin Wolf3d70ff52018-04-24 16:13:52 +0200876 ret = job_complete_sync(&job->job, errp);
sochin.jiang4172a002017-06-15 14:47:33 +0800877 } else {
Kevin Wolf4ad35182018-04-19 17:30:16 +0200878 ret = job->job.ret;
sochin.jiang4172a002017-06-15 14:47:33 +0800879 }
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200880 job_unref(&job->job);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200881 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200882
sochin.jiang4172a002017-06-15 14:47:33 +0800883 /* publish completion progress only when success */
884 if (!ret) {
885 qemu_progress_print(100.f, 0);
886 }
Max Reitzd4a32382014-10-24 15:57:37 +0200887}
888
bellardea2384d2004-08-01 21:59:26 +0000889static int img_commit(int argc, char **argv)
890{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400891 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200892 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200893 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200894 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100895 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200896 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100897 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200898 Error *local_err = NULL;
899 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000900 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200901 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000902
903 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400904 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200905 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000906 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000907 static const struct option long_options[] = {
908 {"help", no_argument, 0, 'h'},
909 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000910 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000911 {0, 0, 0, 0}
912 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800913 c = getopt_long(argc, argv, ":f:ht:b:dpq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000914 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100915 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000916 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100917 }
bellardea2384d2004-08-01 21:59:26 +0000918 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800919 case ':':
920 missing_argument(argv[optind - 1]);
921 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100922 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800923 unrecognized_option(argv[optind - 1]);
924 break;
bellardea2384d2004-08-01 21:59:26 +0000925 case 'h':
926 help();
927 break;
928 case 'f':
929 fmt = optarg;
930 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400931 case 't':
932 cache = optarg;
933 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200934 case 'b':
935 base = optarg;
936 /* -b implies -d */
937 drop = true;
938 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200939 case 'd':
940 drop = true;
941 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200942 case 'p':
943 progress = true;
944 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100945 case 'q':
946 quiet = true;
947 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000948 case OPTION_OBJECT: {
949 QemuOpts *opts;
950 opts = qemu_opts_parse_noisily(&qemu_object_opts,
951 optarg, true);
952 if (!opts) {
953 return 1;
954 }
955 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000956 case OPTION_IMAGE_OPTS:
957 image_opts = true;
958 break;
bellardea2384d2004-08-01 21:59:26 +0000959 }
960 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200961
962 /* Progress is not shown in Quiet mode */
963 if (quiet) {
964 progress = false;
965 }
966
Kevin Wolffc11eb22013-08-05 10:53:04 +0200967 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800968 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100969 }
bellardea2384d2004-08-01 21:59:26 +0000970 filename = argv[optind++];
971
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000972 if (qemu_opts_foreach(&qemu_object_opts,
973 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200974 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000975 return 1;
976 }
977
Max Reitz9a86fe42014-10-24 15:57:38 +0200978 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100979 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400980 if (ret < 0) {
981 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100982 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400983 }
984
Fam Zheng335e9932017-05-03 00:35:39 +0800985 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
986 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200987 if (!blk) {
988 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900989 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200990 bs = blk_bs(blk);
991
Max Reitz687fa1d2014-10-24 15:57:39 +0200992 qemu_progress_init(progress, 1.f);
993 qemu_progress_print(0.f, 100);
994
Max Reitz1b22bff2014-10-24 15:57:40 +0200995 if (base) {
996 base_bs = bdrv_find_backing_image(bs, base);
997 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +0100998 error_setg(&local_err,
999 "Did not find '%s' in the backing chain of '%s'",
1000 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +02001001 goto done;
1002 }
1003 } else {
1004 /* This is different from QMP, which by default uses the deepest file in
1005 * the backing chain (i.e., the very base); however, the traditional
1006 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +02001007 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +02001008 if (!base_bs) {
1009 error_setg(&local_err, "Image does not have a backing file");
1010 goto done;
1011 }
bellardea2384d2004-08-01 21:59:26 +00001012 }
1013
Max Reitzd4a32382014-10-24 15:57:37 +02001014 cbi = (CommonBlockJobCBInfo){
1015 .errp = &local_err,
1016 .bs = bs,
1017 };
1018
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001019 aio_context = bdrv_get_aio_context(bs);
1020 aio_context_acquire(aio_context);
Kevin Wolfbb02b652018-04-19 17:54:56 +02001021 commit_active_start("commit", bs, base_bs, JOB_DEFAULT, 0,
Kevin Wolf0db832f2017-02-20 18:10:05 +01001022 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +08001023 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001024 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +02001025 if (local_err) {
1026 goto done;
1027 }
1028
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001029 /* When the block job completes, the BlockBackend reference will point to
1030 * the old backing file. In order to avoid that the top image is already
1031 * deleted, so we can still empty it afterwards, increment the reference
1032 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +02001033 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001034 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001035 }
1036
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001037 job = block_job_get("commit");
1038 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001039 if (local_err) {
1040 goto unref_backing;
1041 }
1042
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001043 if (!drop && bs->drv->bdrv_make_empty) {
1044 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001045 if (ret) {
1046 error_setg_errno(&local_err, -ret, "Could not empty %s",
1047 filename);
1048 goto unref_backing;
1049 }
1050 }
1051
1052unref_backing:
1053 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001054 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001055 }
Max Reitzd4a32382014-10-24 15:57:37 +02001056
1057done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001058 qemu_progress_end();
1059
Markus Armbruster26f54e92014-10-07 13:59:04 +02001060 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001061
1062 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001063 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001064 return 1;
1065 }
Max Reitzd4a32382014-10-24 15:57:37 +02001066
1067 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001068 return 0;
1069}
1070
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001071/*
Eric Blakedebb38a2017-10-11 22:47:11 -05001072 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1073 * of the first sector boundary within buf where the sector contains a
1074 * non-zero byte. This function is robust to a buffer that is not
1075 * sector-aligned.
1076 */
1077static int64_t find_nonzero(const uint8_t *buf, int64_t n)
1078{
1079 int64_t i;
1080 int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE);
1081
1082 for (i = 0; i < end; i += BDRV_SECTOR_SIZE) {
1083 if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) {
1084 return i;
1085 }
1086 }
1087 if (i < n && !buffer_is_zero(buf + i, n - end)) {
1088 return i;
1089 }
1090 return -1;
1091}
1092
1093/*
thsf58c7b32008-06-05 21:53:49 +00001094 * Returns true iff the first sector pointed to by 'buf' contains at least
1095 * a non-NUL byte.
1096 *
1097 * 'pnum' is set to the number of sectors (including and immediately following
1098 * the first one) that are known to be in the same allocated/unallocated state.
1099 */
bellardea2384d2004-08-01 21:59:26 +00001100static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1101{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001102 bool is_zero;
1103 int i;
bellardea2384d2004-08-01 21:59:26 +00001104
1105 if (n <= 0) {
1106 *pnum = 0;
1107 return 0;
1108 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001109 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001110 for(i = 1; i < n; i++) {
1111 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001112 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001113 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001114 }
bellardea2384d2004-08-01 21:59:26 +00001115 }
1116 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001117 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001118}
1119
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001120/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001121 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1122 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1123 * breaking up write requests for only small sparse areas.
1124 */
1125static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1126 int min)
1127{
1128 int ret;
1129 int num_checked, num_used;
1130
1131 if (n < min) {
1132 min = n;
1133 }
1134
1135 ret = is_allocated_sectors(buf, n, pnum);
1136 if (!ret) {
1137 return ret;
1138 }
1139
1140 num_used = *pnum;
1141 buf += BDRV_SECTOR_SIZE * *pnum;
1142 n -= *pnum;
1143 num_checked = num_used;
1144
1145 while (n > 0) {
1146 ret = is_allocated_sectors(buf, n, pnum);
1147
1148 buf += BDRV_SECTOR_SIZE * *pnum;
1149 n -= *pnum;
1150 num_checked += *pnum;
1151 if (ret) {
1152 num_used = num_checked;
1153 } else if (*pnum >= min) {
1154 break;
1155 }
1156 }
1157
1158 *pnum = num_used;
1159 return 1;
1160}
1161
1162/*
Eric Blakedc61cd32017-10-11 22:47:14 -05001163 * Compares two buffers sector by sector. Returns 0 if the first
1164 * sector of each buffer matches, non-zero otherwise.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001165 *
Eric Blakedc61cd32017-10-11 22:47:14 -05001166 * pnum is set to the sector-aligned size of the buffer prefix that
1167 * has the same matching status as the first sector.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001168 */
Eric Blakedc61cd32017-10-11 22:47:14 -05001169static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2,
1170 int64_t bytes, int64_t *pnum)
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001171{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001172 bool res;
Eric Blakedc61cd32017-10-11 22:47:14 -05001173 int64_t i = MIN(bytes, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001174
Eric Blakedc61cd32017-10-11 22:47:14 -05001175 assert(bytes > 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001176
Eric Blakedc61cd32017-10-11 22:47:14 -05001177 res = !!memcmp(buf1, buf2, i);
1178 while (i < bytes) {
1179 int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001180
Eric Blakedc61cd32017-10-11 22:47:14 -05001181 if (!!memcmp(buf1 + i, buf2 + i, len) != res) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001182 break;
1183 }
Eric Blakedc61cd32017-10-11 22:47:14 -05001184 i += len;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001185 }
1186
1187 *pnum = i;
1188 return res;
1189}
1190
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001191#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001192
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001193/*
1194 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1195 *
Eric Blake0608e402017-10-11 22:47:12 -05001196 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1197 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1198 * failure), and 4 on error (the exit status for read errors), after emitting
1199 * an error message.
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001200 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001201 * @param blk: BlockBackend for the image
Eric Blakec41508e2017-10-11 22:47:13 -05001202 * @param offset: Starting offset to check
1203 * @param bytes: Number of bytes to check
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001204 * @param filename: Name of disk file we are checking (logging purpose)
1205 * @param buffer: Allocated buffer for storing read data
1206 * @param quiet: Flag for quiet mode
1207 */
Eric Blakec41508e2017-10-11 22:47:13 -05001208static int check_empty_sectors(BlockBackend *blk, int64_t offset,
1209 int64_t bytes, const char *filename,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001210 uint8_t *buffer, bool quiet)
1211{
Eric Blakedebb38a2017-10-11 22:47:11 -05001212 int ret = 0;
1213 int64_t idx;
1214
Eric Blakec41508e2017-10-11 22:47:13 -05001215 ret = blk_pread(blk, offset, buffer, bytes);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001216 if (ret < 0) {
1217 error_report("Error while reading offset %" PRId64 " of %s: %s",
Eric Blakec41508e2017-10-11 22:47:13 -05001218 offset, filename, strerror(-ret));
Eric Blake0608e402017-10-11 22:47:12 -05001219 return 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001220 }
Eric Blakec41508e2017-10-11 22:47:13 -05001221 idx = find_nonzero(buffer, bytes);
Eric Blakedebb38a2017-10-11 22:47:11 -05001222 if (idx >= 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001223 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blakec41508e2017-10-11 22:47:13 -05001224 offset + idx);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001225 return 1;
1226 }
1227
1228 return 0;
1229}
1230
1231/*
1232 * Compares two images. Exit codes:
1233 *
1234 * 0 - Images are identical
1235 * 1 - Images differ
1236 * >1 - Error occurred
1237 */
1238static int img_compare(int argc, char **argv)
1239{
Max Reitz40055952014-07-22 22:58:42 +02001240 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001241 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001242 BlockDriverState *bs1, *bs2;
Eric Blake033d9fc2017-10-11 22:47:16 -05001243 int64_t total_size1, total_size2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001244 uint8_t *buf1 = NULL, *buf2 = NULL;
Eric Blake31826642017-10-11 22:47:08 -05001245 int64_t pnum1, pnum2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001246 int allocated1, allocated2;
1247 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1248 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001249 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001250 bool writethrough;
Eric Blake033d9fc2017-10-11 22:47:16 -05001251 int64_t total_size;
1252 int64_t offset = 0;
1253 int64_t chunk;
Eric Blakedc61cd32017-10-11 22:47:14 -05001254 int c;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001255 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001256 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001257 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001258
Max Reitz40055952014-07-22 22:58:42 +02001259 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001260 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001261 static const struct option long_options[] = {
1262 {"help", no_argument, 0, 'h'},
1263 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001264 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001265 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001266 {0, 0, 0, 0}
1267 };
Fam Zheng335e9932017-05-03 00:35:39 +08001268 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001269 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001270 if (c == -1) {
1271 break;
1272 }
1273 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001274 case ':':
1275 missing_argument(argv[optind - 1]);
1276 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001277 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001278 unrecognized_option(argv[optind - 1]);
1279 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001280 case 'h':
1281 help();
1282 break;
1283 case 'f':
1284 fmt1 = optarg;
1285 break;
1286 case 'F':
1287 fmt2 = optarg;
1288 break;
Max Reitz40055952014-07-22 22:58:42 +02001289 case 'T':
1290 cache = optarg;
1291 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001292 case 'p':
1293 progress = true;
1294 break;
1295 case 'q':
1296 quiet = true;
1297 break;
1298 case 's':
1299 strict = true;
1300 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001301 case 'U':
1302 force_share = true;
1303 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001304 case OPTION_OBJECT: {
1305 QemuOpts *opts;
1306 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1307 optarg, true);
1308 if (!opts) {
1309 ret = 2;
1310 goto out4;
1311 }
1312 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001313 case OPTION_IMAGE_OPTS:
1314 image_opts = true;
1315 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001316 }
1317 }
1318
1319 /* Progress is not shown in Quiet mode */
1320 if (quiet) {
1321 progress = false;
1322 }
1323
1324
Kevin Wolffc11eb22013-08-05 10:53:04 +02001325 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001326 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001327 }
1328 filename1 = argv[optind++];
1329 filename2 = argv[optind++];
1330
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001331 if (qemu_opts_foreach(&qemu_object_opts,
1332 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001333 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001334 ret = 2;
1335 goto out4;
1336 }
1337
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001338 /* Initialize before goto out */
1339 qemu_progress_init(progress, 2.0);
1340
Kevin Wolfce099542016-03-15 13:01:04 +01001341 flags = 0;
1342 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001343 if (ret < 0) {
1344 error_report("Invalid source cache option: %s", cache);
1345 ret = 2;
1346 goto out3;
1347 }
1348
Fam Zheng335e9932017-05-03 00:35:39 +08001349 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1350 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001351 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001352 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001353 goto out3;
1354 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001355
Fam Zheng335e9932017-05-03 00:35:39 +08001356 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1357 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001358 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001359 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001360 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001361 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001362 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001363 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001364
Max Reitzf1d3cd72015-02-05 13:58:18 -05001365 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1366 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
Eric Blake033d9fc2017-10-11 22:47:16 -05001367 total_size1 = blk_getlength(blk1);
1368 if (total_size1 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001369 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001370 filename1, strerror(-total_size1));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001371 ret = 4;
1372 goto out;
1373 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001374 total_size2 = blk_getlength(blk2);
1375 if (total_size2 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001376 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001377 filename2, strerror(-total_size2));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001378 ret = 4;
1379 goto out;
1380 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001381 total_size = MIN(total_size1, total_size2);
1382 progress_base = MAX(total_size1, total_size2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001383
1384 qemu_progress_print(0, 100);
1385
Eric Blake033d9fc2017-10-11 22:47:16 -05001386 if (strict && total_size1 != total_size2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001387 ret = 1;
1388 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1389 goto out;
1390 }
1391
Eric Blake033d9fc2017-10-11 22:47:16 -05001392 while (offset < total_size) {
Eric Blake31826642017-10-11 22:47:08 -05001393 int status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001394
Eric Blake033d9fc2017-10-11 22:47:16 -05001395 status1 = bdrv_block_status_above(bs1, NULL, offset,
1396 total_size1 - offset, &pnum1, NULL,
1397 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001398 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001399 ret = 3;
1400 error_report("Sector allocation test failed for %s", filename1);
1401 goto out;
1402 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001403 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001404
Eric Blake033d9fc2017-10-11 22:47:16 -05001405 status2 = bdrv_block_status_above(bs2, NULL, offset,
1406 total_size2 - offset, &pnum2, NULL,
1407 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001408 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001409 ret = 3;
1410 error_report("Sector allocation test failed for %s", filename2);
1411 goto out;
1412 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001413 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
Eric Blake7daddc62017-10-11 22:47:09 -05001414
1415 assert(pnum1 && pnum2);
Eric Blake033d9fc2017-10-11 22:47:16 -05001416 chunk = MIN(pnum1, pnum2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001417
Fam Zheng25ad8e62016-01-13 16:37:41 +08001418 if (strict) {
Eric Blake31826642017-10-11 22:47:08 -05001419 if (status1 != status2) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001420 ret = 1;
1421 qprintf(quiet, "Strict mode: Offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001422 " block status mismatch!\n", offset);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001423 goto out;
1424 }
1425 }
1426 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
Eric Blake7daddc62017-10-11 22:47:09 -05001427 /* nothing to do */
Fam Zheng25ad8e62016-01-13 16:37:41 +08001428 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001429 if (allocated1) {
Eric Blakedc61cd32017-10-11 22:47:14 -05001430 int64_t pnum;
1431
Eric Blake033d9fc2017-10-11 22:47:16 -05001432 chunk = MIN(chunk, IO_BUF_SIZE);
1433 ret = blk_pread(blk1, offset, buf1, chunk);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001434 if (ret < 0) {
1435 error_report("Error while reading offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001436 " of %s: %s",
1437 offset, filename1, strerror(-ret));
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001438 ret = 4;
1439 goto out;
1440 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001441 ret = blk_pread(blk2, offset, buf2, chunk);
1442 if (ret < 0) {
1443 error_report("Error while reading offset %" PRId64
1444 " of %s: %s",
1445 offset, filename2, strerror(-ret));
1446 ret = 4;
1447 goto out;
1448 }
1449 ret = compare_buffers(buf1, buf2, chunk, &pnum);
1450 if (ret || pnum != chunk) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001451 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blake033d9fc2017-10-11 22:47:16 -05001452 offset + (ret ? 0 : pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001453 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001454 goto out;
1455 }
1456 }
1457 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001458 chunk = MIN(chunk, IO_BUF_SIZE);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001459 if (allocated1) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001460 ret = check_empty_sectors(blk1, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001461 filename1, buf1, quiet);
1462 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001463 ret = check_empty_sectors(blk2, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001464 filename2, buf1, quiet);
1465 }
1466 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001467 goto out;
1468 }
1469 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001470 offset += chunk;
1471 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001472 }
1473
Eric Blake033d9fc2017-10-11 22:47:16 -05001474 if (total_size1 != total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001475 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001476 const char *filename_over;
1477
1478 qprintf(quiet, "Warning: Image size mismatch!\n");
Eric Blake033d9fc2017-10-11 22:47:16 -05001479 if (total_size1 > total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001480 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001481 filename_over = filename1;
1482 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001483 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001484 filename_over = filename2;
1485 }
1486
Eric Blake033d9fc2017-10-11 22:47:16 -05001487 while (offset < progress_base) {
1488 ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset,
1489 progress_base - offset, &chunk,
1490 NULL, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001491 if (ret < 0) {
1492 ret = 3;
1493 error_report("Sector allocation test failed for %s",
1494 filename_over);
1495 goto out;
1496
1497 }
Eric Blake391cb1a2017-10-11 22:47:10 -05001498 if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001499 chunk = MIN(chunk, IO_BUF_SIZE);
1500 ret = check_empty_sectors(blk_over, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001501 filename_over, buf1, quiet);
1502 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001503 goto out;
1504 }
1505 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001506 offset += chunk;
1507 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001508 }
1509 }
1510
1511 qprintf(quiet, "Images are identical.\n");
1512 ret = 0;
1513
1514out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001515 qemu_vfree(buf1);
1516 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001517 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001518out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001519 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001520out3:
1521 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001522out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001523 return ret;
1524}
1525
Kevin Wolf690c7302015-03-19 13:33:32 +01001526enum ImgConvertBlockStatus {
1527 BLK_DATA,
1528 BLK_ZERO,
1529 BLK_BACKING_FILE,
1530};
1531
Peter Lieven2d9187b2017-02-28 13:40:07 +01001532#define MAX_COROUTINES 16
1533
Kevin Wolf690c7302015-03-19 13:33:32 +01001534typedef struct ImgConvertState {
1535 BlockBackend **src;
1536 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001537 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001538 int64_t total_sectors;
1539 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001540 int64_t allocated_done;
1541 int64_t sector_num;
1542 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001543 enum ImgConvertBlockStatus status;
1544 int64_t sector_next_status;
1545 BlockBackend *target;
1546 bool has_zero_init;
1547 bool compressed;
1548 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001549 bool wr_in_order;
Fam Zhengee5306d2018-06-01 17:26:48 +08001550 bool copy_range;
Kevin Wolf690c7302015-03-19 13:33:32 +01001551 int min_sparse;
1552 size_t cluster_sectors;
1553 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001554 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001555 int running_coroutines;
1556 Coroutine *co[MAX_COROUTINES];
1557 int64_t wait_sector_num[MAX_COROUTINES];
1558 CoMutex lock;
1559 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001560} ImgConvertState;
1561
Peter Lieven2d9187b2017-02-28 13:40:07 +01001562static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1563 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001564{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001565 *src_cur = 0;
1566 *src_cur_offset = 0;
1567 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1568 *src_cur_offset += s->src_sectors[*src_cur];
1569 (*src_cur)++;
1570 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001571 }
1572}
1573
1574static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1575{
Eric Blake31826642017-10-11 22:47:08 -05001576 int64_t src_cur_offset;
1577 int ret, n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001578
Peter Lieven2d9187b2017-02-28 13:40:07 +01001579 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001580
1581 assert(s->total_sectors > sector_num);
1582 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1583
1584 if (s->sector_next_status <= sector_num) {
Eric Blake31826642017-10-11 22:47:08 -05001585 int64_t count = n * BDRV_SECTOR_SIZE;
1586
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001587 if (s->target_has_backing) {
Eric Blake237d78f2017-10-11 22:47:03 -05001588
1589 ret = bdrv_block_status(blk_bs(s->src[src_cur]),
1590 (sector_num - src_cur_offset) *
1591 BDRV_SECTOR_SIZE,
1592 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001593 } else {
Eric Blake31826642017-10-11 22:47:08 -05001594 ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL,
1595 (sector_num - src_cur_offset) *
1596 BDRV_SECTOR_SIZE,
1597 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001598 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001599 if (ret < 0) {
1600 return ret;
1601 }
Eric Blake31826642017-10-11 22:47:08 -05001602 n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
Kevin Wolf690c7302015-03-19 13:33:32 +01001603
1604 if (ret & BDRV_BLOCK_ZERO) {
1605 s->status = BLK_ZERO;
1606 } else if (ret & BDRV_BLOCK_DATA) {
1607 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001608 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001609 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001610 }
1611
1612 s->sector_next_status = sector_num + n;
1613 }
1614
1615 n = MIN(n, s->sector_next_status - sector_num);
1616 if (s->status == BLK_DATA) {
1617 n = MIN(n, s->buf_sectors);
1618 }
1619
1620 /* We need to write complete clusters for compressed images, so if an
1621 * unallocated area is shorter than that, we must consider the whole
1622 * cluster allocated. */
1623 if (s->compressed) {
1624 if (n < s->cluster_sectors) {
1625 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1626 s->status = BLK_DATA;
1627 } else {
1628 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1629 }
1630 }
1631
1632 return n;
1633}
1634
Peter Lieven2d9187b2017-02-28 13:40:07 +01001635static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1636 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001637{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001638 int n, ret;
1639 QEMUIOVector qiov;
1640 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001641
Kevin Wolf690c7302015-03-19 13:33:32 +01001642 assert(nb_sectors <= s->buf_sectors);
1643 while (nb_sectors > 0) {
1644 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001645 int src_cur;
1646 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001647
1648 /* In the case of compression with multiple source files, we can get a
1649 * nb_sectors that spreads into the next part. So we must be able to
1650 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001651 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1652 blk = s->src[src_cur];
1653 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001654
Peter Lieven2d9187b2017-02-28 13:40:07 +01001655 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1656 iov.iov_base = buf;
1657 iov.iov_len = n << BDRV_SECTOR_BITS;
1658 qemu_iovec_init_external(&qiov, &iov, 1);
1659
1660 ret = blk_co_preadv(
1661 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1662 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001663 if (ret < 0) {
1664 return ret;
1665 }
1666
1667 sector_num += n;
1668 nb_sectors -= n;
1669 buf += n * BDRV_SECTOR_SIZE;
1670 }
1671
1672 return 0;
1673}
1674
Peter Lieven2d9187b2017-02-28 13:40:07 +01001675
1676static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1677 int nb_sectors, uint8_t *buf,
1678 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001679{
1680 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001681 QEMUIOVector qiov;
1682 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001683
1684 while (nb_sectors > 0) {
1685 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001686 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1687
Peter Lieven2d9187b2017-02-28 13:40:07 +01001688 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001689 case BLK_BACKING_FILE:
1690 /* If we have a backing file, leave clusters unallocated that are
1691 * unallocated in the source image, so that the backing file is
1692 * visible at the respective offset. */
1693 assert(s->target_has_backing);
1694 break;
1695
1696 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001697 /* If we're told to keep the target fully allocated (-S 0) or there
1698 * is real non-zero data, we must write it. Otherwise we can treat
1699 * it as zero sectors.
1700 * Compressed clusters need to be written as a whole, so in that
1701 * case we can only save the write if the buffer is completely
1702 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001703 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001704 (!s->compressed &&
1705 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1706 (s->compressed &&
1707 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001708 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001709 iov.iov_base = buf;
1710 iov.iov_len = n << BDRV_SECTOR_BITS;
1711 qemu_iovec_init_external(&qiov, &iov, 1);
1712
1713 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
Lidong Chendb933fb2017-04-27 10:58:27 +08001714 n << BDRV_SECTOR_BITS, &qiov, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001715 if (ret < 0) {
1716 return ret;
1717 }
1718 break;
1719 }
1720 /* fall-through */
1721
1722 case BLK_ZERO:
1723 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001724 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001725 break;
1726 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001727 ret = blk_co_pwrite_zeroes(s->target,
1728 sector_num << BDRV_SECTOR_BITS,
1729 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001730 if (ret < 0) {
1731 return ret;
1732 }
1733 break;
1734 }
1735
1736 sector_num += n;
1737 nb_sectors -= n;
1738 buf += n * BDRV_SECTOR_SIZE;
1739 }
1740
1741 return 0;
1742}
1743
Fam Zhengee5306d2018-06-01 17:26:48 +08001744static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num,
1745 int nb_sectors)
1746{
1747 int n, ret;
1748
1749 while (nb_sectors > 0) {
1750 BlockBackend *blk;
1751 int src_cur;
1752 int64_t bs_sectors, src_cur_offset;
1753 int64_t offset;
1754
1755 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1756 offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS;
1757 blk = s->src[src_cur];
1758 bs_sectors = s->src_sectors[src_cur];
1759
1760 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1761
1762 ret = blk_co_copy_range(blk, offset, s->target,
1763 sector_num << BDRV_SECTOR_BITS,
1764 n << BDRV_SECTOR_BITS, 0);
1765 if (ret < 0) {
1766 return ret;
1767 }
1768
1769 sector_num += n;
1770 nb_sectors -= n;
1771 }
1772 return 0;
1773}
1774
Peter Lieven2d9187b2017-02-28 13:40:07 +01001775static void coroutine_fn convert_co_do_copy(void *opaque)
1776{
1777 ImgConvertState *s = opaque;
1778 uint8_t *buf = NULL;
1779 int ret, i;
1780 int index = -1;
1781
1782 for (i = 0; i < s->num_coroutines; i++) {
1783 if (s->co[i] == qemu_coroutine_self()) {
1784 index = i;
1785 break;
1786 }
1787 }
1788 assert(index >= 0);
1789
1790 s->running_coroutines++;
1791 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1792
1793 while (1) {
1794 int n;
1795 int64_t sector_num;
1796 enum ImgConvertBlockStatus status;
Fam Zhengee5306d2018-06-01 17:26:48 +08001797 bool copy_range;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001798
1799 qemu_co_mutex_lock(&s->lock);
1800 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1801 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001802 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001803 }
1804 n = convert_iteration_sectors(s, s->sector_num);
1805 if (n < 0) {
1806 qemu_co_mutex_unlock(&s->lock);
1807 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001808 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001809 }
1810 /* save current sector and allocation status to local variables */
1811 sector_num = s->sector_num;
1812 status = s->status;
1813 if (!s->min_sparse && s->status == BLK_ZERO) {
1814 n = MIN(n, s->buf_sectors);
1815 }
1816 /* increment global sector counter so that other coroutines can
1817 * already continue reading beyond this request */
1818 s->sector_num += n;
1819 qemu_co_mutex_unlock(&s->lock);
1820
1821 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1822 s->allocated_done += n;
1823 qemu_progress_print(100.0 * s->allocated_done /
1824 s->allocated_sectors, 0);
1825 }
1826
Fam Zhengee5306d2018-06-01 17:26:48 +08001827retry:
1828 copy_range = s->copy_range && s->status == BLK_DATA;
1829 if (status == BLK_DATA && !copy_range) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001830 ret = convert_co_read(s, sector_num, n, buf);
1831 if (ret < 0) {
1832 error_report("error while reading sector %" PRId64
1833 ": %s", sector_num, strerror(-ret));
1834 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001835 }
1836 } else if (!s->min_sparse && status == BLK_ZERO) {
1837 status = BLK_DATA;
1838 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1839 }
1840
1841 if (s->wr_in_order) {
1842 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03001843 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001844 s->wait_sector_num[index] = sector_num;
1845 qemu_coroutine_yield();
1846 }
1847 s->wait_sector_num[index] = -1;
1848 }
1849
Anton Nefedovb91127e2017-04-26 11:33:15 +03001850 if (s->ret == -EINPROGRESS) {
Fam Zhengee5306d2018-06-01 17:26:48 +08001851 if (copy_range) {
1852 ret = convert_co_copy_range(s, sector_num, n);
1853 if (ret) {
1854 s->copy_range = false;
1855 goto retry;
1856 }
1857 } else {
1858 ret = convert_co_write(s, sector_num, n, buf, status);
1859 }
Anton Nefedovb91127e2017-04-26 11:33:15 +03001860 if (ret < 0) {
1861 error_report("error while writing sector %" PRId64
1862 ": %s", sector_num, strerror(-ret));
1863 s->ret = ret;
1864 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001865 }
1866
1867 if (s->wr_in_order) {
1868 /* reenter the coroutine that might have waited
1869 * for this write to complete */
1870 s->wr_offs = sector_num + n;
1871 for (i = 0; i < s->num_coroutines; i++) {
1872 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1873 /*
1874 * A -> B -> A cannot occur because A has
1875 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1876 * B will never enter A during this time window.
1877 */
1878 qemu_coroutine_enter(s->co[i]);
1879 break;
1880 }
1881 }
1882 }
1883 }
1884
Peter Lieven2d9187b2017-02-28 13:40:07 +01001885 qemu_vfree(buf);
1886 s->co[index] = NULL;
1887 s->running_coroutines--;
1888 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1889 /* the convert job finished successfully */
1890 s->ret = 0;
1891 }
1892}
1893
Kevin Wolf690c7302015-03-19 13:33:32 +01001894static int convert_do_copy(ImgConvertState *s)
1895{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001896 int ret, i, n;
1897 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001898
1899 /* Check whether we have zero initialisation or can get it efficiently */
1900 s->has_zero_init = s->min_sparse && !s->target_has_backing
1901 ? bdrv_has_zero_init(blk_bs(s->target))
1902 : false;
1903
1904 if (!s->has_zero_init && !s->target_has_backing &&
1905 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1906 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001907 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001908 if (ret == 0) {
1909 s->has_zero_init = true;
1910 }
1911 }
1912
1913 /* Allocate buffer for copied data. For compressed images, only one cluster
1914 * can be copied at a time. */
1915 if (s->compressed) {
1916 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1917 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001918 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001919 }
1920 s->buf_sectors = s->cluster_sectors;
1921 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001922
Kevin Wolf690c7302015-03-19 13:33:32 +01001923 while (sector_num < s->total_sectors) {
1924 n = convert_iteration_sectors(s, sector_num);
1925 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001926 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001927 }
Max Reitzaad15de2016-03-24 23:33:57 +01001928 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1929 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001930 s->allocated_sectors += n;
1931 }
1932 sector_num += n;
1933 }
1934
1935 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001936 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001937 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001938
Peter Lieven2d9187b2017-02-28 13:40:07 +01001939 qemu_co_mutex_init(&s->lock);
1940 for (i = 0; i < s->num_coroutines; i++) {
1941 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1942 s->wait_sector_num[i] = -1;
1943 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001944 }
1945
Anton Nefedovb91127e2017-04-26 11:33:15 +03001946 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001947 main_loop_wait(false);
1948 }
1949
1950 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001951 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001952 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001953 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001954 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001955 }
1956 }
1957
Peter Lieven2d9187b2017-02-28 13:40:07 +01001958 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001959}
1960
bellardea2384d2004-08-01 21:59:26 +00001961static int img_convert(int argc, char **argv)
1962{
Peter Lieven9fd77f92017-04-21 11:11:55 +02001963 int c, bs_i, flags, src_flags = 0;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001964 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02001965 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1966 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001967 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00001968 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001969 BlockDriverState *out_bs;
1970 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08001971 QemuOptsList *create_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001972 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02001973 Error *local_err = NULL;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001974 bool writethrough, src_writethrough, quiet = false, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001975 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001976 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08001977 bool force_share = false;
bellardea2384d2004-08-01 21:59:26 +00001978
Peter Lieven9fd77f92017-04-21 11:11:55 +02001979 ImgConvertState s = (ImgConvertState) {
1980 /* Need at least 4k of zeros for sparse detection */
1981 .min_sparse = 8,
Fam Zhengee5306d2018-06-01 17:26:48 +08001982 .copy_range = true,
Peter Lieven9fd77f92017-04-21 11:11:55 +02001983 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1984 .wr_in_order = true,
1985 .num_coroutines = 8,
1986 };
1987
bellardea2384d2004-08-01 21:59:26 +00001988 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001989 static const struct option long_options[] = {
1990 {"help", no_argument, 0, 'h'},
1991 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001992 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001993 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001994 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001995 {0, 0, 0, 0}
1996 };
Daniel P. Berrange6b4df542017-07-04 13:30:09 +01001997 c = getopt_long(argc, argv, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001998 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001999 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002000 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002001 }
bellardea2384d2004-08-01 21:59:26 +00002002 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002003 case ':':
2004 missing_argument(argv[optind - 1]);
2005 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002006 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002007 unrecognized_option(argv[optind - 1]);
2008 break;
bellardea2384d2004-08-01 21:59:26 +00002009 case 'h':
2010 help();
2011 break;
2012 case 'f':
2013 fmt = optarg;
2014 break;
2015 case 'O':
2016 out_fmt = optarg;
2017 break;
thsf58c7b32008-06-05 21:53:49 +00002018 case 'B':
2019 out_baseimg = optarg;
2020 break;
bellardea2384d2004-08-01 21:59:26 +00002021 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002022 s.compressed = true;
Fam Zhengee5306d2018-06-01 17:26:48 +08002023 s.copy_range = false;
bellardea2384d2004-08-01 21:59:26 +00002024 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002025 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01002026 if (!is_valid_option_list(optarg)) {
2027 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002028 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01002029 }
2030 if (!options) {
2031 options = g_strdup(optarg);
2032 } else {
2033 char *old_options = options;
2034 options = g_strdup_printf("%s,%s", options, optarg);
2035 g_free(old_options);
2036 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002037 break;
edison51ef6722010-09-21 19:58:41 -07002038 case 's':
2039 snapshot_name = optarg;
2040 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08002041 case 'l':
2042 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01002043 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2044 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08002045 if (!sn_opts) {
2046 error_report("Failed in parsing snapshot param '%s'",
2047 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002048 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002049 }
2050 } else {
2051 snapshot_name = optarg;
2052 }
2053 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002054 case 'S':
2055 {
2056 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002057
2058 sval = cvtnum(optarg);
2059 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02002060 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002061 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002062 }
2063
Peter Lieven9fd77f92017-04-21 11:11:55 +02002064 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Fam Zhengee5306d2018-06-01 17:26:48 +08002065 s.copy_range = false;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002066 break;
2067 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002068 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002069 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002070 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002071 case 't':
2072 cache = optarg;
2073 break;
Max Reitz40055952014-07-22 22:58:42 +02002074 case 'T':
2075 src_cache = optarg;
2076 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002077 case 'q':
2078 quiet = true;
2079 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002080 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002081 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002082 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002083 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002084 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2085 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002086 error_report("Invalid number of coroutines. Allowed number of"
2087 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002088 goto fail_getopt;
2089 }
2090 break;
2091 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002092 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002093 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002094 case 'U':
2095 force_share = true;
2096 break;
Max Reitz3258b912017-04-26 15:46:47 +02002097 case OPTION_OBJECT: {
2098 QemuOpts *object_opts;
2099 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2100 optarg, true);
2101 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002102 goto fail_getopt;
2103 }
2104 break;
Max Reitz3258b912017-04-26 15:46:47 +02002105 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002106 case OPTION_IMAGE_OPTS:
2107 image_opts = true;
2108 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002109 case OPTION_TARGET_IMAGE_OPTS:
2110 tgt_image_opts = true;
2111 break;
bellardea2384d2004-08-01 21:59:26 +00002112 }
2113 }
ths3b46e622007-09-17 08:09:54 +00002114
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002115 if (!out_fmt && !tgt_image_opts) {
2116 out_fmt = "raw";
2117 }
2118
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002119 if (qemu_opts_foreach(&qemu_object_opts,
2120 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002121 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002122 goto fail_getopt;
2123 }
2124
Peter Lieven9fd77f92017-04-21 11:11:55 +02002125 if (!s.wr_in_order && s.compressed) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002126 error_report("Out of order write and compress are mutually exclusive");
Peter Lieven9fd77f92017-04-21 11:11:55 +02002127 goto fail_getopt;
2128 }
2129
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002130 if (tgt_image_opts && !skip_create) {
2131 error_report("--target-image-opts requires use of -n flag");
2132 goto fail_getopt;
2133 }
2134
Peter Lieven9fd77f92017-04-21 11:11:55 +02002135 s.src_num = argc - optind - 1;
2136 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2137
2138 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002139 if (out_fmt) {
2140 ret = print_block_option_help(out_filename, out_fmt);
2141 goto fail_getopt;
2142 } else {
2143 error_report("Option help requires a format be specified");
2144 goto fail_getopt;
2145 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002146 }
2147
2148 if (s.src_num < 1) {
2149 error_report("Must specify image file name");
2150 goto fail_getopt;
2151 }
2152
2153
Peter Lieven9fd77f92017-04-21 11:11:55 +02002154 /* ret is still -EINVAL until here */
2155 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2156 if (ret < 0) {
2157 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002158 goto fail_getopt;
2159 }
2160
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002161 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002162 if (quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002163 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002164 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002165 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002166 qemu_progress_print(0, 100);
2167
Peter Lieven9fd77f92017-04-21 11:11:55 +02002168 s.src = g_new0(BlockBackend *, s.src_num);
2169 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002170
Peter Lieven9fd77f92017-04-21 11:11:55 +02002171 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2172 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Fam Zheng335e9932017-05-03 00:35:39 +08002173 fmt, src_flags, src_writethrough, quiet,
2174 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002175 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002176 ret = -1;
2177 goto out;
2178 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002179 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2180 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002181 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002182 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002183 ret = -1;
2184 goto out;
2185 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002186 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002187 }
bellardea2384d2004-08-01 21:59:26 +00002188
Wenchao Xiaef806542013-12-04 17:10:57 +08002189 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002190 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002191 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2192 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2193 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002194 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002195 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002196 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002197 ret = -1;
2198 goto out;
2199 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002200
Peter Lieven9fd77f92017-04-21 11:11:55 +02002201 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2202 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002203 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002204 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002205 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002206 ret = -1;
2207 goto out;
edison51ef6722010-09-21 19:58:41 -07002208 }
2209
Max Reitz2e024cd2015-02-11 09:58:46 -05002210 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002211 /* Find driver and parse its options */
2212 drv = bdrv_find_format(out_fmt);
2213 if (!drv) {
2214 error_report("Unknown file format '%s'", out_fmt);
2215 ret = -1;
2216 goto out;
2217 }
2218
2219 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2220 if (!proto_drv) {
2221 error_report_err(local_err);
2222 ret = -1;
2223 goto out;
2224 }
2225
Max Reitz2e024cd2015-02-11 09:58:46 -05002226 if (!drv->create_opts) {
2227 error_report("Format driver '%s' does not support image creation",
2228 drv->format_name);
2229 ret = -1;
2230 goto out;
2231 }
Max Reitzf75613c2014-12-02 18:32:46 +01002232
Max Reitz2e024cd2015-02-11 09:58:46 -05002233 if (!proto_drv->create_opts) {
2234 error_report("Protocol driver '%s' does not support image creation",
2235 proto_drv->format_name);
2236 ret = -1;
2237 goto out;
2238 }
Max Reitzf75613c2014-12-02 18:32:46 +01002239
Max Reitz2e024cd2015-02-11 09:58:46 -05002240 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2241 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002242
Max Reitz2e024cd2015-02-11 09:58:46 -05002243 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002244 if (options) {
2245 qemu_opts_do_parse(opts, options, NULL, &local_err);
2246 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002247 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002248 ret = -1;
2249 goto out;
2250 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002251 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002252
Peter Lieven9fd77f92017-04-21 11:11:55 +02002253 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002254 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002255 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2256 if (ret < 0) {
2257 goto out;
2258 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002259 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002260
Kevin Wolfa18953f2010-10-14 15:46:04 +02002261 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002262 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002263 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002264 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002265 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002266 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002267
Max Reitz48758a82017-04-26 15:46:48 +02002268 if (s.src_num > 1 && out_baseimg) {
2269 error_report("Having a backing file for the target makes no sense when "
2270 "concatenating multiple input images");
2271 ret = -1;
2272 goto out;
2273 }
2274
Kevin Wolfefa84d42009-05-18 16:42:12 +02002275 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002276 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002277 bool encryption =
2278 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002279 const char *encryptfmt =
2280 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
Chunyan Liu83d05212014-06-05 17:20:51 +08002281 const char *preallocation =
2282 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002283
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002284 if (drv && !drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002285 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002286 ret = -1;
2287 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002288 }
2289
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002290 if (encryption || encryptfmt) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002291 error_report("Compression and encryption not supported at "
2292 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002293 ret = -1;
2294 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002295 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002296
Chunyan Liu83d05212014-06-05 17:20:51 +08002297 if (preallocation
2298 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002299 {
2300 error_report("Compression and preallocation not supported at "
2301 "the same time");
2302 ret = -1;
2303 goto out;
2304 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002305 }
2306
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002307 if (!skip_create) {
2308 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002309 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002310 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002311 error_reportf_err(local_err, "%s: error while converting %s: ",
2312 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002313 goto out;
bellardea2384d2004-08-01 21:59:26 +00002314 }
2315 }
ths3b46e622007-09-17 08:09:54 +00002316
Peter Lieven9fd77f92017-04-21 11:11:55 +02002317 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002318 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002319 if (ret < 0) {
2320 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002321 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002322 }
2323
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002324 if (skip_create) {
2325 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2326 flags, writethrough, quiet, false);
2327 } else {
2328 /* TODO ultimately we should allow --target-image-opts
2329 * to be used even when -n is not given.
2330 * That has to wait for bdrv_create to be improved
2331 * to allow filenames in option syntax
2332 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01002333 s.target = img_open_new_file(out_filename, opts, out_fmt,
2334 flags, writethrough, quiet, false);
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002335 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002336 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002337 ret = -1;
2338 goto out;
2339 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002340 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002341
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002342 if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2343 error_report("Compression not supported for this file format");
2344 ret = -1;
2345 goto out;
2346 }
2347
Eric Blake5def6b82016-06-23 16:37:19 -06002348 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002349 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2350 * as maximum. */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002351 s.buf_sectors = MIN(32768,
2352 MAX(s.buf_sectors,
2353 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2354 out_bs->bl.pdiscard_alignment >>
2355 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002356
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002357 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002358 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002359 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002360 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002361 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002362 ret = -1;
2363 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002364 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002365 error_report("output file is smaller than input file");
2366 ret = -1;
2367 goto out;
2368 }
2369 }
2370
Peter Lieven24f833c2013-11-27 11:07:07 +01002371 ret = bdrv_get_info(out_bs, &bdi);
2372 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002373 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002374 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002375 goto out;
2376 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002377 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002378 s.compressed = s.compressed || bdi.needs_compressed_writes;
2379 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Peter Lieven24f833c2013-11-27 11:07:07 +01002380 }
2381
Peter Lieven9fd77f92017-04-21 11:11:55 +02002382 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002383out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002384 if (!ret) {
2385 qemu_progress_print(100, 0);
2386 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002387 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002388 qemu_opts_del(opts);
2389 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002390 qemu_opts_del(sn_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002391 blk_unref(s.target);
2392 if (s.src) {
2393 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2394 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002395 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002396 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002397 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002398 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002399fail_getopt:
2400 g_free(options);
2401
Peter Lieven9fd77f92017-04-21 11:11:55 +02002402 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002403}
2404
bellard57d1a2b2004-08-03 21:15:11 +00002405
bellardfaea38e2006-08-05 21:31:00 +00002406static void dump_snapshots(BlockDriverState *bs)
2407{
2408 QEMUSnapshotInfo *sn_tab, *sn;
2409 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002410
2411 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2412 if (nb_sns <= 0)
2413 return;
2414 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002415 bdrv_snapshot_dump(fprintf, stdout, NULL);
2416 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002417 for(i = 0; i < nb_sns; i++) {
2418 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002419 bdrv_snapshot_dump(fprintf, stdout, sn);
2420 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002421 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002422 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002423}
2424
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002425static void dump_json_image_info_list(ImageInfoList *list)
2426{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002427 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002428 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002429 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002430
2431 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2432 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002433 str = qobject_to_json_pretty(obj);
2434 assert(str != NULL);
2435 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002436 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002437 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002438 qobject_unref(str);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002439}
2440
Benoît Canetc054b3f2012-09-05 13:09:02 +02002441static void dump_json_image_info(ImageInfo *info)
2442{
Benoît Canetc054b3f2012-09-05 13:09:02 +02002443 QString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002444 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002445 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002446
2447 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2448 visit_complete(v, &obj);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002449 str = qobject_to_json_pretty(obj);
2450 assert(str != NULL);
2451 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002452 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002453 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002454 qobject_unref(str);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002455}
2456
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002457static void dump_human_image_info_list(ImageInfoList *list)
2458{
2459 ImageInfoList *elem;
2460 bool delim = false;
2461
2462 for (elem = list; elem; elem = elem->next) {
2463 if (delim) {
2464 printf("\n");
2465 }
2466 delim = true;
2467
Wenchao Xia5b917042013-05-25 11:09:45 +08002468 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002469 }
2470}
2471
2472static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2473{
2474 return strcmp(a, b) == 0;
2475}
2476
2477/**
2478 * Open an image file chain and return an ImageInfoList
2479 *
2480 * @filename: topmost image filename
2481 * @fmt: topmost image format (may be NULL to autodetect)
2482 * @chain: true - enumerate entire backing file chain
2483 * false - only topmost image file
2484 *
2485 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2486 * image file. If there was an error a message will have been printed to
2487 * stderr.
2488 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002489static ImageInfoList *collect_image_info_list(bool image_opts,
2490 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002491 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002492 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002493{
2494 ImageInfoList *head = NULL;
2495 ImageInfoList **last = &head;
2496 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002497 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002498
2499 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2500
2501 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002502 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002503 BlockDriverState *bs;
2504 ImageInfo *info;
2505 ImageInfoList *elem;
2506
2507 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2508 error_report("Backing file '%s' creates an infinite loop.",
2509 filename);
2510 goto err;
2511 }
2512 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2513
Max Reitzefaa7c42016-03-16 19:54:38 +01002514 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002515 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2516 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002517 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002518 goto err;
2519 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002520 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002521
Wenchao Xia43526ec2013-06-06 12:27:58 +08002522 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002523 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002524 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002525 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002526 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002527 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002528
2529 elem = g_new0(ImageInfoList, 1);
2530 elem->value = info;
2531 *last = elem;
2532 last = &elem->next;
2533
Markus Armbruster26f54e92014-10-07 13:59:04 +02002534 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002535
2536 filename = fmt = NULL;
2537 if (chain) {
2538 if (info->has_full_backing_filename) {
2539 filename = info->full_backing_filename;
2540 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002541 error_report("Could not determine absolute backing filename,"
2542 " but backing filename '%s' present",
2543 info->backing_filename);
2544 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002545 }
2546 if (info->has_backing_filename_format) {
2547 fmt = info->backing_filename_format;
2548 }
2549 }
2550 }
2551 g_hash_table_destroy(filenames);
2552 return head;
2553
2554err:
2555 qapi_free_ImageInfoList(head);
2556 g_hash_table_destroy(filenames);
2557 return NULL;
2558}
2559
Benoît Canetc054b3f2012-09-05 13:09:02 +02002560static int img_info(int argc, char **argv)
2561{
2562 int c;
2563 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002564 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002565 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002566 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002567 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002568 bool force_share = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002569
bellardea2384d2004-08-01 21:59:26 +00002570 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002571 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002572 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002573 int option_index = 0;
2574 static const struct option long_options[] = {
2575 {"help", no_argument, 0, 'h'},
2576 {"format", required_argument, 0, 'f'},
2577 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002578 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002579 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002580 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002581 {"force-share", no_argument, 0, 'U'},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002582 {0, 0, 0, 0}
2583 };
Fam Zheng335e9932017-05-03 00:35:39 +08002584 c = getopt_long(argc, argv, ":f:hU",
Benoît Canetc054b3f2012-09-05 13:09:02 +02002585 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002586 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002587 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002588 }
bellardea2384d2004-08-01 21:59:26 +00002589 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002590 case ':':
2591 missing_argument(argv[optind - 1]);
2592 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002593 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002594 unrecognized_option(argv[optind - 1]);
2595 break;
bellardea2384d2004-08-01 21:59:26 +00002596 case 'h':
2597 help();
2598 break;
2599 case 'f':
2600 fmt = optarg;
2601 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002602 case 'U':
2603 force_share = true;
2604 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002605 case OPTION_OUTPUT:
2606 output = optarg;
2607 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002608 case OPTION_BACKING_CHAIN:
2609 chain = true;
2610 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002611 case OPTION_OBJECT: {
2612 QemuOpts *opts;
2613 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2614 optarg, true);
2615 if (!opts) {
2616 return 1;
2617 }
2618 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002619 case OPTION_IMAGE_OPTS:
2620 image_opts = true;
2621 break;
bellardea2384d2004-08-01 21:59:26 +00002622 }
2623 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002624 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002625 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002626 }
bellardea2384d2004-08-01 21:59:26 +00002627 filename = argv[optind++];
2628
Benoît Canetc054b3f2012-09-05 13:09:02 +02002629 if (output && !strcmp(output, "json")) {
2630 output_format = OFORMAT_JSON;
2631 } else if (output && !strcmp(output, "human")) {
2632 output_format = OFORMAT_HUMAN;
2633 } else if (output) {
2634 error_report("--output must be used with human or json as argument.");
2635 return 1;
2636 }
2637
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002638 if (qemu_opts_foreach(&qemu_object_opts,
2639 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002640 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002641 return 1;
2642 }
2643
Fam Zheng335e9932017-05-03 00:35:39 +08002644 list = collect_image_info_list(image_opts, filename, fmt, chain,
2645 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002646 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002647 return 1;
2648 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002649
Benoît Canetc054b3f2012-09-05 13:09:02 +02002650 switch (output_format) {
2651 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002652 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002653 break;
2654 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002655 if (chain) {
2656 dump_json_image_info_list(list);
2657 } else {
2658 dump_json_image_info(list->value);
2659 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002660 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002661 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002662
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002663 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002664 return 0;
2665}
2666
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002667static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2668 MapEntry *next)
2669{
2670 switch (output_format) {
2671 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002672 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002673 error_report("File contains external, encrypted or compressed clusters.");
2674 exit(1);
2675 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002676 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002677 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002678 e->start, e->length,
2679 e->has_offset ? e->offset : 0,
2680 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002681 }
2682 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2683 * Modify the flags here to allow more coalescing.
2684 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002685 if (next && (!next->data || next->zero)) {
2686 next->data = false;
2687 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002688 }
2689 break;
2690 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002691 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2692 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002693 (e->start == 0 ? "[" : ",\n"),
2694 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002695 e->zero ? "true" : "false",
2696 e->data ? "true" : "false");
2697 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002698 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002699 }
2700 putchar('}');
2701
2702 if (!next) {
2703 printf("]\n");
2704 }
2705 break;
2706 }
2707}
2708
Eric Blake5e344dd2017-10-11 22:47:02 -05002709static int get_block_status(BlockDriverState *bs, int64_t offset,
2710 int64_t bytes, MapEntry *e)
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002711{
Eric Blake237d78f2017-10-11 22:47:03 -05002712 int ret;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002713 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002714 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002715 bool has_offset;
Eric Blake237d78f2017-10-11 22:47:03 -05002716 int64_t map;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002717
2718 /* As an optimization, we could cache the current range of unallocated
2719 * clusters in each file of the chain, and avoid querying the same
2720 * range repeatedly.
2721 */
2722
2723 depth = 0;
2724 for (;;) {
Eric Blake237d78f2017-10-11 22:47:03 -05002725 ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002726 if (ret < 0) {
2727 return ret;
2728 }
Eric Blake237d78f2017-10-11 22:47:03 -05002729 assert(bytes);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002730 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2731 break;
2732 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002733 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002734 if (bs == NULL) {
2735 ret = 0;
2736 break;
2737 }
2738
2739 depth++;
2740 }
2741
John Snow28756452016-02-05 13:12:33 -05002742 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2743
2744 *e = (MapEntry) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002745 .start = offset,
Eric Blake237d78f2017-10-11 22:47:03 -05002746 .length = bytes,
John Snow28756452016-02-05 13:12:33 -05002747 .data = !!(ret & BDRV_BLOCK_DATA),
2748 .zero = !!(ret & BDRV_BLOCK_ZERO),
Eric Blake237d78f2017-10-11 22:47:03 -05002749 .offset = map,
John Snow28756452016-02-05 13:12:33 -05002750 .has_offset = has_offset,
2751 .depth = depth,
2752 .has_filename = file && has_offset,
2753 .filename = file && has_offset ? file->filename : NULL,
2754 };
2755
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002756 return 0;
2757}
2758
Fam Zheng16b0d552016-01-26 11:59:02 +08002759static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2760{
2761 if (curr->length == 0) {
2762 return false;
2763 }
2764 if (curr->zero != next->zero ||
2765 curr->data != next->data ||
2766 curr->depth != next->depth ||
2767 curr->has_filename != next->has_filename ||
2768 curr->has_offset != next->has_offset) {
2769 return false;
2770 }
2771 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2772 return false;
2773 }
2774 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2775 return false;
2776 }
2777 return true;
2778}
2779
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002780static int img_map(int argc, char **argv)
2781{
2782 int c;
2783 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002784 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002785 BlockDriverState *bs;
2786 const char *filename, *fmt, *output;
2787 int64_t length;
2788 MapEntry curr = { .length = 0 }, next;
2789 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002790 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002791 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002792
2793 fmt = NULL;
2794 output = NULL;
2795 for (;;) {
2796 int option_index = 0;
2797 static const struct option long_options[] = {
2798 {"help", no_argument, 0, 'h'},
2799 {"format", required_argument, 0, 'f'},
2800 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002801 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002802 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002803 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002804 {0, 0, 0, 0}
2805 };
Fam Zheng335e9932017-05-03 00:35:39 +08002806 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002807 long_options, &option_index);
2808 if (c == -1) {
2809 break;
2810 }
2811 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002812 case ':':
2813 missing_argument(argv[optind - 1]);
2814 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002815 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002816 unrecognized_option(argv[optind - 1]);
2817 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002818 case 'h':
2819 help();
2820 break;
2821 case 'f':
2822 fmt = optarg;
2823 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002824 case 'U':
2825 force_share = true;
2826 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002827 case OPTION_OUTPUT:
2828 output = optarg;
2829 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002830 case OPTION_OBJECT: {
2831 QemuOpts *opts;
2832 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2833 optarg, true);
2834 if (!opts) {
2835 return 1;
2836 }
2837 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002838 case OPTION_IMAGE_OPTS:
2839 image_opts = true;
2840 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002841 }
2842 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002843 if (optind != argc - 1) {
2844 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002845 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002846 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002847
2848 if (output && !strcmp(output, "json")) {
2849 output_format = OFORMAT_JSON;
2850 } else if (output && !strcmp(output, "human")) {
2851 output_format = OFORMAT_HUMAN;
2852 } else if (output) {
2853 error_report("--output must be used with human or json as argument.");
2854 return 1;
2855 }
2856
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002857 if (qemu_opts_foreach(&qemu_object_opts,
2858 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002859 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002860 return 1;
2861 }
2862
Fam Zheng335e9932017-05-03 00:35:39 +08002863 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002864 if (!blk) {
2865 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002866 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002867 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002868
2869 if (output_format == OFORMAT_HUMAN) {
2870 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2871 }
2872
Max Reitzf1d3cd72015-02-05 13:58:18 -05002873 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002874 while (curr.start + curr.length < length) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002875 int64_t offset = curr.start + curr.length;
2876 int64_t n;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002877
2878 /* Probe up to 1 GiB at a time. */
Eric Blake5e344dd2017-10-11 22:47:02 -05002879 n = QEMU_ALIGN_DOWN(MIN(1 << 30, length - offset), BDRV_SECTOR_SIZE);
2880 ret = get_block_status(bs, offset, n, &next);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002881
2882 if (ret < 0) {
2883 error_report("Could not read file metadata: %s", strerror(-ret));
2884 goto out;
2885 }
2886
Fam Zheng16b0d552016-01-26 11:59:02 +08002887 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002888 curr.length += next.length;
2889 continue;
2890 }
2891
2892 if (curr.length > 0) {
2893 dump_map_entry(output_format, &curr, &next);
2894 }
2895 curr = next;
2896 }
2897
2898 dump_map_entry(output_format, &curr, NULL);
2899
2900out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002901 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002902 return ret < 0;
2903}
2904
aliguorif7b4a942009-01-07 17:40:15 +00002905#define SNAPSHOT_LIST 1
2906#define SNAPSHOT_CREATE 2
2907#define SNAPSHOT_APPLY 3
2908#define SNAPSHOT_DELETE 4
2909
Stuart Brady153859b2009-06-07 00:42:17 +01002910static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002911{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002912 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002913 BlockDriverState *bs;
2914 QEMUSnapshotInfo sn;
2915 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002916 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002917 int action = 0;
2918 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002919 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002920 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002921 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002922 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00002923
Kevin Wolfce099542016-03-15 13:01:04 +01002924 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002925 /* Parse commandline parameters */
2926 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002927 static const struct option long_options[] = {
2928 {"help", no_argument, 0, 'h'},
2929 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002930 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002931 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002932 {0, 0, 0, 0}
2933 };
Fam Zheng335e9932017-05-03 00:35:39 +08002934 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002935 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002936 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002937 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002938 }
aliguorif7b4a942009-01-07 17:40:15 +00002939 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002940 case ':':
2941 missing_argument(argv[optind - 1]);
2942 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002943 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002944 unrecognized_option(argv[optind - 1]);
2945 break;
aliguorif7b4a942009-01-07 17:40:15 +00002946 case 'h':
2947 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002948 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002949 case 'l':
2950 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002951 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002952 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002953 }
2954 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002955 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002956 break;
2957 case 'a':
2958 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002959 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002960 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002961 }
2962 action = SNAPSHOT_APPLY;
2963 snapshot_name = optarg;
2964 break;
2965 case 'c':
2966 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002967 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002968 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002969 }
2970 action = SNAPSHOT_CREATE;
2971 snapshot_name = optarg;
2972 break;
2973 case 'd':
2974 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002975 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002976 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002977 }
2978 action = SNAPSHOT_DELETE;
2979 snapshot_name = optarg;
2980 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002981 case 'q':
2982 quiet = true;
2983 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002984 case 'U':
2985 force_share = true;
2986 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002987 case OPTION_OBJECT: {
2988 QemuOpts *opts;
2989 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2990 optarg, true);
2991 if (!opts) {
2992 return 1;
2993 }
2994 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002995 case OPTION_IMAGE_OPTS:
2996 image_opts = true;
2997 break;
aliguorif7b4a942009-01-07 17:40:15 +00002998 }
2999 }
3000
Kevin Wolffc11eb22013-08-05 10:53:04 +02003001 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003002 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003003 }
aliguorif7b4a942009-01-07 17:40:15 +00003004 filename = argv[optind++];
3005
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003006 if (qemu_opts_foreach(&qemu_object_opts,
3007 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003008 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003009 return 1;
3010 }
3011
aliguorif7b4a942009-01-07 17:40:15 +00003012 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08003013 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
3014 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003015 if (!blk) {
3016 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003017 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003018 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00003019
3020 /* Perform the requested action */
3021 switch(action) {
3022 case SNAPSHOT_LIST:
3023 dump_snapshots(bs);
3024 break;
3025
3026 case SNAPSHOT_CREATE:
3027 memset(&sn, 0, sizeof(sn));
3028 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
3029
3030 qemu_gettimeofday(&tv);
3031 sn.date_sec = tv.tv_sec;
3032 sn.date_nsec = tv.tv_usec * 1000;
3033
3034 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003035 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003036 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003037 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003038 }
aliguorif7b4a942009-01-07 17:40:15 +00003039 break;
3040
3041 case SNAPSHOT_APPLY:
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01003042 ret = bdrv_snapshot_goto(bs, snapshot_name, &err);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003043 if (ret) {
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01003044 error_reportf_err(err, "Could not apply snapshot '%s': ",
3045 snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003046 }
aliguorif7b4a942009-01-07 17:40:15 +00003047 break;
3048
3049 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003050 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003051 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003052 error_reportf_err(err, "Could not delete snapshot '%s': ",
3053 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003054 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003055 }
aliguorif7b4a942009-01-07 17:40:15 +00003056 break;
3057 }
3058
3059 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003060 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003061 if (ret) {
3062 return 1;
3063 }
Stuart Brady153859b2009-06-07 00:42:17 +01003064 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003065}
3066
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003067static int img_rebase(int argc, char **argv)
3068{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003069 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003070 uint8_t *buf_old = NULL;
3071 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05003072 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003073 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003074 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3075 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003076 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003077 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003078 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003079 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003080 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003081 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003082 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003083
3084 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003085 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003086 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003087 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003088 out_baseimg = NULL;
3089 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003090 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003091 static const struct option long_options[] = {
3092 {"help", no_argument, 0, 'h'},
3093 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003094 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003095 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003096 {0, 0, 0, 0}
3097 };
Fam Zheng335e9932017-05-03 00:35:39 +08003098 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003099 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003100 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003101 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003102 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003103 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003104 case ':':
3105 missing_argument(argv[optind - 1]);
3106 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003107 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003108 unrecognized_option(argv[optind - 1]);
3109 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003110 case 'h':
3111 help();
3112 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003113 case 'f':
3114 fmt = optarg;
3115 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003116 case 'F':
3117 out_basefmt = optarg;
3118 break;
3119 case 'b':
3120 out_baseimg = optarg;
3121 break;
3122 case 'u':
3123 unsafe = 1;
3124 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003125 case 'p':
3126 progress = 1;
3127 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003128 case 't':
3129 cache = optarg;
3130 break;
Max Reitz40055952014-07-22 22:58:42 +02003131 case 'T':
3132 src_cache = optarg;
3133 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003134 case 'q':
3135 quiet = true;
3136 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003137 case OPTION_OBJECT: {
3138 QemuOpts *opts;
3139 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3140 optarg, true);
3141 if (!opts) {
3142 return 1;
3143 }
3144 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003145 case OPTION_IMAGE_OPTS:
3146 image_opts = true;
3147 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003148 case 'U':
3149 force_share = true;
3150 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003151 }
3152 }
3153
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003154 if (quiet) {
3155 progress = 0;
3156 }
3157
Fam Zhengac1307a2014-04-22 13:36:11 +08003158 if (optind != argc - 1) {
3159 error_exit("Expecting one image file name");
3160 }
3161 if (!unsafe && !out_baseimg) {
3162 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003163 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003164 filename = argv[optind++];
3165
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003166 if (qemu_opts_foreach(&qemu_object_opts,
3167 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003168 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003169 return 1;
3170 }
3171
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003172 qemu_progress_init(progress, 2.0);
3173 qemu_progress_print(0, 100);
3174
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003175 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003176 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003177 if (ret < 0) {
3178 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003179 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003180 }
3181
Kevin Wolfce099542016-03-15 13:01:04 +01003182 src_flags = 0;
3183 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003184 if (ret < 0) {
3185 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003186 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003187 }
3188
Kevin Wolfce099542016-03-15 13:01:04 +01003189 /* The source files are opened read-only, don't care about WCE */
3190 assert((src_flags & BDRV_O_RDWR) == 0);
3191 (void) src_writethrough;
3192
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003193 /*
3194 * Open the images.
3195 *
3196 * Ignore the old backing file for unsafe rebase in case we want to correct
3197 * the reference to a renamed or moved backing file.
3198 */
Fam Zheng335e9932017-05-03 00:35:39 +08003199 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3200 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003201 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003202 ret = -1;
3203 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003204 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003205 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003206
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003207 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003208 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003209 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003210 ret = -1;
3211 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003212 }
3213 }
3214
3215 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003216 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003217 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003218 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003219
Max Reitz644483d2015-02-05 13:58:17 -05003220 if (bs->backing_format[0] != '\0') {
3221 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003222 qdict_put_str(options, "driver", bs->backing_format);
Max Reitz644483d2015-02-05 13:58:17 -05003223 }
3224
Fam Zheng335e9932017-05-03 00:35:39 +08003225 if (force_share) {
3226 if (!options) {
3227 options = qdict_new();
3228 }
Eric Blake579cf1d2017-05-15 14:54:39 -05003229 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +08003230 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003231 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003232 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003233 options, src_flags, &local_err);
3234 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003235 error_reportf_err(local_err,
3236 "Could not open old backing file '%s': ",
3237 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003238 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003239 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003240 }
Max Reitz644483d2015-02-05 13:58:17 -05003241
Alex Bligha6166732012-10-16 13:46:18 +01003242 if (out_baseimg[0]) {
Fam Zheng335e9932017-05-03 00:35:39 +08003243 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003244 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003245 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003246 }
3247 if (force_share) {
3248 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003249 }
3250
Max Reitzefaa7c42016-03-16 19:54:38 +01003251 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003252 options, src_flags, &local_err);
3253 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003254 error_reportf_err(local_err,
3255 "Could not open new backing file '%s': ",
3256 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003257 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003258 goto out;
3259 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003260 }
3261 }
3262
3263 /*
3264 * Check each unallocated cluster in the COW file. If it is unallocated,
3265 * accesses go to the backing file. We must therefore compare this cluster
3266 * in the old and new backing file, and if they differ we need to copy it
3267 * from the old backing file into the COW file.
3268 *
3269 * If qemu-img crashes during this step, no harm is done. The content of
3270 * the image is the same as the original one at any time.
3271 */
3272 if (!unsafe) {
Eric Blake41536282017-10-11 22:47:15 -05003273 int64_t size;
3274 int64_t old_backing_size;
3275 int64_t new_backing_size = 0;
3276 uint64_t offset;
3277 int64_t n;
Kevin Wolf1f710492012-10-12 14:29:18 +02003278 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003279
Max Reitzf1d3cd72015-02-05 13:58:18 -05003280 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3281 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003282
Eric Blake41536282017-10-11 22:47:15 -05003283 size = blk_getlength(blk);
3284 if (size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003285 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003286 filename, strerror(-size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003287 ret = -1;
3288 goto out;
3289 }
Eric Blake41536282017-10-11 22:47:15 -05003290 old_backing_size = blk_getlength(blk_old_backing);
3291 if (old_backing_size < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003292 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003293
3294 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3295 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003296 backing_name, strerror(-old_backing_size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003297 ret = -1;
3298 goto out;
3299 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003300 if (blk_new_backing) {
Eric Blake41536282017-10-11 22:47:15 -05003301 new_backing_size = blk_getlength(blk_new_backing);
3302 if (new_backing_size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003303 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003304 out_baseimg, strerror(-new_backing_size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003305 ret = -1;
3306 goto out;
3307 }
Alex Bligha6166732012-10-16 13:46:18 +01003308 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003309
Eric Blake41536282017-10-11 22:47:15 -05003310 if (size != 0) {
3311 local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE));
Kevin Wolf1f710492012-10-12 14:29:18 +02003312 }
3313
Eric Blake41536282017-10-11 22:47:15 -05003314 for (offset = 0; offset < size; offset += n) {
3315 /* How many bytes can we handle with the next read? */
3316 n = MIN(IO_BUF_SIZE, size - offset);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003317
3318 /* If the cluster is allocated, we don't need to take action */
Eric Blake41536282017-10-11 22:47:15 -05003319 ret = bdrv_is_allocated(bs, offset, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003320 if (ret < 0) {
3321 error_report("error while reading image metadata: %s",
3322 strerror(-ret));
3323 goto out;
3324 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003325 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003326 continue;
3327 }
3328
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003329 /*
3330 * Read old and new backing file and take into consideration that
3331 * backing files may be smaller than the COW image.
3332 */
Eric Blake41536282017-10-11 22:47:15 -05003333 if (offset >= old_backing_size) {
3334 memset(buf_old, 0, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003335 } else {
Eric Blake41536282017-10-11 22:47:15 -05003336 if (offset + n > old_backing_size) {
3337 n = old_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003338 }
3339
Eric Blake41536282017-10-11 22:47:15 -05003340 ret = blk_pread(blk_old_backing, offset, buf_old, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003341 if (ret < 0) {
3342 error_report("error while reading from old backing file");
3343 goto out;
3344 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003345 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003346
Eric Blake41536282017-10-11 22:47:15 -05003347 if (offset >= new_backing_size || !blk_new_backing) {
3348 memset(buf_new, 0, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003349 } else {
Eric Blake41536282017-10-11 22:47:15 -05003350 if (offset + n > new_backing_size) {
3351 n = new_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003352 }
3353
Eric Blake41536282017-10-11 22:47:15 -05003354 ret = blk_pread(blk_new_backing, offset, buf_new, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003355 if (ret < 0) {
3356 error_report("error while reading from new backing file");
3357 goto out;
3358 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003359 }
3360
3361 /* If they differ, we need to write to the COW file */
3362 uint64_t written = 0;
3363
Eric Blake41536282017-10-11 22:47:15 -05003364 while (written < n) {
Eric Blakedc61cd32017-10-11 22:47:14 -05003365 int64_t pnum;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003366
Eric Blake41536282017-10-11 22:47:15 -05003367 if (compare_buffers(buf_old + written, buf_new + written,
3368 n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003369 {
Eric Blake41536282017-10-11 22:47:15 -05003370 ret = blk_pwrite(blk, offset + written,
Eric Blakedc61cd32017-10-11 22:47:14 -05003371 buf_old + written, pnum, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003372 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003373 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003374 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003375 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003376 }
3377 }
3378
3379 written += pnum;
3380 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003381 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003382 }
3383 }
3384
3385 /*
3386 * Change the backing file. All clusters that are different from the old
3387 * backing file are overwritten in the COW file now, so the visible content
3388 * doesn't change when we switch the backing file.
3389 */
Alex Bligha6166732012-10-16 13:46:18 +01003390 if (out_baseimg && *out_baseimg) {
3391 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3392 } else {
3393 ret = bdrv_change_backing_file(bs, NULL, NULL);
3394 }
3395
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003396 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003397 error_report("Could not change the backing file to '%s': No "
3398 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003399 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003400 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003401 out_baseimg, strerror(-ret));
3402 }
3403
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003404 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003405 /*
3406 * TODO At this point it is possible to check if any clusters that are
3407 * allocated in the COW file are the same in the backing file. If so, they
3408 * could be dropped from the COW file. Don't do this before switching the
3409 * backing file, in case of a crash this would lead to corruption.
3410 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003411out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003412 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003413 /* Cleanup */
3414 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003415 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003416 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003417 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003418 qemu_vfree(buf_old);
3419 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003420
Markus Armbruster26f54e92014-10-07 13:59:04 +02003421 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003422 if (ret) {
3423 return 1;
3424 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003425 return 0;
3426}
3427
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003428static int img_resize(int argc, char **argv)
3429{
Markus Armbruster6750e792015-02-12 17:43:08 +01003430 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003431 int c, ret, relative;
3432 const char *filename, *fmt, *size;
Max Reitz5279b302018-04-21 18:39:57 +02003433 int64_t n, total_size, current_size, new_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003434 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003435 BlockBackend *blk = NULL;
Max Reitzdc5f6902017-06-13 22:20:55 +02003436 PreallocMode prealloc = PREALLOC_MODE_OFF;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003437 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003438
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003439 static QemuOptsList resize_options = {
3440 .name = "resize_options",
3441 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3442 .desc = {
3443 {
3444 .name = BLOCK_OPT_SIZE,
3445 .type = QEMU_OPT_SIZE,
3446 .help = "Virtual disk size"
3447 }, {
3448 /* end of list */
3449 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003450 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003451 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003452 bool image_opts = false;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003453 bool shrink = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003454
Kevin Wolfe80fec72011-04-29 10:58:12 +02003455 /* Remove size from argv manually so that negative numbers are not treated
3456 * as options by getopt. */
3457 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003458 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003459 return 1;
3460 }
3461
3462 size = argv[--argc];
3463
3464 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003465 fmt = NULL;
3466 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003467 static const struct option long_options[] = {
3468 {"help", no_argument, 0, 'h'},
3469 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003470 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Max Reitzdc5f6902017-06-13 22:20:55 +02003471 {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003472 {"shrink", no_argument, 0, OPTION_SHRINK},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003473 {0, 0, 0, 0}
3474 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003475 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003476 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003477 if (c == -1) {
3478 break;
3479 }
3480 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003481 case ':':
3482 missing_argument(argv[optind - 1]);
3483 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003484 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003485 unrecognized_option(argv[optind - 1]);
3486 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003487 case 'h':
3488 help();
3489 break;
3490 case 'f':
3491 fmt = optarg;
3492 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003493 case 'q':
3494 quiet = true;
3495 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003496 case OPTION_OBJECT: {
3497 QemuOpts *opts;
3498 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3499 optarg, true);
3500 if (!opts) {
3501 return 1;
3502 }
3503 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003504 case OPTION_IMAGE_OPTS:
3505 image_opts = true;
3506 break;
Max Reitzdc5f6902017-06-13 22:20:55 +02003507 case OPTION_PREALLOCATION:
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02003508 prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
Markus Armbruster06c60b62017-08-24 10:45:57 +02003509 PREALLOC_MODE__MAX, NULL);
Max Reitzdc5f6902017-06-13 22:20:55 +02003510 if (prealloc == PREALLOC_MODE__MAX) {
3511 error_report("Invalid preallocation mode '%s'", optarg);
3512 return 1;
3513 }
3514 break;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003515 case OPTION_SHRINK:
3516 shrink = true;
3517 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003518 }
3519 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003520 if (optind != argc - 1) {
Max Reitzbe8fbd42018-02-05 17:27:45 +01003521 error_exit("Expecting image file name and size");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003522 }
3523 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003524
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003525 if (qemu_opts_foreach(&qemu_object_opts,
3526 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003527 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003528 return 1;
3529 }
3530
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003531 /* Choose grow, shrink, or absolute resize mode */
3532 switch (size[0]) {
3533 case '+':
3534 relative = 1;
3535 size++;
3536 break;
3537 case '-':
3538 relative = -1;
3539 size++;
3540 break;
3541 default:
3542 relative = 0;
3543 break;
3544 }
3545
3546 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003547 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003548 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003549 if (err) {
3550 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003551 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003552 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003553 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003554 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003555 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3556 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003557
Max Reitzefaa7c42016-03-16 19:54:38 +01003558 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003559 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3560 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003561 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003562 ret = -1;
3563 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003564 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003565
Max Reitzdc5f6902017-06-13 22:20:55 +02003566 current_size = blk_getlength(blk);
3567 if (current_size < 0) {
3568 error_report("Failed to inquire current image length: %s",
3569 strerror(-current_size));
3570 ret = -1;
3571 goto out;
3572 }
3573
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003574 if (relative) {
Max Reitzdc5f6902017-06-13 22:20:55 +02003575 total_size = current_size + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003576 } else {
3577 total_size = n;
3578 }
3579 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003580 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003581 ret = -1;
3582 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003583 }
3584
Max Reitzdc5f6902017-06-13 22:20:55 +02003585 if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
3586 error_report("Preallocation can only be used for growing images");
3587 ret = -1;
3588 goto out;
3589 }
3590
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003591 if (total_size < current_size && !shrink) {
3592 warn_report("Shrinking an image will delete all data beyond the "
3593 "shrunken image's end. Before performing such an "
3594 "operation, make sure there is no important data there.");
3595
3596 if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) {
3597 error_report(
3598 "Use the --shrink option to perform a shrink operation.");
3599 ret = -1;
3600 goto out;
3601 } else {
3602 warn_report("Using the --shrink option will suppress this message. "
3603 "Note that future versions of qemu-img may refuse to "
3604 "shrink images without this option.");
3605 }
3606 }
3607
Max Reitzdc5f6902017-06-13 22:20:55 +02003608 ret = blk_truncate(blk, total_size, prealloc, &err);
Max Reitz5279b302018-04-21 18:39:57 +02003609 if (ret < 0) {
Max Reitzed3d2ec2017-03-28 22:51:27 +02003610 error_report_err(err);
Max Reitz5279b302018-04-21 18:39:57 +02003611 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003612 }
Max Reitz5279b302018-04-21 18:39:57 +02003613
3614 new_size = blk_getlength(blk);
3615 if (new_size < 0) {
3616 error_report("Failed to verify truncated image length: %s",
3617 strerror(-new_size));
3618 ret = -1;
3619 goto out;
3620 }
3621
3622 /* Some block drivers implement a truncation method, but only so
3623 * the user can cause qemu to refresh the image's size from disk.
3624 * The idea is that the user resizes the image outside of qemu and
3625 * then invokes block_resize to inform qemu about it.
3626 * (This includes iscsi and file-posix for device files.)
3627 * Of course, that is not the behavior someone invoking
3628 * qemu-img resize would find useful, so we catch that behavior
3629 * here and tell the user. */
3630 if (new_size != total_size && new_size == current_size) {
3631 error_report("Image was not resized; resizing may not be supported "
3632 "for this image");
3633 ret = -1;
3634 goto out;
3635 }
3636
3637 if (new_size != total_size) {
3638 warn_report("Image should have been resized to %" PRIi64
3639 " bytes, but was resized to %" PRIi64 " bytes",
3640 total_size, new_size);
3641 }
3642
3643 qprintf(quiet, "Image resized.\n");
3644
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003645out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003646 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003647 if (ret) {
3648 return 1;
3649 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003650 return 0;
3651}
3652
Max Reitz76a3a342014-10-27 11:12:51 +01003653static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003654 int64_t offset, int64_t total_work_size,
3655 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003656{
3657 qemu_progress_print(100.f * offset / total_work_size, 0);
3658}
3659
Max Reitz6f176b42013-09-03 10:09:50 +02003660static int img_amend(int argc, char **argv)
3661{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003662 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003663 int c, ret = 0;
3664 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003665 QemuOptsList *create_opts = NULL;
3666 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003667 const char *fmt = NULL, *filename, *cache;
3668 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003669 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003670 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003671 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003672 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003673 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003674
Max Reitzbd39e6e2014-07-22 22:58:43 +02003675 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003676 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003677 static const struct option long_options[] = {
3678 {"help", no_argument, 0, 'h'},
3679 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003680 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003681 {0, 0, 0, 0}
3682 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003683 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003684 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003685 if (c == -1) {
3686 break;
3687 }
3688
3689 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003690 case ':':
3691 missing_argument(argv[optind - 1]);
3692 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003693 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003694 unrecognized_option(argv[optind - 1]);
3695 break;
3696 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003697 help();
3698 break;
3699 case 'o':
3700 if (!is_valid_option_list(optarg)) {
3701 error_report("Invalid option list: %s", optarg);
3702 ret = -1;
3703 goto out_no_progress;
3704 }
3705 if (!options) {
3706 options = g_strdup(optarg);
3707 } else {
3708 char *old_options = options;
3709 options = g_strdup_printf("%s,%s", options, optarg);
3710 g_free(old_options);
3711 }
3712 break;
3713 case 'f':
3714 fmt = optarg;
3715 break;
3716 case 't':
3717 cache = optarg;
3718 break;
3719 case 'p':
3720 progress = true;
3721 break;
3722 case 'q':
3723 quiet = true;
3724 break;
3725 case OPTION_OBJECT:
3726 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3727 optarg, true);
3728 if (!opts) {
3729 ret = -1;
3730 goto out_no_progress;
3731 }
3732 break;
3733 case OPTION_IMAGE_OPTS:
3734 image_opts = true;
3735 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003736 }
3737 }
3738
Max Reitz6f176b42013-09-03 10:09:50 +02003739 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003740 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003741 }
3742
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003743 if (qemu_opts_foreach(&qemu_object_opts,
3744 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003745 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003746 ret = -1;
3747 goto out_no_progress;
3748 }
3749
Max Reitz76a3a342014-10-27 11:12:51 +01003750 if (quiet) {
3751 progress = false;
3752 }
3753 qemu_progress_init(progress, 1.0);
3754
Kevin Wolfa283cb62014-02-21 16:24:07 +01003755 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3756 if (fmt && has_help_option(options)) {
3757 /* If a format is explicitly specified (and possibly no filename is
3758 * given), print option help here */
3759 ret = print_block_option_help(filename, fmt);
3760 goto out;
3761 }
3762
3763 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003764 error_report("Expecting one image file name");
3765 ret = -1;
3766 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003767 }
Max Reitz6f176b42013-09-03 10:09:50 +02003768
Kevin Wolfce099542016-03-15 13:01:04 +01003769 flags = BDRV_O_RDWR;
3770 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003771 if (ret < 0) {
3772 error_report("Invalid cache option: %s", cache);
3773 goto out;
3774 }
3775
Fam Zheng335e9932017-05-03 00:35:39 +08003776 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3777 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003778 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003779 ret = -1;
3780 goto out;
3781 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003782 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003783
3784 fmt = bs->drv->format_name;
3785
Kevin Wolf626f84f2014-02-21 16:24:06 +01003786 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003787 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003788 ret = print_block_option_help(filename, fmt);
3789 goto out;
3790 }
3791
Max Reitzb2439d22014-12-02 18:32:47 +01003792 if (!bs->drv->create_opts) {
3793 error_report("Format driver '%s' does not support any options to amend",
3794 fmt);
3795 ret = -1;
3796 goto out;
3797 }
3798
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003799 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003800 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003801 qemu_opts_do_parse(opts, options, NULL, &err);
3802 if (err) {
3803 error_report_err(err);
3804 ret = -1;
3805 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003806 }
3807
Max Reitz76a3a342014-10-27 11:12:51 +01003808 /* In case the driver does not call amend_status_cb() */
3809 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003810 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003811 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003812 if (ret < 0) {
3813 error_report("Error while amending options: %s", strerror(-ret));
3814 goto out;
3815 }
3816
3817out:
Max Reitz76a3a342014-10-27 11:12:51 +01003818 qemu_progress_end();
3819
Max Reitze814dff2015-08-20 16:00:38 -07003820out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003821 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003822 qemu_opts_del(opts);
3823 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003824 g_free(options);
3825
Max Reitz6f176b42013-09-03 10:09:50 +02003826 if (ret) {
3827 return 1;
3828 }
3829 return 0;
3830}
3831
Kevin Wolfb6133b82014-08-05 14:17:13 +02003832typedef struct BenchData {
3833 BlockBackend *blk;
3834 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003835 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003836 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003837 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003838 int nrreq;
3839 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003840 int flush_interval;
3841 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003842 uint8_t *buf;
3843 QEMUIOVector *qiov;
3844
3845 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003846 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003847 uint64_t offset;
3848} BenchData;
3849
Kevin Wolf55d539c2016-06-03 13:59:41 +02003850static void bench_undrained_flush_cb(void *opaque, int ret)
3851{
3852 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003853 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003854 exit(EXIT_FAILURE);
3855 }
3856}
3857
Kevin Wolfb6133b82014-08-05 14:17:13 +02003858static void bench_cb(void *opaque, int ret)
3859{
3860 BenchData *b = opaque;
3861 BlockAIOCB *acb;
3862
3863 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003864 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003865 exit(EXIT_FAILURE);
3866 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003867
3868 if (b->in_flush) {
3869 /* Just finished a flush with drained queue: Start next requests */
3870 assert(b->in_flight == 0);
3871 b->in_flush = false;
3872 } else if (b->in_flight > 0) {
3873 int remaining = b->n - b->in_flight;
3874
Kevin Wolfb6133b82014-08-05 14:17:13 +02003875 b->n--;
3876 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003877
3878 /* Time for flush? Drain queue if requested, then flush */
3879 if (b->flush_interval && remaining % b->flush_interval == 0) {
3880 if (!b->in_flight || !b->drain_on_flush) {
3881 BlockCompletionFunc *cb;
3882
3883 if (b->drain_on_flush) {
3884 b->in_flush = true;
3885 cb = bench_cb;
3886 } else {
3887 cb = bench_undrained_flush_cb;
3888 }
3889
3890 acb = blk_aio_flush(b->blk, cb, b);
3891 if (!acb) {
3892 error_report("Failed to issue flush request");
3893 exit(EXIT_FAILURE);
3894 }
3895 }
3896 if (b->drain_on_flush) {
3897 return;
3898 }
3899 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003900 }
3901
3902 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003903 int64_t offset = b->offset;
3904 /* blk_aio_* might look for completed I/Os and kick bench_cb
3905 * again, so make sure this operation is counted by in_flight
3906 * and b->offset is ready for the next submission.
3907 */
3908 b->in_flight++;
3909 b->offset += b->step;
3910 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003911 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003912 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003913 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003914 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003915 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003916 if (!acb) {
3917 error_report("Failed to issue request");
3918 exit(EXIT_FAILURE);
3919 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003920 }
3921}
3922
3923static int img_bench(int argc, char **argv)
3924{
3925 int c, ret = 0;
3926 const char *fmt = NULL, *filename;
3927 bool quiet = false;
3928 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003929 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003930 int count = 75000;
3931 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003932 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003933 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003934 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003935 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003936 int flush_interval = 0;
3937 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003938 int64_t image_size;
3939 BlockBackend *blk = NULL;
3940 BenchData data = {};
3941 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003942 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003943 struct timeval t1, t2;
3944 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08003945 bool force_share = false;
Fam Zheng79d46582018-01-16 14:08:58 +08003946 size_t buf_size;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003947
3948 for (;;) {
3949 static const struct option long_options[] = {
3950 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003951 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003952 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003953 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003954 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08003955 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003956 {0, 0, 0, 0}
3957 };
Fam Zheng335e9932017-05-03 00:35:39 +08003958 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003959 if (c == -1) {
3960 break;
3961 }
3962
3963 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003964 case ':':
3965 missing_argument(argv[optind - 1]);
3966 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003967 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003968 unrecognized_option(argv[optind - 1]);
3969 break;
3970 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02003971 help();
3972 break;
3973 case 'c':
3974 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003975 unsigned long res;
3976
3977 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003978 error_report("Invalid request count specified");
3979 return 1;
3980 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003981 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003982 break;
3983 }
3984 case 'd':
3985 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003986 unsigned long res;
3987
3988 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003989 error_report("Invalid queue depth specified");
3990 return 1;
3991 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003992 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003993 break;
3994 }
3995 case 'f':
3996 fmt = optarg;
3997 break;
3998 case 'n':
3999 flags |= BDRV_O_NATIVE_AIO;
4000 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02004001 case 'o':
4002 {
Markus Armbruster606caa02017-02-21 21:14:04 +01004003 offset = cvtnum(optarg);
4004 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02004005 error_report("Invalid offset specified");
4006 return 1;
4007 }
4008 break;
4009 }
4010 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004011 case 'q':
4012 quiet = true;
4013 break;
4014 case 's':
4015 {
4016 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004017
Markus Armbruster606caa02017-02-21 21:14:04 +01004018 sval = cvtnum(optarg);
4019 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02004020 error_report("Invalid buffer size specified");
4021 return 1;
4022 }
4023
4024 bufsize = sval;
4025 break;
4026 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02004027 case 'S':
4028 {
4029 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02004030
Markus Armbruster606caa02017-02-21 21:14:04 +01004031 sval = cvtnum(optarg);
4032 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02004033 error_report("Invalid step size specified");
4034 return 1;
4035 }
4036
4037 step = sval;
4038 break;
4039 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004040 case 't':
4041 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
4042 if (ret < 0) {
4043 error_report("Invalid cache mode");
4044 ret = -1;
4045 goto out;
4046 }
4047 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004048 case 'w':
4049 flags |= BDRV_O_RDWR;
4050 is_write = true;
4051 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004052 case 'U':
4053 force_share = true;
4054 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004055 case OPTION_PATTERN:
4056 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004057 unsigned long res;
4058
4059 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004060 error_report("Invalid pattern byte specified");
4061 return 1;
4062 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004063 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004064 break;
4065 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02004066 case OPTION_FLUSH_INTERVAL:
4067 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004068 unsigned long res;
4069
4070 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004071 error_report("Invalid flush interval specified");
4072 return 1;
4073 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004074 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004075 break;
4076 }
4077 case OPTION_NO_DRAIN:
4078 drain_on_flush = false;
4079 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004080 case OPTION_IMAGE_OPTS:
4081 image_opts = true;
4082 break;
4083 }
4084 }
4085
4086 if (optind != argc - 1) {
4087 error_exit("Expecting one image file name");
4088 }
4089 filename = argv[argc - 1];
4090
Kevin Wolf55d539c2016-06-03 13:59:41 +02004091 if (!is_write && flush_interval) {
4092 error_report("--flush-interval is only available in write tests");
4093 ret = -1;
4094 goto out;
4095 }
4096 if (flush_interval && flush_interval < depth) {
4097 error_report("Flush interval can't be smaller than depth");
4098 ret = -1;
4099 goto out;
4100 }
4101
Fam Zheng335e9932017-05-03 00:35:39 +08004102 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4103 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004104 if (!blk) {
4105 ret = -1;
4106 goto out;
4107 }
4108
4109 image_size = blk_getlength(blk);
4110 if (image_size < 0) {
4111 ret = image_size;
4112 goto out;
4113 }
4114
4115 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004116 .blk = blk,
4117 .image_size = image_size,
4118 .bufsize = bufsize,
4119 .step = step ?: bufsize,
4120 .nrreq = depth,
4121 .n = count,
4122 .offset = offset,
4123 .write = is_write,
4124 .flush_interval = flush_interval,
4125 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004126 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004127 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004128 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004129 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004130 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004131 if (flush_interval) {
4132 printf("Sending flush every %d requests\n", flush_interval);
4133 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004134
Fam Zheng79d46582018-01-16 14:08:58 +08004135 buf_size = data.nrreq * data.bufsize;
4136 data.buf = blk_blockalign(blk, buf_size);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004137 memset(data.buf, pattern, data.nrreq * data.bufsize);
4138
Fam Zheng79d46582018-01-16 14:08:58 +08004139 blk_register_buf(blk, data.buf, buf_size);
4140
Kevin Wolfb6133b82014-08-05 14:17:13 +02004141 data.qiov = g_new(QEMUIOVector, data.nrreq);
4142 for (i = 0; i < data.nrreq; i++) {
4143 qemu_iovec_init(&data.qiov[i], 1);
4144 qemu_iovec_add(&data.qiov[i],
4145 data.buf + i * data.bufsize, data.bufsize);
4146 }
4147
4148 gettimeofday(&t1, NULL);
4149 bench_cb(&data, 0);
4150
4151 while (data.n > 0) {
4152 main_loop_wait(false);
4153 }
4154 gettimeofday(&t2, NULL);
4155
4156 printf("Run completed in %3.3f seconds.\n",
4157 (t2.tv_sec - t1.tv_sec)
4158 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4159
4160out:
Fam Zheng79d46582018-01-16 14:08:58 +08004161 if (data.buf) {
4162 blk_unregister_buf(blk, data.buf);
4163 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004164 qemu_vfree(data.buf);
4165 blk_unref(blk);
4166
4167 if (ret) {
4168 return 1;
4169 }
4170 return 0;
4171}
4172
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004173#define C_BS 01
4174#define C_COUNT 02
4175#define C_IF 04
4176#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004177#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004178
4179struct DdInfo {
4180 unsigned int flags;
4181 int64_t count;
4182};
4183
4184struct DdIo {
4185 int bsz; /* Block size */
4186 char *filename;
4187 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004188 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004189};
4190
4191struct DdOpts {
4192 const char *name;
4193 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4194 unsigned int flag;
4195};
4196
4197static int img_dd_bs(const char *arg,
4198 struct DdIo *in, struct DdIo *out,
4199 struct DdInfo *dd)
4200{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004201 int64_t res;
4202
Markus Armbruster606caa02017-02-21 21:14:04 +01004203 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004204
Markus Armbruster606caa02017-02-21 21:14:04 +01004205 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004206 error_report("invalid number: '%s'", arg);
4207 return 1;
4208 }
4209 in->bsz = out->bsz = res;
4210
4211 return 0;
4212}
4213
4214static int img_dd_count(const char *arg,
4215 struct DdIo *in, struct DdIo *out,
4216 struct DdInfo *dd)
4217{
Markus Armbruster606caa02017-02-21 21:14:04 +01004218 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004219
Markus Armbruster606caa02017-02-21 21:14:04 +01004220 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004221 error_report("invalid number: '%s'", arg);
4222 return 1;
4223 }
4224
4225 return 0;
4226}
4227
4228static int img_dd_if(const char *arg,
4229 struct DdIo *in, struct DdIo *out,
4230 struct DdInfo *dd)
4231{
4232 in->filename = g_strdup(arg);
4233
4234 return 0;
4235}
4236
4237static int img_dd_of(const char *arg,
4238 struct DdIo *in, struct DdIo *out,
4239 struct DdInfo *dd)
4240{
4241 out->filename = g_strdup(arg);
4242
4243 return 0;
4244}
4245
Reda Sallahif7c15532016-08-10 16:16:09 +02004246static int img_dd_skip(const char *arg,
4247 struct DdIo *in, struct DdIo *out,
4248 struct DdInfo *dd)
4249{
Markus Armbruster606caa02017-02-21 21:14:04 +01004250 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004251
Markus Armbruster606caa02017-02-21 21:14:04 +01004252 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004253 error_report("invalid number: '%s'", arg);
4254 return 1;
4255 }
4256
4257 return 0;
4258}
4259
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004260static int img_dd(int argc, char **argv)
4261{
4262 int ret = 0;
4263 char *arg = NULL;
4264 char *tmp;
4265 BlockDriver *drv = NULL, *proto_drv = NULL;
4266 BlockBackend *blk1 = NULL, *blk2 = NULL;
4267 QemuOpts *opts = NULL;
4268 QemuOptsList *create_opts = NULL;
4269 Error *local_err = NULL;
4270 bool image_opts = false;
4271 int c, i;
4272 const char *out_fmt = "raw";
4273 const char *fmt = NULL;
4274 int64_t size = 0;
4275 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004276 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004277 struct DdInfo dd = {
4278 .flags = 0,
4279 .count = 0,
4280 };
4281 struct DdIo in = {
4282 .bsz = 512, /* Block size is by default 512 bytes */
4283 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004284 .buf = NULL,
4285 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004286 };
4287 struct DdIo out = {
4288 .bsz = 512,
4289 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004290 .buf = NULL,
4291 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004292 };
4293
4294 const struct DdOpts options[] = {
4295 { "bs", img_dd_bs, C_BS },
4296 { "count", img_dd_count, C_COUNT },
4297 { "if", img_dd_if, C_IF },
4298 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004299 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004300 { NULL, NULL, 0 }
4301 };
4302 const struct option long_options[] = {
4303 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004304 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004305 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004306 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004307 { 0, 0, 0, 0 }
4308 };
4309
Fam Zheng335e9932017-05-03 00:35:39 +08004310 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004311 if (c == EOF) {
4312 break;
4313 }
4314 switch (c) {
4315 case 'O':
4316 out_fmt = optarg;
4317 break;
4318 case 'f':
4319 fmt = optarg;
4320 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004321 case ':':
4322 missing_argument(argv[optind - 1]);
4323 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004324 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004325 unrecognized_option(argv[optind - 1]);
4326 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004327 case 'h':
4328 help();
4329 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004330 case 'U':
4331 force_share = true;
4332 break;
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004333 case OPTION_OBJECT:
4334 if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004335 ret = -1;
4336 goto out;
4337 }
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004338 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004339 case OPTION_IMAGE_OPTS:
4340 image_opts = true;
4341 break;
4342 }
4343 }
4344
4345 for (i = optind; i < argc; i++) {
4346 int j;
4347 arg = g_strdup(argv[i]);
4348
4349 tmp = strchr(arg, '=');
4350 if (tmp == NULL) {
4351 error_report("unrecognized operand %s", arg);
4352 ret = -1;
4353 goto out;
4354 }
4355
4356 *tmp++ = '\0';
4357
4358 for (j = 0; options[j].name != NULL; j++) {
4359 if (!strcmp(arg, options[j].name)) {
4360 break;
4361 }
4362 }
4363 if (options[j].name == NULL) {
4364 error_report("unrecognized operand %s", arg);
4365 ret = -1;
4366 goto out;
4367 }
4368
4369 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4370 ret = -1;
4371 goto out;
4372 }
4373 dd.flags |= options[j].flag;
4374 g_free(arg);
4375 arg = NULL;
4376 }
4377
4378 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4379 error_report("Must specify both input and output files");
4380 ret = -1;
4381 goto out;
4382 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004383
4384 if (qemu_opts_foreach(&qemu_object_opts,
4385 user_creatable_add_opts_foreach,
4386 NULL, NULL)) {
4387 ret = -1;
4388 goto out;
4389 }
4390
Fam Zheng335e9932017-05-03 00:35:39 +08004391 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4392 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004393
4394 if (!blk1) {
4395 ret = -1;
4396 goto out;
4397 }
4398
4399 drv = bdrv_find_format(out_fmt);
4400 if (!drv) {
4401 error_report("Unknown file format");
4402 ret = -1;
4403 goto out;
4404 }
4405 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4406
4407 if (!proto_drv) {
4408 error_report_err(local_err);
4409 ret = -1;
4410 goto out;
4411 }
4412 if (!drv->create_opts) {
4413 error_report("Format driver '%s' does not support image creation",
4414 drv->format_name);
4415 ret = -1;
4416 goto out;
4417 }
4418 if (!proto_drv->create_opts) {
4419 error_report("Protocol driver '%s' does not support image creation",
4420 proto_drv->format_name);
4421 ret = -1;
4422 goto out;
4423 }
4424 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4425 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4426
4427 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4428
4429 size = blk_getlength(blk1);
4430 if (size < 0) {
4431 error_report("Failed to get size for '%s'", in.filename);
4432 ret = -1;
4433 goto out;
4434 }
4435
4436 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4437 dd.count * in.bsz < size) {
4438 size = dd.count * in.bsz;
4439 }
4440
Reda Sallahif7c15532016-08-10 16:16:09 +02004441 /* Overflow means the specified offset is beyond input image's size */
4442 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4443 size < in.bsz * in.offset)) {
4444 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4445 } else {
4446 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4447 size - in.bsz * in.offset, &error_abort);
4448 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004449
4450 ret = bdrv_create(drv, out.filename, opts, &local_err);
4451 if (ret < 0) {
4452 error_reportf_err(local_err,
4453 "%s: error while creating output image: ",
4454 out.filename);
4455 ret = -1;
4456 goto out;
4457 }
4458
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004459 /* TODO, we can't honour --image-opts for the target,
4460 * since it needs to be given in a format compatible
4461 * with the bdrv_create() call above which does not
4462 * support image-opts style.
4463 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01004464 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004465 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004466
4467 if (!blk2) {
4468 ret = -1;
4469 goto out;
4470 }
4471
Reda Sallahif7c15532016-08-10 16:16:09 +02004472 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4473 size < in.offset * in.bsz)) {
4474 /* We give a warning if the skip option is bigger than the input
4475 * size and create an empty output disk image (i.e. like dd(1)).
4476 */
4477 error_report("%s: cannot skip to specified offset", in.filename);
4478 in_pos = size;
4479 } else {
4480 in_pos = in.offset * in.bsz;
4481 }
4482
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004483 in.buf = g_new(uint8_t, in.bsz);
4484
Reda Sallahif7c15532016-08-10 16:16:09 +02004485 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004486 int in_ret, out_ret;
4487
4488 if (in_pos + in.bsz > size) {
4489 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4490 } else {
4491 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4492 }
4493 if (in_ret < 0) {
4494 error_report("error while reading from input image file: %s",
4495 strerror(-in_ret));
4496 ret = -1;
4497 goto out;
4498 }
4499 in_pos += in_ret;
4500
4501 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4502
4503 if (out_ret < 0) {
4504 error_report("error while writing to output image file: %s",
4505 strerror(-out_ret));
4506 ret = -1;
4507 goto out;
4508 }
4509 out_pos += out_ret;
4510 }
4511
4512out:
4513 g_free(arg);
4514 qemu_opts_del(opts);
4515 qemu_opts_free(create_opts);
4516 blk_unref(blk1);
4517 blk_unref(blk2);
4518 g_free(in.filename);
4519 g_free(out.filename);
4520 g_free(in.buf);
4521 g_free(out.buf);
4522
4523 if (ret) {
4524 return 1;
4525 }
4526 return 0;
4527}
4528
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004529static void dump_json_block_measure_info(BlockMeasureInfo *info)
4530{
4531 QString *str;
4532 QObject *obj;
4533 Visitor *v = qobject_output_visitor_new(&obj);
4534
4535 visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
4536 visit_complete(v, &obj);
4537 str = qobject_to_json_pretty(obj);
4538 assert(str != NULL);
4539 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004540 qobject_unref(obj);
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004541 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004542 qobject_unref(str);
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004543}
4544
4545static int img_measure(int argc, char **argv)
4546{
4547 static const struct option long_options[] = {
4548 {"help", no_argument, 0, 'h'},
4549 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4550 {"object", required_argument, 0, OPTION_OBJECT},
4551 {"output", required_argument, 0, OPTION_OUTPUT},
4552 {"size", required_argument, 0, OPTION_SIZE},
4553 {"force-share", no_argument, 0, 'U'},
4554 {0, 0, 0, 0}
4555 };
4556 OutputFormat output_format = OFORMAT_HUMAN;
4557 BlockBackend *in_blk = NULL;
4558 BlockDriver *drv;
4559 const char *filename = NULL;
4560 const char *fmt = NULL;
4561 const char *out_fmt = "raw";
4562 char *options = NULL;
4563 char *snapshot_name = NULL;
4564 bool force_share = false;
4565 QemuOpts *opts = NULL;
4566 QemuOpts *object_opts = NULL;
4567 QemuOpts *sn_opts = NULL;
4568 QemuOptsList *create_opts = NULL;
4569 bool image_opts = false;
4570 uint64_t img_size = UINT64_MAX;
4571 BlockMeasureInfo *info = NULL;
4572 Error *local_err = NULL;
4573 int ret = 1;
4574 int c;
4575
4576 while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
4577 long_options, NULL)) != -1) {
4578 switch (c) {
4579 case '?':
4580 case 'h':
4581 help();
4582 break;
4583 case 'f':
4584 fmt = optarg;
4585 break;
4586 case 'O':
4587 out_fmt = optarg;
4588 break;
4589 case 'o':
4590 if (!is_valid_option_list(optarg)) {
4591 error_report("Invalid option list: %s", optarg);
4592 goto out;
4593 }
4594 if (!options) {
4595 options = g_strdup(optarg);
4596 } else {
4597 char *old_options = options;
4598 options = g_strdup_printf("%s,%s", options, optarg);
4599 g_free(old_options);
4600 }
4601 break;
4602 case 'l':
4603 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
4604 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
4605 optarg, false);
4606 if (!sn_opts) {
4607 error_report("Failed in parsing snapshot param '%s'",
4608 optarg);
4609 goto out;
4610 }
4611 } else {
4612 snapshot_name = optarg;
4613 }
4614 break;
4615 case 'U':
4616 force_share = true;
4617 break;
4618 case OPTION_OBJECT:
4619 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
4620 optarg, true);
4621 if (!object_opts) {
4622 goto out;
4623 }
4624 break;
4625 case OPTION_IMAGE_OPTS:
4626 image_opts = true;
4627 break;
4628 case OPTION_OUTPUT:
4629 if (!strcmp(optarg, "json")) {
4630 output_format = OFORMAT_JSON;
4631 } else if (!strcmp(optarg, "human")) {
4632 output_format = OFORMAT_HUMAN;
4633 } else {
4634 error_report("--output must be used with human or json "
4635 "as argument.");
4636 goto out;
4637 }
4638 break;
4639 case OPTION_SIZE:
4640 {
4641 int64_t sval;
4642
4643 sval = cvtnum(optarg);
4644 if (sval < 0) {
4645 if (sval == -ERANGE) {
4646 error_report("Image size must be less than 8 EiB!");
4647 } else {
4648 error_report("Invalid image size specified! You may use "
4649 "k, M, G, T, P or E suffixes for ");
4650 error_report("kilobytes, megabytes, gigabytes, terabytes, "
4651 "petabytes and exabytes.");
4652 }
4653 goto out;
4654 }
4655 img_size = (uint64_t)sval;
4656 }
4657 break;
4658 }
4659 }
4660
4661 if (qemu_opts_foreach(&qemu_object_opts,
4662 user_creatable_add_opts_foreach,
4663 NULL, NULL)) {
4664 goto out;
4665 }
4666
4667 if (argc - optind > 1) {
4668 error_report("At most one filename argument is allowed.");
4669 goto out;
4670 } else if (argc - optind == 1) {
4671 filename = argv[optind];
4672 }
4673
4674 if (!filename &&
4675 (object_opts || image_opts || fmt || snapshot_name || sn_opts)) {
4676 error_report("--object, --image-opts, -f, and -l "
4677 "require a filename argument.");
4678 goto out;
4679 }
4680 if (filename && img_size != UINT64_MAX) {
4681 error_report("--size N cannot be used together with a filename.");
4682 goto out;
4683 }
4684 if (!filename && img_size == UINT64_MAX) {
4685 error_report("Either --size N or one filename must be specified.");
4686 goto out;
4687 }
4688
4689 if (filename) {
4690 in_blk = img_open(image_opts, filename, fmt, 0,
4691 false, false, force_share);
4692 if (!in_blk) {
4693 goto out;
4694 }
4695
4696 if (sn_opts) {
4697 bdrv_snapshot_load_tmp(blk_bs(in_blk),
4698 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
4699 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
4700 &local_err);
4701 } else if (snapshot_name != NULL) {
4702 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
4703 snapshot_name, &local_err);
4704 }
4705 if (local_err) {
4706 error_reportf_err(local_err, "Failed to load snapshot: ");
4707 goto out;
4708 }
4709 }
4710
4711 drv = bdrv_find_format(out_fmt);
4712 if (!drv) {
4713 error_report("Unknown file format '%s'", out_fmt);
4714 goto out;
4715 }
4716 if (!drv->create_opts) {
4717 error_report("Format driver '%s' does not support image creation",
4718 drv->format_name);
4719 goto out;
4720 }
4721
4722 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4723 create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
4724 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4725 if (options) {
4726 qemu_opts_do_parse(opts, options, NULL, &local_err);
4727 if (local_err) {
4728 error_report_err(local_err);
4729 error_report("Invalid options for file format '%s'", out_fmt);
4730 goto out;
4731 }
4732 }
4733 if (img_size != UINT64_MAX) {
4734 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
4735 }
4736
4737 info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
4738 if (local_err) {
4739 error_report_err(local_err);
4740 goto out;
4741 }
4742
4743 if (output_format == OFORMAT_HUMAN) {
4744 printf("required size: %" PRIu64 "\n", info->required);
4745 printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
4746 } else {
4747 dump_json_block_measure_info(info);
4748 }
4749
4750 ret = 0;
4751
4752out:
4753 qapi_free_BlockMeasureInfo(info);
4754 qemu_opts_del(object_opts);
4755 qemu_opts_del(opts);
4756 qemu_opts_del(sn_opts);
4757 qemu_opts_free(create_opts);
4758 g_free(options);
4759 blk_unref(in_blk);
4760 return ret;
4761}
Kevin Wolfb6133b82014-08-05 14:17:13 +02004762
Anthony Liguoric227f092009-10-01 16:12:16 -05004763static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004764#define DEF(option, callback, arg_string) \
4765 { option, callback },
4766#include "qemu-img-cmds.h"
4767#undef DEF
Stuart Brady153859b2009-06-07 00:42:17 +01004768 { NULL, NULL, },
4769};
4770
bellardea2384d2004-08-01 21:59:26 +00004771int main(int argc, char **argv)
4772{
Anthony Liguoric227f092009-10-01 16:12:16 -05004773 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004774 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004775 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004776 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004777 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004778 static const struct option long_options[] = {
4779 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004780 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004781 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004782 {0, 0, 0, 0}
4783 };
bellardea2384d2004-08-01 21:59:26 +00004784
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004785#ifdef CONFIG_POSIX
4786 signal(SIGPIPE, SIG_IGN);
4787#endif
4788
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004789 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004790 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004791 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004792
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004793 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004794 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004795 exit(EXIT_FAILURE);
4796 }
4797
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004798 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004799
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004800 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004801 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004802 if (argc < 2) {
4803 error_exit("Not enough arguments");
4804 }
Stuart Brady153859b2009-06-07 00:42:17 +01004805
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004806 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004807 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004808 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004809
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004810 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004811 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004812 case ':':
4813 missing_argument(argv[optind - 1]);
4814 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08004815 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004816 unrecognized_option(argv[optind - 1]);
4817 return 0;
4818 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03004819 help();
4820 return 0;
4821 case 'V':
4822 printf(QEMU_IMG_VERSION);
4823 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004824 case 'T':
4825 g_free(trace_file);
4826 trace_file = trace_opt_parse(optarg);
4827 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004828 }
bellardea2384d2004-08-01 21:59:26 +00004829 }
Stuart Brady153859b2009-06-07 00:42:17 +01004830
Denis V. Lunev10985132016-06-17 17:44:13 +03004831 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004832
Denis V. Lunev10985132016-06-17 17:44:13 +03004833 /* reset getopt_long scanning */
4834 argc -= optind;
4835 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004836 return 0;
4837 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004838 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004839 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004840
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004841 if (!trace_init_backends()) {
4842 exit(1);
4843 }
4844 trace_init_file(trace_file);
4845 qemu_set_log(LOG_TRACE);
4846
Denis V. Lunev10985132016-06-17 17:44:13 +03004847 /* find the command */
4848 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4849 if (!strcmp(cmdname, cmd->name)) {
4850 return cmd->handler(argc, argv);
4851 }
4852 }
Jeff Cody7db16892014-04-25 17:02:32 -04004853
Stuart Brady153859b2009-06-07 00:42:17 +01004854 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004855 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004856}