blob: efa7b755d490fbdc49898fe21995e32f33ceb03a [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 {
866 aio_poll(aio_context, true);
John Snow62547b82015-11-02 18:28:20 -0500867 qemu_progress_print(job->len ?
868 ((float)job->offset / job->len * 100.f) : 0.0f, 0);
Kevin Wolfdbe5e6c2018-04-19 13:04:01 +0200869 } while (!job->ready && !job_is_completed(&job->job));
Max Reitzd4a32382014-10-24 15:57:37 +0200870
Kevin Wolfdbe5e6c2018-04-19 13:04:01 +0200871 if (!job_is_completed(&job->job)) {
Kevin Wolf3d70ff52018-04-24 16:13:52 +0200872 ret = job_complete_sync(&job->job, errp);
sochin.jiang4172a002017-06-15 14:47:33 +0800873 } else {
Kevin Wolf4ad35182018-04-19 17:30:16 +0200874 ret = job->job.ret;
sochin.jiang4172a002017-06-15 14:47:33 +0800875 }
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200876 job_unref(&job->job);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200877 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200878
sochin.jiang4172a002017-06-15 14:47:33 +0800879 /* publish completion progress only when success */
880 if (!ret) {
881 qemu_progress_print(100.f, 0);
882 }
Max Reitzd4a32382014-10-24 15:57:37 +0200883}
884
bellardea2384d2004-08-01 21:59:26 +0000885static int img_commit(int argc, char **argv)
886{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400887 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200888 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200889 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200890 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100891 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200892 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100893 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200894 Error *local_err = NULL;
895 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000896 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200897 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000898
899 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400900 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200901 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000902 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000903 static const struct option long_options[] = {
904 {"help", no_argument, 0, 'h'},
905 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000906 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000907 {0, 0, 0, 0}
908 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800909 c = getopt_long(argc, argv, ":f:ht:b:dpq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000910 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100911 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000912 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100913 }
bellardea2384d2004-08-01 21:59:26 +0000914 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800915 case ':':
916 missing_argument(argv[optind - 1]);
917 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100918 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800919 unrecognized_option(argv[optind - 1]);
920 break;
bellardea2384d2004-08-01 21:59:26 +0000921 case 'h':
922 help();
923 break;
924 case 'f':
925 fmt = optarg;
926 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400927 case 't':
928 cache = optarg;
929 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200930 case 'b':
931 base = optarg;
932 /* -b implies -d */
933 drop = true;
934 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200935 case 'd':
936 drop = true;
937 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200938 case 'p':
939 progress = true;
940 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100941 case 'q':
942 quiet = true;
943 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000944 case OPTION_OBJECT: {
945 QemuOpts *opts;
946 opts = qemu_opts_parse_noisily(&qemu_object_opts,
947 optarg, true);
948 if (!opts) {
949 return 1;
950 }
951 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000952 case OPTION_IMAGE_OPTS:
953 image_opts = true;
954 break;
bellardea2384d2004-08-01 21:59:26 +0000955 }
956 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200957
958 /* Progress is not shown in Quiet mode */
959 if (quiet) {
960 progress = false;
961 }
962
Kevin Wolffc11eb22013-08-05 10:53:04 +0200963 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800964 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100965 }
bellardea2384d2004-08-01 21:59:26 +0000966 filename = argv[optind++];
967
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000968 if (qemu_opts_foreach(&qemu_object_opts,
969 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200970 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000971 return 1;
972 }
973
Max Reitz9a86fe42014-10-24 15:57:38 +0200974 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100975 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400976 if (ret < 0) {
977 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100978 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400979 }
980
Fam Zheng335e9932017-05-03 00:35:39 +0800981 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
982 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200983 if (!blk) {
984 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900985 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200986 bs = blk_bs(blk);
987
Max Reitz687fa1d2014-10-24 15:57:39 +0200988 qemu_progress_init(progress, 1.f);
989 qemu_progress_print(0.f, 100);
990
Max Reitz1b22bff2014-10-24 15:57:40 +0200991 if (base) {
992 base_bs = bdrv_find_backing_image(bs, base);
993 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +0100994 error_setg(&local_err,
995 "Did not find '%s' in the backing chain of '%s'",
996 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +0200997 goto done;
998 }
999 } else {
1000 /* This is different from QMP, which by default uses the deepest file in
1001 * the backing chain (i.e., the very base); however, the traditional
1002 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +02001003 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +02001004 if (!base_bs) {
1005 error_setg(&local_err, "Image does not have a backing file");
1006 goto done;
1007 }
bellardea2384d2004-08-01 21:59:26 +00001008 }
1009
Max Reitzd4a32382014-10-24 15:57:37 +02001010 cbi = (CommonBlockJobCBInfo){
1011 .errp = &local_err,
1012 .bs = bs,
1013 };
1014
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001015 aio_context = bdrv_get_aio_context(bs);
1016 aio_context_acquire(aio_context);
Kevin Wolfbb02b652018-04-19 17:54:56 +02001017 commit_active_start("commit", bs, base_bs, JOB_DEFAULT, 0,
Kevin Wolf0db832f2017-02-20 18:10:05 +01001018 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +08001019 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001020 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +02001021 if (local_err) {
1022 goto done;
1023 }
1024
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001025 /* When the block job completes, the BlockBackend reference will point to
1026 * the old backing file. In order to avoid that the top image is already
1027 * deleted, so we can still empty it afterwards, increment the reference
1028 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +02001029 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001030 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001031 }
1032
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001033 job = block_job_get("commit");
1034 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001035 if (local_err) {
1036 goto unref_backing;
1037 }
1038
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001039 if (!drop && bs->drv->bdrv_make_empty) {
1040 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001041 if (ret) {
1042 error_setg_errno(&local_err, -ret, "Could not empty %s",
1043 filename);
1044 goto unref_backing;
1045 }
1046 }
1047
1048unref_backing:
1049 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001050 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001051 }
Max Reitzd4a32382014-10-24 15:57:37 +02001052
1053done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001054 qemu_progress_end();
1055
Markus Armbruster26f54e92014-10-07 13:59:04 +02001056 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001057
1058 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001059 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001060 return 1;
1061 }
Max Reitzd4a32382014-10-24 15:57:37 +02001062
1063 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001064 return 0;
1065}
1066
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001067/*
Eric Blakedebb38a2017-10-11 22:47:11 -05001068 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1069 * of the first sector boundary within buf where the sector contains a
1070 * non-zero byte. This function is robust to a buffer that is not
1071 * sector-aligned.
1072 */
1073static int64_t find_nonzero(const uint8_t *buf, int64_t n)
1074{
1075 int64_t i;
1076 int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE);
1077
1078 for (i = 0; i < end; i += BDRV_SECTOR_SIZE) {
1079 if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) {
1080 return i;
1081 }
1082 }
1083 if (i < n && !buffer_is_zero(buf + i, n - end)) {
1084 return i;
1085 }
1086 return -1;
1087}
1088
1089/*
thsf58c7b32008-06-05 21:53:49 +00001090 * Returns true iff the first sector pointed to by 'buf' contains at least
1091 * a non-NUL byte.
1092 *
1093 * 'pnum' is set to the number of sectors (including and immediately following
1094 * the first one) that are known to be in the same allocated/unallocated state.
1095 */
bellardea2384d2004-08-01 21:59:26 +00001096static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1097{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001098 bool is_zero;
1099 int i;
bellardea2384d2004-08-01 21:59:26 +00001100
1101 if (n <= 0) {
1102 *pnum = 0;
1103 return 0;
1104 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001105 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001106 for(i = 1; i < n; i++) {
1107 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001108 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001109 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001110 }
bellardea2384d2004-08-01 21:59:26 +00001111 }
1112 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001113 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001114}
1115
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001116/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001117 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1118 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1119 * breaking up write requests for only small sparse areas.
1120 */
1121static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1122 int min)
1123{
1124 int ret;
1125 int num_checked, num_used;
1126
1127 if (n < min) {
1128 min = n;
1129 }
1130
1131 ret = is_allocated_sectors(buf, n, pnum);
1132 if (!ret) {
1133 return ret;
1134 }
1135
1136 num_used = *pnum;
1137 buf += BDRV_SECTOR_SIZE * *pnum;
1138 n -= *pnum;
1139 num_checked = num_used;
1140
1141 while (n > 0) {
1142 ret = is_allocated_sectors(buf, n, pnum);
1143
1144 buf += BDRV_SECTOR_SIZE * *pnum;
1145 n -= *pnum;
1146 num_checked += *pnum;
1147 if (ret) {
1148 num_used = num_checked;
1149 } else if (*pnum >= min) {
1150 break;
1151 }
1152 }
1153
1154 *pnum = num_used;
1155 return 1;
1156}
1157
1158/*
Eric Blakedc61cd32017-10-11 22:47:14 -05001159 * Compares two buffers sector by sector. Returns 0 if the first
1160 * sector of each buffer matches, non-zero otherwise.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001161 *
Eric Blakedc61cd32017-10-11 22:47:14 -05001162 * pnum is set to the sector-aligned size of the buffer prefix that
1163 * has the same matching status as the first sector.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001164 */
Eric Blakedc61cd32017-10-11 22:47:14 -05001165static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2,
1166 int64_t bytes, int64_t *pnum)
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001167{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001168 bool res;
Eric Blakedc61cd32017-10-11 22:47:14 -05001169 int64_t i = MIN(bytes, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001170
Eric Blakedc61cd32017-10-11 22:47:14 -05001171 assert(bytes > 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001172
Eric Blakedc61cd32017-10-11 22:47:14 -05001173 res = !!memcmp(buf1, buf2, i);
1174 while (i < bytes) {
1175 int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001176
Eric Blakedc61cd32017-10-11 22:47:14 -05001177 if (!!memcmp(buf1 + i, buf2 + i, len) != res) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001178 break;
1179 }
Eric Blakedc61cd32017-10-11 22:47:14 -05001180 i += len;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001181 }
1182
1183 *pnum = i;
1184 return res;
1185}
1186
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001187#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001188
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001189/*
1190 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1191 *
Eric Blake0608e402017-10-11 22:47:12 -05001192 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1193 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1194 * failure), and 4 on error (the exit status for read errors), after emitting
1195 * an error message.
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001196 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001197 * @param blk: BlockBackend for the image
Eric Blakec41508e2017-10-11 22:47:13 -05001198 * @param offset: Starting offset to check
1199 * @param bytes: Number of bytes to check
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001200 * @param filename: Name of disk file we are checking (logging purpose)
1201 * @param buffer: Allocated buffer for storing read data
1202 * @param quiet: Flag for quiet mode
1203 */
Eric Blakec41508e2017-10-11 22:47:13 -05001204static int check_empty_sectors(BlockBackend *blk, int64_t offset,
1205 int64_t bytes, const char *filename,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001206 uint8_t *buffer, bool quiet)
1207{
Eric Blakedebb38a2017-10-11 22:47:11 -05001208 int ret = 0;
1209 int64_t idx;
1210
Eric Blakec41508e2017-10-11 22:47:13 -05001211 ret = blk_pread(blk, offset, buffer, bytes);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001212 if (ret < 0) {
1213 error_report("Error while reading offset %" PRId64 " of %s: %s",
Eric Blakec41508e2017-10-11 22:47:13 -05001214 offset, filename, strerror(-ret));
Eric Blake0608e402017-10-11 22:47:12 -05001215 return 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001216 }
Eric Blakec41508e2017-10-11 22:47:13 -05001217 idx = find_nonzero(buffer, bytes);
Eric Blakedebb38a2017-10-11 22:47:11 -05001218 if (idx >= 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001219 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blakec41508e2017-10-11 22:47:13 -05001220 offset + idx);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001221 return 1;
1222 }
1223
1224 return 0;
1225}
1226
1227/*
1228 * Compares two images. Exit codes:
1229 *
1230 * 0 - Images are identical
1231 * 1 - Images differ
1232 * >1 - Error occurred
1233 */
1234static int img_compare(int argc, char **argv)
1235{
Max Reitz40055952014-07-22 22:58:42 +02001236 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001237 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001238 BlockDriverState *bs1, *bs2;
Eric Blake033d9fc2017-10-11 22:47:16 -05001239 int64_t total_size1, total_size2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001240 uint8_t *buf1 = NULL, *buf2 = NULL;
Eric Blake31826642017-10-11 22:47:08 -05001241 int64_t pnum1, pnum2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001242 int allocated1, allocated2;
1243 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1244 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001245 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001246 bool writethrough;
Eric Blake033d9fc2017-10-11 22:47:16 -05001247 int64_t total_size;
1248 int64_t offset = 0;
1249 int64_t chunk;
Eric Blakedc61cd32017-10-11 22:47:14 -05001250 int c;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001251 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001252 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001253 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001254
Max Reitz40055952014-07-22 22:58:42 +02001255 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001256 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001257 static const struct option long_options[] = {
1258 {"help", no_argument, 0, 'h'},
1259 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001260 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001261 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001262 {0, 0, 0, 0}
1263 };
Fam Zheng335e9932017-05-03 00:35:39 +08001264 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001265 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001266 if (c == -1) {
1267 break;
1268 }
1269 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001270 case ':':
1271 missing_argument(argv[optind - 1]);
1272 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001273 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001274 unrecognized_option(argv[optind - 1]);
1275 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001276 case 'h':
1277 help();
1278 break;
1279 case 'f':
1280 fmt1 = optarg;
1281 break;
1282 case 'F':
1283 fmt2 = optarg;
1284 break;
Max Reitz40055952014-07-22 22:58:42 +02001285 case 'T':
1286 cache = optarg;
1287 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001288 case 'p':
1289 progress = true;
1290 break;
1291 case 'q':
1292 quiet = true;
1293 break;
1294 case 's':
1295 strict = true;
1296 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001297 case 'U':
1298 force_share = true;
1299 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001300 case OPTION_OBJECT: {
1301 QemuOpts *opts;
1302 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1303 optarg, true);
1304 if (!opts) {
1305 ret = 2;
1306 goto out4;
1307 }
1308 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001309 case OPTION_IMAGE_OPTS:
1310 image_opts = true;
1311 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001312 }
1313 }
1314
1315 /* Progress is not shown in Quiet mode */
1316 if (quiet) {
1317 progress = false;
1318 }
1319
1320
Kevin Wolffc11eb22013-08-05 10:53:04 +02001321 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001322 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001323 }
1324 filename1 = argv[optind++];
1325 filename2 = argv[optind++];
1326
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001327 if (qemu_opts_foreach(&qemu_object_opts,
1328 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001329 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001330 ret = 2;
1331 goto out4;
1332 }
1333
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001334 /* Initialize before goto out */
1335 qemu_progress_init(progress, 2.0);
1336
Kevin Wolfce099542016-03-15 13:01:04 +01001337 flags = 0;
1338 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001339 if (ret < 0) {
1340 error_report("Invalid source cache option: %s", cache);
1341 ret = 2;
1342 goto out3;
1343 }
1344
Fam Zheng335e9932017-05-03 00:35:39 +08001345 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1346 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001347 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001348 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001349 goto out3;
1350 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001351
Fam Zheng335e9932017-05-03 00:35:39 +08001352 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1353 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001354 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001355 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001356 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001357 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001358 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001359 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001360
Max Reitzf1d3cd72015-02-05 13:58:18 -05001361 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1362 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
Eric Blake033d9fc2017-10-11 22:47:16 -05001363 total_size1 = blk_getlength(blk1);
1364 if (total_size1 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001365 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001366 filename1, strerror(-total_size1));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001367 ret = 4;
1368 goto out;
1369 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001370 total_size2 = blk_getlength(blk2);
1371 if (total_size2 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001372 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001373 filename2, strerror(-total_size2));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001374 ret = 4;
1375 goto out;
1376 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001377 total_size = MIN(total_size1, total_size2);
1378 progress_base = MAX(total_size1, total_size2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001379
1380 qemu_progress_print(0, 100);
1381
Eric Blake033d9fc2017-10-11 22:47:16 -05001382 if (strict && total_size1 != total_size2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001383 ret = 1;
1384 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1385 goto out;
1386 }
1387
Eric Blake033d9fc2017-10-11 22:47:16 -05001388 while (offset < total_size) {
Eric Blake31826642017-10-11 22:47:08 -05001389 int status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001390
Eric Blake033d9fc2017-10-11 22:47:16 -05001391 status1 = bdrv_block_status_above(bs1, NULL, offset,
1392 total_size1 - offset, &pnum1, NULL,
1393 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001394 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001395 ret = 3;
1396 error_report("Sector allocation test failed for %s", filename1);
1397 goto out;
1398 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001399 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001400
Eric Blake033d9fc2017-10-11 22:47:16 -05001401 status2 = bdrv_block_status_above(bs2, NULL, offset,
1402 total_size2 - offset, &pnum2, NULL,
1403 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001404 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001405 ret = 3;
1406 error_report("Sector allocation test failed for %s", filename2);
1407 goto out;
1408 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001409 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
Eric Blake7daddc62017-10-11 22:47:09 -05001410
1411 assert(pnum1 && pnum2);
Eric Blake033d9fc2017-10-11 22:47:16 -05001412 chunk = MIN(pnum1, pnum2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001413
Fam Zheng25ad8e62016-01-13 16:37:41 +08001414 if (strict) {
Eric Blake31826642017-10-11 22:47:08 -05001415 if (status1 != status2) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001416 ret = 1;
1417 qprintf(quiet, "Strict mode: Offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001418 " block status mismatch!\n", offset);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001419 goto out;
1420 }
1421 }
1422 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
Eric Blake7daddc62017-10-11 22:47:09 -05001423 /* nothing to do */
Fam Zheng25ad8e62016-01-13 16:37:41 +08001424 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001425 if (allocated1) {
Eric Blakedc61cd32017-10-11 22:47:14 -05001426 int64_t pnum;
1427
Eric Blake033d9fc2017-10-11 22:47:16 -05001428 chunk = MIN(chunk, IO_BUF_SIZE);
1429 ret = blk_pread(blk1, offset, buf1, chunk);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001430 if (ret < 0) {
1431 error_report("Error while reading offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001432 " of %s: %s",
1433 offset, filename1, strerror(-ret));
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001434 ret = 4;
1435 goto out;
1436 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001437 ret = blk_pread(blk2, offset, buf2, chunk);
1438 if (ret < 0) {
1439 error_report("Error while reading offset %" PRId64
1440 " of %s: %s",
1441 offset, filename2, strerror(-ret));
1442 ret = 4;
1443 goto out;
1444 }
1445 ret = compare_buffers(buf1, buf2, chunk, &pnum);
1446 if (ret || pnum != chunk) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001447 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blake033d9fc2017-10-11 22:47:16 -05001448 offset + (ret ? 0 : pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001449 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001450 goto out;
1451 }
1452 }
1453 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001454 chunk = MIN(chunk, IO_BUF_SIZE);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001455 if (allocated1) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001456 ret = check_empty_sectors(blk1, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001457 filename1, buf1, quiet);
1458 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001459 ret = check_empty_sectors(blk2, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001460 filename2, buf1, quiet);
1461 }
1462 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001463 goto out;
1464 }
1465 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001466 offset += chunk;
1467 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001468 }
1469
Eric Blake033d9fc2017-10-11 22:47:16 -05001470 if (total_size1 != total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001471 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001472 const char *filename_over;
1473
1474 qprintf(quiet, "Warning: Image size mismatch!\n");
Eric Blake033d9fc2017-10-11 22:47:16 -05001475 if (total_size1 > total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001476 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001477 filename_over = filename1;
1478 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001479 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001480 filename_over = filename2;
1481 }
1482
Eric Blake033d9fc2017-10-11 22:47:16 -05001483 while (offset < progress_base) {
1484 ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset,
1485 progress_base - offset, &chunk,
1486 NULL, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001487 if (ret < 0) {
1488 ret = 3;
1489 error_report("Sector allocation test failed for %s",
1490 filename_over);
1491 goto out;
1492
1493 }
Eric Blake391cb1a2017-10-11 22:47:10 -05001494 if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001495 chunk = MIN(chunk, IO_BUF_SIZE);
1496 ret = check_empty_sectors(blk_over, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001497 filename_over, buf1, quiet);
1498 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001499 goto out;
1500 }
1501 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001502 offset += chunk;
1503 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001504 }
1505 }
1506
1507 qprintf(quiet, "Images are identical.\n");
1508 ret = 0;
1509
1510out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001511 qemu_vfree(buf1);
1512 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001513 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001514out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001515 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001516out3:
1517 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001518out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001519 return ret;
1520}
1521
Kevin Wolf690c7302015-03-19 13:33:32 +01001522enum ImgConvertBlockStatus {
1523 BLK_DATA,
1524 BLK_ZERO,
1525 BLK_BACKING_FILE,
1526};
1527
Peter Lieven2d9187b2017-02-28 13:40:07 +01001528#define MAX_COROUTINES 16
1529
Kevin Wolf690c7302015-03-19 13:33:32 +01001530typedef struct ImgConvertState {
1531 BlockBackend **src;
1532 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001533 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001534 int64_t total_sectors;
1535 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001536 int64_t allocated_done;
1537 int64_t sector_num;
1538 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001539 enum ImgConvertBlockStatus status;
1540 int64_t sector_next_status;
1541 BlockBackend *target;
1542 bool has_zero_init;
1543 bool compressed;
1544 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001545 bool wr_in_order;
Kevin Wolf690c7302015-03-19 13:33:32 +01001546 int min_sparse;
1547 size_t cluster_sectors;
1548 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001549 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001550 int running_coroutines;
1551 Coroutine *co[MAX_COROUTINES];
1552 int64_t wait_sector_num[MAX_COROUTINES];
1553 CoMutex lock;
1554 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001555} ImgConvertState;
1556
Peter Lieven2d9187b2017-02-28 13:40:07 +01001557static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1558 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001559{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001560 *src_cur = 0;
1561 *src_cur_offset = 0;
1562 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1563 *src_cur_offset += s->src_sectors[*src_cur];
1564 (*src_cur)++;
1565 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001566 }
1567}
1568
1569static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1570{
Eric Blake31826642017-10-11 22:47:08 -05001571 int64_t src_cur_offset;
1572 int ret, n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001573
Peter Lieven2d9187b2017-02-28 13:40:07 +01001574 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001575
1576 assert(s->total_sectors > sector_num);
1577 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1578
1579 if (s->sector_next_status <= sector_num) {
Eric Blake31826642017-10-11 22:47:08 -05001580 int64_t count = n * BDRV_SECTOR_SIZE;
1581
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001582 if (s->target_has_backing) {
Eric Blake237d78f2017-10-11 22:47:03 -05001583
1584 ret = bdrv_block_status(blk_bs(s->src[src_cur]),
1585 (sector_num - src_cur_offset) *
1586 BDRV_SECTOR_SIZE,
1587 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001588 } else {
Eric Blake31826642017-10-11 22:47:08 -05001589 ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL,
1590 (sector_num - src_cur_offset) *
1591 BDRV_SECTOR_SIZE,
1592 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001593 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001594 if (ret < 0) {
1595 return ret;
1596 }
Eric Blake31826642017-10-11 22:47:08 -05001597 n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
Kevin Wolf690c7302015-03-19 13:33:32 +01001598
1599 if (ret & BDRV_BLOCK_ZERO) {
1600 s->status = BLK_ZERO;
1601 } else if (ret & BDRV_BLOCK_DATA) {
1602 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001603 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001604 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001605 }
1606
1607 s->sector_next_status = sector_num + n;
1608 }
1609
1610 n = MIN(n, s->sector_next_status - sector_num);
1611 if (s->status == BLK_DATA) {
1612 n = MIN(n, s->buf_sectors);
1613 }
1614
1615 /* We need to write complete clusters for compressed images, so if an
1616 * unallocated area is shorter than that, we must consider the whole
1617 * cluster allocated. */
1618 if (s->compressed) {
1619 if (n < s->cluster_sectors) {
1620 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1621 s->status = BLK_DATA;
1622 } else {
1623 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1624 }
1625 }
1626
1627 return n;
1628}
1629
Peter Lieven2d9187b2017-02-28 13:40:07 +01001630static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1631 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001632{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001633 int n, ret;
1634 QEMUIOVector qiov;
1635 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001636
Kevin Wolf690c7302015-03-19 13:33:32 +01001637 assert(nb_sectors <= s->buf_sectors);
1638 while (nb_sectors > 0) {
1639 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001640 int src_cur;
1641 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001642
1643 /* In the case of compression with multiple source files, we can get a
1644 * nb_sectors that spreads into the next part. So we must be able to
1645 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001646 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1647 blk = s->src[src_cur];
1648 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001649
Peter Lieven2d9187b2017-02-28 13:40:07 +01001650 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1651 iov.iov_base = buf;
1652 iov.iov_len = n << BDRV_SECTOR_BITS;
1653 qemu_iovec_init_external(&qiov, &iov, 1);
1654
1655 ret = blk_co_preadv(
1656 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1657 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001658 if (ret < 0) {
1659 return ret;
1660 }
1661
1662 sector_num += n;
1663 nb_sectors -= n;
1664 buf += n * BDRV_SECTOR_SIZE;
1665 }
1666
1667 return 0;
1668}
1669
Peter Lieven2d9187b2017-02-28 13:40:07 +01001670
1671static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1672 int nb_sectors, uint8_t *buf,
1673 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001674{
1675 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001676 QEMUIOVector qiov;
1677 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001678
1679 while (nb_sectors > 0) {
1680 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001681 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1682
Peter Lieven2d9187b2017-02-28 13:40:07 +01001683 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001684 case BLK_BACKING_FILE:
1685 /* If we have a backing file, leave clusters unallocated that are
1686 * unallocated in the source image, so that the backing file is
1687 * visible at the respective offset. */
1688 assert(s->target_has_backing);
1689 break;
1690
1691 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001692 /* If we're told to keep the target fully allocated (-S 0) or there
1693 * is real non-zero data, we must write it. Otherwise we can treat
1694 * it as zero sectors.
1695 * Compressed clusters need to be written as a whole, so in that
1696 * case we can only save the write if the buffer is completely
1697 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001698 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001699 (!s->compressed &&
1700 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1701 (s->compressed &&
1702 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001703 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001704 iov.iov_base = buf;
1705 iov.iov_len = n << BDRV_SECTOR_BITS;
1706 qemu_iovec_init_external(&qiov, &iov, 1);
1707
1708 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
Lidong Chendb933fb2017-04-27 10:58:27 +08001709 n << BDRV_SECTOR_BITS, &qiov, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001710 if (ret < 0) {
1711 return ret;
1712 }
1713 break;
1714 }
1715 /* fall-through */
1716
1717 case BLK_ZERO:
1718 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001719 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001720 break;
1721 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001722 ret = blk_co_pwrite_zeroes(s->target,
1723 sector_num << BDRV_SECTOR_BITS,
1724 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001725 if (ret < 0) {
1726 return ret;
1727 }
1728 break;
1729 }
1730
1731 sector_num += n;
1732 nb_sectors -= n;
1733 buf += n * BDRV_SECTOR_SIZE;
1734 }
1735
1736 return 0;
1737}
1738
Peter Lieven2d9187b2017-02-28 13:40:07 +01001739static void coroutine_fn convert_co_do_copy(void *opaque)
1740{
1741 ImgConvertState *s = opaque;
1742 uint8_t *buf = NULL;
1743 int ret, i;
1744 int index = -1;
1745
1746 for (i = 0; i < s->num_coroutines; i++) {
1747 if (s->co[i] == qemu_coroutine_self()) {
1748 index = i;
1749 break;
1750 }
1751 }
1752 assert(index >= 0);
1753
1754 s->running_coroutines++;
1755 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1756
1757 while (1) {
1758 int n;
1759 int64_t sector_num;
1760 enum ImgConvertBlockStatus status;
1761
1762 qemu_co_mutex_lock(&s->lock);
1763 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1764 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001765 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001766 }
1767 n = convert_iteration_sectors(s, s->sector_num);
1768 if (n < 0) {
1769 qemu_co_mutex_unlock(&s->lock);
1770 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001771 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001772 }
1773 /* save current sector and allocation status to local variables */
1774 sector_num = s->sector_num;
1775 status = s->status;
1776 if (!s->min_sparse && s->status == BLK_ZERO) {
1777 n = MIN(n, s->buf_sectors);
1778 }
1779 /* increment global sector counter so that other coroutines can
1780 * already continue reading beyond this request */
1781 s->sector_num += n;
1782 qemu_co_mutex_unlock(&s->lock);
1783
1784 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1785 s->allocated_done += n;
1786 qemu_progress_print(100.0 * s->allocated_done /
1787 s->allocated_sectors, 0);
1788 }
1789
1790 if (status == BLK_DATA) {
1791 ret = convert_co_read(s, sector_num, n, buf);
1792 if (ret < 0) {
1793 error_report("error while reading sector %" PRId64
1794 ": %s", sector_num, strerror(-ret));
1795 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001796 }
1797 } else if (!s->min_sparse && status == BLK_ZERO) {
1798 status = BLK_DATA;
1799 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1800 }
1801
1802 if (s->wr_in_order) {
1803 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03001804 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001805 s->wait_sector_num[index] = sector_num;
1806 qemu_coroutine_yield();
1807 }
1808 s->wait_sector_num[index] = -1;
1809 }
1810
Anton Nefedovb91127e2017-04-26 11:33:15 +03001811 if (s->ret == -EINPROGRESS) {
1812 ret = convert_co_write(s, sector_num, n, buf, status);
1813 if (ret < 0) {
1814 error_report("error while writing sector %" PRId64
1815 ": %s", sector_num, strerror(-ret));
1816 s->ret = ret;
1817 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001818 }
1819
1820 if (s->wr_in_order) {
1821 /* reenter the coroutine that might have waited
1822 * for this write to complete */
1823 s->wr_offs = sector_num + n;
1824 for (i = 0; i < s->num_coroutines; i++) {
1825 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1826 /*
1827 * A -> B -> A cannot occur because A has
1828 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1829 * B will never enter A during this time window.
1830 */
1831 qemu_coroutine_enter(s->co[i]);
1832 break;
1833 }
1834 }
1835 }
1836 }
1837
Peter Lieven2d9187b2017-02-28 13:40:07 +01001838 qemu_vfree(buf);
1839 s->co[index] = NULL;
1840 s->running_coroutines--;
1841 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1842 /* the convert job finished successfully */
1843 s->ret = 0;
1844 }
1845}
1846
Kevin Wolf690c7302015-03-19 13:33:32 +01001847static int convert_do_copy(ImgConvertState *s)
1848{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001849 int ret, i, n;
1850 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001851
1852 /* Check whether we have zero initialisation or can get it efficiently */
1853 s->has_zero_init = s->min_sparse && !s->target_has_backing
1854 ? bdrv_has_zero_init(blk_bs(s->target))
1855 : false;
1856
1857 if (!s->has_zero_init && !s->target_has_backing &&
1858 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1859 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001860 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001861 if (ret == 0) {
1862 s->has_zero_init = true;
1863 }
1864 }
1865
1866 /* Allocate buffer for copied data. For compressed images, only one cluster
1867 * can be copied at a time. */
1868 if (s->compressed) {
1869 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1870 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001871 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001872 }
1873 s->buf_sectors = s->cluster_sectors;
1874 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001875
Kevin Wolf690c7302015-03-19 13:33:32 +01001876 while (sector_num < s->total_sectors) {
1877 n = convert_iteration_sectors(s, sector_num);
1878 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001879 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001880 }
Max Reitzaad15de2016-03-24 23:33:57 +01001881 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1882 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001883 s->allocated_sectors += n;
1884 }
1885 sector_num += n;
1886 }
1887
1888 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001889 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001890 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001891
Peter Lieven2d9187b2017-02-28 13:40:07 +01001892 qemu_co_mutex_init(&s->lock);
1893 for (i = 0; i < s->num_coroutines; i++) {
1894 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1895 s->wait_sector_num[i] = -1;
1896 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001897 }
1898
Anton Nefedovb91127e2017-04-26 11:33:15 +03001899 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001900 main_loop_wait(false);
1901 }
1902
1903 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001904 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001905 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001906 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001907 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001908 }
1909 }
1910
Peter Lieven2d9187b2017-02-28 13:40:07 +01001911 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001912}
1913
bellardea2384d2004-08-01 21:59:26 +00001914static int img_convert(int argc, char **argv)
1915{
Peter Lieven9fd77f92017-04-21 11:11:55 +02001916 int c, bs_i, flags, src_flags = 0;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001917 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02001918 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1919 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001920 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00001921 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001922 BlockDriverState *out_bs;
1923 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08001924 QemuOptsList *create_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001925 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02001926 Error *local_err = NULL;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001927 bool writethrough, src_writethrough, quiet = false, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001928 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001929 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08001930 bool force_share = false;
bellardea2384d2004-08-01 21:59:26 +00001931
Peter Lieven9fd77f92017-04-21 11:11:55 +02001932 ImgConvertState s = (ImgConvertState) {
1933 /* Need at least 4k of zeros for sparse detection */
1934 .min_sparse = 8,
1935 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1936 .wr_in_order = true,
1937 .num_coroutines = 8,
1938 };
1939
bellardea2384d2004-08-01 21:59:26 +00001940 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001941 static const struct option long_options[] = {
1942 {"help", no_argument, 0, 'h'},
1943 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001944 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001945 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001946 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001947 {0, 0, 0, 0}
1948 };
Daniel P. Berrange6b4df542017-07-04 13:30:09 +01001949 c = getopt_long(argc, argv, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001950 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001951 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001952 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001953 }
bellardea2384d2004-08-01 21:59:26 +00001954 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001955 case ':':
1956 missing_argument(argv[optind - 1]);
1957 break;
Jes Sorensenef873942010-12-06 15:25:40 +01001958 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001959 unrecognized_option(argv[optind - 1]);
1960 break;
bellardea2384d2004-08-01 21:59:26 +00001961 case 'h':
1962 help();
1963 break;
1964 case 'f':
1965 fmt = optarg;
1966 break;
1967 case 'O':
1968 out_fmt = optarg;
1969 break;
thsf58c7b32008-06-05 21:53:49 +00001970 case 'B':
1971 out_baseimg = optarg;
1972 break;
bellardea2384d2004-08-01 21:59:26 +00001973 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02001974 s.compressed = true;
bellardea2384d2004-08-01 21:59:26 +00001975 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001976 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001977 if (!is_valid_option_list(optarg)) {
1978 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001979 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001980 }
1981 if (!options) {
1982 options = g_strdup(optarg);
1983 } else {
1984 char *old_options = options;
1985 options = g_strdup_printf("%s,%s", options, optarg);
1986 g_free(old_options);
1987 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001988 break;
edison51ef6722010-09-21 19:58:41 -07001989 case 's':
1990 snapshot_name = optarg;
1991 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001992 case 'l':
1993 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01001994 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
1995 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08001996 if (!sn_opts) {
1997 error_report("Failed in parsing snapshot param '%s'",
1998 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001999 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002000 }
2001 } else {
2002 snapshot_name = optarg;
2003 }
2004 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002005 case 'S':
2006 {
2007 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002008
2009 sval = cvtnum(optarg);
2010 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02002011 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002012 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002013 }
2014
Peter Lieven9fd77f92017-04-21 11:11:55 +02002015 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002016 break;
2017 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002018 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002019 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002020 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002021 case 't':
2022 cache = optarg;
2023 break;
Max Reitz40055952014-07-22 22:58:42 +02002024 case 'T':
2025 src_cache = optarg;
2026 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002027 case 'q':
2028 quiet = true;
2029 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002030 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002031 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002032 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002033 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002034 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2035 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002036 error_report("Invalid number of coroutines. Allowed number of"
2037 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002038 goto fail_getopt;
2039 }
2040 break;
2041 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002042 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002043 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002044 case 'U':
2045 force_share = true;
2046 break;
Max Reitz3258b912017-04-26 15:46:47 +02002047 case OPTION_OBJECT: {
2048 QemuOpts *object_opts;
2049 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2050 optarg, true);
2051 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002052 goto fail_getopt;
2053 }
2054 break;
Max Reitz3258b912017-04-26 15:46:47 +02002055 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002056 case OPTION_IMAGE_OPTS:
2057 image_opts = true;
2058 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002059 case OPTION_TARGET_IMAGE_OPTS:
2060 tgt_image_opts = true;
2061 break;
bellardea2384d2004-08-01 21:59:26 +00002062 }
2063 }
ths3b46e622007-09-17 08:09:54 +00002064
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002065 if (!out_fmt && !tgt_image_opts) {
2066 out_fmt = "raw";
2067 }
2068
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002069 if (qemu_opts_foreach(&qemu_object_opts,
2070 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002071 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002072 goto fail_getopt;
2073 }
2074
Peter Lieven9fd77f92017-04-21 11:11:55 +02002075 if (!s.wr_in_order && s.compressed) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002076 error_report("Out of order write and compress are mutually exclusive");
Peter Lieven9fd77f92017-04-21 11:11:55 +02002077 goto fail_getopt;
2078 }
2079
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002080 if (tgt_image_opts && !skip_create) {
2081 error_report("--target-image-opts requires use of -n flag");
2082 goto fail_getopt;
2083 }
2084
Peter Lieven9fd77f92017-04-21 11:11:55 +02002085 s.src_num = argc - optind - 1;
2086 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2087
2088 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002089 if (out_fmt) {
2090 ret = print_block_option_help(out_filename, out_fmt);
2091 goto fail_getopt;
2092 } else {
2093 error_report("Option help requires a format be specified");
2094 goto fail_getopt;
2095 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002096 }
2097
2098 if (s.src_num < 1) {
2099 error_report("Must specify image file name");
2100 goto fail_getopt;
2101 }
2102
2103
Peter Lieven9fd77f92017-04-21 11:11:55 +02002104 /* ret is still -EINVAL until here */
2105 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2106 if (ret < 0) {
2107 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002108 goto fail_getopt;
2109 }
2110
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002111 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002112 if (quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002113 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002114 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002115 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002116 qemu_progress_print(0, 100);
2117
Peter Lieven9fd77f92017-04-21 11:11:55 +02002118 s.src = g_new0(BlockBackend *, s.src_num);
2119 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002120
Peter Lieven9fd77f92017-04-21 11:11:55 +02002121 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2122 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Fam Zheng335e9932017-05-03 00:35:39 +08002123 fmt, src_flags, src_writethrough, quiet,
2124 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002125 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002126 ret = -1;
2127 goto out;
2128 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002129 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2130 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002131 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002132 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002133 ret = -1;
2134 goto out;
2135 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002136 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002137 }
bellardea2384d2004-08-01 21:59:26 +00002138
Wenchao Xiaef806542013-12-04 17:10:57 +08002139 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002140 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002141 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2142 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2143 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002144 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002145 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002146 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002147 ret = -1;
2148 goto out;
2149 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002150
Peter Lieven9fd77f92017-04-21 11:11:55 +02002151 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2152 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002153 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002154 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002155 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002156 ret = -1;
2157 goto out;
edison51ef6722010-09-21 19:58:41 -07002158 }
2159
Max Reitz2e024cd2015-02-11 09:58:46 -05002160 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002161 /* Find driver and parse its options */
2162 drv = bdrv_find_format(out_fmt);
2163 if (!drv) {
2164 error_report("Unknown file format '%s'", out_fmt);
2165 ret = -1;
2166 goto out;
2167 }
2168
2169 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2170 if (!proto_drv) {
2171 error_report_err(local_err);
2172 ret = -1;
2173 goto out;
2174 }
2175
Max Reitz2e024cd2015-02-11 09:58:46 -05002176 if (!drv->create_opts) {
2177 error_report("Format driver '%s' does not support image creation",
2178 drv->format_name);
2179 ret = -1;
2180 goto out;
2181 }
Max Reitzf75613c2014-12-02 18:32:46 +01002182
Max Reitz2e024cd2015-02-11 09:58:46 -05002183 if (!proto_drv->create_opts) {
2184 error_report("Protocol driver '%s' does not support image creation",
2185 proto_drv->format_name);
2186 ret = -1;
2187 goto out;
2188 }
Max Reitzf75613c2014-12-02 18:32:46 +01002189
Max Reitz2e024cd2015-02-11 09:58:46 -05002190 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2191 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002192
Max Reitz2e024cd2015-02-11 09:58:46 -05002193 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002194 if (options) {
2195 qemu_opts_do_parse(opts, options, NULL, &local_err);
2196 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002197 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002198 ret = -1;
2199 goto out;
2200 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002201 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002202
Peter Lieven9fd77f92017-04-21 11:11:55 +02002203 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002204 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002205 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2206 if (ret < 0) {
2207 goto out;
2208 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002209 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002210
Kevin Wolfa18953f2010-10-14 15:46:04 +02002211 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002212 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002213 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002214 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002215 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002216 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002217
Max Reitz48758a82017-04-26 15:46:48 +02002218 if (s.src_num > 1 && out_baseimg) {
2219 error_report("Having a backing file for the target makes no sense when "
2220 "concatenating multiple input images");
2221 ret = -1;
2222 goto out;
2223 }
2224
Kevin Wolfefa84d42009-05-18 16:42:12 +02002225 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002226 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002227 bool encryption =
2228 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002229 const char *encryptfmt =
2230 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
Chunyan Liu83d05212014-06-05 17:20:51 +08002231 const char *preallocation =
2232 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002233
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002234 if (drv && !drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002235 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002236 ret = -1;
2237 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002238 }
2239
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002240 if (encryption || encryptfmt) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002241 error_report("Compression and encryption not supported at "
2242 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002243 ret = -1;
2244 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002245 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002246
Chunyan Liu83d05212014-06-05 17:20:51 +08002247 if (preallocation
2248 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002249 {
2250 error_report("Compression and preallocation not supported at "
2251 "the same time");
2252 ret = -1;
2253 goto out;
2254 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002255 }
2256
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002257 if (!skip_create) {
2258 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002259 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002260 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002261 error_reportf_err(local_err, "%s: error while converting %s: ",
2262 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002263 goto out;
bellardea2384d2004-08-01 21:59:26 +00002264 }
2265 }
ths3b46e622007-09-17 08:09:54 +00002266
Peter Lieven9fd77f92017-04-21 11:11:55 +02002267 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002268 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002269 if (ret < 0) {
2270 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002271 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002272 }
2273
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002274 if (skip_create) {
2275 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2276 flags, writethrough, quiet, false);
2277 } else {
2278 /* TODO ultimately we should allow --target-image-opts
2279 * to be used even when -n is not given.
2280 * That has to wait for bdrv_create to be improved
2281 * to allow filenames in option syntax
2282 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01002283 s.target = img_open_new_file(out_filename, opts, out_fmt,
2284 flags, writethrough, quiet, false);
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002285 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002286 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002287 ret = -1;
2288 goto out;
2289 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002290 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002291
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002292 if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2293 error_report("Compression not supported for this file format");
2294 ret = -1;
2295 goto out;
2296 }
2297
Eric Blake5def6b82016-06-23 16:37:19 -06002298 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002299 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2300 * as maximum. */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002301 s.buf_sectors = MIN(32768,
2302 MAX(s.buf_sectors,
2303 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2304 out_bs->bl.pdiscard_alignment >>
2305 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002306
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002307 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002308 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002309 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002310 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002311 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002312 ret = -1;
2313 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002314 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002315 error_report("output file is smaller than input file");
2316 ret = -1;
2317 goto out;
2318 }
2319 }
2320
Peter Lieven24f833c2013-11-27 11:07:07 +01002321 ret = bdrv_get_info(out_bs, &bdi);
2322 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002323 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002324 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002325 goto out;
2326 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002327 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002328 s.compressed = s.compressed || bdi.needs_compressed_writes;
2329 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Peter Lieven24f833c2013-11-27 11:07:07 +01002330 }
2331
Peter Lieven9fd77f92017-04-21 11:11:55 +02002332 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002333out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002334 if (!ret) {
2335 qemu_progress_print(100, 0);
2336 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002337 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002338 qemu_opts_del(opts);
2339 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002340 qemu_opts_del(sn_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002341 blk_unref(s.target);
2342 if (s.src) {
2343 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2344 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002345 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002346 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002347 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002348 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002349fail_getopt:
2350 g_free(options);
2351
Peter Lieven9fd77f92017-04-21 11:11:55 +02002352 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002353}
2354
bellard57d1a2b2004-08-03 21:15:11 +00002355
bellardfaea38e2006-08-05 21:31:00 +00002356static void dump_snapshots(BlockDriverState *bs)
2357{
2358 QEMUSnapshotInfo *sn_tab, *sn;
2359 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002360
2361 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2362 if (nb_sns <= 0)
2363 return;
2364 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002365 bdrv_snapshot_dump(fprintf, stdout, NULL);
2366 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002367 for(i = 0; i < nb_sns; i++) {
2368 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002369 bdrv_snapshot_dump(fprintf, stdout, sn);
2370 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002371 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002372 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002373}
2374
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002375static void dump_json_image_info_list(ImageInfoList *list)
2376{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002377 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002378 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002379 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002380
2381 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2382 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002383 str = qobject_to_json_pretty(obj);
2384 assert(str != NULL);
2385 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002386 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002387 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002388 qobject_unref(str);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002389}
2390
Benoît Canetc054b3f2012-09-05 13:09:02 +02002391static void dump_json_image_info(ImageInfo *info)
2392{
Benoît Canetc054b3f2012-09-05 13:09:02 +02002393 QString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002394 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002395 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002396
2397 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2398 visit_complete(v, &obj);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002399 str = qobject_to_json_pretty(obj);
2400 assert(str != NULL);
2401 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002402 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002403 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002404 qobject_unref(str);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002405}
2406
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002407static void dump_human_image_info_list(ImageInfoList *list)
2408{
2409 ImageInfoList *elem;
2410 bool delim = false;
2411
2412 for (elem = list; elem; elem = elem->next) {
2413 if (delim) {
2414 printf("\n");
2415 }
2416 delim = true;
2417
Wenchao Xia5b917042013-05-25 11:09:45 +08002418 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002419 }
2420}
2421
2422static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2423{
2424 return strcmp(a, b) == 0;
2425}
2426
2427/**
2428 * Open an image file chain and return an ImageInfoList
2429 *
2430 * @filename: topmost image filename
2431 * @fmt: topmost image format (may be NULL to autodetect)
2432 * @chain: true - enumerate entire backing file chain
2433 * false - only topmost image file
2434 *
2435 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2436 * image file. If there was an error a message will have been printed to
2437 * stderr.
2438 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002439static ImageInfoList *collect_image_info_list(bool image_opts,
2440 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002441 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002442 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002443{
2444 ImageInfoList *head = NULL;
2445 ImageInfoList **last = &head;
2446 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002447 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002448
2449 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2450
2451 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002452 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002453 BlockDriverState *bs;
2454 ImageInfo *info;
2455 ImageInfoList *elem;
2456
2457 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2458 error_report("Backing file '%s' creates an infinite loop.",
2459 filename);
2460 goto err;
2461 }
2462 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2463
Max Reitzefaa7c42016-03-16 19:54:38 +01002464 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002465 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2466 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002467 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002468 goto err;
2469 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002470 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002471
Wenchao Xia43526ec2013-06-06 12:27:58 +08002472 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002473 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002474 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002475 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002476 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002477 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002478
2479 elem = g_new0(ImageInfoList, 1);
2480 elem->value = info;
2481 *last = elem;
2482 last = &elem->next;
2483
Markus Armbruster26f54e92014-10-07 13:59:04 +02002484 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002485
2486 filename = fmt = NULL;
2487 if (chain) {
2488 if (info->has_full_backing_filename) {
2489 filename = info->full_backing_filename;
2490 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002491 error_report("Could not determine absolute backing filename,"
2492 " but backing filename '%s' present",
2493 info->backing_filename);
2494 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002495 }
2496 if (info->has_backing_filename_format) {
2497 fmt = info->backing_filename_format;
2498 }
2499 }
2500 }
2501 g_hash_table_destroy(filenames);
2502 return head;
2503
2504err:
2505 qapi_free_ImageInfoList(head);
2506 g_hash_table_destroy(filenames);
2507 return NULL;
2508}
2509
Benoît Canetc054b3f2012-09-05 13:09:02 +02002510static int img_info(int argc, char **argv)
2511{
2512 int c;
2513 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002514 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002515 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002516 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002517 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002518 bool force_share = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002519
bellardea2384d2004-08-01 21:59:26 +00002520 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002521 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002522 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002523 int option_index = 0;
2524 static const struct option long_options[] = {
2525 {"help", no_argument, 0, 'h'},
2526 {"format", required_argument, 0, 'f'},
2527 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002528 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002529 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002530 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002531 {"force-share", no_argument, 0, 'U'},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002532 {0, 0, 0, 0}
2533 };
Fam Zheng335e9932017-05-03 00:35:39 +08002534 c = getopt_long(argc, argv, ":f:hU",
Benoît Canetc054b3f2012-09-05 13:09:02 +02002535 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002536 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002537 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002538 }
bellardea2384d2004-08-01 21:59:26 +00002539 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002540 case ':':
2541 missing_argument(argv[optind - 1]);
2542 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002543 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002544 unrecognized_option(argv[optind - 1]);
2545 break;
bellardea2384d2004-08-01 21:59:26 +00002546 case 'h':
2547 help();
2548 break;
2549 case 'f':
2550 fmt = optarg;
2551 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002552 case 'U':
2553 force_share = true;
2554 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002555 case OPTION_OUTPUT:
2556 output = optarg;
2557 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002558 case OPTION_BACKING_CHAIN:
2559 chain = true;
2560 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002561 case OPTION_OBJECT: {
2562 QemuOpts *opts;
2563 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2564 optarg, true);
2565 if (!opts) {
2566 return 1;
2567 }
2568 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002569 case OPTION_IMAGE_OPTS:
2570 image_opts = true;
2571 break;
bellardea2384d2004-08-01 21:59:26 +00002572 }
2573 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002574 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002575 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002576 }
bellardea2384d2004-08-01 21:59:26 +00002577 filename = argv[optind++];
2578
Benoît Canetc054b3f2012-09-05 13:09:02 +02002579 if (output && !strcmp(output, "json")) {
2580 output_format = OFORMAT_JSON;
2581 } else if (output && !strcmp(output, "human")) {
2582 output_format = OFORMAT_HUMAN;
2583 } else if (output) {
2584 error_report("--output must be used with human or json as argument.");
2585 return 1;
2586 }
2587
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002588 if (qemu_opts_foreach(&qemu_object_opts,
2589 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002590 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002591 return 1;
2592 }
2593
Fam Zheng335e9932017-05-03 00:35:39 +08002594 list = collect_image_info_list(image_opts, filename, fmt, chain,
2595 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002596 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002597 return 1;
2598 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002599
Benoît Canetc054b3f2012-09-05 13:09:02 +02002600 switch (output_format) {
2601 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002602 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002603 break;
2604 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002605 if (chain) {
2606 dump_json_image_info_list(list);
2607 } else {
2608 dump_json_image_info(list->value);
2609 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002610 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002611 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002612
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002613 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002614 return 0;
2615}
2616
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002617static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2618 MapEntry *next)
2619{
2620 switch (output_format) {
2621 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002622 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002623 error_report("File contains external, encrypted or compressed clusters.");
2624 exit(1);
2625 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002626 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002627 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002628 e->start, e->length,
2629 e->has_offset ? e->offset : 0,
2630 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002631 }
2632 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2633 * Modify the flags here to allow more coalescing.
2634 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002635 if (next && (!next->data || next->zero)) {
2636 next->data = false;
2637 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002638 }
2639 break;
2640 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002641 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2642 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002643 (e->start == 0 ? "[" : ",\n"),
2644 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002645 e->zero ? "true" : "false",
2646 e->data ? "true" : "false");
2647 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002648 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002649 }
2650 putchar('}');
2651
2652 if (!next) {
2653 printf("]\n");
2654 }
2655 break;
2656 }
2657}
2658
Eric Blake5e344dd2017-10-11 22:47:02 -05002659static int get_block_status(BlockDriverState *bs, int64_t offset,
2660 int64_t bytes, MapEntry *e)
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002661{
Eric Blake237d78f2017-10-11 22:47:03 -05002662 int ret;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002663 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002664 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002665 bool has_offset;
Eric Blake237d78f2017-10-11 22:47:03 -05002666 int64_t map;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002667
2668 /* As an optimization, we could cache the current range of unallocated
2669 * clusters in each file of the chain, and avoid querying the same
2670 * range repeatedly.
2671 */
2672
2673 depth = 0;
2674 for (;;) {
Eric Blake237d78f2017-10-11 22:47:03 -05002675 ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002676 if (ret < 0) {
2677 return ret;
2678 }
Eric Blake237d78f2017-10-11 22:47:03 -05002679 assert(bytes);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002680 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2681 break;
2682 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002683 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002684 if (bs == NULL) {
2685 ret = 0;
2686 break;
2687 }
2688
2689 depth++;
2690 }
2691
John Snow28756452016-02-05 13:12:33 -05002692 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2693
2694 *e = (MapEntry) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002695 .start = offset,
Eric Blake237d78f2017-10-11 22:47:03 -05002696 .length = bytes,
John Snow28756452016-02-05 13:12:33 -05002697 .data = !!(ret & BDRV_BLOCK_DATA),
2698 .zero = !!(ret & BDRV_BLOCK_ZERO),
Eric Blake237d78f2017-10-11 22:47:03 -05002699 .offset = map,
John Snow28756452016-02-05 13:12:33 -05002700 .has_offset = has_offset,
2701 .depth = depth,
2702 .has_filename = file && has_offset,
2703 .filename = file && has_offset ? file->filename : NULL,
2704 };
2705
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002706 return 0;
2707}
2708
Fam Zheng16b0d552016-01-26 11:59:02 +08002709static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2710{
2711 if (curr->length == 0) {
2712 return false;
2713 }
2714 if (curr->zero != next->zero ||
2715 curr->data != next->data ||
2716 curr->depth != next->depth ||
2717 curr->has_filename != next->has_filename ||
2718 curr->has_offset != next->has_offset) {
2719 return false;
2720 }
2721 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2722 return false;
2723 }
2724 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2725 return false;
2726 }
2727 return true;
2728}
2729
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002730static int img_map(int argc, char **argv)
2731{
2732 int c;
2733 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002734 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002735 BlockDriverState *bs;
2736 const char *filename, *fmt, *output;
2737 int64_t length;
2738 MapEntry curr = { .length = 0 }, next;
2739 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002740 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002741 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002742
2743 fmt = NULL;
2744 output = NULL;
2745 for (;;) {
2746 int option_index = 0;
2747 static const struct option long_options[] = {
2748 {"help", no_argument, 0, 'h'},
2749 {"format", required_argument, 0, 'f'},
2750 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002751 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002752 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002753 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002754 {0, 0, 0, 0}
2755 };
Fam Zheng335e9932017-05-03 00:35:39 +08002756 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002757 long_options, &option_index);
2758 if (c == -1) {
2759 break;
2760 }
2761 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002762 case ':':
2763 missing_argument(argv[optind - 1]);
2764 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002765 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002766 unrecognized_option(argv[optind - 1]);
2767 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002768 case 'h':
2769 help();
2770 break;
2771 case 'f':
2772 fmt = optarg;
2773 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002774 case 'U':
2775 force_share = true;
2776 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002777 case OPTION_OUTPUT:
2778 output = optarg;
2779 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002780 case OPTION_OBJECT: {
2781 QemuOpts *opts;
2782 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2783 optarg, true);
2784 if (!opts) {
2785 return 1;
2786 }
2787 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002788 case OPTION_IMAGE_OPTS:
2789 image_opts = true;
2790 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002791 }
2792 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002793 if (optind != argc - 1) {
2794 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002795 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002796 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002797
2798 if (output && !strcmp(output, "json")) {
2799 output_format = OFORMAT_JSON;
2800 } else if (output && !strcmp(output, "human")) {
2801 output_format = OFORMAT_HUMAN;
2802 } else if (output) {
2803 error_report("--output must be used with human or json as argument.");
2804 return 1;
2805 }
2806
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002807 if (qemu_opts_foreach(&qemu_object_opts,
2808 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002809 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002810 return 1;
2811 }
2812
Fam Zheng335e9932017-05-03 00:35:39 +08002813 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002814 if (!blk) {
2815 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002816 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002817 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002818
2819 if (output_format == OFORMAT_HUMAN) {
2820 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2821 }
2822
Max Reitzf1d3cd72015-02-05 13:58:18 -05002823 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002824 while (curr.start + curr.length < length) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002825 int64_t offset = curr.start + curr.length;
2826 int64_t n;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002827
2828 /* Probe up to 1 GiB at a time. */
Eric Blake5e344dd2017-10-11 22:47:02 -05002829 n = QEMU_ALIGN_DOWN(MIN(1 << 30, length - offset), BDRV_SECTOR_SIZE);
2830 ret = get_block_status(bs, offset, n, &next);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002831
2832 if (ret < 0) {
2833 error_report("Could not read file metadata: %s", strerror(-ret));
2834 goto out;
2835 }
2836
Fam Zheng16b0d552016-01-26 11:59:02 +08002837 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002838 curr.length += next.length;
2839 continue;
2840 }
2841
2842 if (curr.length > 0) {
2843 dump_map_entry(output_format, &curr, &next);
2844 }
2845 curr = next;
2846 }
2847
2848 dump_map_entry(output_format, &curr, NULL);
2849
2850out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002851 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002852 return ret < 0;
2853}
2854
aliguorif7b4a942009-01-07 17:40:15 +00002855#define SNAPSHOT_LIST 1
2856#define SNAPSHOT_CREATE 2
2857#define SNAPSHOT_APPLY 3
2858#define SNAPSHOT_DELETE 4
2859
Stuart Brady153859b2009-06-07 00:42:17 +01002860static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002861{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002862 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002863 BlockDriverState *bs;
2864 QEMUSnapshotInfo sn;
2865 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002866 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002867 int action = 0;
2868 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002869 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002870 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002871 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002872 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00002873
Kevin Wolfce099542016-03-15 13:01:04 +01002874 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002875 /* Parse commandline parameters */
2876 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002877 static const struct option long_options[] = {
2878 {"help", no_argument, 0, 'h'},
2879 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002880 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002881 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002882 {0, 0, 0, 0}
2883 };
Fam Zheng335e9932017-05-03 00:35:39 +08002884 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002885 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002886 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002887 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002888 }
aliguorif7b4a942009-01-07 17:40:15 +00002889 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002890 case ':':
2891 missing_argument(argv[optind - 1]);
2892 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002893 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002894 unrecognized_option(argv[optind - 1]);
2895 break;
aliguorif7b4a942009-01-07 17:40:15 +00002896 case 'h':
2897 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002898 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002899 case 'l':
2900 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002901 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002902 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002903 }
2904 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002905 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002906 break;
2907 case 'a':
2908 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002909 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002910 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002911 }
2912 action = SNAPSHOT_APPLY;
2913 snapshot_name = optarg;
2914 break;
2915 case 'c':
2916 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002917 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002918 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002919 }
2920 action = SNAPSHOT_CREATE;
2921 snapshot_name = optarg;
2922 break;
2923 case 'd':
2924 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002925 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002926 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002927 }
2928 action = SNAPSHOT_DELETE;
2929 snapshot_name = optarg;
2930 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002931 case 'q':
2932 quiet = true;
2933 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002934 case 'U':
2935 force_share = true;
2936 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002937 case OPTION_OBJECT: {
2938 QemuOpts *opts;
2939 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2940 optarg, true);
2941 if (!opts) {
2942 return 1;
2943 }
2944 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002945 case OPTION_IMAGE_OPTS:
2946 image_opts = true;
2947 break;
aliguorif7b4a942009-01-07 17:40:15 +00002948 }
2949 }
2950
Kevin Wolffc11eb22013-08-05 10:53:04 +02002951 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002952 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002953 }
aliguorif7b4a942009-01-07 17:40:15 +00002954 filename = argv[optind++];
2955
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002956 if (qemu_opts_foreach(&qemu_object_opts,
2957 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002958 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002959 return 1;
2960 }
2961
aliguorif7b4a942009-01-07 17:40:15 +00002962 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08002963 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
2964 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002965 if (!blk) {
2966 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002967 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002968 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002969
2970 /* Perform the requested action */
2971 switch(action) {
2972 case SNAPSHOT_LIST:
2973 dump_snapshots(bs);
2974 break;
2975
2976 case SNAPSHOT_CREATE:
2977 memset(&sn, 0, sizeof(sn));
2978 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2979
2980 qemu_gettimeofday(&tv);
2981 sn.date_sec = tv.tv_sec;
2982 sn.date_nsec = tv.tv_usec * 1000;
2983
2984 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002985 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002986 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002987 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002988 }
aliguorif7b4a942009-01-07 17:40:15 +00002989 break;
2990
2991 case SNAPSHOT_APPLY:
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01002992 ret = bdrv_snapshot_goto(bs, snapshot_name, &err);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002993 if (ret) {
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01002994 error_reportf_err(err, "Could not apply snapshot '%s': ",
2995 snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002996 }
aliguorif7b4a942009-01-07 17:40:15 +00002997 break;
2998
2999 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003000 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003001 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003002 error_reportf_err(err, "Could not delete snapshot '%s': ",
3003 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003004 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003005 }
aliguorif7b4a942009-01-07 17:40:15 +00003006 break;
3007 }
3008
3009 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003010 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003011 if (ret) {
3012 return 1;
3013 }
Stuart Brady153859b2009-06-07 00:42:17 +01003014 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003015}
3016
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003017static int img_rebase(int argc, char **argv)
3018{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003019 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003020 uint8_t *buf_old = NULL;
3021 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05003022 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003023 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003024 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3025 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003026 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003027 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003028 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003029 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003030 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003031 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003032 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003033
3034 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003035 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003036 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003037 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003038 out_baseimg = NULL;
3039 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003040 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003041 static const struct option long_options[] = {
3042 {"help", no_argument, 0, 'h'},
3043 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003044 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003045 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003046 {0, 0, 0, 0}
3047 };
Fam Zheng335e9932017-05-03 00:35:39 +08003048 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003049 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003050 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003051 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003052 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003053 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003054 case ':':
3055 missing_argument(argv[optind - 1]);
3056 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003057 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003058 unrecognized_option(argv[optind - 1]);
3059 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003060 case 'h':
3061 help();
3062 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003063 case 'f':
3064 fmt = optarg;
3065 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003066 case 'F':
3067 out_basefmt = optarg;
3068 break;
3069 case 'b':
3070 out_baseimg = optarg;
3071 break;
3072 case 'u':
3073 unsafe = 1;
3074 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003075 case 'p':
3076 progress = 1;
3077 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003078 case 't':
3079 cache = optarg;
3080 break;
Max Reitz40055952014-07-22 22:58:42 +02003081 case 'T':
3082 src_cache = optarg;
3083 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003084 case 'q':
3085 quiet = true;
3086 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003087 case OPTION_OBJECT: {
3088 QemuOpts *opts;
3089 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3090 optarg, true);
3091 if (!opts) {
3092 return 1;
3093 }
3094 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003095 case OPTION_IMAGE_OPTS:
3096 image_opts = true;
3097 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003098 case 'U':
3099 force_share = true;
3100 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003101 }
3102 }
3103
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003104 if (quiet) {
3105 progress = 0;
3106 }
3107
Fam Zhengac1307a2014-04-22 13:36:11 +08003108 if (optind != argc - 1) {
3109 error_exit("Expecting one image file name");
3110 }
3111 if (!unsafe && !out_baseimg) {
3112 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003113 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003114 filename = argv[optind++];
3115
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003116 if (qemu_opts_foreach(&qemu_object_opts,
3117 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003118 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003119 return 1;
3120 }
3121
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003122 qemu_progress_init(progress, 2.0);
3123 qemu_progress_print(0, 100);
3124
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003125 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003126 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003127 if (ret < 0) {
3128 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003129 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003130 }
3131
Kevin Wolfce099542016-03-15 13:01:04 +01003132 src_flags = 0;
3133 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003134 if (ret < 0) {
3135 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003136 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003137 }
3138
Kevin Wolfce099542016-03-15 13:01:04 +01003139 /* The source files are opened read-only, don't care about WCE */
3140 assert((src_flags & BDRV_O_RDWR) == 0);
3141 (void) src_writethrough;
3142
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003143 /*
3144 * Open the images.
3145 *
3146 * Ignore the old backing file for unsafe rebase in case we want to correct
3147 * the reference to a renamed or moved backing file.
3148 */
Fam Zheng335e9932017-05-03 00:35:39 +08003149 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3150 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003151 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003152 ret = -1;
3153 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003154 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003155 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003156
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003157 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003158 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003159 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003160 ret = -1;
3161 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003162 }
3163 }
3164
3165 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003166 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003167 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003168 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003169
Max Reitz644483d2015-02-05 13:58:17 -05003170 if (bs->backing_format[0] != '\0') {
3171 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003172 qdict_put_str(options, "driver", bs->backing_format);
Max Reitz644483d2015-02-05 13:58:17 -05003173 }
3174
Fam Zheng335e9932017-05-03 00:35:39 +08003175 if (force_share) {
3176 if (!options) {
3177 options = qdict_new();
3178 }
Eric Blake579cf1d2017-05-15 14:54:39 -05003179 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +08003180 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003181 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003182 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003183 options, src_flags, &local_err);
3184 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003185 error_reportf_err(local_err,
3186 "Could not open old backing file '%s': ",
3187 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003188 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003189 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003190 }
Max Reitz644483d2015-02-05 13:58:17 -05003191
Alex Bligha6166732012-10-16 13:46:18 +01003192 if (out_baseimg[0]) {
Fam Zheng335e9932017-05-03 00:35:39 +08003193 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003194 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003195 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003196 }
3197 if (force_share) {
3198 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003199 }
3200
Max Reitzefaa7c42016-03-16 19:54:38 +01003201 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003202 options, src_flags, &local_err);
3203 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003204 error_reportf_err(local_err,
3205 "Could not open new backing file '%s': ",
3206 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003207 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003208 goto out;
3209 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003210 }
3211 }
3212
3213 /*
3214 * Check each unallocated cluster in the COW file. If it is unallocated,
3215 * accesses go to the backing file. We must therefore compare this cluster
3216 * in the old and new backing file, and if they differ we need to copy it
3217 * from the old backing file into the COW file.
3218 *
3219 * If qemu-img crashes during this step, no harm is done. The content of
3220 * the image is the same as the original one at any time.
3221 */
3222 if (!unsafe) {
Eric Blake41536282017-10-11 22:47:15 -05003223 int64_t size;
3224 int64_t old_backing_size;
3225 int64_t new_backing_size = 0;
3226 uint64_t offset;
3227 int64_t n;
Kevin Wolf1f710492012-10-12 14:29:18 +02003228 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003229
Max Reitzf1d3cd72015-02-05 13:58:18 -05003230 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3231 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003232
Eric Blake41536282017-10-11 22:47:15 -05003233 size = blk_getlength(blk);
3234 if (size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003235 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003236 filename, strerror(-size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003237 ret = -1;
3238 goto out;
3239 }
Eric Blake41536282017-10-11 22:47:15 -05003240 old_backing_size = blk_getlength(blk_old_backing);
3241 if (old_backing_size < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003242 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003243
3244 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3245 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003246 backing_name, strerror(-old_backing_size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003247 ret = -1;
3248 goto out;
3249 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003250 if (blk_new_backing) {
Eric Blake41536282017-10-11 22:47:15 -05003251 new_backing_size = blk_getlength(blk_new_backing);
3252 if (new_backing_size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003253 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003254 out_baseimg, strerror(-new_backing_size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003255 ret = -1;
3256 goto out;
3257 }
Alex Bligha6166732012-10-16 13:46:18 +01003258 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003259
Eric Blake41536282017-10-11 22:47:15 -05003260 if (size != 0) {
3261 local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE));
Kevin Wolf1f710492012-10-12 14:29:18 +02003262 }
3263
Eric Blake41536282017-10-11 22:47:15 -05003264 for (offset = 0; offset < size; offset += n) {
3265 /* How many bytes can we handle with the next read? */
3266 n = MIN(IO_BUF_SIZE, size - offset);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003267
3268 /* If the cluster is allocated, we don't need to take action */
Eric Blake41536282017-10-11 22:47:15 -05003269 ret = bdrv_is_allocated(bs, offset, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003270 if (ret < 0) {
3271 error_report("error while reading image metadata: %s",
3272 strerror(-ret));
3273 goto out;
3274 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003275 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003276 continue;
3277 }
3278
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003279 /*
3280 * Read old and new backing file and take into consideration that
3281 * backing files may be smaller than the COW image.
3282 */
Eric Blake41536282017-10-11 22:47:15 -05003283 if (offset >= old_backing_size) {
3284 memset(buf_old, 0, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003285 } else {
Eric Blake41536282017-10-11 22:47:15 -05003286 if (offset + n > old_backing_size) {
3287 n = old_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003288 }
3289
Eric Blake41536282017-10-11 22:47:15 -05003290 ret = blk_pread(blk_old_backing, offset, buf_old, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003291 if (ret < 0) {
3292 error_report("error while reading from old backing file");
3293 goto out;
3294 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003295 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003296
Eric Blake41536282017-10-11 22:47:15 -05003297 if (offset >= new_backing_size || !blk_new_backing) {
3298 memset(buf_new, 0, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003299 } else {
Eric Blake41536282017-10-11 22:47:15 -05003300 if (offset + n > new_backing_size) {
3301 n = new_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003302 }
3303
Eric Blake41536282017-10-11 22:47:15 -05003304 ret = blk_pread(blk_new_backing, offset, buf_new, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003305 if (ret < 0) {
3306 error_report("error while reading from new backing file");
3307 goto out;
3308 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003309 }
3310
3311 /* If they differ, we need to write to the COW file */
3312 uint64_t written = 0;
3313
Eric Blake41536282017-10-11 22:47:15 -05003314 while (written < n) {
Eric Blakedc61cd32017-10-11 22:47:14 -05003315 int64_t pnum;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003316
Eric Blake41536282017-10-11 22:47:15 -05003317 if (compare_buffers(buf_old + written, buf_new + written,
3318 n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003319 {
Eric Blake41536282017-10-11 22:47:15 -05003320 ret = blk_pwrite(blk, offset + written,
Eric Blakedc61cd32017-10-11 22:47:14 -05003321 buf_old + written, pnum, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003322 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003323 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003324 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003325 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003326 }
3327 }
3328
3329 written += pnum;
3330 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003331 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003332 }
3333 }
3334
3335 /*
3336 * Change the backing file. All clusters that are different from the old
3337 * backing file are overwritten in the COW file now, so the visible content
3338 * doesn't change when we switch the backing file.
3339 */
Alex Bligha6166732012-10-16 13:46:18 +01003340 if (out_baseimg && *out_baseimg) {
3341 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3342 } else {
3343 ret = bdrv_change_backing_file(bs, NULL, NULL);
3344 }
3345
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003346 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003347 error_report("Could not change the backing file to '%s': No "
3348 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003349 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003350 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003351 out_baseimg, strerror(-ret));
3352 }
3353
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003354 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003355 /*
3356 * TODO At this point it is possible to check if any clusters that are
3357 * allocated in the COW file are the same in the backing file. If so, they
3358 * could be dropped from the COW file. Don't do this before switching the
3359 * backing file, in case of a crash this would lead to corruption.
3360 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003361out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003362 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003363 /* Cleanup */
3364 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003365 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003366 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003367 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003368 qemu_vfree(buf_old);
3369 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003370
Markus Armbruster26f54e92014-10-07 13:59:04 +02003371 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003372 if (ret) {
3373 return 1;
3374 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003375 return 0;
3376}
3377
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003378static int img_resize(int argc, char **argv)
3379{
Markus Armbruster6750e792015-02-12 17:43:08 +01003380 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003381 int c, ret, relative;
3382 const char *filename, *fmt, *size;
Max Reitz5279b302018-04-21 18:39:57 +02003383 int64_t n, total_size, current_size, new_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003384 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003385 BlockBackend *blk = NULL;
Max Reitzdc5f6902017-06-13 22:20:55 +02003386 PreallocMode prealloc = PREALLOC_MODE_OFF;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003387 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003388
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003389 static QemuOptsList resize_options = {
3390 .name = "resize_options",
3391 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3392 .desc = {
3393 {
3394 .name = BLOCK_OPT_SIZE,
3395 .type = QEMU_OPT_SIZE,
3396 .help = "Virtual disk size"
3397 }, {
3398 /* end of list */
3399 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003400 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003401 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003402 bool image_opts = false;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003403 bool shrink = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003404
Kevin Wolfe80fec72011-04-29 10:58:12 +02003405 /* Remove size from argv manually so that negative numbers are not treated
3406 * as options by getopt. */
3407 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003408 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003409 return 1;
3410 }
3411
3412 size = argv[--argc];
3413
3414 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003415 fmt = NULL;
3416 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003417 static const struct option long_options[] = {
3418 {"help", no_argument, 0, 'h'},
3419 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003420 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Max Reitzdc5f6902017-06-13 22:20:55 +02003421 {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003422 {"shrink", no_argument, 0, OPTION_SHRINK},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003423 {0, 0, 0, 0}
3424 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003425 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003426 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003427 if (c == -1) {
3428 break;
3429 }
3430 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003431 case ':':
3432 missing_argument(argv[optind - 1]);
3433 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003434 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003435 unrecognized_option(argv[optind - 1]);
3436 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003437 case 'h':
3438 help();
3439 break;
3440 case 'f':
3441 fmt = optarg;
3442 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003443 case 'q':
3444 quiet = true;
3445 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003446 case OPTION_OBJECT: {
3447 QemuOpts *opts;
3448 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3449 optarg, true);
3450 if (!opts) {
3451 return 1;
3452 }
3453 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003454 case OPTION_IMAGE_OPTS:
3455 image_opts = true;
3456 break;
Max Reitzdc5f6902017-06-13 22:20:55 +02003457 case OPTION_PREALLOCATION:
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02003458 prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
Markus Armbruster06c60b62017-08-24 10:45:57 +02003459 PREALLOC_MODE__MAX, NULL);
Max Reitzdc5f6902017-06-13 22:20:55 +02003460 if (prealloc == PREALLOC_MODE__MAX) {
3461 error_report("Invalid preallocation mode '%s'", optarg);
3462 return 1;
3463 }
3464 break;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003465 case OPTION_SHRINK:
3466 shrink = true;
3467 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003468 }
3469 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003470 if (optind != argc - 1) {
Max Reitzbe8fbd42018-02-05 17:27:45 +01003471 error_exit("Expecting image file name and size");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003472 }
3473 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003474
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003475 if (qemu_opts_foreach(&qemu_object_opts,
3476 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003477 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003478 return 1;
3479 }
3480
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003481 /* Choose grow, shrink, or absolute resize mode */
3482 switch (size[0]) {
3483 case '+':
3484 relative = 1;
3485 size++;
3486 break;
3487 case '-':
3488 relative = -1;
3489 size++;
3490 break;
3491 default:
3492 relative = 0;
3493 break;
3494 }
3495
3496 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003497 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003498 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003499 if (err) {
3500 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003501 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003502 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003503 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003504 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003505 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3506 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003507
Max Reitzefaa7c42016-03-16 19:54:38 +01003508 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003509 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3510 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003511 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003512 ret = -1;
3513 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003514 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003515
Max Reitzdc5f6902017-06-13 22:20:55 +02003516 current_size = blk_getlength(blk);
3517 if (current_size < 0) {
3518 error_report("Failed to inquire current image length: %s",
3519 strerror(-current_size));
3520 ret = -1;
3521 goto out;
3522 }
3523
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003524 if (relative) {
Max Reitzdc5f6902017-06-13 22:20:55 +02003525 total_size = current_size + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003526 } else {
3527 total_size = n;
3528 }
3529 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003530 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003531 ret = -1;
3532 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003533 }
3534
Max Reitzdc5f6902017-06-13 22:20:55 +02003535 if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
3536 error_report("Preallocation can only be used for growing images");
3537 ret = -1;
3538 goto out;
3539 }
3540
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003541 if (total_size < current_size && !shrink) {
3542 warn_report("Shrinking an image will delete all data beyond the "
3543 "shrunken image's end. Before performing such an "
3544 "operation, make sure there is no important data there.");
3545
3546 if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) {
3547 error_report(
3548 "Use the --shrink option to perform a shrink operation.");
3549 ret = -1;
3550 goto out;
3551 } else {
3552 warn_report("Using the --shrink option will suppress this message. "
3553 "Note that future versions of qemu-img may refuse to "
3554 "shrink images without this option.");
3555 }
3556 }
3557
Max Reitzdc5f6902017-06-13 22:20:55 +02003558 ret = blk_truncate(blk, total_size, prealloc, &err);
Max Reitz5279b302018-04-21 18:39:57 +02003559 if (ret < 0) {
Max Reitzed3d2ec2017-03-28 22:51:27 +02003560 error_report_err(err);
Max Reitz5279b302018-04-21 18:39:57 +02003561 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003562 }
Max Reitz5279b302018-04-21 18:39:57 +02003563
3564 new_size = blk_getlength(blk);
3565 if (new_size < 0) {
3566 error_report("Failed to verify truncated image length: %s",
3567 strerror(-new_size));
3568 ret = -1;
3569 goto out;
3570 }
3571
3572 /* Some block drivers implement a truncation method, but only so
3573 * the user can cause qemu to refresh the image's size from disk.
3574 * The idea is that the user resizes the image outside of qemu and
3575 * then invokes block_resize to inform qemu about it.
3576 * (This includes iscsi and file-posix for device files.)
3577 * Of course, that is not the behavior someone invoking
3578 * qemu-img resize would find useful, so we catch that behavior
3579 * here and tell the user. */
3580 if (new_size != total_size && new_size == current_size) {
3581 error_report("Image was not resized; resizing may not be supported "
3582 "for this image");
3583 ret = -1;
3584 goto out;
3585 }
3586
3587 if (new_size != total_size) {
3588 warn_report("Image should have been resized to %" PRIi64
3589 " bytes, but was resized to %" PRIi64 " bytes",
3590 total_size, new_size);
3591 }
3592
3593 qprintf(quiet, "Image resized.\n");
3594
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003595out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003596 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003597 if (ret) {
3598 return 1;
3599 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003600 return 0;
3601}
3602
Max Reitz76a3a342014-10-27 11:12:51 +01003603static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003604 int64_t offset, int64_t total_work_size,
3605 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003606{
3607 qemu_progress_print(100.f * offset / total_work_size, 0);
3608}
3609
Max Reitz6f176b42013-09-03 10:09:50 +02003610static int img_amend(int argc, char **argv)
3611{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003612 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003613 int c, ret = 0;
3614 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003615 QemuOptsList *create_opts = NULL;
3616 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003617 const char *fmt = NULL, *filename, *cache;
3618 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003619 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003620 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003621 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003622 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003623 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003624
Max Reitzbd39e6e2014-07-22 22:58:43 +02003625 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003626 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003627 static const struct option long_options[] = {
3628 {"help", no_argument, 0, 'h'},
3629 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003630 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003631 {0, 0, 0, 0}
3632 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003633 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003634 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003635 if (c == -1) {
3636 break;
3637 }
3638
3639 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003640 case ':':
3641 missing_argument(argv[optind - 1]);
3642 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003643 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003644 unrecognized_option(argv[optind - 1]);
3645 break;
3646 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003647 help();
3648 break;
3649 case 'o':
3650 if (!is_valid_option_list(optarg)) {
3651 error_report("Invalid option list: %s", optarg);
3652 ret = -1;
3653 goto out_no_progress;
3654 }
3655 if (!options) {
3656 options = g_strdup(optarg);
3657 } else {
3658 char *old_options = options;
3659 options = g_strdup_printf("%s,%s", options, optarg);
3660 g_free(old_options);
3661 }
3662 break;
3663 case 'f':
3664 fmt = optarg;
3665 break;
3666 case 't':
3667 cache = optarg;
3668 break;
3669 case 'p':
3670 progress = true;
3671 break;
3672 case 'q':
3673 quiet = true;
3674 break;
3675 case OPTION_OBJECT:
3676 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3677 optarg, true);
3678 if (!opts) {
3679 ret = -1;
3680 goto out_no_progress;
3681 }
3682 break;
3683 case OPTION_IMAGE_OPTS:
3684 image_opts = true;
3685 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003686 }
3687 }
3688
Max Reitz6f176b42013-09-03 10:09:50 +02003689 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003690 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003691 }
3692
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003693 if (qemu_opts_foreach(&qemu_object_opts,
3694 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003695 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003696 ret = -1;
3697 goto out_no_progress;
3698 }
3699
Max Reitz76a3a342014-10-27 11:12:51 +01003700 if (quiet) {
3701 progress = false;
3702 }
3703 qemu_progress_init(progress, 1.0);
3704
Kevin Wolfa283cb62014-02-21 16:24:07 +01003705 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3706 if (fmt && has_help_option(options)) {
3707 /* If a format is explicitly specified (and possibly no filename is
3708 * given), print option help here */
3709 ret = print_block_option_help(filename, fmt);
3710 goto out;
3711 }
3712
3713 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003714 error_report("Expecting one image file name");
3715 ret = -1;
3716 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003717 }
Max Reitz6f176b42013-09-03 10:09:50 +02003718
Kevin Wolfce099542016-03-15 13:01:04 +01003719 flags = BDRV_O_RDWR;
3720 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003721 if (ret < 0) {
3722 error_report("Invalid cache option: %s", cache);
3723 goto out;
3724 }
3725
Fam Zheng335e9932017-05-03 00:35:39 +08003726 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3727 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003728 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003729 ret = -1;
3730 goto out;
3731 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003732 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003733
3734 fmt = bs->drv->format_name;
3735
Kevin Wolf626f84f2014-02-21 16:24:06 +01003736 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003737 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003738 ret = print_block_option_help(filename, fmt);
3739 goto out;
3740 }
3741
Max Reitzb2439d22014-12-02 18:32:47 +01003742 if (!bs->drv->create_opts) {
3743 error_report("Format driver '%s' does not support any options to amend",
3744 fmt);
3745 ret = -1;
3746 goto out;
3747 }
3748
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003749 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003750 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003751 qemu_opts_do_parse(opts, options, NULL, &err);
3752 if (err) {
3753 error_report_err(err);
3754 ret = -1;
3755 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003756 }
3757
Max Reitz76a3a342014-10-27 11:12:51 +01003758 /* In case the driver does not call amend_status_cb() */
3759 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003760 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003761 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003762 if (ret < 0) {
3763 error_report("Error while amending options: %s", strerror(-ret));
3764 goto out;
3765 }
3766
3767out:
Max Reitz76a3a342014-10-27 11:12:51 +01003768 qemu_progress_end();
3769
Max Reitze814dff2015-08-20 16:00:38 -07003770out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003771 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003772 qemu_opts_del(opts);
3773 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003774 g_free(options);
3775
Max Reitz6f176b42013-09-03 10:09:50 +02003776 if (ret) {
3777 return 1;
3778 }
3779 return 0;
3780}
3781
Kevin Wolfb6133b82014-08-05 14:17:13 +02003782typedef struct BenchData {
3783 BlockBackend *blk;
3784 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003785 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003786 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003787 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003788 int nrreq;
3789 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003790 int flush_interval;
3791 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003792 uint8_t *buf;
3793 QEMUIOVector *qiov;
3794
3795 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003796 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003797 uint64_t offset;
3798} BenchData;
3799
Kevin Wolf55d539c2016-06-03 13:59:41 +02003800static void bench_undrained_flush_cb(void *opaque, int ret)
3801{
3802 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003803 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003804 exit(EXIT_FAILURE);
3805 }
3806}
3807
Kevin Wolfb6133b82014-08-05 14:17:13 +02003808static void bench_cb(void *opaque, int ret)
3809{
3810 BenchData *b = opaque;
3811 BlockAIOCB *acb;
3812
3813 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003814 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003815 exit(EXIT_FAILURE);
3816 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003817
3818 if (b->in_flush) {
3819 /* Just finished a flush with drained queue: Start next requests */
3820 assert(b->in_flight == 0);
3821 b->in_flush = false;
3822 } else if (b->in_flight > 0) {
3823 int remaining = b->n - b->in_flight;
3824
Kevin Wolfb6133b82014-08-05 14:17:13 +02003825 b->n--;
3826 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003827
3828 /* Time for flush? Drain queue if requested, then flush */
3829 if (b->flush_interval && remaining % b->flush_interval == 0) {
3830 if (!b->in_flight || !b->drain_on_flush) {
3831 BlockCompletionFunc *cb;
3832
3833 if (b->drain_on_flush) {
3834 b->in_flush = true;
3835 cb = bench_cb;
3836 } else {
3837 cb = bench_undrained_flush_cb;
3838 }
3839
3840 acb = blk_aio_flush(b->blk, cb, b);
3841 if (!acb) {
3842 error_report("Failed to issue flush request");
3843 exit(EXIT_FAILURE);
3844 }
3845 }
3846 if (b->drain_on_flush) {
3847 return;
3848 }
3849 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003850 }
3851
3852 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003853 int64_t offset = b->offset;
3854 /* blk_aio_* might look for completed I/Os and kick bench_cb
3855 * again, so make sure this operation is counted by in_flight
3856 * and b->offset is ready for the next submission.
3857 */
3858 b->in_flight++;
3859 b->offset += b->step;
3860 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003861 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003862 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003863 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003864 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003865 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003866 if (!acb) {
3867 error_report("Failed to issue request");
3868 exit(EXIT_FAILURE);
3869 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003870 }
3871}
3872
3873static int img_bench(int argc, char **argv)
3874{
3875 int c, ret = 0;
3876 const char *fmt = NULL, *filename;
3877 bool quiet = false;
3878 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003879 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003880 int count = 75000;
3881 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003882 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003883 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003884 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003885 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003886 int flush_interval = 0;
3887 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003888 int64_t image_size;
3889 BlockBackend *blk = NULL;
3890 BenchData data = {};
3891 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003892 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003893 struct timeval t1, t2;
3894 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08003895 bool force_share = false;
Fam Zheng79d46582018-01-16 14:08:58 +08003896 size_t buf_size;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003897
3898 for (;;) {
3899 static const struct option long_options[] = {
3900 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003901 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003902 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003903 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003904 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08003905 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003906 {0, 0, 0, 0}
3907 };
Fam Zheng335e9932017-05-03 00:35:39 +08003908 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003909 if (c == -1) {
3910 break;
3911 }
3912
3913 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003914 case ':':
3915 missing_argument(argv[optind - 1]);
3916 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003917 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003918 unrecognized_option(argv[optind - 1]);
3919 break;
3920 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02003921 help();
3922 break;
3923 case 'c':
3924 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003925 unsigned long res;
3926
3927 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003928 error_report("Invalid request count specified");
3929 return 1;
3930 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003931 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003932 break;
3933 }
3934 case 'd':
3935 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003936 unsigned long res;
3937
3938 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003939 error_report("Invalid queue depth specified");
3940 return 1;
3941 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003942 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003943 break;
3944 }
3945 case 'f':
3946 fmt = optarg;
3947 break;
3948 case 'n':
3949 flags |= BDRV_O_NATIVE_AIO;
3950 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003951 case 'o':
3952 {
Markus Armbruster606caa02017-02-21 21:14:04 +01003953 offset = cvtnum(optarg);
3954 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02003955 error_report("Invalid offset specified");
3956 return 1;
3957 }
3958 break;
3959 }
3960 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003961 case 'q':
3962 quiet = true;
3963 break;
3964 case 's':
3965 {
3966 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003967
Markus Armbruster606caa02017-02-21 21:14:04 +01003968 sval = cvtnum(optarg);
3969 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003970 error_report("Invalid buffer size specified");
3971 return 1;
3972 }
3973
3974 bufsize = sval;
3975 break;
3976 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003977 case 'S':
3978 {
3979 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003980
Markus Armbruster606caa02017-02-21 21:14:04 +01003981 sval = cvtnum(optarg);
3982 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02003983 error_report("Invalid step size specified");
3984 return 1;
3985 }
3986
3987 step = sval;
3988 break;
3989 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003990 case 't':
3991 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3992 if (ret < 0) {
3993 error_report("Invalid cache mode");
3994 ret = -1;
3995 goto out;
3996 }
3997 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003998 case 'w':
3999 flags |= BDRV_O_RDWR;
4000 is_write = true;
4001 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004002 case 'U':
4003 force_share = true;
4004 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004005 case OPTION_PATTERN:
4006 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004007 unsigned long res;
4008
4009 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004010 error_report("Invalid pattern byte specified");
4011 return 1;
4012 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004013 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004014 break;
4015 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02004016 case OPTION_FLUSH_INTERVAL:
4017 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004018 unsigned long res;
4019
4020 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004021 error_report("Invalid flush interval specified");
4022 return 1;
4023 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004024 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004025 break;
4026 }
4027 case OPTION_NO_DRAIN:
4028 drain_on_flush = false;
4029 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004030 case OPTION_IMAGE_OPTS:
4031 image_opts = true;
4032 break;
4033 }
4034 }
4035
4036 if (optind != argc - 1) {
4037 error_exit("Expecting one image file name");
4038 }
4039 filename = argv[argc - 1];
4040
Kevin Wolf55d539c2016-06-03 13:59:41 +02004041 if (!is_write && flush_interval) {
4042 error_report("--flush-interval is only available in write tests");
4043 ret = -1;
4044 goto out;
4045 }
4046 if (flush_interval && flush_interval < depth) {
4047 error_report("Flush interval can't be smaller than depth");
4048 ret = -1;
4049 goto out;
4050 }
4051
Fam Zheng335e9932017-05-03 00:35:39 +08004052 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4053 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004054 if (!blk) {
4055 ret = -1;
4056 goto out;
4057 }
4058
4059 image_size = blk_getlength(blk);
4060 if (image_size < 0) {
4061 ret = image_size;
4062 goto out;
4063 }
4064
4065 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004066 .blk = blk,
4067 .image_size = image_size,
4068 .bufsize = bufsize,
4069 .step = step ?: bufsize,
4070 .nrreq = depth,
4071 .n = count,
4072 .offset = offset,
4073 .write = is_write,
4074 .flush_interval = flush_interval,
4075 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004076 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004077 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004078 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004079 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004080 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004081 if (flush_interval) {
4082 printf("Sending flush every %d requests\n", flush_interval);
4083 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004084
Fam Zheng79d46582018-01-16 14:08:58 +08004085 buf_size = data.nrreq * data.bufsize;
4086 data.buf = blk_blockalign(blk, buf_size);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004087 memset(data.buf, pattern, data.nrreq * data.bufsize);
4088
Fam Zheng79d46582018-01-16 14:08:58 +08004089 blk_register_buf(blk, data.buf, buf_size);
4090
Kevin Wolfb6133b82014-08-05 14:17:13 +02004091 data.qiov = g_new(QEMUIOVector, data.nrreq);
4092 for (i = 0; i < data.nrreq; i++) {
4093 qemu_iovec_init(&data.qiov[i], 1);
4094 qemu_iovec_add(&data.qiov[i],
4095 data.buf + i * data.bufsize, data.bufsize);
4096 }
4097
4098 gettimeofday(&t1, NULL);
4099 bench_cb(&data, 0);
4100
4101 while (data.n > 0) {
4102 main_loop_wait(false);
4103 }
4104 gettimeofday(&t2, NULL);
4105
4106 printf("Run completed in %3.3f seconds.\n",
4107 (t2.tv_sec - t1.tv_sec)
4108 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4109
4110out:
Fam Zheng79d46582018-01-16 14:08:58 +08004111 if (data.buf) {
4112 blk_unregister_buf(blk, data.buf);
4113 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004114 qemu_vfree(data.buf);
4115 blk_unref(blk);
4116
4117 if (ret) {
4118 return 1;
4119 }
4120 return 0;
4121}
4122
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004123#define C_BS 01
4124#define C_COUNT 02
4125#define C_IF 04
4126#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004127#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004128
4129struct DdInfo {
4130 unsigned int flags;
4131 int64_t count;
4132};
4133
4134struct DdIo {
4135 int bsz; /* Block size */
4136 char *filename;
4137 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004138 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004139};
4140
4141struct DdOpts {
4142 const char *name;
4143 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4144 unsigned int flag;
4145};
4146
4147static int img_dd_bs(const char *arg,
4148 struct DdIo *in, struct DdIo *out,
4149 struct DdInfo *dd)
4150{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004151 int64_t res;
4152
Markus Armbruster606caa02017-02-21 21:14:04 +01004153 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004154
Markus Armbruster606caa02017-02-21 21:14:04 +01004155 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004156 error_report("invalid number: '%s'", arg);
4157 return 1;
4158 }
4159 in->bsz = out->bsz = res;
4160
4161 return 0;
4162}
4163
4164static int img_dd_count(const char *arg,
4165 struct DdIo *in, struct DdIo *out,
4166 struct DdInfo *dd)
4167{
Markus Armbruster606caa02017-02-21 21:14:04 +01004168 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004169
Markus Armbruster606caa02017-02-21 21:14:04 +01004170 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004171 error_report("invalid number: '%s'", arg);
4172 return 1;
4173 }
4174
4175 return 0;
4176}
4177
4178static int img_dd_if(const char *arg,
4179 struct DdIo *in, struct DdIo *out,
4180 struct DdInfo *dd)
4181{
4182 in->filename = g_strdup(arg);
4183
4184 return 0;
4185}
4186
4187static int img_dd_of(const char *arg,
4188 struct DdIo *in, struct DdIo *out,
4189 struct DdInfo *dd)
4190{
4191 out->filename = g_strdup(arg);
4192
4193 return 0;
4194}
4195
Reda Sallahif7c15532016-08-10 16:16:09 +02004196static int img_dd_skip(const char *arg,
4197 struct DdIo *in, struct DdIo *out,
4198 struct DdInfo *dd)
4199{
Markus Armbruster606caa02017-02-21 21:14:04 +01004200 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004201
Markus Armbruster606caa02017-02-21 21:14:04 +01004202 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004203 error_report("invalid number: '%s'", arg);
4204 return 1;
4205 }
4206
4207 return 0;
4208}
4209
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004210static int img_dd(int argc, char **argv)
4211{
4212 int ret = 0;
4213 char *arg = NULL;
4214 char *tmp;
4215 BlockDriver *drv = NULL, *proto_drv = NULL;
4216 BlockBackend *blk1 = NULL, *blk2 = NULL;
4217 QemuOpts *opts = NULL;
4218 QemuOptsList *create_opts = NULL;
4219 Error *local_err = NULL;
4220 bool image_opts = false;
4221 int c, i;
4222 const char *out_fmt = "raw";
4223 const char *fmt = NULL;
4224 int64_t size = 0;
4225 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004226 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004227 struct DdInfo dd = {
4228 .flags = 0,
4229 .count = 0,
4230 };
4231 struct DdIo in = {
4232 .bsz = 512, /* Block size is by default 512 bytes */
4233 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004234 .buf = NULL,
4235 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004236 };
4237 struct DdIo out = {
4238 .bsz = 512,
4239 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004240 .buf = NULL,
4241 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004242 };
4243
4244 const struct DdOpts options[] = {
4245 { "bs", img_dd_bs, C_BS },
4246 { "count", img_dd_count, C_COUNT },
4247 { "if", img_dd_if, C_IF },
4248 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004249 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004250 { NULL, NULL, 0 }
4251 };
4252 const struct option long_options[] = {
4253 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004254 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004255 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004256 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004257 { 0, 0, 0, 0 }
4258 };
4259
Fam Zheng335e9932017-05-03 00:35:39 +08004260 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004261 if (c == EOF) {
4262 break;
4263 }
4264 switch (c) {
4265 case 'O':
4266 out_fmt = optarg;
4267 break;
4268 case 'f':
4269 fmt = optarg;
4270 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004271 case ':':
4272 missing_argument(argv[optind - 1]);
4273 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004274 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004275 unrecognized_option(argv[optind - 1]);
4276 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004277 case 'h':
4278 help();
4279 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004280 case 'U':
4281 force_share = true;
4282 break;
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004283 case OPTION_OBJECT:
4284 if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004285 ret = -1;
4286 goto out;
4287 }
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004288 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004289 case OPTION_IMAGE_OPTS:
4290 image_opts = true;
4291 break;
4292 }
4293 }
4294
4295 for (i = optind; i < argc; i++) {
4296 int j;
4297 arg = g_strdup(argv[i]);
4298
4299 tmp = strchr(arg, '=');
4300 if (tmp == NULL) {
4301 error_report("unrecognized operand %s", arg);
4302 ret = -1;
4303 goto out;
4304 }
4305
4306 *tmp++ = '\0';
4307
4308 for (j = 0; options[j].name != NULL; j++) {
4309 if (!strcmp(arg, options[j].name)) {
4310 break;
4311 }
4312 }
4313 if (options[j].name == NULL) {
4314 error_report("unrecognized operand %s", arg);
4315 ret = -1;
4316 goto out;
4317 }
4318
4319 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4320 ret = -1;
4321 goto out;
4322 }
4323 dd.flags |= options[j].flag;
4324 g_free(arg);
4325 arg = NULL;
4326 }
4327
4328 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4329 error_report("Must specify both input and output files");
4330 ret = -1;
4331 goto out;
4332 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004333
4334 if (qemu_opts_foreach(&qemu_object_opts,
4335 user_creatable_add_opts_foreach,
4336 NULL, NULL)) {
4337 ret = -1;
4338 goto out;
4339 }
4340
Fam Zheng335e9932017-05-03 00:35:39 +08004341 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4342 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004343
4344 if (!blk1) {
4345 ret = -1;
4346 goto out;
4347 }
4348
4349 drv = bdrv_find_format(out_fmt);
4350 if (!drv) {
4351 error_report("Unknown file format");
4352 ret = -1;
4353 goto out;
4354 }
4355 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4356
4357 if (!proto_drv) {
4358 error_report_err(local_err);
4359 ret = -1;
4360 goto out;
4361 }
4362 if (!drv->create_opts) {
4363 error_report("Format driver '%s' does not support image creation",
4364 drv->format_name);
4365 ret = -1;
4366 goto out;
4367 }
4368 if (!proto_drv->create_opts) {
4369 error_report("Protocol driver '%s' does not support image creation",
4370 proto_drv->format_name);
4371 ret = -1;
4372 goto out;
4373 }
4374 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4375 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4376
4377 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4378
4379 size = blk_getlength(blk1);
4380 if (size < 0) {
4381 error_report("Failed to get size for '%s'", in.filename);
4382 ret = -1;
4383 goto out;
4384 }
4385
4386 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4387 dd.count * in.bsz < size) {
4388 size = dd.count * in.bsz;
4389 }
4390
Reda Sallahif7c15532016-08-10 16:16:09 +02004391 /* Overflow means the specified offset is beyond input image's size */
4392 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4393 size < in.bsz * in.offset)) {
4394 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4395 } else {
4396 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4397 size - in.bsz * in.offset, &error_abort);
4398 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004399
4400 ret = bdrv_create(drv, out.filename, opts, &local_err);
4401 if (ret < 0) {
4402 error_reportf_err(local_err,
4403 "%s: error while creating output image: ",
4404 out.filename);
4405 ret = -1;
4406 goto out;
4407 }
4408
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004409 /* TODO, we can't honour --image-opts for the target,
4410 * since it needs to be given in a format compatible
4411 * with the bdrv_create() call above which does not
4412 * support image-opts style.
4413 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01004414 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004415 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004416
4417 if (!blk2) {
4418 ret = -1;
4419 goto out;
4420 }
4421
Reda Sallahif7c15532016-08-10 16:16:09 +02004422 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4423 size < in.offset * in.bsz)) {
4424 /* We give a warning if the skip option is bigger than the input
4425 * size and create an empty output disk image (i.e. like dd(1)).
4426 */
4427 error_report("%s: cannot skip to specified offset", in.filename);
4428 in_pos = size;
4429 } else {
4430 in_pos = in.offset * in.bsz;
4431 }
4432
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004433 in.buf = g_new(uint8_t, in.bsz);
4434
Reda Sallahif7c15532016-08-10 16:16:09 +02004435 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004436 int in_ret, out_ret;
4437
4438 if (in_pos + in.bsz > size) {
4439 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4440 } else {
4441 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4442 }
4443 if (in_ret < 0) {
4444 error_report("error while reading from input image file: %s",
4445 strerror(-in_ret));
4446 ret = -1;
4447 goto out;
4448 }
4449 in_pos += in_ret;
4450
4451 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4452
4453 if (out_ret < 0) {
4454 error_report("error while writing to output image file: %s",
4455 strerror(-out_ret));
4456 ret = -1;
4457 goto out;
4458 }
4459 out_pos += out_ret;
4460 }
4461
4462out:
4463 g_free(arg);
4464 qemu_opts_del(opts);
4465 qemu_opts_free(create_opts);
4466 blk_unref(blk1);
4467 blk_unref(blk2);
4468 g_free(in.filename);
4469 g_free(out.filename);
4470 g_free(in.buf);
4471 g_free(out.buf);
4472
4473 if (ret) {
4474 return 1;
4475 }
4476 return 0;
4477}
4478
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004479static void dump_json_block_measure_info(BlockMeasureInfo *info)
4480{
4481 QString *str;
4482 QObject *obj;
4483 Visitor *v = qobject_output_visitor_new(&obj);
4484
4485 visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
4486 visit_complete(v, &obj);
4487 str = qobject_to_json_pretty(obj);
4488 assert(str != NULL);
4489 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004490 qobject_unref(obj);
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004491 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004492 qobject_unref(str);
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004493}
4494
4495static int img_measure(int argc, char **argv)
4496{
4497 static const struct option long_options[] = {
4498 {"help", no_argument, 0, 'h'},
4499 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4500 {"object", required_argument, 0, OPTION_OBJECT},
4501 {"output", required_argument, 0, OPTION_OUTPUT},
4502 {"size", required_argument, 0, OPTION_SIZE},
4503 {"force-share", no_argument, 0, 'U'},
4504 {0, 0, 0, 0}
4505 };
4506 OutputFormat output_format = OFORMAT_HUMAN;
4507 BlockBackend *in_blk = NULL;
4508 BlockDriver *drv;
4509 const char *filename = NULL;
4510 const char *fmt = NULL;
4511 const char *out_fmt = "raw";
4512 char *options = NULL;
4513 char *snapshot_name = NULL;
4514 bool force_share = false;
4515 QemuOpts *opts = NULL;
4516 QemuOpts *object_opts = NULL;
4517 QemuOpts *sn_opts = NULL;
4518 QemuOptsList *create_opts = NULL;
4519 bool image_opts = false;
4520 uint64_t img_size = UINT64_MAX;
4521 BlockMeasureInfo *info = NULL;
4522 Error *local_err = NULL;
4523 int ret = 1;
4524 int c;
4525
4526 while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
4527 long_options, NULL)) != -1) {
4528 switch (c) {
4529 case '?':
4530 case 'h':
4531 help();
4532 break;
4533 case 'f':
4534 fmt = optarg;
4535 break;
4536 case 'O':
4537 out_fmt = optarg;
4538 break;
4539 case 'o':
4540 if (!is_valid_option_list(optarg)) {
4541 error_report("Invalid option list: %s", optarg);
4542 goto out;
4543 }
4544 if (!options) {
4545 options = g_strdup(optarg);
4546 } else {
4547 char *old_options = options;
4548 options = g_strdup_printf("%s,%s", options, optarg);
4549 g_free(old_options);
4550 }
4551 break;
4552 case 'l':
4553 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
4554 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
4555 optarg, false);
4556 if (!sn_opts) {
4557 error_report("Failed in parsing snapshot param '%s'",
4558 optarg);
4559 goto out;
4560 }
4561 } else {
4562 snapshot_name = optarg;
4563 }
4564 break;
4565 case 'U':
4566 force_share = true;
4567 break;
4568 case OPTION_OBJECT:
4569 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
4570 optarg, true);
4571 if (!object_opts) {
4572 goto out;
4573 }
4574 break;
4575 case OPTION_IMAGE_OPTS:
4576 image_opts = true;
4577 break;
4578 case OPTION_OUTPUT:
4579 if (!strcmp(optarg, "json")) {
4580 output_format = OFORMAT_JSON;
4581 } else if (!strcmp(optarg, "human")) {
4582 output_format = OFORMAT_HUMAN;
4583 } else {
4584 error_report("--output must be used with human or json "
4585 "as argument.");
4586 goto out;
4587 }
4588 break;
4589 case OPTION_SIZE:
4590 {
4591 int64_t sval;
4592
4593 sval = cvtnum(optarg);
4594 if (sval < 0) {
4595 if (sval == -ERANGE) {
4596 error_report("Image size must be less than 8 EiB!");
4597 } else {
4598 error_report("Invalid image size specified! You may use "
4599 "k, M, G, T, P or E suffixes for ");
4600 error_report("kilobytes, megabytes, gigabytes, terabytes, "
4601 "petabytes and exabytes.");
4602 }
4603 goto out;
4604 }
4605 img_size = (uint64_t)sval;
4606 }
4607 break;
4608 }
4609 }
4610
4611 if (qemu_opts_foreach(&qemu_object_opts,
4612 user_creatable_add_opts_foreach,
4613 NULL, NULL)) {
4614 goto out;
4615 }
4616
4617 if (argc - optind > 1) {
4618 error_report("At most one filename argument is allowed.");
4619 goto out;
4620 } else if (argc - optind == 1) {
4621 filename = argv[optind];
4622 }
4623
4624 if (!filename &&
4625 (object_opts || image_opts || fmt || snapshot_name || sn_opts)) {
4626 error_report("--object, --image-opts, -f, and -l "
4627 "require a filename argument.");
4628 goto out;
4629 }
4630 if (filename && img_size != UINT64_MAX) {
4631 error_report("--size N cannot be used together with a filename.");
4632 goto out;
4633 }
4634 if (!filename && img_size == UINT64_MAX) {
4635 error_report("Either --size N or one filename must be specified.");
4636 goto out;
4637 }
4638
4639 if (filename) {
4640 in_blk = img_open(image_opts, filename, fmt, 0,
4641 false, false, force_share);
4642 if (!in_blk) {
4643 goto out;
4644 }
4645
4646 if (sn_opts) {
4647 bdrv_snapshot_load_tmp(blk_bs(in_blk),
4648 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
4649 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
4650 &local_err);
4651 } else if (snapshot_name != NULL) {
4652 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
4653 snapshot_name, &local_err);
4654 }
4655 if (local_err) {
4656 error_reportf_err(local_err, "Failed to load snapshot: ");
4657 goto out;
4658 }
4659 }
4660
4661 drv = bdrv_find_format(out_fmt);
4662 if (!drv) {
4663 error_report("Unknown file format '%s'", out_fmt);
4664 goto out;
4665 }
4666 if (!drv->create_opts) {
4667 error_report("Format driver '%s' does not support image creation",
4668 drv->format_name);
4669 goto out;
4670 }
4671
4672 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4673 create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
4674 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4675 if (options) {
4676 qemu_opts_do_parse(opts, options, NULL, &local_err);
4677 if (local_err) {
4678 error_report_err(local_err);
4679 error_report("Invalid options for file format '%s'", out_fmt);
4680 goto out;
4681 }
4682 }
4683 if (img_size != UINT64_MAX) {
4684 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
4685 }
4686
4687 info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
4688 if (local_err) {
4689 error_report_err(local_err);
4690 goto out;
4691 }
4692
4693 if (output_format == OFORMAT_HUMAN) {
4694 printf("required size: %" PRIu64 "\n", info->required);
4695 printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
4696 } else {
4697 dump_json_block_measure_info(info);
4698 }
4699
4700 ret = 0;
4701
4702out:
4703 qapi_free_BlockMeasureInfo(info);
4704 qemu_opts_del(object_opts);
4705 qemu_opts_del(opts);
4706 qemu_opts_del(sn_opts);
4707 qemu_opts_free(create_opts);
4708 g_free(options);
4709 blk_unref(in_blk);
4710 return ret;
4711}
Kevin Wolfb6133b82014-08-05 14:17:13 +02004712
Anthony Liguoric227f092009-10-01 16:12:16 -05004713static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004714#define DEF(option, callback, arg_string) \
4715 { option, callback },
4716#include "qemu-img-cmds.h"
4717#undef DEF
Stuart Brady153859b2009-06-07 00:42:17 +01004718 { NULL, NULL, },
4719};
4720
bellardea2384d2004-08-01 21:59:26 +00004721int main(int argc, char **argv)
4722{
Anthony Liguoric227f092009-10-01 16:12:16 -05004723 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004724 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004725 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004726 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004727 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004728 static const struct option long_options[] = {
4729 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004730 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004731 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004732 {0, 0, 0, 0}
4733 };
bellardea2384d2004-08-01 21:59:26 +00004734
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004735#ifdef CONFIG_POSIX
4736 signal(SIGPIPE, SIG_IGN);
4737#endif
4738
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004739 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004740 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004741 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004742
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004743 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004744 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004745 exit(EXIT_FAILURE);
4746 }
4747
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004748 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004749
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004750 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004751 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004752 if (argc < 2) {
4753 error_exit("Not enough arguments");
4754 }
Stuart Brady153859b2009-06-07 00:42:17 +01004755
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004756 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004757 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004758 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004759
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004760 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004761 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004762 case ':':
4763 missing_argument(argv[optind - 1]);
4764 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08004765 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004766 unrecognized_option(argv[optind - 1]);
4767 return 0;
4768 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03004769 help();
4770 return 0;
4771 case 'V':
4772 printf(QEMU_IMG_VERSION);
4773 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004774 case 'T':
4775 g_free(trace_file);
4776 trace_file = trace_opt_parse(optarg);
4777 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004778 }
bellardea2384d2004-08-01 21:59:26 +00004779 }
Stuart Brady153859b2009-06-07 00:42:17 +01004780
Denis V. Lunev10985132016-06-17 17:44:13 +03004781 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004782
Denis V. Lunev10985132016-06-17 17:44:13 +03004783 /* reset getopt_long scanning */
4784 argc -= optind;
4785 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004786 return 0;
4787 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004788 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004789 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004790
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004791 if (!trace_init_backends()) {
4792 exit(1);
4793 }
4794 trace_init_file(trace_file);
4795 qemu_set_log(LOG_TRACE);
4796
Denis V. Lunev10985132016-06-17 17:44:13 +03004797 /* find the command */
4798 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4799 if (!strcmp(cmdname, cmd->name)) {
4800 return cmd->handler(argc, argv);
4801 }
4802 }
Jeff Cody7db16892014-04-25 17:02:32 -04004803
Stuart Brady153859b2009-06-07 00:42:17 +01004804 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004805 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004806}