blob: d36b21b7581e3fcfee7f857c1b337630c2bb2c11 [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
Markus Armbruster6d2b5cb2020-04-15 09:49:25 +0200226static int accumulate_options(char **options, char *optarg)
227{
228 char *new_options;
229
230 if (!is_valid_option_list(optarg)) {
231 error_report("Invalid option list: %s", optarg);
232 return -1;
233 }
234
235 if (!*options) {
236 *options = g_strdup(optarg);
237 } else {
238 new_options = g_strdup_printf("%s,%s", *options, optarg);
239 g_free(*options);
240 *options = new_options;
241 }
242 return 0;
243}
244
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000245static QemuOptsList qemu_source_opts = {
246 .name = "source",
247 .implied_opt_name = "file",
248 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
249 .desc = {
250 { }
251 },
252};
253
Stefan Weil7c30f652013-06-16 17:01:05 +0200254static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100255{
256 int ret = 0;
257 if (!quiet) {
258 va_list args;
259 va_start(args, fmt);
260 ret = vprintf(fmt, args);
261 va_end(args);
262 }
263 return ret;
264}
265
bellardea2384d2004-08-01 21:59:26 +0000266
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100267static int print_block_option_help(const char *filename, const char *fmt)
268{
269 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800270 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500271 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100272
273 /* Find driver and parse its options */
274 drv = bdrv_find_format(fmt);
275 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100276 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100277 return 1;
278 }
279
Max Reitzd402b6a2018-05-09 23:00:21 +0200280 if (!drv->create_opts) {
281 error_report("Format driver '%s' does not support image creation", fmt);
282 return 1;
283 }
284
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800285 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100286 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500287 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100288 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100289 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800290 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100291 return 1;
292 }
Max Reitzd402b6a2018-05-09 23:00:21 +0200293 if (!proto_drv->create_opts) {
Max Reitzf0998872018-11-19 11:19:20 +0100294 error_report("Protocol driver '%s' does not support image creation",
Max Reitzd402b6a2018-05-09 23:00:21 +0200295 proto_drv->format_name);
Max Reitz3ecd5a42018-11-19 11:19:21 +0100296 qemu_opts_free(create_opts);
Max Reitzd402b6a2018-05-09 23:00:21 +0200297 return 1;
298 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800299 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100300 }
301
Max Reitzf4619af2019-04-13 17:20:37 +0200302 if (filename) {
303 printf("Supported options:\n");
304 } else {
305 printf("Supported %s options:\n", fmt);
306 }
Max Reitz63898712018-10-19 18:49:25 +0200307 qemu_opts_print_help(create_opts, false);
Chunyan Liu83d05212014-06-05 17:20:51 +0800308 qemu_opts_free(create_opts);
Max Reitzf4619af2019-04-13 17:20:37 +0200309
310 if (!filename) {
311 printf("\n"
312 "The protocol level may support further options.\n"
313 "Specify the target filename to include those options.\n");
314 }
315
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100316 return 0;
317}
318
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000319
Max Reitzefaa7c42016-03-16 19:54:38 +0100320static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100321 QemuOpts *opts, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800322 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000323{
324 QDict *options;
325 Error *local_err = NULL;
326 BlockBackend *blk;
327 options = qemu_opts_to_qdict(opts, NULL);
Fam Zheng335e9932017-05-03 00:35:39 +0800328 if (force_share) {
329 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
Max Reitz4615f872018-05-02 22:20:50 +0200330 && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) {
Fam Zheng335e9932017-05-03 00:35:39 +0800331 error_report("--force-share/-U conflicts with image options");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200332 qobject_unref(options);
Fam Zheng335e9932017-05-03 00:35:39 +0800333 return NULL;
334 }
Max Reitz4615f872018-05-02 22:20:50 +0200335 qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on");
Fam Zheng335e9932017-05-03 00:35:39 +0800336 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100337 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000338 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100339 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000340 return NULL;
341 }
Kevin Wolfce099542016-03-15 13:01:04 +0100342 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000343
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000344 return blk;
345}
346
Max Reitzefaa7c42016-03-16 19:54:38 +0100347static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100348 QDict *options,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000349 const char *fmt, int flags,
Fam Zheng335e9932017-05-03 00:35:39 +0800350 bool writethrough, bool quiet,
351 bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000352{
353 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200354 Error *local_err = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100355
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100356 if (!options) {
357 options = qdict_new();
358 }
bellard75c23802004-08-27 21:28:58 +0000359 if (fmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500360 qdict_put_str(options, "driver", fmt);
bellard75c23802004-08-27 21:28:58 +0000361 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100362
Fam Zheng335e9932017-05-03 00:35:39 +0800363 if (force_share) {
Eric Blake579cf1d2017-05-15 14:54:39 -0500364 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800365 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100366 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500367 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100368 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000369 return NULL;
bellard75c23802004-08-27 21:28:58 +0000370 }
Kevin Wolfce099542016-03-15 13:01:04 +0100371 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100372
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200373 return blk;
bellard75c23802004-08-27 21:28:58 +0000374}
375
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000376
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100377static int img_add_key_secrets(void *opaque,
378 const char *name, const char *value,
379 Error **errp)
380{
381 QDict *options = opaque;
382
383 if (g_str_has_suffix(name, "key-secret")) {
Eric Blake187f47e2017-06-24 12:10:07 -0600384 qdict_put_str(options, name, value);
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100385 }
386
387 return 0;
388}
389
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100390
Max Reitzefaa7c42016-03-16 19:54:38 +0100391static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000392 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100393 const char *fmt, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800394 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000395{
396 BlockBackend *blk;
397 if (image_opts) {
398 QemuOpts *opts;
399 if (fmt) {
400 error_report("--image-opts and --format are mutually exclusive");
401 return NULL;
402 }
403 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
404 filename, true);
405 if (!opts) {
406 return NULL;
407 }
Fam Zheng335e9932017-05-03 00:35:39 +0800408 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
409 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000410 } else {
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100411 blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
Fam Zheng335e9932017-05-03 00:35:39 +0800412 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000413 }
414 return blk;
415}
416
417
Chunyan Liu83d05212014-06-05 17:20:51 +0800418static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100419 const char *base_filename,
420 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200421{
Markus Armbruster6750e792015-02-12 17:43:08 +0100422 Error *err = NULL;
423
Kevin Wolfefa84d42009-05-18 16:42:12 +0200424 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100425 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100426 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100427 error_report("Backing file not supported for file format '%s'",
428 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100429 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900430 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200431 }
432 }
433 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100434 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100435 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100436 error_report("Backing file format not supported for file "
437 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100438 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900439 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200440 }
441 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900442 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200443}
444
Markus Armbruster606caa02017-02-21 21:14:04 +0100445static int64_t cvtnum(const char *s)
446{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100447 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100448 uint64_t value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100449
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100450 err = qemu_strtosz(s, NULL, &value);
451 if (err < 0) {
452 return err;
453 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100454 if (value > INT64_MAX) {
455 return -ERANGE;
456 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100457 return value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100458}
459
bellardea2384d2004-08-01 21:59:26 +0000460static int img_create(int argc, char **argv)
461{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200462 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100463 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000464 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000465 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000466 const char *filename;
467 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200468 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200469 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100470 bool quiet = false;
John Snow6e6e55f2017-07-17 20:34:22 -0400471 int flags = 0;
ths3b46e622007-09-17 08:09:54 +0000472
bellardea2384d2004-08-01 21:59:26 +0000473 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000474 static const struct option long_options[] = {
475 {"help", no_argument, 0, 'h'},
476 {"object", required_argument, 0, OPTION_OBJECT},
477 {0, 0, 0, 0}
478 };
John Snow6e6e55f2017-07-17 20:34:22 -0400479 c = getopt_long(argc, argv, ":F:b:f:ho:qu",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000480 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100481 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000482 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100483 }
bellardea2384d2004-08-01 21:59:26 +0000484 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800485 case ':':
486 missing_argument(argv[optind - 1]);
487 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100488 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800489 unrecognized_option(argv[optind - 1]);
490 break;
bellardea2384d2004-08-01 21:59:26 +0000491 case 'h':
492 help();
493 break;
aliguori9230eaf2009-03-28 17:55:19 +0000494 case 'F':
495 base_fmt = optarg;
496 break;
bellardea2384d2004-08-01 21:59:26 +0000497 case 'b':
498 base_filename = optarg;
499 break;
500 case 'f':
501 fmt = optarg;
502 break;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200503 case 'o':
Markus Armbruster6d2b5cb2020-04-15 09:49:25 +0200504 if (accumulate_options(&options, optarg) < 0) {
Kevin Wolf77386bf2014-02-21 16:24:04 +0100505 goto fail;
506 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200507 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100508 case 'q':
509 quiet = true;
510 break;
John Snow6e6e55f2017-07-17 20:34:22 -0400511 case 'u':
512 flags |= BDRV_O_NO_BACKING;
513 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000514 case OPTION_OBJECT: {
515 QemuOpts *opts;
516 opts = qemu_opts_parse_noisily(&qemu_object_opts,
517 optarg, true);
518 if (!opts) {
519 goto fail;
520 }
521 } break;
bellardea2384d2004-08-01 21:59:26 +0000522 }
523 }
aliguori9230eaf2009-03-28 17:55:19 +0000524
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900525 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100526 filename = (optind < argc) ? argv[optind] : NULL;
527 if (options && has_help_option(options)) {
528 g_free(options);
529 return print_block_option_help(filename, fmt);
530 }
531
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100532 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800533 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100534 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100535 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900536
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000537 if (qemu_opts_foreach(&qemu_object_opts,
538 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +0200539 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000540 goto fail;
541 }
542
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100543 /* Get image size, if specified */
544 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100545 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100546
547 sval = cvtnum(argv[optind++]);
548 if (sval < 0) {
liguang79443392012-12-17 09:49:23 +0800549 if (sval == -ERANGE) {
550 error_report("Image size must be less than 8 EiB!");
551 } else {
552 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200553 "G, T, P or E suffixes for ");
554 error_report("kilobytes, megabytes, gigabytes, terabytes, "
555 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800556 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100557 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100558 }
559 img_size = (uint64_t)sval;
560 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200561 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800562 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200563 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100564
Luiz Capitulino9b375252012-11-30 10:52:05 -0200565 bdrv_img_create(filename, fmt, base_filename, base_fmt,
John Snow6e6e55f2017-07-17 20:34:22 -0400566 options, img_size, flags, quiet, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100567 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100568 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100569 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900570 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200571
Kevin Wolf77386bf2014-02-21 16:24:04 +0100572 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000573 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100574
575fail:
576 g_free(options);
577 return 1;
bellardea2384d2004-08-01 21:59:26 +0000578}
579
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100580static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500581{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500582 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500583 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100584 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600585
586 visit_type_ImageCheck(v, NULL, &check, &error_abort);
587 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500588 str = qobject_to_json_pretty(obj);
589 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100590 qprintf(quiet, "%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200591 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600592 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200593 qobject_unref(str);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500594}
595
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100596static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500597{
598 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100599 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500600 } else {
601 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100602 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
603 "Data may be corrupted, or further writes to the image "
604 "may corrupt it.\n",
605 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500606 }
607
608 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100609 qprintf(quiet,
610 "\n%" PRId64 " leaked clusters were found on the image.\n"
611 "This means waste of disk space, but no harm to data.\n",
612 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500613 }
614
615 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100616 qprintf(quiet,
617 "\n%" PRId64
618 " internal errors have occurred during the check.\n",
619 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500620 }
621 }
622
623 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100624 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
625 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
626 check->allocated_clusters, check->total_clusters,
627 check->allocated_clusters * 100.0 / check->total_clusters,
628 check->fragmented_clusters * 100.0 / check->allocated_clusters,
629 check->compressed_clusters * 100.0 /
630 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500631 }
632
633 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100634 qprintf(quiet,
635 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500636 }
637}
638
639static int collect_image_check(BlockDriverState *bs,
640 ImageCheck *check,
641 const char *filename,
642 const char *fmt,
643 int fix)
644{
645 int ret;
646 BdrvCheckResult result;
647
648 ret = bdrv_check(bs, &result, fix);
649 if (ret < 0) {
650 return ret;
651 }
652
653 check->filename = g_strdup(filename);
654 check->format = g_strdup(bdrv_get_format_name(bs));
655 check->check_errors = result.check_errors;
656 check->corruptions = result.corruptions;
657 check->has_corruptions = result.corruptions != 0;
658 check->leaks = result.leaks;
659 check->has_leaks = result.leaks != 0;
660 check->corruptions_fixed = result.corruptions_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100661 check->has_corruptions_fixed = result.corruptions_fixed != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500662 check->leaks_fixed = result.leaks_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100663 check->has_leaks_fixed = result.leaks_fixed != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500664 check->image_end_offset = result.image_end_offset;
665 check->has_image_end_offset = result.image_end_offset != 0;
666 check->total_clusters = result.bfi.total_clusters;
667 check->has_total_clusters = result.bfi.total_clusters != 0;
668 check->allocated_clusters = result.bfi.allocated_clusters;
669 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
670 check->fragmented_clusters = result.bfi.fragmented_clusters;
671 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100672 check->compressed_clusters = result.bfi.compressed_clusters;
673 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500674
675 return 0;
676}
677
Kevin Wolfe076f332010-06-29 11:43:13 +0200678/*
679 * Checks an image for consistency. Exit codes:
680 *
Max Reitzd6635c42014-06-02 22:15:21 +0200681 * 0 - Check completed, image is good
682 * 1 - Check not completed because of internal errors
683 * 2 - Check completed, image is corrupted
684 * 3 - Check completed, image has leaked clusters, but is good otherwise
685 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200686 */
aliguori15859692009-04-21 23:11:53 +0000687static int img_check(int argc, char **argv)
688{
689 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500690 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200691 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200692 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000693 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200694 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100695 int flags = BDRV_O_CHECK;
696 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200697 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100698 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000699 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +0800700 bool force_share = false;
aliguori15859692009-04-21 23:11:53 +0000701
702 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500703 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200704 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100705
aliguori15859692009-04-21 23:11:53 +0000706 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500707 int option_index = 0;
708 static const struct option long_options[] = {
709 {"help", no_argument, 0, 'h'},
710 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530711 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500712 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000713 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000714 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +0800715 {"force-share", no_argument, 0, 'U'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500716 {0, 0, 0, 0}
717 };
Fam Zheng335e9932017-05-03 00:35:39 +0800718 c = getopt_long(argc, argv, ":hf:r:T:qU",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500719 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100720 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000721 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100722 }
aliguori15859692009-04-21 23:11:53 +0000723 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800724 case ':':
725 missing_argument(argv[optind - 1]);
726 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100727 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800728 unrecognized_option(argv[optind - 1]);
729 break;
aliguori15859692009-04-21 23:11:53 +0000730 case 'h':
731 help();
732 break;
733 case 'f':
734 fmt = optarg;
735 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200736 case 'r':
737 flags |= BDRV_O_RDWR;
738
739 if (!strcmp(optarg, "leaks")) {
740 fix = BDRV_FIX_LEAKS;
741 } else if (!strcmp(optarg, "all")) {
742 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
743 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800744 error_exit("Unknown option value for -r "
745 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200746 }
747 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500748 case OPTION_OUTPUT:
749 output = optarg;
750 break;
Max Reitz40055952014-07-22 22:58:42 +0200751 case 'T':
752 cache = optarg;
753 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100754 case 'q':
755 quiet = true;
756 break;
Fam Zheng335e9932017-05-03 00:35:39 +0800757 case 'U':
758 force_share = true;
759 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000760 case OPTION_OBJECT: {
761 QemuOpts *opts;
762 opts = qemu_opts_parse_noisily(&qemu_object_opts,
763 optarg, true);
764 if (!opts) {
765 return 1;
766 }
767 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000768 case OPTION_IMAGE_OPTS:
769 image_opts = true;
770 break;
aliguori15859692009-04-21 23:11:53 +0000771 }
772 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200773 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800774 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100775 }
aliguori15859692009-04-21 23:11:53 +0000776 filename = argv[optind++];
777
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500778 if (output && !strcmp(output, "json")) {
779 output_format = OFORMAT_JSON;
780 } else if (output && !strcmp(output, "human")) {
781 output_format = OFORMAT_HUMAN;
782 } else if (output) {
783 error_report("--output must be used with human or json as argument.");
784 return 1;
785 }
786
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000787 if (qemu_opts_foreach(&qemu_object_opts,
788 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +0200789 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000790 return 1;
791 }
792
Kevin Wolfce099542016-03-15 13:01:04 +0100793 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200794 if (ret < 0) {
795 error_report("Invalid source cache option: %s", cache);
796 return 1;
797 }
798
Fam Zheng335e9932017-05-03 00:35:39 +0800799 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
800 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200801 if (!blk) {
802 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900803 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200804 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500805
806 check = g_new0(ImageCheck, 1);
807 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200808
809 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200810 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200811 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500812 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200813 }
814
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500815 if (check->corruptions_fixed || check->leaks_fixed) {
816 int corruptions_fixed, leaks_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100817 bool has_leaks_fixed, has_corruptions_fixed;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500818
819 leaks_fixed = check->leaks_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100820 has_leaks_fixed = check->has_leaks_fixed;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500821 corruptions_fixed = check->corruptions_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100822 has_corruptions_fixed = check->has_corruptions_fixed;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500823
824 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100825 qprintf(quiet,
826 "The following inconsistencies were found and repaired:\n\n"
827 " %" PRId64 " leaked clusters\n"
828 " %" PRId64 " corruptions\n\n"
829 "Double checking the fixed image now...\n",
830 check->leaks_fixed,
831 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500832 }
833
Pan Nengyuanfc124ea2020-02-27 09:29:50 +0800834 qapi_free_ImageCheck(check);
835 check = g_new0(ImageCheck, 1);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500836 ret = collect_image_check(bs, check, filename, fmt, 0);
837
838 check->leaks_fixed = leaks_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100839 check->has_leaks_fixed = has_leaks_fixed;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500840 check->corruptions_fixed = corruptions_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100841 check->has_corruptions_fixed = has_corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200842 }
843
Max Reitz832390a2014-10-23 15:29:12 +0200844 if (!ret) {
845 switch (output_format) {
846 case OFORMAT_HUMAN:
847 dump_human_image_check(check, quiet);
848 break;
849 case OFORMAT_JSON:
850 dump_json_image_check(check, quiet);
851 break;
852 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500853 }
854
855 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200856 if (ret) {
857 error_report("Check failed: %s", strerror(-ret));
858 } else {
859 error_report("Check failed");
860 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500861 ret = 1;
862 goto fail;
863 }
864
865 if (check->corruptions) {
866 ret = 2;
867 } else if (check->leaks) {
868 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200869 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500870 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000871 }
872
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500873fail:
874 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200875 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500876 return ret;
aliguori15859692009-04-21 23:11:53 +0000877}
878
Max Reitzd4a32382014-10-24 15:57:37 +0200879typedef struct CommonBlockJobCBInfo {
880 BlockDriverState *bs;
881 Error **errp;
882} CommonBlockJobCBInfo;
883
884static void common_block_job_cb(void *opaque, int ret)
885{
886 CommonBlockJobCBInfo *cbi = opaque;
887
888 if (ret < 0) {
889 error_setg_errno(cbi->errp, -ret, "Block job failed");
890 }
Max Reitzd4a32382014-10-24 15:57:37 +0200891}
892
893static void run_block_job(BlockJob *job, Error **errp)
894{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200895 AioContext *aio_context = blk_get_aio_context(job->blk);
sochin.jiang4172a002017-06-15 14:47:33 +0800896 int ret = 0;
Max Reitzd4a32382014-10-24 15:57:37 +0200897
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200898 aio_context_acquire(aio_context);
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200899 job_ref(&job->job);
Max Reitzd4a32382014-10-24 15:57:37 +0200900 do {
Kevin Wolf30a5c882018-05-04 12:17:20 +0200901 float progress = 0.0f;
Max Reitzd4a32382014-10-24 15:57:37 +0200902 aio_poll(aio_context, true);
Vladimir Sementsov-Ogievskiy01fe1ca2020-03-11 13:29:56 +0300903 if (job->job.progress.total) {
904 progress = (float)job->job.progress.current /
905 job->job.progress.total * 100.f;
Kevin Wolf30a5c882018-05-04 12:17:20 +0200906 }
907 qemu_progress_print(progress, 0);
Kevin Wolfdf956ae2018-04-25 15:09:58 +0200908 } while (!job_is_ready(&job->job) && !job_is_completed(&job->job));
Max Reitzd4a32382014-10-24 15:57:37 +0200909
Kevin Wolfdbe5e6c2018-04-19 13:04:01 +0200910 if (!job_is_completed(&job->job)) {
Kevin Wolf3d70ff52018-04-24 16:13:52 +0200911 ret = job_complete_sync(&job->job, errp);
sochin.jiang4172a002017-06-15 14:47:33 +0800912 } else {
Kevin Wolf4ad35182018-04-19 17:30:16 +0200913 ret = job->job.ret;
sochin.jiang4172a002017-06-15 14:47:33 +0800914 }
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200915 job_unref(&job->job);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200916 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200917
sochin.jiang4172a002017-06-15 14:47:33 +0800918 /* publish completion progress only when success */
919 if (!ret) {
920 qemu_progress_print(100.f, 0);
921 }
Max Reitzd4a32382014-10-24 15:57:37 +0200922}
923
bellardea2384d2004-08-01 21:59:26 +0000924static int img_commit(int argc, char **argv)
925{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400926 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200927 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200928 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200929 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100930 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200931 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100932 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200933 Error *local_err = NULL;
934 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000935 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200936 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000937
938 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400939 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200940 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000941 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000942 static const struct option long_options[] = {
943 {"help", no_argument, 0, 'h'},
944 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000945 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000946 {0, 0, 0, 0}
947 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800948 c = getopt_long(argc, argv, ":f:ht:b:dpq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000949 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100950 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000951 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100952 }
bellardea2384d2004-08-01 21:59:26 +0000953 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800954 case ':':
955 missing_argument(argv[optind - 1]);
956 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100957 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800958 unrecognized_option(argv[optind - 1]);
959 break;
bellardea2384d2004-08-01 21:59:26 +0000960 case 'h':
961 help();
962 break;
963 case 'f':
964 fmt = optarg;
965 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400966 case 't':
967 cache = optarg;
968 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200969 case 'b':
970 base = optarg;
971 /* -b implies -d */
972 drop = true;
973 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200974 case 'd':
975 drop = true;
976 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200977 case 'p':
978 progress = true;
979 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100980 case 'q':
981 quiet = true;
982 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000983 case OPTION_OBJECT: {
984 QemuOpts *opts;
985 opts = qemu_opts_parse_noisily(&qemu_object_opts,
986 optarg, true);
987 if (!opts) {
988 return 1;
989 }
990 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000991 case OPTION_IMAGE_OPTS:
992 image_opts = true;
993 break;
bellardea2384d2004-08-01 21:59:26 +0000994 }
995 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200996
997 /* Progress is not shown in Quiet mode */
998 if (quiet) {
999 progress = false;
1000 }
1001
Kevin Wolffc11eb22013-08-05 10:53:04 +02001002 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001003 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001004 }
bellardea2384d2004-08-01 21:59:26 +00001005 filename = argv[optind++];
1006
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001007 if (qemu_opts_foreach(&qemu_object_opts,
1008 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02001009 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001010 return 1;
1011 }
1012
Max Reitz9a86fe42014-10-24 15:57:38 +02001013 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +01001014 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001015 if (ret < 0) {
1016 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +01001017 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001018 }
1019
Fam Zheng335e9932017-05-03 00:35:39 +08001020 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
1021 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001022 if (!blk) {
1023 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001024 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001025 bs = blk_bs(blk);
1026
Max Reitz687fa1d2014-10-24 15:57:39 +02001027 qemu_progress_init(progress, 1.f);
1028 qemu_progress_print(0.f, 100);
1029
Max Reitz1b22bff2014-10-24 15:57:40 +02001030 if (base) {
1031 base_bs = bdrv_find_backing_image(bs, base);
1032 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +01001033 error_setg(&local_err,
1034 "Did not find '%s' in the backing chain of '%s'",
1035 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +02001036 goto done;
1037 }
1038 } else {
1039 /* This is different from QMP, which by default uses the deepest file in
1040 * the backing chain (i.e., the very base); however, the traditional
1041 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +02001042 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +02001043 if (!base_bs) {
1044 error_setg(&local_err, "Image does not have a backing file");
1045 goto done;
1046 }
bellardea2384d2004-08-01 21:59:26 +00001047 }
1048
Max Reitzd4a32382014-10-24 15:57:37 +02001049 cbi = (CommonBlockJobCBInfo){
1050 .errp = &local_err,
1051 .bs = bs,
1052 };
1053
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001054 aio_context = bdrv_get_aio_context(bs);
1055 aio_context_acquire(aio_context);
Kevin Wolfbb02b652018-04-19 17:54:56 +02001056 commit_active_start("commit", bs, base_bs, JOB_DEFAULT, 0,
Kevin Wolf0db832f2017-02-20 18:10:05 +01001057 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +08001058 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001059 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +02001060 if (local_err) {
1061 goto done;
1062 }
1063
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001064 /* When the block job completes, the BlockBackend reference will point to
1065 * the old backing file. In order to avoid that the top image is already
1066 * deleted, so we can still empty it afterwards, increment the reference
1067 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +02001068 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001069 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001070 }
1071
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001072 job = block_job_get("commit");
Liam Merwick2e2db262018-11-05 21:38:37 +00001073 assert(job);
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001074 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001075 if (local_err) {
1076 goto unref_backing;
1077 }
1078
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001079 if (!drop && bs->drv->bdrv_make_empty) {
1080 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001081 if (ret) {
1082 error_setg_errno(&local_err, -ret, "Could not empty %s",
1083 filename);
1084 goto unref_backing;
1085 }
1086 }
1087
1088unref_backing:
1089 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001090 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001091 }
Max Reitzd4a32382014-10-24 15:57:37 +02001092
1093done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001094 qemu_progress_end();
1095
Markus Armbruster26f54e92014-10-07 13:59:04 +02001096 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001097
1098 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001099 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001100 return 1;
1101 }
Max Reitzd4a32382014-10-24 15:57:37 +02001102
1103 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001104 return 0;
1105}
1106
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001107/*
Eric Blakedebb38a2017-10-11 22:47:11 -05001108 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1109 * of the first sector boundary within buf where the sector contains a
1110 * non-zero byte. This function is robust to a buffer that is not
1111 * sector-aligned.
1112 */
1113static int64_t find_nonzero(const uint8_t *buf, int64_t n)
1114{
1115 int64_t i;
1116 int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE);
1117
1118 for (i = 0; i < end; i += BDRV_SECTOR_SIZE) {
1119 if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) {
1120 return i;
1121 }
1122 }
1123 if (i < n && !buffer_is_zero(buf + i, n - end)) {
1124 return i;
1125 }
1126 return -1;
1127}
1128
1129/*
thsf58c7b32008-06-05 21:53:49 +00001130 * Returns true iff the first sector pointed to by 'buf' contains at least
1131 * a non-NUL byte.
1132 *
1133 * 'pnum' is set to the number of sectors (including and immediately following
1134 * the first one) that are known to be in the same allocated/unallocated state.
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001135 * The function will try to align the end offset to alignment boundaries so
1136 * that the request will at least end aligned and consequtive requests will
1137 * also start at an aligned offset.
thsf58c7b32008-06-05 21:53:49 +00001138 */
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001139static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum,
1140 int64_t sector_num, int alignment)
bellardea2384d2004-08-01 21:59:26 +00001141{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001142 bool is_zero;
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001143 int i, tail;
bellardea2384d2004-08-01 21:59:26 +00001144
1145 if (n <= 0) {
1146 *pnum = 0;
1147 return 0;
1148 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001149 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001150 for(i = 1; i < n; i++) {
1151 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001152 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001153 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001154 }
bellardea2384d2004-08-01 21:59:26 +00001155 }
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001156
1157 tail = (sector_num + i) & (alignment - 1);
1158 if (tail) {
1159 if (is_zero && i <= tail) {
1160 /* treat unallocated areas which only consist
1161 * of a small tail as allocated. */
1162 is_zero = false;
1163 }
1164 if (!is_zero) {
1165 /* align up end offset of allocated areas. */
1166 i += alignment - tail;
1167 i = MIN(i, n);
1168 } else {
1169 /* align down end offset of zero areas. */
1170 i -= tail;
1171 }
1172 }
bellardea2384d2004-08-01 21:59:26 +00001173 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001174 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001175}
1176
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001177/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001178 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1179 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1180 * breaking up write requests for only small sparse areas.
1181 */
1182static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001183 int min, int64_t sector_num, int alignment)
Kevin Wolfa22f1232011-08-26 15:27:13 +02001184{
1185 int ret;
1186 int num_checked, num_used;
1187
1188 if (n < min) {
1189 min = n;
1190 }
1191
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001192 ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment);
Kevin Wolfa22f1232011-08-26 15:27:13 +02001193 if (!ret) {
1194 return ret;
1195 }
1196
1197 num_used = *pnum;
1198 buf += BDRV_SECTOR_SIZE * *pnum;
1199 n -= *pnum;
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001200 sector_num += *pnum;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001201 num_checked = num_used;
1202
1203 while (n > 0) {
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001204 ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment);
Kevin Wolfa22f1232011-08-26 15:27:13 +02001205
1206 buf += BDRV_SECTOR_SIZE * *pnum;
1207 n -= *pnum;
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001208 sector_num += *pnum;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001209 num_checked += *pnum;
1210 if (ret) {
1211 num_used = num_checked;
1212 } else if (*pnum >= min) {
1213 break;
1214 }
1215 }
1216
1217 *pnum = num_used;
1218 return 1;
1219}
1220
1221/*
Eric Blakedc61cd32017-10-11 22:47:14 -05001222 * Compares two buffers sector by sector. Returns 0 if the first
1223 * sector of each buffer matches, non-zero otherwise.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001224 *
Eric Blakedc61cd32017-10-11 22:47:14 -05001225 * pnum is set to the sector-aligned size of the buffer prefix that
1226 * has the same matching status as the first sector.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001227 */
Eric Blakedc61cd32017-10-11 22:47:14 -05001228static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2,
1229 int64_t bytes, int64_t *pnum)
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001230{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001231 bool res;
Eric Blakedc61cd32017-10-11 22:47:14 -05001232 int64_t i = MIN(bytes, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001233
Eric Blakedc61cd32017-10-11 22:47:14 -05001234 assert(bytes > 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001235
Eric Blakedc61cd32017-10-11 22:47:14 -05001236 res = !!memcmp(buf1, buf2, i);
1237 while (i < bytes) {
1238 int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001239
Eric Blakedc61cd32017-10-11 22:47:14 -05001240 if (!!memcmp(buf1 + i, buf2 + i, len) != res) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001241 break;
1242 }
Eric Blakedc61cd32017-10-11 22:47:14 -05001243 i += len;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001244 }
1245
1246 *pnum = i;
1247 return res;
1248}
1249
Stefano Garzarella97ede572019-05-08 12:43:24 +02001250#define IO_BUF_SIZE (2 * MiB)
bellardea2384d2004-08-01 21:59:26 +00001251
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001252/*
1253 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1254 *
Eric Blake0608e402017-10-11 22:47:12 -05001255 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1256 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1257 * failure), and 4 on error (the exit status for read errors), after emitting
1258 * an error message.
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001259 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001260 * @param blk: BlockBackend for the image
Eric Blakec41508e2017-10-11 22:47:13 -05001261 * @param offset: Starting offset to check
1262 * @param bytes: Number of bytes to check
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001263 * @param filename: Name of disk file we are checking (logging purpose)
1264 * @param buffer: Allocated buffer for storing read data
1265 * @param quiet: Flag for quiet mode
1266 */
Eric Blakec41508e2017-10-11 22:47:13 -05001267static int check_empty_sectors(BlockBackend *blk, int64_t offset,
1268 int64_t bytes, const char *filename,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001269 uint8_t *buffer, bool quiet)
1270{
Eric Blakedebb38a2017-10-11 22:47:11 -05001271 int ret = 0;
1272 int64_t idx;
1273
Eric Blakec41508e2017-10-11 22:47:13 -05001274 ret = blk_pread(blk, offset, buffer, bytes);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001275 if (ret < 0) {
1276 error_report("Error while reading offset %" PRId64 " of %s: %s",
Eric Blakec41508e2017-10-11 22:47:13 -05001277 offset, filename, strerror(-ret));
Eric Blake0608e402017-10-11 22:47:12 -05001278 return 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001279 }
Eric Blakec41508e2017-10-11 22:47:13 -05001280 idx = find_nonzero(buffer, bytes);
Eric Blakedebb38a2017-10-11 22:47:11 -05001281 if (idx >= 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001282 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blakec41508e2017-10-11 22:47:13 -05001283 offset + idx);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001284 return 1;
1285 }
1286
1287 return 0;
1288}
1289
1290/*
1291 * Compares two images. Exit codes:
1292 *
1293 * 0 - Images are identical
1294 * 1 - Images differ
1295 * >1 - Error occurred
1296 */
1297static int img_compare(int argc, char **argv)
1298{
Max Reitz40055952014-07-22 22:58:42 +02001299 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001300 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001301 BlockDriverState *bs1, *bs2;
Eric Blake033d9fc2017-10-11 22:47:16 -05001302 int64_t total_size1, total_size2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001303 uint8_t *buf1 = NULL, *buf2 = NULL;
Eric Blake31826642017-10-11 22:47:08 -05001304 int64_t pnum1, pnum2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001305 int allocated1, allocated2;
1306 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1307 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001308 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001309 bool writethrough;
Eric Blake033d9fc2017-10-11 22:47:16 -05001310 int64_t total_size;
1311 int64_t offset = 0;
1312 int64_t chunk;
Eric Blakedc61cd32017-10-11 22:47:14 -05001313 int c;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001314 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001315 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001316 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001317
Max Reitz40055952014-07-22 22:58:42 +02001318 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001319 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001320 static const struct option long_options[] = {
1321 {"help", no_argument, 0, 'h'},
1322 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001323 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001324 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001325 {0, 0, 0, 0}
1326 };
Fam Zheng335e9932017-05-03 00:35:39 +08001327 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001328 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001329 if (c == -1) {
1330 break;
1331 }
1332 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001333 case ':':
1334 missing_argument(argv[optind - 1]);
1335 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001336 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001337 unrecognized_option(argv[optind - 1]);
1338 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001339 case 'h':
1340 help();
1341 break;
1342 case 'f':
1343 fmt1 = optarg;
1344 break;
1345 case 'F':
1346 fmt2 = optarg;
1347 break;
Max Reitz40055952014-07-22 22:58:42 +02001348 case 'T':
1349 cache = optarg;
1350 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001351 case 'p':
1352 progress = true;
1353 break;
1354 case 'q':
1355 quiet = true;
1356 break;
1357 case 's':
1358 strict = true;
1359 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001360 case 'U':
1361 force_share = true;
1362 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001363 case OPTION_OBJECT: {
1364 QemuOpts *opts;
1365 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1366 optarg, true);
1367 if (!opts) {
1368 ret = 2;
1369 goto out4;
1370 }
1371 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001372 case OPTION_IMAGE_OPTS:
1373 image_opts = true;
1374 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001375 }
1376 }
1377
1378 /* Progress is not shown in Quiet mode */
1379 if (quiet) {
1380 progress = false;
1381 }
1382
1383
Kevin Wolffc11eb22013-08-05 10:53:04 +02001384 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001385 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001386 }
1387 filename1 = argv[optind++];
1388 filename2 = argv[optind++];
1389
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001390 if (qemu_opts_foreach(&qemu_object_opts,
1391 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02001392 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001393 ret = 2;
1394 goto out4;
1395 }
1396
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001397 /* Initialize before goto out */
1398 qemu_progress_init(progress, 2.0);
1399
Kevin Wolfce099542016-03-15 13:01:04 +01001400 flags = 0;
1401 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001402 if (ret < 0) {
1403 error_report("Invalid source cache option: %s", cache);
1404 ret = 2;
1405 goto out3;
1406 }
1407
Fam Zheng335e9932017-05-03 00:35:39 +08001408 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1409 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001410 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001411 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001412 goto out3;
1413 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001414
Fam Zheng335e9932017-05-03 00:35:39 +08001415 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1416 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001417 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001418 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001419 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001420 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001421 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001422 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001423
Max Reitzf1d3cd72015-02-05 13:58:18 -05001424 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1425 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
Eric Blake033d9fc2017-10-11 22:47:16 -05001426 total_size1 = blk_getlength(blk1);
1427 if (total_size1 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001428 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001429 filename1, strerror(-total_size1));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001430 ret = 4;
1431 goto out;
1432 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001433 total_size2 = blk_getlength(blk2);
1434 if (total_size2 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001435 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001436 filename2, strerror(-total_size2));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001437 ret = 4;
1438 goto out;
1439 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001440 total_size = MIN(total_size1, total_size2);
1441 progress_base = MAX(total_size1, total_size2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001442
1443 qemu_progress_print(0, 100);
1444
Eric Blake033d9fc2017-10-11 22:47:16 -05001445 if (strict && total_size1 != total_size2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001446 ret = 1;
1447 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1448 goto out;
1449 }
1450
Eric Blake033d9fc2017-10-11 22:47:16 -05001451 while (offset < total_size) {
Eric Blake31826642017-10-11 22:47:08 -05001452 int status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001453
Eric Blake033d9fc2017-10-11 22:47:16 -05001454 status1 = bdrv_block_status_above(bs1, NULL, offset,
1455 total_size1 - offset, &pnum1, NULL,
1456 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001457 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001458 ret = 3;
1459 error_report("Sector allocation test failed for %s", filename1);
1460 goto out;
1461 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001462 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001463
Eric Blake033d9fc2017-10-11 22:47:16 -05001464 status2 = bdrv_block_status_above(bs2, NULL, offset,
1465 total_size2 - offset, &pnum2, NULL,
1466 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001467 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001468 ret = 3;
1469 error_report("Sector allocation test failed for %s", filename2);
1470 goto out;
1471 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001472 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
Eric Blake7daddc62017-10-11 22:47:09 -05001473
1474 assert(pnum1 && pnum2);
Eric Blake033d9fc2017-10-11 22:47:16 -05001475 chunk = MIN(pnum1, pnum2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001476
Fam Zheng25ad8e62016-01-13 16:37:41 +08001477 if (strict) {
Eric Blake31826642017-10-11 22:47:08 -05001478 if (status1 != status2) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001479 ret = 1;
1480 qprintf(quiet, "Strict mode: Offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001481 " block status mismatch!\n", offset);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001482 goto out;
1483 }
1484 }
1485 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
Eric Blake7daddc62017-10-11 22:47:09 -05001486 /* nothing to do */
Fam Zheng25ad8e62016-01-13 16:37:41 +08001487 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001488 if (allocated1) {
Eric Blakedc61cd32017-10-11 22:47:14 -05001489 int64_t pnum;
1490
Eric Blake033d9fc2017-10-11 22:47:16 -05001491 chunk = MIN(chunk, IO_BUF_SIZE);
1492 ret = blk_pread(blk1, offset, buf1, chunk);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001493 if (ret < 0) {
1494 error_report("Error while reading offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001495 " of %s: %s",
1496 offset, filename1, strerror(-ret));
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001497 ret = 4;
1498 goto out;
1499 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001500 ret = blk_pread(blk2, offset, buf2, chunk);
1501 if (ret < 0) {
1502 error_report("Error while reading offset %" PRId64
1503 " of %s: %s",
1504 offset, filename2, strerror(-ret));
1505 ret = 4;
1506 goto out;
1507 }
1508 ret = compare_buffers(buf1, buf2, chunk, &pnum);
1509 if (ret || pnum != chunk) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001510 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blake033d9fc2017-10-11 22:47:16 -05001511 offset + (ret ? 0 : pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001512 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001513 goto out;
1514 }
1515 }
1516 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001517 chunk = MIN(chunk, IO_BUF_SIZE);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001518 if (allocated1) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001519 ret = check_empty_sectors(blk1, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001520 filename1, buf1, quiet);
1521 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001522 ret = check_empty_sectors(blk2, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001523 filename2, buf1, quiet);
1524 }
1525 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001526 goto out;
1527 }
1528 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001529 offset += chunk;
1530 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001531 }
1532
Eric Blake033d9fc2017-10-11 22:47:16 -05001533 if (total_size1 != total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001534 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001535 const char *filename_over;
1536
1537 qprintf(quiet, "Warning: Image size mismatch!\n");
Eric Blake033d9fc2017-10-11 22:47:16 -05001538 if (total_size1 > total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001539 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001540 filename_over = filename1;
1541 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001542 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001543 filename_over = filename2;
1544 }
1545
Eric Blake033d9fc2017-10-11 22:47:16 -05001546 while (offset < progress_base) {
1547 ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset,
1548 progress_base - offset, &chunk,
1549 NULL, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001550 if (ret < 0) {
1551 ret = 3;
1552 error_report("Sector allocation test failed for %s",
1553 filename_over);
1554 goto out;
1555
1556 }
Eric Blake391cb1a2017-10-11 22:47:10 -05001557 if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001558 chunk = MIN(chunk, IO_BUF_SIZE);
1559 ret = check_empty_sectors(blk_over, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001560 filename_over, buf1, quiet);
1561 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001562 goto out;
1563 }
1564 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001565 offset += chunk;
1566 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001567 }
1568 }
1569
1570 qprintf(quiet, "Images are identical.\n");
1571 ret = 0;
1572
1573out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001574 qemu_vfree(buf1);
1575 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001576 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001577out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001578 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001579out3:
1580 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001581out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001582 return ret;
1583}
1584
Kevin Wolf690c7302015-03-19 13:33:32 +01001585enum ImgConvertBlockStatus {
1586 BLK_DATA,
1587 BLK_ZERO,
1588 BLK_BACKING_FILE,
1589};
1590
Peter Lieven2d9187b2017-02-28 13:40:07 +01001591#define MAX_COROUTINES 16
1592
Kevin Wolf690c7302015-03-19 13:33:32 +01001593typedef struct ImgConvertState {
1594 BlockBackend **src;
1595 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001596 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001597 int64_t total_sectors;
1598 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001599 int64_t allocated_done;
1600 int64_t sector_num;
1601 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001602 enum ImgConvertBlockStatus status;
1603 int64_t sector_next_status;
1604 BlockBackend *target;
1605 bool has_zero_init;
1606 bool compressed;
Max Reitz351c8ef2018-05-01 18:57:49 +02001607 bool unallocated_blocks_are_zero;
Max Reitz4d7c4872019-07-24 19:12:29 +02001608 bool target_is_new;
Kevin Wolf690c7302015-03-19 13:33:32 +01001609 bool target_has_backing;
Max Reitz351c8ef2018-05-01 18:57:49 +02001610 int64_t target_backing_sectors; /* negative if unknown */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001611 bool wr_in_order;
Fam Zhengee5306d2018-06-01 17:26:48 +08001612 bool copy_range;
Max Reitz8eaac022019-05-07 22:35:03 +02001613 bool salvage;
Max Reitz3d96cb92019-05-07 22:35:02 +02001614 bool quiet;
Kevin Wolf690c7302015-03-19 13:33:32 +01001615 int min_sparse;
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001616 int alignment;
Kevin Wolf690c7302015-03-19 13:33:32 +01001617 size_t cluster_sectors;
1618 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001619 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001620 int running_coroutines;
1621 Coroutine *co[MAX_COROUTINES];
1622 int64_t wait_sector_num[MAX_COROUTINES];
1623 CoMutex lock;
1624 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001625} ImgConvertState;
1626
Peter Lieven2d9187b2017-02-28 13:40:07 +01001627static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1628 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001629{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001630 *src_cur = 0;
1631 *src_cur_offset = 0;
1632 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1633 *src_cur_offset += s->src_sectors[*src_cur];
1634 (*src_cur)++;
1635 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001636 }
1637}
1638
1639static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1640{
Eric Blake31826642017-10-11 22:47:08 -05001641 int64_t src_cur_offset;
1642 int ret, n, src_cur;
Max Reitz351c8ef2018-05-01 18:57:49 +02001643 bool post_backing_zero = false;
Kevin Wolf690c7302015-03-19 13:33:32 +01001644
Peter Lieven2d9187b2017-02-28 13:40:07 +01001645 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001646
1647 assert(s->total_sectors > sector_num);
1648 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1649
Max Reitz351c8ef2018-05-01 18:57:49 +02001650 if (s->target_backing_sectors >= 0) {
1651 if (sector_num >= s->target_backing_sectors) {
1652 post_backing_zero = s->unallocated_blocks_are_zero;
1653 } else if (sector_num + n > s->target_backing_sectors) {
1654 /* Split requests around target_backing_sectors (because
1655 * starting from there, zeros are handled differently) */
1656 n = s->target_backing_sectors - sector_num;
1657 }
1658 }
1659
Kevin Wolf690c7302015-03-19 13:33:32 +01001660 if (s->sector_next_status <= sector_num) {
Max Reitz8eaac022019-05-07 22:35:03 +02001661 uint64_t offset = (sector_num - src_cur_offset) * BDRV_SECTOR_SIZE;
1662 int64_t count;
Eric Blake31826642017-10-11 22:47:08 -05001663
Max Reitz8eaac022019-05-07 22:35:03 +02001664 do {
1665 count = n * BDRV_SECTOR_SIZE;
Eric Blake237d78f2017-10-11 22:47:03 -05001666
Max Reitz8eaac022019-05-07 22:35:03 +02001667 if (s->target_has_backing) {
1668 ret = bdrv_block_status(blk_bs(s->src[src_cur]), offset,
1669 count, &count, NULL, NULL);
1670 } else {
1671 ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL,
1672 offset, count, &count, NULL,
1673 NULL);
1674 }
1675
1676 if (ret < 0) {
1677 if (s->salvage) {
1678 if (n == 1) {
1679 if (!s->quiet) {
1680 warn_report("error while reading block status at "
1681 "offset %" PRIu64 ": %s", offset,
1682 strerror(-ret));
1683 }
1684 /* Just try to read the data, then */
1685 ret = BDRV_BLOCK_DATA;
1686 count = BDRV_SECTOR_SIZE;
1687 } else {
1688 /* Retry on a shorter range */
1689 n = DIV_ROUND_UP(n, 4);
1690 }
1691 } else {
1692 error_report("error while reading block status at offset "
1693 "%" PRIu64 ": %s", offset, strerror(-ret));
1694 return ret;
1695 }
1696 }
1697 } while (ret < 0);
1698
Eric Blake31826642017-10-11 22:47:08 -05001699 n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
Kevin Wolf690c7302015-03-19 13:33:32 +01001700
1701 if (ret & BDRV_BLOCK_ZERO) {
Max Reitz351c8ef2018-05-01 18:57:49 +02001702 s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO;
Kevin Wolf690c7302015-03-19 13:33:32 +01001703 } else if (ret & BDRV_BLOCK_DATA) {
1704 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001705 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001706 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001707 }
1708
1709 s->sector_next_status = sector_num + n;
1710 }
1711
1712 n = MIN(n, s->sector_next_status - sector_num);
1713 if (s->status == BLK_DATA) {
1714 n = MIN(n, s->buf_sectors);
1715 }
1716
1717 /* We need to write complete clusters for compressed images, so if an
1718 * unallocated area is shorter than that, we must consider the whole
1719 * cluster allocated. */
1720 if (s->compressed) {
1721 if (n < s->cluster_sectors) {
1722 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1723 s->status = BLK_DATA;
1724 } else {
1725 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1726 }
1727 }
1728
1729 return n;
1730}
1731
Peter Lieven2d9187b2017-02-28 13:40:07 +01001732static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1733 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001734{
Max Reitz8eaac022019-05-07 22:35:03 +02001735 uint64_t single_read_until = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001736 int n, ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001737
Kevin Wolf690c7302015-03-19 13:33:32 +01001738 assert(nb_sectors <= s->buf_sectors);
1739 while (nb_sectors > 0) {
1740 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001741 int src_cur;
1742 int64_t bs_sectors, src_cur_offset;
Max Reitz8eaac022019-05-07 22:35:03 +02001743 uint64_t offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001744
1745 /* In the case of compression with multiple source files, we can get a
1746 * nb_sectors that spreads into the next part. So we must be able to
1747 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001748 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1749 blk = s->src[src_cur];
1750 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001751
Max Reitz8eaac022019-05-07 22:35:03 +02001752 offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001753
Max Reitz8eaac022019-05-07 22:35:03 +02001754 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1755 if (single_read_until > offset) {
1756 n = 1;
1757 }
1758
1759 ret = blk_co_pread(blk, offset, n << BDRV_SECTOR_BITS, buf, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001760 if (ret < 0) {
Max Reitz8eaac022019-05-07 22:35:03 +02001761 if (s->salvage) {
1762 if (n > 1) {
1763 single_read_until = offset + (n << BDRV_SECTOR_BITS);
1764 continue;
1765 } else {
1766 if (!s->quiet) {
1767 warn_report("error while reading offset %" PRIu64
1768 ": %s", offset, strerror(-ret));
1769 }
1770 memset(buf, 0, BDRV_SECTOR_SIZE);
1771 }
1772 } else {
1773 return ret;
1774 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001775 }
1776
1777 sector_num += n;
1778 nb_sectors -= n;
1779 buf += n * BDRV_SECTOR_SIZE;
1780 }
1781
1782 return 0;
1783}
1784
Peter Lieven2d9187b2017-02-28 13:40:07 +01001785
1786static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1787 int nb_sectors, uint8_t *buf,
1788 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001789{
1790 int ret;
1791
1792 while (nb_sectors > 0) {
1793 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001794 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1795
Peter Lieven2d9187b2017-02-28 13:40:07 +01001796 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001797 case BLK_BACKING_FILE:
1798 /* If we have a backing file, leave clusters unallocated that are
1799 * unallocated in the source image, so that the backing file is
1800 * visible at the respective offset. */
1801 assert(s->target_has_backing);
1802 break;
1803
1804 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001805 /* If we're told to keep the target fully allocated (-S 0) or there
1806 * is real non-zero data, we must write it. Otherwise we can treat
1807 * it as zero sectors.
1808 * Compressed clusters need to be written as a whole, so in that
1809 * case we can only save the write if the buffer is completely
1810 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001811 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001812 (!s->compressed &&
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001813 is_allocated_sectors_min(buf, n, &n, s->min_sparse,
1814 sector_num, s->alignment)) ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001815 (s->compressed &&
1816 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001817 {
Vladimir Sementsov-Ogievskiy265a7e52019-04-22 17:58:38 +03001818 ret = blk_co_pwrite(s->target, sector_num << BDRV_SECTOR_BITS,
1819 n << BDRV_SECTOR_BITS, buf, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001820 if (ret < 0) {
1821 return ret;
1822 }
1823 break;
1824 }
1825 /* fall-through */
1826
1827 case BLK_ZERO:
1828 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001829 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001830 break;
1831 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001832 ret = blk_co_pwrite_zeroes(s->target,
1833 sector_num << BDRV_SECTOR_BITS,
Nir Soffera3d6ae22019-03-24 02:20:12 +02001834 n << BDRV_SECTOR_BITS,
1835 BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001836 if (ret < 0) {
1837 return ret;
1838 }
1839 break;
1840 }
1841
1842 sector_num += n;
1843 nb_sectors -= n;
1844 buf += n * BDRV_SECTOR_SIZE;
1845 }
1846
1847 return 0;
1848}
1849
Fam Zhengee5306d2018-06-01 17:26:48 +08001850static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num,
1851 int nb_sectors)
1852{
1853 int n, ret;
1854
1855 while (nb_sectors > 0) {
1856 BlockBackend *blk;
1857 int src_cur;
1858 int64_t bs_sectors, src_cur_offset;
1859 int64_t offset;
1860
1861 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1862 offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS;
1863 blk = s->src[src_cur];
1864 bs_sectors = s->src_sectors[src_cur];
1865
1866 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1867
1868 ret = blk_co_copy_range(blk, offset, s->target,
1869 sector_num << BDRV_SECTOR_BITS,
Vladimir Sementsov-Ogievskiy67b51fb2018-07-09 19:37:17 +03001870 n << BDRV_SECTOR_BITS, 0, 0);
Fam Zhengee5306d2018-06-01 17:26:48 +08001871 if (ret < 0) {
1872 return ret;
1873 }
1874
1875 sector_num += n;
1876 nb_sectors -= n;
1877 }
1878 return 0;
1879}
1880
Peter Lieven2d9187b2017-02-28 13:40:07 +01001881static void coroutine_fn convert_co_do_copy(void *opaque)
1882{
1883 ImgConvertState *s = opaque;
1884 uint8_t *buf = NULL;
1885 int ret, i;
1886 int index = -1;
1887
1888 for (i = 0; i < s->num_coroutines; i++) {
1889 if (s->co[i] == qemu_coroutine_self()) {
1890 index = i;
1891 break;
1892 }
1893 }
1894 assert(index >= 0);
1895
1896 s->running_coroutines++;
1897 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1898
1899 while (1) {
1900 int n;
1901 int64_t sector_num;
1902 enum ImgConvertBlockStatus status;
Fam Zhengee5306d2018-06-01 17:26:48 +08001903 bool copy_range;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001904
1905 qemu_co_mutex_lock(&s->lock);
1906 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1907 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001908 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001909 }
1910 n = convert_iteration_sectors(s, s->sector_num);
1911 if (n < 0) {
1912 qemu_co_mutex_unlock(&s->lock);
1913 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001914 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001915 }
1916 /* save current sector and allocation status to local variables */
1917 sector_num = s->sector_num;
1918 status = s->status;
1919 if (!s->min_sparse && s->status == BLK_ZERO) {
1920 n = MIN(n, s->buf_sectors);
1921 }
1922 /* increment global sector counter so that other coroutines can
1923 * already continue reading beyond this request */
1924 s->sector_num += n;
1925 qemu_co_mutex_unlock(&s->lock);
1926
1927 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1928 s->allocated_done += n;
1929 qemu_progress_print(100.0 * s->allocated_done /
1930 s->allocated_sectors, 0);
1931 }
1932
Fam Zhengee5306d2018-06-01 17:26:48 +08001933retry:
1934 copy_range = s->copy_range && s->status == BLK_DATA;
1935 if (status == BLK_DATA && !copy_range) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001936 ret = convert_co_read(s, sector_num, n, buf);
1937 if (ret < 0) {
Eric Blake39f77cb2020-04-02 08:57:17 -05001938 error_report("error while reading at byte %lld: %s",
1939 sector_num * BDRV_SECTOR_SIZE, strerror(-ret));
Peter Lieven2d9187b2017-02-28 13:40:07 +01001940 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001941 }
1942 } else if (!s->min_sparse && status == BLK_ZERO) {
1943 status = BLK_DATA;
1944 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1945 }
1946
1947 if (s->wr_in_order) {
1948 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03001949 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001950 s->wait_sector_num[index] = sector_num;
1951 qemu_coroutine_yield();
1952 }
1953 s->wait_sector_num[index] = -1;
1954 }
1955
Anton Nefedovb91127e2017-04-26 11:33:15 +03001956 if (s->ret == -EINPROGRESS) {
Fam Zhengee5306d2018-06-01 17:26:48 +08001957 if (copy_range) {
1958 ret = convert_co_copy_range(s, sector_num, n);
1959 if (ret) {
1960 s->copy_range = false;
1961 goto retry;
1962 }
1963 } else {
1964 ret = convert_co_write(s, sector_num, n, buf, status);
1965 }
Anton Nefedovb91127e2017-04-26 11:33:15 +03001966 if (ret < 0) {
Eric Blake39f77cb2020-04-02 08:57:17 -05001967 error_report("error while writing at byte %lld: %s",
1968 sector_num * BDRV_SECTOR_SIZE, strerror(-ret));
Anton Nefedovb91127e2017-04-26 11:33:15 +03001969 s->ret = ret;
1970 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001971 }
1972
1973 if (s->wr_in_order) {
1974 /* reenter the coroutine that might have waited
1975 * for this write to complete */
1976 s->wr_offs = sector_num + n;
1977 for (i = 0; i < s->num_coroutines; i++) {
1978 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1979 /*
1980 * A -> B -> A cannot occur because A has
1981 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1982 * B will never enter A during this time window.
1983 */
1984 qemu_coroutine_enter(s->co[i]);
1985 break;
1986 }
1987 }
1988 }
1989 }
1990
Peter Lieven2d9187b2017-02-28 13:40:07 +01001991 qemu_vfree(buf);
1992 s->co[index] = NULL;
1993 s->running_coroutines--;
1994 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1995 /* the convert job finished successfully */
1996 s->ret = 0;
1997 }
1998}
1999
Kevin Wolf690c7302015-03-19 13:33:32 +01002000static int convert_do_copy(ImgConvertState *s)
2001{
Peter Lieven2d9187b2017-02-28 13:40:07 +01002002 int ret, i, n;
2003 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01002004
2005 /* Check whether we have zero initialisation or can get it efficiently */
David Edmondson168468f2020-02-05 11:02:48 +00002006 if (!s->has_zero_init && s->target_is_new && s->min_sparse &&
2007 !s->target_has_backing) {
Max Reitz4d7c4872019-07-24 19:12:29 +02002008 s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target));
Max Reitz4d7c4872019-07-24 19:12:29 +02002009 }
Kevin Wolf690c7302015-03-19 13:33:32 +01002010
2011 if (!s->has_zero_init && !s->target_has_backing &&
2012 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
2013 {
Kevin Wolfc9fdcf22019-03-22 13:49:28 +01002014 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK);
Kevin Wolf690c7302015-03-19 13:33:32 +01002015 if (ret == 0) {
2016 s->has_zero_init = true;
2017 }
2018 }
2019
2020 /* Allocate buffer for copied data. For compressed images, only one cluster
2021 * can be copied at a time. */
2022 if (s->compressed) {
2023 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
2024 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01002025 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01002026 }
2027 s->buf_sectors = s->cluster_sectors;
2028 }
Kevin Wolf690c7302015-03-19 13:33:32 +01002029
Kevin Wolf690c7302015-03-19 13:33:32 +01002030 while (sector_num < s->total_sectors) {
2031 n = convert_iteration_sectors(s, sector_num);
2032 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002033 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01002034 }
Max Reitzaad15de2016-03-24 23:33:57 +01002035 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
2036 {
Kevin Wolf690c7302015-03-19 13:33:32 +01002037 s->allocated_sectors += n;
2038 }
2039 sector_num += n;
2040 }
2041
2042 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01002043 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002044 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01002045
Peter Lieven2d9187b2017-02-28 13:40:07 +01002046 qemu_co_mutex_init(&s->lock);
2047 for (i = 0; i < s->num_coroutines; i++) {
2048 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
2049 s->wait_sector_num[i] = -1;
2050 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01002051 }
2052
Anton Nefedovb91127e2017-04-26 11:33:15 +03002053 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002054 main_loop_wait(false);
2055 }
2056
2057 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01002058 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03002059 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01002060 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002061 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01002062 }
2063 }
2064
Peter Lieven2d9187b2017-02-28 13:40:07 +01002065 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01002066}
2067
Peter Lieven6360ab22018-07-13 09:15:39 +02002068#define MAX_BUF_SECTORS 32768
2069
bellardea2384d2004-08-01 21:59:26 +00002070static int img_convert(int argc, char **argv)
2071{
Peter Lieven9fd77f92017-04-21 11:11:55 +02002072 int c, bs_i, flags, src_flags = 0;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002073 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002074 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
2075 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002076 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00002077 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002078 BlockDriverState *out_bs;
2079 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08002080 QemuOptsList *create_opts = NULL;
Daniel P. Berrangé8d65a3c2018-08-14 13:39:47 +01002081 QDict *open_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002082 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02002083 Error *local_err = NULL;
Max Reitz3d96cb92019-05-07 22:35:02 +02002084 bool writethrough, src_writethrough, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002085 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002086 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08002087 bool force_share = false;
Fam Zhenge11ce122018-07-27 11:34:01 +08002088 bool explict_min_sparse = false;
bellardea2384d2004-08-01 21:59:26 +00002089
Peter Lieven9fd77f92017-04-21 11:11:55 +02002090 ImgConvertState s = (ImgConvertState) {
2091 /* Need at least 4k of zeros for sparse detection */
2092 .min_sparse = 8,
Fam Zhenge11ce122018-07-27 11:34:01 +08002093 .copy_range = false,
Peter Lieven9fd77f92017-04-21 11:11:55 +02002094 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
2095 .wr_in_order = true,
2096 .num_coroutines = 8,
2097 };
2098
bellardea2384d2004-08-01 21:59:26 +00002099 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002100 static const struct option long_options[] = {
2101 {"help", no_argument, 0, 'h'},
2102 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002103 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002104 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002105 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Max Reitz8eaac022019-05-07 22:35:03 +02002106 {"salvage", no_argument, 0, OPTION_SALVAGE},
David Edmondson168468f2020-02-05 11:02:48 +00002107 {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002108 {0, 0, 0, 0}
2109 };
Fam Zhenge11ce122018-07-27 11:34:01 +08002110 c = getopt_long(argc, argv, ":hf:O:B:Cco:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002111 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002112 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002113 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002114 }
bellardea2384d2004-08-01 21:59:26 +00002115 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002116 case ':':
2117 missing_argument(argv[optind - 1]);
2118 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002119 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002120 unrecognized_option(argv[optind - 1]);
2121 break;
bellardea2384d2004-08-01 21:59:26 +00002122 case 'h':
2123 help();
2124 break;
2125 case 'f':
2126 fmt = optarg;
2127 break;
2128 case 'O':
2129 out_fmt = optarg;
2130 break;
thsf58c7b32008-06-05 21:53:49 +00002131 case 'B':
2132 out_baseimg = optarg;
2133 break;
Fam Zhenge11ce122018-07-27 11:34:01 +08002134 case 'C':
2135 s.copy_range = true;
2136 break;
bellardea2384d2004-08-01 21:59:26 +00002137 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002138 s.compressed = true;
bellardea2384d2004-08-01 21:59:26 +00002139 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002140 case 'o':
Markus Armbruster6d2b5cb2020-04-15 09:49:25 +02002141 if (accumulate_options(&options, optarg) < 0) {
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002142 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01002143 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002144 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08002145 case 'l':
2146 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01002147 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2148 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08002149 if (!sn_opts) {
2150 error_report("Failed in parsing snapshot param '%s'",
2151 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002152 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002153 }
2154 } else {
2155 snapshot_name = optarg;
2156 }
2157 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002158 case 'S':
2159 {
2160 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002161
2162 sval = cvtnum(optarg);
Nir Soffer1bbbf322019-08-27 21:59:12 +03002163 if (sval < 0 || !QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) ||
Peter Lieven6360ab22018-07-13 09:15:39 +02002164 sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) {
2165 error_report("Invalid buffer size for sparse output specified. "
2166 "Valid sizes are multiples of %llu up to %llu. Select "
2167 "0 to disable sparse detection (fully allocates output).",
2168 BDRV_SECTOR_SIZE, MAX_BUF_SECTORS * BDRV_SECTOR_SIZE);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002169 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002170 }
2171
Peter Lieven9fd77f92017-04-21 11:11:55 +02002172 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Fam Zhenge11ce122018-07-27 11:34:01 +08002173 explict_min_sparse = true;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002174 break;
2175 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002176 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002177 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002178 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002179 case 't':
2180 cache = optarg;
2181 break;
Max Reitz40055952014-07-22 22:58:42 +02002182 case 'T':
2183 src_cache = optarg;
2184 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002185 case 'q':
Max Reitz3d96cb92019-05-07 22:35:02 +02002186 s.quiet = true;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002187 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002188 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002189 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002190 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002191 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002192 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2193 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002194 error_report("Invalid number of coroutines. Allowed number of"
2195 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002196 goto fail_getopt;
2197 }
2198 break;
2199 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002200 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002201 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002202 case 'U':
2203 force_share = true;
2204 break;
Max Reitz3258b912017-04-26 15:46:47 +02002205 case OPTION_OBJECT: {
2206 QemuOpts *object_opts;
2207 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2208 optarg, true);
2209 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002210 goto fail_getopt;
2211 }
2212 break;
Max Reitz3258b912017-04-26 15:46:47 +02002213 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002214 case OPTION_IMAGE_OPTS:
2215 image_opts = true;
2216 break;
Max Reitz8eaac022019-05-07 22:35:03 +02002217 case OPTION_SALVAGE:
2218 s.salvage = true;
2219 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002220 case OPTION_TARGET_IMAGE_OPTS:
2221 tgt_image_opts = true;
2222 break;
David Edmondson168468f2020-02-05 11:02:48 +00002223 case OPTION_TARGET_IS_ZERO:
2224 /*
2225 * The user asserting that the target is blank has the
2226 * same effect as the target driver supporting zero
2227 * initialisation.
2228 */
2229 s.has_zero_init = true;
2230 break;
bellardea2384d2004-08-01 21:59:26 +00002231 }
2232 }
ths3b46e622007-09-17 08:09:54 +00002233
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002234 if (!out_fmt && !tgt_image_opts) {
2235 out_fmt = "raw";
2236 }
2237
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002238 if (qemu_opts_foreach(&qemu_object_opts,
2239 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02002240 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002241 goto fail_getopt;
2242 }
2243
Fam Zhenge11ce122018-07-27 11:34:01 +08002244 if (s.compressed && s.copy_range) {
2245 error_report("Cannot enable copy offloading when -c is used");
2246 goto fail_getopt;
2247 }
2248
2249 if (explict_min_sparse && s.copy_range) {
2250 error_report("Cannot enable copy offloading when -S is used");
2251 goto fail_getopt;
2252 }
2253
Max Reitz8eaac022019-05-07 22:35:03 +02002254 if (s.copy_range && s.salvage) {
2255 error_report("Cannot use copy offloading in salvaging mode");
2256 goto fail_getopt;
2257 }
2258
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002259 if (tgt_image_opts && !skip_create) {
2260 error_report("--target-image-opts requires use of -n flag");
2261 goto fail_getopt;
2262 }
2263
Kevin Wolfffd8e8f2019-08-09 11:09:21 +02002264 if (skip_create && options) {
2265 warn_report("-o has no effect when skipping image creation");
2266 warn_report("This will become an error in future QEMU versions.");
2267 }
2268
David Edmondson168468f2020-02-05 11:02:48 +00002269 if (s.has_zero_init && !skip_create) {
2270 error_report("--target-is-zero requires use of -n flag");
2271 goto fail_getopt;
2272 }
2273
Peter Lieven9fd77f92017-04-21 11:11:55 +02002274 s.src_num = argc - optind - 1;
2275 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2276
2277 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002278 if (out_fmt) {
2279 ret = print_block_option_help(out_filename, out_fmt);
2280 goto fail_getopt;
2281 } else {
2282 error_report("Option help requires a format be specified");
2283 goto fail_getopt;
2284 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002285 }
2286
2287 if (s.src_num < 1) {
2288 error_report("Must specify image file name");
2289 goto fail_getopt;
2290 }
2291
2292
Peter Lieven9fd77f92017-04-21 11:11:55 +02002293 /* ret is still -EINVAL until here */
2294 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2295 if (ret < 0) {
2296 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002297 goto fail_getopt;
2298 }
2299
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002300 /* Initialize before goto out */
Max Reitz3d96cb92019-05-07 22:35:02 +02002301 if (s.quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002302 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002303 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002304 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002305 qemu_progress_print(0, 100);
2306
Peter Lieven9fd77f92017-04-21 11:11:55 +02002307 s.src = g_new0(BlockBackend *, s.src_num);
2308 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002309
Peter Lieven9fd77f92017-04-21 11:11:55 +02002310 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2311 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Max Reitz3d96cb92019-05-07 22:35:02 +02002312 fmt, src_flags, src_writethrough, s.quiet,
Fam Zheng335e9932017-05-03 00:35:39 +08002313 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002314 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002315 ret = -1;
2316 goto out;
2317 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002318 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2319 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002320 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002321 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002322 ret = -1;
2323 goto out;
2324 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002325 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002326 }
bellardea2384d2004-08-01 21:59:26 +00002327
Wenchao Xiaef806542013-12-04 17:10:57 +08002328 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002329 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002330 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2331 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2332 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002333 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002334 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002335 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002336 ret = -1;
2337 goto out;
2338 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002339
Peter Lieven9fd77f92017-04-21 11:11:55 +02002340 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2341 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002342 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002343 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002344 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002345 ret = -1;
2346 goto out;
edison51ef6722010-09-21 19:58:41 -07002347 }
2348
Max Reitz2e024cd2015-02-11 09:58:46 -05002349 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002350 /* Find driver and parse its options */
2351 drv = bdrv_find_format(out_fmt);
2352 if (!drv) {
2353 error_report("Unknown file format '%s'", out_fmt);
2354 ret = -1;
2355 goto out;
2356 }
2357
2358 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2359 if (!proto_drv) {
2360 error_report_err(local_err);
2361 ret = -1;
2362 goto out;
2363 }
2364
Max Reitz2e024cd2015-02-11 09:58:46 -05002365 if (!drv->create_opts) {
2366 error_report("Format driver '%s' does not support image creation",
2367 drv->format_name);
2368 ret = -1;
2369 goto out;
2370 }
Max Reitzf75613c2014-12-02 18:32:46 +01002371
Max Reitz2e024cd2015-02-11 09:58:46 -05002372 if (!proto_drv->create_opts) {
2373 error_report("Protocol driver '%s' does not support image creation",
2374 proto_drv->format_name);
2375 ret = -1;
2376 goto out;
2377 }
Max Reitzf75613c2014-12-02 18:32:46 +01002378
Max Reitz2e024cd2015-02-11 09:58:46 -05002379 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2380 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002381
Max Reitz2e024cd2015-02-11 09:58:46 -05002382 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002383 if (options) {
2384 qemu_opts_do_parse(opts, options, NULL, &local_err);
2385 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002386 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002387 ret = -1;
2388 goto out;
2389 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002390 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002391
Peter Lieven9fd77f92017-04-21 11:11:55 +02002392 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002393 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002394 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2395 if (ret < 0) {
2396 goto out;
2397 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002398 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002399
Kevin Wolfa18953f2010-10-14 15:46:04 +02002400 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002401 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002402 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002403 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002404 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002405 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002406
David Edmondson168468f2020-02-05 11:02:48 +00002407 if (s.has_zero_init && s.target_has_backing) {
2408 error_report("Cannot use --target-is-zero when the destination "
2409 "image has a backing file");
2410 goto out;
2411 }
2412
Max Reitz48758a82017-04-26 15:46:48 +02002413 if (s.src_num > 1 && out_baseimg) {
2414 error_report("Having a backing file for the target makes no sense when "
2415 "concatenating multiple input images");
2416 ret = -1;
2417 goto out;
2418 }
2419
Kevin Wolfefa84d42009-05-18 16:42:12 +02002420 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002421 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002422 bool encryption =
2423 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002424 const char *encryptfmt =
2425 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
Chunyan Liu83d05212014-06-05 17:20:51 +08002426 const char *preallocation =
2427 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002428
Vladimir Sementsov-Ogievskiyac850bf2019-06-04 19:15:06 +03002429 if (drv && !block_driver_can_compress(drv)) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002430 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002431 ret = -1;
2432 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002433 }
2434
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002435 if (encryption || encryptfmt) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002436 error_report("Compression and encryption not supported at "
2437 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002438 ret = -1;
2439 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002440 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002441
Chunyan Liu83d05212014-06-05 17:20:51 +08002442 if (preallocation
2443 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002444 {
2445 error_report("Compression and preallocation not supported at "
2446 "the same time");
2447 ret = -1;
2448 goto out;
2449 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002450 }
2451
Daniel P. Berrangé8d65a3c2018-08-14 13:39:47 +01002452 /*
2453 * The later open call will need any decryption secrets, and
2454 * bdrv_create() will purge "opts", so extract them now before
2455 * they are lost.
2456 */
2457 if (!skip_create) {
2458 open_opts = qdict_new();
2459 qemu_opt_foreach(opts, img_add_key_secrets, open_opts, &error_abort);
2460 }
2461
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002462 if (!skip_create) {
2463 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002464 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002465 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002466 error_reportf_err(local_err, "%s: error while converting %s: ",
2467 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002468 goto out;
bellardea2384d2004-08-01 21:59:26 +00002469 }
2470 }
ths3b46e622007-09-17 08:09:54 +00002471
Max Reitz4d7c4872019-07-24 19:12:29 +02002472 s.target_is_new = !skip_create;
2473
Peter Lieven9fd77f92017-04-21 11:11:55 +02002474 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002475 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002476 if (ret < 0) {
2477 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002478 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002479 }
2480
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002481 if (skip_create) {
2482 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
Max Reitz3d96cb92019-05-07 22:35:02 +02002483 flags, writethrough, s.quiet, false);
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002484 } else {
2485 /* TODO ultimately we should allow --target-image-opts
2486 * to be used even when -n is not given.
2487 * That has to wait for bdrv_create to be improved
2488 * to allow filenames in option syntax
2489 */
Daniel P. Berrangé8d65a3c2018-08-14 13:39:47 +01002490 s.target = img_open_file(out_filename, open_opts, out_fmt,
Max Reitz3d96cb92019-05-07 22:35:02 +02002491 flags, writethrough, s.quiet, false);
Daniel P. Berrangé8d65a3c2018-08-14 13:39:47 +01002492 open_opts = NULL; /* blk_new_open will have freed it */
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002493 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002494 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002495 ret = -1;
2496 goto out;
2497 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002498 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002499
Vladimir Sementsov-Ogievskiyac850bf2019-06-04 19:15:06 +03002500 if (s.compressed && !block_driver_can_compress(out_bs->drv)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002501 error_report("Compression not supported for this file format");
2502 ret = -1;
2503 goto out;
2504 }
2505
Eric Blake5def6b82016-06-23 16:37:19 -06002506 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lieven6360ab22018-07-13 09:15:39 +02002507 * or discard_alignment of the out_bs is greater. Limit to
2508 * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */
2509 s.buf_sectors = MIN(MAX_BUF_SECTORS,
Peter Lieven9fd77f92017-04-21 11:11:55 +02002510 MAX(s.buf_sectors,
2511 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2512 out_bs->bl.pdiscard_alignment >>
2513 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002514
Peter Lieven8dcd3c92018-07-12 15:00:10 +02002515 /* try to align the write requests to the destination to avoid unnecessary
2516 * RMW cycles. */
2517 s.alignment = MAX(pow2floor(s.min_sparse),
2518 DIV_ROUND_UP(out_bs->bl.request_alignment,
2519 BDRV_SECTOR_SIZE));
2520 assert(is_power_of_2(s.alignment));
2521
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002522 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002523 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002524 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002525 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002526 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002527 ret = -1;
2528 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002529 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002530 error_report("output file is smaller than input file");
2531 ret = -1;
2532 goto out;
2533 }
2534 }
2535
Max Reitzc69291e2020-01-21 16:59:14 +01002536 if (s.target_has_backing && s.target_is_new) {
Max Reitz351c8ef2018-05-01 18:57:49 +02002537 /* Errors are treated as "backing length unknown" (which means
2538 * s.target_backing_sectors has to be negative, which it will
2539 * be automatically). The backing file length is used only
2540 * for optimizations, so such a case is not fatal. */
2541 s.target_backing_sectors = bdrv_nb_sectors(out_bs->backing->bs);
2542 } else {
2543 s.target_backing_sectors = -1;
2544 }
2545
Peter Lieven24f833c2013-11-27 11:07:07 +01002546 ret = bdrv_get_info(out_bs, &bdi);
2547 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002548 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002549 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002550 goto out;
2551 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002552 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002553 s.compressed = s.compressed || bdi.needs_compressed_writes;
2554 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Max Reitz351c8ef2018-05-01 18:57:49 +02002555 s.unallocated_blocks_are_zero = bdi.unallocated_blocks_are_zero;
Peter Lieven24f833c2013-11-27 11:07:07 +01002556 }
2557
Peter Lieven9fd77f92017-04-21 11:11:55 +02002558 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002559out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002560 if (!ret) {
2561 qemu_progress_print(100, 0);
2562 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002563 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002564 qemu_opts_del(opts);
2565 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002566 qemu_opts_del(sn_opts);
Daniel P. Berrangé8d65a3c2018-08-14 13:39:47 +01002567 qobject_unref(open_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002568 blk_unref(s.target);
2569 if (s.src) {
2570 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2571 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002572 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002573 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002574 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002575 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002576fail_getopt:
2577 g_free(options);
2578
Peter Lieven9fd77f92017-04-21 11:11:55 +02002579 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002580}
2581
bellard57d1a2b2004-08-03 21:15:11 +00002582
bellardfaea38e2006-08-05 21:31:00 +00002583static void dump_snapshots(BlockDriverState *bs)
2584{
2585 QEMUSnapshotInfo *sn_tab, *sn;
2586 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002587
2588 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2589 if (nb_sns <= 0)
2590 return;
2591 printf("Snapshot list:\n");
Markus Armbrustere1ce7d72019-04-17 21:17:55 +02002592 bdrv_snapshot_dump(NULL);
Wenchao Xia5b917042013-05-25 11:09:45 +08002593 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002594 for(i = 0; i < nb_sns; i++) {
2595 sn = &sn_tab[i];
Markus Armbrustere1ce7d72019-04-17 21:17:55 +02002596 bdrv_snapshot_dump(sn);
Wenchao Xia5b917042013-05-25 11:09:45 +08002597 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002598 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002599 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002600}
2601
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002602static void dump_json_image_info_list(ImageInfoList *list)
2603{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002604 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002605 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002606 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002607
2608 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2609 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002610 str = qobject_to_json_pretty(obj);
2611 assert(str != NULL);
2612 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002613 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002614 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002615 qobject_unref(str);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002616}
2617
Benoît Canetc054b3f2012-09-05 13:09:02 +02002618static void dump_json_image_info(ImageInfo *info)
2619{
Benoît Canetc054b3f2012-09-05 13:09:02 +02002620 QString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002621 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002622 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002623
2624 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2625 visit_complete(v, &obj);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002626 str = qobject_to_json_pretty(obj);
2627 assert(str != NULL);
2628 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002629 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002630 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002631 qobject_unref(str);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002632}
2633
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002634static void dump_human_image_info_list(ImageInfoList *list)
2635{
2636 ImageInfoList *elem;
2637 bool delim = false;
2638
2639 for (elem = list; elem; elem = elem->next) {
2640 if (delim) {
2641 printf("\n");
2642 }
2643 delim = true;
2644
Markus Armbrustere1ce7d72019-04-17 21:17:55 +02002645 bdrv_image_info_dump(elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002646 }
2647}
2648
2649static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2650{
2651 return strcmp(a, b) == 0;
2652}
2653
2654/**
2655 * Open an image file chain and return an ImageInfoList
2656 *
2657 * @filename: topmost image filename
2658 * @fmt: topmost image format (may be NULL to autodetect)
2659 * @chain: true - enumerate entire backing file chain
2660 * false - only topmost image file
2661 *
2662 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2663 * image file. If there was an error a message will have been printed to
2664 * stderr.
2665 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002666static ImageInfoList *collect_image_info_list(bool image_opts,
2667 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002668 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002669 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002670{
2671 ImageInfoList *head = NULL;
2672 ImageInfoList **last = &head;
2673 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002674 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002675
2676 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2677
2678 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002679 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002680 BlockDriverState *bs;
2681 ImageInfo *info;
2682 ImageInfoList *elem;
2683
2684 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2685 error_report("Backing file '%s' creates an infinite loop.",
2686 filename);
2687 goto err;
2688 }
2689 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2690
Max Reitzefaa7c42016-03-16 19:54:38 +01002691 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002692 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2693 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002694 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002695 goto err;
2696 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002697 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002698
Wenchao Xia43526ec2013-06-06 12:27:58 +08002699 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002700 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002701 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002702 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002703 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002704 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002705
2706 elem = g_new0(ImageInfoList, 1);
2707 elem->value = info;
2708 *last = elem;
2709 last = &elem->next;
2710
Markus Armbruster26f54e92014-10-07 13:59:04 +02002711 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002712
Stefan Hajnoczi0da7d132019-12-05 13:46:46 +00002713 /* Clear parameters that only apply to the topmost image */
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002714 filename = fmt = NULL;
Stefan Hajnoczi0da7d132019-12-05 13:46:46 +00002715 image_opts = false;
2716
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002717 if (chain) {
2718 if (info->has_full_backing_filename) {
2719 filename = info->full_backing_filename;
2720 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002721 error_report("Could not determine absolute backing filename,"
2722 " but backing filename '%s' present",
2723 info->backing_filename);
2724 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002725 }
2726 if (info->has_backing_filename_format) {
2727 fmt = info->backing_filename_format;
2728 }
2729 }
2730 }
2731 g_hash_table_destroy(filenames);
2732 return head;
2733
2734err:
2735 qapi_free_ImageInfoList(head);
2736 g_hash_table_destroy(filenames);
2737 return NULL;
2738}
2739
Benoît Canetc054b3f2012-09-05 13:09:02 +02002740static int img_info(int argc, char **argv)
2741{
2742 int c;
2743 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002744 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002745 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002746 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002747 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002748 bool force_share = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002749
bellardea2384d2004-08-01 21:59:26 +00002750 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002751 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002752 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002753 int option_index = 0;
2754 static const struct option long_options[] = {
2755 {"help", no_argument, 0, 'h'},
2756 {"format", required_argument, 0, 'f'},
2757 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002758 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002759 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002760 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002761 {"force-share", no_argument, 0, 'U'},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002762 {0, 0, 0, 0}
2763 };
Fam Zheng335e9932017-05-03 00:35:39 +08002764 c = getopt_long(argc, argv, ":f:hU",
Benoît Canetc054b3f2012-09-05 13:09:02 +02002765 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002766 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002767 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002768 }
bellardea2384d2004-08-01 21:59:26 +00002769 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002770 case ':':
2771 missing_argument(argv[optind - 1]);
2772 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002773 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002774 unrecognized_option(argv[optind - 1]);
2775 break;
bellardea2384d2004-08-01 21:59:26 +00002776 case 'h':
2777 help();
2778 break;
2779 case 'f':
2780 fmt = optarg;
2781 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002782 case 'U':
2783 force_share = true;
2784 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002785 case OPTION_OUTPUT:
2786 output = optarg;
2787 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002788 case OPTION_BACKING_CHAIN:
2789 chain = true;
2790 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002791 case OPTION_OBJECT: {
2792 QemuOpts *opts;
2793 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2794 optarg, true);
2795 if (!opts) {
2796 return 1;
2797 }
2798 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002799 case OPTION_IMAGE_OPTS:
2800 image_opts = true;
2801 break;
bellardea2384d2004-08-01 21:59:26 +00002802 }
2803 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002804 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002805 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002806 }
bellardea2384d2004-08-01 21:59:26 +00002807 filename = argv[optind++];
2808
Benoît Canetc054b3f2012-09-05 13:09:02 +02002809 if (output && !strcmp(output, "json")) {
2810 output_format = OFORMAT_JSON;
2811 } else if (output && !strcmp(output, "human")) {
2812 output_format = OFORMAT_HUMAN;
2813 } else if (output) {
2814 error_report("--output must be used with human or json as argument.");
2815 return 1;
2816 }
2817
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002818 if (qemu_opts_foreach(&qemu_object_opts,
2819 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02002820 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002821 return 1;
2822 }
2823
Fam Zheng335e9932017-05-03 00:35:39 +08002824 list = collect_image_info_list(image_opts, filename, fmt, chain,
2825 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002826 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002827 return 1;
2828 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002829
Benoît Canetc054b3f2012-09-05 13:09:02 +02002830 switch (output_format) {
2831 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002832 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002833 break;
2834 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002835 if (chain) {
2836 dump_json_image_info_list(list);
2837 } else {
2838 dump_json_image_info(list->value);
2839 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002840 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002841 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002842
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002843 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002844 return 0;
2845}
2846
Eric Blake30065d12019-03-26 13:40:43 -05002847static int dump_map_entry(OutputFormat output_format, MapEntry *e,
2848 MapEntry *next)
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002849{
2850 switch (output_format) {
2851 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002852 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002853 error_report("File contains external, encrypted or compressed clusters.");
Eric Blake30065d12019-03-26 13:40:43 -05002854 return -1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002855 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002856 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002857 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002858 e->start, e->length,
2859 e->has_offset ? e->offset : 0,
2860 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002861 }
2862 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2863 * Modify the flags here to allow more coalescing.
2864 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002865 if (next && (!next->data || next->zero)) {
2866 next->data = false;
2867 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002868 }
2869 break;
2870 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002871 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2872 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002873 (e->start == 0 ? "[" : ",\n"),
2874 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002875 e->zero ? "true" : "false",
2876 e->data ? "true" : "false");
2877 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002878 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002879 }
2880 putchar('}');
2881
2882 if (!next) {
2883 printf("]\n");
2884 }
2885 break;
2886 }
Eric Blake30065d12019-03-26 13:40:43 -05002887 return 0;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002888}
2889
Eric Blake5e344dd2017-10-11 22:47:02 -05002890static int get_block_status(BlockDriverState *bs, int64_t offset,
2891 int64_t bytes, MapEntry *e)
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002892{
Eric Blake237d78f2017-10-11 22:47:03 -05002893 int ret;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002894 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002895 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002896 bool has_offset;
Eric Blake237d78f2017-10-11 22:47:03 -05002897 int64_t map;
Max Reitzf30c66b2019-02-01 20:29:05 +01002898 char *filename = NULL;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002899
2900 /* As an optimization, we could cache the current range of unallocated
2901 * clusters in each file of the chain, and avoid querying the same
2902 * range repeatedly.
2903 */
2904
2905 depth = 0;
2906 for (;;) {
Eric Blake237d78f2017-10-11 22:47:03 -05002907 ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002908 if (ret < 0) {
2909 return ret;
2910 }
Eric Blake237d78f2017-10-11 22:47:03 -05002911 assert(bytes);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002912 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2913 break;
2914 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002915 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002916 if (bs == NULL) {
2917 ret = 0;
2918 break;
2919 }
2920
2921 depth++;
2922 }
2923
John Snow28756452016-02-05 13:12:33 -05002924 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2925
Max Reitzf30c66b2019-02-01 20:29:05 +01002926 if (file && has_offset) {
2927 bdrv_refresh_filename(file);
2928 filename = file->filename;
2929 }
2930
John Snow28756452016-02-05 13:12:33 -05002931 *e = (MapEntry) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002932 .start = offset,
Eric Blake237d78f2017-10-11 22:47:03 -05002933 .length = bytes,
John Snow28756452016-02-05 13:12:33 -05002934 .data = !!(ret & BDRV_BLOCK_DATA),
2935 .zero = !!(ret & BDRV_BLOCK_ZERO),
Eric Blake237d78f2017-10-11 22:47:03 -05002936 .offset = map,
John Snow28756452016-02-05 13:12:33 -05002937 .has_offset = has_offset,
2938 .depth = depth,
Max Reitzf30c66b2019-02-01 20:29:05 +01002939 .has_filename = filename,
2940 .filename = filename,
John Snow28756452016-02-05 13:12:33 -05002941 };
2942
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002943 return 0;
2944}
2945
Fam Zheng16b0d552016-01-26 11:59:02 +08002946static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2947{
2948 if (curr->length == 0) {
2949 return false;
2950 }
2951 if (curr->zero != next->zero ||
2952 curr->data != next->data ||
2953 curr->depth != next->depth ||
2954 curr->has_filename != next->has_filename ||
2955 curr->has_offset != next->has_offset) {
2956 return false;
2957 }
2958 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2959 return false;
2960 }
2961 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2962 return false;
2963 }
2964 return true;
2965}
2966
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002967static int img_map(int argc, char **argv)
2968{
2969 int c;
2970 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002971 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002972 BlockDriverState *bs;
2973 const char *filename, *fmt, *output;
2974 int64_t length;
2975 MapEntry curr = { .length = 0 }, next;
2976 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002977 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002978 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002979
2980 fmt = NULL;
2981 output = NULL;
2982 for (;;) {
2983 int option_index = 0;
2984 static const struct option long_options[] = {
2985 {"help", no_argument, 0, 'h'},
2986 {"format", required_argument, 0, 'f'},
2987 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002988 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002989 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002990 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002991 {0, 0, 0, 0}
2992 };
Fam Zheng335e9932017-05-03 00:35:39 +08002993 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002994 long_options, &option_index);
2995 if (c == -1) {
2996 break;
2997 }
2998 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002999 case ':':
3000 missing_argument(argv[optind - 1]);
3001 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003002 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003003 unrecognized_option(argv[optind - 1]);
3004 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003005 case 'h':
3006 help();
3007 break;
3008 case 'f':
3009 fmt = optarg;
3010 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003011 case 'U':
3012 force_share = true;
3013 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003014 case OPTION_OUTPUT:
3015 output = optarg;
3016 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003017 case OPTION_OBJECT: {
3018 QemuOpts *opts;
3019 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3020 optarg, true);
3021 if (!opts) {
3022 return 1;
3023 }
3024 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003025 case OPTION_IMAGE_OPTS:
3026 image_opts = true;
3027 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003028 }
3029 }
Fam Zhengac1307a2014-04-22 13:36:11 +08003030 if (optind != argc - 1) {
3031 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003032 }
Fam Zhengac1307a2014-04-22 13:36:11 +08003033 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003034
3035 if (output && !strcmp(output, "json")) {
3036 output_format = OFORMAT_JSON;
3037 } else if (output && !strcmp(output, "human")) {
3038 output_format = OFORMAT_HUMAN;
3039 } else if (output) {
3040 error_report("--output must be used with human or json as argument.");
3041 return 1;
3042 }
3043
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003044 if (qemu_opts_foreach(&qemu_object_opts,
3045 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02003046 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003047 return 1;
3048 }
3049
Fam Zheng335e9932017-05-03 00:35:39 +08003050 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003051 if (!blk) {
3052 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003053 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003054 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003055
3056 if (output_format == OFORMAT_HUMAN) {
3057 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
3058 }
3059
Max Reitzf1d3cd72015-02-05 13:58:18 -05003060 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003061 while (curr.start + curr.length < length) {
Eric Blake5e344dd2017-10-11 22:47:02 -05003062 int64_t offset = curr.start + curr.length;
3063 int64_t n;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003064
3065 /* Probe up to 1 GiB at a time. */
Stefano Garzarella97ede572019-05-08 12:43:24 +02003066 n = MIN(1 * GiB, length - offset);
Eric Blake5e344dd2017-10-11 22:47:02 -05003067 ret = get_block_status(bs, offset, n, &next);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003068
3069 if (ret < 0) {
3070 error_report("Could not read file metadata: %s", strerror(-ret));
3071 goto out;
3072 }
3073
Fam Zheng16b0d552016-01-26 11:59:02 +08003074 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003075 curr.length += next.length;
3076 continue;
3077 }
3078
3079 if (curr.length > 0) {
Eric Blake30065d12019-03-26 13:40:43 -05003080 ret = dump_map_entry(output_format, &curr, &next);
3081 if (ret < 0) {
3082 goto out;
3083 }
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003084 }
3085 curr = next;
3086 }
3087
Eric Blake30065d12019-03-26 13:40:43 -05003088 ret = dump_map_entry(output_format, &curr, NULL);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003089
3090out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003091 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003092 return ret < 0;
3093}
3094
aliguorif7b4a942009-01-07 17:40:15 +00003095#define SNAPSHOT_LIST 1
3096#define SNAPSHOT_CREATE 2
3097#define SNAPSHOT_APPLY 3
3098#define SNAPSHOT_DELETE 4
3099
Stuart Brady153859b2009-06-07 00:42:17 +01003100static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00003101{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003102 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00003103 BlockDriverState *bs;
3104 QEMUSnapshotInfo sn;
3105 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003106 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00003107 int action = 0;
3108 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003109 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003110 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003111 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08003112 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00003113
Kevin Wolfce099542016-03-15 13:01:04 +01003114 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00003115 /* Parse commandline parameters */
3116 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003117 static const struct option long_options[] = {
3118 {"help", no_argument, 0, 'h'},
3119 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003120 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003121 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003122 {0, 0, 0, 0}
3123 };
Fam Zheng335e9932017-05-03 00:35:39 +08003124 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003125 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003126 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00003127 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003128 }
aliguorif7b4a942009-01-07 17:40:15 +00003129 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003130 case ':':
3131 missing_argument(argv[optind - 1]);
3132 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003133 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003134 unrecognized_option(argv[optind - 1]);
3135 break;
aliguorif7b4a942009-01-07 17:40:15 +00003136 case 'h':
3137 help();
Stuart Brady153859b2009-06-07 00:42:17 +01003138 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003139 case 'l':
3140 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003141 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01003142 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003143 }
3144 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02003145 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00003146 break;
3147 case 'a':
3148 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003149 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01003150 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003151 }
3152 action = SNAPSHOT_APPLY;
3153 snapshot_name = optarg;
3154 break;
3155 case 'c':
3156 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003157 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01003158 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003159 }
3160 action = SNAPSHOT_CREATE;
3161 snapshot_name = optarg;
3162 break;
3163 case 'd':
3164 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003165 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01003166 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003167 }
3168 action = SNAPSHOT_DELETE;
3169 snapshot_name = optarg;
3170 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003171 case 'q':
3172 quiet = true;
3173 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003174 case 'U':
3175 force_share = true;
3176 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003177 case OPTION_OBJECT: {
3178 QemuOpts *opts;
3179 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3180 optarg, true);
3181 if (!opts) {
3182 return 1;
3183 }
3184 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003185 case OPTION_IMAGE_OPTS:
3186 image_opts = true;
3187 break;
aliguorif7b4a942009-01-07 17:40:15 +00003188 }
3189 }
3190
Kevin Wolffc11eb22013-08-05 10:53:04 +02003191 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003192 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003193 }
aliguorif7b4a942009-01-07 17:40:15 +00003194 filename = argv[optind++];
3195
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003196 if (qemu_opts_foreach(&qemu_object_opts,
3197 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02003198 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003199 return 1;
3200 }
3201
aliguorif7b4a942009-01-07 17:40:15 +00003202 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08003203 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
3204 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003205 if (!blk) {
3206 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003207 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003208 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00003209
3210 /* Perform the requested action */
3211 switch(action) {
3212 case SNAPSHOT_LIST:
3213 dump_snapshots(bs);
3214 break;
3215
3216 case SNAPSHOT_CREATE:
3217 memset(&sn, 0, sizeof(sn));
3218 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
3219
3220 qemu_gettimeofday(&tv);
3221 sn.date_sec = tv.tv_sec;
3222 sn.date_nsec = tv.tv_usec * 1000;
3223
3224 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003225 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003226 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003227 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003228 }
aliguorif7b4a942009-01-07 17:40:15 +00003229 break;
3230
3231 case SNAPSHOT_APPLY:
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01003232 ret = bdrv_snapshot_goto(bs, snapshot_name, &err);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003233 if (ret) {
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01003234 error_reportf_err(err, "Could not apply snapshot '%s': ",
3235 snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003236 }
aliguorif7b4a942009-01-07 17:40:15 +00003237 break;
3238
3239 case SNAPSHOT_DELETE:
Daniel Henrique Barboza8c040932018-11-07 11:09:59 -02003240 ret = bdrv_snapshot_find(bs, &sn, snapshot_name);
3241 if (ret < 0) {
3242 error_report("Could not delete snapshot '%s': snapshot not "
3243 "found", snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003244 ret = 1;
Daniel Henrique Barboza8c040932018-11-07 11:09:59 -02003245 } else {
3246 ret = bdrv_snapshot_delete(bs, sn.id_str, sn.name, &err);
3247 if (ret < 0) {
3248 error_reportf_err(err, "Could not delete snapshot '%s': ",
3249 snapshot_name);
3250 ret = 1;
3251 }
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003252 }
aliguorif7b4a942009-01-07 17:40:15 +00003253 break;
3254 }
3255
3256 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003257 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003258 if (ret) {
3259 return 1;
3260 }
Stuart Brady153859b2009-06-07 00:42:17 +01003261 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003262}
3263
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003264static int img_rebase(int argc, char **argv)
3265{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003266 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003267 uint8_t *buf_old = NULL;
3268 uint8_t *buf_new = NULL;
Sam Eiderman863cc782019-05-23 19:33:36 +03003269 BlockDriverState *bs = NULL, *prefix_chain_bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003270 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003271 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3272 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003273 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003274 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003275 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003276 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003277 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003278 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003279 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003280
3281 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003282 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003283 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003284 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003285 out_baseimg = NULL;
3286 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003287 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003288 static const struct option long_options[] = {
3289 {"help", no_argument, 0, 'h'},
3290 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003291 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003292 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003293 {0, 0, 0, 0}
3294 };
Fam Zheng335e9932017-05-03 00:35:39 +08003295 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003296 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003297 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003298 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003299 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003300 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003301 case ':':
3302 missing_argument(argv[optind - 1]);
3303 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003304 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003305 unrecognized_option(argv[optind - 1]);
3306 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003307 case 'h':
3308 help();
3309 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003310 case 'f':
3311 fmt = optarg;
3312 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003313 case 'F':
3314 out_basefmt = optarg;
3315 break;
3316 case 'b':
3317 out_baseimg = optarg;
3318 break;
3319 case 'u':
3320 unsafe = 1;
3321 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003322 case 'p':
3323 progress = 1;
3324 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003325 case 't':
3326 cache = optarg;
3327 break;
Max Reitz40055952014-07-22 22:58:42 +02003328 case 'T':
3329 src_cache = optarg;
3330 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003331 case 'q':
3332 quiet = true;
3333 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003334 case OPTION_OBJECT: {
3335 QemuOpts *opts;
3336 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3337 optarg, true);
3338 if (!opts) {
3339 return 1;
3340 }
3341 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003342 case OPTION_IMAGE_OPTS:
3343 image_opts = true;
3344 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003345 case 'U':
3346 force_share = true;
3347 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003348 }
3349 }
3350
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003351 if (quiet) {
3352 progress = 0;
3353 }
3354
Fam Zhengac1307a2014-04-22 13:36:11 +08003355 if (optind != argc - 1) {
3356 error_exit("Expecting one image file name");
3357 }
3358 if (!unsafe && !out_baseimg) {
3359 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003360 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003361 filename = argv[optind++];
3362
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003363 if (qemu_opts_foreach(&qemu_object_opts,
3364 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02003365 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003366 return 1;
3367 }
3368
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003369 qemu_progress_init(progress, 2.0);
3370 qemu_progress_print(0, 100);
3371
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003372 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003373 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003374 if (ret < 0) {
3375 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003376 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003377 }
3378
Kevin Wolfce099542016-03-15 13:01:04 +01003379 src_flags = 0;
3380 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003381 if (ret < 0) {
3382 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003383 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003384 }
3385
Kevin Wolfce099542016-03-15 13:01:04 +01003386 /* The source files are opened read-only, don't care about WCE */
3387 assert((src_flags & BDRV_O_RDWR) == 0);
3388 (void) src_writethrough;
3389
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003390 /*
3391 * Open the images.
3392 *
3393 * Ignore the old backing file for unsafe rebase in case we want to correct
3394 * the reference to a renamed or moved backing file.
3395 */
Fam Zheng335e9932017-05-03 00:35:39 +08003396 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3397 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003398 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003399 ret = -1;
3400 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003401 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003402 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003403
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003404 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003405 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003406 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003407 ret = -1;
3408 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003409 }
3410 }
3411
3412 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003413 if (!unsafe) {
Max Reitz644483d2015-02-05 13:58:17 -05003414 QDict *options = NULL;
Sam Eiderman4ebe0612019-05-23 19:33:35 +03003415 BlockDriverState *base_bs = backing_bs(bs);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003416
Sam Eiderman4ebe0612019-05-23 19:33:35 +03003417 if (base_bs) {
Kevin Wolfd861ab32019-04-25 14:25:10 +02003418 blk_old_backing = blk_new(qemu_get_aio_context(),
3419 BLK_PERM_CONSISTENT_READ,
Sam Eiderman4ebe0612019-05-23 19:33:35 +03003420 BLK_PERM_ALL);
3421 ret = blk_insert_bs(blk_old_backing, base_bs,
3422 &local_err);
3423 if (ret < 0) {
Max Reitz35ddd932019-05-09 19:52:35 +02003424 error_reportf_err(local_err,
Sam Eiderman4ebe0612019-05-23 19:33:35 +03003425 "Could not reuse old backing file '%s': ",
3426 base_bs->filename);
Max Reitz35ddd932019-05-09 19:52:35 +02003427 goto out;
3428 }
3429 } else {
3430 blk_old_backing = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003431 }
Max Reitz644483d2015-02-05 13:58:17 -05003432
Alex Bligha6166732012-10-16 13:46:18 +01003433 if (out_baseimg[0]) {
Max Reitzd16699b2018-05-09 20:20:01 +02003434 const char *overlay_filename;
3435 char *out_real_path;
3436
Fam Zheng335e9932017-05-03 00:35:39 +08003437 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003438 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003439 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003440 }
3441 if (force_share) {
3442 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003443 }
3444
Max Reitzf30c66b2019-02-01 20:29:05 +01003445 bdrv_refresh_filename(bs);
Max Reitzd16699b2018-05-09 20:20:01 +02003446 overlay_filename = bs->exact_filename[0] ? bs->exact_filename
3447 : bs->filename;
Max Reitz645ae7d2019-02-01 20:29:14 +01003448 out_real_path =
3449 bdrv_get_full_backing_filename_from_filename(overlay_filename,
3450 out_baseimg,
3451 &local_err);
Max Reitzd16699b2018-05-09 20:20:01 +02003452 if (local_err) {
Max Reitzf22356d2019-05-28 21:53:38 +02003453 qobject_unref(options);
Max Reitzd16699b2018-05-09 20:20:01 +02003454 error_reportf_err(local_err,
3455 "Could not resolve backing filename: ");
3456 ret = -1;
Max Reitzd16699b2018-05-09 20:20:01 +02003457 goto out;
3458 }
3459
Sam Eiderman863cc782019-05-23 19:33:36 +03003460 /*
3461 * Find out whether we rebase an image on top of a previous image
3462 * in its chain.
3463 */
3464 prefix_chain_bs = bdrv_find_backing_image(bs, out_real_path);
Sam Eiderman330c7292019-05-23 19:33:37 +03003465 if (prefix_chain_bs) {
Max Reitzf22356d2019-05-28 21:53:38 +02003466 qobject_unref(options);
Sam Eiderman330c7292019-05-23 19:33:37 +03003467 g_free(out_real_path);
Max Reitzf22356d2019-05-28 21:53:38 +02003468
Kevin Wolfd861ab32019-04-25 14:25:10 +02003469 blk_new_backing = blk_new(qemu_get_aio_context(),
3470 BLK_PERM_CONSISTENT_READ,
Sam Eiderman330c7292019-05-23 19:33:37 +03003471 BLK_PERM_ALL);
3472 ret = blk_insert_bs(blk_new_backing, prefix_chain_bs,
3473 &local_err);
3474 if (ret < 0) {
3475 error_reportf_err(local_err,
3476 "Could not reuse backing file '%s': ",
3477 out_baseimg);
3478 goto out;
3479 }
3480 } else {
3481 blk_new_backing = blk_new_open(out_real_path, NULL,
3482 options, src_flags, &local_err);
3483 g_free(out_real_path);
3484 if (!blk_new_backing) {
3485 error_reportf_err(local_err,
3486 "Could not open new backing file '%s': ",
3487 out_baseimg);
3488 ret = -1;
3489 goto out;
3490 }
Alex Bligha6166732012-10-16 13:46:18 +01003491 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003492 }
3493 }
3494
3495 /*
3496 * Check each unallocated cluster in the COW file. If it is unallocated,
3497 * accesses go to the backing file. We must therefore compare this cluster
3498 * in the old and new backing file, and if they differ we need to copy it
3499 * from the old backing file into the COW file.
3500 *
3501 * If qemu-img crashes during this step, no harm is done. The content of
3502 * the image is the same as the original one at any time.
3503 */
3504 if (!unsafe) {
Eric Blake41536282017-10-11 22:47:15 -05003505 int64_t size;
Max Reitz35ddd932019-05-09 19:52:35 +02003506 int64_t old_backing_size = 0;
Eric Blake41536282017-10-11 22:47:15 -05003507 int64_t new_backing_size = 0;
3508 uint64_t offset;
3509 int64_t n;
Kevin Wolf1f710492012-10-12 14:29:18 +02003510 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003511
Max Reitzf1d3cd72015-02-05 13:58:18 -05003512 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3513 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003514
Eric Blake41536282017-10-11 22:47:15 -05003515 size = blk_getlength(blk);
3516 if (size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003517 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003518 filename, strerror(-size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003519 ret = -1;
3520 goto out;
3521 }
Max Reitz35ddd932019-05-09 19:52:35 +02003522 if (blk_old_backing) {
3523 old_backing_size = blk_getlength(blk_old_backing);
3524 if (old_backing_size < 0) {
3525 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003526
Max Reitz35ddd932019-05-09 19:52:35 +02003527 bdrv_get_backing_filename(bs, backing_name,
3528 sizeof(backing_name));
3529 error_report("Could not get size of '%s': %s",
3530 backing_name, strerror(-old_backing_size));
3531 ret = -1;
3532 goto out;
3533 }
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003534 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003535 if (blk_new_backing) {
Eric Blake41536282017-10-11 22:47:15 -05003536 new_backing_size = blk_getlength(blk_new_backing);
3537 if (new_backing_size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003538 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003539 out_baseimg, strerror(-new_backing_size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003540 ret = -1;
3541 goto out;
3542 }
Alex Bligha6166732012-10-16 13:46:18 +01003543 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003544
Eric Blake41536282017-10-11 22:47:15 -05003545 if (size != 0) {
3546 local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE));
Kevin Wolf1f710492012-10-12 14:29:18 +02003547 }
3548
Eric Blake41536282017-10-11 22:47:15 -05003549 for (offset = 0; offset < size; offset += n) {
Max Reitz1c6e8772019-05-09 19:52:36 +02003550 bool buf_old_is_zero = false;
3551
Eric Blake41536282017-10-11 22:47:15 -05003552 /* How many bytes can we handle with the next read? */
3553 n = MIN(IO_BUF_SIZE, size - offset);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003554
3555 /* If the cluster is allocated, we don't need to take action */
Eric Blake41536282017-10-11 22:47:15 -05003556 ret = bdrv_is_allocated(bs, offset, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003557 if (ret < 0) {
3558 error_report("error while reading image metadata: %s",
3559 strerror(-ret));
3560 goto out;
3561 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003562 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003563 continue;
3564 }
3565
Sam Eiderman863cc782019-05-23 19:33:36 +03003566 if (prefix_chain_bs) {
3567 /*
3568 * If cluster wasn't changed since prefix_chain, we don't need
3569 * to take action
3570 */
3571 ret = bdrv_is_allocated_above(backing_bs(bs), prefix_chain_bs,
Andrey Shinkevich170d3bd2019-05-29 20:56:14 +03003572 false, offset, n, &n);
Sam Eiderman863cc782019-05-23 19:33:36 +03003573 if (ret < 0) {
3574 error_report("error while reading image metadata: %s",
3575 strerror(-ret));
3576 goto out;
3577 }
3578 if (!ret) {
3579 continue;
3580 }
3581 }
3582
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003583 /*
3584 * Read old and new backing file and take into consideration that
3585 * backing files may be smaller than the COW image.
3586 */
Eric Blake41536282017-10-11 22:47:15 -05003587 if (offset >= old_backing_size) {
3588 memset(buf_old, 0, n);
Max Reitz1c6e8772019-05-09 19:52:36 +02003589 buf_old_is_zero = true;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003590 } else {
Eric Blake41536282017-10-11 22:47:15 -05003591 if (offset + n > old_backing_size) {
3592 n = old_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003593 }
3594
Eric Blake41536282017-10-11 22:47:15 -05003595 ret = blk_pread(blk_old_backing, offset, buf_old, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003596 if (ret < 0) {
3597 error_report("error while reading from old backing file");
3598 goto out;
3599 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003600 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003601
Eric Blake41536282017-10-11 22:47:15 -05003602 if (offset >= new_backing_size || !blk_new_backing) {
3603 memset(buf_new, 0, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003604 } else {
Eric Blake41536282017-10-11 22:47:15 -05003605 if (offset + n > new_backing_size) {
3606 n = new_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003607 }
3608
Eric Blake41536282017-10-11 22:47:15 -05003609 ret = blk_pread(blk_new_backing, offset, buf_new, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003610 if (ret < 0) {
3611 error_report("error while reading from new backing file");
3612 goto out;
3613 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003614 }
3615
3616 /* If they differ, we need to write to the COW file */
3617 uint64_t written = 0;
3618
Eric Blake41536282017-10-11 22:47:15 -05003619 while (written < n) {
Eric Blakedc61cd32017-10-11 22:47:14 -05003620 int64_t pnum;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003621
Eric Blake41536282017-10-11 22:47:15 -05003622 if (compare_buffers(buf_old + written, buf_new + written,
3623 n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003624 {
Max Reitz1c6e8772019-05-09 19:52:36 +02003625 if (buf_old_is_zero) {
3626 ret = blk_pwrite_zeroes(blk, offset + written, pnum, 0);
3627 } else {
3628 ret = blk_pwrite(blk, offset + written,
3629 buf_old + written, pnum, 0);
3630 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003631 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003632 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003633 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003634 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003635 }
3636 }
3637
3638 written += pnum;
3639 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003640 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003641 }
3642 }
3643
3644 /*
3645 * Change the backing file. All clusters that are different from the old
3646 * backing file are overwritten in the COW file now, so the visible content
3647 * doesn't change when we switch the backing file.
3648 */
Alex Bligha6166732012-10-16 13:46:18 +01003649 if (out_baseimg && *out_baseimg) {
3650 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3651 } else {
3652 ret = bdrv_change_backing_file(bs, NULL, NULL);
3653 }
3654
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003655 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003656 error_report("Could not change the backing file to '%s': No "
3657 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003658 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003659 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003660 out_baseimg, strerror(-ret));
3661 }
3662
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003663 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003664 /*
3665 * TODO At this point it is possible to check if any clusters that are
3666 * allocated in the COW file are the same in the backing file. If so, they
3667 * could be dropped from the COW file. Don't do this before switching the
3668 * backing file, in case of a crash this would lead to corruption.
3669 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003670out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003671 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003672 /* Cleanup */
3673 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003674 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003675 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003676 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003677 qemu_vfree(buf_old);
3678 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003679
Markus Armbruster26f54e92014-10-07 13:59:04 +02003680 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003681 if (ret) {
3682 return 1;
3683 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003684 return 0;
3685}
3686
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003687static int img_resize(int argc, char **argv)
3688{
Markus Armbruster6750e792015-02-12 17:43:08 +01003689 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003690 int c, ret, relative;
3691 const char *filename, *fmt, *size;
Max Reitz09c5c6d2019-09-18 11:51:44 +02003692 int64_t n, total_size, current_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003693 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003694 BlockBackend *blk = NULL;
Max Reitzdc5f6902017-06-13 22:20:55 +02003695 PreallocMode prealloc = PREALLOC_MODE_OFF;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003696 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003697
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003698 static QemuOptsList resize_options = {
3699 .name = "resize_options",
3700 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3701 .desc = {
3702 {
3703 .name = BLOCK_OPT_SIZE,
3704 .type = QEMU_OPT_SIZE,
3705 .help = "Virtual disk size"
3706 }, {
3707 /* end of list */
3708 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003709 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003710 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003711 bool image_opts = false;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003712 bool shrink = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003713
Kevin Wolfe80fec72011-04-29 10:58:12 +02003714 /* Remove size from argv manually so that negative numbers are not treated
3715 * as options by getopt. */
3716 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003717 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003718 return 1;
3719 }
3720
3721 size = argv[--argc];
3722
3723 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003724 fmt = NULL;
3725 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003726 static const struct option long_options[] = {
3727 {"help", no_argument, 0, 'h'},
3728 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003729 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Max Reitzdc5f6902017-06-13 22:20:55 +02003730 {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003731 {"shrink", no_argument, 0, OPTION_SHRINK},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003732 {0, 0, 0, 0}
3733 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003734 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003735 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003736 if (c == -1) {
3737 break;
3738 }
3739 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003740 case ':':
3741 missing_argument(argv[optind - 1]);
3742 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003743 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003744 unrecognized_option(argv[optind - 1]);
3745 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003746 case 'h':
3747 help();
3748 break;
3749 case 'f':
3750 fmt = optarg;
3751 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003752 case 'q':
3753 quiet = true;
3754 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003755 case OPTION_OBJECT: {
3756 QemuOpts *opts;
3757 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3758 optarg, true);
3759 if (!opts) {
3760 return 1;
3761 }
3762 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003763 case OPTION_IMAGE_OPTS:
3764 image_opts = true;
3765 break;
Max Reitzdc5f6902017-06-13 22:20:55 +02003766 case OPTION_PREALLOCATION:
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02003767 prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
Markus Armbruster06c60b62017-08-24 10:45:57 +02003768 PREALLOC_MODE__MAX, NULL);
Max Reitzdc5f6902017-06-13 22:20:55 +02003769 if (prealloc == PREALLOC_MODE__MAX) {
3770 error_report("Invalid preallocation mode '%s'", optarg);
3771 return 1;
3772 }
3773 break;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003774 case OPTION_SHRINK:
3775 shrink = true;
3776 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003777 }
3778 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003779 if (optind != argc - 1) {
Max Reitzbe8fbd42018-02-05 17:27:45 +01003780 error_exit("Expecting image file name and size");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003781 }
3782 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003783
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003784 if (qemu_opts_foreach(&qemu_object_opts,
3785 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02003786 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003787 return 1;
3788 }
3789
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003790 /* Choose grow, shrink, or absolute resize mode */
3791 switch (size[0]) {
3792 case '+':
3793 relative = 1;
3794 size++;
3795 break;
3796 case '-':
3797 relative = -1;
3798 size++;
3799 break;
3800 default:
3801 relative = 0;
3802 break;
3803 }
3804
3805 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003806 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003807 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003808 if (err) {
3809 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003810 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003811 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003812 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003813 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003814 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3815 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003816
Max Reitzefaa7c42016-03-16 19:54:38 +01003817 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003818 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3819 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003820 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003821 ret = -1;
3822 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003823 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003824
Max Reitzdc5f6902017-06-13 22:20:55 +02003825 current_size = blk_getlength(blk);
3826 if (current_size < 0) {
3827 error_report("Failed to inquire current image length: %s",
3828 strerror(-current_size));
3829 ret = -1;
3830 goto out;
3831 }
3832
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003833 if (relative) {
Max Reitzdc5f6902017-06-13 22:20:55 +02003834 total_size = current_size + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003835 } else {
3836 total_size = n;
3837 }
3838 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003839 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003840 ret = -1;
3841 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003842 }
3843
Max Reitzdc5f6902017-06-13 22:20:55 +02003844 if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
3845 error_report("Preallocation can only be used for growing images");
3846 ret = -1;
3847 goto out;
3848 }
3849
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003850 if (total_size < current_size && !shrink) {
3851 warn_report("Shrinking an image will delete all data beyond the "
3852 "shrunken image's end. Before performing such an "
3853 "operation, make sure there is no important data there.");
3854
3855 if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) {
3856 error_report(
3857 "Use the --shrink option to perform a shrink operation.");
3858 ret = -1;
3859 goto out;
3860 } else {
3861 warn_report("Using the --shrink option will suppress this message. "
3862 "Note that future versions of qemu-img may refuse to "
3863 "shrink images without this option.");
3864 }
3865 }
3866
Max Reitze8d04f92019-09-18 11:51:43 +02003867 /*
3868 * The user expects the image to have the desired size after
3869 * resizing, so pass @exact=true. It is of no use to report
3870 * success when the image has not actually been resized.
3871 */
3872 ret = blk_truncate(blk, total_size, true, prealloc, &err);
Max Reitz09c5c6d2019-09-18 11:51:44 +02003873 if (!ret) {
3874 qprintf(quiet, "Image resized.\n");
3875 } else {
Max Reitzed3d2ec2017-03-28 22:51:27 +02003876 error_report_err(err);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003877 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003878out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003879 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003880 if (ret) {
3881 return 1;
3882 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003883 return 0;
3884}
3885
Max Reitz76a3a342014-10-27 11:12:51 +01003886static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003887 int64_t offset, int64_t total_work_size,
3888 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003889{
3890 qemu_progress_print(100.f * offset / total_work_size, 0);
3891}
3892
Max Reitz51641352018-05-09 23:00:20 +02003893static int print_amend_option_help(const char *format)
3894{
3895 BlockDriver *drv;
3896
3897 /* Find driver and parse its options */
3898 drv = bdrv_find_format(format);
3899 if (!drv) {
3900 error_report("Unknown file format '%s'", format);
3901 return 1;
3902 }
3903
3904 if (!drv->bdrv_amend_options) {
3905 error_report("Format driver '%s' does not support option amendment",
3906 format);
3907 return 1;
3908 }
3909
3910 /* Every driver supporting amendment must have create_opts */
3911 assert(drv->create_opts);
3912
3913 printf("Creation options for '%s':\n", format);
Max Reitz63898712018-10-19 18:49:25 +02003914 qemu_opts_print_help(drv->create_opts, false);
Max Reitz51641352018-05-09 23:00:20 +02003915 printf("\nNote that not all of these options may be amendable.\n");
3916 return 0;
3917}
3918
Max Reitz6f176b42013-09-03 10:09:50 +02003919static int img_amend(int argc, char **argv)
3920{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003921 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003922 int c, ret = 0;
3923 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003924 QemuOptsList *create_opts = NULL;
3925 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003926 const char *fmt = NULL, *filename, *cache;
3927 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003928 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003929 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003930 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003931 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003932 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003933
Max Reitzbd39e6e2014-07-22 22:58:43 +02003934 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003935 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003936 static const struct option long_options[] = {
3937 {"help", no_argument, 0, 'h'},
3938 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003939 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003940 {0, 0, 0, 0}
3941 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003942 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003943 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003944 if (c == -1) {
3945 break;
3946 }
3947
3948 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003949 case ':':
3950 missing_argument(argv[optind - 1]);
3951 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003952 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003953 unrecognized_option(argv[optind - 1]);
3954 break;
3955 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003956 help();
3957 break;
3958 case 'o':
Markus Armbruster6d2b5cb2020-04-15 09:49:25 +02003959 if (accumulate_options(&options, optarg) < 0) {
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003960 ret = -1;
3961 goto out_no_progress;
3962 }
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003963 break;
3964 case 'f':
3965 fmt = optarg;
3966 break;
3967 case 't':
3968 cache = optarg;
3969 break;
3970 case 'p':
3971 progress = true;
3972 break;
3973 case 'q':
3974 quiet = true;
3975 break;
3976 case OPTION_OBJECT:
3977 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3978 optarg, true);
3979 if (!opts) {
3980 ret = -1;
3981 goto out_no_progress;
3982 }
3983 break;
3984 case OPTION_IMAGE_OPTS:
3985 image_opts = true;
3986 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003987 }
3988 }
3989
Max Reitz6f176b42013-09-03 10:09:50 +02003990 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003991 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003992 }
3993
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003994 if (qemu_opts_foreach(&qemu_object_opts,
3995 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02003996 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003997 ret = -1;
3998 goto out_no_progress;
3999 }
4000
Max Reitz76a3a342014-10-27 11:12:51 +01004001 if (quiet) {
4002 progress = false;
4003 }
4004 qemu_progress_init(progress, 1.0);
4005
Kevin Wolfa283cb62014-02-21 16:24:07 +01004006 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
4007 if (fmt && has_help_option(options)) {
4008 /* If a format is explicitly specified (and possibly no filename is
4009 * given), print option help here */
Max Reitz51641352018-05-09 23:00:20 +02004010 ret = print_amend_option_help(fmt);
Kevin Wolfa283cb62014-02-21 16:24:07 +01004011 goto out;
4012 }
4013
4014 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01004015 error_report("Expecting one image file name");
4016 ret = -1;
4017 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01004018 }
Max Reitz6f176b42013-09-03 10:09:50 +02004019
Kevin Wolfce099542016-03-15 13:01:04 +01004020 flags = BDRV_O_RDWR;
4021 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02004022 if (ret < 0) {
4023 error_report("Invalid cache option: %s", cache);
4024 goto out;
4025 }
4026
Fam Zheng335e9932017-05-03 00:35:39 +08004027 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4028 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02004029 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02004030 ret = -1;
4031 goto out;
4032 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02004033 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02004034
4035 fmt = bs->drv->format_name;
4036
Kevin Wolf626f84f2014-02-21 16:24:06 +01004037 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01004038 /* If the format was auto-detected, print option help here */
Max Reitz51641352018-05-09 23:00:20 +02004039 ret = print_amend_option_help(fmt);
Max Reitz6f176b42013-09-03 10:09:50 +02004040 goto out;
4041 }
4042
Max Reitz1f996682018-05-09 23:00:17 +02004043 if (!bs->drv->bdrv_amend_options) {
4044 error_report("Format driver '%s' does not support option amendment",
Max Reitzb2439d22014-12-02 18:32:47 +01004045 fmt);
4046 ret = -1;
4047 goto out;
4048 }
4049
Max Reitz1f996682018-05-09 23:00:17 +02004050 /* Every driver supporting amendment must have create_opts */
4051 assert(bs->drv->create_opts);
4052
Chunyan Liuc282e1f2014-06-05 17:21:11 +08004053 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08004054 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01004055 qemu_opts_do_parse(opts, options, NULL, &err);
4056 if (err) {
4057 error_report_err(err);
4058 ret = -1;
4059 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02004060 }
4061
Max Reitz76a3a342014-10-27 11:12:51 +01004062 /* In case the driver does not call amend_status_cb() */
4063 qemu_progress_print(0.f, 0);
Max Reitzd1402b52018-05-09 23:00:18 +02004064 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, &err);
Max Reitz76a3a342014-10-27 11:12:51 +01004065 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02004066 if (ret < 0) {
Max Reitzd1402b52018-05-09 23:00:18 +02004067 error_report_err(err);
Max Reitz6f176b42013-09-03 10:09:50 +02004068 goto out;
4069 }
4070
4071out:
Max Reitz76a3a342014-10-27 11:12:51 +01004072 qemu_progress_end();
4073
Max Reitze814dff2015-08-20 16:00:38 -07004074out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02004075 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08004076 qemu_opts_del(opts);
4077 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01004078 g_free(options);
4079
Max Reitz6f176b42013-09-03 10:09:50 +02004080 if (ret) {
4081 return 1;
4082 }
4083 return 0;
4084}
4085
Kevin Wolfb6133b82014-08-05 14:17:13 +02004086typedef struct BenchData {
4087 BlockBackend *blk;
4088 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004089 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004090 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02004091 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004092 int nrreq;
4093 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004094 int flush_interval;
4095 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004096 uint8_t *buf;
4097 QEMUIOVector *qiov;
4098
4099 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004100 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004101 uint64_t offset;
4102} BenchData;
4103
Kevin Wolf55d539c2016-06-03 13:59:41 +02004104static void bench_undrained_flush_cb(void *opaque, int ret)
4105{
4106 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02004107 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02004108 exit(EXIT_FAILURE);
4109 }
4110}
4111
Kevin Wolfb6133b82014-08-05 14:17:13 +02004112static void bench_cb(void *opaque, int ret)
4113{
4114 BenchData *b = opaque;
4115 BlockAIOCB *acb;
4116
4117 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02004118 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02004119 exit(EXIT_FAILURE);
4120 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02004121
4122 if (b->in_flush) {
4123 /* Just finished a flush with drained queue: Start next requests */
4124 assert(b->in_flight == 0);
4125 b->in_flush = false;
4126 } else if (b->in_flight > 0) {
4127 int remaining = b->n - b->in_flight;
4128
Kevin Wolfb6133b82014-08-05 14:17:13 +02004129 b->n--;
4130 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004131
4132 /* Time for flush? Drain queue if requested, then flush */
4133 if (b->flush_interval && remaining % b->flush_interval == 0) {
4134 if (!b->in_flight || !b->drain_on_flush) {
4135 BlockCompletionFunc *cb;
4136
4137 if (b->drain_on_flush) {
4138 b->in_flush = true;
4139 cb = bench_cb;
4140 } else {
4141 cb = bench_undrained_flush_cb;
4142 }
4143
4144 acb = blk_aio_flush(b->blk, cb, b);
4145 if (!acb) {
4146 error_report("Failed to issue flush request");
4147 exit(EXIT_FAILURE);
4148 }
4149 }
4150 if (b->drain_on_flush) {
4151 return;
4152 }
4153 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004154 }
4155
4156 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01004157 int64_t offset = b->offset;
4158 /* blk_aio_* might look for completed I/Os and kick bench_cb
4159 * again, so make sure this operation is counted by in_flight
4160 * and b->offset is ready for the next submission.
4161 */
4162 b->in_flight++;
4163 b->offset += b->step;
4164 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004165 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01004166 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004167 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01004168 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004169 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004170 if (!acb) {
4171 error_report("Failed to issue request");
4172 exit(EXIT_FAILURE);
4173 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004174 }
4175}
4176
4177static int img_bench(int argc, char **argv)
4178{
4179 int c, ret = 0;
4180 const char *fmt = NULL, *filename;
4181 bool quiet = false;
4182 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004183 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004184 int count = 75000;
4185 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02004186 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004187 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004188 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02004189 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004190 int flush_interval = 0;
4191 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004192 int64_t image_size;
4193 BlockBackend *blk = NULL;
4194 BenchData data = {};
4195 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02004196 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004197 struct timeval t1, t2;
4198 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08004199 bool force_share = false;
Fam Zheng79d46582018-01-16 14:08:58 +08004200 size_t buf_size;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004201
4202 for (;;) {
4203 static const struct option long_options[] = {
4204 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02004205 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02004206 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004207 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02004208 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08004209 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02004210 {0, 0, 0, 0}
4211 };
Aarushi Mehtacdd26772020-01-20 14:18:55 +00004212 c = getopt_long(argc, argv, ":hc:d:f:ni:o:qs:S:t:wU", long_options,
4213 NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004214 if (c == -1) {
4215 break;
4216 }
4217
4218 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004219 case ':':
4220 missing_argument(argv[optind - 1]);
4221 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004222 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004223 unrecognized_option(argv[optind - 1]);
4224 break;
4225 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02004226 help();
4227 break;
4228 case 'c':
4229 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004230 unsigned long res;
4231
4232 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02004233 error_report("Invalid request count specified");
4234 return 1;
4235 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004236 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004237 break;
4238 }
4239 case 'd':
4240 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004241 unsigned long res;
4242
4243 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02004244 error_report("Invalid queue depth specified");
4245 return 1;
4246 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004247 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004248 break;
4249 }
4250 case 'f':
4251 fmt = optarg;
4252 break;
4253 case 'n':
4254 flags |= BDRV_O_NATIVE_AIO;
4255 break;
Aarushi Mehtacdd26772020-01-20 14:18:55 +00004256 case 'i':
4257 ret = bdrv_parse_aio(optarg, &flags);
4258 if (ret < 0) {
4259 error_report("Invalid aio option: %s", optarg);
4260 ret = -1;
4261 goto out;
4262 }
4263 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02004264 case 'o':
4265 {
Markus Armbruster606caa02017-02-21 21:14:04 +01004266 offset = cvtnum(optarg);
4267 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02004268 error_report("Invalid offset specified");
4269 return 1;
4270 }
4271 break;
4272 }
4273 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004274 case 'q':
4275 quiet = true;
4276 break;
4277 case 's':
4278 {
4279 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004280
Markus Armbruster606caa02017-02-21 21:14:04 +01004281 sval = cvtnum(optarg);
4282 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02004283 error_report("Invalid buffer size specified");
4284 return 1;
4285 }
4286
4287 bufsize = sval;
4288 break;
4289 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02004290 case 'S':
4291 {
4292 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02004293
Markus Armbruster606caa02017-02-21 21:14:04 +01004294 sval = cvtnum(optarg);
4295 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02004296 error_report("Invalid step size specified");
4297 return 1;
4298 }
4299
4300 step = sval;
4301 break;
4302 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004303 case 't':
4304 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
4305 if (ret < 0) {
4306 error_report("Invalid cache mode");
4307 ret = -1;
4308 goto out;
4309 }
4310 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004311 case 'w':
4312 flags |= BDRV_O_RDWR;
4313 is_write = true;
4314 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004315 case 'U':
4316 force_share = true;
4317 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004318 case OPTION_PATTERN:
4319 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004320 unsigned long res;
4321
4322 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004323 error_report("Invalid pattern byte specified");
4324 return 1;
4325 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004326 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004327 break;
4328 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02004329 case OPTION_FLUSH_INTERVAL:
4330 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004331 unsigned long res;
4332
4333 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004334 error_report("Invalid flush interval specified");
4335 return 1;
4336 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004337 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004338 break;
4339 }
4340 case OPTION_NO_DRAIN:
4341 drain_on_flush = false;
4342 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004343 case OPTION_IMAGE_OPTS:
4344 image_opts = true;
4345 break;
4346 }
4347 }
4348
4349 if (optind != argc - 1) {
4350 error_exit("Expecting one image file name");
4351 }
4352 filename = argv[argc - 1];
4353
Kevin Wolf55d539c2016-06-03 13:59:41 +02004354 if (!is_write && flush_interval) {
4355 error_report("--flush-interval is only available in write tests");
4356 ret = -1;
4357 goto out;
4358 }
4359 if (flush_interval && flush_interval < depth) {
4360 error_report("Flush interval can't be smaller than depth");
4361 ret = -1;
4362 goto out;
4363 }
4364
Fam Zheng335e9932017-05-03 00:35:39 +08004365 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4366 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004367 if (!blk) {
4368 ret = -1;
4369 goto out;
4370 }
4371
4372 image_size = blk_getlength(blk);
4373 if (image_size < 0) {
4374 ret = image_size;
4375 goto out;
4376 }
4377
4378 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004379 .blk = blk,
4380 .image_size = image_size,
4381 .bufsize = bufsize,
4382 .step = step ?: bufsize,
4383 .nrreq = depth,
4384 .n = count,
4385 .offset = offset,
4386 .write = is_write,
4387 .flush_interval = flush_interval,
4388 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004389 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004390 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004391 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004392 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004393 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004394 if (flush_interval) {
4395 printf("Sending flush every %d requests\n", flush_interval);
4396 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004397
Fam Zheng79d46582018-01-16 14:08:58 +08004398 buf_size = data.nrreq * data.bufsize;
4399 data.buf = blk_blockalign(blk, buf_size);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004400 memset(data.buf, pattern, data.nrreq * data.bufsize);
4401
Fam Zheng79d46582018-01-16 14:08:58 +08004402 blk_register_buf(blk, data.buf, buf_size);
4403
Kevin Wolfb6133b82014-08-05 14:17:13 +02004404 data.qiov = g_new(QEMUIOVector, data.nrreq);
4405 for (i = 0; i < data.nrreq; i++) {
4406 qemu_iovec_init(&data.qiov[i], 1);
4407 qemu_iovec_add(&data.qiov[i],
4408 data.buf + i * data.bufsize, data.bufsize);
4409 }
4410
4411 gettimeofday(&t1, NULL);
4412 bench_cb(&data, 0);
4413
4414 while (data.n > 0) {
4415 main_loop_wait(false);
4416 }
4417 gettimeofday(&t2, NULL);
4418
4419 printf("Run completed in %3.3f seconds.\n",
4420 (t2.tv_sec - t1.tv_sec)
4421 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4422
4423out:
Fam Zheng79d46582018-01-16 14:08:58 +08004424 if (data.buf) {
4425 blk_unregister_buf(blk, data.buf);
4426 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004427 qemu_vfree(data.buf);
4428 blk_unref(blk);
4429
4430 if (ret) {
4431 return 1;
4432 }
4433 return 0;
4434}
4435
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004436#define C_BS 01
4437#define C_COUNT 02
4438#define C_IF 04
4439#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004440#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004441
4442struct DdInfo {
4443 unsigned int flags;
4444 int64_t count;
4445};
4446
4447struct DdIo {
4448 int bsz; /* Block size */
4449 char *filename;
4450 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004451 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004452};
4453
4454struct DdOpts {
4455 const char *name;
4456 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4457 unsigned int flag;
4458};
4459
4460static int img_dd_bs(const char *arg,
4461 struct DdIo *in, struct DdIo *out,
4462 struct DdInfo *dd)
4463{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004464 int64_t res;
4465
Markus Armbruster606caa02017-02-21 21:14:04 +01004466 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004467
Markus Armbruster606caa02017-02-21 21:14:04 +01004468 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004469 error_report("invalid number: '%s'", arg);
4470 return 1;
4471 }
4472 in->bsz = out->bsz = res;
4473
4474 return 0;
4475}
4476
4477static int img_dd_count(const char *arg,
4478 struct DdIo *in, struct DdIo *out,
4479 struct DdInfo *dd)
4480{
Markus Armbruster606caa02017-02-21 21:14:04 +01004481 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004482
Markus Armbruster606caa02017-02-21 21:14:04 +01004483 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004484 error_report("invalid number: '%s'", arg);
4485 return 1;
4486 }
4487
4488 return 0;
4489}
4490
4491static int img_dd_if(const char *arg,
4492 struct DdIo *in, struct DdIo *out,
4493 struct DdInfo *dd)
4494{
4495 in->filename = g_strdup(arg);
4496
4497 return 0;
4498}
4499
4500static int img_dd_of(const char *arg,
4501 struct DdIo *in, struct DdIo *out,
4502 struct DdInfo *dd)
4503{
4504 out->filename = g_strdup(arg);
4505
4506 return 0;
4507}
4508
Reda Sallahif7c15532016-08-10 16:16:09 +02004509static int img_dd_skip(const char *arg,
4510 struct DdIo *in, struct DdIo *out,
4511 struct DdInfo *dd)
4512{
Markus Armbruster606caa02017-02-21 21:14:04 +01004513 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004514
Markus Armbruster606caa02017-02-21 21:14:04 +01004515 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004516 error_report("invalid number: '%s'", arg);
4517 return 1;
4518 }
4519
4520 return 0;
4521}
4522
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004523static int img_dd(int argc, char **argv)
4524{
4525 int ret = 0;
4526 char *arg = NULL;
4527 char *tmp;
4528 BlockDriver *drv = NULL, *proto_drv = NULL;
4529 BlockBackend *blk1 = NULL, *blk2 = NULL;
4530 QemuOpts *opts = NULL;
4531 QemuOptsList *create_opts = NULL;
4532 Error *local_err = NULL;
4533 bool image_opts = false;
4534 int c, i;
4535 const char *out_fmt = "raw";
4536 const char *fmt = NULL;
4537 int64_t size = 0;
4538 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004539 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004540 struct DdInfo dd = {
4541 .flags = 0,
4542 .count = 0,
4543 };
4544 struct DdIo in = {
4545 .bsz = 512, /* Block size is by default 512 bytes */
4546 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004547 .buf = NULL,
4548 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004549 };
4550 struct DdIo out = {
4551 .bsz = 512,
4552 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004553 .buf = NULL,
4554 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004555 };
4556
4557 const struct DdOpts options[] = {
4558 { "bs", img_dd_bs, C_BS },
4559 { "count", img_dd_count, C_COUNT },
4560 { "if", img_dd_if, C_IF },
4561 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004562 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004563 { NULL, NULL, 0 }
4564 };
4565 const struct option long_options[] = {
4566 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004567 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004568 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004569 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004570 { 0, 0, 0, 0 }
4571 };
4572
Fam Zheng335e9932017-05-03 00:35:39 +08004573 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004574 if (c == EOF) {
4575 break;
4576 }
4577 switch (c) {
4578 case 'O':
4579 out_fmt = optarg;
4580 break;
4581 case 'f':
4582 fmt = optarg;
4583 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004584 case ':':
4585 missing_argument(argv[optind - 1]);
4586 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004587 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004588 unrecognized_option(argv[optind - 1]);
4589 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004590 case 'h':
4591 help();
4592 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004593 case 'U':
4594 force_share = true;
4595 break;
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004596 case OPTION_OBJECT:
4597 if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004598 ret = -1;
4599 goto out;
4600 }
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004601 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004602 case OPTION_IMAGE_OPTS:
4603 image_opts = true;
4604 break;
4605 }
4606 }
4607
4608 for (i = optind; i < argc; i++) {
4609 int j;
4610 arg = g_strdup(argv[i]);
4611
4612 tmp = strchr(arg, '=');
4613 if (tmp == NULL) {
4614 error_report("unrecognized operand %s", arg);
4615 ret = -1;
4616 goto out;
4617 }
4618
4619 *tmp++ = '\0';
4620
4621 for (j = 0; options[j].name != NULL; j++) {
4622 if (!strcmp(arg, options[j].name)) {
4623 break;
4624 }
4625 }
4626 if (options[j].name == NULL) {
4627 error_report("unrecognized operand %s", arg);
4628 ret = -1;
4629 goto out;
4630 }
4631
4632 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4633 ret = -1;
4634 goto out;
4635 }
4636 dd.flags |= options[j].flag;
4637 g_free(arg);
4638 arg = NULL;
4639 }
4640
4641 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4642 error_report("Must specify both input and output files");
4643 ret = -1;
4644 goto out;
4645 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004646
4647 if (qemu_opts_foreach(&qemu_object_opts,
4648 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02004649 qemu_img_object_print_help, &error_fatal)) {
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004650 ret = -1;
4651 goto out;
4652 }
4653
Fam Zheng335e9932017-05-03 00:35:39 +08004654 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4655 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004656
4657 if (!blk1) {
4658 ret = -1;
4659 goto out;
4660 }
4661
4662 drv = bdrv_find_format(out_fmt);
4663 if (!drv) {
4664 error_report("Unknown file format");
4665 ret = -1;
4666 goto out;
4667 }
4668 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4669
4670 if (!proto_drv) {
4671 error_report_err(local_err);
4672 ret = -1;
4673 goto out;
4674 }
4675 if (!drv->create_opts) {
4676 error_report("Format driver '%s' does not support image creation",
4677 drv->format_name);
4678 ret = -1;
4679 goto out;
4680 }
4681 if (!proto_drv->create_opts) {
4682 error_report("Protocol driver '%s' does not support image creation",
4683 proto_drv->format_name);
4684 ret = -1;
4685 goto out;
4686 }
4687 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4688 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4689
4690 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4691
4692 size = blk_getlength(blk1);
4693 if (size < 0) {
4694 error_report("Failed to get size for '%s'", in.filename);
4695 ret = -1;
4696 goto out;
4697 }
4698
4699 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4700 dd.count * in.bsz < size) {
4701 size = dd.count * in.bsz;
4702 }
4703
Reda Sallahif7c15532016-08-10 16:16:09 +02004704 /* Overflow means the specified offset is beyond input image's size */
4705 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4706 size < in.bsz * in.offset)) {
4707 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4708 } else {
4709 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4710 size - in.bsz * in.offset, &error_abort);
4711 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004712
4713 ret = bdrv_create(drv, out.filename, opts, &local_err);
4714 if (ret < 0) {
4715 error_reportf_err(local_err,
4716 "%s: error while creating output image: ",
4717 out.filename);
4718 ret = -1;
4719 goto out;
4720 }
4721
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004722 /* TODO, we can't honour --image-opts for the target,
4723 * since it needs to be given in a format compatible
4724 * with the bdrv_create() call above which does not
4725 * support image-opts style.
4726 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01004727 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004728 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004729
4730 if (!blk2) {
4731 ret = -1;
4732 goto out;
4733 }
4734
Reda Sallahif7c15532016-08-10 16:16:09 +02004735 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4736 size < in.offset * in.bsz)) {
4737 /* We give a warning if the skip option is bigger than the input
4738 * size and create an empty output disk image (i.e. like dd(1)).
4739 */
4740 error_report("%s: cannot skip to specified offset", in.filename);
4741 in_pos = size;
4742 } else {
4743 in_pos = in.offset * in.bsz;
4744 }
4745
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004746 in.buf = g_new(uint8_t, in.bsz);
4747
Reda Sallahif7c15532016-08-10 16:16:09 +02004748 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004749 int in_ret, out_ret;
4750
4751 if (in_pos + in.bsz > size) {
4752 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4753 } else {
4754 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4755 }
4756 if (in_ret < 0) {
4757 error_report("error while reading from input image file: %s",
4758 strerror(-in_ret));
4759 ret = -1;
4760 goto out;
4761 }
4762 in_pos += in_ret;
4763
4764 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4765
4766 if (out_ret < 0) {
4767 error_report("error while writing to output image file: %s",
4768 strerror(-out_ret));
4769 ret = -1;
4770 goto out;
4771 }
4772 out_pos += out_ret;
4773 }
4774
4775out:
4776 g_free(arg);
4777 qemu_opts_del(opts);
4778 qemu_opts_free(create_opts);
4779 blk_unref(blk1);
4780 blk_unref(blk2);
4781 g_free(in.filename);
4782 g_free(out.filename);
4783 g_free(in.buf);
4784 g_free(out.buf);
4785
4786 if (ret) {
4787 return 1;
4788 }
4789 return 0;
4790}
4791
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004792static void dump_json_block_measure_info(BlockMeasureInfo *info)
4793{
4794 QString *str;
4795 QObject *obj;
4796 Visitor *v = qobject_output_visitor_new(&obj);
4797
4798 visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
4799 visit_complete(v, &obj);
4800 str = qobject_to_json_pretty(obj);
4801 assert(str != NULL);
4802 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004803 qobject_unref(obj);
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004804 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004805 qobject_unref(str);
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004806}
4807
4808static int img_measure(int argc, char **argv)
4809{
4810 static const struct option long_options[] = {
4811 {"help", no_argument, 0, 'h'},
4812 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4813 {"object", required_argument, 0, OPTION_OBJECT},
4814 {"output", required_argument, 0, OPTION_OUTPUT},
4815 {"size", required_argument, 0, OPTION_SIZE},
4816 {"force-share", no_argument, 0, 'U'},
4817 {0, 0, 0, 0}
4818 };
4819 OutputFormat output_format = OFORMAT_HUMAN;
4820 BlockBackend *in_blk = NULL;
4821 BlockDriver *drv;
4822 const char *filename = NULL;
4823 const char *fmt = NULL;
4824 const char *out_fmt = "raw";
4825 char *options = NULL;
4826 char *snapshot_name = NULL;
4827 bool force_share = false;
4828 QemuOpts *opts = NULL;
4829 QemuOpts *object_opts = NULL;
4830 QemuOpts *sn_opts = NULL;
4831 QemuOptsList *create_opts = NULL;
4832 bool image_opts = false;
4833 uint64_t img_size = UINT64_MAX;
4834 BlockMeasureInfo *info = NULL;
4835 Error *local_err = NULL;
4836 int ret = 1;
4837 int c;
4838
4839 while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
4840 long_options, NULL)) != -1) {
4841 switch (c) {
4842 case '?':
4843 case 'h':
4844 help();
4845 break;
4846 case 'f':
4847 fmt = optarg;
4848 break;
4849 case 'O':
4850 out_fmt = optarg;
4851 break;
4852 case 'o':
Markus Armbruster6d2b5cb2020-04-15 09:49:25 +02004853 if (accumulate_options(&options, optarg) < 0) {
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004854 goto out;
4855 }
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004856 break;
4857 case 'l':
4858 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
4859 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
4860 optarg, false);
4861 if (!sn_opts) {
4862 error_report("Failed in parsing snapshot param '%s'",
4863 optarg);
4864 goto out;
4865 }
4866 } else {
4867 snapshot_name = optarg;
4868 }
4869 break;
4870 case 'U':
4871 force_share = true;
4872 break;
4873 case OPTION_OBJECT:
4874 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
4875 optarg, true);
4876 if (!object_opts) {
4877 goto out;
4878 }
4879 break;
4880 case OPTION_IMAGE_OPTS:
4881 image_opts = true;
4882 break;
4883 case OPTION_OUTPUT:
4884 if (!strcmp(optarg, "json")) {
4885 output_format = OFORMAT_JSON;
4886 } else if (!strcmp(optarg, "human")) {
4887 output_format = OFORMAT_HUMAN;
4888 } else {
4889 error_report("--output must be used with human or json "
4890 "as argument.");
4891 goto out;
4892 }
4893 break;
4894 case OPTION_SIZE:
4895 {
4896 int64_t sval;
4897
4898 sval = cvtnum(optarg);
4899 if (sval < 0) {
4900 if (sval == -ERANGE) {
4901 error_report("Image size must be less than 8 EiB!");
4902 } else {
4903 error_report("Invalid image size specified! You may use "
4904 "k, M, G, T, P or E suffixes for ");
4905 error_report("kilobytes, megabytes, gigabytes, terabytes, "
4906 "petabytes and exabytes.");
4907 }
4908 goto out;
4909 }
4910 img_size = (uint64_t)sval;
4911 }
4912 break;
4913 }
4914 }
4915
4916 if (qemu_opts_foreach(&qemu_object_opts,
4917 user_creatable_add_opts_foreach,
Kevin Wolfc6e5cdf2019-10-11 21:49:17 +02004918 qemu_img_object_print_help, &error_fatal)) {
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004919 goto out;
4920 }
4921
4922 if (argc - optind > 1) {
4923 error_report("At most one filename argument is allowed.");
4924 goto out;
4925 } else if (argc - optind == 1) {
4926 filename = argv[optind];
4927 }
4928
Stefan Hajnoczic3673dc2020-02-21 11:25:21 +00004929 if (!filename && (image_opts || fmt || snapshot_name || sn_opts)) {
4930 error_report("--image-opts, -f, and -l require a filename argument.");
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004931 goto out;
4932 }
4933 if (filename && img_size != UINT64_MAX) {
4934 error_report("--size N cannot be used together with a filename.");
4935 goto out;
4936 }
4937 if (!filename && img_size == UINT64_MAX) {
4938 error_report("Either --size N or one filename must be specified.");
4939 goto out;
4940 }
4941
4942 if (filename) {
4943 in_blk = img_open(image_opts, filename, fmt, 0,
4944 false, false, force_share);
4945 if (!in_blk) {
4946 goto out;
4947 }
4948
4949 if (sn_opts) {
4950 bdrv_snapshot_load_tmp(blk_bs(in_blk),
4951 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
4952 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
4953 &local_err);
4954 } else if (snapshot_name != NULL) {
4955 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
4956 snapshot_name, &local_err);
4957 }
4958 if (local_err) {
4959 error_reportf_err(local_err, "Failed to load snapshot: ");
4960 goto out;
4961 }
4962 }
4963
4964 drv = bdrv_find_format(out_fmt);
4965 if (!drv) {
4966 error_report("Unknown file format '%s'", out_fmt);
4967 goto out;
4968 }
4969 if (!drv->create_opts) {
4970 error_report("Format driver '%s' does not support image creation",
4971 drv->format_name);
4972 goto out;
4973 }
4974
4975 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4976 create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
4977 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4978 if (options) {
4979 qemu_opts_do_parse(opts, options, NULL, &local_err);
4980 if (local_err) {
4981 error_report_err(local_err);
4982 error_report("Invalid options for file format '%s'", out_fmt);
4983 goto out;
4984 }
4985 }
4986 if (img_size != UINT64_MAX) {
4987 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
4988 }
4989
4990 info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
4991 if (local_err) {
4992 error_report_err(local_err);
4993 goto out;
4994 }
4995
4996 if (output_format == OFORMAT_HUMAN) {
4997 printf("required size: %" PRIu64 "\n", info->required);
4998 printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
4999 } else {
5000 dump_json_block_measure_info(info);
5001 }
5002
5003 ret = 0;
5004
5005out:
5006 qapi_free_BlockMeasureInfo(info);
5007 qemu_opts_del(object_opts);
5008 qemu_opts_del(opts);
5009 qemu_opts_del(sn_opts);
5010 qemu_opts_free(create_opts);
5011 g_free(options);
5012 blk_unref(in_blk);
5013 return ret;
5014}
Kevin Wolfb6133b82014-08-05 14:17:13 +02005015
Anthony Liguoric227f092009-10-01 16:12:16 -05005016static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01005017#define DEF(option, callback, arg_string) \
5018 { option, callback },
5019#include "qemu-img-cmds.h"
5020#undef DEF
Stuart Brady153859b2009-06-07 00:42:17 +01005021 { NULL, NULL, },
5022};
5023
bellardea2384d2004-08-01 21:59:26 +00005024int main(int argc, char **argv)
5025{
Anthony Liguoric227f092009-10-01 16:12:16 -05005026 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01005027 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03005028 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03005029 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04005030 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04005031 static const struct option long_options[] = {
5032 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03005033 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03005034 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04005035 {0, 0, 0, 0}
5036 };
bellardea2384d2004-08-01 21:59:26 +00005037
MORITA Kazutaka526eda12013-07-23 17:30:11 +09005038#ifdef CONFIG_POSIX
5039 signal(SIGPIPE, SIG_IGN);
5040#endif
5041
Christophe Fergeauf5852ef2019-01-31 17:46:14 +01005042 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01005043 module_call_init(MODULE_INIT_TRACE);
Fam Zheng10f5bff2014-02-10 14:48:51 +08005044 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01005045
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03005046 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01005047 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03005048 exit(EXIT_FAILURE);
5049 }
5050
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03005051 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01005052
Daniel P. Berrange064097d2016-02-10 18:41:01 +00005053 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00005054 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08005055 if (argc < 2) {
5056 error_exit("Not enough arguments");
5057 }
Stuart Brady153859b2009-06-07 00:42:17 +01005058
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00005059 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00005060 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03005061 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00005062
Stefan Hajnoczic9192972017-03-17 18:45:41 +08005063 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03005064 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08005065 case ':':
5066 missing_argument(argv[optind - 1]);
5067 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08005068 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08005069 unrecognized_option(argv[optind - 1]);
5070 return 0;
5071 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03005072 help();
5073 return 0;
5074 case 'V':
5075 printf(QEMU_IMG_VERSION);
5076 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03005077 case 'T':
5078 g_free(trace_file);
5079 trace_file = trace_opt_parse(optarg);
5080 break;
Stuart Brady153859b2009-06-07 00:42:17 +01005081 }
bellardea2384d2004-08-01 21:59:26 +00005082 }
Stuart Brady153859b2009-06-07 00:42:17 +01005083
Denis V. Lunev10985132016-06-17 17:44:13 +03005084 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04005085
Denis V. Lunev10985132016-06-17 17:44:13 +03005086 /* reset getopt_long scanning */
5087 argc -= optind;
5088 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04005089 return 0;
5090 }
Denis V. Lunev10985132016-06-17 17:44:13 +03005091 argv += optind;
Richard W.M. Jonesd339d762019-01-18 10:11:14 +00005092 qemu_reset_optind();
Denis V. Lunev10985132016-06-17 17:44:13 +03005093
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03005094 if (!trace_init_backends()) {
5095 exit(1);
5096 }
5097 trace_init_file(trace_file);
5098 qemu_set_log(LOG_TRACE);
5099
Denis V. Lunev10985132016-06-17 17:44:13 +03005100 /* find the command */
5101 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
5102 if (!strcmp(cmdname, cmd->name)) {
5103 return cmd->handler(argc, argv);
5104 }
5105 }
Jeff Cody7db16892014-04-25 17:02:32 -04005106
Stuart Brady153859b2009-06-07 00:42:17 +01005107 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08005108 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00005109}