blob: 53cae6bb66a176771b1d933f00e01dfdf0cd9238 [file] [log] [blame]
bellardea2384d2004-08-01 21:59:26 +00001/*
bellardfb43f4d2006-08-07 21:34:46 +00002 * QEMU disk image utility
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardea2384d2004-08-01 21:59:26 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Peter Maydell80c71a22016-01-18 18:01:42 +000024#include "qemu/osdep.h"
Eric Blakec2a3d7d2017-07-21 08:50:47 -050025#include <getopt.h>
26
Fam Zheng67a1de02016-06-01 17:44:21 +080027#include "qemu-version.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010028#include "qapi/error.h"
BenoƮt Canetc054b3f2012-09-05 13:09:02 +020029#include "qapi-visit.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010030#include "qapi/qobject-output-visitor.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010031#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010032#include "qapi/qmp/qjson.h"
Fam Zheng335e9932017-05-03 00:35:39 +080033#include "qapi/qmp/qbool.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020034#include "qemu/cutils.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000035#include "qemu/config-file.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010036#include "qemu/option.h"
37#include "qemu/error-report.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030038#include "qemu/log.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000039#include "qom/object_interfaces.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010040#include "sysemu/sysemu.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020041#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010042#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020043#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080044#include "block/qapi.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010045#include "crypto/init.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030046#include "trace/control.h"
bellarde8445332006-06-14 15:32:10 +000047
Don Slutz61979a62015-01-09 10:17:35 -050048#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
Thomas Huth0781dd62016-10-05 11:54:44 +020049 "\n" QEMU_COPYRIGHT "\n"
Jeff Cody5f6979c2014-04-28 14:37:18 -040050
Anthony Liguoric227f092009-10-01 16:12:16 -050051typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010052 const char *name;
53 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050054} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010055
Federico Simoncelli8599ea42013-01-28 06:59:47 -050056enum {
57 OPTION_OUTPUT = 256,
58 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000059 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000060 OPTION_IMAGE_OPTS = 259,
Kevin Wolfb6495fa2015-07-10 18:09:18 +020061 OPTION_PATTERN = 260,
Kevin Wolf55d539c2016-06-03 13:59:41 +020062 OPTION_FLUSH_INTERVAL = 261,
63 OPTION_NO_DRAIN = 262,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +010064 OPTION_TARGET_IMAGE_OPTS = 263,
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +010065 OPTION_SIZE = 264,
Max Reitzdc5f6902017-06-13 22:20:55 +020066 OPTION_PREALLOCATION = 265,
Pavel Butsykin4ffca892017-09-18 15:42:27 +030067 OPTION_SHRINK = 266,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050068};
69
70typedef enum OutputFormat {
71 OFORMAT_JSON,
72 OFORMAT_HUMAN,
73} OutputFormat;
74
Kevin Wolfe6996142016-03-15 13:03:11 +010075/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040076#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000077
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010078static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000079{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010080 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000081}
82
Fam Zhengac1307a2014-04-22 13:36:11 +080083static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
84{
85 va_list ap;
86
87 error_printf("qemu-img: ");
88
89 va_start(ap, fmt);
90 error_vprintf(fmt, ap);
91 va_end(ap);
92
93 error_printf("\nTry 'qemu-img --help' for more information\n");
94 exit(EXIT_FAILURE);
95}
96
Stefan Hajnoczic9192972017-03-17 18:45:41 +080097static void QEMU_NORETURN missing_argument(const char *option)
98{
99 error_exit("missing argument for option '%s'", option);
100}
101
102static void QEMU_NORETURN unrecognized_option(const char *option)
103{
104 error_exit("unrecognized option '%s'", option);
105}
106
blueswir1d2c639d2009-01-24 18:19:25 +0000107/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +0800108static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +0000109{
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100110 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -0400111 QEMU_IMG_VERSION
Denis V. Lunev10985132016-06-17 17:44:13 +0300112 "usage: qemu-img [standard options] command [command options]\n"
malc3f020d72010-02-08 12:04:56 +0300113 "QEMU disk image utility\n"
114 "\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300115 " '-h', '--help' display this help and exit\n"
116 " '-V', '--version' output version information and exit\n"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +0300117 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
118 " specify tracing options\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300119 "\n"
malc3f020d72010-02-08 12:04:56 +0300120 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100121#define DEF(option, callback, arg_string) \
122 " " arg_string "\n"
123#include "qemu-img-cmds.h"
124#undef DEF
125#undef GEN_DOCS
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)
279 && !qdict_get_bool(options, BDRV_OPT_FORCE_SHARE)) {
280 error_report("--force-share/-U conflicts with image options");
Fam Zhengadb998c2017-05-15 22:10:14 +0800281 QDECREF(options);
Fam Zheng335e9932017-05-03 00:35:39 +0800282 return NULL;
283 }
Eric Blake579cf1d2017-05-15 14:54:39 -0500284 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
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));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500563 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600564 visit_free(v);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500565 QDECREF(str);
566}
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);
sochin.jiang4172a002017-06-15 14:47:33 +0800864 block_job_ref(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);
sochin.jiang4172a002017-06-15 14:47:33 +0800869 } while (!job->ready && !job->completed);
Max Reitzd4a32382014-10-24 15:57:37 +0200870
sochin.jiang4172a002017-06-15 14:47:33 +0800871 if (!job->completed) {
872 ret = block_job_complete_sync(job, errp);
873 } else {
874 ret = job->ret;
875 }
876 block_job_unref(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);
John Snow47970df2016-10-27 12:06:57 -04001017 commit_active_start("commit", bs, base_bs, BLOCK_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/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001159 * Compares two buffers sector by sector. Returns 0 if the first sector of both
1160 * buffers matches, non-zero otherwise.
1161 *
1162 * pnum is set to the number of sectors (including and immediately following
1163 * the first one) that are known to have the same comparison result
1164 */
1165static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1166 int *pnum)
1167{
Radim KrčmÔř8c1ac472015-02-20 17:06:15 +01001168 bool res;
1169 int i;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001170
1171 if (n <= 0) {
1172 *pnum = 0;
1173 return 0;
1174 }
1175
1176 res = !!memcmp(buf1, buf2, 512);
1177 for(i = 1; i < n; i++) {
1178 buf1 += 512;
1179 buf2 += 512;
1180
1181 if (!!memcmp(buf1, buf2, 512) != res) {
1182 break;
1183 }
1184 }
1185
1186 *pnum = i;
1187 return res;
1188}
1189
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001190#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001191
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001192static int64_t sectors_to_bytes(int64_t sectors)
1193{
1194 return sectors << BDRV_SECTOR_BITS;
1195}
1196
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001197/*
1198 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1199 *
Eric Blake0608e402017-10-11 22:47:12 -05001200 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1201 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1202 * failure), and 4 on error (the exit status for read errors), after emitting
1203 * an error message.
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001204 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001205 * @param blk: BlockBackend for the image
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001206 * @param sect_num: Number of first sector to check
1207 * @param sect_count: Number of sectors to check
1208 * @param filename: Name of disk file we are checking (logging purpose)
1209 * @param buffer: Allocated buffer for storing read data
1210 * @param quiet: Flag for quiet mode
1211 */
Max Reitzf1d3cd72015-02-05 13:58:18 -05001212static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001213 int sect_count, const char *filename,
1214 uint8_t *buffer, bool quiet)
1215{
Eric Blakedebb38a2017-10-11 22:47:11 -05001216 int ret = 0;
1217 int64_t idx;
1218
Eric Blake91669202016-05-06 10:26:43 -06001219 ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer,
1220 sect_count << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001221 if (ret < 0) {
1222 error_report("Error while reading offset %" PRId64 " of %s: %s",
1223 sectors_to_bytes(sect_num), filename, strerror(-ret));
Eric Blake0608e402017-10-11 22:47:12 -05001224 return 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001225 }
Eric Blakedebb38a2017-10-11 22:47:11 -05001226 idx = find_nonzero(buffer, sect_count * BDRV_SECTOR_SIZE);
1227 if (idx >= 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001228 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blakedebb38a2017-10-11 22:47:11 -05001229 sectors_to_bytes(sect_num) + idx);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001230 return 1;
1231 }
1232
1233 return 0;
1234}
1235
1236/*
1237 * Compares two images. Exit codes:
1238 *
1239 * 0 - Images are identical
1240 * 1 - Images differ
1241 * >1 - Error occurred
1242 */
1243static int img_compare(int argc, char **argv)
1244{
Max Reitz40055952014-07-22 22:58:42 +02001245 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001246 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001247 BlockDriverState *bs1, *bs2;
1248 int64_t total_sectors1, total_sectors2;
1249 uint8_t *buf1 = NULL, *buf2 = NULL;
Eric Blake31826642017-10-11 22:47:08 -05001250 int64_t pnum1, pnum2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001251 int allocated1, allocated2;
1252 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1253 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001254 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001255 bool writethrough;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001256 int64_t total_sectors;
1257 int64_t sector_num = 0;
1258 int64_t nb_sectors;
1259 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001260 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001261 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001262 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001263
Max Reitz40055952014-07-22 22:58:42 +02001264 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001265 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001266 static const struct option long_options[] = {
1267 {"help", no_argument, 0, 'h'},
1268 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001269 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001270 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001271 {0, 0, 0, 0}
1272 };
Fam Zheng335e9932017-05-03 00:35:39 +08001273 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001274 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001275 if (c == -1) {
1276 break;
1277 }
1278 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001279 case ':':
1280 missing_argument(argv[optind - 1]);
1281 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001282 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001283 unrecognized_option(argv[optind - 1]);
1284 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001285 case 'h':
1286 help();
1287 break;
1288 case 'f':
1289 fmt1 = optarg;
1290 break;
1291 case 'F':
1292 fmt2 = optarg;
1293 break;
Max Reitz40055952014-07-22 22:58:42 +02001294 case 'T':
1295 cache = optarg;
1296 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001297 case 'p':
1298 progress = true;
1299 break;
1300 case 'q':
1301 quiet = true;
1302 break;
1303 case 's':
1304 strict = true;
1305 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001306 case 'U':
1307 force_share = true;
1308 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001309 case OPTION_OBJECT: {
1310 QemuOpts *opts;
1311 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1312 optarg, true);
1313 if (!opts) {
1314 ret = 2;
1315 goto out4;
1316 }
1317 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001318 case OPTION_IMAGE_OPTS:
1319 image_opts = true;
1320 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001321 }
1322 }
1323
1324 /* Progress is not shown in Quiet mode */
1325 if (quiet) {
1326 progress = false;
1327 }
1328
1329
Kevin Wolffc11eb22013-08-05 10:53:04 +02001330 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001331 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001332 }
1333 filename1 = argv[optind++];
1334 filename2 = argv[optind++];
1335
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001336 if (qemu_opts_foreach(&qemu_object_opts,
1337 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001338 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001339 ret = 2;
1340 goto out4;
1341 }
1342
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001343 /* Initialize before goto out */
1344 qemu_progress_init(progress, 2.0);
1345
Kevin Wolfce099542016-03-15 13:01:04 +01001346 flags = 0;
1347 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001348 if (ret < 0) {
1349 error_report("Invalid source cache option: %s", cache);
1350 ret = 2;
1351 goto out3;
1352 }
1353
Fam Zheng335e9932017-05-03 00:35:39 +08001354 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1355 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001356 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001357 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001358 goto out3;
1359 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001360
Fam Zheng335e9932017-05-03 00:35:39 +08001361 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1362 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001363 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001364 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001365 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001366 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001367 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001368 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001369
Max Reitzf1d3cd72015-02-05 13:58:18 -05001370 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1371 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1372 total_sectors1 = blk_nb_sectors(blk1);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001373 if (total_sectors1 < 0) {
1374 error_report("Can't get size of %s: %s",
1375 filename1, strerror(-total_sectors1));
1376 ret = 4;
1377 goto out;
1378 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05001379 total_sectors2 = blk_nb_sectors(blk2);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001380 if (total_sectors2 < 0) {
1381 error_report("Can't get size of %s: %s",
1382 filename2, strerror(-total_sectors2));
1383 ret = 4;
1384 goto out;
1385 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001386 total_sectors = MIN(total_sectors1, total_sectors2);
1387 progress_base = MAX(total_sectors1, total_sectors2);
1388
1389 qemu_progress_print(0, 100);
1390
1391 if (strict && total_sectors1 != total_sectors2) {
1392 ret = 1;
1393 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1394 goto out;
1395 }
1396
Eric Blake7daddc62017-10-11 22:47:09 -05001397 while (sector_num < total_sectors) {
Eric Blake31826642017-10-11 22:47:08 -05001398 int status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001399
Eric Blake31826642017-10-11 22:47:08 -05001400 status1 = bdrv_block_status_above(bs1, NULL,
1401 sector_num * BDRV_SECTOR_SIZE,
1402 (total_sectors1 - sector_num) *
1403 BDRV_SECTOR_SIZE,
1404 &pnum1, NULL, NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001405 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001406 ret = 3;
1407 error_report("Sector allocation test failed for %s", filename1);
1408 goto out;
1409 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001410 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001411
Eric Blake31826642017-10-11 22:47:08 -05001412 status2 = bdrv_block_status_above(bs2, NULL,
1413 sector_num * BDRV_SECTOR_SIZE,
1414 (total_sectors2 - sector_num) *
1415 BDRV_SECTOR_SIZE,
1416 &pnum2, NULL, NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001417 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001418 ret = 3;
1419 error_report("Sector allocation test failed for %s", filename2);
1420 goto out;
1421 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001422 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
Eric Blake31826642017-10-11 22:47:08 -05001423 /* TODO: Relax this once comparison is byte-based, and we no longer
1424 * have to worry about sector alignment */
1425 assert(QEMU_IS_ALIGNED(pnum1 | pnum2, BDRV_SECTOR_SIZE));
Eric Blake7daddc62017-10-11 22:47:09 -05001426
1427 assert(pnum1 && pnum2);
1428 nb_sectors = MIN(pnum1, pnum2) >> BDRV_SECTOR_BITS;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001429
Fam Zheng25ad8e62016-01-13 16:37:41 +08001430 if (strict) {
Eric Blake31826642017-10-11 22:47:08 -05001431 if (status1 != status2) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001432 ret = 1;
1433 qprintf(quiet, "Strict mode: Offset %" PRId64
1434 " block status mismatch!\n",
1435 sectors_to_bytes(sector_num));
1436 goto out;
1437 }
1438 }
1439 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
Eric Blake7daddc62017-10-11 22:47:09 -05001440 /* nothing to do */
Fam Zheng25ad8e62016-01-13 16:37:41 +08001441 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001442 if (allocated1) {
Eric Blake7daddc62017-10-11 22:47:09 -05001443 nb_sectors = MIN(nb_sectors, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
Eric Blake91669202016-05-06 10:26:43 -06001444 ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1,
1445 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001446 if (ret < 0) {
1447 error_report("Error while reading offset %" PRId64 " of %s:"
1448 " %s", sectors_to_bytes(sector_num), filename1,
1449 strerror(-ret));
1450 ret = 4;
1451 goto out;
1452 }
Eric Blake91669202016-05-06 10:26:43 -06001453 ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2,
1454 nb_sectors << BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001455 if (ret < 0) {
1456 error_report("Error while reading offset %" PRId64
1457 " of %s: %s", sectors_to_bytes(sector_num),
1458 filename2, strerror(-ret));
1459 ret = 4;
1460 goto out;
1461 }
1462 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1463 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001464 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1465 sectors_to_bytes(
1466 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001467 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001468 goto out;
1469 }
1470 }
1471 } else {
Eric Blake7daddc62017-10-11 22:47:09 -05001472 nb_sectors = MIN(nb_sectors, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001473 if (allocated1) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001474 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001475 filename1, buf1, quiet);
1476 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001477 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001478 filename2, buf1, quiet);
1479 }
1480 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001481 goto out;
1482 }
1483 }
1484 sector_num += nb_sectors;
1485 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1486 }
1487
1488 if (total_sectors1 != total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001489 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001490 const char *filename_over;
1491
1492 qprintf(quiet, "Warning: Image size mismatch!\n");
1493 if (total_sectors1 > total_sectors2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001494 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001495 filename_over = filename1;
1496 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001497 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001498 filename_over = filename2;
1499 }
1500
Eric Blake7daddc62017-10-11 22:47:09 -05001501 while (sector_num < progress_base) {
Eric Blake51b0a482017-07-07 07:44:59 -05001502 int64_t count;
1503
Eric Blake391cb1a2017-10-11 22:47:10 -05001504 ret = bdrv_block_status_above(blk_bs(blk_over), NULL,
Eric Blake51b0a482017-07-07 07:44:59 -05001505 sector_num * BDRV_SECTOR_SIZE,
Eric Blake7daddc62017-10-11 22:47:09 -05001506 (progress_base - sector_num) *
1507 BDRV_SECTOR_SIZE,
Eric Blake391cb1a2017-10-11 22:47:10 -05001508 &count, NULL, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001509 if (ret < 0) {
1510 ret = 3;
1511 error_report("Sector allocation test failed for %s",
1512 filename_over);
1513 goto out;
1514
1515 }
Eric Blake391cb1a2017-10-11 22:47:10 -05001516 /* TODO relax this once bdrv_block_status_above does not enforce
Eric Blake51b0a482017-07-07 07:44:59 -05001517 * sector alignment */
1518 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
1519 nb_sectors = count >> BDRV_SECTOR_BITS;
Eric Blake391cb1a2017-10-11 22:47:10 -05001520 if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) {
Eric Blake7daddc62017-10-11 22:47:09 -05001521 nb_sectors = MIN(nb_sectors, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
Max Reitzf1d3cd72015-02-05 13:58:18 -05001522 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001523 filename_over, buf1, quiet);
1524 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001525 goto out;
1526 }
1527 }
1528 sector_num += nb_sectors;
1529 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1530 }
1531 }
1532
1533 qprintf(quiet, "Images are identical.\n");
1534 ret = 0;
1535
1536out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001537 qemu_vfree(buf1);
1538 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001539 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001540out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001541 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001542out3:
1543 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001544out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001545 return ret;
1546}
1547
Kevin Wolf690c7302015-03-19 13:33:32 +01001548enum ImgConvertBlockStatus {
1549 BLK_DATA,
1550 BLK_ZERO,
1551 BLK_BACKING_FILE,
1552};
1553
Peter Lieven2d9187b2017-02-28 13:40:07 +01001554#define MAX_COROUTINES 16
1555
Kevin Wolf690c7302015-03-19 13:33:32 +01001556typedef struct ImgConvertState {
1557 BlockBackend **src;
1558 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001559 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001560 int64_t total_sectors;
1561 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001562 int64_t allocated_done;
1563 int64_t sector_num;
1564 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001565 enum ImgConvertBlockStatus status;
1566 int64_t sector_next_status;
1567 BlockBackend *target;
1568 bool has_zero_init;
1569 bool compressed;
1570 bool target_has_backing;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001571 bool wr_in_order;
Kevin Wolf690c7302015-03-19 13:33:32 +01001572 int min_sparse;
1573 size_t cluster_sectors;
1574 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001575 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001576 int running_coroutines;
1577 Coroutine *co[MAX_COROUTINES];
1578 int64_t wait_sector_num[MAX_COROUTINES];
1579 CoMutex lock;
1580 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001581} ImgConvertState;
1582
Peter Lieven2d9187b2017-02-28 13:40:07 +01001583static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1584 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001585{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001586 *src_cur = 0;
1587 *src_cur_offset = 0;
1588 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1589 *src_cur_offset += s->src_sectors[*src_cur];
1590 (*src_cur)++;
1591 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001592 }
1593}
1594
1595static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1596{
Eric Blake31826642017-10-11 22:47:08 -05001597 int64_t src_cur_offset;
1598 int ret, n, src_cur;
Kevin Wolf690c7302015-03-19 13:33:32 +01001599
Peter Lieven2d9187b2017-02-28 13:40:07 +01001600 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001601
1602 assert(s->total_sectors > sector_num);
1603 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1604
1605 if (s->sector_next_status <= sector_num) {
Eric Blake31826642017-10-11 22:47:08 -05001606 int64_t count = n * BDRV_SECTOR_SIZE;
1607
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001608 if (s->target_has_backing) {
Eric Blake237d78f2017-10-11 22:47:03 -05001609
1610 ret = bdrv_block_status(blk_bs(s->src[src_cur]),
1611 (sector_num - src_cur_offset) *
1612 BDRV_SECTOR_SIZE,
1613 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001614 } else {
Eric Blake31826642017-10-11 22:47:08 -05001615 ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL,
1616 (sector_num - src_cur_offset) *
1617 BDRV_SECTOR_SIZE,
1618 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001619 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001620 if (ret < 0) {
1621 return ret;
1622 }
Eric Blake31826642017-10-11 22:47:08 -05001623 n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
Kevin Wolf690c7302015-03-19 13:33:32 +01001624
1625 if (ret & BDRV_BLOCK_ZERO) {
1626 s->status = BLK_ZERO;
1627 } else if (ret & BDRV_BLOCK_DATA) {
1628 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001629 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001630 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001631 }
1632
1633 s->sector_next_status = sector_num + n;
1634 }
1635
1636 n = MIN(n, s->sector_next_status - sector_num);
1637 if (s->status == BLK_DATA) {
1638 n = MIN(n, s->buf_sectors);
1639 }
1640
1641 /* We need to write complete clusters for compressed images, so if an
1642 * unallocated area is shorter than that, we must consider the whole
1643 * cluster allocated. */
1644 if (s->compressed) {
1645 if (n < s->cluster_sectors) {
1646 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1647 s->status = BLK_DATA;
1648 } else {
1649 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1650 }
1651 }
1652
1653 return n;
1654}
1655
Peter Lieven2d9187b2017-02-28 13:40:07 +01001656static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1657 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001658{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001659 int n, ret;
1660 QEMUIOVector qiov;
1661 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001662
Kevin Wolf690c7302015-03-19 13:33:32 +01001663 assert(nb_sectors <= s->buf_sectors);
1664 while (nb_sectors > 0) {
1665 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001666 int src_cur;
1667 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001668
1669 /* In the case of compression with multiple source files, we can get a
1670 * nb_sectors that spreads into the next part. So we must be able to
1671 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001672 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1673 blk = s->src[src_cur];
1674 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001675
Peter Lieven2d9187b2017-02-28 13:40:07 +01001676 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1677 iov.iov_base = buf;
1678 iov.iov_len = n << BDRV_SECTOR_BITS;
1679 qemu_iovec_init_external(&qiov, &iov, 1);
1680
1681 ret = blk_co_preadv(
1682 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1683 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001684 if (ret < 0) {
1685 return ret;
1686 }
1687
1688 sector_num += n;
1689 nb_sectors -= n;
1690 buf += n * BDRV_SECTOR_SIZE;
1691 }
1692
1693 return 0;
1694}
1695
Peter Lieven2d9187b2017-02-28 13:40:07 +01001696
1697static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1698 int nb_sectors, uint8_t *buf,
1699 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001700{
1701 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001702 QEMUIOVector qiov;
1703 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001704
1705 while (nb_sectors > 0) {
1706 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001707 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1708
Peter Lieven2d9187b2017-02-28 13:40:07 +01001709 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001710 case BLK_BACKING_FILE:
1711 /* If we have a backing file, leave clusters unallocated that are
1712 * unallocated in the source image, so that the backing file is
1713 * visible at the respective offset. */
1714 assert(s->target_has_backing);
1715 break;
1716
1717 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001718 /* If we're told to keep the target fully allocated (-S 0) or there
1719 * is real non-zero data, we must write it. Otherwise we can treat
1720 * it as zero sectors.
1721 * Compressed clusters need to be written as a whole, so in that
1722 * case we can only save the write if the buffer is completely
1723 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001724 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001725 (!s->compressed &&
1726 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1727 (s->compressed &&
1728 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001729 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001730 iov.iov_base = buf;
1731 iov.iov_len = n << BDRV_SECTOR_BITS;
1732 qemu_iovec_init_external(&qiov, &iov, 1);
1733
1734 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
Lidong Chendb933fb2017-04-27 10:58:27 +08001735 n << BDRV_SECTOR_BITS, &qiov, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001736 if (ret < 0) {
1737 return ret;
1738 }
1739 break;
1740 }
1741 /* fall-through */
1742
1743 case BLK_ZERO:
1744 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001745 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001746 break;
1747 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001748 ret = blk_co_pwrite_zeroes(s->target,
1749 sector_num << BDRV_SECTOR_BITS,
1750 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001751 if (ret < 0) {
1752 return ret;
1753 }
1754 break;
1755 }
1756
1757 sector_num += n;
1758 nb_sectors -= n;
1759 buf += n * BDRV_SECTOR_SIZE;
1760 }
1761
1762 return 0;
1763}
1764
Peter Lieven2d9187b2017-02-28 13:40:07 +01001765static void coroutine_fn convert_co_do_copy(void *opaque)
1766{
1767 ImgConvertState *s = opaque;
1768 uint8_t *buf = NULL;
1769 int ret, i;
1770 int index = -1;
1771
1772 for (i = 0; i < s->num_coroutines; i++) {
1773 if (s->co[i] == qemu_coroutine_self()) {
1774 index = i;
1775 break;
1776 }
1777 }
1778 assert(index >= 0);
1779
1780 s->running_coroutines++;
1781 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1782
1783 while (1) {
1784 int n;
1785 int64_t sector_num;
1786 enum ImgConvertBlockStatus status;
1787
1788 qemu_co_mutex_lock(&s->lock);
1789 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1790 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001791 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001792 }
1793 n = convert_iteration_sectors(s, s->sector_num);
1794 if (n < 0) {
1795 qemu_co_mutex_unlock(&s->lock);
1796 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001797 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001798 }
1799 /* save current sector and allocation status to local variables */
1800 sector_num = s->sector_num;
1801 status = s->status;
1802 if (!s->min_sparse && s->status == BLK_ZERO) {
1803 n = MIN(n, s->buf_sectors);
1804 }
1805 /* increment global sector counter so that other coroutines can
1806 * already continue reading beyond this request */
1807 s->sector_num += n;
1808 qemu_co_mutex_unlock(&s->lock);
1809
1810 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1811 s->allocated_done += n;
1812 qemu_progress_print(100.0 * s->allocated_done /
1813 s->allocated_sectors, 0);
1814 }
1815
1816 if (status == BLK_DATA) {
1817 ret = convert_co_read(s, sector_num, n, buf);
1818 if (ret < 0) {
1819 error_report("error while reading sector %" PRId64
1820 ": %s", sector_num, strerror(-ret));
1821 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001822 }
1823 } else if (!s->min_sparse && status == BLK_ZERO) {
1824 status = BLK_DATA;
1825 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1826 }
1827
1828 if (s->wr_in_order) {
1829 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03001830 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001831 s->wait_sector_num[index] = sector_num;
1832 qemu_coroutine_yield();
1833 }
1834 s->wait_sector_num[index] = -1;
1835 }
1836
Anton Nefedovb91127e2017-04-26 11:33:15 +03001837 if (s->ret == -EINPROGRESS) {
1838 ret = convert_co_write(s, sector_num, n, buf, status);
1839 if (ret < 0) {
1840 error_report("error while writing sector %" PRId64
1841 ": %s", sector_num, strerror(-ret));
1842 s->ret = ret;
1843 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001844 }
1845
1846 if (s->wr_in_order) {
1847 /* reenter the coroutine that might have waited
1848 * for this write to complete */
1849 s->wr_offs = sector_num + n;
1850 for (i = 0; i < s->num_coroutines; i++) {
1851 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1852 /*
1853 * A -> B -> A cannot occur because A has
1854 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1855 * B will never enter A during this time window.
1856 */
1857 qemu_coroutine_enter(s->co[i]);
1858 break;
1859 }
1860 }
1861 }
1862 }
1863
Peter Lieven2d9187b2017-02-28 13:40:07 +01001864 qemu_vfree(buf);
1865 s->co[index] = NULL;
1866 s->running_coroutines--;
1867 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1868 /* the convert job finished successfully */
1869 s->ret = 0;
1870 }
1871}
1872
Kevin Wolf690c7302015-03-19 13:33:32 +01001873static int convert_do_copy(ImgConvertState *s)
1874{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001875 int ret, i, n;
1876 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001877
1878 /* Check whether we have zero initialisation or can get it efficiently */
1879 s->has_zero_init = s->min_sparse && !s->target_has_backing
1880 ? bdrv_has_zero_init(blk_bs(s->target))
1881 : false;
1882
1883 if (!s->has_zero_init && !s->target_has_backing &&
1884 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1885 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001886 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001887 if (ret == 0) {
1888 s->has_zero_init = true;
1889 }
1890 }
1891
1892 /* Allocate buffer for copied data. For compressed images, only one cluster
1893 * can be copied at a time. */
1894 if (s->compressed) {
1895 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1896 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001897 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001898 }
1899 s->buf_sectors = s->cluster_sectors;
1900 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001901
Kevin Wolf690c7302015-03-19 13:33:32 +01001902 while (sector_num < s->total_sectors) {
1903 n = convert_iteration_sectors(s, sector_num);
1904 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001905 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001906 }
Max Reitzaad15de2016-03-24 23:33:57 +01001907 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1908 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001909 s->allocated_sectors += n;
1910 }
1911 sector_num += n;
1912 }
1913
1914 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001915 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001916 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001917
Peter Lieven2d9187b2017-02-28 13:40:07 +01001918 qemu_co_mutex_init(&s->lock);
1919 for (i = 0; i < s->num_coroutines; i++) {
1920 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1921 s->wait_sector_num[i] = -1;
1922 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001923 }
1924
Anton Nefedovb91127e2017-04-26 11:33:15 +03001925 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001926 main_loop_wait(false);
1927 }
1928
1929 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001930 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001931 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001932 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001933 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001934 }
1935 }
1936
Peter Lieven2d9187b2017-02-28 13:40:07 +01001937 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001938}
1939
bellardea2384d2004-08-01 21:59:26 +00001940static int img_convert(int argc, char **argv)
1941{
Peter Lieven9fd77f92017-04-21 11:11:55 +02001942 int c, bs_i, flags, src_flags = 0;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001943 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02001944 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1945 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001946 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00001947 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001948 BlockDriverState *out_bs;
1949 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08001950 QemuOptsList *create_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001951 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02001952 Error *local_err = NULL;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001953 bool writethrough, src_writethrough, quiet = false, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001954 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001955 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08001956 bool force_share = false;
bellardea2384d2004-08-01 21:59:26 +00001957
Peter Lieven9fd77f92017-04-21 11:11:55 +02001958 ImgConvertState s = (ImgConvertState) {
1959 /* Need at least 4k of zeros for sparse detection */
1960 .min_sparse = 8,
1961 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1962 .wr_in_order = true,
1963 .num_coroutines = 8,
1964 };
1965
bellardea2384d2004-08-01 21:59:26 +00001966 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001967 static const struct option long_options[] = {
1968 {"help", no_argument, 0, 'h'},
1969 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001970 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001971 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001972 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001973 {0, 0, 0, 0}
1974 };
Daniel P. Berrange6b4df542017-07-04 13:30:09 +01001975 c = getopt_long(argc, argv, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001976 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001977 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001978 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001979 }
bellardea2384d2004-08-01 21:59:26 +00001980 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001981 case ':':
1982 missing_argument(argv[optind - 1]);
1983 break;
Jes Sorensenef873942010-12-06 15:25:40 +01001984 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001985 unrecognized_option(argv[optind - 1]);
1986 break;
bellardea2384d2004-08-01 21:59:26 +00001987 case 'h':
1988 help();
1989 break;
1990 case 'f':
1991 fmt = optarg;
1992 break;
1993 case 'O':
1994 out_fmt = optarg;
1995 break;
thsf58c7b32008-06-05 21:53:49 +00001996 case 'B':
1997 out_baseimg = optarg;
1998 break;
bellardea2384d2004-08-01 21:59:26 +00001999 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002000 s.compressed = true;
bellardea2384d2004-08-01 21:59:26 +00002001 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002002 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01002003 if (!is_valid_option_list(optarg)) {
2004 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002005 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01002006 }
2007 if (!options) {
2008 options = g_strdup(optarg);
2009 } else {
2010 char *old_options = options;
2011 options = g_strdup_printf("%s,%s", options, optarg);
2012 g_free(old_options);
2013 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002014 break;
edison51ef6722010-09-21 19:58:41 -07002015 case 's':
2016 snapshot_name = optarg;
2017 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08002018 case 'l':
2019 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01002020 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2021 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08002022 if (!sn_opts) {
2023 error_report("Failed in parsing snapshot param '%s'",
2024 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002025 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002026 }
2027 } else {
2028 snapshot_name = optarg;
2029 }
2030 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002031 case 'S':
2032 {
2033 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002034
2035 sval = cvtnum(optarg);
2036 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02002037 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002038 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002039 }
2040
Peter Lieven9fd77f92017-04-21 11:11:55 +02002041 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002042 break;
2043 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002044 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002045 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002046 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002047 case 't':
2048 cache = optarg;
2049 break;
Max Reitz40055952014-07-22 22:58:42 +02002050 case 'T':
2051 src_cache = optarg;
2052 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002053 case 'q':
2054 quiet = true;
2055 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002056 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002057 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002058 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002059 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002060 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2061 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002062 error_report("Invalid number of coroutines. Allowed number of"
2063 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002064 goto fail_getopt;
2065 }
2066 break;
2067 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002068 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002069 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002070 case 'U':
2071 force_share = true;
2072 break;
Max Reitz3258b912017-04-26 15:46:47 +02002073 case OPTION_OBJECT: {
2074 QemuOpts *object_opts;
2075 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2076 optarg, true);
2077 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002078 goto fail_getopt;
2079 }
2080 break;
Max Reitz3258b912017-04-26 15:46:47 +02002081 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002082 case OPTION_IMAGE_OPTS:
2083 image_opts = true;
2084 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002085 case OPTION_TARGET_IMAGE_OPTS:
2086 tgt_image_opts = true;
2087 break;
bellardea2384d2004-08-01 21:59:26 +00002088 }
2089 }
ths3b46e622007-09-17 08:09:54 +00002090
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002091 if (!out_fmt && !tgt_image_opts) {
2092 out_fmt = "raw";
2093 }
2094
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002095 if (qemu_opts_foreach(&qemu_object_opts,
2096 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002097 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002098 goto fail_getopt;
2099 }
2100
Peter Lieven9fd77f92017-04-21 11:11:55 +02002101 if (!s.wr_in_order && s.compressed) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002102 error_report("Out of order write and compress are mutually exclusive");
Peter Lieven9fd77f92017-04-21 11:11:55 +02002103 goto fail_getopt;
2104 }
2105
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002106 if (tgt_image_opts && !skip_create) {
2107 error_report("--target-image-opts requires use of -n flag");
2108 goto fail_getopt;
2109 }
2110
Peter Lieven9fd77f92017-04-21 11:11:55 +02002111 s.src_num = argc - optind - 1;
2112 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2113
2114 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002115 if (out_fmt) {
2116 ret = print_block_option_help(out_filename, out_fmt);
2117 goto fail_getopt;
2118 } else {
2119 error_report("Option help requires a format be specified");
2120 goto fail_getopt;
2121 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002122 }
2123
2124 if (s.src_num < 1) {
2125 error_report("Must specify image file name");
2126 goto fail_getopt;
2127 }
2128
2129
Peter Lieven9fd77f92017-04-21 11:11:55 +02002130 /* ret is still -EINVAL until here */
2131 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2132 if (ret < 0) {
2133 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002134 goto fail_getopt;
2135 }
2136
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002137 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002138 if (quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002139 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002140 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002141 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002142 qemu_progress_print(0, 100);
2143
Peter Lieven9fd77f92017-04-21 11:11:55 +02002144 s.src = g_new0(BlockBackend *, s.src_num);
2145 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002146
Peter Lieven9fd77f92017-04-21 11:11:55 +02002147 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2148 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Fam Zheng335e9932017-05-03 00:35:39 +08002149 fmt, src_flags, src_writethrough, quiet,
2150 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002151 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002152 ret = -1;
2153 goto out;
2154 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002155 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2156 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002157 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002158 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002159 ret = -1;
2160 goto out;
2161 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002162 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002163 }
bellardea2384d2004-08-01 21:59:26 +00002164
Wenchao Xiaef806542013-12-04 17:10:57 +08002165 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002166 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002167 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2168 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2169 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002170 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002171 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002172 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002173 ret = -1;
2174 goto out;
2175 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002176
Peter Lieven9fd77f92017-04-21 11:11:55 +02002177 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2178 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002179 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002180 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002181 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002182 ret = -1;
2183 goto out;
edison51ef6722010-09-21 19:58:41 -07002184 }
2185
Max Reitz2e024cd2015-02-11 09:58:46 -05002186 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002187 /* Find driver and parse its options */
2188 drv = bdrv_find_format(out_fmt);
2189 if (!drv) {
2190 error_report("Unknown file format '%s'", out_fmt);
2191 ret = -1;
2192 goto out;
2193 }
2194
2195 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2196 if (!proto_drv) {
2197 error_report_err(local_err);
2198 ret = -1;
2199 goto out;
2200 }
2201
Max Reitz2e024cd2015-02-11 09:58:46 -05002202 if (!drv->create_opts) {
2203 error_report("Format driver '%s' does not support image creation",
2204 drv->format_name);
2205 ret = -1;
2206 goto out;
2207 }
Max Reitzf75613c2014-12-02 18:32:46 +01002208
Max Reitz2e024cd2015-02-11 09:58:46 -05002209 if (!proto_drv->create_opts) {
2210 error_report("Protocol driver '%s' does not support image creation",
2211 proto_drv->format_name);
2212 ret = -1;
2213 goto out;
2214 }
Max Reitzf75613c2014-12-02 18:32:46 +01002215
Max Reitz2e024cd2015-02-11 09:58:46 -05002216 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2217 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002218
Max Reitz2e024cd2015-02-11 09:58:46 -05002219 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002220 if (options) {
2221 qemu_opts_do_parse(opts, options, NULL, &local_err);
2222 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002223 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002224 ret = -1;
2225 goto out;
2226 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002227 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002228
Peter Lieven9fd77f92017-04-21 11:11:55 +02002229 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002230 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002231 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2232 if (ret < 0) {
2233 goto out;
2234 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002235 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002236
Kevin Wolfa18953f2010-10-14 15:46:04 +02002237 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002238 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002239 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002240 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002241 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002242 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002243
Max Reitz48758a82017-04-26 15:46:48 +02002244 if (s.src_num > 1 && out_baseimg) {
2245 error_report("Having a backing file for the target makes no sense when "
2246 "concatenating multiple input images");
2247 ret = -1;
2248 goto out;
2249 }
2250
Kevin Wolfefa84d42009-05-18 16:42:12 +02002251 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002252 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002253 bool encryption =
2254 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002255 const char *encryptfmt =
2256 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
Chunyan Liu83d05212014-06-05 17:20:51 +08002257 const char *preallocation =
2258 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002259
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002260 if (drv && !drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002261 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002262 ret = -1;
2263 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002264 }
2265
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002266 if (encryption || encryptfmt) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002267 error_report("Compression and encryption not supported at "
2268 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002269 ret = -1;
2270 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002271 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002272
Chunyan Liu83d05212014-06-05 17:20:51 +08002273 if (preallocation
2274 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002275 {
2276 error_report("Compression and preallocation not supported at "
2277 "the same time");
2278 ret = -1;
2279 goto out;
2280 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002281 }
2282
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002283 if (!skip_create) {
2284 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002285 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002286 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002287 error_reportf_err(local_err, "%s: error while converting %s: ",
2288 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002289 goto out;
bellardea2384d2004-08-01 21:59:26 +00002290 }
2291 }
ths3b46e622007-09-17 08:09:54 +00002292
Peter Lieven9fd77f92017-04-21 11:11:55 +02002293 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002294 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002295 if (ret < 0) {
2296 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002297 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002298 }
2299
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002300 if (skip_create) {
2301 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2302 flags, writethrough, quiet, false);
2303 } else {
2304 /* TODO ultimately we should allow --target-image-opts
2305 * to be used even when -n is not given.
2306 * That has to wait for bdrv_create to be improved
2307 * to allow filenames in option syntax
2308 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01002309 s.target = img_open_new_file(out_filename, opts, out_fmt,
2310 flags, writethrough, quiet, false);
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002311 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002312 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002313 ret = -1;
2314 goto out;
2315 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002316 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002317
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002318 if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2319 error_report("Compression not supported for this file format");
2320 ret = -1;
2321 goto out;
2322 }
2323
Eric Blake5def6b82016-06-23 16:37:19 -06002324 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002325 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2326 * as maximum. */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002327 s.buf_sectors = MIN(32768,
2328 MAX(s.buf_sectors,
2329 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2330 out_bs->bl.pdiscard_alignment >>
2331 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002332
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002333 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002334 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002335 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002336 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002337 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002338 ret = -1;
2339 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002340 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002341 error_report("output file is smaller than input file");
2342 ret = -1;
2343 goto out;
2344 }
2345 }
2346
Peter Lieven24f833c2013-11-27 11:07:07 +01002347 ret = bdrv_get_info(out_bs, &bdi);
2348 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002349 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002350 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002351 goto out;
2352 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002353 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002354 s.compressed = s.compressed || bdi.needs_compressed_writes;
2355 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Peter Lieven24f833c2013-11-27 11:07:07 +01002356 }
2357
Peter Lieven9fd77f92017-04-21 11:11:55 +02002358 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002359out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002360 if (!ret) {
2361 qemu_progress_print(100, 0);
2362 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002363 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002364 qemu_opts_del(opts);
2365 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002366 qemu_opts_del(sn_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002367 blk_unref(s.target);
2368 if (s.src) {
2369 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2370 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002371 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002372 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002373 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002374 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002375fail_getopt:
2376 g_free(options);
2377
Peter Lieven9fd77f92017-04-21 11:11:55 +02002378 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002379}
2380
bellard57d1a2b2004-08-03 21:15:11 +00002381
bellardfaea38e2006-08-05 21:31:00 +00002382static void dump_snapshots(BlockDriverState *bs)
2383{
2384 QEMUSnapshotInfo *sn_tab, *sn;
2385 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002386
2387 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2388 if (nb_sns <= 0)
2389 return;
2390 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002391 bdrv_snapshot_dump(fprintf, stdout, NULL);
2392 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002393 for(i = 0; i < nb_sns; i++) {
2394 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002395 bdrv_snapshot_dump(fprintf, stdout, sn);
2396 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002397 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002398 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002399}
2400
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002401static void dump_json_image_info_list(ImageInfoList *list)
2402{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002403 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002404 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002405 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002406
2407 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2408 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002409 str = qobject_to_json_pretty(obj);
2410 assert(str != NULL);
2411 printf("%s\n", qstring_get_str(str));
2412 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002413 visit_free(v);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002414 QDECREF(str);
2415}
2416
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002417static void dump_json_image_info(ImageInfo *info)
2418{
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002419 QString *str;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002420 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002421 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002422
2423 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2424 visit_complete(v, &obj);
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002425 str = qobject_to_json_pretty(obj);
2426 assert(str != NULL);
2427 printf("%s\n", qstring_get_str(str));
2428 qobject_decref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002429 visit_free(v);
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002430 QDECREF(str);
2431}
2432
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002433static void dump_human_image_info_list(ImageInfoList *list)
2434{
2435 ImageInfoList *elem;
2436 bool delim = false;
2437
2438 for (elem = list; elem; elem = elem->next) {
2439 if (delim) {
2440 printf("\n");
2441 }
2442 delim = true;
2443
Wenchao Xia5b917042013-05-25 11:09:45 +08002444 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002445 }
2446}
2447
2448static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2449{
2450 return strcmp(a, b) == 0;
2451}
2452
2453/**
2454 * Open an image file chain and return an ImageInfoList
2455 *
2456 * @filename: topmost image filename
2457 * @fmt: topmost image format (may be NULL to autodetect)
2458 * @chain: true - enumerate entire backing file chain
2459 * false - only topmost image file
2460 *
2461 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2462 * image file. If there was an error a message will have been printed to
2463 * stderr.
2464 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002465static ImageInfoList *collect_image_info_list(bool image_opts,
2466 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002467 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002468 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002469{
2470 ImageInfoList *head = NULL;
2471 ImageInfoList **last = &head;
2472 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002473 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002474
2475 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2476
2477 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002478 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002479 BlockDriverState *bs;
2480 ImageInfo *info;
2481 ImageInfoList *elem;
2482
2483 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2484 error_report("Backing file '%s' creates an infinite loop.",
2485 filename);
2486 goto err;
2487 }
2488 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2489
Max Reitzefaa7c42016-03-16 19:54:38 +01002490 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002491 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2492 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002493 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002494 goto err;
2495 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002496 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002497
Wenchao Xia43526ec2013-06-06 12:27:58 +08002498 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002499 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002500 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002501 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002502 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002503 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002504
2505 elem = g_new0(ImageInfoList, 1);
2506 elem->value = info;
2507 *last = elem;
2508 last = &elem->next;
2509
Markus Armbruster26f54e92014-10-07 13:59:04 +02002510 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002511
2512 filename = fmt = NULL;
2513 if (chain) {
2514 if (info->has_full_backing_filename) {
2515 filename = info->full_backing_filename;
2516 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002517 error_report("Could not determine absolute backing filename,"
2518 " but backing filename '%s' present",
2519 info->backing_filename);
2520 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002521 }
2522 if (info->has_backing_filename_format) {
2523 fmt = info->backing_filename_format;
2524 }
2525 }
2526 }
2527 g_hash_table_destroy(filenames);
2528 return head;
2529
2530err:
2531 qapi_free_ImageInfoList(head);
2532 g_hash_table_destroy(filenames);
2533 return NULL;
2534}
2535
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002536static int img_info(int argc, char **argv)
2537{
2538 int c;
2539 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002540 bool chain = false;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002541 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002542 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002543 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002544 bool force_share = false;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002545
bellardea2384d2004-08-01 21:59:26 +00002546 fmt = NULL;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002547 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002548 for(;;) {
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002549 int option_index = 0;
2550 static const struct option long_options[] = {
2551 {"help", no_argument, 0, 'h'},
2552 {"format", required_argument, 0, 'f'},
2553 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002554 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002555 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002556 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002557 {"force-share", no_argument, 0, 'U'},
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002558 {0, 0, 0, 0}
2559 };
Fam Zheng335e9932017-05-03 00:35:39 +08002560 c = getopt_long(argc, argv, ":f:hU",
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002561 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002562 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002563 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002564 }
bellardea2384d2004-08-01 21:59:26 +00002565 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002566 case ':':
2567 missing_argument(argv[optind - 1]);
2568 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002569 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002570 unrecognized_option(argv[optind - 1]);
2571 break;
bellardea2384d2004-08-01 21:59:26 +00002572 case 'h':
2573 help();
2574 break;
2575 case 'f':
2576 fmt = optarg;
2577 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002578 case 'U':
2579 force_share = true;
2580 break;
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002581 case OPTION_OUTPUT:
2582 output = optarg;
2583 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002584 case OPTION_BACKING_CHAIN:
2585 chain = true;
2586 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002587 case OPTION_OBJECT: {
2588 QemuOpts *opts;
2589 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2590 optarg, true);
2591 if (!opts) {
2592 return 1;
2593 }
2594 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002595 case OPTION_IMAGE_OPTS:
2596 image_opts = true;
2597 break;
bellardea2384d2004-08-01 21:59:26 +00002598 }
2599 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002600 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002601 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002602 }
bellardea2384d2004-08-01 21:59:26 +00002603 filename = argv[optind++];
2604
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002605 if (output && !strcmp(output, "json")) {
2606 output_format = OFORMAT_JSON;
2607 } else if (output && !strcmp(output, "human")) {
2608 output_format = OFORMAT_HUMAN;
2609 } else if (output) {
2610 error_report("--output must be used with human or json as argument.");
2611 return 1;
2612 }
2613
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002614 if (qemu_opts_foreach(&qemu_object_opts,
2615 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002616 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002617 return 1;
2618 }
2619
Fam Zheng335e9932017-05-03 00:35:39 +08002620 list = collect_image_info_list(image_opts, filename, fmt, chain,
2621 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002622 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002623 return 1;
2624 }
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002625
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002626 switch (output_format) {
2627 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002628 dump_human_image_info_list(list);
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002629 break;
2630 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002631 if (chain) {
2632 dump_json_image_info_list(list);
2633 } else {
2634 dump_json_image_info(list->value);
2635 }
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002636 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002637 }
BenoƮt Canetc054b3f2012-09-05 13:09:02 +02002638
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002639 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002640 return 0;
2641}
2642
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002643static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2644 MapEntry *next)
2645{
2646 switch (output_format) {
2647 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002648 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002649 error_report("File contains external, encrypted or compressed clusters.");
2650 exit(1);
2651 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002652 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002653 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002654 e->start, e->length,
2655 e->has_offset ? e->offset : 0,
2656 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002657 }
2658 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2659 * Modify the flags here to allow more coalescing.
2660 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002661 if (next && (!next->data || next->zero)) {
2662 next->data = false;
2663 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002664 }
2665 break;
2666 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002667 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2668 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002669 (e->start == 0 ? "[" : ",\n"),
2670 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002671 e->zero ? "true" : "false",
2672 e->data ? "true" : "false");
2673 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002674 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002675 }
2676 putchar('}');
2677
2678 if (!next) {
2679 printf("]\n");
2680 }
2681 break;
2682 }
2683}
2684
Eric Blake5e344dd2017-10-11 22:47:02 -05002685static int get_block_status(BlockDriverState *bs, int64_t offset,
2686 int64_t bytes, MapEntry *e)
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002687{
Eric Blake237d78f2017-10-11 22:47:03 -05002688 int ret;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002689 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002690 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002691 bool has_offset;
Eric Blake237d78f2017-10-11 22:47:03 -05002692 int64_t map;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002693
2694 /* As an optimization, we could cache the current range of unallocated
2695 * clusters in each file of the chain, and avoid querying the same
2696 * range repeatedly.
2697 */
2698
2699 depth = 0;
2700 for (;;) {
Eric Blake237d78f2017-10-11 22:47:03 -05002701 ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002702 if (ret < 0) {
2703 return ret;
2704 }
Eric Blake237d78f2017-10-11 22:47:03 -05002705 assert(bytes);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002706 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2707 break;
2708 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002709 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002710 if (bs == NULL) {
2711 ret = 0;
2712 break;
2713 }
2714
2715 depth++;
2716 }
2717
John Snow28756452016-02-05 13:12:33 -05002718 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2719
2720 *e = (MapEntry) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002721 .start = offset,
Eric Blake237d78f2017-10-11 22:47:03 -05002722 .length = bytes,
John Snow28756452016-02-05 13:12:33 -05002723 .data = !!(ret & BDRV_BLOCK_DATA),
2724 .zero = !!(ret & BDRV_BLOCK_ZERO),
Eric Blake237d78f2017-10-11 22:47:03 -05002725 .offset = map,
John Snow28756452016-02-05 13:12:33 -05002726 .has_offset = has_offset,
2727 .depth = depth,
2728 .has_filename = file && has_offset,
2729 .filename = file && has_offset ? file->filename : NULL,
2730 };
2731
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002732 return 0;
2733}
2734
Fam Zheng16b0d552016-01-26 11:59:02 +08002735static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2736{
2737 if (curr->length == 0) {
2738 return false;
2739 }
2740 if (curr->zero != next->zero ||
2741 curr->data != next->data ||
2742 curr->depth != next->depth ||
2743 curr->has_filename != next->has_filename ||
2744 curr->has_offset != next->has_offset) {
2745 return false;
2746 }
2747 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2748 return false;
2749 }
2750 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2751 return false;
2752 }
2753 return true;
2754}
2755
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002756static int img_map(int argc, char **argv)
2757{
2758 int c;
2759 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002760 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002761 BlockDriverState *bs;
2762 const char *filename, *fmt, *output;
2763 int64_t length;
2764 MapEntry curr = { .length = 0 }, next;
2765 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002766 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002767 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002768
2769 fmt = NULL;
2770 output = NULL;
2771 for (;;) {
2772 int option_index = 0;
2773 static const struct option long_options[] = {
2774 {"help", no_argument, 0, 'h'},
2775 {"format", required_argument, 0, 'f'},
2776 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002777 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002778 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002779 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002780 {0, 0, 0, 0}
2781 };
Fam Zheng335e9932017-05-03 00:35:39 +08002782 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002783 long_options, &option_index);
2784 if (c == -1) {
2785 break;
2786 }
2787 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002788 case ':':
2789 missing_argument(argv[optind - 1]);
2790 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002791 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002792 unrecognized_option(argv[optind - 1]);
2793 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002794 case 'h':
2795 help();
2796 break;
2797 case 'f':
2798 fmt = optarg;
2799 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002800 case 'U':
2801 force_share = true;
2802 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002803 case OPTION_OUTPUT:
2804 output = optarg;
2805 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002806 case OPTION_OBJECT: {
2807 QemuOpts *opts;
2808 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2809 optarg, true);
2810 if (!opts) {
2811 return 1;
2812 }
2813 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002814 case OPTION_IMAGE_OPTS:
2815 image_opts = true;
2816 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002817 }
2818 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002819 if (optind != argc - 1) {
2820 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002821 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002822 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002823
2824 if (output && !strcmp(output, "json")) {
2825 output_format = OFORMAT_JSON;
2826 } else if (output && !strcmp(output, "human")) {
2827 output_format = OFORMAT_HUMAN;
2828 } else if (output) {
2829 error_report("--output must be used with human or json as argument.");
2830 return 1;
2831 }
2832
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002833 if (qemu_opts_foreach(&qemu_object_opts,
2834 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002835 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002836 return 1;
2837 }
2838
Fam Zheng335e9932017-05-03 00:35:39 +08002839 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002840 if (!blk) {
2841 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002842 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002843 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002844
2845 if (output_format == OFORMAT_HUMAN) {
2846 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2847 }
2848
Max Reitzf1d3cd72015-02-05 13:58:18 -05002849 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002850 while (curr.start + curr.length < length) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002851 int64_t offset = curr.start + curr.length;
2852 int64_t n;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002853
2854 /* Probe up to 1 GiB at a time. */
Eric Blake5e344dd2017-10-11 22:47:02 -05002855 n = QEMU_ALIGN_DOWN(MIN(1 << 30, length - offset), BDRV_SECTOR_SIZE);
2856 ret = get_block_status(bs, offset, n, &next);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002857
2858 if (ret < 0) {
2859 error_report("Could not read file metadata: %s", strerror(-ret));
2860 goto out;
2861 }
2862
Fam Zheng16b0d552016-01-26 11:59:02 +08002863 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002864 curr.length += next.length;
2865 continue;
2866 }
2867
2868 if (curr.length > 0) {
2869 dump_map_entry(output_format, &curr, &next);
2870 }
2871 curr = next;
2872 }
2873
2874 dump_map_entry(output_format, &curr, NULL);
2875
2876out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002877 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002878 return ret < 0;
2879}
2880
aliguorif7b4a942009-01-07 17:40:15 +00002881#define SNAPSHOT_LIST 1
2882#define SNAPSHOT_CREATE 2
2883#define SNAPSHOT_APPLY 3
2884#define SNAPSHOT_DELETE 4
2885
Stuart Brady153859b2009-06-07 00:42:17 +01002886static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002887{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002888 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002889 BlockDriverState *bs;
2890 QEMUSnapshotInfo sn;
2891 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002892 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002893 int action = 0;
2894 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002895 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002896 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002897 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002898 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00002899
Kevin Wolfce099542016-03-15 13:01:04 +01002900 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002901 /* Parse commandline parameters */
2902 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002903 static const struct option long_options[] = {
2904 {"help", no_argument, 0, 'h'},
2905 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002906 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002907 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002908 {0, 0, 0, 0}
2909 };
Fam Zheng335e9932017-05-03 00:35:39 +08002910 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002911 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002912 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002913 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002914 }
aliguorif7b4a942009-01-07 17:40:15 +00002915 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002916 case ':':
2917 missing_argument(argv[optind - 1]);
2918 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002919 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002920 unrecognized_option(argv[optind - 1]);
2921 break;
aliguorif7b4a942009-01-07 17:40:15 +00002922 case 'h':
2923 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002924 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002925 case 'l':
2926 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002927 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002928 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002929 }
2930 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002931 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002932 break;
2933 case 'a':
2934 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002935 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002936 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002937 }
2938 action = SNAPSHOT_APPLY;
2939 snapshot_name = optarg;
2940 break;
2941 case 'c':
2942 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002943 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002944 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002945 }
2946 action = SNAPSHOT_CREATE;
2947 snapshot_name = optarg;
2948 break;
2949 case 'd':
2950 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002951 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002952 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002953 }
2954 action = SNAPSHOT_DELETE;
2955 snapshot_name = optarg;
2956 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002957 case 'q':
2958 quiet = true;
2959 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002960 case 'U':
2961 force_share = true;
2962 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002963 case OPTION_OBJECT: {
2964 QemuOpts *opts;
2965 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2966 optarg, true);
2967 if (!opts) {
2968 return 1;
2969 }
2970 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002971 case OPTION_IMAGE_OPTS:
2972 image_opts = true;
2973 break;
aliguorif7b4a942009-01-07 17:40:15 +00002974 }
2975 }
2976
Kevin Wolffc11eb22013-08-05 10:53:04 +02002977 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002978 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002979 }
aliguorif7b4a942009-01-07 17:40:15 +00002980 filename = argv[optind++];
2981
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002982 if (qemu_opts_foreach(&qemu_object_opts,
2983 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002984 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002985 return 1;
2986 }
2987
aliguorif7b4a942009-01-07 17:40:15 +00002988 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08002989 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
2990 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002991 if (!blk) {
2992 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002993 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002994 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00002995
2996 /* Perform the requested action */
2997 switch(action) {
2998 case SNAPSHOT_LIST:
2999 dump_snapshots(bs);
3000 break;
3001
3002 case SNAPSHOT_CREATE:
3003 memset(&sn, 0, sizeof(sn));
3004 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
3005
3006 qemu_gettimeofday(&tv);
3007 sn.date_sec = tv.tv_sec;
3008 sn.date_nsec = tv.tv_usec * 1000;
3009
3010 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003011 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003012 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003013 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003014 }
aliguorif7b4a942009-01-07 17:40:15 +00003015 break;
3016
3017 case SNAPSHOT_APPLY:
3018 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003019 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003020 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003021 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003022 }
aliguorif7b4a942009-01-07 17:40:15 +00003023 break;
3024
3025 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003026 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003027 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003028 error_reportf_err(err, "Could not delete snapshot '%s': ",
3029 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003030 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003031 }
aliguorif7b4a942009-01-07 17:40:15 +00003032 break;
3033 }
3034
3035 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003036 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003037 if (ret) {
3038 return 1;
3039 }
Stuart Brady153859b2009-06-07 00:42:17 +01003040 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003041}
3042
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003043static int img_rebase(int argc, char **argv)
3044{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003045 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003046 uint8_t *buf_old = NULL;
3047 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05003048 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003049 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003050 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3051 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003052 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003053 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003054 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003055 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003056 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003057 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003058 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003059
3060 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003061 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003062 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003063 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003064 out_baseimg = NULL;
3065 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003066 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003067 static const struct option long_options[] = {
3068 {"help", no_argument, 0, 'h'},
3069 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003070 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003071 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003072 {0, 0, 0, 0}
3073 };
Fam Zheng335e9932017-05-03 00:35:39 +08003074 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003075 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003076 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003077 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003078 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003079 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003080 case ':':
3081 missing_argument(argv[optind - 1]);
3082 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003083 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003084 unrecognized_option(argv[optind - 1]);
3085 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003086 case 'h':
3087 help();
3088 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003089 case 'f':
3090 fmt = optarg;
3091 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003092 case 'F':
3093 out_basefmt = optarg;
3094 break;
3095 case 'b':
3096 out_baseimg = optarg;
3097 break;
3098 case 'u':
3099 unsafe = 1;
3100 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003101 case 'p':
3102 progress = 1;
3103 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003104 case 't':
3105 cache = optarg;
3106 break;
Max Reitz40055952014-07-22 22:58:42 +02003107 case 'T':
3108 src_cache = optarg;
3109 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003110 case 'q':
3111 quiet = true;
3112 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003113 case OPTION_OBJECT: {
3114 QemuOpts *opts;
3115 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3116 optarg, true);
3117 if (!opts) {
3118 return 1;
3119 }
3120 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003121 case OPTION_IMAGE_OPTS:
3122 image_opts = true;
3123 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003124 case 'U':
3125 force_share = true;
3126 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003127 }
3128 }
3129
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003130 if (quiet) {
3131 progress = 0;
3132 }
3133
Fam Zhengac1307a2014-04-22 13:36:11 +08003134 if (optind != argc - 1) {
3135 error_exit("Expecting one image file name");
3136 }
3137 if (!unsafe && !out_baseimg) {
3138 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003139 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003140 filename = argv[optind++];
3141
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003142 if (qemu_opts_foreach(&qemu_object_opts,
3143 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003144 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003145 return 1;
3146 }
3147
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003148 qemu_progress_init(progress, 2.0);
3149 qemu_progress_print(0, 100);
3150
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003151 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003152 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003153 if (ret < 0) {
3154 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003155 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003156 }
3157
Kevin Wolfce099542016-03-15 13:01:04 +01003158 src_flags = 0;
3159 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003160 if (ret < 0) {
3161 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003162 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003163 }
3164
Kevin Wolfce099542016-03-15 13:01:04 +01003165 /* The source files are opened read-only, don't care about WCE */
3166 assert((src_flags & BDRV_O_RDWR) == 0);
3167 (void) src_writethrough;
3168
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003169 /*
3170 * Open the images.
3171 *
3172 * Ignore the old backing file for unsafe rebase in case we want to correct
3173 * the reference to a renamed or moved backing file.
3174 */
Fam Zheng335e9932017-05-03 00:35:39 +08003175 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3176 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003177 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003178 ret = -1;
3179 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003180 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003181 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003182
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003183 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003184 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003185 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003186 ret = -1;
3187 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003188 }
3189 }
3190
3191 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003192 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003193 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003194 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003195
Max Reitz644483d2015-02-05 13:58:17 -05003196 if (bs->backing_format[0] != '\0') {
3197 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003198 qdict_put_str(options, "driver", bs->backing_format);
Max Reitz644483d2015-02-05 13:58:17 -05003199 }
3200
Fam Zheng335e9932017-05-03 00:35:39 +08003201 if (force_share) {
3202 if (!options) {
3203 options = qdict_new();
3204 }
Eric Blake579cf1d2017-05-15 14:54:39 -05003205 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +08003206 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003207 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003208 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003209 options, src_flags, &local_err);
3210 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003211 error_reportf_err(local_err,
3212 "Could not open old backing file '%s': ",
3213 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003214 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003215 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003216 }
Max Reitz644483d2015-02-05 13:58:17 -05003217
Alex Bligha6166732012-10-16 13:46:18 +01003218 if (out_baseimg[0]) {
Fam Zheng335e9932017-05-03 00:35:39 +08003219 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003220 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003221 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003222 }
3223 if (force_share) {
3224 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003225 }
3226
Max Reitzefaa7c42016-03-16 19:54:38 +01003227 blk_new_backing = blk_new_open(out_baseimg, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003228 options, src_flags, &local_err);
3229 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003230 error_reportf_err(local_err,
3231 "Could not open new backing file '%s': ",
3232 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003233 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003234 goto out;
3235 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003236 }
3237 }
3238
3239 /*
3240 * Check each unallocated cluster in the COW file. If it is unallocated,
3241 * accesses go to the backing file. We must therefore compare this cluster
3242 * in the old and new backing file, and if they differ we need to copy it
3243 * from the old backing file into the COW file.
3244 *
3245 * If qemu-img crashes during this step, no harm is done. The content of
3246 * the image is the same as the original one at any time.
3247 */
3248 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003249 int64_t num_sectors;
3250 int64_t old_backing_num_sectors;
3251 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003252 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003253 int n;
Eric Blaked6a644b2017-07-07 07:44:57 -05003254 int64_t count;
Kevin Wolf1f710492012-10-12 14:29:18 +02003255 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003256
Max Reitzf1d3cd72015-02-05 13:58:18 -05003257 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3258 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003259
Max Reitzf1d3cd72015-02-05 13:58:18 -05003260 num_sectors = blk_nb_sectors(blk);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003261 if (num_sectors < 0) {
3262 error_report("Could not get size of '%s': %s",
3263 filename, strerror(-num_sectors));
3264 ret = -1;
3265 goto out;
3266 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003267 old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003268 if (old_backing_num_sectors < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003269 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003270
3271 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3272 error_report("Could not get size of '%s': %s",
3273 backing_name, strerror(-old_backing_num_sectors));
3274 ret = -1;
3275 goto out;
3276 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003277 if (blk_new_backing) {
3278 new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003279 if (new_backing_num_sectors < 0) {
3280 error_report("Could not get size of '%s': %s",
3281 out_baseimg, strerror(-new_backing_num_sectors));
3282 ret = -1;
3283 goto out;
3284 }
Alex Bligha6166732012-10-16 13:46:18 +01003285 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003286
Kevin Wolf1f710492012-10-12 14:29:18 +02003287 if (num_sectors != 0) {
3288 local_progress = (float)100 /
3289 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
3290 }
3291
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003292 for (sector = 0; sector < num_sectors; sector += n) {
3293
3294 /* How many sectors can we handle with the next read? */
3295 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
3296 n = (IO_BUF_SIZE / 512);
3297 } else {
3298 n = num_sectors - sector;
3299 }
3300
3301 /* If the cluster is allocated, we don't need to take action */
Eric Blaked6a644b2017-07-07 07:44:57 -05003302 ret = bdrv_is_allocated(bs, sector << BDRV_SECTOR_BITS,
3303 n << BDRV_SECTOR_BITS, &count);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003304 if (ret < 0) {
3305 error_report("error while reading image metadata: %s",
3306 strerror(-ret));
3307 goto out;
3308 }
Eric Blaked6a644b2017-07-07 07:44:57 -05003309 /* TODO relax this once bdrv_is_allocated does not enforce
3310 * sector alignment */
3311 assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
3312 n = count >> BDRV_SECTOR_BITS;
Kevin Wolfcc60e322010-04-29 14:47:48 +02003313 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003314 continue;
3315 }
3316
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003317 /*
3318 * Read old and new backing file and take into consideration that
3319 * backing files may be smaller than the COW image.
3320 */
3321 if (sector >= old_backing_num_sectors) {
3322 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3323 } else {
3324 if (sector + n > old_backing_num_sectors) {
3325 n = old_backing_num_sectors - sector;
3326 }
3327
Eric Blake91669202016-05-06 10:26:43 -06003328 ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS,
3329 buf_old, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003330 if (ret < 0) {
3331 error_report("error while reading from old backing file");
3332 goto out;
3333 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003334 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003335
Max Reitzf1d3cd72015-02-05 13:58:18 -05003336 if (sector >= new_backing_num_sectors || !blk_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003337 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3338 } else {
3339 if (sector + n > new_backing_num_sectors) {
3340 n = new_backing_num_sectors - sector;
3341 }
3342
Eric Blake91669202016-05-06 10:26:43 -06003343 ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS,
3344 buf_new, n << BDRV_SECTOR_BITS);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003345 if (ret < 0) {
3346 error_report("error while reading from new backing file");
3347 goto out;
3348 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003349 }
3350
3351 /* If they differ, we need to write to the COW file */
3352 uint64_t written = 0;
3353
3354 while (written < n) {
3355 int pnum;
3356
3357 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01003358 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003359 {
Eric Blake91669202016-05-06 10:26:43 -06003360 ret = blk_pwrite(blk,
3361 (sector + written) << BDRV_SECTOR_BITS,
3362 buf_old + written * 512,
3363 pnum << BDRV_SECTOR_BITS, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003364 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003365 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003366 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003367 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003368 }
3369 }
3370
3371 written += pnum;
3372 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003373 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003374 }
3375 }
3376
3377 /*
3378 * Change the backing file. All clusters that are different from the old
3379 * backing file are overwritten in the COW file now, so the visible content
3380 * doesn't change when we switch the backing file.
3381 */
Alex Bligha6166732012-10-16 13:46:18 +01003382 if (out_baseimg && *out_baseimg) {
3383 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3384 } else {
3385 ret = bdrv_change_backing_file(bs, NULL, NULL);
3386 }
3387
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003388 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003389 error_report("Could not change the backing file to '%s': No "
3390 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003391 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003392 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003393 out_baseimg, strerror(-ret));
3394 }
3395
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003396 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003397 /*
3398 * TODO At this point it is possible to check if any clusters that are
3399 * allocated in the COW file are the same in the backing file. If so, they
3400 * could be dropped from the COW file. Don't do this before switching the
3401 * backing file, in case of a crash this would lead to corruption.
3402 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003403out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003404 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003405 /* Cleanup */
3406 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003407 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003408 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003409 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003410 qemu_vfree(buf_old);
3411 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003412
Markus Armbruster26f54e92014-10-07 13:59:04 +02003413 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003414 if (ret) {
3415 return 1;
3416 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003417 return 0;
3418}
3419
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003420static int img_resize(int argc, char **argv)
3421{
Markus Armbruster6750e792015-02-12 17:43:08 +01003422 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003423 int c, ret, relative;
3424 const char *filename, *fmt, *size;
Max Reitzdc5f6902017-06-13 22:20:55 +02003425 int64_t n, total_size, current_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003426 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003427 BlockBackend *blk = NULL;
Max Reitzdc5f6902017-06-13 22:20:55 +02003428 PreallocMode prealloc = PREALLOC_MODE_OFF;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003429 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003430
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003431 static QemuOptsList resize_options = {
3432 .name = "resize_options",
3433 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3434 .desc = {
3435 {
3436 .name = BLOCK_OPT_SIZE,
3437 .type = QEMU_OPT_SIZE,
3438 .help = "Virtual disk size"
3439 }, {
3440 /* end of list */
3441 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003442 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003443 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003444 bool image_opts = false;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003445 bool shrink = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003446
Kevin Wolfe80fec72011-04-29 10:58:12 +02003447 /* Remove size from argv manually so that negative numbers are not treated
3448 * as options by getopt. */
3449 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003450 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003451 return 1;
3452 }
3453
3454 size = argv[--argc];
3455
3456 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003457 fmt = NULL;
3458 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003459 static const struct option long_options[] = {
3460 {"help", no_argument, 0, 'h'},
3461 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003462 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Max Reitzdc5f6902017-06-13 22:20:55 +02003463 {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003464 {"shrink", no_argument, 0, OPTION_SHRINK},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003465 {0, 0, 0, 0}
3466 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003467 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003468 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003469 if (c == -1) {
3470 break;
3471 }
3472 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003473 case ':':
3474 missing_argument(argv[optind - 1]);
3475 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003476 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003477 unrecognized_option(argv[optind - 1]);
3478 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003479 case 'h':
3480 help();
3481 break;
3482 case 'f':
3483 fmt = optarg;
3484 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003485 case 'q':
3486 quiet = true;
3487 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003488 case OPTION_OBJECT: {
3489 QemuOpts *opts;
3490 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3491 optarg, true);
3492 if (!opts) {
3493 return 1;
3494 }
3495 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003496 case OPTION_IMAGE_OPTS:
3497 image_opts = true;
3498 break;
Max Reitzdc5f6902017-06-13 22:20:55 +02003499 case OPTION_PREALLOCATION:
Marc-AndrƩ Lureauf7abe0e2017-08-24 10:46:10 +02003500 prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
Markus Armbruster06c60b62017-08-24 10:45:57 +02003501 PREALLOC_MODE__MAX, NULL);
Max Reitzdc5f6902017-06-13 22:20:55 +02003502 if (prealloc == PREALLOC_MODE__MAX) {
3503 error_report("Invalid preallocation mode '%s'", optarg);
3504 return 1;
3505 }
3506 break;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003507 case OPTION_SHRINK:
3508 shrink = true;
3509 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003510 }
3511 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003512 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003513 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003514 }
3515 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003516
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003517 if (qemu_opts_foreach(&qemu_object_opts,
3518 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003519 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003520 return 1;
3521 }
3522
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003523 /* Choose grow, shrink, or absolute resize mode */
3524 switch (size[0]) {
3525 case '+':
3526 relative = 1;
3527 size++;
3528 break;
3529 case '-':
3530 relative = -1;
3531 size++;
3532 break;
3533 default:
3534 relative = 0;
3535 break;
3536 }
3537
3538 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003539 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003540 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003541 if (err) {
3542 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003543 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003544 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003545 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003546 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003547 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3548 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003549
Max Reitzefaa7c42016-03-16 19:54:38 +01003550 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003551 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3552 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003553 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003554 ret = -1;
3555 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003556 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003557
Max Reitzdc5f6902017-06-13 22:20:55 +02003558 current_size = blk_getlength(blk);
3559 if (current_size < 0) {
3560 error_report("Failed to inquire current image length: %s",
3561 strerror(-current_size));
3562 ret = -1;
3563 goto out;
3564 }
3565
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003566 if (relative) {
Max Reitzdc5f6902017-06-13 22:20:55 +02003567 total_size = current_size + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003568 } else {
3569 total_size = n;
3570 }
3571 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003572 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003573 ret = -1;
3574 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003575 }
3576
Max Reitzdc5f6902017-06-13 22:20:55 +02003577 if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
3578 error_report("Preallocation can only be used for growing images");
3579 ret = -1;
3580 goto out;
3581 }
3582
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003583 if (total_size < current_size && !shrink) {
3584 warn_report("Shrinking an image will delete all data beyond the "
3585 "shrunken image's end. Before performing such an "
3586 "operation, make sure there is no important data there.");
3587
3588 if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) {
3589 error_report(
3590 "Use the --shrink option to perform a shrink operation.");
3591 ret = -1;
3592 goto out;
3593 } else {
3594 warn_report("Using the --shrink option will suppress this message. "
3595 "Note that future versions of qemu-img may refuse to "
3596 "shrink images without this option.");
3597 }
3598 }
3599
Max Reitzdc5f6902017-06-13 22:20:55 +02003600 ret = blk_truncate(blk, total_size, prealloc, &err);
Max Reitzed3d2ec2017-03-28 22:51:27 +02003601 if (!ret) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003602 qprintf(quiet, "Image resized.\n");
Max Reitzed3d2ec2017-03-28 22:51:27 +02003603 } else {
3604 error_report_err(err);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003605 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003606out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003607 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003608 if (ret) {
3609 return 1;
3610 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003611 return 0;
3612}
3613
Max Reitz76a3a342014-10-27 11:12:51 +01003614static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003615 int64_t offset, int64_t total_work_size,
3616 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003617{
3618 qemu_progress_print(100.f * offset / total_work_size, 0);
3619}
3620
Max Reitz6f176b42013-09-03 10:09:50 +02003621static int img_amend(int argc, char **argv)
3622{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003623 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003624 int c, ret = 0;
3625 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003626 QemuOptsList *create_opts = NULL;
3627 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003628 const char *fmt = NULL, *filename, *cache;
3629 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003630 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003631 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003632 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003633 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003634 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003635
Max Reitzbd39e6e2014-07-22 22:58:43 +02003636 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003637 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003638 static const struct option long_options[] = {
3639 {"help", no_argument, 0, 'h'},
3640 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003641 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003642 {0, 0, 0, 0}
3643 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003644 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003645 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003646 if (c == -1) {
3647 break;
3648 }
3649
3650 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003651 case ':':
3652 missing_argument(argv[optind - 1]);
3653 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003654 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003655 unrecognized_option(argv[optind - 1]);
3656 break;
3657 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003658 help();
3659 break;
3660 case 'o':
3661 if (!is_valid_option_list(optarg)) {
3662 error_report("Invalid option list: %s", optarg);
3663 ret = -1;
3664 goto out_no_progress;
3665 }
3666 if (!options) {
3667 options = g_strdup(optarg);
3668 } else {
3669 char *old_options = options;
3670 options = g_strdup_printf("%s,%s", options, optarg);
3671 g_free(old_options);
3672 }
3673 break;
3674 case 'f':
3675 fmt = optarg;
3676 break;
3677 case 't':
3678 cache = optarg;
3679 break;
3680 case 'p':
3681 progress = true;
3682 break;
3683 case 'q':
3684 quiet = true;
3685 break;
3686 case OPTION_OBJECT:
3687 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3688 optarg, true);
3689 if (!opts) {
3690 ret = -1;
3691 goto out_no_progress;
3692 }
3693 break;
3694 case OPTION_IMAGE_OPTS:
3695 image_opts = true;
3696 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003697 }
3698 }
3699
Max Reitz6f176b42013-09-03 10:09:50 +02003700 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003701 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003702 }
3703
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003704 if (qemu_opts_foreach(&qemu_object_opts,
3705 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003706 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003707 ret = -1;
3708 goto out_no_progress;
3709 }
3710
Max Reitz76a3a342014-10-27 11:12:51 +01003711 if (quiet) {
3712 progress = false;
3713 }
3714 qemu_progress_init(progress, 1.0);
3715
Kevin Wolfa283cb62014-02-21 16:24:07 +01003716 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3717 if (fmt && has_help_option(options)) {
3718 /* If a format is explicitly specified (and possibly no filename is
3719 * given), print option help here */
3720 ret = print_block_option_help(filename, fmt);
3721 goto out;
3722 }
3723
3724 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003725 error_report("Expecting one image file name");
3726 ret = -1;
3727 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003728 }
Max Reitz6f176b42013-09-03 10:09:50 +02003729
Kevin Wolfce099542016-03-15 13:01:04 +01003730 flags = BDRV_O_RDWR;
3731 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003732 if (ret < 0) {
3733 error_report("Invalid cache option: %s", cache);
3734 goto out;
3735 }
3736
Fam Zheng335e9932017-05-03 00:35:39 +08003737 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3738 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003739 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003740 ret = -1;
3741 goto out;
3742 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003743 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003744
3745 fmt = bs->drv->format_name;
3746
Kevin Wolf626f84f2014-02-21 16:24:06 +01003747 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003748 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02003749 ret = print_block_option_help(filename, fmt);
3750 goto out;
3751 }
3752
Max Reitzb2439d22014-12-02 18:32:47 +01003753 if (!bs->drv->create_opts) {
3754 error_report("Format driver '%s' does not support any options to amend",
3755 fmt);
3756 ret = -1;
3757 goto out;
3758 }
3759
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003760 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003761 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003762 qemu_opts_do_parse(opts, options, NULL, &err);
3763 if (err) {
3764 error_report_err(err);
3765 ret = -1;
3766 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003767 }
3768
Max Reitz76a3a342014-10-27 11:12:51 +01003769 /* In case the driver does not call amend_status_cb() */
3770 qemu_progress_print(0.f, 0);
Max Reitz8b139762015-07-27 17:51:32 +02003771 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
Max Reitz76a3a342014-10-27 11:12:51 +01003772 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003773 if (ret < 0) {
3774 error_report("Error while amending options: %s", strerror(-ret));
3775 goto out;
3776 }
3777
3778out:
Max Reitz76a3a342014-10-27 11:12:51 +01003779 qemu_progress_end();
3780
Max Reitze814dff2015-08-20 16:00:38 -07003781out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003782 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003783 qemu_opts_del(opts);
3784 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003785 g_free(options);
3786
Max Reitz6f176b42013-09-03 10:09:50 +02003787 if (ret) {
3788 return 1;
3789 }
3790 return 0;
3791}
3792
Kevin Wolfb6133b82014-08-05 14:17:13 +02003793typedef struct BenchData {
3794 BlockBackend *blk;
3795 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003796 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003797 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003798 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003799 int nrreq;
3800 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003801 int flush_interval;
3802 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003803 uint8_t *buf;
3804 QEMUIOVector *qiov;
3805
3806 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003807 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003808 uint64_t offset;
3809} BenchData;
3810
Kevin Wolf55d539c2016-06-03 13:59:41 +02003811static void bench_undrained_flush_cb(void *opaque, int ret)
3812{
3813 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003814 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003815 exit(EXIT_FAILURE);
3816 }
3817}
3818
Kevin Wolfb6133b82014-08-05 14:17:13 +02003819static void bench_cb(void *opaque, int ret)
3820{
3821 BenchData *b = opaque;
3822 BlockAIOCB *acb;
3823
3824 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003825 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003826 exit(EXIT_FAILURE);
3827 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003828
3829 if (b->in_flush) {
3830 /* Just finished a flush with drained queue: Start next requests */
3831 assert(b->in_flight == 0);
3832 b->in_flush = false;
3833 } else if (b->in_flight > 0) {
3834 int remaining = b->n - b->in_flight;
3835
Kevin Wolfb6133b82014-08-05 14:17:13 +02003836 b->n--;
3837 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003838
3839 /* Time for flush? Drain queue if requested, then flush */
3840 if (b->flush_interval && remaining % b->flush_interval == 0) {
3841 if (!b->in_flight || !b->drain_on_flush) {
3842 BlockCompletionFunc *cb;
3843
3844 if (b->drain_on_flush) {
3845 b->in_flush = true;
3846 cb = bench_cb;
3847 } else {
3848 cb = bench_undrained_flush_cb;
3849 }
3850
3851 acb = blk_aio_flush(b->blk, cb, b);
3852 if (!acb) {
3853 error_report("Failed to issue flush request");
3854 exit(EXIT_FAILURE);
3855 }
3856 }
3857 if (b->drain_on_flush) {
3858 return;
3859 }
3860 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003861 }
3862
3863 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003864 int64_t offset = b->offset;
3865 /* blk_aio_* might look for completed I/Os and kick bench_cb
3866 * again, so make sure this operation is counted by in_flight
3867 * and b->offset is ready for the next submission.
3868 */
3869 b->in_flight++;
3870 b->offset += b->step;
3871 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003872 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003873 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003874 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003875 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003876 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003877 if (!acb) {
3878 error_report("Failed to issue request");
3879 exit(EXIT_FAILURE);
3880 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003881 }
3882}
3883
3884static int img_bench(int argc, char **argv)
3885{
3886 int c, ret = 0;
3887 const char *fmt = NULL, *filename;
3888 bool quiet = false;
3889 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003890 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003891 int count = 75000;
3892 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003893 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003894 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003895 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003896 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003897 int flush_interval = 0;
3898 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003899 int64_t image_size;
3900 BlockBackend *blk = NULL;
3901 BenchData data = {};
3902 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02003903 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003904 struct timeval t1, t2;
3905 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08003906 bool force_share = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003907
3908 for (;;) {
3909 static const struct option long_options[] = {
3910 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003911 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003912 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003913 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02003914 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08003915 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02003916 {0, 0, 0, 0}
3917 };
Fam Zheng335e9932017-05-03 00:35:39 +08003918 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02003919 if (c == -1) {
3920 break;
3921 }
3922
3923 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003924 case ':':
3925 missing_argument(argv[optind - 1]);
3926 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003927 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003928 unrecognized_option(argv[optind - 1]);
3929 break;
3930 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02003931 help();
3932 break;
3933 case 'c':
3934 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003935 unsigned long res;
3936
3937 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003938 error_report("Invalid request count specified");
3939 return 1;
3940 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003941 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003942 break;
3943 }
3944 case 'd':
3945 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00003946 unsigned long res;
3947
3948 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003949 error_report("Invalid queue depth specified");
3950 return 1;
3951 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00003952 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003953 break;
3954 }
3955 case 'f':
3956 fmt = optarg;
3957 break;
3958 case 'n':
3959 flags |= BDRV_O_NATIVE_AIO;
3960 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02003961 case 'o':
3962 {
Markus Armbruster606caa02017-02-21 21:14:04 +01003963 offset = cvtnum(optarg);
3964 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02003965 error_report("Invalid offset specified");
3966 return 1;
3967 }
3968 break;
3969 }
3970 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003971 case 'q':
3972 quiet = true;
3973 break;
3974 case 's':
3975 {
3976 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003977
Markus Armbruster606caa02017-02-21 21:14:04 +01003978 sval = cvtnum(optarg);
3979 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02003980 error_report("Invalid buffer size specified");
3981 return 1;
3982 }
3983
3984 bufsize = sval;
3985 break;
3986 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02003987 case 'S':
3988 {
3989 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003990
Markus Armbruster606caa02017-02-21 21:14:04 +01003991 sval = cvtnum(optarg);
3992 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02003993 error_report("Invalid step size specified");
3994 return 1;
3995 }
3996
3997 step = sval;
3998 break;
3999 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004000 case 't':
4001 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
4002 if (ret < 0) {
4003 error_report("Invalid cache mode");
4004 ret = -1;
4005 goto out;
4006 }
4007 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004008 case 'w':
4009 flags |= BDRV_O_RDWR;
4010 is_write = true;
4011 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004012 case 'U':
4013 force_share = true;
4014 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004015 case OPTION_PATTERN:
4016 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004017 unsigned long res;
4018
4019 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004020 error_report("Invalid pattern byte specified");
4021 return 1;
4022 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004023 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004024 break;
4025 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02004026 case OPTION_FLUSH_INTERVAL:
4027 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004028 unsigned long res;
4029
4030 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004031 error_report("Invalid flush interval specified");
4032 return 1;
4033 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004034 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004035 break;
4036 }
4037 case OPTION_NO_DRAIN:
4038 drain_on_flush = false;
4039 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004040 case OPTION_IMAGE_OPTS:
4041 image_opts = true;
4042 break;
4043 }
4044 }
4045
4046 if (optind != argc - 1) {
4047 error_exit("Expecting one image file name");
4048 }
4049 filename = argv[argc - 1];
4050
Kevin Wolf55d539c2016-06-03 13:59:41 +02004051 if (!is_write && flush_interval) {
4052 error_report("--flush-interval is only available in write tests");
4053 ret = -1;
4054 goto out;
4055 }
4056 if (flush_interval && flush_interval < depth) {
4057 error_report("Flush interval can't be smaller than depth");
4058 ret = -1;
4059 goto out;
4060 }
4061
Fam Zheng335e9932017-05-03 00:35:39 +08004062 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4063 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004064 if (!blk) {
4065 ret = -1;
4066 goto out;
4067 }
4068
4069 image_size = blk_getlength(blk);
4070 if (image_size < 0) {
4071 ret = image_size;
4072 goto out;
4073 }
4074
4075 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004076 .blk = blk,
4077 .image_size = image_size,
4078 .bufsize = bufsize,
4079 .step = step ?: bufsize,
4080 .nrreq = depth,
4081 .n = count,
4082 .offset = offset,
4083 .write = is_write,
4084 .flush_interval = flush_interval,
4085 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004086 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004087 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004088 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004089 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004090 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004091 if (flush_interval) {
4092 printf("Sending flush every %d requests\n", flush_interval);
4093 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004094
4095 data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004096 memset(data.buf, pattern, data.nrreq * data.bufsize);
4097
Kevin Wolfb6133b82014-08-05 14:17:13 +02004098 data.qiov = g_new(QEMUIOVector, data.nrreq);
4099 for (i = 0; i < data.nrreq; i++) {
4100 qemu_iovec_init(&data.qiov[i], 1);
4101 qemu_iovec_add(&data.qiov[i],
4102 data.buf + i * data.bufsize, data.bufsize);
4103 }
4104
4105 gettimeofday(&t1, NULL);
4106 bench_cb(&data, 0);
4107
4108 while (data.n > 0) {
4109 main_loop_wait(false);
4110 }
4111 gettimeofday(&t2, NULL);
4112
4113 printf("Run completed in %3.3f seconds.\n",
4114 (t2.tv_sec - t1.tv_sec)
4115 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4116
4117out:
4118 qemu_vfree(data.buf);
4119 blk_unref(blk);
4120
4121 if (ret) {
4122 return 1;
4123 }
4124 return 0;
4125}
4126
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004127#define C_BS 01
4128#define C_COUNT 02
4129#define C_IF 04
4130#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004131#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004132
4133struct DdInfo {
4134 unsigned int flags;
4135 int64_t count;
4136};
4137
4138struct DdIo {
4139 int bsz; /* Block size */
4140 char *filename;
4141 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004142 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004143};
4144
4145struct DdOpts {
4146 const char *name;
4147 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4148 unsigned int flag;
4149};
4150
4151static int img_dd_bs(const char *arg,
4152 struct DdIo *in, struct DdIo *out,
4153 struct DdInfo *dd)
4154{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004155 int64_t res;
4156
Markus Armbruster606caa02017-02-21 21:14:04 +01004157 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004158
Markus Armbruster606caa02017-02-21 21:14:04 +01004159 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004160 error_report("invalid number: '%s'", arg);
4161 return 1;
4162 }
4163 in->bsz = out->bsz = res;
4164
4165 return 0;
4166}
4167
4168static int img_dd_count(const char *arg,
4169 struct DdIo *in, struct DdIo *out,
4170 struct DdInfo *dd)
4171{
Markus Armbruster606caa02017-02-21 21:14:04 +01004172 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004173
Markus Armbruster606caa02017-02-21 21:14:04 +01004174 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004175 error_report("invalid number: '%s'", arg);
4176 return 1;
4177 }
4178
4179 return 0;
4180}
4181
4182static int img_dd_if(const char *arg,
4183 struct DdIo *in, struct DdIo *out,
4184 struct DdInfo *dd)
4185{
4186 in->filename = g_strdup(arg);
4187
4188 return 0;
4189}
4190
4191static int img_dd_of(const char *arg,
4192 struct DdIo *in, struct DdIo *out,
4193 struct DdInfo *dd)
4194{
4195 out->filename = g_strdup(arg);
4196
4197 return 0;
4198}
4199
Reda Sallahif7c15532016-08-10 16:16:09 +02004200static int img_dd_skip(const char *arg,
4201 struct DdIo *in, struct DdIo *out,
4202 struct DdInfo *dd)
4203{
Markus Armbruster606caa02017-02-21 21:14:04 +01004204 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004205
Markus Armbruster606caa02017-02-21 21:14:04 +01004206 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004207 error_report("invalid number: '%s'", arg);
4208 return 1;
4209 }
4210
4211 return 0;
4212}
4213
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004214static int img_dd(int argc, char **argv)
4215{
4216 int ret = 0;
4217 char *arg = NULL;
4218 char *tmp;
4219 BlockDriver *drv = NULL, *proto_drv = NULL;
4220 BlockBackend *blk1 = NULL, *blk2 = NULL;
4221 QemuOpts *opts = NULL;
4222 QemuOptsList *create_opts = NULL;
4223 Error *local_err = NULL;
4224 bool image_opts = false;
4225 int c, i;
4226 const char *out_fmt = "raw";
4227 const char *fmt = NULL;
4228 int64_t size = 0;
4229 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004230 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004231 struct DdInfo dd = {
4232 .flags = 0,
4233 .count = 0,
4234 };
4235 struct DdIo in = {
4236 .bsz = 512, /* Block size is by default 512 bytes */
4237 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004238 .buf = NULL,
4239 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004240 };
4241 struct DdIo out = {
4242 .bsz = 512,
4243 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004244 .buf = NULL,
4245 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004246 };
4247
4248 const struct DdOpts options[] = {
4249 { "bs", img_dd_bs, C_BS },
4250 { "count", img_dd_count, C_COUNT },
4251 { "if", img_dd_if, C_IF },
4252 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004253 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004254 { NULL, NULL, 0 }
4255 };
4256 const struct option long_options[] = {
4257 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004258 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004259 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004260 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004261 { 0, 0, 0, 0 }
4262 };
4263
Fam Zheng335e9932017-05-03 00:35:39 +08004264 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004265 if (c == EOF) {
4266 break;
4267 }
4268 switch (c) {
4269 case 'O':
4270 out_fmt = optarg;
4271 break;
4272 case 'f':
4273 fmt = optarg;
4274 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004275 case ':':
4276 missing_argument(argv[optind - 1]);
4277 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004278 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004279 unrecognized_option(argv[optind - 1]);
4280 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004281 case 'h':
4282 help();
4283 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004284 case 'U':
4285 force_share = true;
4286 break;
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004287 case OPTION_OBJECT:
4288 if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004289 ret = -1;
4290 goto out;
4291 }
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004292 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004293 case OPTION_IMAGE_OPTS:
4294 image_opts = true;
4295 break;
4296 }
4297 }
4298
4299 for (i = optind; i < argc; i++) {
4300 int j;
4301 arg = g_strdup(argv[i]);
4302
4303 tmp = strchr(arg, '=');
4304 if (tmp == NULL) {
4305 error_report("unrecognized operand %s", arg);
4306 ret = -1;
4307 goto out;
4308 }
4309
4310 *tmp++ = '\0';
4311
4312 for (j = 0; options[j].name != NULL; j++) {
4313 if (!strcmp(arg, options[j].name)) {
4314 break;
4315 }
4316 }
4317 if (options[j].name == NULL) {
4318 error_report("unrecognized operand %s", arg);
4319 ret = -1;
4320 goto out;
4321 }
4322
4323 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4324 ret = -1;
4325 goto out;
4326 }
4327 dd.flags |= options[j].flag;
4328 g_free(arg);
4329 arg = NULL;
4330 }
4331
4332 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4333 error_report("Must specify both input and output files");
4334 ret = -1;
4335 goto out;
4336 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004337
4338 if (qemu_opts_foreach(&qemu_object_opts,
4339 user_creatable_add_opts_foreach,
4340 NULL, NULL)) {
4341 ret = -1;
4342 goto out;
4343 }
4344
Fam Zheng335e9932017-05-03 00:35:39 +08004345 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4346 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004347
4348 if (!blk1) {
4349 ret = -1;
4350 goto out;
4351 }
4352
4353 drv = bdrv_find_format(out_fmt);
4354 if (!drv) {
4355 error_report("Unknown file format");
4356 ret = -1;
4357 goto out;
4358 }
4359 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4360
4361 if (!proto_drv) {
4362 error_report_err(local_err);
4363 ret = -1;
4364 goto out;
4365 }
4366 if (!drv->create_opts) {
4367 error_report("Format driver '%s' does not support image creation",
4368 drv->format_name);
4369 ret = -1;
4370 goto out;
4371 }
4372 if (!proto_drv->create_opts) {
4373 error_report("Protocol driver '%s' does not support image creation",
4374 proto_drv->format_name);
4375 ret = -1;
4376 goto out;
4377 }
4378 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4379 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4380
4381 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4382
4383 size = blk_getlength(blk1);
4384 if (size < 0) {
4385 error_report("Failed to get size for '%s'", in.filename);
4386 ret = -1;
4387 goto out;
4388 }
4389
4390 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4391 dd.count * in.bsz < size) {
4392 size = dd.count * in.bsz;
4393 }
4394
Reda Sallahif7c15532016-08-10 16:16:09 +02004395 /* Overflow means the specified offset is beyond input image's size */
4396 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4397 size < in.bsz * in.offset)) {
4398 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4399 } else {
4400 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4401 size - in.bsz * in.offset, &error_abort);
4402 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004403
4404 ret = bdrv_create(drv, out.filename, opts, &local_err);
4405 if (ret < 0) {
4406 error_reportf_err(local_err,
4407 "%s: error while creating output image: ",
4408 out.filename);
4409 ret = -1;
4410 goto out;
4411 }
4412
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004413 /* TODO, we can't honour --image-opts for the target,
4414 * since it needs to be given in a format compatible
4415 * with the bdrv_create() call above which does not
4416 * support image-opts style.
4417 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01004418 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004419 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004420
4421 if (!blk2) {
4422 ret = -1;
4423 goto out;
4424 }
4425
Reda Sallahif7c15532016-08-10 16:16:09 +02004426 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4427 size < in.offset * in.bsz)) {
4428 /* We give a warning if the skip option is bigger than the input
4429 * size and create an empty output disk image (i.e. like dd(1)).
4430 */
4431 error_report("%s: cannot skip to specified offset", in.filename);
4432 in_pos = size;
4433 } else {
4434 in_pos = in.offset * in.bsz;
4435 }
4436
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004437 in.buf = g_new(uint8_t, in.bsz);
4438
Reda Sallahif7c15532016-08-10 16:16:09 +02004439 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004440 int in_ret, out_ret;
4441
4442 if (in_pos + in.bsz > size) {
4443 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4444 } else {
4445 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4446 }
4447 if (in_ret < 0) {
4448 error_report("error while reading from input image file: %s",
4449 strerror(-in_ret));
4450 ret = -1;
4451 goto out;
4452 }
4453 in_pos += in_ret;
4454
4455 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4456
4457 if (out_ret < 0) {
4458 error_report("error while writing to output image file: %s",
4459 strerror(-out_ret));
4460 ret = -1;
4461 goto out;
4462 }
4463 out_pos += out_ret;
4464 }
4465
4466out:
4467 g_free(arg);
4468 qemu_opts_del(opts);
4469 qemu_opts_free(create_opts);
4470 blk_unref(blk1);
4471 blk_unref(blk2);
4472 g_free(in.filename);
4473 g_free(out.filename);
4474 g_free(in.buf);
4475 g_free(out.buf);
4476
4477 if (ret) {
4478 return 1;
4479 }
4480 return 0;
4481}
4482
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004483static void dump_json_block_measure_info(BlockMeasureInfo *info)
4484{
4485 QString *str;
4486 QObject *obj;
4487 Visitor *v = qobject_output_visitor_new(&obj);
4488
4489 visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
4490 visit_complete(v, &obj);
4491 str = qobject_to_json_pretty(obj);
4492 assert(str != NULL);
4493 printf("%s\n", qstring_get_str(str));
4494 qobject_decref(obj);
4495 visit_free(v);
4496 QDECREF(str);
4497}
4498
4499static int img_measure(int argc, char **argv)
4500{
4501 static const struct option long_options[] = {
4502 {"help", no_argument, 0, 'h'},
4503 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4504 {"object", required_argument, 0, OPTION_OBJECT},
4505 {"output", required_argument, 0, OPTION_OUTPUT},
4506 {"size", required_argument, 0, OPTION_SIZE},
4507 {"force-share", no_argument, 0, 'U'},
4508 {0, 0, 0, 0}
4509 };
4510 OutputFormat output_format = OFORMAT_HUMAN;
4511 BlockBackend *in_blk = NULL;
4512 BlockDriver *drv;
4513 const char *filename = NULL;
4514 const char *fmt = NULL;
4515 const char *out_fmt = "raw";
4516 char *options = NULL;
4517 char *snapshot_name = NULL;
4518 bool force_share = false;
4519 QemuOpts *opts = NULL;
4520 QemuOpts *object_opts = NULL;
4521 QemuOpts *sn_opts = NULL;
4522 QemuOptsList *create_opts = NULL;
4523 bool image_opts = false;
4524 uint64_t img_size = UINT64_MAX;
4525 BlockMeasureInfo *info = NULL;
4526 Error *local_err = NULL;
4527 int ret = 1;
4528 int c;
4529
4530 while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
4531 long_options, NULL)) != -1) {
4532 switch (c) {
4533 case '?':
4534 case 'h':
4535 help();
4536 break;
4537 case 'f':
4538 fmt = optarg;
4539 break;
4540 case 'O':
4541 out_fmt = optarg;
4542 break;
4543 case 'o':
4544 if (!is_valid_option_list(optarg)) {
4545 error_report("Invalid option list: %s", optarg);
4546 goto out;
4547 }
4548 if (!options) {
4549 options = g_strdup(optarg);
4550 } else {
4551 char *old_options = options;
4552 options = g_strdup_printf("%s,%s", options, optarg);
4553 g_free(old_options);
4554 }
4555 break;
4556 case 'l':
4557 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
4558 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
4559 optarg, false);
4560 if (!sn_opts) {
4561 error_report("Failed in parsing snapshot param '%s'",
4562 optarg);
4563 goto out;
4564 }
4565 } else {
4566 snapshot_name = optarg;
4567 }
4568 break;
4569 case 'U':
4570 force_share = true;
4571 break;
4572 case OPTION_OBJECT:
4573 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
4574 optarg, true);
4575 if (!object_opts) {
4576 goto out;
4577 }
4578 break;
4579 case OPTION_IMAGE_OPTS:
4580 image_opts = true;
4581 break;
4582 case OPTION_OUTPUT:
4583 if (!strcmp(optarg, "json")) {
4584 output_format = OFORMAT_JSON;
4585 } else if (!strcmp(optarg, "human")) {
4586 output_format = OFORMAT_HUMAN;
4587 } else {
4588 error_report("--output must be used with human or json "
4589 "as argument.");
4590 goto out;
4591 }
4592 break;
4593 case OPTION_SIZE:
4594 {
4595 int64_t sval;
4596
4597 sval = cvtnum(optarg);
4598 if (sval < 0) {
4599 if (sval == -ERANGE) {
4600 error_report("Image size must be less than 8 EiB!");
4601 } else {
4602 error_report("Invalid image size specified! You may use "
4603 "k, M, G, T, P or E suffixes for ");
4604 error_report("kilobytes, megabytes, gigabytes, terabytes, "
4605 "petabytes and exabytes.");
4606 }
4607 goto out;
4608 }
4609 img_size = (uint64_t)sval;
4610 }
4611 break;
4612 }
4613 }
4614
4615 if (qemu_opts_foreach(&qemu_object_opts,
4616 user_creatable_add_opts_foreach,
4617 NULL, NULL)) {
4618 goto out;
4619 }
4620
4621 if (argc - optind > 1) {
4622 error_report("At most one filename argument is allowed.");
4623 goto out;
4624 } else if (argc - optind == 1) {
4625 filename = argv[optind];
4626 }
4627
4628 if (!filename &&
4629 (object_opts || image_opts || fmt || snapshot_name || sn_opts)) {
4630 error_report("--object, --image-opts, -f, and -l "
4631 "require a filename argument.");
4632 goto out;
4633 }
4634 if (filename && img_size != UINT64_MAX) {
4635 error_report("--size N cannot be used together with a filename.");
4636 goto out;
4637 }
4638 if (!filename && img_size == UINT64_MAX) {
4639 error_report("Either --size N or one filename must be specified.");
4640 goto out;
4641 }
4642
4643 if (filename) {
4644 in_blk = img_open(image_opts, filename, fmt, 0,
4645 false, false, force_share);
4646 if (!in_blk) {
4647 goto out;
4648 }
4649
4650 if (sn_opts) {
4651 bdrv_snapshot_load_tmp(blk_bs(in_blk),
4652 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
4653 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
4654 &local_err);
4655 } else if (snapshot_name != NULL) {
4656 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
4657 snapshot_name, &local_err);
4658 }
4659 if (local_err) {
4660 error_reportf_err(local_err, "Failed to load snapshot: ");
4661 goto out;
4662 }
4663 }
4664
4665 drv = bdrv_find_format(out_fmt);
4666 if (!drv) {
4667 error_report("Unknown file format '%s'", out_fmt);
4668 goto out;
4669 }
4670 if (!drv->create_opts) {
4671 error_report("Format driver '%s' does not support image creation",
4672 drv->format_name);
4673 goto out;
4674 }
4675
4676 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4677 create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
4678 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4679 if (options) {
4680 qemu_opts_do_parse(opts, options, NULL, &local_err);
4681 if (local_err) {
4682 error_report_err(local_err);
4683 error_report("Invalid options for file format '%s'", out_fmt);
4684 goto out;
4685 }
4686 }
4687 if (img_size != UINT64_MAX) {
4688 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
4689 }
4690
4691 info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
4692 if (local_err) {
4693 error_report_err(local_err);
4694 goto out;
4695 }
4696
4697 if (output_format == OFORMAT_HUMAN) {
4698 printf("required size: %" PRIu64 "\n", info->required);
4699 printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
4700 } else {
4701 dump_json_block_measure_info(info);
4702 }
4703
4704 ret = 0;
4705
4706out:
4707 qapi_free_BlockMeasureInfo(info);
4708 qemu_opts_del(object_opts);
4709 qemu_opts_del(opts);
4710 qemu_opts_del(sn_opts);
4711 qemu_opts_free(create_opts);
4712 g_free(options);
4713 blk_unref(in_blk);
4714 return ret;
4715}
Kevin Wolfb6133b82014-08-05 14:17:13 +02004716
Anthony Liguoric227f092009-10-01 16:12:16 -05004717static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004718#define DEF(option, callback, arg_string) \
4719 { option, callback },
4720#include "qemu-img-cmds.h"
4721#undef DEF
4722#undef GEN_DOCS
4723 { NULL, NULL, },
4724};
4725
bellardea2384d2004-08-01 21:59:26 +00004726int main(int argc, char **argv)
4727{
Anthony Liguoric227f092009-10-01 16:12:16 -05004728 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004729 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004730 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004731 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004732 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004733 static const struct option long_options[] = {
4734 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004735 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004736 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004737 {0, 0, 0, 0}
4738 };
bellardea2384d2004-08-01 21:59:26 +00004739
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004740#ifdef CONFIG_POSIX
4741 signal(SIGPIPE, SIG_IGN);
4742#endif
4743
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004744 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004745 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004746 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004747
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004748 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004749 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004750 exit(EXIT_FAILURE);
4751 }
4752
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004753 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004754
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004755 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004756 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004757 if (argc < 2) {
4758 error_exit("Not enough arguments");
4759 }
Stuart Brady153859b2009-06-07 00:42:17 +01004760
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004761 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004762 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004763 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004764
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004765 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004766 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004767 case ':':
4768 missing_argument(argv[optind - 1]);
4769 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08004770 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004771 unrecognized_option(argv[optind - 1]);
4772 return 0;
4773 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03004774 help();
4775 return 0;
4776 case 'V':
4777 printf(QEMU_IMG_VERSION);
4778 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004779 case 'T':
4780 g_free(trace_file);
4781 trace_file = trace_opt_parse(optarg);
4782 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004783 }
bellardea2384d2004-08-01 21:59:26 +00004784 }
Stuart Brady153859b2009-06-07 00:42:17 +01004785
Denis V. Lunev10985132016-06-17 17:44:13 +03004786 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004787
Denis V. Lunev10985132016-06-17 17:44:13 +03004788 /* reset getopt_long scanning */
4789 argc -= optind;
4790 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004791 return 0;
4792 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004793 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004794 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004795
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004796 if (!trace_init_backends()) {
4797 exit(1);
4798 }
4799 trace_init_file(trace_file);
4800 qemu_set_log(LOG_TRACE);
4801
Denis V. Lunev10985132016-06-17 17:44:13 +03004802 /* find the command */
4803 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4804 if (!strcmp(cmdname, cmd->name)) {
4805 return cmd->handler(argc, argv);
4806 }
4807 }
Jeff Cody7db16892014-04-25 17:02:32 -04004808
Stuart Brady153859b2009-06-07 00:42:17 +01004809 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004810 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004811}