blob: 821cbf610e5fbd5dd647ecba70f750c685b7bc5c [file] [log] [blame]
bellardea2384d2004-08-01 21:59:26 +00001/*
bellardfb43f4d2006-08-07 21:34:46 +00002 * QEMU disk image utility
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardea2384d2004-08-01 21:59:26 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Markus Armbruster452fcdb2018-02-01 12:18:39 +010024
Peter Maydell80c71a22016-01-18 18:01:42 +000025#include "qemu/osdep.h"
Eric Blakec2a3d7d2017-07-21 08:50:47 -050026#include <getopt.h>
27
Markus Armbrustera8d25322019-05-23 16:35:08 +020028#include "qemu-common.h"
Fam Zheng67a1de02016-06-01 17:44:21 +080029#include "qemu-version.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010030#include "qapi/error.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010031#include "qapi/qapi-visit-block-core.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010032#include "qapi/qobject-output-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010033#include "qapi/qmp/qjson.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010034#include "qapi/qmp/qdict.h"
Markus Armbrusterfc81fa12018-02-01 12:18:40 +010035#include "qapi/qmp/qstring.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020036#include "qemu/cutils.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000037#include "qemu/config-file.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010038#include "qemu/option.h"
39#include "qemu/error-report.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030040#include "qemu/log.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020041#include "qemu/main-loop.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020042#include "qemu/module.h"
Stefano Garzarella97ede572019-05-08 12:43:24 +020043#include "qemu/units.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000044#include "qom/object_interfaces.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020045#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010046#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020047#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080048#include "block/qapi.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010049#include "crypto/init.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030050#include "trace/control.h"
bellarde8445332006-06-14 15:32:10 +000051
Thomas Huth7e563bf2018-02-15 12:06:47 +010052#define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \
Thomas Huth0781dd62016-10-05 11:54:44 +020053 "\n" QEMU_COPYRIGHT "\n"
Jeff Cody5f6979c2014-04-28 14:37:18 -040054
Anthony Liguoric227f092009-10-01 16:12:16 -050055typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010056 const char *name;
57 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050058} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010059
Federico Simoncelli8599ea42013-01-28 06:59:47 -050060enum {
61 OPTION_OUTPUT = 256,
62 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000063 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000064 OPTION_IMAGE_OPTS = 259,
Kevin Wolfb6495fa2015-07-10 18:09:18 +020065 OPTION_PATTERN = 260,
Kevin Wolf55d539c2016-06-03 13:59:41 +020066 OPTION_FLUSH_INTERVAL = 261,
67 OPTION_NO_DRAIN = 262,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +010068 OPTION_TARGET_IMAGE_OPTS = 263,
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +010069 OPTION_SIZE = 264,
Max Reitzdc5f6902017-06-13 22:20:55 +020070 OPTION_PREALLOCATION = 265,
Pavel Butsykin4ffca892017-09-18 15:42:27 +030071 OPTION_SHRINK = 266,
Max Reitz8eaac022019-05-07 22:35:03 +020072 OPTION_SALVAGE = 267,
David Edmondson168468f2020-02-05 11:02:48 +000073 OPTION_TARGET_IS_ZERO = 268,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050074};
75
76typedef enum OutputFormat {
77 OFORMAT_JSON,
78 OFORMAT_HUMAN,
79} OutputFormat;
80
Kevin Wolfe6996142016-03-15 13:03:11 +010081/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040082#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000083
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010084static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000085{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010086 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000087}
88
Fam Zhengac1307a2014-04-22 13:36:11 +080089static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
90{
91 va_list ap;
92
Fam Zhengac1307a2014-04-22 13:36:11 +080093 va_start(ap, fmt);
Markus Armbrustere9e1d922019-04-17 21:06:27 +020094 error_vreport(fmt, ap);
Fam Zhengac1307a2014-04-22 13:36:11 +080095 va_end(ap);
96
Markus Armbrustere9e1d922019-04-17 21:06:27 +020097 error_printf("Try 'qemu-img --help' for more information\n");
Fam Zhengac1307a2014-04-22 13:36:11 +080098 exit(EXIT_FAILURE);
99}
100
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800101static void QEMU_NORETURN missing_argument(const char *option)
102{
103 error_exit("missing argument for option '%s'", option);
104}
105
106static void QEMU_NORETURN unrecognized_option(const char *option)
107{
108 error_exit("unrecognized option '%s'", option);
109}
110
blueswir1d2c639d2009-01-24 18:19:25 +0000111/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +0800112static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +0000113{
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100114 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -0400115 QEMU_IMG_VERSION
Denis V. Lunev10985132016-06-17 17:44:13 +0300116 "usage: qemu-img [standard options] command [command options]\n"
malc3f020d72010-02-08 12:04:56 +0300117 "QEMU disk image utility\n"
118 "\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300119 " '-h', '--help' display this help and exit\n"
120 " '-V', '--version' output version information and exit\n"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +0300121 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
122 " specify tracing options\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300123 "\n"
malc3f020d72010-02-08 12:04:56 +0300124 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100125#define DEF(option, callback, arg_string) \
126 " " arg_string "\n"
127#include "qemu-img-cmds.h"
128#undef DEF
malc3f020d72010-02-08 12:04:56 +0300129 "\n"
130 "Command parameters:\n"
131 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000132 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
133 " manual page for a description of the object properties. The most common\n"
134 " object type is a 'secret', which is used to supply passwords and/or\n"
135 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300136 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400137 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800138 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
139 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100140 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
141 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300142 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200143 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
144 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
145 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300146 " 'output_filename' is the destination disk image filename\n"
147 " 'output_fmt' is the destination format\n"
148 " 'options' is a comma separated list of format specific options in a\n"
149 " name=value format. Use -o ? for an overview of the options supported by the\n"
150 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800151 " 'snapshot_param' is param used for internal snapshot, format\n"
152 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
153 " '[ID_OR_NAME]'\n"
malc3f020d72010-02-08 12:04:56 +0300154 " '-c' indicates that target image must be compressed (qcow format only)\n"
John Snow6e6e55f2017-07-17 20:34:22 -0400155 " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
156 " new backing file match exactly. The image doesn't need a working\n"
157 " backing file before rebasing in this case (useful for renaming the\n"
158 " backing file). For image creation, allow creating without attempting\n"
159 " to open the backing file.\n"
malc3f020d72010-02-08 12:04:56 +0300160 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200161 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100162 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200163 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
164 " contain only zeros for qemu-img to create a sparse image during\n"
165 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
166 " unallocated or zero sectors, and the destination image will always be\n"
167 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200168 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100169 " '-n' skips the target volume creation (useful if the volume is created\n"
170 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300171 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200172 "Parameters to check subcommand:\n"
173 " '-r' tries to repair any inconsistencies that are found during the check.\n"
174 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
175 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200176 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200177 "\n"
Peter Lieven2d9187b2017-02-28 13:40:07 +0100178 "Parameters to convert subcommand:\n"
179 " '-m' specifies how many coroutines work in parallel during the convert\n"
180 " process (defaults to 8)\n"
181 " '-W' allow to write to the target out of order rather than sequential\n"
182 "\n"
malc3f020d72010-02-08 12:04:56 +0300183 "Parameters to snapshot subcommand:\n"
184 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
185 " '-a' applies a snapshot (revert disk to saved state)\n"
186 " '-c' creates a snapshot\n"
187 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100188 " '-l' lists all snapshots in the given image\n"
189 "\n"
190 "Parameters to compare subcommand:\n"
191 " '-f' first image format\n"
192 " '-F' second image format\n"
Reda Sallahi86ce1f62016-08-10 04:43:12 +0200193 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
194 "\n"
195 "Parameters to dd subcommand:\n"
196 " 'bs=BYTES' read and write up to BYTES bytes at a time "
197 "(default: 512)\n"
198 " 'count=N' copy only N input blocks\n"
199 " 'if=FILE' read from FILE\n"
Reda Sallahif7c15532016-08-10 16:16:09 +0200200 " 'of=FILE' write to FILE\n"
201 " 'skip=N' skip N bs-sized blocks at the start of input\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100202
203 printf("%s\nSupported formats:", help_msg);
Andrey Shinkevich9ac404c2019-03-07 16:33:58 +0300204 bdrv_iterate_format(format_print, NULL, false);
Eric Blakef5048cb2017-08-03 11:33:53 -0500205 printf("\n\n" QEMU_HELP_BOTTOM "\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800206 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000207}
208
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000209static QemuOptsList qemu_object_opts = {
210 .name = "object",
211 .implied_opt_name = "qom-type",
212 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
213 .desc = {
214 { }
215 },
216};
217
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +0200218static bool qemu_img_object_print_help(const char *type, QemuOpts *opts)
219{
220 if (user_creatable_print_help(type, opts)) {
221 exit(0);
222 }
223 return true;
224}
225
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000226static QemuOptsList qemu_source_opts = {
227 .name = "source",
228 .implied_opt_name = "file",
229 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
230 .desc = {
231 { }
232 },
233};
234
Stefan Weil7c30f652013-06-16 17:01:05 +0200235static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100236{
237 int ret = 0;
238 if (!quiet) {
239 va_list args;
240 va_start(args, fmt);
241 ret = vprintf(fmt, args);
242 va_end(args);
243 }
244 return ret;
245}
246
bellardea2384d2004-08-01 21:59:26 +0000247
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100248static int print_block_option_help(const char *filename, const char *fmt)
249{
250 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800251 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500252 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100253
254 /* Find driver and parse its options */
255 drv = bdrv_find_format(fmt);
256 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100257 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100258 return 1;
259 }
260
Max Reitzd402b6a2018-05-09 23:00:21 +0200261 if (!drv->create_opts) {
262 error_report("Format driver '%s' does not support image creation", fmt);
263 return 1;
264 }
265
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800266 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100267 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500268 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100269 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100270 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800271 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100272 return 1;
273 }
Max Reitzd402b6a2018-05-09 23:00:21 +0200274 if (!proto_drv->create_opts) {
Max Reitzf0998872018-11-19 11:19:20 +0100275 error_report("Protocol driver '%s' does not support image creation",
Max Reitzd402b6a2018-05-09 23:00:21 +0200276 proto_drv->format_name);
Max Reitz3ecd5a42018-11-19 11:19:21 +0100277 qemu_opts_free(create_opts);
Max Reitzd402b6a2018-05-09 23:00:21 +0200278 return 1;
279 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800280 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100281 }
282
Max Reitzf4619af2019-04-13 17:20:37 +0200283 if (filename) {
284 printf("Supported options:\n");
285 } else {
286 printf("Supported %s options:\n", fmt);
287 }
Max Reitz63898712018-10-19 18:49:25 +0200288 qemu_opts_print_help(create_opts, false);
Chunyan Liu83d05212014-06-05 17:20:51 +0800289 qemu_opts_free(create_opts);
Max Reitzf4619af2019-04-13 17:20:37 +0200290
291 if (!filename) {
292 printf("\n"
293 "The protocol level may support further options.\n"
294 "Specify the target filename to include those options.\n");
295 }
296
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100297 return 0;
298}
299
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000300
Max Reitzefaa7c42016-03-16 19:54:38 +0100301static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100302 QemuOpts *opts, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800303 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000304{
305 QDict *options;
306 Error *local_err = NULL;
307 BlockBackend *blk;
308 options = qemu_opts_to_qdict(opts, NULL);
Fam Zheng335e9932017-05-03 00:35:39 +0800309 if (force_share) {
310 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
Max Reitz4615f872018-05-02 22:20:50 +0200311 && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) {
Fam Zheng335e9932017-05-03 00:35:39 +0800312 error_report("--force-share/-U conflicts with image options");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200313 qobject_unref(options);
Fam Zheng335e9932017-05-03 00:35:39 +0800314 return NULL;
315 }
Max Reitz4615f872018-05-02 22:20:50 +0200316 qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on");
Fam Zheng335e9932017-05-03 00:35:39 +0800317 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100318 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000319 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100320 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000321 return NULL;
322 }
Kevin Wolfce099542016-03-15 13:01:04 +0100323 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000324
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000325 return blk;
326}
327
Max Reitzefaa7c42016-03-16 19:54:38 +0100328static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100329 QDict *options,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000330 const char *fmt, int flags,
Fam Zheng335e9932017-05-03 00:35:39 +0800331 bool writethrough, bool quiet,
332 bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000333{
334 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200335 Error *local_err = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100336
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100337 if (!options) {
338 options = qdict_new();
339 }
bellard75c23802004-08-27 21:28:58 +0000340 if (fmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500341 qdict_put_str(options, "driver", fmt);
bellard75c23802004-08-27 21:28:58 +0000342 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100343
Fam Zheng335e9932017-05-03 00:35:39 +0800344 if (force_share) {
Eric Blake579cf1d2017-05-15 14:54:39 -0500345 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800346 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100347 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500348 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100349 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000350 return NULL;
bellard75c23802004-08-27 21:28:58 +0000351 }
Kevin Wolfce099542016-03-15 13:01:04 +0100352 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100353
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200354 return blk;
bellard75c23802004-08-27 21:28:58 +0000355}
356
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000357
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100358static int img_add_key_secrets(void *opaque,
359 const char *name, const char *value,
360 Error **errp)
361{
362 QDict *options = opaque;
363
364 if (g_str_has_suffix(name, "key-secret")) {
Eric Blake187f47e2017-06-24 12:10:07 -0600365 qdict_put_str(options, name, value);
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100366 }
367
368 return 0;
369}
370
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100371
Max Reitzefaa7c42016-03-16 19:54:38 +0100372static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000373 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100374 const char *fmt, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800375 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000376{
377 BlockBackend *blk;
378 if (image_opts) {
379 QemuOpts *opts;
380 if (fmt) {
381 error_report("--image-opts and --format are mutually exclusive");
382 return NULL;
383 }
384 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
385 filename, true);
386 if (!opts) {
387 return NULL;
388 }
Fam Zheng335e9932017-05-03 00:35:39 +0800389 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
390 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000391 } else {
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100392 blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
Fam Zheng335e9932017-05-03 00:35:39 +0800393 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000394 }
395 return blk;
396}
397
398
Chunyan Liu83d05212014-06-05 17:20:51 +0800399static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100400 const char *base_filename,
401 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200402{
Markus Armbruster6750e792015-02-12 17:43:08 +0100403 Error *err = NULL;
404
Kevin Wolfefa84d42009-05-18 16:42:12 +0200405 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100406 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100407 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100408 error_report("Backing file not supported for file format '%s'",
409 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100410 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900411 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200412 }
413 }
414 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100415 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100416 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100417 error_report("Backing file format not supported for file "
418 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100419 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900420 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200421 }
422 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900423 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200424}
425
Markus Armbruster606caa02017-02-21 21:14:04 +0100426static int64_t cvtnum(const char *s)
427{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100428 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100429 uint64_t value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100430
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100431 err = qemu_strtosz(s, NULL, &value);
432 if (err < 0) {
433 return err;
434 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100435 if (value > INT64_MAX) {
436 return -ERANGE;
437 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100438 return value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100439}
440
bellardea2384d2004-08-01 21:59:26 +0000441static int img_create(int argc, char **argv)
442{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200443 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100444 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000445 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000446 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000447 const char *filename;
448 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200449 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200450 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100451 bool quiet = false;
John Snow6e6e55f2017-07-17 20:34:22 -0400452 int flags = 0;
ths3b46e622007-09-17 08:09:54 +0000453
bellardea2384d2004-08-01 21:59:26 +0000454 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000455 static const struct option long_options[] = {
456 {"help", no_argument, 0, 'h'},
457 {"object", required_argument, 0, OPTION_OBJECT},
458 {0, 0, 0, 0}
459 };
John Snow6e6e55f2017-07-17 20:34:22 -0400460 c = getopt_long(argc, argv, ":F:b:f:ho:qu",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000461 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100462 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000463 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100464 }
bellardea2384d2004-08-01 21:59:26 +0000465 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800466 case ':':
467 missing_argument(argv[optind - 1]);
468 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100469 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800470 unrecognized_option(argv[optind - 1]);
471 break;
bellardea2384d2004-08-01 21:59:26 +0000472 case 'h':
473 help();
474 break;
aliguori9230eaf2009-03-28 17:55:19 +0000475 case 'F':
476 base_fmt = optarg;
477 break;
bellardea2384d2004-08-01 21:59:26 +0000478 case 'b':
479 base_filename = optarg;
480 break;
481 case 'f':
482 fmt = optarg;
483 break;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200484 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100485 if (!is_valid_option_list(optarg)) {
486 error_report("Invalid option list: %s", optarg);
487 goto fail;
488 }
489 if (!options) {
490 options = g_strdup(optarg);
491 } else {
492 char *old_options = options;
493 options = g_strdup_printf("%s,%s", options, optarg);
494 g_free(old_options);
495 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200496 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100497 case 'q':
498 quiet = true;
499 break;
John Snow6e6e55f2017-07-17 20:34:22 -0400500 case 'u':
501 flags |= BDRV_O_NO_BACKING;
502 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000503 case OPTION_OBJECT: {
504 QemuOpts *opts;
505 opts = qemu_opts_parse_noisily(&qemu_object_opts,
506 optarg, true);
507 if (!opts) {
508 goto fail;
509 }
510 } break;
bellardea2384d2004-08-01 21:59:26 +0000511 }
512 }
aliguori9230eaf2009-03-28 17:55:19 +0000513
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900514 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100515 filename = (optind < argc) ? argv[optind] : NULL;
516 if (options && has_help_option(options)) {
517 g_free(options);
518 return print_block_option_help(filename, fmt);
519 }
520
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100521 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800522 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100523 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100524 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900525
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000526 if (qemu_opts_foreach(&qemu_object_opts,
527 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +0200528 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000529 goto fail;
530 }
531
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100532 /* Get image size, if specified */
533 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100534 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100535
536 sval = cvtnum(argv[optind++]);
537 if (sval < 0) {
liguang79443392012-12-17 09:49:23 +0800538 if (sval == -ERANGE) {
539 error_report("Image size must be less than 8 EiB!");
540 } else {
541 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200542 "G, T, P or E suffixes for ");
543 error_report("kilobytes, megabytes, gigabytes, terabytes, "
544 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800545 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100546 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100547 }
548 img_size = (uint64_t)sval;
549 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200550 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800551 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200552 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100553
Luiz Capitulino9b375252012-11-30 10:52:05 -0200554 bdrv_img_create(filename, fmt, base_filename, base_fmt,
John Snow6e6e55f2017-07-17 20:34:22 -0400555 options, img_size, flags, quiet, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100556 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100557 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100558 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900559 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200560
Kevin Wolf77386bf2014-02-21 16:24:04 +0100561 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000562 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100563
564fail:
565 g_free(options);
566 return 1;
bellardea2384d2004-08-01 21:59:26 +0000567}
568
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100569static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500570{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500571 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500572 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100573 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600574
575 visit_type_ImageCheck(v, NULL, &check, &error_abort);
576 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500577 str = qobject_to_json_pretty(obj);
578 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100579 qprintf(quiet, "%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200580 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600581 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200582 qobject_unref(str);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500583}
584
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100585static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500586{
587 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100588 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500589 } else {
590 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100591 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
592 "Data may be corrupted, or further writes to the image "
593 "may corrupt it.\n",
594 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500595 }
596
597 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100598 qprintf(quiet,
599 "\n%" PRId64 " leaked clusters were found on the image.\n"
600 "This means waste of disk space, but no harm to data.\n",
601 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500602 }
603
604 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100605 qprintf(quiet,
606 "\n%" PRId64
607 " internal errors have occurred during the check.\n",
608 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500609 }
610 }
611
612 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100613 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
614 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
615 check->allocated_clusters, check->total_clusters,
616 check->allocated_clusters * 100.0 / check->total_clusters,
617 check->fragmented_clusters * 100.0 / check->allocated_clusters,
618 check->compressed_clusters * 100.0 /
619 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500620 }
621
622 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100623 qprintf(quiet,
624 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500625 }
626}
627
628static int collect_image_check(BlockDriverState *bs,
629 ImageCheck *check,
630 const char *filename,
631 const char *fmt,
632 int fix)
633{
634 int ret;
635 BdrvCheckResult result;
636
637 ret = bdrv_check(bs, &result, fix);
638 if (ret < 0) {
639 return ret;
640 }
641
642 check->filename = g_strdup(filename);
643 check->format = g_strdup(bdrv_get_format_name(bs));
644 check->check_errors = result.check_errors;
645 check->corruptions = result.corruptions;
646 check->has_corruptions = result.corruptions != 0;
647 check->leaks = result.leaks;
648 check->has_leaks = result.leaks != 0;
649 check->corruptions_fixed = result.corruptions_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100650 check->has_corruptions_fixed = result.corruptions_fixed != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500651 check->leaks_fixed = result.leaks_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100652 check->has_leaks_fixed = result.leaks_fixed != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500653 check->image_end_offset = result.image_end_offset;
654 check->has_image_end_offset = result.image_end_offset != 0;
655 check->total_clusters = result.bfi.total_clusters;
656 check->has_total_clusters = result.bfi.total_clusters != 0;
657 check->allocated_clusters = result.bfi.allocated_clusters;
658 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
659 check->fragmented_clusters = result.bfi.fragmented_clusters;
660 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100661 check->compressed_clusters = result.bfi.compressed_clusters;
662 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500663
664 return 0;
665}
666
Kevin Wolfe076f332010-06-29 11:43:13 +0200667/*
668 * Checks an image for consistency. Exit codes:
669 *
Max Reitzd6635c42014-06-02 22:15:21 +0200670 * 0 - Check completed, image is good
671 * 1 - Check not completed because of internal errors
672 * 2 - Check completed, image is corrupted
673 * 3 - Check completed, image has leaked clusters, but is good otherwise
674 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200675 */
aliguori15859692009-04-21 23:11:53 +0000676static int img_check(int argc, char **argv)
677{
678 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500679 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200680 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200681 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000682 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200683 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100684 int flags = BDRV_O_CHECK;
685 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200686 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100687 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000688 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +0800689 bool force_share = false;
aliguori15859692009-04-21 23:11:53 +0000690
691 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500692 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200693 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100694
aliguori15859692009-04-21 23:11:53 +0000695 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500696 int option_index = 0;
697 static const struct option long_options[] = {
698 {"help", no_argument, 0, 'h'},
699 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530700 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500701 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000702 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000703 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +0800704 {"force-share", no_argument, 0, 'U'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500705 {0, 0, 0, 0}
706 };
Fam Zheng335e9932017-05-03 00:35:39 +0800707 c = getopt_long(argc, argv, ":hf:r:T:qU",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500708 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100709 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000710 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100711 }
aliguori15859692009-04-21 23:11:53 +0000712 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800713 case ':':
714 missing_argument(argv[optind - 1]);
715 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100716 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800717 unrecognized_option(argv[optind - 1]);
718 break;
aliguori15859692009-04-21 23:11:53 +0000719 case 'h':
720 help();
721 break;
722 case 'f':
723 fmt = optarg;
724 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200725 case 'r':
726 flags |= BDRV_O_RDWR;
727
728 if (!strcmp(optarg, "leaks")) {
729 fix = BDRV_FIX_LEAKS;
730 } else if (!strcmp(optarg, "all")) {
731 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
732 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800733 error_exit("Unknown option value for -r "
734 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200735 }
736 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500737 case OPTION_OUTPUT:
738 output = optarg;
739 break;
Max Reitz40055952014-07-22 22:58:42 +0200740 case 'T':
741 cache = optarg;
742 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100743 case 'q':
744 quiet = true;
745 break;
Fam Zheng335e9932017-05-03 00:35:39 +0800746 case 'U':
747 force_share = true;
748 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000749 case OPTION_OBJECT: {
750 QemuOpts *opts;
751 opts = qemu_opts_parse_noisily(&qemu_object_opts,
752 optarg, true);
753 if (!opts) {
754 return 1;
755 }
756 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000757 case OPTION_IMAGE_OPTS:
758 image_opts = true;
759 break;
aliguori15859692009-04-21 23:11:53 +0000760 }
761 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200762 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800763 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100764 }
aliguori15859692009-04-21 23:11:53 +0000765 filename = argv[optind++];
766
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500767 if (output && !strcmp(output, "json")) {
768 output_format = OFORMAT_JSON;
769 } else if (output && !strcmp(output, "human")) {
770 output_format = OFORMAT_HUMAN;
771 } else if (output) {
772 error_report("--output must be used with human or json as argument.");
773 return 1;
774 }
775
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000776 if (qemu_opts_foreach(&qemu_object_opts,
777 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +0200778 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000779 return 1;
780 }
781
Kevin Wolfce099542016-03-15 13:01:04 +0100782 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200783 if (ret < 0) {
784 error_report("Invalid source cache option: %s", cache);
785 return 1;
786 }
787
Fam Zheng335e9932017-05-03 00:35:39 +0800788 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
789 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200790 if (!blk) {
791 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900792 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200793 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500794
795 check = g_new0(ImageCheck, 1);
796 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200797
798 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200799 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200800 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500801 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200802 }
803
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500804 if (check->corruptions_fixed || check->leaks_fixed) {
805 int corruptions_fixed, leaks_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100806 bool has_leaks_fixed, has_corruptions_fixed;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500807
808 leaks_fixed = check->leaks_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100809 has_leaks_fixed = check->has_leaks_fixed;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500810 corruptions_fixed = check->corruptions_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100811 has_corruptions_fixed = check->has_corruptions_fixed;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500812
813 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100814 qprintf(quiet,
815 "The following inconsistencies were found and repaired:\n\n"
816 " %" PRId64 " leaked clusters\n"
817 " %" PRId64 " corruptions\n\n"
818 "Double checking the fixed image now...\n",
819 check->leaks_fixed,
820 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500821 }
822
Pan Nengyuanfc124ea2020-02-27 09:29:50 +0800823 qapi_free_ImageCheck(check);
824 check = g_new0(ImageCheck, 1);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500825 ret = collect_image_check(bs, check, filename, fmt, 0);
826
827 check->leaks_fixed = leaks_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100828 check->has_leaks_fixed = has_leaks_fixed;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500829 check->corruptions_fixed = corruptions_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100830 check->has_corruptions_fixed = has_corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200831 }
832
Max Reitz832390a2014-10-23 15:29:12 +0200833 if (!ret) {
834 switch (output_format) {
835 case OFORMAT_HUMAN:
836 dump_human_image_check(check, quiet);
837 break;
838 case OFORMAT_JSON:
839 dump_json_image_check(check, quiet);
840 break;
841 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500842 }
843
844 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200845 if (ret) {
846 error_report("Check failed: %s", strerror(-ret));
847 } else {
848 error_report("Check failed");
849 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500850 ret = 1;
851 goto fail;
852 }
853
854 if (check->corruptions) {
855 ret = 2;
856 } else if (check->leaks) {
857 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200858 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500859 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000860 }
861
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500862fail:
863 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200864 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500865 return ret;
aliguori15859692009-04-21 23:11:53 +0000866}
867
Max Reitzd4a32382014-10-24 15:57:37 +0200868typedef struct CommonBlockJobCBInfo {
869 BlockDriverState *bs;
870 Error **errp;
871} CommonBlockJobCBInfo;
872
873static void common_block_job_cb(void *opaque, int ret)
874{
875 CommonBlockJobCBInfo *cbi = opaque;
876
877 if (ret < 0) {
878 error_setg_errno(cbi->errp, -ret, "Block job failed");
879 }
Max Reitzd4a32382014-10-24 15:57:37 +0200880}
881
882static void run_block_job(BlockJob *job, Error **errp)
883{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200884 AioContext *aio_context = blk_get_aio_context(job->blk);
sochin.jiang4172a002017-06-15 14:47:33 +0800885 int ret = 0;
Max Reitzd4a32382014-10-24 15:57:37 +0200886
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200887 aio_context_acquire(aio_context);
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200888 job_ref(&job->job);
Max Reitzd4a32382014-10-24 15:57:37 +0200889 do {
Kevin Wolf30a5c882018-05-04 12:17:20 +0200890 float progress = 0.0f;
Max Reitzd4a32382014-10-24 15:57:37 +0200891 aio_poll(aio_context, true);
Vladimir Sementsov-Ogievskiy01fe1ca2020-03-11 13:29:56 +0300892 if (job->job.progress.total) {
893 progress = (float)job->job.progress.current /
894 job->job.progress.total * 100.f;
Kevin Wolf30a5c882018-05-04 12:17:20 +0200895 }
896 qemu_progress_print(progress, 0);
Kevin Wolfdf956ae2018-04-25 15:09:58 +0200897 } while (!job_is_ready(&job->job) && !job_is_completed(&job->job));
Max Reitzd4a32382014-10-24 15:57:37 +0200898
Kevin Wolfdbe5e6c2018-04-19 13:04:01 +0200899 if (!job_is_completed(&job->job)) {
Kevin Wolf3d70ff52018-04-24 16:13:52 +0200900 ret = job_complete_sync(&job->job, errp);
sochin.jiang4172a002017-06-15 14:47:33 +0800901 } else {
Kevin Wolf4ad35182018-04-19 17:30:16 +0200902 ret = job->job.ret;
sochin.jiang4172a002017-06-15 14:47:33 +0800903 }
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200904 job_unref(&job->job);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200905 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200906
sochin.jiang4172a002017-06-15 14:47:33 +0800907 /* publish completion progress only when success */
908 if (!ret) {
909 qemu_progress_print(100.f, 0);
910 }
Max Reitzd4a32382014-10-24 15:57:37 +0200911}
912
bellardea2384d2004-08-01 21:59:26 +0000913static int img_commit(int argc, char **argv)
914{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400915 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200916 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200917 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200918 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100919 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200920 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100921 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200922 Error *local_err = NULL;
923 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000924 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200925 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000926
927 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400928 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200929 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000930 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000931 static const struct option long_options[] = {
932 {"help", no_argument, 0, 'h'},
933 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000934 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000935 {0, 0, 0, 0}
936 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800937 c = getopt_long(argc, argv, ":f:ht:b:dpq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000938 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100939 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000940 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100941 }
bellardea2384d2004-08-01 21:59:26 +0000942 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800943 case ':':
944 missing_argument(argv[optind - 1]);
945 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100946 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800947 unrecognized_option(argv[optind - 1]);
948 break;
bellardea2384d2004-08-01 21:59:26 +0000949 case 'h':
950 help();
951 break;
952 case 'f':
953 fmt = optarg;
954 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400955 case 't':
956 cache = optarg;
957 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200958 case 'b':
959 base = optarg;
960 /* -b implies -d */
961 drop = true;
962 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200963 case 'd':
964 drop = true;
965 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200966 case 'p':
967 progress = true;
968 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100969 case 'q':
970 quiet = true;
971 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000972 case OPTION_OBJECT: {
973 QemuOpts *opts;
974 opts = qemu_opts_parse_noisily(&qemu_object_opts,
975 optarg, true);
976 if (!opts) {
977 return 1;
978 }
979 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000980 case OPTION_IMAGE_OPTS:
981 image_opts = true;
982 break;
bellardea2384d2004-08-01 21:59:26 +0000983 }
984 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200985
986 /* Progress is not shown in Quiet mode */
987 if (quiet) {
988 progress = false;
989 }
990
Kevin Wolffc11eb22013-08-05 10:53:04 +0200991 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800992 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100993 }
bellardea2384d2004-08-01 21:59:26 +0000994 filename = argv[optind++];
995
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000996 if (qemu_opts_foreach(&qemu_object_opts,
997 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +0200998 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000999 return 1;
1000 }
1001
Max Reitz9a86fe42014-10-24 15:57:38 +02001002 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +01001003 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001004 if (ret < 0) {
1005 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +01001006 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001007 }
1008
Fam Zheng335e9932017-05-03 00:35:39 +08001009 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
1010 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001011 if (!blk) {
1012 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001013 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001014 bs = blk_bs(blk);
1015
Max Reitz687fa1d2014-10-24 15:57:39 +02001016 qemu_progress_init(progress, 1.f);
1017 qemu_progress_print(0.f, 100);
1018
Max Reitz1b22bff2014-10-24 15:57:40 +02001019 if (base) {
1020 base_bs = bdrv_find_backing_image(bs, base);
1021 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +01001022 error_setg(&local_err,
1023 "Did not find '%s' in the backing chain of '%s'",
1024 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +02001025 goto done;
1026 }
1027 } else {
1028 /* This is different from QMP, which by default uses the deepest file in
1029 * the backing chain (i.e., the very base); however, the traditional
1030 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +02001031 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +02001032 if (!base_bs) {
1033 error_setg(&local_err, "Image does not have a backing file");
1034 goto done;
1035 }
bellardea2384d2004-08-01 21:59:26 +00001036 }
1037
Max Reitzd4a32382014-10-24 15:57:37 +02001038 cbi = (CommonBlockJobCBInfo){
1039 .errp = &local_err,
1040 .bs = bs,
1041 };
1042
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001043 aio_context = bdrv_get_aio_context(bs);
1044 aio_context_acquire(aio_context);
Kevin Wolfbb02b652018-04-19 17:54:56 +02001045 commit_active_start("commit", bs, base_bs, JOB_DEFAULT, 0,
Kevin Wolf0db832f2017-02-20 18:10:05 +01001046 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +08001047 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001048 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +02001049 if (local_err) {
1050 goto done;
1051 }
1052
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001053 /* When the block job completes, the BlockBackend reference will point to
1054 * the old backing file. In order to avoid that the top image is already
1055 * deleted, so we can still empty it afterwards, increment the reference
1056 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +02001057 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001058 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001059 }
1060
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001061 job = block_job_get("commit");
Liam Merwick2e2db262018-11-05 21:38:37 +00001062 assert(job);
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001063 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001064 if (local_err) {
1065 goto unref_backing;
1066 }
1067
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001068 if (!drop && bs->drv->bdrv_make_empty) {
1069 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001070 if (ret) {
1071 error_setg_errno(&local_err, -ret, "Could not empty %s",
1072 filename);
1073 goto unref_backing;
1074 }
1075 }
1076
1077unref_backing:
1078 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001079 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001080 }
Max Reitzd4a32382014-10-24 15:57:37 +02001081
1082done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001083 qemu_progress_end();
1084
Markus Armbruster26f54e92014-10-07 13:59:04 +02001085 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001086
1087 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001088 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001089 return 1;
1090 }
Max Reitzd4a32382014-10-24 15:57:37 +02001091
1092 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001093 return 0;
1094}
1095
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001096/*
Eric Blakedebb38a2017-10-11 22:47:11 -05001097 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1098 * of the first sector boundary within buf where the sector contains a
1099 * non-zero byte. This function is robust to a buffer that is not
1100 * sector-aligned.
1101 */
1102static int64_t find_nonzero(const uint8_t *buf, int64_t n)
1103{
1104 int64_t i;
1105 int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE);
1106
1107 for (i = 0; i < end; i += BDRV_SECTOR_SIZE) {
1108 if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) {
1109 return i;
1110 }
1111 }
1112 if (i < n && !buffer_is_zero(buf + i, n - end)) {
1113 return i;
1114 }
1115 return -1;
1116}
1117
1118/*
thsf58c7b32008-06-05 21:53:49 +00001119 * Returns true iff the first sector pointed to by 'buf' contains at least
1120 * a non-NUL byte.
1121 *
1122 * 'pnum' is set to the number of sectors (including and immediately following
1123 * the first one) that are known to be in the same allocated/unallocated state.
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001124 * The function will try to align the end offset to alignment boundaries so
1125 * that the request will at least end aligned and consequtive requests will
1126 * also start at an aligned offset.
thsf58c7b32008-06-05 21:53:49 +00001127 */
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001128static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum,
1129 int64_t sector_num, int alignment)
bellardea2384d2004-08-01 21:59:26 +00001130{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001131 bool is_zero;
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001132 int i, tail;
bellardea2384d2004-08-01 21:59:26 +00001133
1134 if (n <= 0) {
1135 *pnum = 0;
1136 return 0;
1137 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001138 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001139 for(i = 1; i < n; i++) {
1140 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001141 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001142 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001143 }
bellardea2384d2004-08-01 21:59:26 +00001144 }
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001145
1146 tail = (sector_num + i) & (alignment - 1);
1147 if (tail) {
1148 if (is_zero && i <= tail) {
1149 /* treat unallocated areas which only consist
1150 * of a small tail as allocated. */
1151 is_zero = false;
1152 }
1153 if (!is_zero) {
1154 /* align up end offset of allocated areas. */
1155 i += alignment - tail;
1156 i = MIN(i, n);
1157 } else {
1158 /* align down end offset of zero areas. */
1159 i -= tail;
1160 }
1161 }
bellardea2384d2004-08-01 21:59:26 +00001162 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001163 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001164}
1165
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001166/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001167 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1168 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1169 * breaking up write requests for only small sparse areas.
1170 */
1171static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001172 int min, int64_t sector_num, int alignment)
Kevin Wolfa22f1232011-08-26 15:27:13 +02001173{
1174 int ret;
1175 int num_checked, num_used;
1176
1177 if (n < min) {
1178 min = n;
1179 }
1180
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001181 ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment);
Kevin Wolfa22f1232011-08-26 15:27:13 +02001182 if (!ret) {
1183 return ret;
1184 }
1185
1186 num_used = *pnum;
1187 buf += BDRV_SECTOR_SIZE * *pnum;
1188 n -= *pnum;
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001189 sector_num += *pnum;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001190 num_checked = num_used;
1191
1192 while (n > 0) {
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001193 ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment);
Kevin Wolfa22f1232011-08-26 15:27:13 +02001194
1195 buf += BDRV_SECTOR_SIZE * *pnum;
1196 n -= *pnum;
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001197 sector_num += *pnum;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001198 num_checked += *pnum;
1199 if (ret) {
1200 num_used = num_checked;
1201 } else if (*pnum >= min) {
1202 break;
1203 }
1204 }
1205
1206 *pnum = num_used;
1207 return 1;
1208}
1209
1210/*
Eric Blakedc61cd32017-10-11 22:47:14 -05001211 * Compares two buffers sector by sector. Returns 0 if the first
1212 * sector of each buffer matches, non-zero otherwise.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001213 *
Eric Blakedc61cd32017-10-11 22:47:14 -05001214 * pnum is set to the sector-aligned size of the buffer prefix that
1215 * has the same matching status as the first sector.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001216 */
Eric Blakedc61cd32017-10-11 22:47:14 -05001217static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2,
1218 int64_t bytes, int64_t *pnum)
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001219{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001220 bool res;
Eric Blakedc61cd32017-10-11 22:47:14 -05001221 int64_t i = MIN(bytes, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001222
Eric Blakedc61cd32017-10-11 22:47:14 -05001223 assert(bytes > 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001224
Eric Blakedc61cd32017-10-11 22:47:14 -05001225 res = !!memcmp(buf1, buf2, i);
1226 while (i < bytes) {
1227 int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001228
Eric Blakedc61cd32017-10-11 22:47:14 -05001229 if (!!memcmp(buf1 + i, buf2 + i, len) != res) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001230 break;
1231 }
Eric Blakedc61cd32017-10-11 22:47:14 -05001232 i += len;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001233 }
1234
1235 *pnum = i;
1236 return res;
1237}
1238
Stefano Garzarella97ede572019-05-08 12:43:24 +02001239#define IO_BUF_SIZE (2 * MiB)
bellardea2384d2004-08-01 21:59:26 +00001240
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001241/*
1242 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1243 *
Eric Blake0608e402017-10-11 22:47:12 -05001244 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1245 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1246 * failure), and 4 on error (the exit status for read errors), after emitting
1247 * an error message.
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001248 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001249 * @param blk: BlockBackend for the image
Eric Blakec41508e2017-10-11 22:47:13 -05001250 * @param offset: Starting offset to check
1251 * @param bytes: Number of bytes to check
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001252 * @param filename: Name of disk file we are checking (logging purpose)
1253 * @param buffer: Allocated buffer for storing read data
1254 * @param quiet: Flag for quiet mode
1255 */
Eric Blakec41508e2017-10-11 22:47:13 -05001256static int check_empty_sectors(BlockBackend *blk, int64_t offset,
1257 int64_t bytes, const char *filename,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001258 uint8_t *buffer, bool quiet)
1259{
Eric Blakedebb38a2017-10-11 22:47:11 -05001260 int ret = 0;
1261 int64_t idx;
1262
Eric Blakec41508e2017-10-11 22:47:13 -05001263 ret = blk_pread(blk, offset, buffer, bytes);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001264 if (ret < 0) {
1265 error_report("Error while reading offset %" PRId64 " of %s: %s",
Eric Blakec41508e2017-10-11 22:47:13 -05001266 offset, filename, strerror(-ret));
Eric Blake0608e402017-10-11 22:47:12 -05001267 return 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001268 }
Eric Blakec41508e2017-10-11 22:47:13 -05001269 idx = find_nonzero(buffer, bytes);
Eric Blakedebb38a2017-10-11 22:47:11 -05001270 if (idx >= 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001271 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blakec41508e2017-10-11 22:47:13 -05001272 offset + idx);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001273 return 1;
1274 }
1275
1276 return 0;
1277}
1278
1279/*
1280 * Compares two images. Exit codes:
1281 *
1282 * 0 - Images are identical
1283 * 1 - Images differ
1284 * >1 - Error occurred
1285 */
1286static int img_compare(int argc, char **argv)
1287{
Max Reitz40055952014-07-22 22:58:42 +02001288 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001289 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001290 BlockDriverState *bs1, *bs2;
Eric Blake033d9fc2017-10-11 22:47:16 -05001291 int64_t total_size1, total_size2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001292 uint8_t *buf1 = NULL, *buf2 = NULL;
Eric Blake31826642017-10-11 22:47:08 -05001293 int64_t pnum1, pnum2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001294 int allocated1, allocated2;
1295 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1296 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001297 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001298 bool writethrough;
Eric Blake033d9fc2017-10-11 22:47:16 -05001299 int64_t total_size;
1300 int64_t offset = 0;
1301 int64_t chunk;
Eric Blakedc61cd32017-10-11 22:47:14 -05001302 int c;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001303 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001304 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001305 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001306
Max Reitz40055952014-07-22 22:58:42 +02001307 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001308 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001309 static const struct option long_options[] = {
1310 {"help", no_argument, 0, 'h'},
1311 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001312 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001313 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001314 {0, 0, 0, 0}
1315 };
Fam Zheng335e9932017-05-03 00:35:39 +08001316 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001317 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001318 if (c == -1) {
1319 break;
1320 }
1321 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001322 case ':':
1323 missing_argument(argv[optind - 1]);
1324 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001325 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001326 unrecognized_option(argv[optind - 1]);
1327 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001328 case 'h':
1329 help();
1330 break;
1331 case 'f':
1332 fmt1 = optarg;
1333 break;
1334 case 'F':
1335 fmt2 = optarg;
1336 break;
Max Reitz40055952014-07-22 22:58:42 +02001337 case 'T':
1338 cache = optarg;
1339 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001340 case 'p':
1341 progress = true;
1342 break;
1343 case 'q':
1344 quiet = true;
1345 break;
1346 case 's':
1347 strict = true;
1348 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001349 case 'U':
1350 force_share = true;
1351 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001352 case OPTION_OBJECT: {
1353 QemuOpts *opts;
1354 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1355 optarg, true);
1356 if (!opts) {
1357 ret = 2;
1358 goto out4;
1359 }
1360 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001361 case OPTION_IMAGE_OPTS:
1362 image_opts = true;
1363 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001364 }
1365 }
1366
1367 /* Progress is not shown in Quiet mode */
1368 if (quiet) {
1369 progress = false;
1370 }
1371
1372
Kevin Wolffc11eb22013-08-05 10:53:04 +02001373 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001374 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001375 }
1376 filename1 = argv[optind++];
1377 filename2 = argv[optind++];
1378
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001379 if (qemu_opts_foreach(&qemu_object_opts,
1380 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02001381 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001382 ret = 2;
1383 goto out4;
1384 }
1385
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001386 /* Initialize before goto out */
1387 qemu_progress_init(progress, 2.0);
1388
Kevin Wolfce099542016-03-15 13:01:04 +01001389 flags = 0;
1390 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001391 if (ret < 0) {
1392 error_report("Invalid source cache option: %s", cache);
1393 ret = 2;
1394 goto out3;
1395 }
1396
Fam Zheng335e9932017-05-03 00:35:39 +08001397 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1398 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001399 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001400 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001401 goto out3;
1402 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001403
Fam Zheng335e9932017-05-03 00:35:39 +08001404 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1405 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001406 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001407 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001408 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001409 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001410 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001411 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001412
Max Reitzf1d3cd72015-02-05 13:58:18 -05001413 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1414 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
Eric Blake033d9fc2017-10-11 22:47:16 -05001415 total_size1 = blk_getlength(blk1);
1416 if (total_size1 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001417 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001418 filename1, strerror(-total_size1));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001419 ret = 4;
1420 goto out;
1421 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001422 total_size2 = blk_getlength(blk2);
1423 if (total_size2 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001424 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001425 filename2, strerror(-total_size2));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001426 ret = 4;
1427 goto out;
1428 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001429 total_size = MIN(total_size1, total_size2);
1430 progress_base = MAX(total_size1, total_size2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001431
1432 qemu_progress_print(0, 100);
1433
Eric Blake033d9fc2017-10-11 22:47:16 -05001434 if (strict && total_size1 != total_size2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001435 ret = 1;
1436 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1437 goto out;
1438 }
1439
Eric Blake033d9fc2017-10-11 22:47:16 -05001440 while (offset < total_size) {
Eric Blake31826642017-10-11 22:47:08 -05001441 int status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001442
Eric Blake033d9fc2017-10-11 22:47:16 -05001443 status1 = bdrv_block_status_above(bs1, NULL, offset,
1444 total_size1 - offset, &pnum1, NULL,
1445 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001446 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001447 ret = 3;
1448 error_report("Sector allocation test failed for %s", filename1);
1449 goto out;
1450 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001451 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001452
Eric Blake033d9fc2017-10-11 22:47:16 -05001453 status2 = bdrv_block_status_above(bs2, NULL, offset,
1454 total_size2 - offset, &pnum2, NULL,
1455 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001456 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001457 ret = 3;
1458 error_report("Sector allocation test failed for %s", filename2);
1459 goto out;
1460 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001461 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
Eric Blake7daddc62017-10-11 22:47:09 -05001462
1463 assert(pnum1 && pnum2);
Eric Blake033d9fc2017-10-11 22:47:16 -05001464 chunk = MIN(pnum1, pnum2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001465
Fam Zheng25ad8e62016-01-13 16:37:41 +08001466 if (strict) {
Eric Blake31826642017-10-11 22:47:08 -05001467 if (status1 != status2) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001468 ret = 1;
1469 qprintf(quiet, "Strict mode: Offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001470 " block status mismatch!\n", offset);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001471 goto out;
1472 }
1473 }
1474 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
Eric Blake7daddc62017-10-11 22:47:09 -05001475 /* nothing to do */
Fam Zheng25ad8e62016-01-13 16:37:41 +08001476 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001477 if (allocated1) {
Eric Blakedc61cd32017-10-11 22:47:14 -05001478 int64_t pnum;
1479
Eric Blake033d9fc2017-10-11 22:47:16 -05001480 chunk = MIN(chunk, IO_BUF_SIZE);
1481 ret = blk_pread(blk1, offset, buf1, chunk);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001482 if (ret < 0) {
1483 error_report("Error while reading offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001484 " of %s: %s",
1485 offset, filename1, strerror(-ret));
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001486 ret = 4;
1487 goto out;
1488 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001489 ret = blk_pread(blk2, offset, buf2, chunk);
1490 if (ret < 0) {
1491 error_report("Error while reading offset %" PRId64
1492 " of %s: %s",
1493 offset, filename2, strerror(-ret));
1494 ret = 4;
1495 goto out;
1496 }
1497 ret = compare_buffers(buf1, buf2, chunk, &pnum);
1498 if (ret || pnum != chunk) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001499 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blake033d9fc2017-10-11 22:47:16 -05001500 offset + (ret ? 0 : pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001501 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001502 goto out;
1503 }
1504 }
1505 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001506 chunk = MIN(chunk, IO_BUF_SIZE);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001507 if (allocated1) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001508 ret = check_empty_sectors(blk1, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001509 filename1, buf1, quiet);
1510 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001511 ret = check_empty_sectors(blk2, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001512 filename2, buf1, quiet);
1513 }
1514 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001515 goto out;
1516 }
1517 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001518 offset += chunk;
1519 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001520 }
1521
Eric Blake033d9fc2017-10-11 22:47:16 -05001522 if (total_size1 != total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001523 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001524 const char *filename_over;
1525
1526 qprintf(quiet, "Warning: Image size mismatch!\n");
Eric Blake033d9fc2017-10-11 22:47:16 -05001527 if (total_size1 > total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001528 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001529 filename_over = filename1;
1530 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001531 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001532 filename_over = filename2;
1533 }
1534
Eric Blake033d9fc2017-10-11 22:47:16 -05001535 while (offset < progress_base) {
1536 ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset,
1537 progress_base - offset, &chunk,
1538 NULL, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001539 if (ret < 0) {
1540 ret = 3;
1541 error_report("Sector allocation test failed for %s",
1542 filename_over);
1543 goto out;
1544
1545 }
Eric Blake391cb1a2017-10-11 22:47:10 -05001546 if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001547 chunk = MIN(chunk, IO_BUF_SIZE);
1548 ret = check_empty_sectors(blk_over, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001549 filename_over, buf1, quiet);
1550 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001551 goto out;
1552 }
1553 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001554 offset += chunk;
1555 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001556 }
1557 }
1558
1559 qprintf(quiet, "Images are identical.\n");
1560 ret = 0;
1561
1562out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001563 qemu_vfree(buf1);
1564 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001565 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001566out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001567 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001568out3:
1569 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001570out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001571 return ret;
1572}
1573
Kevin Wolf690c7302015-03-19 13:33:32 +01001574enum ImgConvertBlockStatus {
1575 BLK_DATA,
1576 BLK_ZERO,
1577 BLK_BACKING_FILE,
1578};
1579
Peter Lieven2d9187b2017-02-28 13:40:07 +01001580#define MAX_COROUTINES 16
1581
Kevin Wolf690c7302015-03-19 13:33:32 +01001582typedef struct ImgConvertState {
1583 BlockBackend **src;
1584 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001585 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001586 int64_t total_sectors;
1587 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001588 int64_t allocated_done;
1589 int64_t sector_num;
1590 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001591 enum ImgConvertBlockStatus status;
1592 int64_t sector_next_status;
1593 BlockBackend *target;
1594 bool has_zero_init;
1595 bool compressed;
Max Reitz351c8ef2018-05-01 18:57:49 +02001596 bool unallocated_blocks_are_zero;
Max Reitz4d7c4872019-07-24 19:12:29 +02001597 bool target_is_new;
Kevin Wolf690c7302015-03-19 13:33:32 +01001598 bool target_has_backing;
Max Reitz351c8ef2018-05-01 18:57:49 +02001599 int64_t target_backing_sectors; /* negative if unknown */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001600 bool wr_in_order;
Fam Zhengee5306d2018-06-01 17:26:48 +08001601 bool copy_range;
Max Reitz8eaac022019-05-07 22:35:03 +02001602 bool salvage;
Max Reitz3d96cb92019-05-07 22:35:02 +02001603 bool quiet;
Kevin Wolf690c7302015-03-19 13:33:32 +01001604 int min_sparse;
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001605 int alignment;
Kevin Wolf690c7302015-03-19 13:33:32 +01001606 size_t cluster_sectors;
1607 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001608 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001609 int running_coroutines;
1610 Coroutine *co[MAX_COROUTINES];
1611 int64_t wait_sector_num[MAX_COROUTINES];
1612 CoMutex lock;
1613 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001614} ImgConvertState;
1615
Peter Lieven2d9187b2017-02-28 13:40:07 +01001616static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1617 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001618{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001619 *src_cur = 0;
1620 *src_cur_offset = 0;
1621 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1622 *src_cur_offset += s->src_sectors[*src_cur];
1623 (*src_cur)++;
1624 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001625 }
1626}
1627
1628static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1629{
Eric Blake31826642017-10-11 22:47:08 -05001630 int64_t src_cur_offset;
1631 int ret, n, src_cur;
Max Reitz351c8ef2018-05-01 18:57:49 +02001632 bool post_backing_zero = false;
Kevin Wolf690c7302015-03-19 13:33:32 +01001633
Peter Lieven2d9187b2017-02-28 13:40:07 +01001634 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001635
1636 assert(s->total_sectors > sector_num);
1637 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1638
Max Reitz351c8ef2018-05-01 18:57:49 +02001639 if (s->target_backing_sectors >= 0) {
1640 if (sector_num >= s->target_backing_sectors) {
1641 post_backing_zero = s->unallocated_blocks_are_zero;
1642 } else if (sector_num + n > s->target_backing_sectors) {
1643 /* Split requests around target_backing_sectors (because
1644 * starting from there, zeros are handled differently) */
1645 n = s->target_backing_sectors - sector_num;
1646 }
1647 }
1648
Kevin Wolf690c7302015-03-19 13:33:32 +01001649 if (s->sector_next_status <= sector_num) {
Max Reitz8eaac022019-05-07 22:35:03 +02001650 uint64_t offset = (sector_num - src_cur_offset) * BDRV_SECTOR_SIZE;
1651 int64_t count;
Eric Blake31826642017-10-11 22:47:08 -05001652
Max Reitz8eaac022019-05-07 22:35:03 +02001653 do {
1654 count = n * BDRV_SECTOR_SIZE;
Eric Blake237d78f2017-10-11 22:47:03 -05001655
Max Reitz8eaac022019-05-07 22:35:03 +02001656 if (s->target_has_backing) {
1657 ret = bdrv_block_status(blk_bs(s->src[src_cur]), offset,
1658 count, &count, NULL, NULL);
1659 } else {
1660 ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL,
1661 offset, count, &count, NULL,
1662 NULL);
1663 }
1664
1665 if (ret < 0) {
1666 if (s->salvage) {
1667 if (n == 1) {
1668 if (!s->quiet) {
1669 warn_report("error while reading block status at "
1670 "offset %" PRIu64 ": %s", offset,
1671 strerror(-ret));
1672 }
1673 /* Just try to read the data, then */
1674 ret = BDRV_BLOCK_DATA;
1675 count = BDRV_SECTOR_SIZE;
1676 } else {
1677 /* Retry on a shorter range */
1678 n = DIV_ROUND_UP(n, 4);
1679 }
1680 } else {
1681 error_report("error while reading block status at offset "
1682 "%" PRIu64 ": %s", offset, strerror(-ret));
1683 return ret;
1684 }
1685 }
1686 } while (ret < 0);
1687
Eric Blake31826642017-10-11 22:47:08 -05001688 n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
Kevin Wolf690c7302015-03-19 13:33:32 +01001689
1690 if (ret & BDRV_BLOCK_ZERO) {
Max Reitz351c8ef2018-05-01 18:57:49 +02001691 s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO;
Kevin Wolf690c7302015-03-19 13:33:32 +01001692 } else if (ret & BDRV_BLOCK_DATA) {
1693 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001694 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001695 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001696 }
1697
1698 s->sector_next_status = sector_num + n;
1699 }
1700
1701 n = MIN(n, s->sector_next_status - sector_num);
1702 if (s->status == BLK_DATA) {
1703 n = MIN(n, s->buf_sectors);
1704 }
1705
1706 /* We need to write complete clusters for compressed images, so if an
1707 * unallocated area is shorter than that, we must consider the whole
1708 * cluster allocated. */
1709 if (s->compressed) {
1710 if (n < s->cluster_sectors) {
1711 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1712 s->status = BLK_DATA;
1713 } else {
1714 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1715 }
1716 }
1717
1718 return n;
1719}
1720
Peter Lieven2d9187b2017-02-28 13:40:07 +01001721static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1722 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001723{
Max Reitz8eaac022019-05-07 22:35:03 +02001724 uint64_t single_read_until = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001725 int n, ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001726
Kevin Wolf690c7302015-03-19 13:33:32 +01001727 assert(nb_sectors <= s->buf_sectors);
1728 while (nb_sectors > 0) {
1729 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001730 int src_cur;
1731 int64_t bs_sectors, src_cur_offset;
Max Reitz8eaac022019-05-07 22:35:03 +02001732 uint64_t offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001733
1734 /* In the case of compression with multiple source files, we can get a
1735 * nb_sectors that spreads into the next part. So we must be able to
1736 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001737 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1738 blk = s->src[src_cur];
1739 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001740
Max Reitz8eaac022019-05-07 22:35:03 +02001741 offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001742
Max Reitz8eaac022019-05-07 22:35:03 +02001743 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1744 if (single_read_until > offset) {
1745 n = 1;
1746 }
1747
1748 ret = blk_co_pread(blk, offset, n << BDRV_SECTOR_BITS, buf, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001749 if (ret < 0) {
Max Reitz8eaac022019-05-07 22:35:03 +02001750 if (s->salvage) {
1751 if (n > 1) {
1752 single_read_until = offset + (n << BDRV_SECTOR_BITS);
1753 continue;
1754 } else {
1755 if (!s->quiet) {
1756 warn_report("error while reading offset %" PRIu64
1757 ": %s", offset, strerror(-ret));
1758 }
1759 memset(buf, 0, BDRV_SECTOR_SIZE);
1760 }
1761 } else {
1762 return ret;
1763 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001764 }
1765
1766 sector_num += n;
1767 nb_sectors -= n;
1768 buf += n * BDRV_SECTOR_SIZE;
1769 }
1770
1771 return 0;
1772}
1773
Peter Lieven2d9187b2017-02-28 13:40:07 +01001774
1775static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1776 int nb_sectors, uint8_t *buf,
1777 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001778{
1779 int ret;
1780
1781 while (nb_sectors > 0) {
1782 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001783 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1784
Peter Lieven2d9187b2017-02-28 13:40:07 +01001785 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001786 case BLK_BACKING_FILE:
1787 /* If we have a backing file, leave clusters unallocated that are
1788 * unallocated in the source image, so that the backing file is
1789 * visible at the respective offset. */
1790 assert(s->target_has_backing);
1791 break;
1792
1793 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001794 /* If we're told to keep the target fully allocated (-S 0) or there
1795 * is real non-zero data, we must write it. Otherwise we can treat
1796 * it as zero sectors.
1797 * Compressed clusters need to be written as a whole, so in that
1798 * case we can only save the write if the buffer is completely
1799 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001800 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001801 (!s->compressed &&
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001802 is_allocated_sectors_min(buf, n, &n, s->min_sparse,
1803 sector_num, s->alignment)) ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001804 (s->compressed &&
1805 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001806 {
Vladimir Sementsov-Ogievskiy265a7e52019-04-22 17:58:38 +03001807 ret = blk_co_pwrite(s->target, sector_num << BDRV_SECTOR_BITS,
1808 n << BDRV_SECTOR_BITS, buf, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001809 if (ret < 0) {
1810 return ret;
1811 }
1812 break;
1813 }
1814 /* fall-through */
1815
1816 case BLK_ZERO:
1817 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001818 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001819 break;
1820 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001821 ret = blk_co_pwrite_zeroes(s->target,
1822 sector_num << BDRV_SECTOR_BITS,
Nir Soffera3d6ae22019-03-24 02:20:12 +02001823 n << BDRV_SECTOR_BITS,
1824 BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001825 if (ret < 0) {
1826 return ret;
1827 }
1828 break;
1829 }
1830
1831 sector_num += n;
1832 nb_sectors -= n;
1833 buf += n * BDRV_SECTOR_SIZE;
1834 }
1835
1836 return 0;
1837}
1838
Fam Zhengee5306d2018-06-01 17:26:48 +08001839static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num,
1840 int nb_sectors)
1841{
1842 int n, ret;
1843
1844 while (nb_sectors > 0) {
1845 BlockBackend *blk;
1846 int src_cur;
1847 int64_t bs_sectors, src_cur_offset;
1848 int64_t offset;
1849
1850 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1851 offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS;
1852 blk = s->src[src_cur];
1853 bs_sectors = s->src_sectors[src_cur];
1854
1855 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1856
1857 ret = blk_co_copy_range(blk, offset, s->target,
1858 sector_num << BDRV_SECTOR_BITS,
Vladimir Sementsov-Ogievskiy67b51fb2018-07-09 19:37:17 +03001859 n << BDRV_SECTOR_BITS, 0, 0);
Fam Zhengee5306d2018-06-01 17:26:48 +08001860 if (ret < 0) {
1861 return ret;
1862 }
1863
1864 sector_num += n;
1865 nb_sectors -= n;
1866 }
1867 return 0;
1868}
1869
Peter Lieven2d9187b2017-02-28 13:40:07 +01001870static void coroutine_fn convert_co_do_copy(void *opaque)
1871{
1872 ImgConvertState *s = opaque;
1873 uint8_t *buf = NULL;
1874 int ret, i;
1875 int index = -1;
1876
1877 for (i = 0; i < s->num_coroutines; i++) {
1878 if (s->co[i] == qemu_coroutine_self()) {
1879 index = i;
1880 break;
1881 }
1882 }
1883 assert(index >= 0);
1884
1885 s->running_coroutines++;
1886 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1887
1888 while (1) {
1889 int n;
1890 int64_t sector_num;
1891 enum ImgConvertBlockStatus status;
Fam Zhengee5306d2018-06-01 17:26:48 +08001892 bool copy_range;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001893
1894 qemu_co_mutex_lock(&s->lock);
1895 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1896 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001897 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001898 }
1899 n = convert_iteration_sectors(s, s->sector_num);
1900 if (n < 0) {
1901 qemu_co_mutex_unlock(&s->lock);
1902 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001903 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001904 }
1905 /* save current sector and allocation status to local variables */
1906 sector_num = s->sector_num;
1907 status = s->status;
1908 if (!s->min_sparse && s->status == BLK_ZERO) {
1909 n = MIN(n, s->buf_sectors);
1910 }
1911 /* increment global sector counter so that other coroutines can
1912 * already continue reading beyond this request */
1913 s->sector_num += n;
1914 qemu_co_mutex_unlock(&s->lock);
1915
1916 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1917 s->allocated_done += n;
1918 qemu_progress_print(100.0 * s->allocated_done /
1919 s->allocated_sectors, 0);
1920 }
1921
Fam Zhengee5306d2018-06-01 17:26:48 +08001922retry:
1923 copy_range = s->copy_range && s->status == BLK_DATA;
1924 if (status == BLK_DATA && !copy_range) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001925 ret = convert_co_read(s, sector_num, n, buf);
1926 if (ret < 0) {
Eric Blake39f77cb2020-04-02 08:57:17 -05001927 error_report("error while reading at byte %lld: %s",
1928 sector_num * BDRV_SECTOR_SIZE, strerror(-ret));
Peter Lieven2d9187b2017-02-28 13:40:07 +01001929 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001930 }
1931 } else if (!s->min_sparse && status == BLK_ZERO) {
1932 status = BLK_DATA;
1933 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1934 }
1935
1936 if (s->wr_in_order) {
1937 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03001938 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001939 s->wait_sector_num[index] = sector_num;
1940 qemu_coroutine_yield();
1941 }
1942 s->wait_sector_num[index] = -1;
1943 }
1944
Anton Nefedovb91127e2017-04-26 11:33:15 +03001945 if (s->ret == -EINPROGRESS) {
Fam Zhengee5306d2018-06-01 17:26:48 +08001946 if (copy_range) {
1947 ret = convert_co_copy_range(s, sector_num, n);
1948 if (ret) {
1949 s->copy_range = false;
1950 goto retry;
1951 }
1952 } else {
1953 ret = convert_co_write(s, sector_num, n, buf, status);
1954 }
Anton Nefedovb91127e2017-04-26 11:33:15 +03001955 if (ret < 0) {
Eric Blake39f77cb2020-04-02 08:57:17 -05001956 error_report("error while writing at byte %lld: %s",
1957 sector_num * BDRV_SECTOR_SIZE, strerror(-ret));
Anton Nefedovb91127e2017-04-26 11:33:15 +03001958 s->ret = ret;
1959 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001960 }
1961
1962 if (s->wr_in_order) {
1963 /* reenter the coroutine that might have waited
1964 * for this write to complete */
1965 s->wr_offs = sector_num + n;
1966 for (i = 0; i < s->num_coroutines; i++) {
1967 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1968 /*
1969 * A -> B -> A cannot occur because A has
1970 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1971 * B will never enter A during this time window.
1972 */
1973 qemu_coroutine_enter(s->co[i]);
1974 break;
1975 }
1976 }
1977 }
1978 }
1979
Peter Lieven2d9187b2017-02-28 13:40:07 +01001980 qemu_vfree(buf);
1981 s->co[index] = NULL;
1982 s->running_coroutines--;
1983 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1984 /* the convert job finished successfully */
1985 s->ret = 0;
1986 }
1987}
1988
Kevin Wolf690c7302015-03-19 13:33:32 +01001989static int convert_do_copy(ImgConvertState *s)
1990{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001991 int ret, i, n;
1992 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001993
1994 /* Check whether we have zero initialisation or can get it efficiently */
David Edmondson168468f2020-02-05 11:02:48 +00001995 if (!s->has_zero_init && s->target_is_new && s->min_sparse &&
1996 !s->target_has_backing) {
Max Reitz4d7c4872019-07-24 19:12:29 +02001997 s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target));
Max Reitz4d7c4872019-07-24 19:12:29 +02001998 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001999
2000 if (!s->has_zero_init && !s->target_has_backing &&
2001 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
2002 {
Kevin Wolfc9fdcf22019-03-22 13:49:28 +01002003 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK);
Kevin Wolf690c7302015-03-19 13:33:32 +01002004 if (ret == 0) {
2005 s->has_zero_init = true;
2006 }
2007 }
2008
2009 /* Allocate buffer for copied data. For compressed images, only one cluster
2010 * can be copied at a time. */
2011 if (s->compressed) {
2012 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
2013 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01002014 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01002015 }
2016 s->buf_sectors = s->cluster_sectors;
2017 }
Kevin Wolf690c7302015-03-19 13:33:32 +01002018
Kevin Wolf690c7302015-03-19 13:33:32 +01002019 while (sector_num < s->total_sectors) {
2020 n = convert_iteration_sectors(s, sector_num);
2021 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002022 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01002023 }
Max Reitzaad15de2016-03-24 23:33:57 +01002024 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
2025 {
Kevin Wolf690c7302015-03-19 13:33:32 +01002026 s->allocated_sectors += n;
2027 }
2028 sector_num += n;
2029 }
2030
2031 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01002032 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002033 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01002034
Peter Lieven2d9187b2017-02-28 13:40:07 +01002035 qemu_co_mutex_init(&s->lock);
2036 for (i = 0; i < s->num_coroutines; i++) {
2037 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
2038 s->wait_sector_num[i] = -1;
2039 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01002040 }
2041
Anton Nefedovb91127e2017-04-26 11:33:15 +03002042 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002043 main_loop_wait(false);
2044 }
2045
2046 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01002047 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03002048 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01002049 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002050 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01002051 }
2052 }
2053
Peter Lieven2d9187b2017-02-28 13:40:07 +01002054 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01002055}
2056
Peter Lieven6360ab22018-07-13 09:15:39 +02002057#define MAX_BUF_SECTORS 32768
2058
bellardea2384d2004-08-01 21:59:26 +00002059static int img_convert(int argc, char **argv)
2060{
Peter Lieven9fd77f92017-04-21 11:11:55 +02002061 int c, bs_i, flags, src_flags = 0;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002062 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002063 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
2064 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002065 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00002066 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002067 BlockDriverState *out_bs;
2068 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08002069 QemuOptsList *create_opts = NULL;
Daniel P. Berrangé8d65a3c2018-08-14 13:39:47 +01002070 QDict *open_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002071 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02002072 Error *local_err = NULL;
Max Reitz3d96cb92019-05-07 22:35:02 +02002073 bool writethrough, src_writethrough, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002074 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002075 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08002076 bool force_share = false;
Fam Zhenge11ce122018-07-27 11:34:01 +08002077 bool explict_min_sparse = false;
bellardea2384d2004-08-01 21:59:26 +00002078
Peter Lieven9fd77f92017-04-21 11:11:55 +02002079 ImgConvertState s = (ImgConvertState) {
2080 /* Need at least 4k of zeros for sparse detection */
2081 .min_sparse = 8,
Fam Zhenge11ce122018-07-27 11:34:01 +08002082 .copy_range = false,
Peter Lieven9fd77f92017-04-21 11:11:55 +02002083 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
2084 .wr_in_order = true,
2085 .num_coroutines = 8,
2086 };
2087
bellardea2384d2004-08-01 21:59:26 +00002088 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002089 static const struct option long_options[] = {
2090 {"help", no_argument, 0, 'h'},
2091 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002092 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002093 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002094 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Max Reitz8eaac022019-05-07 22:35:03 +02002095 {"salvage", no_argument, 0, OPTION_SALVAGE},
David Edmondson168468f2020-02-05 11:02:48 +00002096 {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002097 {0, 0, 0, 0}
2098 };
Fam Zhenge11ce122018-07-27 11:34:01 +08002099 c = getopt_long(argc, argv, ":hf:O:B:Cco:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002100 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002101 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002102 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002103 }
bellardea2384d2004-08-01 21:59:26 +00002104 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002105 case ':':
2106 missing_argument(argv[optind - 1]);
2107 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002108 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002109 unrecognized_option(argv[optind - 1]);
2110 break;
bellardea2384d2004-08-01 21:59:26 +00002111 case 'h':
2112 help();
2113 break;
2114 case 'f':
2115 fmt = optarg;
2116 break;
2117 case 'O':
2118 out_fmt = optarg;
2119 break;
thsf58c7b32008-06-05 21:53:49 +00002120 case 'B':
2121 out_baseimg = optarg;
2122 break;
Fam Zhenge11ce122018-07-27 11:34:01 +08002123 case 'C':
2124 s.copy_range = true;
2125 break;
bellardea2384d2004-08-01 21:59:26 +00002126 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002127 s.compressed = true;
bellardea2384d2004-08-01 21:59:26 +00002128 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002129 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01002130 if (!is_valid_option_list(optarg)) {
2131 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002132 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01002133 }
2134 if (!options) {
2135 options = g_strdup(optarg);
2136 } else {
2137 char *old_options = options;
2138 options = g_strdup_printf("%s,%s", options, optarg);
2139 g_free(old_options);
2140 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002141 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08002142 case 'l':
2143 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01002144 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2145 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08002146 if (!sn_opts) {
2147 error_report("Failed in parsing snapshot param '%s'",
2148 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002149 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002150 }
2151 } else {
2152 snapshot_name = optarg;
2153 }
2154 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002155 case 'S':
2156 {
2157 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002158
2159 sval = cvtnum(optarg);
Nir Soffer1bbbf322019-08-27 21:59:12 +03002160 if (sval < 0 || !QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) ||
Peter Lieven6360ab22018-07-13 09:15:39 +02002161 sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) {
2162 error_report("Invalid buffer size for sparse output specified. "
2163 "Valid sizes are multiples of %llu up to %llu. Select "
2164 "0 to disable sparse detection (fully allocates output).",
2165 BDRV_SECTOR_SIZE, MAX_BUF_SECTORS * BDRV_SECTOR_SIZE);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002166 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002167 }
2168
Peter Lieven9fd77f92017-04-21 11:11:55 +02002169 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Fam Zhenge11ce122018-07-27 11:34:01 +08002170 explict_min_sparse = true;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002171 break;
2172 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002173 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002174 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002175 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002176 case 't':
2177 cache = optarg;
2178 break;
Max Reitz40055952014-07-22 22:58:42 +02002179 case 'T':
2180 src_cache = optarg;
2181 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002182 case 'q':
Max Reitz3d96cb92019-05-07 22:35:02 +02002183 s.quiet = true;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002184 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002185 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002186 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002187 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002188 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002189 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2190 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002191 error_report("Invalid number of coroutines. Allowed number of"
2192 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002193 goto fail_getopt;
2194 }
2195 break;
2196 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002197 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002198 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002199 case 'U':
2200 force_share = true;
2201 break;
Max Reitz3258b912017-04-26 15:46:47 +02002202 case OPTION_OBJECT: {
2203 QemuOpts *object_opts;
2204 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2205 optarg, true);
2206 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002207 goto fail_getopt;
2208 }
2209 break;
Max Reitz3258b912017-04-26 15:46:47 +02002210 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002211 case OPTION_IMAGE_OPTS:
2212 image_opts = true;
2213 break;
Max Reitz8eaac022019-05-07 22:35:03 +02002214 case OPTION_SALVAGE:
2215 s.salvage = true;
2216 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002217 case OPTION_TARGET_IMAGE_OPTS:
2218 tgt_image_opts = true;
2219 break;
David Edmondson168468f2020-02-05 11:02:48 +00002220 case OPTION_TARGET_IS_ZERO:
2221 /*
2222 * The user asserting that the target is blank has the
2223 * same effect as the target driver supporting zero
2224 * initialisation.
2225 */
2226 s.has_zero_init = true;
2227 break;
bellardea2384d2004-08-01 21:59:26 +00002228 }
2229 }
ths3b46e622007-09-17 08:09:54 +00002230
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002231 if (!out_fmt && !tgt_image_opts) {
2232 out_fmt = "raw";
2233 }
2234
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002235 if (qemu_opts_foreach(&qemu_object_opts,
2236 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02002237 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002238 goto fail_getopt;
2239 }
2240
Fam Zhenge11ce122018-07-27 11:34:01 +08002241 if (s.compressed && s.copy_range) {
2242 error_report("Cannot enable copy offloading when -c is used");
2243 goto fail_getopt;
2244 }
2245
2246 if (explict_min_sparse && s.copy_range) {
2247 error_report("Cannot enable copy offloading when -S is used");
2248 goto fail_getopt;
2249 }
2250
Max Reitz8eaac022019-05-07 22:35:03 +02002251 if (s.copy_range && s.salvage) {
2252 error_report("Cannot use copy offloading in salvaging mode");
2253 goto fail_getopt;
2254 }
2255
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002256 if (tgt_image_opts && !skip_create) {
2257 error_report("--target-image-opts requires use of -n flag");
2258 goto fail_getopt;
2259 }
2260
Kevin Wolfffd8e8f2019-08-09 11:09:21 +02002261 if (skip_create && options) {
2262 warn_report("-o has no effect when skipping image creation");
2263 warn_report("This will become an error in future QEMU versions.");
2264 }
2265
David Edmondson168468f2020-02-05 11:02:48 +00002266 if (s.has_zero_init && !skip_create) {
2267 error_report("--target-is-zero requires use of -n flag");
2268 goto fail_getopt;
2269 }
2270
Peter Lieven9fd77f92017-04-21 11:11:55 +02002271 s.src_num = argc - optind - 1;
2272 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2273
2274 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002275 if (out_fmt) {
2276 ret = print_block_option_help(out_filename, out_fmt);
2277 goto fail_getopt;
2278 } else {
2279 error_report("Option help requires a format be specified");
2280 goto fail_getopt;
2281 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002282 }
2283
2284 if (s.src_num < 1) {
2285 error_report("Must specify image file name");
2286 goto fail_getopt;
2287 }
2288
2289
Peter Lieven9fd77f92017-04-21 11:11:55 +02002290 /* ret is still -EINVAL until here */
2291 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2292 if (ret < 0) {
2293 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002294 goto fail_getopt;
2295 }
2296
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002297 /* Initialize before goto out */
Max Reitz3d96cb92019-05-07 22:35:02 +02002298 if (s.quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002299 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002300 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002301 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002302 qemu_progress_print(0, 100);
2303
Peter Lieven9fd77f92017-04-21 11:11:55 +02002304 s.src = g_new0(BlockBackend *, s.src_num);
2305 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002306
Peter Lieven9fd77f92017-04-21 11:11:55 +02002307 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2308 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Max Reitz3d96cb92019-05-07 22:35:02 +02002309 fmt, src_flags, src_writethrough, s.quiet,
Fam Zheng335e9932017-05-03 00:35:39 +08002310 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002311 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002312 ret = -1;
2313 goto out;
2314 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002315 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2316 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002317 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002318 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002319 ret = -1;
2320 goto out;
2321 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002322 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002323 }
bellardea2384d2004-08-01 21:59:26 +00002324
Wenchao Xiaef806542013-12-04 17:10:57 +08002325 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002326 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002327 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2328 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2329 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002330 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002331 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002332 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002333 ret = -1;
2334 goto out;
2335 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002336
Peter Lieven9fd77f92017-04-21 11:11:55 +02002337 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2338 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002339 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002340 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002341 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002342 ret = -1;
2343 goto out;
edison51ef6722010-09-21 19:58:41 -07002344 }
2345
Max Reitz2e024cd2015-02-11 09:58:46 -05002346 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002347 /* Find driver and parse its options */
2348 drv = bdrv_find_format(out_fmt);
2349 if (!drv) {
2350 error_report("Unknown file format '%s'", out_fmt);
2351 ret = -1;
2352 goto out;
2353 }
2354
2355 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2356 if (!proto_drv) {
2357 error_report_err(local_err);
2358 ret = -1;
2359 goto out;
2360 }
2361
Max Reitz2e024cd2015-02-11 09:58:46 -05002362 if (!drv->create_opts) {
2363 error_report("Format driver '%s' does not support image creation",
2364 drv->format_name);
2365 ret = -1;
2366 goto out;
2367 }
Max Reitzf75613c2014-12-02 18:32:46 +01002368
Max Reitz2e024cd2015-02-11 09:58:46 -05002369 if (!proto_drv->create_opts) {
2370 error_report("Protocol driver '%s' does not support image creation",
2371 proto_drv->format_name);
2372 ret = -1;
2373 goto out;
2374 }
Max Reitzf75613c2014-12-02 18:32:46 +01002375
Max Reitz2e024cd2015-02-11 09:58:46 -05002376 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2377 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002378
Max Reitz2e024cd2015-02-11 09:58:46 -05002379 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002380 if (options) {
2381 qemu_opts_do_parse(opts, options, NULL, &local_err);
2382 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002383 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002384 ret = -1;
2385 goto out;
2386 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002387 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002388
Peter Lieven9fd77f92017-04-21 11:11:55 +02002389 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002390 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002391 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2392 if (ret < 0) {
2393 goto out;
2394 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002395 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002396
Kevin Wolfa18953f2010-10-14 15:46:04 +02002397 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002398 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002399 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002400 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002401 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002402 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002403
David Edmondson168468f2020-02-05 11:02:48 +00002404 if (s.has_zero_init && s.target_has_backing) {
2405 error_report("Cannot use --target-is-zero when the destination "
2406 "image has a backing file");
2407 goto out;
2408 }
2409
Max Reitz48758a82017-04-26 15:46:48 +02002410 if (s.src_num > 1 && out_baseimg) {
2411 error_report("Having a backing file for the target makes no sense when "
2412 "concatenating multiple input images");
2413 ret = -1;
2414 goto out;
2415 }
2416
Kevin Wolfefa84d42009-05-18 16:42:12 +02002417 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002418 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002419 bool encryption =
2420 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002421 const char *encryptfmt =
2422 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
Chunyan Liu83d05212014-06-05 17:20:51 +08002423 const char *preallocation =
2424 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002425
Vladimir Sementsov-Ogievskiyac850bf2019-06-04 19:15:06 +03002426 if (drv && !block_driver_can_compress(drv)) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002427 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002428 ret = -1;
2429 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002430 }
2431
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002432 if (encryption || encryptfmt) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002433 error_report("Compression and encryption not supported at "
2434 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002435 ret = -1;
2436 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002437 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002438
Chunyan Liu83d05212014-06-05 17:20:51 +08002439 if (preallocation
2440 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002441 {
2442 error_report("Compression and preallocation not supported at "
2443 "the same time");
2444 ret = -1;
2445 goto out;
2446 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002447 }
2448
Daniel P. Berrangé8d65a3c2018-08-14 13:39:47 +01002449 /*
2450 * The later open call will need any decryption secrets, and
2451 * bdrv_create() will purge "opts", so extract them now before
2452 * they are lost.
2453 */
2454 if (!skip_create) {
2455 open_opts = qdict_new();
2456 qemu_opt_foreach(opts, img_add_key_secrets, open_opts, &error_abort);
2457 }
2458
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002459 if (!skip_create) {
2460 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002461 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002462 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002463 error_reportf_err(local_err, "%s: error while converting %s: ",
2464 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002465 goto out;
bellardea2384d2004-08-01 21:59:26 +00002466 }
2467 }
ths3b46e622007-09-17 08:09:54 +00002468
Max Reitz4d7c4872019-07-24 19:12:29 +02002469 s.target_is_new = !skip_create;
2470
Peter Lieven9fd77f92017-04-21 11:11:55 +02002471 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002472 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002473 if (ret < 0) {
2474 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002475 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002476 }
2477
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002478 if (skip_create) {
2479 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
Max Reitz3d96cb92019-05-07 22:35:02 +02002480 flags, writethrough, s.quiet, false);
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002481 } else {
2482 /* TODO ultimately we should allow --target-image-opts
2483 * to be used even when -n is not given.
2484 * That has to wait for bdrv_create to be improved
2485 * to allow filenames in option syntax
2486 */
Daniel P. Berrangé8d65a3c2018-08-14 13:39:47 +01002487 s.target = img_open_file(out_filename, open_opts, out_fmt,
Max Reitz3d96cb92019-05-07 22:35:02 +02002488 flags, writethrough, s.quiet, false);
Daniel P. Berrangé8d65a3c2018-08-14 13:39:47 +01002489 open_opts = NULL; /* blk_new_open will have freed it */
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002490 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002491 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002492 ret = -1;
2493 goto out;
2494 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002495 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002496
Vladimir Sementsov-Ogievskiyac850bf2019-06-04 19:15:06 +03002497 if (s.compressed && !block_driver_can_compress(out_bs->drv)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002498 error_report("Compression not supported for this file format");
2499 ret = -1;
2500 goto out;
2501 }
2502
Eric Blake5def6b82016-06-23 16:37:19 -06002503 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lieven6360ab22018-07-13 09:15:39 +02002504 * or discard_alignment of the out_bs is greater. Limit to
2505 * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */
2506 s.buf_sectors = MIN(MAX_BUF_SECTORS,
Peter Lieven9fd77f92017-04-21 11:11:55 +02002507 MAX(s.buf_sectors,
2508 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2509 out_bs->bl.pdiscard_alignment >>
2510 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002511
Peter Lieven8dcd3c92018-07-12 15:00:10 +02002512 /* try to align the write requests to the destination to avoid unnecessary
2513 * RMW cycles. */
2514 s.alignment = MAX(pow2floor(s.min_sparse),
2515 DIV_ROUND_UP(out_bs->bl.request_alignment,
2516 BDRV_SECTOR_SIZE));
2517 assert(is_power_of_2(s.alignment));
2518
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002519 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002520 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002521 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002522 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002523 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002524 ret = -1;
2525 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002526 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002527 error_report("output file is smaller than input file");
2528 ret = -1;
2529 goto out;
2530 }
2531 }
2532
Max Reitzc69291e2020-01-21 16:59:14 +01002533 if (s.target_has_backing && s.target_is_new) {
Max Reitz351c8ef2018-05-01 18:57:49 +02002534 /* Errors are treated as "backing length unknown" (which means
2535 * s.target_backing_sectors has to be negative, which it will
2536 * be automatically). The backing file length is used only
2537 * for optimizations, so such a case is not fatal. */
2538 s.target_backing_sectors = bdrv_nb_sectors(out_bs->backing->bs);
2539 } else {
2540 s.target_backing_sectors = -1;
2541 }
2542
Peter Lieven24f833c2013-11-27 11:07:07 +01002543 ret = bdrv_get_info(out_bs, &bdi);
2544 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002545 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002546 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002547 goto out;
2548 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002549 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002550 s.compressed = s.compressed || bdi.needs_compressed_writes;
2551 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Max Reitz351c8ef2018-05-01 18:57:49 +02002552 s.unallocated_blocks_are_zero = bdi.unallocated_blocks_are_zero;
Peter Lieven24f833c2013-11-27 11:07:07 +01002553 }
2554
Peter Lieven9fd77f92017-04-21 11:11:55 +02002555 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002556out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002557 if (!ret) {
2558 qemu_progress_print(100, 0);
2559 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002560 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002561 qemu_opts_del(opts);
2562 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002563 qemu_opts_del(sn_opts);
Daniel P. Berrangé8d65a3c2018-08-14 13:39:47 +01002564 qobject_unref(open_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002565 blk_unref(s.target);
2566 if (s.src) {
2567 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2568 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002569 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002570 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002571 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002572 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002573fail_getopt:
2574 g_free(options);
2575
Peter Lieven9fd77f92017-04-21 11:11:55 +02002576 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002577}
2578
bellard57d1a2b2004-08-03 21:15:11 +00002579
bellardfaea38e2006-08-05 21:31:00 +00002580static void dump_snapshots(BlockDriverState *bs)
2581{
2582 QEMUSnapshotInfo *sn_tab, *sn;
2583 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002584
2585 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2586 if (nb_sns <= 0)
2587 return;
2588 printf("Snapshot list:\n");
Markus Armbrustere1ce7d72019-04-17 21:17:55 +02002589 bdrv_snapshot_dump(NULL);
Wenchao Xia5b917042013-05-25 11:09:45 +08002590 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002591 for(i = 0; i < nb_sns; i++) {
2592 sn = &sn_tab[i];
Markus Armbrustere1ce7d72019-04-17 21:17:55 +02002593 bdrv_snapshot_dump(sn);
Wenchao Xia5b917042013-05-25 11:09:45 +08002594 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002595 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002596 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002597}
2598
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002599static void dump_json_image_info_list(ImageInfoList *list)
2600{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002601 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002602 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002603 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002604
2605 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2606 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002607 str = qobject_to_json_pretty(obj);
2608 assert(str != NULL);
2609 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002610 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002611 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002612 qobject_unref(str);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002613}
2614
Benoît Canetc054b3f2012-09-05 13:09:02 +02002615static void dump_json_image_info(ImageInfo *info)
2616{
Benoît Canetc054b3f2012-09-05 13:09:02 +02002617 QString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002618 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002619 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002620
2621 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2622 visit_complete(v, &obj);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002623 str = qobject_to_json_pretty(obj);
2624 assert(str != NULL);
2625 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002626 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002627 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002628 qobject_unref(str);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002629}
2630
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002631static void dump_human_image_info_list(ImageInfoList *list)
2632{
2633 ImageInfoList *elem;
2634 bool delim = false;
2635
2636 for (elem = list; elem; elem = elem->next) {
2637 if (delim) {
2638 printf("\n");
2639 }
2640 delim = true;
2641
Markus Armbrustere1ce7d72019-04-17 21:17:55 +02002642 bdrv_image_info_dump(elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002643 }
2644}
2645
2646static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2647{
2648 return strcmp(a, b) == 0;
2649}
2650
2651/**
2652 * Open an image file chain and return an ImageInfoList
2653 *
2654 * @filename: topmost image filename
2655 * @fmt: topmost image format (may be NULL to autodetect)
2656 * @chain: true - enumerate entire backing file chain
2657 * false - only topmost image file
2658 *
2659 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2660 * image file. If there was an error a message will have been printed to
2661 * stderr.
2662 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002663static ImageInfoList *collect_image_info_list(bool image_opts,
2664 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002665 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002666 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002667{
2668 ImageInfoList *head = NULL;
2669 ImageInfoList **last = &head;
2670 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002671 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002672
2673 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2674
2675 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002676 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002677 BlockDriverState *bs;
2678 ImageInfo *info;
2679 ImageInfoList *elem;
2680
2681 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2682 error_report("Backing file '%s' creates an infinite loop.",
2683 filename);
2684 goto err;
2685 }
2686 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2687
Max Reitzefaa7c42016-03-16 19:54:38 +01002688 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002689 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2690 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002691 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002692 goto err;
2693 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002694 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002695
Wenchao Xia43526ec2013-06-06 12:27:58 +08002696 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002697 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002698 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002699 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002700 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002701 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002702
2703 elem = g_new0(ImageInfoList, 1);
2704 elem->value = info;
2705 *last = elem;
2706 last = &elem->next;
2707
Markus Armbruster26f54e92014-10-07 13:59:04 +02002708 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002709
Stefan Hajnoczi0da7d132019-12-05 13:46:46 +00002710 /* Clear parameters that only apply to the topmost image */
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002711 filename = fmt = NULL;
Stefan Hajnoczi0da7d132019-12-05 13:46:46 +00002712 image_opts = false;
2713
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002714 if (chain) {
2715 if (info->has_full_backing_filename) {
2716 filename = info->full_backing_filename;
2717 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002718 error_report("Could not determine absolute backing filename,"
2719 " but backing filename '%s' present",
2720 info->backing_filename);
2721 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002722 }
2723 if (info->has_backing_filename_format) {
2724 fmt = info->backing_filename_format;
2725 }
2726 }
2727 }
2728 g_hash_table_destroy(filenames);
2729 return head;
2730
2731err:
2732 qapi_free_ImageInfoList(head);
2733 g_hash_table_destroy(filenames);
2734 return NULL;
2735}
2736
Benoît Canetc054b3f2012-09-05 13:09:02 +02002737static int img_info(int argc, char **argv)
2738{
2739 int c;
2740 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002741 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002742 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002743 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002744 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002745 bool force_share = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002746
bellardea2384d2004-08-01 21:59:26 +00002747 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002748 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002749 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002750 int option_index = 0;
2751 static const struct option long_options[] = {
2752 {"help", no_argument, 0, 'h'},
2753 {"format", required_argument, 0, 'f'},
2754 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002755 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002756 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002757 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002758 {"force-share", no_argument, 0, 'U'},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002759 {0, 0, 0, 0}
2760 };
Fam Zheng335e9932017-05-03 00:35:39 +08002761 c = getopt_long(argc, argv, ":f:hU",
Benoît Canetc054b3f2012-09-05 13:09:02 +02002762 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002763 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002764 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002765 }
bellardea2384d2004-08-01 21:59:26 +00002766 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002767 case ':':
2768 missing_argument(argv[optind - 1]);
2769 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002770 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002771 unrecognized_option(argv[optind - 1]);
2772 break;
bellardea2384d2004-08-01 21:59:26 +00002773 case 'h':
2774 help();
2775 break;
2776 case 'f':
2777 fmt = optarg;
2778 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002779 case 'U':
2780 force_share = true;
2781 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002782 case OPTION_OUTPUT:
2783 output = optarg;
2784 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002785 case OPTION_BACKING_CHAIN:
2786 chain = true;
2787 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002788 case OPTION_OBJECT: {
2789 QemuOpts *opts;
2790 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2791 optarg, true);
2792 if (!opts) {
2793 return 1;
2794 }
2795 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002796 case OPTION_IMAGE_OPTS:
2797 image_opts = true;
2798 break;
bellardea2384d2004-08-01 21:59:26 +00002799 }
2800 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002801 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002802 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002803 }
bellardea2384d2004-08-01 21:59:26 +00002804 filename = argv[optind++];
2805
Benoît Canetc054b3f2012-09-05 13:09:02 +02002806 if (output && !strcmp(output, "json")) {
2807 output_format = OFORMAT_JSON;
2808 } else if (output && !strcmp(output, "human")) {
2809 output_format = OFORMAT_HUMAN;
2810 } else if (output) {
2811 error_report("--output must be used with human or json as argument.");
2812 return 1;
2813 }
2814
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002815 if (qemu_opts_foreach(&qemu_object_opts,
2816 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02002817 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002818 return 1;
2819 }
2820
Fam Zheng335e9932017-05-03 00:35:39 +08002821 list = collect_image_info_list(image_opts, filename, fmt, chain,
2822 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002823 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002824 return 1;
2825 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002826
Benoît Canetc054b3f2012-09-05 13:09:02 +02002827 switch (output_format) {
2828 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002829 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002830 break;
2831 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002832 if (chain) {
2833 dump_json_image_info_list(list);
2834 } else {
2835 dump_json_image_info(list->value);
2836 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002837 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002838 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002839
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002840 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002841 return 0;
2842}
2843
Eric Blake30065d12019-03-26 13:40:43 -05002844static int dump_map_entry(OutputFormat output_format, MapEntry *e,
2845 MapEntry *next)
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002846{
2847 switch (output_format) {
2848 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002849 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002850 error_report("File contains external, encrypted or compressed clusters.");
Eric Blake30065d12019-03-26 13:40:43 -05002851 return -1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002852 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002853 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002854 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002855 e->start, e->length,
2856 e->has_offset ? e->offset : 0,
2857 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002858 }
2859 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2860 * Modify the flags here to allow more coalescing.
2861 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002862 if (next && (!next->data || next->zero)) {
2863 next->data = false;
2864 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002865 }
2866 break;
2867 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002868 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2869 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002870 (e->start == 0 ? "[" : ",\n"),
2871 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002872 e->zero ? "true" : "false",
2873 e->data ? "true" : "false");
2874 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002875 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002876 }
2877 putchar('}');
2878
2879 if (!next) {
2880 printf("]\n");
2881 }
2882 break;
2883 }
Eric Blake30065d12019-03-26 13:40:43 -05002884 return 0;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002885}
2886
Eric Blake5e344dd2017-10-11 22:47:02 -05002887static int get_block_status(BlockDriverState *bs, int64_t offset,
2888 int64_t bytes, MapEntry *e)
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002889{
Eric Blake237d78f2017-10-11 22:47:03 -05002890 int ret;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002891 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002892 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002893 bool has_offset;
Eric Blake237d78f2017-10-11 22:47:03 -05002894 int64_t map;
Max Reitzf30c66b2019-02-01 20:29:05 +01002895 char *filename = NULL;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002896
2897 /* As an optimization, we could cache the current range of unallocated
2898 * clusters in each file of the chain, and avoid querying the same
2899 * range repeatedly.
2900 */
2901
2902 depth = 0;
2903 for (;;) {
Eric Blake237d78f2017-10-11 22:47:03 -05002904 ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002905 if (ret < 0) {
2906 return ret;
2907 }
Eric Blake237d78f2017-10-11 22:47:03 -05002908 assert(bytes);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002909 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2910 break;
2911 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002912 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002913 if (bs == NULL) {
2914 ret = 0;
2915 break;
2916 }
2917
2918 depth++;
2919 }
2920
John Snow28756452016-02-05 13:12:33 -05002921 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2922
Max Reitzf30c66b2019-02-01 20:29:05 +01002923 if (file && has_offset) {
2924 bdrv_refresh_filename(file);
2925 filename = file->filename;
2926 }
2927
John Snow28756452016-02-05 13:12:33 -05002928 *e = (MapEntry) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002929 .start = offset,
Eric Blake237d78f2017-10-11 22:47:03 -05002930 .length = bytes,
John Snow28756452016-02-05 13:12:33 -05002931 .data = !!(ret & BDRV_BLOCK_DATA),
2932 .zero = !!(ret & BDRV_BLOCK_ZERO),
Eric Blake237d78f2017-10-11 22:47:03 -05002933 .offset = map,
John Snow28756452016-02-05 13:12:33 -05002934 .has_offset = has_offset,
2935 .depth = depth,
Max Reitzf30c66b2019-02-01 20:29:05 +01002936 .has_filename = filename,
2937 .filename = filename,
John Snow28756452016-02-05 13:12:33 -05002938 };
2939
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002940 return 0;
2941}
2942
Fam Zheng16b0d552016-01-26 11:59:02 +08002943static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2944{
2945 if (curr->length == 0) {
2946 return false;
2947 }
2948 if (curr->zero != next->zero ||
2949 curr->data != next->data ||
2950 curr->depth != next->depth ||
2951 curr->has_filename != next->has_filename ||
2952 curr->has_offset != next->has_offset) {
2953 return false;
2954 }
2955 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2956 return false;
2957 }
2958 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2959 return false;
2960 }
2961 return true;
2962}
2963
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002964static int img_map(int argc, char **argv)
2965{
2966 int c;
2967 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002968 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002969 BlockDriverState *bs;
2970 const char *filename, *fmt, *output;
2971 int64_t length;
2972 MapEntry curr = { .length = 0 }, next;
2973 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002974 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002975 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002976
2977 fmt = NULL;
2978 output = NULL;
2979 for (;;) {
2980 int option_index = 0;
2981 static const struct option long_options[] = {
2982 {"help", no_argument, 0, 'h'},
2983 {"format", required_argument, 0, 'f'},
2984 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002985 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002986 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002987 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002988 {0, 0, 0, 0}
2989 };
Fam Zheng335e9932017-05-03 00:35:39 +08002990 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002991 long_options, &option_index);
2992 if (c == -1) {
2993 break;
2994 }
2995 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002996 case ':':
2997 missing_argument(argv[optind - 1]);
2998 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002999 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003000 unrecognized_option(argv[optind - 1]);
3001 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003002 case 'h':
3003 help();
3004 break;
3005 case 'f':
3006 fmt = optarg;
3007 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003008 case 'U':
3009 force_share = true;
3010 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003011 case OPTION_OUTPUT:
3012 output = optarg;
3013 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003014 case OPTION_OBJECT: {
3015 QemuOpts *opts;
3016 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3017 optarg, true);
3018 if (!opts) {
3019 return 1;
3020 }
3021 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003022 case OPTION_IMAGE_OPTS:
3023 image_opts = true;
3024 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003025 }
3026 }
Fam Zhengac1307a2014-04-22 13:36:11 +08003027 if (optind != argc - 1) {
3028 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003029 }
Fam Zhengac1307a2014-04-22 13:36:11 +08003030 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003031
3032 if (output && !strcmp(output, "json")) {
3033 output_format = OFORMAT_JSON;
3034 } else if (output && !strcmp(output, "human")) {
3035 output_format = OFORMAT_HUMAN;
3036 } else if (output) {
3037 error_report("--output must be used with human or json as argument.");
3038 return 1;
3039 }
3040
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003041 if (qemu_opts_foreach(&qemu_object_opts,
3042 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02003043 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003044 return 1;
3045 }
3046
Fam Zheng335e9932017-05-03 00:35:39 +08003047 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003048 if (!blk) {
3049 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003050 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003051 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003052
3053 if (output_format == OFORMAT_HUMAN) {
3054 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
3055 }
3056
Max Reitzf1d3cd72015-02-05 13:58:18 -05003057 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003058 while (curr.start + curr.length < length) {
Eric Blake5e344dd2017-10-11 22:47:02 -05003059 int64_t offset = curr.start + curr.length;
3060 int64_t n;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003061
3062 /* Probe up to 1 GiB at a time. */
Stefano Garzarella97ede572019-05-08 12:43:24 +02003063 n = MIN(1 * GiB, length - offset);
Eric Blake5e344dd2017-10-11 22:47:02 -05003064 ret = get_block_status(bs, offset, n, &next);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003065
3066 if (ret < 0) {
3067 error_report("Could not read file metadata: %s", strerror(-ret));
3068 goto out;
3069 }
3070
Fam Zheng16b0d552016-01-26 11:59:02 +08003071 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003072 curr.length += next.length;
3073 continue;
3074 }
3075
3076 if (curr.length > 0) {
Eric Blake30065d12019-03-26 13:40:43 -05003077 ret = dump_map_entry(output_format, &curr, &next);
3078 if (ret < 0) {
3079 goto out;
3080 }
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003081 }
3082 curr = next;
3083 }
3084
Eric Blake30065d12019-03-26 13:40:43 -05003085 ret = dump_map_entry(output_format, &curr, NULL);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003086
3087out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003088 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003089 return ret < 0;
3090}
3091
aliguorif7b4a942009-01-07 17:40:15 +00003092#define SNAPSHOT_LIST 1
3093#define SNAPSHOT_CREATE 2
3094#define SNAPSHOT_APPLY 3
3095#define SNAPSHOT_DELETE 4
3096
Stuart Brady153859b2009-06-07 00:42:17 +01003097static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00003098{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003099 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00003100 BlockDriverState *bs;
3101 QEMUSnapshotInfo sn;
3102 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003103 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00003104 int action = 0;
3105 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003106 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003107 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003108 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08003109 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00003110
Kevin Wolfce099542016-03-15 13:01:04 +01003111 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00003112 /* Parse commandline parameters */
3113 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003114 static const struct option long_options[] = {
3115 {"help", no_argument, 0, 'h'},
3116 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003117 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003118 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003119 {0, 0, 0, 0}
3120 };
Fam Zheng335e9932017-05-03 00:35:39 +08003121 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003122 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003123 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00003124 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003125 }
aliguorif7b4a942009-01-07 17:40:15 +00003126 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003127 case ':':
3128 missing_argument(argv[optind - 1]);
3129 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003130 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003131 unrecognized_option(argv[optind - 1]);
3132 break;
aliguorif7b4a942009-01-07 17:40:15 +00003133 case 'h':
3134 help();
Stuart Brady153859b2009-06-07 00:42:17 +01003135 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003136 case 'l':
3137 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003138 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01003139 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003140 }
3141 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02003142 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00003143 break;
3144 case 'a':
3145 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003146 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01003147 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003148 }
3149 action = SNAPSHOT_APPLY;
3150 snapshot_name = optarg;
3151 break;
3152 case 'c':
3153 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003154 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01003155 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003156 }
3157 action = SNAPSHOT_CREATE;
3158 snapshot_name = optarg;
3159 break;
3160 case 'd':
3161 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003162 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01003163 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003164 }
3165 action = SNAPSHOT_DELETE;
3166 snapshot_name = optarg;
3167 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003168 case 'q':
3169 quiet = true;
3170 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003171 case 'U':
3172 force_share = true;
3173 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003174 case OPTION_OBJECT: {
3175 QemuOpts *opts;
3176 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3177 optarg, true);
3178 if (!opts) {
3179 return 1;
3180 }
3181 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003182 case OPTION_IMAGE_OPTS:
3183 image_opts = true;
3184 break;
aliguorif7b4a942009-01-07 17:40:15 +00003185 }
3186 }
3187
Kevin Wolffc11eb22013-08-05 10:53:04 +02003188 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003189 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003190 }
aliguorif7b4a942009-01-07 17:40:15 +00003191 filename = argv[optind++];
3192
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003193 if (qemu_opts_foreach(&qemu_object_opts,
3194 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02003195 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003196 return 1;
3197 }
3198
aliguorif7b4a942009-01-07 17:40:15 +00003199 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08003200 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
3201 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003202 if (!blk) {
3203 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003204 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003205 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00003206
3207 /* Perform the requested action */
3208 switch(action) {
3209 case SNAPSHOT_LIST:
3210 dump_snapshots(bs);
3211 break;
3212
3213 case SNAPSHOT_CREATE:
3214 memset(&sn, 0, sizeof(sn));
3215 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
3216
3217 qemu_gettimeofday(&tv);
3218 sn.date_sec = tv.tv_sec;
3219 sn.date_nsec = tv.tv_usec * 1000;
3220
3221 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003222 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003223 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003224 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003225 }
aliguorif7b4a942009-01-07 17:40:15 +00003226 break;
3227
3228 case SNAPSHOT_APPLY:
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01003229 ret = bdrv_snapshot_goto(bs, snapshot_name, &err);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003230 if (ret) {
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01003231 error_reportf_err(err, "Could not apply snapshot '%s': ",
3232 snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003233 }
aliguorif7b4a942009-01-07 17:40:15 +00003234 break;
3235
3236 case SNAPSHOT_DELETE:
Daniel Henrique Barboza8c040932018-11-07 11:09:59 -02003237 ret = bdrv_snapshot_find(bs, &sn, snapshot_name);
3238 if (ret < 0) {
3239 error_report("Could not delete snapshot '%s': snapshot not "
3240 "found", snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003241 ret = 1;
Daniel Henrique Barboza8c040932018-11-07 11:09:59 -02003242 } else {
3243 ret = bdrv_snapshot_delete(bs, sn.id_str, sn.name, &err);
3244 if (ret < 0) {
3245 error_reportf_err(err, "Could not delete snapshot '%s': ",
3246 snapshot_name);
3247 ret = 1;
3248 }
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003249 }
aliguorif7b4a942009-01-07 17:40:15 +00003250 break;
3251 }
3252
3253 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003254 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003255 if (ret) {
3256 return 1;
3257 }
Stuart Brady153859b2009-06-07 00:42:17 +01003258 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003259}
3260
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003261static int img_rebase(int argc, char **argv)
3262{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003263 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003264 uint8_t *buf_old = NULL;
3265 uint8_t *buf_new = NULL;
Sam Eiderman863cc782019-05-23 19:33:36 +03003266 BlockDriverState *bs = NULL, *prefix_chain_bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003267 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003268 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3269 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003270 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003271 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003272 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003273 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003274 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003275 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003276 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003277
3278 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003279 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003280 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003281 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003282 out_baseimg = NULL;
3283 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003284 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003285 static const struct option long_options[] = {
3286 {"help", no_argument, 0, 'h'},
3287 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003288 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003289 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003290 {0, 0, 0, 0}
3291 };
Fam Zheng335e9932017-05-03 00:35:39 +08003292 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003293 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003294 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003295 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003296 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003297 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003298 case ':':
3299 missing_argument(argv[optind - 1]);
3300 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003301 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003302 unrecognized_option(argv[optind - 1]);
3303 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003304 case 'h':
3305 help();
3306 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003307 case 'f':
3308 fmt = optarg;
3309 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003310 case 'F':
3311 out_basefmt = optarg;
3312 break;
3313 case 'b':
3314 out_baseimg = optarg;
3315 break;
3316 case 'u':
3317 unsafe = 1;
3318 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003319 case 'p':
3320 progress = 1;
3321 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003322 case 't':
3323 cache = optarg;
3324 break;
Max Reitz40055952014-07-22 22:58:42 +02003325 case 'T':
3326 src_cache = optarg;
3327 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003328 case 'q':
3329 quiet = true;
3330 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003331 case OPTION_OBJECT: {
3332 QemuOpts *opts;
3333 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3334 optarg, true);
3335 if (!opts) {
3336 return 1;
3337 }
3338 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003339 case OPTION_IMAGE_OPTS:
3340 image_opts = true;
3341 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003342 case 'U':
3343 force_share = true;
3344 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003345 }
3346 }
3347
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003348 if (quiet) {
3349 progress = 0;
3350 }
3351
Fam Zhengac1307a2014-04-22 13:36:11 +08003352 if (optind != argc - 1) {
3353 error_exit("Expecting one image file name");
3354 }
3355 if (!unsafe && !out_baseimg) {
3356 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003357 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003358 filename = argv[optind++];
3359
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003360 if (qemu_opts_foreach(&qemu_object_opts,
3361 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02003362 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003363 return 1;
3364 }
3365
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003366 qemu_progress_init(progress, 2.0);
3367 qemu_progress_print(0, 100);
3368
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003369 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003370 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003371 if (ret < 0) {
3372 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003373 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003374 }
3375
Kevin Wolfce099542016-03-15 13:01:04 +01003376 src_flags = 0;
3377 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003378 if (ret < 0) {
3379 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003380 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003381 }
3382
Kevin Wolfce099542016-03-15 13:01:04 +01003383 /* The source files are opened read-only, don't care about WCE */
3384 assert((src_flags & BDRV_O_RDWR) == 0);
3385 (void) src_writethrough;
3386
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003387 /*
3388 * Open the images.
3389 *
3390 * Ignore the old backing file for unsafe rebase in case we want to correct
3391 * the reference to a renamed or moved backing file.
3392 */
Fam Zheng335e9932017-05-03 00:35:39 +08003393 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3394 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003395 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003396 ret = -1;
3397 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003398 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003399 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003400
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003401 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003402 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003403 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003404 ret = -1;
3405 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003406 }
3407 }
3408
3409 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003410 if (!unsafe) {
Max Reitz644483d2015-02-05 13:58:17 -05003411 QDict *options = NULL;
Sam Eiderman4ebe0612019-05-23 19:33:35 +03003412 BlockDriverState *base_bs = backing_bs(bs);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003413
Sam Eiderman4ebe0612019-05-23 19:33:35 +03003414 if (base_bs) {
Kevin Wolfd861ab32019-04-25 14:25:10 +02003415 blk_old_backing = blk_new(qemu_get_aio_context(),
3416 BLK_PERM_CONSISTENT_READ,
Sam Eiderman4ebe0612019-05-23 19:33:35 +03003417 BLK_PERM_ALL);
3418 ret = blk_insert_bs(blk_old_backing, base_bs,
3419 &local_err);
3420 if (ret < 0) {
Max Reitz35ddd932019-05-09 19:52:35 +02003421 error_reportf_err(local_err,
Sam Eiderman4ebe0612019-05-23 19:33:35 +03003422 "Could not reuse old backing file '%s': ",
3423 base_bs->filename);
Max Reitz35ddd932019-05-09 19:52:35 +02003424 goto out;
3425 }
3426 } else {
3427 blk_old_backing = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003428 }
Max Reitz644483d2015-02-05 13:58:17 -05003429
Alex Bligha6166732012-10-16 13:46:18 +01003430 if (out_baseimg[0]) {
Max Reitzd16699b2018-05-09 20:20:01 +02003431 const char *overlay_filename;
3432 char *out_real_path;
3433
Fam Zheng335e9932017-05-03 00:35:39 +08003434 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003435 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003436 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003437 }
3438 if (force_share) {
3439 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003440 }
3441
Max Reitzf30c66b2019-02-01 20:29:05 +01003442 bdrv_refresh_filename(bs);
Max Reitzd16699b2018-05-09 20:20:01 +02003443 overlay_filename = bs->exact_filename[0] ? bs->exact_filename
3444 : bs->filename;
Max Reitz645ae7d2019-02-01 20:29:14 +01003445 out_real_path =
3446 bdrv_get_full_backing_filename_from_filename(overlay_filename,
3447 out_baseimg,
3448 &local_err);
Max Reitzd16699b2018-05-09 20:20:01 +02003449 if (local_err) {
Max Reitzf22356d2019-05-28 21:53:38 +02003450 qobject_unref(options);
Max Reitzd16699b2018-05-09 20:20:01 +02003451 error_reportf_err(local_err,
3452 "Could not resolve backing filename: ");
3453 ret = -1;
Max Reitzd16699b2018-05-09 20:20:01 +02003454 goto out;
3455 }
3456
Sam Eiderman863cc782019-05-23 19:33:36 +03003457 /*
3458 * Find out whether we rebase an image on top of a previous image
3459 * in its chain.
3460 */
3461 prefix_chain_bs = bdrv_find_backing_image(bs, out_real_path);
Sam Eiderman330c7292019-05-23 19:33:37 +03003462 if (prefix_chain_bs) {
Max Reitzf22356d2019-05-28 21:53:38 +02003463 qobject_unref(options);
Sam Eiderman330c7292019-05-23 19:33:37 +03003464 g_free(out_real_path);
Max Reitzf22356d2019-05-28 21:53:38 +02003465
Kevin Wolfd861ab32019-04-25 14:25:10 +02003466 blk_new_backing = blk_new(qemu_get_aio_context(),
3467 BLK_PERM_CONSISTENT_READ,
Sam Eiderman330c7292019-05-23 19:33:37 +03003468 BLK_PERM_ALL);
3469 ret = blk_insert_bs(blk_new_backing, prefix_chain_bs,
3470 &local_err);
3471 if (ret < 0) {
3472 error_reportf_err(local_err,
3473 "Could not reuse backing file '%s': ",
3474 out_baseimg);
3475 goto out;
3476 }
3477 } else {
3478 blk_new_backing = blk_new_open(out_real_path, NULL,
3479 options, src_flags, &local_err);
3480 g_free(out_real_path);
3481 if (!blk_new_backing) {
3482 error_reportf_err(local_err,
3483 "Could not open new backing file '%s': ",
3484 out_baseimg);
3485 ret = -1;
3486 goto out;
3487 }
Alex Bligha6166732012-10-16 13:46:18 +01003488 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003489 }
3490 }
3491
3492 /*
3493 * Check each unallocated cluster in the COW file. If it is unallocated,
3494 * accesses go to the backing file. We must therefore compare this cluster
3495 * in the old and new backing file, and if they differ we need to copy it
3496 * from the old backing file into the COW file.
3497 *
3498 * If qemu-img crashes during this step, no harm is done. The content of
3499 * the image is the same as the original one at any time.
3500 */
3501 if (!unsafe) {
Eric Blake41536282017-10-11 22:47:15 -05003502 int64_t size;
Max Reitz35ddd932019-05-09 19:52:35 +02003503 int64_t old_backing_size = 0;
Eric Blake41536282017-10-11 22:47:15 -05003504 int64_t new_backing_size = 0;
3505 uint64_t offset;
3506 int64_t n;
Kevin Wolf1f710492012-10-12 14:29:18 +02003507 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003508
Max Reitzf1d3cd72015-02-05 13:58:18 -05003509 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3510 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003511
Eric Blake41536282017-10-11 22:47:15 -05003512 size = blk_getlength(blk);
3513 if (size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003514 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003515 filename, strerror(-size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003516 ret = -1;
3517 goto out;
3518 }
Max Reitz35ddd932019-05-09 19:52:35 +02003519 if (blk_old_backing) {
3520 old_backing_size = blk_getlength(blk_old_backing);
3521 if (old_backing_size < 0) {
3522 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003523
Max Reitz35ddd932019-05-09 19:52:35 +02003524 bdrv_get_backing_filename(bs, backing_name,
3525 sizeof(backing_name));
3526 error_report("Could not get size of '%s': %s",
3527 backing_name, strerror(-old_backing_size));
3528 ret = -1;
3529 goto out;
3530 }
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003531 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003532 if (blk_new_backing) {
Eric Blake41536282017-10-11 22:47:15 -05003533 new_backing_size = blk_getlength(blk_new_backing);
3534 if (new_backing_size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003535 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003536 out_baseimg, strerror(-new_backing_size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003537 ret = -1;
3538 goto out;
3539 }
Alex Bligha6166732012-10-16 13:46:18 +01003540 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003541
Eric Blake41536282017-10-11 22:47:15 -05003542 if (size != 0) {
3543 local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE));
Kevin Wolf1f710492012-10-12 14:29:18 +02003544 }
3545
Eric Blake41536282017-10-11 22:47:15 -05003546 for (offset = 0; offset < size; offset += n) {
Max Reitz1c6e8772019-05-09 19:52:36 +02003547 bool buf_old_is_zero = false;
3548
Eric Blake41536282017-10-11 22:47:15 -05003549 /* How many bytes can we handle with the next read? */
3550 n = MIN(IO_BUF_SIZE, size - offset);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003551
3552 /* If the cluster is allocated, we don't need to take action */
Eric Blake41536282017-10-11 22:47:15 -05003553 ret = bdrv_is_allocated(bs, offset, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003554 if (ret < 0) {
3555 error_report("error while reading image metadata: %s",
3556 strerror(-ret));
3557 goto out;
3558 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003559 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003560 continue;
3561 }
3562
Sam Eiderman863cc782019-05-23 19:33:36 +03003563 if (prefix_chain_bs) {
3564 /*
3565 * If cluster wasn't changed since prefix_chain, we don't need
3566 * to take action
3567 */
3568 ret = bdrv_is_allocated_above(backing_bs(bs), prefix_chain_bs,
Andrey Shinkevich170d3bd2019-05-29 20:56:14 +03003569 false, offset, n, &n);
Sam Eiderman863cc782019-05-23 19:33:36 +03003570 if (ret < 0) {
3571 error_report("error while reading image metadata: %s",
3572 strerror(-ret));
3573 goto out;
3574 }
3575 if (!ret) {
3576 continue;
3577 }
3578 }
3579
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003580 /*
3581 * Read old and new backing file and take into consideration that
3582 * backing files may be smaller than the COW image.
3583 */
Eric Blake41536282017-10-11 22:47:15 -05003584 if (offset >= old_backing_size) {
3585 memset(buf_old, 0, n);
Max Reitz1c6e8772019-05-09 19:52:36 +02003586 buf_old_is_zero = true;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003587 } else {
Eric Blake41536282017-10-11 22:47:15 -05003588 if (offset + n > old_backing_size) {
3589 n = old_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003590 }
3591
Eric Blake41536282017-10-11 22:47:15 -05003592 ret = blk_pread(blk_old_backing, offset, buf_old, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003593 if (ret < 0) {
3594 error_report("error while reading from old backing file");
3595 goto out;
3596 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003597 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003598
Eric Blake41536282017-10-11 22:47:15 -05003599 if (offset >= new_backing_size || !blk_new_backing) {
3600 memset(buf_new, 0, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003601 } else {
Eric Blake41536282017-10-11 22:47:15 -05003602 if (offset + n > new_backing_size) {
3603 n = new_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003604 }
3605
Eric Blake41536282017-10-11 22:47:15 -05003606 ret = blk_pread(blk_new_backing, offset, buf_new, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003607 if (ret < 0) {
3608 error_report("error while reading from new backing file");
3609 goto out;
3610 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003611 }
3612
3613 /* If they differ, we need to write to the COW file */
3614 uint64_t written = 0;
3615
Eric Blake41536282017-10-11 22:47:15 -05003616 while (written < n) {
Eric Blakedc61cd32017-10-11 22:47:14 -05003617 int64_t pnum;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003618
Eric Blake41536282017-10-11 22:47:15 -05003619 if (compare_buffers(buf_old + written, buf_new + written,
3620 n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003621 {
Max Reitz1c6e8772019-05-09 19:52:36 +02003622 if (buf_old_is_zero) {
3623 ret = blk_pwrite_zeroes(blk, offset + written, pnum, 0);
3624 } else {
3625 ret = blk_pwrite(blk, offset + written,
3626 buf_old + written, pnum, 0);
3627 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003628 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003629 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003630 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003631 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003632 }
3633 }
3634
3635 written += pnum;
3636 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003637 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003638 }
3639 }
3640
3641 /*
3642 * Change the backing file. All clusters that are different from the old
3643 * backing file are overwritten in the COW file now, so the visible content
3644 * doesn't change when we switch the backing file.
3645 */
Alex Bligha6166732012-10-16 13:46:18 +01003646 if (out_baseimg && *out_baseimg) {
3647 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3648 } else {
3649 ret = bdrv_change_backing_file(bs, NULL, NULL);
3650 }
3651
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003652 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003653 error_report("Could not change the backing file to '%s': No "
3654 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003655 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003656 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003657 out_baseimg, strerror(-ret));
3658 }
3659
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003660 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003661 /*
3662 * TODO At this point it is possible to check if any clusters that are
3663 * allocated in the COW file are the same in the backing file. If so, they
3664 * could be dropped from the COW file. Don't do this before switching the
3665 * backing file, in case of a crash this would lead to corruption.
3666 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003667out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003668 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003669 /* Cleanup */
3670 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003671 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003672 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003673 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003674 qemu_vfree(buf_old);
3675 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003676
Markus Armbruster26f54e92014-10-07 13:59:04 +02003677 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003678 if (ret) {
3679 return 1;
3680 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003681 return 0;
3682}
3683
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003684static int img_resize(int argc, char **argv)
3685{
Markus Armbruster6750e792015-02-12 17:43:08 +01003686 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003687 int c, ret, relative;
3688 const char *filename, *fmt, *size;
Max Reitz09c5c6d2019-09-18 11:51:44 +02003689 int64_t n, total_size, current_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003690 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003691 BlockBackend *blk = NULL;
Max Reitzdc5f6902017-06-13 22:20:55 +02003692 PreallocMode prealloc = PREALLOC_MODE_OFF;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003693 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003694
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003695 static QemuOptsList resize_options = {
3696 .name = "resize_options",
3697 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3698 .desc = {
3699 {
3700 .name = BLOCK_OPT_SIZE,
3701 .type = QEMU_OPT_SIZE,
3702 .help = "Virtual disk size"
3703 }, {
3704 /* end of list */
3705 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003706 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003707 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003708 bool image_opts = false;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003709 bool shrink = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003710
Kevin Wolfe80fec72011-04-29 10:58:12 +02003711 /* Remove size from argv manually so that negative numbers are not treated
3712 * as options by getopt. */
3713 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003714 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003715 return 1;
3716 }
3717
3718 size = argv[--argc];
3719
3720 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003721 fmt = NULL;
3722 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003723 static const struct option long_options[] = {
3724 {"help", no_argument, 0, 'h'},
3725 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003726 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Max Reitzdc5f6902017-06-13 22:20:55 +02003727 {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003728 {"shrink", no_argument, 0, OPTION_SHRINK},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003729 {0, 0, 0, 0}
3730 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003731 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003732 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003733 if (c == -1) {
3734 break;
3735 }
3736 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003737 case ':':
3738 missing_argument(argv[optind - 1]);
3739 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003740 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003741 unrecognized_option(argv[optind - 1]);
3742 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003743 case 'h':
3744 help();
3745 break;
3746 case 'f':
3747 fmt = optarg;
3748 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003749 case 'q':
3750 quiet = true;
3751 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003752 case OPTION_OBJECT: {
3753 QemuOpts *opts;
3754 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3755 optarg, true);
3756 if (!opts) {
3757 return 1;
3758 }
3759 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003760 case OPTION_IMAGE_OPTS:
3761 image_opts = true;
3762 break;
Max Reitzdc5f6902017-06-13 22:20:55 +02003763 case OPTION_PREALLOCATION:
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02003764 prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
Markus Armbruster06c60b62017-08-24 10:45:57 +02003765 PREALLOC_MODE__MAX, NULL);
Max Reitzdc5f6902017-06-13 22:20:55 +02003766 if (prealloc == PREALLOC_MODE__MAX) {
3767 error_report("Invalid preallocation mode '%s'", optarg);
3768 return 1;
3769 }
3770 break;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003771 case OPTION_SHRINK:
3772 shrink = true;
3773 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003774 }
3775 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003776 if (optind != argc - 1) {
Max Reitzbe8fbd42018-02-05 17:27:45 +01003777 error_exit("Expecting image file name and size");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003778 }
3779 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003780
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003781 if (qemu_opts_foreach(&qemu_object_opts,
3782 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02003783 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003784 return 1;
3785 }
3786
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003787 /* Choose grow, shrink, or absolute resize mode */
3788 switch (size[0]) {
3789 case '+':
3790 relative = 1;
3791 size++;
3792 break;
3793 case '-':
3794 relative = -1;
3795 size++;
3796 break;
3797 default:
3798 relative = 0;
3799 break;
3800 }
3801
3802 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003803 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003804 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003805 if (err) {
3806 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003807 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003808 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003809 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003810 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003811 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3812 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003813
Max Reitzefaa7c42016-03-16 19:54:38 +01003814 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003815 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3816 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003817 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003818 ret = -1;
3819 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003820 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003821
Max Reitzdc5f6902017-06-13 22:20:55 +02003822 current_size = blk_getlength(blk);
3823 if (current_size < 0) {
3824 error_report("Failed to inquire current image length: %s",
3825 strerror(-current_size));
3826 ret = -1;
3827 goto out;
3828 }
3829
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003830 if (relative) {
Max Reitzdc5f6902017-06-13 22:20:55 +02003831 total_size = current_size + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003832 } else {
3833 total_size = n;
3834 }
3835 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003836 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003837 ret = -1;
3838 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003839 }
3840
Max Reitzdc5f6902017-06-13 22:20:55 +02003841 if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
3842 error_report("Preallocation can only be used for growing images");
3843 ret = -1;
3844 goto out;
3845 }
3846
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003847 if (total_size < current_size && !shrink) {
3848 warn_report("Shrinking an image will delete all data beyond the "
3849 "shrunken image's end. Before performing such an "
3850 "operation, make sure there is no important data there.");
3851
3852 if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) {
3853 error_report(
3854 "Use the --shrink option to perform a shrink operation.");
3855 ret = -1;
3856 goto out;
3857 } else {
3858 warn_report("Using the --shrink option will suppress this message. "
3859 "Note that future versions of qemu-img may refuse to "
3860 "shrink images without this option.");
3861 }
3862 }
3863
Max Reitze8d04f92019-09-18 11:51:43 +02003864 /*
3865 * The user expects the image to have the desired size after
3866 * resizing, so pass @exact=true. It is of no use to report
3867 * success when the image has not actually been resized.
3868 */
3869 ret = blk_truncate(blk, total_size, true, prealloc, &err);
Max Reitz09c5c6d2019-09-18 11:51:44 +02003870 if (!ret) {
3871 qprintf(quiet, "Image resized.\n");
3872 } else {
Max Reitzed3d2ec2017-03-28 22:51:27 +02003873 error_report_err(err);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003874 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003875out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003876 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003877 if (ret) {
3878 return 1;
3879 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003880 return 0;
3881}
3882
Max Reitz76a3a342014-10-27 11:12:51 +01003883static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003884 int64_t offset, int64_t total_work_size,
3885 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003886{
3887 qemu_progress_print(100.f * offset / total_work_size, 0);
3888}
3889
Max Reitz51641352018-05-09 23:00:20 +02003890static int print_amend_option_help(const char *format)
3891{
3892 BlockDriver *drv;
3893
3894 /* Find driver and parse its options */
3895 drv = bdrv_find_format(format);
3896 if (!drv) {
3897 error_report("Unknown file format '%s'", format);
3898 return 1;
3899 }
3900
3901 if (!drv->bdrv_amend_options) {
3902 error_report("Format driver '%s' does not support option amendment",
3903 format);
3904 return 1;
3905 }
3906
3907 /* Every driver supporting amendment must have create_opts */
3908 assert(drv->create_opts);
3909
3910 printf("Creation options for '%s':\n", format);
Max Reitz63898712018-10-19 18:49:25 +02003911 qemu_opts_print_help(drv->create_opts, false);
Max Reitz51641352018-05-09 23:00:20 +02003912 printf("\nNote that not all of these options may be amendable.\n");
3913 return 0;
3914}
3915
Max Reitz6f176b42013-09-03 10:09:50 +02003916static int img_amend(int argc, char **argv)
3917{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003918 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003919 int c, ret = 0;
3920 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003921 QemuOptsList *create_opts = NULL;
3922 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003923 const char *fmt = NULL, *filename, *cache;
3924 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003925 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003926 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003927 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003928 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003929 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003930
Max Reitzbd39e6e2014-07-22 22:58:43 +02003931 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003932 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003933 static const struct option long_options[] = {
3934 {"help", no_argument, 0, 'h'},
3935 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003936 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003937 {0, 0, 0, 0}
3938 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003939 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003940 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003941 if (c == -1) {
3942 break;
3943 }
3944
3945 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003946 case ':':
3947 missing_argument(argv[optind - 1]);
3948 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003949 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003950 unrecognized_option(argv[optind - 1]);
3951 break;
3952 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003953 help();
3954 break;
3955 case 'o':
3956 if (!is_valid_option_list(optarg)) {
3957 error_report("Invalid option list: %s", optarg);
3958 ret = -1;
3959 goto out_no_progress;
3960 }
3961 if (!options) {
3962 options = g_strdup(optarg);
3963 } else {
3964 char *old_options = options;
3965 options = g_strdup_printf("%s,%s", options, optarg);
3966 g_free(old_options);
3967 }
3968 break;
3969 case 'f':
3970 fmt = optarg;
3971 break;
3972 case 't':
3973 cache = optarg;
3974 break;
3975 case 'p':
3976 progress = true;
3977 break;
3978 case 'q':
3979 quiet = true;
3980 break;
3981 case OPTION_OBJECT:
3982 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3983 optarg, true);
3984 if (!opts) {
3985 ret = -1;
3986 goto out_no_progress;
3987 }
3988 break;
3989 case OPTION_IMAGE_OPTS:
3990 image_opts = true;
3991 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003992 }
3993 }
3994
Max Reitz6f176b42013-09-03 10:09:50 +02003995 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003996 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003997 }
3998
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003999 if (qemu_opts_foreach(&qemu_object_opts,
4000 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02004001 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004002 ret = -1;
4003 goto out_no_progress;
4004 }
4005
Max Reitz76a3a342014-10-27 11:12:51 +01004006 if (quiet) {
4007 progress = false;
4008 }
4009 qemu_progress_init(progress, 1.0);
4010
Kevin Wolfa283cb62014-02-21 16:24:07 +01004011 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
4012 if (fmt && has_help_option(options)) {
4013 /* If a format is explicitly specified (and possibly no filename is
4014 * given), print option help here */
Max Reitz51641352018-05-09 23:00:20 +02004015 ret = print_amend_option_help(fmt);
Kevin Wolfa283cb62014-02-21 16:24:07 +01004016 goto out;
4017 }
4018
4019 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01004020 error_report("Expecting one image file name");
4021 ret = -1;
4022 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01004023 }
Max Reitz6f176b42013-09-03 10:09:50 +02004024
Kevin Wolfce099542016-03-15 13:01:04 +01004025 flags = BDRV_O_RDWR;
4026 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02004027 if (ret < 0) {
4028 error_report("Invalid cache option: %s", cache);
4029 goto out;
4030 }
4031
Fam Zheng335e9932017-05-03 00:35:39 +08004032 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4033 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02004034 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02004035 ret = -1;
4036 goto out;
4037 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02004038 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02004039
4040 fmt = bs->drv->format_name;
4041
Kevin Wolf626f84f2014-02-21 16:24:06 +01004042 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01004043 /* If the format was auto-detected, print option help here */
Max Reitz51641352018-05-09 23:00:20 +02004044 ret = print_amend_option_help(fmt);
Max Reitz6f176b42013-09-03 10:09:50 +02004045 goto out;
4046 }
4047
Max Reitz1f996682018-05-09 23:00:17 +02004048 if (!bs->drv->bdrv_amend_options) {
4049 error_report("Format driver '%s' does not support option amendment",
Max Reitzb2439d22014-12-02 18:32:47 +01004050 fmt);
4051 ret = -1;
4052 goto out;
4053 }
4054
Max Reitz1f996682018-05-09 23:00:17 +02004055 /* Every driver supporting amendment must have create_opts */
4056 assert(bs->drv->create_opts);
4057
Chunyan Liuc282e1f2014-06-05 17:21:11 +08004058 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08004059 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01004060 qemu_opts_do_parse(opts, options, NULL, &err);
4061 if (err) {
4062 error_report_err(err);
4063 ret = -1;
4064 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02004065 }
4066
Max Reitz76a3a342014-10-27 11:12:51 +01004067 /* In case the driver does not call amend_status_cb() */
4068 qemu_progress_print(0.f, 0);
Max Reitzd1402b52018-05-09 23:00:18 +02004069 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, &err);
Max Reitz76a3a342014-10-27 11:12:51 +01004070 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02004071 if (ret < 0) {
Max Reitzd1402b52018-05-09 23:00:18 +02004072 error_report_err(err);
Max Reitz6f176b42013-09-03 10:09:50 +02004073 goto out;
4074 }
4075
4076out:
Max Reitz76a3a342014-10-27 11:12:51 +01004077 qemu_progress_end();
4078
Max Reitze814dff2015-08-20 16:00:38 -07004079out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02004080 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08004081 qemu_opts_del(opts);
4082 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01004083 g_free(options);
4084
Max Reitz6f176b42013-09-03 10:09:50 +02004085 if (ret) {
4086 return 1;
4087 }
4088 return 0;
4089}
4090
Kevin Wolfb6133b82014-08-05 14:17:13 +02004091typedef struct BenchData {
4092 BlockBackend *blk;
4093 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004094 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004095 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02004096 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004097 int nrreq;
4098 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004099 int flush_interval;
4100 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004101 uint8_t *buf;
4102 QEMUIOVector *qiov;
4103
4104 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004105 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004106 uint64_t offset;
4107} BenchData;
4108
Kevin Wolf55d539c2016-06-03 13:59:41 +02004109static void bench_undrained_flush_cb(void *opaque, int ret)
4110{
4111 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02004112 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02004113 exit(EXIT_FAILURE);
4114 }
4115}
4116
Kevin Wolfb6133b82014-08-05 14:17:13 +02004117static void bench_cb(void *opaque, int ret)
4118{
4119 BenchData *b = opaque;
4120 BlockAIOCB *acb;
4121
4122 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02004123 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02004124 exit(EXIT_FAILURE);
4125 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02004126
4127 if (b->in_flush) {
4128 /* Just finished a flush with drained queue: Start next requests */
4129 assert(b->in_flight == 0);
4130 b->in_flush = false;
4131 } else if (b->in_flight > 0) {
4132 int remaining = b->n - b->in_flight;
4133
Kevin Wolfb6133b82014-08-05 14:17:13 +02004134 b->n--;
4135 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004136
4137 /* Time for flush? Drain queue if requested, then flush */
4138 if (b->flush_interval && remaining % b->flush_interval == 0) {
4139 if (!b->in_flight || !b->drain_on_flush) {
4140 BlockCompletionFunc *cb;
4141
4142 if (b->drain_on_flush) {
4143 b->in_flush = true;
4144 cb = bench_cb;
4145 } else {
4146 cb = bench_undrained_flush_cb;
4147 }
4148
4149 acb = blk_aio_flush(b->blk, cb, b);
4150 if (!acb) {
4151 error_report("Failed to issue flush request");
4152 exit(EXIT_FAILURE);
4153 }
4154 }
4155 if (b->drain_on_flush) {
4156 return;
4157 }
4158 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004159 }
4160
4161 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01004162 int64_t offset = b->offset;
4163 /* blk_aio_* might look for completed I/Os and kick bench_cb
4164 * again, so make sure this operation is counted by in_flight
4165 * and b->offset is ready for the next submission.
4166 */
4167 b->in_flight++;
4168 b->offset += b->step;
4169 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004170 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01004171 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004172 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01004173 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004174 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004175 if (!acb) {
4176 error_report("Failed to issue request");
4177 exit(EXIT_FAILURE);
4178 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004179 }
4180}
4181
4182static int img_bench(int argc, char **argv)
4183{
4184 int c, ret = 0;
4185 const char *fmt = NULL, *filename;
4186 bool quiet = false;
4187 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004188 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004189 int count = 75000;
4190 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02004191 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004192 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004193 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02004194 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004195 int flush_interval = 0;
4196 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004197 int64_t image_size;
4198 BlockBackend *blk = NULL;
4199 BenchData data = {};
4200 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02004201 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004202 struct timeval t1, t2;
4203 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08004204 bool force_share = false;
Fam Zheng79d46582018-01-16 14:08:58 +08004205 size_t buf_size;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004206
4207 for (;;) {
4208 static const struct option long_options[] = {
4209 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02004210 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02004211 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004212 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02004213 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08004214 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02004215 {0, 0, 0, 0}
4216 };
Aarushi Mehtacdd26772020-01-20 14:18:55 +00004217 c = getopt_long(argc, argv, ":hc:d:f:ni:o:qs:S:t:wU", long_options,
4218 NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004219 if (c == -1) {
4220 break;
4221 }
4222
4223 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004224 case ':':
4225 missing_argument(argv[optind - 1]);
4226 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004227 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004228 unrecognized_option(argv[optind - 1]);
4229 break;
4230 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02004231 help();
4232 break;
4233 case 'c':
4234 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004235 unsigned long res;
4236
4237 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02004238 error_report("Invalid request count specified");
4239 return 1;
4240 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004241 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004242 break;
4243 }
4244 case 'd':
4245 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004246 unsigned long res;
4247
4248 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02004249 error_report("Invalid queue depth specified");
4250 return 1;
4251 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004252 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004253 break;
4254 }
4255 case 'f':
4256 fmt = optarg;
4257 break;
4258 case 'n':
4259 flags |= BDRV_O_NATIVE_AIO;
4260 break;
Aarushi Mehtacdd26772020-01-20 14:18:55 +00004261 case 'i':
4262 ret = bdrv_parse_aio(optarg, &flags);
4263 if (ret < 0) {
4264 error_report("Invalid aio option: %s", optarg);
4265 ret = -1;
4266 goto out;
4267 }
4268 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02004269 case 'o':
4270 {
Markus Armbruster606caa02017-02-21 21:14:04 +01004271 offset = cvtnum(optarg);
4272 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02004273 error_report("Invalid offset specified");
4274 return 1;
4275 }
4276 break;
4277 }
4278 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004279 case 'q':
4280 quiet = true;
4281 break;
4282 case 's':
4283 {
4284 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004285
Markus Armbruster606caa02017-02-21 21:14:04 +01004286 sval = cvtnum(optarg);
4287 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02004288 error_report("Invalid buffer size specified");
4289 return 1;
4290 }
4291
4292 bufsize = sval;
4293 break;
4294 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02004295 case 'S':
4296 {
4297 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02004298
Markus Armbruster606caa02017-02-21 21:14:04 +01004299 sval = cvtnum(optarg);
4300 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02004301 error_report("Invalid step size specified");
4302 return 1;
4303 }
4304
4305 step = sval;
4306 break;
4307 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004308 case 't':
4309 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
4310 if (ret < 0) {
4311 error_report("Invalid cache mode");
4312 ret = -1;
4313 goto out;
4314 }
4315 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004316 case 'w':
4317 flags |= BDRV_O_RDWR;
4318 is_write = true;
4319 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004320 case 'U':
4321 force_share = true;
4322 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004323 case OPTION_PATTERN:
4324 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004325 unsigned long res;
4326
4327 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004328 error_report("Invalid pattern byte specified");
4329 return 1;
4330 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004331 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004332 break;
4333 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02004334 case OPTION_FLUSH_INTERVAL:
4335 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004336 unsigned long res;
4337
4338 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004339 error_report("Invalid flush interval specified");
4340 return 1;
4341 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004342 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004343 break;
4344 }
4345 case OPTION_NO_DRAIN:
4346 drain_on_flush = false;
4347 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004348 case OPTION_IMAGE_OPTS:
4349 image_opts = true;
4350 break;
4351 }
4352 }
4353
4354 if (optind != argc - 1) {
4355 error_exit("Expecting one image file name");
4356 }
4357 filename = argv[argc - 1];
4358
Kevin Wolf55d539c2016-06-03 13:59:41 +02004359 if (!is_write && flush_interval) {
4360 error_report("--flush-interval is only available in write tests");
4361 ret = -1;
4362 goto out;
4363 }
4364 if (flush_interval && flush_interval < depth) {
4365 error_report("Flush interval can't be smaller than depth");
4366 ret = -1;
4367 goto out;
4368 }
4369
Fam Zheng335e9932017-05-03 00:35:39 +08004370 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4371 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004372 if (!blk) {
4373 ret = -1;
4374 goto out;
4375 }
4376
4377 image_size = blk_getlength(blk);
4378 if (image_size < 0) {
4379 ret = image_size;
4380 goto out;
4381 }
4382
4383 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004384 .blk = blk,
4385 .image_size = image_size,
4386 .bufsize = bufsize,
4387 .step = step ?: bufsize,
4388 .nrreq = depth,
4389 .n = count,
4390 .offset = offset,
4391 .write = is_write,
4392 .flush_interval = flush_interval,
4393 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004394 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004395 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004396 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004397 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004398 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004399 if (flush_interval) {
4400 printf("Sending flush every %d requests\n", flush_interval);
4401 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004402
Fam Zheng79d46582018-01-16 14:08:58 +08004403 buf_size = data.nrreq * data.bufsize;
4404 data.buf = blk_blockalign(blk, buf_size);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004405 memset(data.buf, pattern, data.nrreq * data.bufsize);
4406
Fam Zheng79d46582018-01-16 14:08:58 +08004407 blk_register_buf(blk, data.buf, buf_size);
4408
Kevin Wolfb6133b82014-08-05 14:17:13 +02004409 data.qiov = g_new(QEMUIOVector, data.nrreq);
4410 for (i = 0; i < data.nrreq; i++) {
4411 qemu_iovec_init(&data.qiov[i], 1);
4412 qemu_iovec_add(&data.qiov[i],
4413 data.buf + i * data.bufsize, data.bufsize);
4414 }
4415
4416 gettimeofday(&t1, NULL);
4417 bench_cb(&data, 0);
4418
4419 while (data.n > 0) {
4420 main_loop_wait(false);
4421 }
4422 gettimeofday(&t2, NULL);
4423
4424 printf("Run completed in %3.3f seconds.\n",
4425 (t2.tv_sec - t1.tv_sec)
4426 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4427
4428out:
Fam Zheng79d46582018-01-16 14:08:58 +08004429 if (data.buf) {
4430 blk_unregister_buf(blk, data.buf);
4431 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004432 qemu_vfree(data.buf);
4433 blk_unref(blk);
4434
4435 if (ret) {
4436 return 1;
4437 }
4438 return 0;
4439}
4440
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004441#define C_BS 01
4442#define C_COUNT 02
4443#define C_IF 04
4444#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004445#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004446
4447struct DdInfo {
4448 unsigned int flags;
4449 int64_t count;
4450};
4451
4452struct DdIo {
4453 int bsz; /* Block size */
4454 char *filename;
4455 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004456 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004457};
4458
4459struct DdOpts {
4460 const char *name;
4461 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4462 unsigned int flag;
4463};
4464
4465static int img_dd_bs(const char *arg,
4466 struct DdIo *in, struct DdIo *out,
4467 struct DdInfo *dd)
4468{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004469 int64_t res;
4470
Markus Armbruster606caa02017-02-21 21:14:04 +01004471 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004472
Markus Armbruster606caa02017-02-21 21:14:04 +01004473 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004474 error_report("invalid number: '%s'", arg);
4475 return 1;
4476 }
4477 in->bsz = out->bsz = res;
4478
4479 return 0;
4480}
4481
4482static int img_dd_count(const char *arg,
4483 struct DdIo *in, struct DdIo *out,
4484 struct DdInfo *dd)
4485{
Markus Armbruster606caa02017-02-21 21:14:04 +01004486 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004487
Markus Armbruster606caa02017-02-21 21:14:04 +01004488 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004489 error_report("invalid number: '%s'", arg);
4490 return 1;
4491 }
4492
4493 return 0;
4494}
4495
4496static int img_dd_if(const char *arg,
4497 struct DdIo *in, struct DdIo *out,
4498 struct DdInfo *dd)
4499{
4500 in->filename = g_strdup(arg);
4501
4502 return 0;
4503}
4504
4505static int img_dd_of(const char *arg,
4506 struct DdIo *in, struct DdIo *out,
4507 struct DdInfo *dd)
4508{
4509 out->filename = g_strdup(arg);
4510
4511 return 0;
4512}
4513
Reda Sallahif7c15532016-08-10 16:16:09 +02004514static int img_dd_skip(const char *arg,
4515 struct DdIo *in, struct DdIo *out,
4516 struct DdInfo *dd)
4517{
Markus Armbruster606caa02017-02-21 21:14:04 +01004518 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004519
Markus Armbruster606caa02017-02-21 21:14:04 +01004520 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004521 error_report("invalid number: '%s'", arg);
4522 return 1;
4523 }
4524
4525 return 0;
4526}
4527
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004528static int img_dd(int argc, char **argv)
4529{
4530 int ret = 0;
4531 char *arg = NULL;
4532 char *tmp;
4533 BlockDriver *drv = NULL, *proto_drv = NULL;
4534 BlockBackend *blk1 = NULL, *blk2 = NULL;
4535 QemuOpts *opts = NULL;
4536 QemuOptsList *create_opts = NULL;
4537 Error *local_err = NULL;
4538 bool image_opts = false;
4539 int c, i;
4540 const char *out_fmt = "raw";
4541 const char *fmt = NULL;
4542 int64_t size = 0;
4543 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004544 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004545 struct DdInfo dd = {
4546 .flags = 0,
4547 .count = 0,
4548 };
4549 struct DdIo in = {
4550 .bsz = 512, /* Block size is by default 512 bytes */
4551 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004552 .buf = NULL,
4553 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004554 };
4555 struct DdIo out = {
4556 .bsz = 512,
4557 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004558 .buf = NULL,
4559 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004560 };
4561
4562 const struct DdOpts options[] = {
4563 { "bs", img_dd_bs, C_BS },
4564 { "count", img_dd_count, C_COUNT },
4565 { "if", img_dd_if, C_IF },
4566 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004567 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004568 { NULL, NULL, 0 }
4569 };
4570 const struct option long_options[] = {
4571 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004572 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004573 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004574 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004575 { 0, 0, 0, 0 }
4576 };
4577
Fam Zheng335e9932017-05-03 00:35:39 +08004578 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004579 if (c == EOF) {
4580 break;
4581 }
4582 switch (c) {
4583 case 'O':
4584 out_fmt = optarg;
4585 break;
4586 case 'f':
4587 fmt = optarg;
4588 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004589 case ':':
4590 missing_argument(argv[optind - 1]);
4591 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004592 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004593 unrecognized_option(argv[optind - 1]);
4594 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004595 case 'h':
4596 help();
4597 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004598 case 'U':
4599 force_share = true;
4600 break;
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004601 case OPTION_OBJECT:
4602 if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004603 ret = -1;
4604 goto out;
4605 }
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004606 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004607 case OPTION_IMAGE_OPTS:
4608 image_opts = true;
4609 break;
4610 }
4611 }
4612
4613 for (i = optind; i < argc; i++) {
4614 int j;
4615 arg = g_strdup(argv[i]);
4616
4617 tmp = strchr(arg, '=');
4618 if (tmp == NULL) {
4619 error_report("unrecognized operand %s", arg);
4620 ret = -1;
4621 goto out;
4622 }
4623
4624 *tmp++ = '\0';
4625
4626 for (j = 0; options[j].name != NULL; j++) {
4627 if (!strcmp(arg, options[j].name)) {
4628 break;
4629 }
4630 }
4631 if (options[j].name == NULL) {
4632 error_report("unrecognized operand %s", arg);
4633 ret = -1;
4634 goto out;
4635 }
4636
4637 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4638 ret = -1;
4639 goto out;
4640 }
4641 dd.flags |= options[j].flag;
4642 g_free(arg);
4643 arg = NULL;
4644 }
4645
4646 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4647 error_report("Must specify both input and output files");
4648 ret = -1;
4649 goto out;
4650 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004651
4652 if (qemu_opts_foreach(&qemu_object_opts,
4653 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02004654 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004655 ret = -1;
4656 goto out;
4657 }
4658
Fam Zheng335e9932017-05-03 00:35:39 +08004659 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4660 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004661
4662 if (!blk1) {
4663 ret = -1;
4664 goto out;
4665 }
4666
4667 drv = bdrv_find_format(out_fmt);
4668 if (!drv) {
4669 error_report("Unknown file format");
4670 ret = -1;
4671 goto out;
4672 }
4673 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4674
4675 if (!proto_drv) {
4676 error_report_err(local_err);
4677 ret = -1;
4678 goto out;
4679 }
4680 if (!drv->create_opts) {
4681 error_report("Format driver '%s' does not support image creation",
4682 drv->format_name);
4683 ret = -1;
4684 goto out;
4685 }
4686 if (!proto_drv->create_opts) {
4687 error_report("Protocol driver '%s' does not support image creation",
4688 proto_drv->format_name);
4689 ret = -1;
4690 goto out;
4691 }
4692 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4693 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4694
4695 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4696
4697 size = blk_getlength(blk1);
4698 if (size < 0) {
4699 error_report("Failed to get size for '%s'", in.filename);
4700 ret = -1;
4701 goto out;
4702 }
4703
4704 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4705 dd.count * in.bsz < size) {
4706 size = dd.count * in.bsz;
4707 }
4708
Reda Sallahif7c15532016-08-10 16:16:09 +02004709 /* Overflow means the specified offset is beyond input image's size */
4710 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4711 size < in.bsz * in.offset)) {
4712 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4713 } else {
4714 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4715 size - in.bsz * in.offset, &error_abort);
4716 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004717
4718 ret = bdrv_create(drv, out.filename, opts, &local_err);
4719 if (ret < 0) {
4720 error_reportf_err(local_err,
4721 "%s: error while creating output image: ",
4722 out.filename);
4723 ret = -1;
4724 goto out;
4725 }
4726
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004727 /* TODO, we can't honour --image-opts for the target,
4728 * since it needs to be given in a format compatible
4729 * with the bdrv_create() call above which does not
4730 * support image-opts style.
4731 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01004732 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004733 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004734
4735 if (!blk2) {
4736 ret = -1;
4737 goto out;
4738 }
4739
Reda Sallahif7c15532016-08-10 16:16:09 +02004740 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4741 size < in.offset * in.bsz)) {
4742 /* We give a warning if the skip option is bigger than the input
4743 * size and create an empty output disk image (i.e. like dd(1)).
4744 */
4745 error_report("%s: cannot skip to specified offset", in.filename);
4746 in_pos = size;
4747 } else {
4748 in_pos = in.offset * in.bsz;
4749 }
4750
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004751 in.buf = g_new(uint8_t, in.bsz);
4752
Reda Sallahif7c15532016-08-10 16:16:09 +02004753 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004754 int in_ret, out_ret;
4755
4756 if (in_pos + in.bsz > size) {
4757 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4758 } else {
4759 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4760 }
4761 if (in_ret < 0) {
4762 error_report("error while reading from input image file: %s",
4763 strerror(-in_ret));
4764 ret = -1;
4765 goto out;
4766 }
4767 in_pos += in_ret;
4768
4769 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4770
4771 if (out_ret < 0) {
4772 error_report("error while writing to output image file: %s",
4773 strerror(-out_ret));
4774 ret = -1;
4775 goto out;
4776 }
4777 out_pos += out_ret;
4778 }
4779
4780out:
4781 g_free(arg);
4782 qemu_opts_del(opts);
4783 qemu_opts_free(create_opts);
4784 blk_unref(blk1);
4785 blk_unref(blk2);
4786 g_free(in.filename);
4787 g_free(out.filename);
4788 g_free(in.buf);
4789 g_free(out.buf);
4790
4791 if (ret) {
4792 return 1;
4793 }
4794 return 0;
4795}
4796
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004797static void dump_json_block_measure_info(BlockMeasureInfo *info)
4798{
4799 QString *str;
4800 QObject *obj;
4801 Visitor *v = qobject_output_visitor_new(&obj);
4802
4803 visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
4804 visit_complete(v, &obj);
4805 str = qobject_to_json_pretty(obj);
4806 assert(str != NULL);
4807 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004808 qobject_unref(obj);
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004809 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004810 qobject_unref(str);
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004811}
4812
4813static int img_measure(int argc, char **argv)
4814{
4815 static const struct option long_options[] = {
4816 {"help", no_argument, 0, 'h'},
4817 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4818 {"object", required_argument, 0, OPTION_OBJECT},
4819 {"output", required_argument, 0, OPTION_OUTPUT},
4820 {"size", required_argument, 0, OPTION_SIZE},
4821 {"force-share", no_argument, 0, 'U'},
4822 {0, 0, 0, 0}
4823 };
4824 OutputFormat output_format = OFORMAT_HUMAN;
4825 BlockBackend *in_blk = NULL;
4826 BlockDriver *drv;
4827 const char *filename = NULL;
4828 const char *fmt = NULL;
4829 const char *out_fmt = "raw";
4830 char *options = NULL;
4831 char *snapshot_name = NULL;
4832 bool force_share = false;
4833 QemuOpts *opts = NULL;
4834 QemuOpts *object_opts = NULL;
4835 QemuOpts *sn_opts = NULL;
4836 QemuOptsList *create_opts = NULL;
4837 bool image_opts = false;
4838 uint64_t img_size = UINT64_MAX;
4839 BlockMeasureInfo *info = NULL;
4840 Error *local_err = NULL;
4841 int ret = 1;
4842 int c;
4843
4844 while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
4845 long_options, NULL)) != -1) {
4846 switch (c) {
4847 case '?':
4848 case 'h':
4849 help();
4850 break;
4851 case 'f':
4852 fmt = optarg;
4853 break;
4854 case 'O':
4855 out_fmt = optarg;
4856 break;
4857 case 'o':
4858 if (!is_valid_option_list(optarg)) {
4859 error_report("Invalid option list: %s", optarg);
4860 goto out;
4861 }
4862 if (!options) {
4863 options = g_strdup(optarg);
4864 } else {
4865 char *old_options = options;
4866 options = g_strdup_printf("%s,%s", options, optarg);
4867 g_free(old_options);
4868 }
4869 break;
4870 case 'l':
4871 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
4872 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
4873 optarg, false);
4874 if (!sn_opts) {
4875 error_report("Failed in parsing snapshot param '%s'",
4876 optarg);
4877 goto out;
4878 }
4879 } else {
4880 snapshot_name = optarg;
4881 }
4882 break;
4883 case 'U':
4884 force_share = true;
4885 break;
4886 case OPTION_OBJECT:
4887 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
4888 optarg, true);
4889 if (!object_opts) {
4890 goto out;
4891 }
4892 break;
4893 case OPTION_IMAGE_OPTS:
4894 image_opts = true;
4895 break;
4896 case OPTION_OUTPUT:
4897 if (!strcmp(optarg, "json")) {
4898 output_format = OFORMAT_JSON;
4899 } else if (!strcmp(optarg, "human")) {
4900 output_format = OFORMAT_HUMAN;
4901 } else {
4902 error_report("--output must be used with human or json "
4903 "as argument.");
4904 goto out;
4905 }
4906 break;
4907 case OPTION_SIZE:
4908 {
4909 int64_t sval;
4910
4911 sval = cvtnum(optarg);
4912 if (sval < 0) {
4913 if (sval == -ERANGE) {
4914 error_report("Image size must be less than 8 EiB!");
4915 } else {
4916 error_report("Invalid image size specified! You may use "
4917 "k, M, G, T, P or E suffixes for ");
4918 error_report("kilobytes, megabytes, gigabytes, terabytes, "
4919 "petabytes and exabytes.");
4920 }
4921 goto out;
4922 }
4923 img_size = (uint64_t)sval;
4924 }
4925 break;
4926 }
4927 }
4928
4929 if (qemu_opts_foreach(&qemu_object_opts,
4930 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02004931 qemu_img_object_print_help, &error_fatal)) {
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004932 goto out;
4933 }
4934
4935 if (argc - optind > 1) {
4936 error_report("At most one filename argument is allowed.");
4937 goto out;
4938 } else if (argc - optind == 1) {
4939 filename = argv[optind];
4940 }
4941
Stefan Hajnoczic3673dc2020-02-21 11:25:21 +00004942 if (!filename && (image_opts || fmt || snapshot_name || sn_opts)) {
4943 error_report("--image-opts, -f, and -l require a filename argument.");
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004944 goto out;
4945 }
4946 if (filename && img_size != UINT64_MAX) {
4947 error_report("--size N cannot be used together with a filename.");
4948 goto out;
4949 }
4950 if (!filename && img_size == UINT64_MAX) {
4951 error_report("Either --size N or one filename must be specified.");
4952 goto out;
4953 }
4954
4955 if (filename) {
4956 in_blk = img_open(image_opts, filename, fmt, 0,
4957 false, false, force_share);
4958 if (!in_blk) {
4959 goto out;
4960 }
4961
4962 if (sn_opts) {
4963 bdrv_snapshot_load_tmp(blk_bs(in_blk),
4964 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
4965 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
4966 &local_err);
4967 } else if (snapshot_name != NULL) {
4968 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
4969 snapshot_name, &local_err);
4970 }
4971 if (local_err) {
4972 error_reportf_err(local_err, "Failed to load snapshot: ");
4973 goto out;
4974 }
4975 }
4976
4977 drv = bdrv_find_format(out_fmt);
4978 if (!drv) {
4979 error_report("Unknown file format '%s'", out_fmt);
4980 goto out;
4981 }
4982 if (!drv->create_opts) {
4983 error_report("Format driver '%s' does not support image creation",
4984 drv->format_name);
4985 goto out;
4986 }
4987
4988 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4989 create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
4990 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4991 if (options) {
4992 qemu_opts_do_parse(opts, options, NULL, &local_err);
4993 if (local_err) {
4994 error_report_err(local_err);
4995 error_report("Invalid options for file format '%s'", out_fmt);
4996 goto out;
4997 }
4998 }
4999 if (img_size != UINT64_MAX) {
5000 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
5001 }
5002
5003 info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
5004 if (local_err) {
5005 error_report_err(local_err);
5006 goto out;
5007 }
5008
5009 if (output_format == OFORMAT_HUMAN) {
5010 printf("required size: %" PRIu64 "\n", info->required);
5011 printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
5012 } else {
5013 dump_json_block_measure_info(info);
5014 }
5015
5016 ret = 0;
5017
5018out:
5019 qapi_free_BlockMeasureInfo(info);
5020 qemu_opts_del(object_opts);
5021 qemu_opts_del(opts);
5022 qemu_opts_del(sn_opts);
5023 qemu_opts_free(create_opts);
5024 g_free(options);
5025 blk_unref(in_blk);
5026 return ret;
5027}
Kevin Wolfb6133b82014-08-05 14:17:13 +02005028
Anthony Liguoric227f092009-10-01 16:12:16 -05005029static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01005030#define DEF(option, callback, arg_string) \
5031 { option, callback },
5032#include "qemu-img-cmds.h"
5033#undef DEF
Stuart Brady153859b2009-06-07 00:42:17 +01005034 { NULL, NULL, },
5035};
5036
bellardea2384d2004-08-01 21:59:26 +00005037int main(int argc, char **argv)
5038{
Anthony Liguoric227f092009-10-01 16:12:16 -05005039 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01005040 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03005041 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03005042 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04005043 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04005044 static const struct option long_options[] = {
5045 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03005046 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03005047 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04005048 {0, 0, 0, 0}
5049 };
bellardea2384d2004-08-01 21:59:26 +00005050
MORITA Kazutaka526eda12013-07-23 17:30:11 +09005051#ifdef CONFIG_POSIX
5052 signal(SIGPIPE, SIG_IGN);
5053#endif
5054
Christophe Fergeauf5852ef2019-01-31 17:46:14 +01005055 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01005056 module_call_init(MODULE_INIT_TRACE);
Fam Zheng10f5bff2014-02-10 14:48:51 +08005057 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01005058
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03005059 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01005060 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03005061 exit(EXIT_FAILURE);
5062 }
5063
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03005064 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01005065
Daniel P. Berrange064097d2016-02-10 18:41:01 +00005066 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00005067 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08005068 if (argc < 2) {
5069 error_exit("Not enough arguments");
5070 }
Stuart Brady153859b2009-06-07 00:42:17 +01005071
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00005072 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00005073 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03005074 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00005075
Stefan Hajnoczic9192972017-03-17 18:45:41 +08005076 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03005077 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08005078 case ':':
5079 missing_argument(argv[optind - 1]);
5080 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08005081 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08005082 unrecognized_option(argv[optind - 1]);
5083 return 0;
5084 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03005085 help();
5086 return 0;
5087 case 'V':
5088 printf(QEMU_IMG_VERSION);
5089 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03005090 case 'T':
5091 g_free(trace_file);
5092 trace_file = trace_opt_parse(optarg);
5093 break;
Stuart Brady153859b2009-06-07 00:42:17 +01005094 }
bellardea2384d2004-08-01 21:59:26 +00005095 }
Stuart Brady153859b2009-06-07 00:42:17 +01005096
Denis V. Lunev10985132016-06-17 17:44:13 +03005097 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04005098
Denis V. Lunev10985132016-06-17 17:44:13 +03005099 /* reset getopt_long scanning */
5100 argc -= optind;
5101 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04005102 return 0;
5103 }
Denis V. Lunev10985132016-06-17 17:44:13 +03005104 argv += optind;
Richard W.M. Jonesd339d762019-01-18 10:11:14 +00005105 qemu_reset_optind();
Denis V. Lunev10985132016-06-17 17:44:13 +03005106
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03005107 if (!trace_init_backends()) {
5108 exit(1);
5109 }
5110 trace_init_file(trace_file);
5111 qemu_set_log(LOG_TRACE);
5112
Denis V. Lunev10985132016-06-17 17:44:13 +03005113 /* find the command */
5114 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
5115 if (!strcmp(cmdname, cmd->name)) {
5116 return cmd->handler(argc, argv);
5117 }
5118 }
Jeff Cody7db16892014-04-25 17:02:32 -04005119
Stuart Brady153859b2009-06-07 00:42:17 +01005120 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08005121 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00005122}