blob: 5dffb3e616010cca465bbcacd4c33862dda4a8b6 [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"
Eric Blake3b51ab42020-05-12 20:16:45 -050031#include "qapi/qapi-commands-block-core.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010032#include "qapi/qapi-visit-block-core.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010033#include "qapi/qobject-output-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010034#include "qapi/qmp/qjson.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010035#include "qapi/qmp/qdict.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"
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +010043#include "qemu/sockets.h"
Stefano Garzarella97ede572019-05-08 12:43:24 +020044#include "qemu/units.h"
Peter Maydell5df022c2022-02-26 18:07:23 +000045#include "qemu/memalign.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000046#include "qom/object_interfaces.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020047#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010048#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020049#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080050#include "block/qapi.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010051#include "crypto/init.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030052#include "trace/control.h"
Zhengui0c8c4892020-10-20 14:47:44 +000053#include "qemu/throttle.h"
54#include "block/throttle-groups.h"
bellarde8445332006-06-14 15:32:10 +000055
Thomas Huth7e563bf2018-02-15 12:06:47 +010056#define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \
Thomas Huth0781dd62016-10-05 11:54:44 +020057 "\n" QEMU_COPYRIGHT "\n"
Jeff Cody5f6979c2014-04-28 14:37:18 -040058
Anthony Liguoric227f092009-10-01 16:12:16 -050059typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010060 const char *name;
61 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050062} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010063
Federico Simoncelli8599ea42013-01-28 06:59:47 -050064enum {
65 OPTION_OUTPUT = 256,
66 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000067 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000068 OPTION_IMAGE_OPTS = 259,
Kevin Wolfb6495fa2015-07-10 18:09:18 +020069 OPTION_PATTERN = 260,
Kevin Wolf55d539c2016-06-03 13:59:41 +020070 OPTION_FLUSH_INTERVAL = 261,
71 OPTION_NO_DRAIN = 262,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +010072 OPTION_TARGET_IMAGE_OPTS = 263,
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +010073 OPTION_SIZE = 264,
Max Reitzdc5f6902017-06-13 22:20:55 +020074 OPTION_PREALLOCATION = 265,
Pavel Butsykin4ffca892017-09-18 15:42:27 +030075 OPTION_SHRINK = 266,
Max Reitz8eaac022019-05-07 22:35:03 +020076 OPTION_SALVAGE = 267,
David Edmondson168468f2020-02-05 11:02:48 +000077 OPTION_TARGET_IS_ZERO = 268,
Eric Blake3b51ab42020-05-12 20:16:45 -050078 OPTION_ADD = 269,
79 OPTION_REMOVE = 270,
80 OPTION_CLEAR = 271,
81 OPTION_ENABLE = 272,
82 OPTION_DISABLE = 273,
83 OPTION_MERGE = 274,
Eric Blake15e39ad2020-05-21 14:21:36 -050084 OPTION_BITMAPS = 275,
Maxim Levitskya3579bf2020-06-25 14:55:38 +020085 OPTION_FORCE = 276,
Eric Blake955171e2021-07-21 10:53:48 -050086 OPTION_SKIP_BROKEN = 277,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050087};
88
89typedef enum OutputFormat {
90 OFORMAT_JSON,
91 OFORMAT_HUMAN,
92} OutputFormat;
93
Kevin Wolfe6996142016-03-15 13:03:11 +010094/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040095#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000096
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010097static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000098{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010099 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +0000100}
101
Fam Zhengac1307a2014-04-22 13:36:11 +0800102static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
103{
104 va_list ap;
105
Fam Zhengac1307a2014-04-22 13:36:11 +0800106 va_start(ap, fmt);
Markus Armbrustere9e1d922019-04-17 21:06:27 +0200107 error_vreport(fmt, ap);
Fam Zhengac1307a2014-04-22 13:36:11 +0800108 va_end(ap);
109
Markus Armbrustere9e1d922019-04-17 21:06:27 +0200110 error_printf("Try 'qemu-img --help' for more information\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800111 exit(EXIT_FAILURE);
112}
113
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800114static void QEMU_NORETURN missing_argument(const char *option)
115{
116 error_exit("missing argument for option '%s'", option);
117}
118
119static void QEMU_NORETURN unrecognized_option(const char *option)
120{
121 error_exit("unrecognized option '%s'", option);
122}
123
Eric Blake0562adf2020-05-12 20:16:41 -0500124/* Please keep in synch with docs/tools/qemu-img.rst */
Fam Zhengac1307a2014-04-22 13:36:11 +0800125static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +0000126{
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100127 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -0400128 QEMU_IMG_VERSION
Denis V. Lunev10985132016-06-17 17:44:13 +0300129 "usage: qemu-img [standard options] command [command options]\n"
malc3f020d72010-02-08 12:04:56 +0300130 "QEMU disk image utility\n"
131 "\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300132 " '-h', '--help' display this help and exit\n"
133 " '-V', '--version' output version information and exit\n"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +0300134 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
135 " specify tracing options\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300136 "\n"
malc3f020d72010-02-08 12:04:56 +0300137 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100138#define DEF(option, callback, arg_string) \
139 " " arg_string "\n"
140#include "qemu-img-cmds.h"
141#undef DEF
malc3f020d72010-02-08 12:04:56 +0300142 "\n"
143 "Command parameters:\n"
144 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000145 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
146 " manual page for a description of the object properties. The most common\n"
147 " object type is a 'secret', which is used to supply passwords and/or\n"
148 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300149 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400150 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800151 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
152 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100153 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
154 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300155 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200156 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
157 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
158 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300159 " 'output_filename' is the destination disk image filename\n"
160 " 'output_fmt' is the destination format\n"
161 " 'options' is a comma separated list of format specific options in a\n"
162 " name=value format. Use -o ? for an overview of the options supported by the\n"
163 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800164 " 'snapshot_param' is param used for internal snapshot, format\n"
165 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
166 " '[ID_OR_NAME]'\n"
malc3f020d72010-02-08 12:04:56 +0300167 " '-c' indicates that target image must be compressed (qcow format only)\n"
John Snow6e6e55f2017-07-17 20:34:22 -0400168 " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
169 " new backing file match exactly. The image doesn't need a working\n"
170 " backing file before rebasing in this case (useful for renaming the\n"
171 " backing file). For image creation, allow creating without attempting\n"
172 " to open the backing file.\n"
malc3f020d72010-02-08 12:04:56 +0300173 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200174 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100175 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200176 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
177 " contain only zeros for qemu-img to create a sparse image during\n"
178 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
179 " unallocated or zero sectors, and the destination image will always be\n"
180 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200181 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100182 " '-n' skips the target volume creation (useful if the volume is created\n"
183 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300184 "\n"
Eric Blake3b51ab42020-05-12 20:16:45 -0500185 "Parameters to bitmap subcommand:\n"
186 " 'bitmap' is the name of the bitmap to manipulate, through one or more\n"
187 " actions from '--add', '--remove', '--clear', '--enable', '--disable',\n"
188 " or '--merge source'\n"
189 " '-g granularity' sets the granularity for '--add' actions\n"
190 " '-b source' and '-F src_fmt' tell '--merge' actions to find the source\n"
191 " bitmaps from an alternative file\n"
192 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200193 "Parameters to check subcommand:\n"
194 " '-r' tries to repair any inconsistencies that are found during the check.\n"
195 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
196 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200197 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200198 "\n"
Peter Lieven2d9187b2017-02-28 13:40:07 +0100199 "Parameters to convert subcommand:\n"
Eric Blake15e39ad2020-05-21 14:21:36 -0500200 " '--bitmaps' copies all top-level persistent bitmaps to destination\n"
Peter Lieven2d9187b2017-02-28 13:40:07 +0100201 " '-m' specifies how many coroutines work in parallel during the convert\n"
202 " process (defaults to 8)\n"
203 " '-W' allow to write to the target out of order rather than sequential\n"
204 "\n"
malc3f020d72010-02-08 12:04:56 +0300205 "Parameters to snapshot subcommand:\n"
206 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
207 " '-a' applies a snapshot (revert disk to saved state)\n"
208 " '-c' creates a snapshot\n"
209 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100210 " '-l' lists all snapshots in the given image\n"
211 "\n"
212 "Parameters to compare subcommand:\n"
213 " '-f' first image format\n"
214 " '-F' second image format\n"
Reda Sallahi86ce1f62016-08-10 04:43:12 +0200215 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
216 "\n"
217 "Parameters to dd subcommand:\n"
218 " 'bs=BYTES' read and write up to BYTES bytes at a time "
219 "(default: 512)\n"
220 " 'count=N' copy only N input blocks\n"
221 " 'if=FILE' read from FILE\n"
Reda Sallahif7c15532016-08-10 16:16:09 +0200222 " 'of=FILE' write to FILE\n"
223 " 'skip=N' skip N bs-sized blocks at the start of input\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100224
225 printf("%s\nSupported formats:", help_msg);
Andrey Shinkevich9ac404c2019-03-07 16:33:58 +0300226 bdrv_iterate_format(format_print, NULL, false);
Eric Blakef5048cb2017-08-03 11:33:53 -0500227 printf("\n\n" QEMU_HELP_BOTTOM "\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800228 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000229}
230
Markus Armbruster80c710c2020-04-15 09:49:26 +0200231/*
232 * Is @optarg safe for accumulate_options()?
233 * It is when multiple of them can be joined together separated by ','.
234 * To make that work, @optarg must not start with ',' (or else a
235 * separating ',' preceding it gets escaped), and it must not end with
236 * an odd number of ',' (or else a separating ',' following it gets
Markus Armbrusterf62514b2020-04-15 09:49:27 +0200237 * escaped), or be empty (or else a separating ',' preceding it can
238 * escape a separating ',' following it).
239 *
Markus Armbruster80c710c2020-04-15 09:49:26 +0200240 */
241static bool is_valid_option_list(const char *optarg)
242{
243 size_t len = strlen(optarg);
244 size_t i;
245
Markus Armbrusterf62514b2020-04-15 09:49:27 +0200246 if (!optarg[0] || optarg[0] == ',') {
Markus Armbruster80c710c2020-04-15 09:49:26 +0200247 return false;
248 }
249
250 for (i = len; i > 0 && optarg[i - 1] == ','; i--) {
251 }
252 if ((len - i) % 2) {
253 return false;
254 }
255
256 return true;
257}
258
Markus Armbruster6d2b5cb2020-04-15 09:49:25 +0200259static int accumulate_options(char **options, char *optarg)
260{
261 char *new_options;
262
263 if (!is_valid_option_list(optarg)) {
264 error_report("Invalid option list: %s", optarg);
265 return -1;
266 }
267
268 if (!*options) {
269 *options = g_strdup(optarg);
270 } else {
271 new_options = g_strdup_printf("%s,%s", *options, optarg);
272 g_free(*options);
273 *options = new_options;
274 }
275 return 0;
276}
277
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000278static QemuOptsList qemu_source_opts = {
279 .name = "source",
280 .implied_opt_name = "file",
281 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
282 .desc = {
283 { }
284 },
285};
286
Stefan Weil7c30f652013-06-16 17:01:05 +0200287static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100288{
289 int ret = 0;
290 if (!quiet) {
291 va_list args;
292 va_start(args, fmt);
293 ret = vprintf(fmt, args);
294 va_end(args);
295 }
296 return ret;
297}
298
bellardea2384d2004-08-01 21:59:26 +0000299
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100300static int print_block_option_help(const char *filename, const char *fmt)
301{
302 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800303 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500304 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100305
306 /* Find driver and parse its options */
307 drv = bdrv_find_format(fmt);
308 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100309 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100310 return 1;
311 }
312
Max Reitzd402b6a2018-05-09 23:00:21 +0200313 if (!drv->create_opts) {
314 error_report("Format driver '%s' does not support image creation", fmt);
315 return 1;
316 }
317
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800318 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100319 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500320 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100321 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100322 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800323 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100324 return 1;
325 }
Max Reitzd402b6a2018-05-09 23:00:21 +0200326 if (!proto_drv->create_opts) {
Max Reitzf0998872018-11-19 11:19:20 +0100327 error_report("Protocol driver '%s' does not support image creation",
Max Reitzd402b6a2018-05-09 23:00:21 +0200328 proto_drv->format_name);
Max Reitz3ecd5a42018-11-19 11:19:21 +0100329 qemu_opts_free(create_opts);
Max Reitzd402b6a2018-05-09 23:00:21 +0200330 return 1;
331 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800332 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100333 }
334
Max Reitzf4619af2019-04-13 17:20:37 +0200335 if (filename) {
336 printf("Supported options:\n");
337 } else {
338 printf("Supported %s options:\n", fmt);
339 }
Max Reitz63898712018-10-19 18:49:25 +0200340 qemu_opts_print_help(create_opts, false);
Chunyan Liu83d05212014-06-05 17:20:51 +0800341 qemu_opts_free(create_opts);
Max Reitzf4619af2019-04-13 17:20:37 +0200342
343 if (!filename) {
344 printf("\n"
345 "The protocol level may support further options.\n"
346 "Specify the target filename to include those options.\n");
347 }
348
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100349 return 0;
350}
351
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000352
Max Reitzefaa7c42016-03-16 19:54:38 +0100353static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100354 QemuOpts *opts, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800355 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000356{
357 QDict *options;
358 Error *local_err = NULL;
359 BlockBackend *blk;
360 options = qemu_opts_to_qdict(opts, NULL);
Fam Zheng335e9932017-05-03 00:35:39 +0800361 if (force_share) {
362 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
Max Reitz4615f872018-05-02 22:20:50 +0200363 && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) {
Fam Zheng335e9932017-05-03 00:35:39 +0800364 error_report("--force-share/-U conflicts with image options");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200365 qobject_unref(options);
Fam Zheng335e9932017-05-03 00:35:39 +0800366 return NULL;
367 }
Max Reitz4615f872018-05-02 22:20:50 +0200368 qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on");
Fam Zheng335e9932017-05-03 00:35:39 +0800369 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100370 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000371 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100372 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000373 return NULL;
374 }
Kevin Wolfce099542016-03-15 13:01:04 +0100375 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000376
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000377 return blk;
378}
379
Max Reitzefaa7c42016-03-16 19:54:38 +0100380static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100381 QDict *options,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000382 const char *fmt, int flags,
Fam Zheng335e9932017-05-03 00:35:39 +0800383 bool writethrough, bool quiet,
384 bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000385{
386 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200387 Error *local_err = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100388
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100389 if (!options) {
390 options = qdict_new();
391 }
bellard75c23802004-08-27 21:28:58 +0000392 if (fmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500393 qdict_put_str(options, "driver", fmt);
bellard75c23802004-08-27 21:28:58 +0000394 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100395
Fam Zheng335e9932017-05-03 00:35:39 +0800396 if (force_share) {
Eric Blake579cf1d2017-05-15 14:54:39 -0500397 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800398 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100399 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500400 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100401 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000402 return NULL;
bellard75c23802004-08-27 21:28:58 +0000403 }
Kevin Wolfce099542016-03-15 13:01:04 +0100404 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100405
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200406 return blk;
bellard75c23802004-08-27 21:28:58 +0000407}
408
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000409
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100410static int img_add_key_secrets(void *opaque,
411 const char *name, const char *value,
412 Error **errp)
413{
414 QDict *options = opaque;
415
416 if (g_str_has_suffix(name, "key-secret")) {
Eric Blake187f47e2017-06-24 12:10:07 -0600417 qdict_put_str(options, name, value);
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100418 }
419
420 return 0;
421}
422
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100423
Max Reitzefaa7c42016-03-16 19:54:38 +0100424static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000425 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100426 const char *fmt, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800427 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000428{
429 BlockBackend *blk;
430 if (image_opts) {
431 QemuOpts *opts;
432 if (fmt) {
433 error_report("--image-opts and --format are mutually exclusive");
434 return NULL;
435 }
436 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
437 filename, true);
438 if (!opts) {
439 return NULL;
440 }
Fam Zheng335e9932017-05-03 00:35:39 +0800441 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
442 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000443 } else {
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100444 blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
Fam Zheng335e9932017-05-03 00:35:39 +0800445 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000446 }
447 return blk;
448}
449
450
Chunyan Liu83d05212014-06-05 17:20:51 +0800451static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100452 const char *base_filename,
453 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200454{
Kevin Wolfefa84d42009-05-18 16:42:12 +0200455 if (base_filename) {
Markus Armbruster235e59c2020-07-07 18:05:42 +0200456 if (!qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename,
Markus Armbruster9e194e02020-07-07 18:06:11 +0200457 NULL)) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100458 error_report("Backing file not supported for file format '%s'",
459 fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900460 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200461 }
462 }
463 if (base_fmt) {
Markus Armbruster9e194e02020-07-07 18:06:11 +0200464 if (!qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, NULL)) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100465 error_report("Backing file format not supported for file "
466 "format '%s'", fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900467 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200468 }
469 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900470 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200471}
472
Eyal Moscovici43d589b2020-05-13 16:36:26 +0300473static int64_t cvtnum_full(const char *name, const char *value, int64_t min,
474 int64_t max)
Markus Armbruster606caa02017-02-21 21:14:04 +0100475{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100476 int err;
Eyal Moscovici43d589b2020-05-13 16:36:26 +0300477 uint64_t res;
Markus Armbruster606caa02017-02-21 21:14:04 +0100478
Eyal Moscovici43d589b2020-05-13 16:36:26 +0300479 err = qemu_strtosz(value, NULL, &res);
480 if (err < 0 && err != -ERANGE) {
481 error_report("Invalid %s specified. You may use "
482 "k, M, G, T, P or E suffixes for", name);
483 error_report("kilobytes, megabytes, gigabytes, terabytes, "
484 "petabytes and exabytes.");
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100485 return err;
486 }
Eyal Moscovici43d589b2020-05-13 16:36:26 +0300487 if (err == -ERANGE || res > max || res < min) {
488 error_report("Invalid %s specified. Must be between %" PRId64
489 " and %" PRId64 ".", name, min, max);
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100490 return -ERANGE;
491 }
Eyal Moscovici43d589b2020-05-13 16:36:26 +0300492 return res;
493}
494
495static int64_t cvtnum(const char *name, const char *value)
496{
497 return cvtnum_full(name, value, 0, INT64_MAX);
Markus Armbruster606caa02017-02-21 21:14:04 +0100498}
499
bellardea2384d2004-08-01 21:59:26 +0000500static int img_create(int argc, char **argv)
501{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200502 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100503 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000504 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000505 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000506 const char *filename;
507 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200508 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200509 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100510 bool quiet = false;
John Snow6e6e55f2017-07-17 20:34:22 -0400511 int flags = 0;
ths3b46e622007-09-17 08:09:54 +0000512
bellardea2384d2004-08-01 21:59:26 +0000513 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000514 static const struct option long_options[] = {
515 {"help", no_argument, 0, 'h'},
516 {"object", required_argument, 0, OPTION_OBJECT},
517 {0, 0, 0, 0}
518 };
John Snow6e6e55f2017-07-17 20:34:22 -0400519 c = getopt_long(argc, argv, ":F:b:f:ho:qu",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000520 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100521 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000522 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100523 }
bellardea2384d2004-08-01 21:59:26 +0000524 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800525 case ':':
526 missing_argument(argv[optind - 1]);
527 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100528 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800529 unrecognized_option(argv[optind - 1]);
530 break;
bellardea2384d2004-08-01 21:59:26 +0000531 case 'h':
532 help();
533 break;
aliguori9230eaf2009-03-28 17:55:19 +0000534 case 'F':
535 base_fmt = optarg;
536 break;
bellardea2384d2004-08-01 21:59:26 +0000537 case 'b':
538 base_filename = optarg;
539 break;
540 case 'f':
541 fmt = optarg;
542 break;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200543 case 'o':
Markus Armbruster6d2b5cb2020-04-15 09:49:25 +0200544 if (accumulate_options(&options, optarg) < 0) {
Kevin Wolf77386bf2014-02-21 16:24:04 +0100545 goto fail;
546 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200547 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100548 case 'q':
549 quiet = true;
550 break;
John Snow6e6e55f2017-07-17 20:34:22 -0400551 case 'u':
552 flags |= BDRV_O_NO_BACKING;
553 break;
Kevin Wolf99b1e642021-02-17 12:56:45 +0100554 case OPTION_OBJECT:
555 user_creatable_process_cmdline(optarg);
556 break;
bellardea2384d2004-08-01 21:59:26 +0000557 }
558 }
aliguori9230eaf2009-03-28 17:55:19 +0000559
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900560 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100561 filename = (optind < argc) ? argv[optind] : NULL;
562 if (options && has_help_option(options)) {
563 g_free(options);
564 return print_block_option_help(filename, fmt);
565 }
566
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100567 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800568 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100569 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100570 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900571
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100572 /* Get image size, if specified */
573 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100574 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100575
Eyal Moscovici43d589b2020-05-13 16:36:26 +0300576 sval = cvtnum("image size", argv[optind++]);
Markus Armbruster606caa02017-02-21 21:14:04 +0100577 if (sval < 0) {
Kevin Wolf77386bf2014-02-21 16:24:04 +0100578 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100579 }
580 img_size = (uint64_t)sval;
581 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200582 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800583 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200584 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100585
Luiz Capitulino9b375252012-11-30 10:52:05 -0200586 bdrv_img_create(filename, fmt, base_filename, base_fmt,
John Snow6e6e55f2017-07-17 20:34:22 -0400587 options, img_size, flags, quiet, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100588 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100589 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100590 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900591 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200592
Kevin Wolf77386bf2014-02-21 16:24:04 +0100593 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000594 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100595
596fail:
597 g_free(options);
598 return 1;
bellardea2384d2004-08-01 21:59:26 +0000599}
600
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100601static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500602{
Markus Armbrustereab3a462020-12-11 18:11:37 +0100603 GString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500604 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100605 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600606
607 visit_type_ImageCheck(v, NULL, &check, &error_abort);
608 visit_complete(v, &obj);
Markus Armbruster6589f452020-12-11 18:11:35 +0100609 str = qobject_to_json_pretty(obj, true);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500610 assert(str != NULL);
Markus Armbrustereab3a462020-12-11 18:11:37 +0100611 qprintf(quiet, "%s\n", str->str);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200612 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600613 visit_free(v);
Markus Armbrustereab3a462020-12-11 18:11:37 +0100614 g_string_free(str, true);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500615}
616
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100617static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500618{
619 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100620 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500621 } else {
622 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100623 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
624 "Data may be corrupted, or further writes to the image "
625 "may corrupt it.\n",
626 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500627 }
628
629 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100630 qprintf(quiet,
631 "\n%" PRId64 " leaked clusters were found on the image.\n"
632 "This means waste of disk space, but no harm to data.\n",
633 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500634 }
635
636 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100637 qprintf(quiet,
638 "\n%" PRId64
639 " internal errors have occurred during the check.\n",
640 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500641 }
642 }
643
644 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100645 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
646 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
647 check->allocated_clusters, check->total_clusters,
648 check->allocated_clusters * 100.0 / check->total_clusters,
649 check->fragmented_clusters * 100.0 / check->allocated_clusters,
650 check->compressed_clusters * 100.0 /
651 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500652 }
653
654 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100655 qprintf(quiet,
656 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500657 }
658}
659
660static int collect_image_check(BlockDriverState *bs,
661 ImageCheck *check,
662 const char *filename,
663 const char *fmt,
664 int fix)
665{
666 int ret;
667 BdrvCheckResult result;
668
669 ret = bdrv_check(bs, &result, fix);
670 if (ret < 0) {
671 return ret;
672 }
673
674 check->filename = g_strdup(filename);
675 check->format = g_strdup(bdrv_get_format_name(bs));
676 check->check_errors = result.check_errors;
677 check->corruptions = result.corruptions;
678 check->has_corruptions = result.corruptions != 0;
679 check->leaks = result.leaks;
680 check->has_leaks = result.leaks != 0;
681 check->corruptions_fixed = result.corruptions_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100682 check->has_corruptions_fixed = result.corruptions_fixed != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500683 check->leaks_fixed = result.leaks_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100684 check->has_leaks_fixed = result.leaks_fixed != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500685 check->image_end_offset = result.image_end_offset;
686 check->has_image_end_offset = result.image_end_offset != 0;
687 check->total_clusters = result.bfi.total_clusters;
688 check->has_total_clusters = result.bfi.total_clusters != 0;
689 check->allocated_clusters = result.bfi.allocated_clusters;
690 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
691 check->fragmented_clusters = result.bfi.fragmented_clusters;
692 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100693 check->compressed_clusters = result.bfi.compressed_clusters;
694 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500695
696 return 0;
697}
698
Kevin Wolfe076f332010-06-29 11:43:13 +0200699/*
700 * Checks an image for consistency. Exit codes:
701 *
Max Reitzd6635c42014-06-02 22:15:21 +0200702 * 0 - Check completed, image is good
703 * 1 - Check not completed because of internal errors
704 * 2 - Check completed, image is corrupted
705 * 3 - Check completed, image has leaked clusters, but is good otherwise
706 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200707 */
aliguori15859692009-04-21 23:11:53 +0000708static int img_check(int argc, char **argv)
709{
710 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500711 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200712 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200713 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000714 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200715 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100716 int flags = BDRV_O_CHECK;
717 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200718 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100719 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000720 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +0800721 bool force_share = false;
aliguori15859692009-04-21 23:11:53 +0000722
723 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500724 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200725 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100726
aliguori15859692009-04-21 23:11:53 +0000727 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500728 int option_index = 0;
729 static const struct option long_options[] = {
730 {"help", no_argument, 0, 'h'},
731 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530732 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500733 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000734 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000735 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +0800736 {"force-share", no_argument, 0, 'U'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500737 {0, 0, 0, 0}
738 };
Fam Zheng335e9932017-05-03 00:35:39 +0800739 c = getopt_long(argc, argv, ":hf:r:T:qU",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500740 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100741 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000742 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100743 }
aliguori15859692009-04-21 23:11:53 +0000744 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800745 case ':':
746 missing_argument(argv[optind - 1]);
747 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100748 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800749 unrecognized_option(argv[optind - 1]);
750 break;
aliguori15859692009-04-21 23:11:53 +0000751 case 'h':
752 help();
753 break;
754 case 'f':
755 fmt = optarg;
756 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200757 case 'r':
758 flags |= BDRV_O_RDWR;
759
760 if (!strcmp(optarg, "leaks")) {
761 fix = BDRV_FIX_LEAKS;
762 } else if (!strcmp(optarg, "all")) {
763 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
764 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800765 error_exit("Unknown option value for -r "
766 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200767 }
768 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500769 case OPTION_OUTPUT:
770 output = optarg;
771 break;
Max Reitz40055952014-07-22 22:58:42 +0200772 case 'T':
773 cache = optarg;
774 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100775 case 'q':
776 quiet = true;
777 break;
Fam Zheng335e9932017-05-03 00:35:39 +0800778 case 'U':
779 force_share = true;
780 break;
Kevin Wolf99b1e642021-02-17 12:56:45 +0100781 case OPTION_OBJECT:
782 user_creatable_process_cmdline(optarg);
783 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000784 case OPTION_IMAGE_OPTS:
785 image_opts = true;
786 break;
aliguori15859692009-04-21 23:11:53 +0000787 }
788 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200789 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800790 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100791 }
aliguori15859692009-04-21 23:11:53 +0000792 filename = argv[optind++];
793
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500794 if (output && !strcmp(output, "json")) {
795 output_format = OFORMAT_JSON;
796 } else if (output && !strcmp(output, "human")) {
797 output_format = OFORMAT_HUMAN;
798 } else if (output) {
799 error_report("--output must be used with human or json as argument.");
800 return 1;
801 }
802
Kevin Wolfce099542016-03-15 13:01:04 +0100803 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200804 if (ret < 0) {
805 error_report("Invalid source cache option: %s", cache);
806 return 1;
807 }
808
Fam Zheng335e9932017-05-03 00:35:39 +0800809 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
810 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200811 if (!blk) {
812 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900813 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200814 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500815
816 check = g_new0(ImageCheck, 1);
817 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200818
819 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200820 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200821 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500822 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200823 }
824
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500825 if (check->corruptions_fixed || check->leaks_fixed) {
826 int corruptions_fixed, leaks_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100827 bool has_leaks_fixed, has_corruptions_fixed;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500828
829 leaks_fixed = check->leaks_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100830 has_leaks_fixed = check->has_leaks_fixed;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500831 corruptions_fixed = check->corruptions_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100832 has_corruptions_fixed = check->has_corruptions_fixed;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500833
834 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100835 qprintf(quiet,
836 "The following inconsistencies were found and repaired:\n\n"
837 " %" PRId64 " leaked clusters\n"
838 " %" PRId64 " corruptions\n\n"
839 "Double checking the fixed image now...\n",
840 check->leaks_fixed,
841 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500842 }
843
Pan Nengyuanfc124ea2020-02-27 09:29:50 +0800844 qapi_free_ImageCheck(check);
845 check = g_new0(ImageCheck, 1);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500846 ret = collect_image_check(bs, check, filename, fmt, 0);
847
848 check->leaks_fixed = leaks_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100849 check->has_leaks_fixed = has_leaks_fixed;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500850 check->corruptions_fixed = corruptions_fixed;
Max Reitz16563242020-03-24 18:27:55 +0100851 check->has_corruptions_fixed = has_corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200852 }
853
Max Reitz832390a2014-10-23 15:29:12 +0200854 if (!ret) {
855 switch (output_format) {
856 case OFORMAT_HUMAN:
857 dump_human_image_check(check, quiet);
858 break;
859 case OFORMAT_JSON:
860 dump_json_image_check(check, quiet);
861 break;
862 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500863 }
864
865 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200866 if (ret) {
867 error_report("Check failed: %s", strerror(-ret));
868 } else {
869 error_report("Check failed");
870 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500871 ret = 1;
872 goto fail;
873 }
874
875 if (check->corruptions) {
876 ret = 2;
877 } else if (check->leaks) {
878 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200879 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500880 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000881 }
882
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500883fail:
884 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200885 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500886 return ret;
aliguori15859692009-04-21 23:11:53 +0000887}
888
Max Reitzd4a32382014-10-24 15:57:37 +0200889typedef struct CommonBlockJobCBInfo {
890 BlockDriverState *bs;
891 Error **errp;
892} CommonBlockJobCBInfo;
893
894static void common_block_job_cb(void *opaque, int ret)
895{
896 CommonBlockJobCBInfo *cbi = opaque;
897
898 if (ret < 0) {
899 error_setg_errno(cbi->errp, -ret, "Block job failed");
900 }
Max Reitzd4a32382014-10-24 15:57:37 +0200901}
902
903static void run_block_job(BlockJob *job, Error **errp)
904{
Emanuele Giuseppe Espositoa7b4f8f2021-06-14 10:11:29 +0200905 uint64_t progress_current, progress_total;
Vladimir Sementsov-Ogievskiydf9a3162021-05-06 17:13:53 +0300906 AioContext *aio_context = block_job_get_aio_context(job);
sochin.jiang4172a002017-06-15 14:47:33 +0800907 int ret = 0;
Max Reitzd4a32382014-10-24 15:57:37 +0200908
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200909 aio_context_acquire(aio_context);
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200910 job_ref(&job->job);
Max Reitzd4a32382014-10-24 15:57:37 +0200911 do {
Kevin Wolf30a5c882018-05-04 12:17:20 +0200912 float progress = 0.0f;
Max Reitzd4a32382014-10-24 15:57:37 +0200913 aio_poll(aio_context, true);
Emanuele Giuseppe Espositoa7b4f8f2021-06-14 10:11:29 +0200914
915 progress_get_snapshot(&job->job.progress, &progress_current,
916 &progress_total);
917 if (progress_total) {
918 progress = (float)progress_current / progress_total * 100.f;
Kevin Wolf30a5c882018-05-04 12:17:20 +0200919 }
920 qemu_progress_print(progress, 0);
Kevin Wolfdf956ae2018-04-25 15:09:58 +0200921 } while (!job_is_ready(&job->job) && !job_is_completed(&job->job));
Max Reitzd4a32382014-10-24 15:57:37 +0200922
Kevin Wolfdbe5e6c2018-04-19 13:04:01 +0200923 if (!job_is_completed(&job->job)) {
Kevin Wolf3d70ff52018-04-24 16:13:52 +0200924 ret = job_complete_sync(&job->job, errp);
sochin.jiang4172a002017-06-15 14:47:33 +0800925 } else {
Kevin Wolf4ad35182018-04-19 17:30:16 +0200926 ret = job->job.ret;
sochin.jiang4172a002017-06-15 14:47:33 +0800927 }
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200928 job_unref(&job->job);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200929 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200930
sochin.jiang4172a002017-06-15 14:47:33 +0800931 /* publish completion progress only when success */
932 if (!ret) {
933 qemu_progress_print(100.f, 0);
934 }
Max Reitzd4a32382014-10-24 15:57:37 +0200935}
936
bellardea2384d2004-08-01 21:59:26 +0000937static int img_commit(int argc, char **argv)
938{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400939 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200940 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200941 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200942 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100943 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200944 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100945 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200946 Error *local_err = NULL;
947 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000948 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200949 AioContext *aio_context;
Zhenguia0441b62020-10-20 14:47:43 +0000950 int64_t rate_limit = 0;
bellardea2384d2004-08-01 21:59:26 +0000951
952 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400953 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200954 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000955 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000956 static const struct option long_options[] = {
957 {"help", no_argument, 0, 'h'},
958 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000959 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000960 {0, 0, 0, 0}
961 };
Zhenguia0441b62020-10-20 14:47:43 +0000962 c = getopt_long(argc, argv, ":f:ht:b:dpqr:",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000963 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100964 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000965 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100966 }
bellardea2384d2004-08-01 21:59:26 +0000967 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800968 case ':':
969 missing_argument(argv[optind - 1]);
970 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100971 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800972 unrecognized_option(argv[optind - 1]);
973 break;
bellardea2384d2004-08-01 21:59:26 +0000974 case 'h':
975 help();
976 break;
977 case 'f':
978 fmt = optarg;
979 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400980 case 't':
981 cache = optarg;
982 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200983 case 'b':
984 base = optarg;
985 /* -b implies -d */
986 drop = true;
987 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200988 case 'd':
989 drop = true;
990 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200991 case 'p':
992 progress = true;
993 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100994 case 'q':
995 quiet = true;
996 break;
Zhenguia0441b62020-10-20 14:47:43 +0000997 case 'r':
998 rate_limit = cvtnum("rate limit", optarg);
999 if (rate_limit < 0) {
1000 return 1;
1001 }
1002 break;
Kevin Wolf99b1e642021-02-17 12:56:45 +01001003 case OPTION_OBJECT:
1004 user_creatable_process_cmdline(optarg);
1005 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001006 case OPTION_IMAGE_OPTS:
1007 image_opts = true;
1008 break;
bellardea2384d2004-08-01 21:59:26 +00001009 }
1010 }
Max Reitz687fa1d2014-10-24 15:57:39 +02001011
1012 /* Progress is not shown in Quiet mode */
1013 if (quiet) {
1014 progress = false;
1015 }
1016
Kevin Wolffc11eb22013-08-05 10:53:04 +02001017 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001018 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001019 }
bellardea2384d2004-08-01 21:59:26 +00001020 filename = argv[optind++];
1021
Max Reitz9a86fe42014-10-24 15:57:38 +02001022 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +01001023 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001024 if (ret < 0) {
1025 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +01001026 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001027 }
1028
Fam Zheng335e9932017-05-03 00:35:39 +08001029 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
1030 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001031 if (!blk) {
1032 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001033 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001034 bs = blk_bs(blk);
1035
Max Reitz687fa1d2014-10-24 15:57:39 +02001036 qemu_progress_init(progress, 1.f);
1037 qemu_progress_print(0.f, 100);
1038
Max Reitz1b22bff2014-10-24 15:57:40 +02001039 if (base) {
1040 base_bs = bdrv_find_backing_image(bs, base);
1041 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +01001042 error_setg(&local_err,
1043 "Did not find '%s' in the backing chain of '%s'",
1044 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +02001045 goto done;
1046 }
1047 } else {
1048 /* This is different from QMP, which by default uses the deepest file in
1049 * the backing chain (i.e., the very base); however, the traditional
1050 * behavior of qemu-img commit is using the immediate backing file. */
Max Reitz4a2061e2019-06-12 19:00:30 +02001051 base_bs = bdrv_backing_chain_next(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +02001052 if (!base_bs) {
1053 error_setg(&local_err, "Image does not have a backing file");
1054 goto done;
1055 }
bellardea2384d2004-08-01 21:59:26 +00001056 }
1057
Max Reitzd4a32382014-10-24 15:57:37 +02001058 cbi = (CommonBlockJobCBInfo){
1059 .errp = &local_err,
1060 .bs = bs,
1061 };
1062
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001063 aio_context = bdrv_get_aio_context(bs);
1064 aio_context_acquire(aio_context);
Zhenguia0441b62020-10-20 14:47:43 +00001065 commit_active_start("commit", bs, base_bs, JOB_DEFAULT, rate_limit,
Kevin Wolf0db832f2017-02-20 18:10:05 +01001066 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +08001067 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001068 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +02001069 if (local_err) {
1070 goto done;
1071 }
1072
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001073 /* When the block job completes, the BlockBackend reference will point to
1074 * the old backing file. In order to avoid that the top image is already
1075 * deleted, so we can still empty it afterwards, increment the reference
1076 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +02001077 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001078 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001079 }
1080
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001081 job = block_job_get("commit");
Liam Merwick2e2db262018-11-05 21:38:37 +00001082 assert(job);
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001083 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001084 if (local_err) {
1085 goto unref_backing;
1086 }
1087
Max Reitz2d97fde2020-04-29 16:11:26 +02001088 if (!drop) {
1089 BlockBackend *old_backing_blk;
1090
1091 old_backing_blk = blk_new_with_bs(bs, BLK_PERM_WRITE, BLK_PERM_ALL,
1092 &local_err);
1093 if (!old_backing_blk) {
1094 goto unref_backing;
1095 }
1096 ret = blk_make_empty(old_backing_blk, &local_err);
1097 blk_unref(old_backing_blk);
1098 if (ret == -ENOTSUP) {
1099 error_free(local_err);
1100 local_err = NULL;
1101 } else if (ret < 0) {
Max Reitz9a86fe42014-10-24 15:57:38 +02001102 goto unref_backing;
1103 }
1104 }
1105
1106unref_backing:
1107 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001108 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001109 }
Max Reitzd4a32382014-10-24 15:57:37 +02001110
1111done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001112 qemu_progress_end();
1113
Markus Armbruster26f54e92014-10-07 13:59:04 +02001114 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001115
1116 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001117 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001118 return 1;
1119 }
Max Reitzd4a32382014-10-24 15:57:37 +02001120
1121 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001122 return 0;
1123}
1124
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001125/*
Eric Blakedebb38a2017-10-11 22:47:11 -05001126 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1127 * of the first sector boundary within buf where the sector contains a
1128 * non-zero byte. This function is robust to a buffer that is not
1129 * sector-aligned.
1130 */
1131static int64_t find_nonzero(const uint8_t *buf, int64_t n)
1132{
1133 int64_t i;
1134 int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE);
1135
1136 for (i = 0; i < end; i += BDRV_SECTOR_SIZE) {
1137 if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) {
1138 return i;
1139 }
1140 }
1141 if (i < n && !buffer_is_zero(buf + i, n - end)) {
1142 return i;
1143 }
1144 return -1;
1145}
1146
1147/*
thsf58c7b32008-06-05 21:53:49 +00001148 * Returns true iff the first sector pointed to by 'buf' contains at least
1149 * a non-NUL byte.
1150 *
1151 * 'pnum' is set to the number of sectors (including and immediately following
1152 * the first one) that are known to be in the same allocated/unallocated state.
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001153 * The function will try to align the end offset to alignment boundaries so
zhaolichange3a6e0d2020-09-17 15:50:20 +08001154 * that the request will at least end aligned and consecutive requests will
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001155 * also start at an aligned offset.
thsf58c7b32008-06-05 21:53:49 +00001156 */
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001157static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum,
1158 int64_t sector_num, int alignment)
bellardea2384d2004-08-01 21:59:26 +00001159{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001160 bool is_zero;
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001161 int i, tail;
bellardea2384d2004-08-01 21:59:26 +00001162
1163 if (n <= 0) {
1164 *pnum = 0;
1165 return 0;
1166 }
Yi Lic075c422020-08-19 09:36:07 +08001167 is_zero = buffer_is_zero(buf, BDRV_SECTOR_SIZE);
bellardea2384d2004-08-01 21:59:26 +00001168 for(i = 1; i < n; i++) {
Yi Lic075c422020-08-19 09:36:07 +08001169 buf += BDRV_SECTOR_SIZE;
1170 if (is_zero != buffer_is_zero(buf, BDRV_SECTOR_SIZE)) {
bellardea2384d2004-08-01 21:59:26 +00001171 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001172 }
bellardea2384d2004-08-01 21:59:26 +00001173 }
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001174
Vladimir Sementsov-Ogievskiy96054c72021-12-17 17:46:54 +01001175 if (i == n) {
1176 /*
1177 * The whole buf is the same.
1178 * No reason to split it into chunks, so return now.
1179 */
1180 *pnum = i;
1181 return !is_zero;
1182 }
1183
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001184 tail = (sector_num + i) & (alignment - 1);
1185 if (tail) {
1186 if (is_zero && i <= tail) {
Vladimir Sementsov-Ogievskiy96054c72021-12-17 17:46:54 +01001187 /*
1188 * For sure next sector after i is data, and it will rewrite this
1189 * tail anyway due to RMW. So, let's just write data now.
1190 */
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001191 is_zero = false;
1192 }
1193 if (!is_zero) {
Vladimir Sementsov-Ogievskiy96054c72021-12-17 17:46:54 +01001194 /* If possible, align up end offset of allocated areas. */
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001195 i += alignment - tail;
1196 i = MIN(i, n);
1197 } else {
Vladimir Sementsov-Ogievskiy96054c72021-12-17 17:46:54 +01001198 /*
1199 * For sure next sector after i is data, and it will rewrite this
1200 * tail anyway due to RMW. Better is avoid RMW and write zeroes up
1201 * to aligned bound.
1202 */
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001203 i -= tail;
1204 }
1205 }
bellardea2384d2004-08-01 21:59:26 +00001206 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001207 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001208}
1209
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001210/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001211 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1212 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1213 * breaking up write requests for only small sparse areas.
1214 */
1215static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001216 int min, int64_t sector_num, int alignment)
Kevin Wolfa22f1232011-08-26 15:27:13 +02001217{
1218 int ret;
1219 int num_checked, num_used;
1220
1221 if (n < min) {
1222 min = n;
1223 }
1224
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001225 ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment);
Kevin Wolfa22f1232011-08-26 15:27:13 +02001226 if (!ret) {
1227 return ret;
1228 }
1229
1230 num_used = *pnum;
1231 buf += BDRV_SECTOR_SIZE * *pnum;
1232 n -= *pnum;
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001233 sector_num += *pnum;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001234 num_checked = num_used;
1235
1236 while (n > 0) {
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001237 ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment);
Kevin Wolfa22f1232011-08-26 15:27:13 +02001238
1239 buf += BDRV_SECTOR_SIZE * *pnum;
1240 n -= *pnum;
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001241 sector_num += *pnum;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001242 num_checked += *pnum;
1243 if (ret) {
1244 num_used = num_checked;
1245 } else if (*pnum >= min) {
1246 break;
1247 }
1248 }
1249
1250 *pnum = num_used;
1251 return 1;
1252}
1253
1254/*
Eric Blakedc61cd32017-10-11 22:47:14 -05001255 * Compares two buffers sector by sector. Returns 0 if the first
1256 * sector of each buffer matches, non-zero otherwise.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001257 *
Eric Blakedc61cd32017-10-11 22:47:14 -05001258 * pnum is set to the sector-aligned size of the buffer prefix that
1259 * has the same matching status as the first sector.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001260 */
Eric Blakedc61cd32017-10-11 22:47:14 -05001261static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2,
1262 int64_t bytes, int64_t *pnum)
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001263{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001264 bool res;
Eric Blakedc61cd32017-10-11 22:47:14 -05001265 int64_t i = MIN(bytes, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001266
Eric Blakedc61cd32017-10-11 22:47:14 -05001267 assert(bytes > 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001268
Eric Blakedc61cd32017-10-11 22:47:14 -05001269 res = !!memcmp(buf1, buf2, i);
1270 while (i < bytes) {
1271 int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001272
Eric Blakedc61cd32017-10-11 22:47:14 -05001273 if (!!memcmp(buf1 + i, buf2 + i, len) != res) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001274 break;
1275 }
Eric Blakedc61cd32017-10-11 22:47:14 -05001276 i += len;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001277 }
1278
1279 *pnum = i;
1280 return res;
1281}
1282
Stefano Garzarella97ede572019-05-08 12:43:24 +02001283#define IO_BUF_SIZE (2 * MiB)
bellardea2384d2004-08-01 21:59:26 +00001284
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001285/*
1286 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1287 *
Eric Blake0608e402017-10-11 22:47:12 -05001288 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1289 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1290 * failure), and 4 on error (the exit status for read errors), after emitting
1291 * an error message.
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001292 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001293 * @param blk: BlockBackend for the image
Eric Blakec41508e2017-10-11 22:47:13 -05001294 * @param offset: Starting offset to check
1295 * @param bytes: Number of bytes to check
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001296 * @param filename: Name of disk file we are checking (logging purpose)
1297 * @param buffer: Allocated buffer for storing read data
1298 * @param quiet: Flag for quiet mode
1299 */
Eric Blakec41508e2017-10-11 22:47:13 -05001300static int check_empty_sectors(BlockBackend *blk, int64_t offset,
1301 int64_t bytes, const char *filename,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001302 uint8_t *buffer, bool quiet)
1303{
Eric Blakedebb38a2017-10-11 22:47:11 -05001304 int ret = 0;
1305 int64_t idx;
1306
Eric Blakec41508e2017-10-11 22:47:13 -05001307 ret = blk_pread(blk, offset, buffer, bytes);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001308 if (ret < 0) {
1309 error_report("Error while reading offset %" PRId64 " of %s: %s",
Eric Blakec41508e2017-10-11 22:47:13 -05001310 offset, filename, strerror(-ret));
Eric Blake0608e402017-10-11 22:47:12 -05001311 return 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001312 }
Eric Blakec41508e2017-10-11 22:47:13 -05001313 idx = find_nonzero(buffer, bytes);
Eric Blakedebb38a2017-10-11 22:47:11 -05001314 if (idx >= 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001315 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blakec41508e2017-10-11 22:47:13 -05001316 offset + idx);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001317 return 1;
1318 }
1319
1320 return 0;
1321}
1322
1323/*
1324 * Compares two images. Exit codes:
1325 *
Kevin Wolf99b1e642021-02-17 12:56:45 +01001326 * 0 - Images are identical or the requested help was printed
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001327 * 1 - Images differ
1328 * >1 - Error occurred
1329 */
1330static int img_compare(int argc, char **argv)
1331{
Max Reitz40055952014-07-22 22:58:42 +02001332 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001333 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001334 BlockDriverState *bs1, *bs2;
Eric Blake033d9fc2017-10-11 22:47:16 -05001335 int64_t total_size1, total_size2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001336 uint8_t *buf1 = NULL, *buf2 = NULL;
Eric Blake31826642017-10-11 22:47:08 -05001337 int64_t pnum1, pnum2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001338 int allocated1, allocated2;
1339 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1340 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001341 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001342 bool writethrough;
Eric Blake033d9fc2017-10-11 22:47:16 -05001343 int64_t total_size;
1344 int64_t offset = 0;
1345 int64_t chunk;
Eric Blakedc61cd32017-10-11 22:47:14 -05001346 int c;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001347 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001348 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001349 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001350
Max Reitz40055952014-07-22 22:58:42 +02001351 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001352 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001353 static const struct option long_options[] = {
1354 {"help", no_argument, 0, 'h'},
1355 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001356 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001357 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001358 {0, 0, 0, 0}
1359 };
Fam Zheng335e9932017-05-03 00:35:39 +08001360 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001361 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001362 if (c == -1) {
1363 break;
1364 }
1365 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001366 case ':':
1367 missing_argument(argv[optind - 1]);
1368 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001369 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001370 unrecognized_option(argv[optind - 1]);
1371 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001372 case 'h':
1373 help();
1374 break;
1375 case 'f':
1376 fmt1 = optarg;
1377 break;
1378 case 'F':
1379 fmt2 = optarg;
1380 break;
Max Reitz40055952014-07-22 22:58:42 +02001381 case 'T':
1382 cache = optarg;
1383 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001384 case 'p':
1385 progress = true;
1386 break;
1387 case 'q':
1388 quiet = true;
1389 break;
1390 case 's':
1391 strict = true;
1392 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001393 case 'U':
1394 force_share = true;
1395 break;
Kevin Wolf99b1e642021-02-17 12:56:45 +01001396 case OPTION_OBJECT:
1397 {
1398 Error *local_err = NULL;
1399
1400 if (!user_creatable_add_from_str(optarg, &local_err)) {
1401 if (local_err) {
1402 error_report_err(local_err);
1403 exit(2);
1404 } else {
1405 /* Help was printed */
1406 exit(EXIT_SUCCESS);
1407 }
1408 }
1409 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001410 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001411 case OPTION_IMAGE_OPTS:
1412 image_opts = true;
1413 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001414 }
1415 }
1416
1417 /* Progress is not shown in Quiet mode */
1418 if (quiet) {
1419 progress = false;
1420 }
1421
1422
Kevin Wolffc11eb22013-08-05 10:53:04 +02001423 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001424 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001425 }
1426 filename1 = argv[optind++];
1427 filename2 = argv[optind++];
1428
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001429 /* Initialize before goto out */
1430 qemu_progress_init(progress, 2.0);
1431
Kevin Wolfce099542016-03-15 13:01:04 +01001432 flags = 0;
1433 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001434 if (ret < 0) {
1435 error_report("Invalid source cache option: %s", cache);
1436 ret = 2;
1437 goto out3;
1438 }
1439
Fam Zheng335e9932017-05-03 00:35:39 +08001440 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1441 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001442 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001443 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001444 goto out3;
1445 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001446
Fam Zheng335e9932017-05-03 00:35:39 +08001447 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1448 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001449 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001450 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001451 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001452 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001453 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001454 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001455
Max Reitzf1d3cd72015-02-05 13:58:18 -05001456 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1457 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
Eric Blake033d9fc2017-10-11 22:47:16 -05001458 total_size1 = blk_getlength(blk1);
1459 if (total_size1 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001460 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001461 filename1, strerror(-total_size1));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001462 ret = 4;
1463 goto out;
1464 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001465 total_size2 = blk_getlength(blk2);
1466 if (total_size2 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001467 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001468 filename2, strerror(-total_size2));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001469 ret = 4;
1470 goto out;
1471 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001472 total_size = MIN(total_size1, total_size2);
1473 progress_base = MAX(total_size1, total_size2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001474
1475 qemu_progress_print(0, 100);
1476
Eric Blake033d9fc2017-10-11 22:47:16 -05001477 if (strict && total_size1 != total_size2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001478 ret = 1;
1479 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1480 goto out;
1481 }
1482
Eric Blake033d9fc2017-10-11 22:47:16 -05001483 while (offset < total_size) {
Eric Blake31826642017-10-11 22:47:08 -05001484 int status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001485
Eric Blake033d9fc2017-10-11 22:47:16 -05001486 status1 = bdrv_block_status_above(bs1, NULL, offset,
1487 total_size1 - offset, &pnum1, NULL,
1488 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001489 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001490 ret = 3;
1491 error_report("Sector allocation test failed for %s", filename1);
1492 goto out;
1493 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001494 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001495
Eric Blake033d9fc2017-10-11 22:47:16 -05001496 status2 = bdrv_block_status_above(bs2, NULL, offset,
1497 total_size2 - offset, &pnum2, NULL,
1498 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001499 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001500 ret = 3;
1501 error_report("Sector allocation test failed for %s", filename2);
1502 goto out;
1503 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001504 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
Eric Blake7daddc62017-10-11 22:47:09 -05001505
1506 assert(pnum1 && pnum2);
Eric Blake033d9fc2017-10-11 22:47:16 -05001507 chunk = MIN(pnum1, pnum2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001508
Fam Zheng25ad8e62016-01-13 16:37:41 +08001509 if (strict) {
Eric Blake31826642017-10-11 22:47:08 -05001510 if (status1 != status2) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001511 ret = 1;
1512 qprintf(quiet, "Strict mode: Offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001513 " block status mismatch!\n", offset);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001514 goto out;
1515 }
1516 }
1517 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
Eric Blake7daddc62017-10-11 22:47:09 -05001518 /* nothing to do */
Fam Zheng25ad8e62016-01-13 16:37:41 +08001519 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001520 if (allocated1) {
Eric Blakedc61cd32017-10-11 22:47:14 -05001521 int64_t pnum;
1522
Eric Blake033d9fc2017-10-11 22:47:16 -05001523 chunk = MIN(chunk, IO_BUF_SIZE);
1524 ret = blk_pread(blk1, offset, buf1, chunk);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001525 if (ret < 0) {
1526 error_report("Error while reading offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001527 " of %s: %s",
1528 offset, filename1, strerror(-ret));
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001529 ret = 4;
1530 goto out;
1531 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001532 ret = blk_pread(blk2, offset, buf2, chunk);
1533 if (ret < 0) {
1534 error_report("Error while reading offset %" PRId64
1535 " of %s: %s",
1536 offset, filename2, strerror(-ret));
1537 ret = 4;
1538 goto out;
1539 }
1540 ret = compare_buffers(buf1, buf2, chunk, &pnum);
1541 if (ret || pnum != chunk) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001542 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blake033d9fc2017-10-11 22:47:16 -05001543 offset + (ret ? 0 : pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001544 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001545 goto out;
1546 }
1547 }
1548 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001549 chunk = MIN(chunk, IO_BUF_SIZE);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001550 if (allocated1) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001551 ret = check_empty_sectors(blk1, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001552 filename1, buf1, quiet);
1553 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001554 ret = check_empty_sectors(blk2, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001555 filename2, buf1, quiet);
1556 }
1557 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001558 goto out;
1559 }
1560 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001561 offset += chunk;
1562 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001563 }
1564
Eric Blake033d9fc2017-10-11 22:47:16 -05001565 if (total_size1 != total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001566 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001567 const char *filename_over;
1568
1569 qprintf(quiet, "Warning: Image size mismatch!\n");
Eric Blake033d9fc2017-10-11 22:47:16 -05001570 if (total_size1 > total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001571 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001572 filename_over = filename1;
1573 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001574 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001575 filename_over = filename2;
1576 }
1577
Eric Blake033d9fc2017-10-11 22:47:16 -05001578 while (offset < progress_base) {
1579 ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset,
1580 progress_base - offset, &chunk,
1581 NULL, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001582 if (ret < 0) {
1583 ret = 3;
1584 error_report("Sector allocation test failed for %s",
1585 filename_over);
1586 goto out;
1587
1588 }
Eric Blake391cb1a2017-10-11 22:47:10 -05001589 if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001590 chunk = MIN(chunk, IO_BUF_SIZE);
1591 ret = check_empty_sectors(blk_over, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001592 filename_over, buf1, quiet);
1593 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001594 goto out;
1595 }
1596 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001597 offset += chunk;
1598 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001599 }
1600 }
1601
1602 qprintf(quiet, "Images are identical.\n");
1603 ret = 0;
1604
1605out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001606 qemu_vfree(buf1);
1607 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001608 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001609out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001610 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001611out3:
1612 qemu_progress_end();
1613 return ret;
1614}
1615
Eric Blake6c729dd2020-05-21 14:21:35 -05001616/* Convenience wrapper around qmp_block_dirty_bitmap_merge */
1617static void do_dirty_bitmap_merge(const char *dst_node, const char *dst_name,
1618 const char *src_node, const char *src_name,
1619 Error **errp)
1620{
1621 BlockDirtyBitmapMergeSource *merge_src;
Eric Blake54aa3de2020-11-12 19:13:37 -06001622 BlockDirtyBitmapMergeSourceList *list = NULL;
Eric Blake6c729dd2020-05-21 14:21:35 -05001623
1624 merge_src = g_new0(BlockDirtyBitmapMergeSource, 1);
1625 merge_src->type = QTYPE_QDICT;
1626 merge_src->u.external.node = g_strdup(src_node);
1627 merge_src->u.external.name = g_strdup(src_name);
Eric Blake54aa3de2020-11-12 19:13:37 -06001628 QAPI_LIST_PREPEND(list, merge_src);
Eric Blake6c729dd2020-05-21 14:21:35 -05001629 qmp_block_dirty_bitmap_merge(dst_node, dst_name, list, errp);
1630 qapi_free_BlockDirtyBitmapMergeSourceList(list);
1631}
1632
Kevin Wolf690c7302015-03-19 13:33:32 +01001633enum ImgConvertBlockStatus {
1634 BLK_DATA,
1635 BLK_ZERO,
1636 BLK_BACKING_FILE,
1637};
1638
Peter Lieven2d9187b2017-02-28 13:40:07 +01001639#define MAX_COROUTINES 16
Zhengui0c8c4892020-10-20 14:47:44 +00001640#define CONVERT_THROTTLE_GROUP "img_convert"
Peter Lieven2d9187b2017-02-28 13:40:07 +01001641
Kevin Wolf690c7302015-03-19 13:33:32 +01001642typedef struct ImgConvertState {
1643 BlockBackend **src;
1644 int64_t *src_sectors;
Peter Lievenaf8d43d2020-09-01 14:51:29 +02001645 int *src_alignment;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001646 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001647 int64_t total_sectors;
1648 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001649 int64_t allocated_done;
1650 int64_t sector_num;
1651 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001652 enum ImgConvertBlockStatus status;
1653 int64_t sector_next_status;
1654 BlockBackend *target;
1655 bool has_zero_init;
1656 bool compressed;
Max Reitz4d7c4872019-07-24 19:12:29 +02001657 bool target_is_new;
Kevin Wolf690c7302015-03-19 13:33:32 +01001658 bool target_has_backing;
Max Reitz351c8ef2018-05-01 18:57:49 +02001659 int64_t target_backing_sectors; /* negative if unknown */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001660 bool wr_in_order;
Fam Zhengee5306d2018-06-01 17:26:48 +08001661 bool copy_range;
Max Reitz8eaac022019-05-07 22:35:03 +02001662 bool salvage;
Max Reitz3d96cb92019-05-07 22:35:02 +02001663 bool quiet;
Kevin Wolf690c7302015-03-19 13:33:32 +01001664 int min_sparse;
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001665 int alignment;
Kevin Wolf690c7302015-03-19 13:33:32 +01001666 size_t cluster_sectors;
1667 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001668 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001669 int running_coroutines;
1670 Coroutine *co[MAX_COROUTINES];
1671 int64_t wait_sector_num[MAX_COROUTINES];
1672 CoMutex lock;
1673 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001674} ImgConvertState;
1675
Peter Lieven2d9187b2017-02-28 13:40:07 +01001676static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1677 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001678{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001679 *src_cur = 0;
1680 *src_cur_offset = 0;
1681 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1682 *src_cur_offset += s->src_sectors[*src_cur];
1683 (*src_cur)++;
1684 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001685 }
1686}
1687
1688static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1689{
Eric Blake31826642017-10-11 22:47:08 -05001690 int64_t src_cur_offset;
1691 int ret, n, src_cur;
Max Reitz351c8ef2018-05-01 18:57:49 +02001692 bool post_backing_zero = false;
Kevin Wolf690c7302015-03-19 13:33:32 +01001693
Peter Lieven2d9187b2017-02-28 13:40:07 +01001694 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001695
1696 assert(s->total_sectors > sector_num);
1697 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1698
Max Reitz351c8ef2018-05-01 18:57:49 +02001699 if (s->target_backing_sectors >= 0) {
1700 if (sector_num >= s->target_backing_sectors) {
Vladimir Sementsov-Ogievskiy2253d862020-05-28 12:43:56 +03001701 post_backing_zero = true;
Max Reitz351c8ef2018-05-01 18:57:49 +02001702 } else if (sector_num + n > s->target_backing_sectors) {
1703 /* Split requests around target_backing_sectors (because
1704 * starting from there, zeros are handled differently) */
1705 n = s->target_backing_sectors - sector_num;
1706 }
1707 }
1708
Kevin Wolf690c7302015-03-19 13:33:32 +01001709 if (s->sector_next_status <= sector_num) {
Max Reitz8eaac022019-05-07 22:35:03 +02001710 uint64_t offset = (sector_num - src_cur_offset) * BDRV_SECTOR_SIZE;
1711 int64_t count;
Peter Lievenaf8d43d2020-09-01 14:51:29 +02001712 int tail;
Max Reitz4a2061e2019-06-12 19:00:30 +02001713 BlockDriverState *src_bs = blk_bs(s->src[src_cur]);
1714 BlockDriverState *base;
1715
1716 if (s->target_has_backing) {
1717 base = bdrv_cow_bs(bdrv_skip_filters(src_bs));
1718 } else {
1719 base = NULL;
1720 }
Eric Blake31826642017-10-11 22:47:08 -05001721
Max Reitz8eaac022019-05-07 22:35:03 +02001722 do {
1723 count = n * BDRV_SECTOR_SIZE;
Eric Blake237d78f2017-10-11 22:47:03 -05001724
Max Reitz4a2061e2019-06-12 19:00:30 +02001725 ret = bdrv_block_status_above(src_bs, base, offset, count, &count,
1726 NULL, NULL);
Max Reitz8eaac022019-05-07 22:35:03 +02001727
1728 if (ret < 0) {
1729 if (s->salvage) {
1730 if (n == 1) {
1731 if (!s->quiet) {
1732 warn_report("error while reading block status at "
1733 "offset %" PRIu64 ": %s", offset,
1734 strerror(-ret));
1735 }
1736 /* Just try to read the data, then */
1737 ret = BDRV_BLOCK_DATA;
1738 count = BDRV_SECTOR_SIZE;
1739 } else {
1740 /* Retry on a shorter range */
1741 n = DIV_ROUND_UP(n, 4);
1742 }
1743 } else {
1744 error_report("error while reading block status at offset "
1745 "%" PRIu64 ": %s", offset, strerror(-ret));
1746 return ret;
1747 }
1748 }
1749 } while (ret < 0);
1750
Eric Blake31826642017-10-11 22:47:08 -05001751 n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
Kevin Wolf690c7302015-03-19 13:33:32 +01001752
Peter Lievenaf8d43d2020-09-01 14:51:29 +02001753 /*
1754 * Avoid that s->sector_next_status becomes unaligned to the source
1755 * request alignment and/or cluster size to avoid unnecessary read
1756 * cycles.
1757 */
1758 tail = (sector_num - src_cur_offset + n) % s->src_alignment[src_cur];
1759 if (n > tail) {
1760 n -= tail;
1761 }
1762
Kevin Wolf690c7302015-03-19 13:33:32 +01001763 if (ret & BDRV_BLOCK_ZERO) {
Max Reitz351c8ef2018-05-01 18:57:49 +02001764 s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO;
Kevin Wolf690c7302015-03-19 13:33:32 +01001765 } else if (ret & BDRV_BLOCK_DATA) {
1766 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001767 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001768 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001769 }
1770
1771 s->sector_next_status = sector_num + n;
1772 }
1773
1774 n = MIN(n, s->sector_next_status - sector_num);
1775 if (s->status == BLK_DATA) {
1776 n = MIN(n, s->buf_sectors);
1777 }
1778
1779 /* We need to write complete clusters for compressed images, so if an
1780 * unallocated area is shorter than that, we must consider the whole
1781 * cluster allocated. */
1782 if (s->compressed) {
1783 if (n < s->cluster_sectors) {
1784 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1785 s->status = BLK_DATA;
1786 } else {
1787 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1788 }
1789 }
1790
1791 return n;
1792}
1793
Peter Lieven2d9187b2017-02-28 13:40:07 +01001794static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1795 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001796{
Max Reitz8eaac022019-05-07 22:35:03 +02001797 uint64_t single_read_until = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001798 int n, ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001799
Kevin Wolf690c7302015-03-19 13:33:32 +01001800 assert(nb_sectors <= s->buf_sectors);
1801 while (nb_sectors > 0) {
1802 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001803 int src_cur;
1804 int64_t bs_sectors, src_cur_offset;
Max Reitz8eaac022019-05-07 22:35:03 +02001805 uint64_t offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001806
1807 /* In the case of compression with multiple source files, we can get a
1808 * nb_sectors that spreads into the next part. So we must be able to
1809 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001810 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1811 blk = s->src[src_cur];
1812 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001813
Max Reitz8eaac022019-05-07 22:35:03 +02001814 offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001815
Max Reitz8eaac022019-05-07 22:35:03 +02001816 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1817 if (single_read_until > offset) {
1818 n = 1;
1819 }
1820
1821 ret = blk_co_pread(blk, offset, n << BDRV_SECTOR_BITS, buf, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001822 if (ret < 0) {
Max Reitz8eaac022019-05-07 22:35:03 +02001823 if (s->salvage) {
1824 if (n > 1) {
1825 single_read_until = offset + (n << BDRV_SECTOR_BITS);
1826 continue;
1827 } else {
1828 if (!s->quiet) {
1829 warn_report("error while reading offset %" PRIu64
1830 ": %s", offset, strerror(-ret));
1831 }
1832 memset(buf, 0, BDRV_SECTOR_SIZE);
1833 }
1834 } else {
1835 return ret;
1836 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001837 }
1838
1839 sector_num += n;
1840 nb_sectors -= n;
1841 buf += n * BDRV_SECTOR_SIZE;
1842 }
1843
1844 return 0;
1845}
1846
Peter Lieven2d9187b2017-02-28 13:40:07 +01001847
1848static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1849 int nb_sectors, uint8_t *buf,
1850 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001851{
1852 int ret;
1853
1854 while (nb_sectors > 0) {
1855 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001856 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1857
Peter Lieven2d9187b2017-02-28 13:40:07 +01001858 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001859 case BLK_BACKING_FILE:
1860 /* If we have a backing file, leave clusters unallocated that are
1861 * unallocated in the source image, so that the backing file is
1862 * visible at the respective offset. */
1863 assert(s->target_has_backing);
1864 break;
1865
1866 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001867 /* If we're told to keep the target fully allocated (-S 0) or there
1868 * is real non-zero data, we must write it. Otherwise we can treat
1869 * it as zero sectors.
1870 * Compressed clusters need to be written as a whole, so in that
1871 * case we can only save the write if the buffer is completely
1872 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001873 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001874 (!s->compressed &&
Peter Lieven8dcd3c92018-07-12 15:00:10 +02001875 is_allocated_sectors_min(buf, n, &n, s->min_sparse,
1876 sector_num, s->alignment)) ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001877 (s->compressed &&
1878 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001879 {
Vladimir Sementsov-Ogievskiy265a7e52019-04-22 17:58:38 +03001880 ret = blk_co_pwrite(s->target, sector_num << BDRV_SECTOR_BITS,
1881 n << BDRV_SECTOR_BITS, buf, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001882 if (ret < 0) {
1883 return ret;
1884 }
1885 break;
1886 }
1887 /* fall-through */
1888
1889 case BLK_ZERO:
1890 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001891 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001892 break;
1893 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001894 ret = blk_co_pwrite_zeroes(s->target,
1895 sector_num << BDRV_SECTOR_BITS,
Nir Soffera3d6ae22019-03-24 02:20:12 +02001896 n << BDRV_SECTOR_BITS,
1897 BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001898 if (ret < 0) {
1899 return ret;
1900 }
1901 break;
1902 }
1903
1904 sector_num += n;
1905 nb_sectors -= n;
1906 buf += n * BDRV_SECTOR_SIZE;
1907 }
1908
1909 return 0;
1910}
1911
Fam Zhengee5306d2018-06-01 17:26:48 +08001912static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num,
1913 int nb_sectors)
1914{
1915 int n, ret;
1916
1917 while (nb_sectors > 0) {
1918 BlockBackend *blk;
1919 int src_cur;
1920 int64_t bs_sectors, src_cur_offset;
1921 int64_t offset;
1922
1923 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1924 offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS;
1925 blk = s->src[src_cur];
1926 bs_sectors = s->src_sectors[src_cur];
1927
1928 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1929
1930 ret = blk_co_copy_range(blk, offset, s->target,
1931 sector_num << BDRV_SECTOR_BITS,
Vladimir Sementsov-Ogievskiy67b51fb2018-07-09 19:37:17 +03001932 n << BDRV_SECTOR_BITS, 0, 0);
Fam Zhengee5306d2018-06-01 17:26:48 +08001933 if (ret < 0) {
1934 return ret;
1935 }
1936
1937 sector_num += n;
1938 nb_sectors -= n;
1939 }
1940 return 0;
1941}
1942
Peter Lieven2d9187b2017-02-28 13:40:07 +01001943static void coroutine_fn convert_co_do_copy(void *opaque)
1944{
1945 ImgConvertState *s = opaque;
1946 uint8_t *buf = NULL;
1947 int ret, i;
1948 int index = -1;
1949
1950 for (i = 0; i < s->num_coroutines; i++) {
1951 if (s->co[i] == qemu_coroutine_self()) {
1952 index = i;
1953 break;
1954 }
1955 }
1956 assert(index >= 0);
1957
1958 s->running_coroutines++;
1959 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1960
1961 while (1) {
1962 int n;
1963 int64_t sector_num;
1964 enum ImgConvertBlockStatus status;
Fam Zhengee5306d2018-06-01 17:26:48 +08001965 bool copy_range;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001966
1967 qemu_co_mutex_lock(&s->lock);
1968 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1969 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001970 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001971 }
1972 n = convert_iteration_sectors(s, s->sector_num);
1973 if (n < 0) {
1974 qemu_co_mutex_unlock(&s->lock);
1975 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001976 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001977 }
1978 /* save current sector and allocation status to local variables */
1979 sector_num = s->sector_num;
1980 status = s->status;
1981 if (!s->min_sparse && s->status == BLK_ZERO) {
1982 n = MIN(n, s->buf_sectors);
1983 }
1984 /* increment global sector counter so that other coroutines can
1985 * already continue reading beyond this request */
1986 s->sector_num += n;
1987 qemu_co_mutex_unlock(&s->lock);
1988
1989 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1990 s->allocated_done += n;
1991 qemu_progress_print(100.0 * s->allocated_done /
1992 s->allocated_sectors, 0);
1993 }
1994
Fam Zhengee5306d2018-06-01 17:26:48 +08001995retry:
1996 copy_range = s->copy_range && s->status == BLK_DATA;
1997 if (status == BLK_DATA && !copy_range) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001998 ret = convert_co_read(s, sector_num, n, buf);
1999 if (ret < 0) {
Eric Blake39f77cb2020-04-02 08:57:17 -05002000 error_report("error while reading at byte %lld: %s",
2001 sector_num * BDRV_SECTOR_SIZE, strerror(-ret));
Peter Lieven2d9187b2017-02-28 13:40:07 +01002002 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002003 }
2004 } else if (!s->min_sparse && status == BLK_ZERO) {
2005 status = BLK_DATA;
2006 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
2007 }
2008
2009 if (s->wr_in_order) {
2010 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03002011 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002012 s->wait_sector_num[index] = sector_num;
2013 qemu_coroutine_yield();
2014 }
2015 s->wait_sector_num[index] = -1;
2016 }
2017
Anton Nefedovb91127e2017-04-26 11:33:15 +03002018 if (s->ret == -EINPROGRESS) {
Fam Zhengee5306d2018-06-01 17:26:48 +08002019 if (copy_range) {
2020 ret = convert_co_copy_range(s, sector_num, n);
2021 if (ret) {
2022 s->copy_range = false;
2023 goto retry;
2024 }
2025 } else {
2026 ret = convert_co_write(s, sector_num, n, buf, status);
2027 }
Anton Nefedovb91127e2017-04-26 11:33:15 +03002028 if (ret < 0) {
Eric Blake39f77cb2020-04-02 08:57:17 -05002029 error_report("error while writing at byte %lld: %s",
2030 sector_num * BDRV_SECTOR_SIZE, strerror(-ret));
Anton Nefedovb91127e2017-04-26 11:33:15 +03002031 s->ret = ret;
2032 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01002033 }
2034
2035 if (s->wr_in_order) {
2036 /* reenter the coroutine that might have waited
2037 * for this write to complete */
2038 s->wr_offs = sector_num + n;
2039 for (i = 0; i < s->num_coroutines; i++) {
2040 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
2041 /*
2042 * A -> B -> A cannot occur because A has
2043 * s->wait_sector_num[i] == -1 during A -> B. Therefore
2044 * B will never enter A during this time window.
2045 */
2046 qemu_coroutine_enter(s->co[i]);
2047 break;
2048 }
2049 }
2050 }
2051 }
2052
Peter Lieven2d9187b2017-02-28 13:40:07 +01002053 qemu_vfree(buf);
2054 s->co[index] = NULL;
2055 s->running_coroutines--;
2056 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
2057 /* the convert job finished successfully */
2058 s->ret = 0;
2059 }
2060}
2061
Kevin Wolf690c7302015-03-19 13:33:32 +01002062static int convert_do_copy(ImgConvertState *s)
2063{
Peter Lieven2d9187b2017-02-28 13:40:07 +01002064 int ret, i, n;
2065 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01002066
2067 /* Check whether we have zero initialisation or can get it efficiently */
David Edmondson168468f2020-02-05 11:02:48 +00002068 if (!s->has_zero_init && s->target_is_new && s->min_sparse &&
2069 !s->target_has_backing) {
Max Reitz4d7c4872019-07-24 19:12:29 +02002070 s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target));
Max Reitz4d7c4872019-07-24 19:12:29 +02002071 }
Kevin Wolf690c7302015-03-19 13:33:32 +01002072
Kevin Wolf690c7302015-03-19 13:33:32 +01002073 /* Allocate buffer for copied data. For compressed images, only one cluster
2074 * can be copied at a time. */
2075 if (s->compressed) {
2076 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
2077 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01002078 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01002079 }
2080 s->buf_sectors = s->cluster_sectors;
2081 }
Kevin Wolf690c7302015-03-19 13:33:32 +01002082
Kevin Wolf690c7302015-03-19 13:33:32 +01002083 while (sector_num < s->total_sectors) {
2084 n = convert_iteration_sectors(s, sector_num);
2085 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002086 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01002087 }
Max Reitzaad15de2016-03-24 23:33:57 +01002088 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
2089 {
Kevin Wolf690c7302015-03-19 13:33:32 +01002090 s->allocated_sectors += n;
2091 }
2092 sector_num += n;
2093 }
2094
2095 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01002096 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002097 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01002098
Peter Lieven2d9187b2017-02-28 13:40:07 +01002099 qemu_co_mutex_init(&s->lock);
2100 for (i = 0; i < s->num_coroutines; i++) {
2101 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
2102 s->wait_sector_num[i] = -1;
2103 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01002104 }
2105
Anton Nefedovb91127e2017-04-26 11:33:15 +03002106 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002107 main_loop_wait(false);
2108 }
2109
2110 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01002111 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03002112 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01002113 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002114 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01002115 }
2116 }
2117
Peter Lieven2d9187b2017-02-28 13:40:07 +01002118 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01002119}
2120
Eric Blake74a43202021-07-09 10:39:50 -05002121/* Check that bitmaps can be copied, or output an error */
Eric Blake955171e2021-07-21 10:53:48 -05002122static int convert_check_bitmaps(BlockDriverState *src, bool skip_broken)
Eric Blake74a43202021-07-09 10:39:50 -05002123{
2124 BdrvDirtyBitmap *bm;
2125
2126 if (!bdrv_supports_persistent_dirty_bitmap(src)) {
2127 error_report("Source lacks bitmap support");
2128 return -1;
2129 }
2130 FOR_EACH_DIRTY_BITMAP(src, bm) {
2131 if (!bdrv_dirty_bitmap_get_persistence(bm)) {
2132 continue;
2133 }
Eric Blake955171e2021-07-21 10:53:48 -05002134 if (!skip_broken && bdrv_dirty_bitmap_inconsistent(bm)) {
Eric Blake74a43202021-07-09 10:39:50 -05002135 error_report("Cannot copy inconsistent bitmap '%s'",
2136 bdrv_dirty_bitmap_name(bm));
Eric Blake955171e2021-07-21 10:53:48 -05002137 error_printf("Try --skip-broken-bitmaps, or "
2138 "use 'qemu-img bitmap --remove' to delete it\n");
Eric Blake74a43202021-07-09 10:39:50 -05002139 return -1;
2140 }
2141 }
2142 return 0;
2143}
2144
Eric Blake955171e2021-07-21 10:53:48 -05002145static int convert_copy_bitmaps(BlockDriverState *src, BlockDriverState *dst,
2146 bool skip_broken)
Eric Blake15e39ad2020-05-21 14:21:36 -05002147{
2148 BdrvDirtyBitmap *bm;
2149 Error *err = NULL;
2150
2151 FOR_EACH_DIRTY_BITMAP(src, bm) {
2152 const char *name;
2153
2154 if (!bdrv_dirty_bitmap_get_persistence(bm)) {
2155 continue;
2156 }
2157 name = bdrv_dirty_bitmap_name(bm);
Eric Blake955171e2021-07-21 10:53:48 -05002158 if (skip_broken && bdrv_dirty_bitmap_inconsistent(bm)) {
2159 warn_report("Skipping inconsistent bitmap '%s'", name);
2160 continue;
2161 }
Eric Blake15e39ad2020-05-21 14:21:36 -05002162 qmp_block_dirty_bitmap_add(dst->node_name, name,
2163 true, bdrv_dirty_bitmap_granularity(bm),
2164 true, true,
2165 true, !bdrv_dirty_bitmap_enabled(bm),
2166 &err);
2167 if (err) {
2168 error_reportf_err(err, "Failed to create bitmap %s: ", name);
2169 return -1;
2170 }
2171
2172 do_dirty_bitmap_merge(dst->node_name, name, src->node_name, name,
2173 &err);
2174 if (err) {
2175 error_reportf_err(err, "Failed to populate bitmap %s: ", name);
Eric Blake74a43202021-07-09 10:39:50 -05002176 qmp_block_dirty_bitmap_remove(dst->node_name, name, NULL);
Eric Blake15e39ad2020-05-21 14:21:36 -05002177 return -1;
2178 }
2179 }
2180
2181 return 0;
2182}
2183
Peter Lieven6360ab22018-07-13 09:15:39 +02002184#define MAX_BUF_SECTORS 32768
2185
Zhengui0c8c4892020-10-20 14:47:44 +00002186static void set_rate_limit(BlockBackend *blk, int64_t rate_limit)
2187{
2188 ThrottleConfig cfg;
2189
2190 throttle_config_init(&cfg);
2191 cfg.buckets[THROTTLE_BPS_WRITE].avg = rate_limit;
2192
2193 blk_io_limits_enable(blk, CONVERT_THROTTLE_GROUP);
2194 blk_set_io_limits(blk, &cfg);
2195}
2196
bellardea2384d2004-08-01 21:59:26 +00002197static int img_convert(int argc, char **argv)
2198{
Kevin Wolf0b8fb552021-04-22 18:43:44 +02002199 int c, bs_i, flags, src_flags = BDRV_O_NO_SHARE;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002200 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002201 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
Eric Blake1899bf42021-09-13 08:17:35 -05002202 *out_filename, *out_baseimg_param, *snapshot_name = NULL,
2203 *backing_fmt = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002204 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00002205 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002206 BlockDriverState *out_bs;
2207 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08002208 QemuOptsList *create_opts = NULL;
Daniel P. Berrangé8d65a3c2018-08-14 13:39:47 +01002209 QDict *open_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002210 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02002211 Error *local_err = NULL;
Max Reitz3d96cb92019-05-07 22:35:02 +02002212 bool writethrough, src_writethrough, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002213 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002214 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08002215 bool force_share = false;
Fam Zhenge11ce122018-07-27 11:34:01 +08002216 bool explict_min_sparse = false;
Eric Blake15e39ad2020-05-21 14:21:36 -05002217 bool bitmaps = false;
Eric Blake955171e2021-07-21 10:53:48 -05002218 bool skip_broken = false;
Zhengui0c8c4892020-10-20 14:47:44 +00002219 int64_t rate_limit = 0;
bellardea2384d2004-08-01 21:59:26 +00002220
Peter Lieven9fd77f92017-04-21 11:11:55 +02002221 ImgConvertState s = (ImgConvertState) {
2222 /* Need at least 4k of zeros for sparse detection */
2223 .min_sparse = 8,
Fam Zhenge11ce122018-07-27 11:34:01 +08002224 .copy_range = false,
Peter Lieven9fd77f92017-04-21 11:11:55 +02002225 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
2226 .wr_in_order = true,
2227 .num_coroutines = 8,
2228 };
2229
bellardea2384d2004-08-01 21:59:26 +00002230 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002231 static const struct option long_options[] = {
2232 {"help", no_argument, 0, 'h'},
2233 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002234 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002235 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002236 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Max Reitz8eaac022019-05-07 22:35:03 +02002237 {"salvage", no_argument, 0, OPTION_SALVAGE},
David Edmondson168468f2020-02-05 11:02:48 +00002238 {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO},
Eric Blake15e39ad2020-05-21 14:21:36 -05002239 {"bitmaps", no_argument, 0, OPTION_BITMAPS},
Eric Blake955171e2021-07-21 10:53:48 -05002240 {"skip-broken-bitmaps", no_argument, 0, OPTION_SKIP_BROKEN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002241 {0, 0, 0, 0}
2242 };
Eric Blake1899bf42021-09-13 08:17:35 -05002243 c = getopt_long(argc, argv, ":hf:O:B:CcF:o:l:S:pt:T:qnm:WUr:",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002244 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002245 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002246 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002247 }
bellardea2384d2004-08-01 21:59:26 +00002248 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002249 case ':':
2250 missing_argument(argv[optind - 1]);
2251 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002252 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002253 unrecognized_option(argv[optind - 1]);
2254 break;
bellardea2384d2004-08-01 21:59:26 +00002255 case 'h':
2256 help();
2257 break;
2258 case 'f':
2259 fmt = optarg;
2260 break;
2261 case 'O':
2262 out_fmt = optarg;
2263 break;
thsf58c7b32008-06-05 21:53:49 +00002264 case 'B':
2265 out_baseimg = optarg;
2266 break;
Fam Zhenge11ce122018-07-27 11:34:01 +08002267 case 'C':
2268 s.copy_range = true;
2269 break;
bellardea2384d2004-08-01 21:59:26 +00002270 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002271 s.compressed = true;
bellardea2384d2004-08-01 21:59:26 +00002272 break;
Eric Blake1899bf42021-09-13 08:17:35 -05002273 case 'F':
2274 backing_fmt = optarg;
2275 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002276 case 'o':
Markus Armbruster6d2b5cb2020-04-15 09:49:25 +02002277 if (accumulate_options(&options, optarg) < 0) {
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002278 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01002279 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002280 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08002281 case 'l':
2282 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01002283 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2284 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08002285 if (!sn_opts) {
2286 error_report("Failed in parsing snapshot param '%s'",
2287 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002288 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002289 }
2290 } else {
2291 snapshot_name = optarg;
2292 }
2293 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002294 case 'S':
2295 {
2296 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002297
Eyal Moscovici43d589b2020-05-13 16:36:26 +03002298 sval = cvtnum("buffer size for sparse output", optarg);
2299 if (sval < 0) {
2300 goto fail_getopt;
2301 } else if (!QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) ||
Peter Lieven6360ab22018-07-13 09:15:39 +02002302 sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) {
2303 error_report("Invalid buffer size for sparse output specified. "
2304 "Valid sizes are multiples of %llu up to %llu. Select "
2305 "0 to disable sparse detection (fully allocates output).",
2306 BDRV_SECTOR_SIZE, MAX_BUF_SECTORS * BDRV_SECTOR_SIZE);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002307 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002308 }
2309
Peter Lieven9fd77f92017-04-21 11:11:55 +02002310 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Fam Zhenge11ce122018-07-27 11:34:01 +08002311 explict_min_sparse = true;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002312 break;
2313 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002314 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002315 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002316 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002317 case 't':
2318 cache = optarg;
2319 break;
Max Reitz40055952014-07-22 22:58:42 +02002320 case 'T':
2321 src_cache = optarg;
2322 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002323 case 'q':
Max Reitz3d96cb92019-05-07 22:35:02 +02002324 s.quiet = true;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002325 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002326 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002327 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002328 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002329 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002330 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2331 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002332 error_report("Invalid number of coroutines. Allowed number of"
2333 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002334 goto fail_getopt;
2335 }
2336 break;
2337 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002338 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002339 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002340 case 'U':
2341 force_share = true;
2342 break;
Zhengui0c8c4892020-10-20 14:47:44 +00002343 case 'r':
2344 rate_limit = cvtnum("rate limit", optarg);
2345 if (rate_limit < 0) {
2346 goto fail_getopt;
2347 }
2348 break;
Kevin Wolf99b1e642021-02-17 12:56:45 +01002349 case OPTION_OBJECT:
2350 user_creatable_process_cmdline(optarg);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002351 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002352 case OPTION_IMAGE_OPTS:
2353 image_opts = true;
2354 break;
Max Reitz8eaac022019-05-07 22:35:03 +02002355 case OPTION_SALVAGE:
2356 s.salvage = true;
2357 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002358 case OPTION_TARGET_IMAGE_OPTS:
2359 tgt_image_opts = true;
2360 break;
David Edmondson168468f2020-02-05 11:02:48 +00002361 case OPTION_TARGET_IS_ZERO:
2362 /*
2363 * The user asserting that the target is blank has the
2364 * same effect as the target driver supporting zero
2365 * initialisation.
2366 */
2367 s.has_zero_init = true;
2368 break;
Eric Blake15e39ad2020-05-21 14:21:36 -05002369 case OPTION_BITMAPS:
2370 bitmaps = true;
2371 break;
Eric Blake955171e2021-07-21 10:53:48 -05002372 case OPTION_SKIP_BROKEN:
2373 skip_broken = true;
2374 break;
bellardea2384d2004-08-01 21:59:26 +00002375 }
2376 }
ths3b46e622007-09-17 08:09:54 +00002377
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002378 if (!out_fmt && !tgt_image_opts) {
2379 out_fmt = "raw";
2380 }
2381
Eric Blake955171e2021-07-21 10:53:48 -05002382 if (skip_broken && !bitmaps) {
2383 error_report("Use of --skip-broken-bitmaps requires --bitmaps");
2384 goto fail_getopt;
2385 }
2386
Fam Zhenge11ce122018-07-27 11:34:01 +08002387 if (s.compressed && s.copy_range) {
2388 error_report("Cannot enable copy offloading when -c is used");
2389 goto fail_getopt;
2390 }
2391
2392 if (explict_min_sparse && s.copy_range) {
2393 error_report("Cannot enable copy offloading when -S is used");
2394 goto fail_getopt;
2395 }
2396
Max Reitz8eaac022019-05-07 22:35:03 +02002397 if (s.copy_range && s.salvage) {
2398 error_report("Cannot use copy offloading in salvaging mode");
2399 goto fail_getopt;
2400 }
2401
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002402 if (tgt_image_opts && !skip_create) {
2403 error_report("--target-image-opts requires use of -n flag");
2404 goto fail_getopt;
2405 }
2406
Kevin Wolfffd8e8f2019-08-09 11:09:21 +02002407 if (skip_create && options) {
Eric Blake25956af2020-07-06 15:39:46 -05002408 error_report("-o has no effect when skipping image creation");
2409 goto fail_getopt;
Kevin Wolfffd8e8f2019-08-09 11:09:21 +02002410 }
2411
David Edmondson168468f2020-02-05 11:02:48 +00002412 if (s.has_zero_init && !skip_create) {
2413 error_report("--target-is-zero requires use of -n flag");
2414 goto fail_getopt;
2415 }
2416
Peter Lieven9fd77f92017-04-21 11:11:55 +02002417 s.src_num = argc - optind - 1;
2418 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2419
2420 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002421 if (out_fmt) {
2422 ret = print_block_option_help(out_filename, out_fmt);
2423 goto fail_getopt;
2424 } else {
2425 error_report("Option help requires a format be specified");
2426 goto fail_getopt;
2427 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002428 }
2429
2430 if (s.src_num < 1) {
2431 error_report("Must specify image file name");
2432 goto fail_getopt;
2433 }
2434
Peter Lieven9fd77f92017-04-21 11:11:55 +02002435 /* ret is still -EINVAL until here */
2436 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2437 if (ret < 0) {
2438 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002439 goto fail_getopt;
2440 }
2441
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002442 /* Initialize before goto out */
Max Reitz3d96cb92019-05-07 22:35:02 +02002443 if (s.quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002444 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002445 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002446 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002447 qemu_progress_print(0, 100);
2448
Peter Lieven9fd77f92017-04-21 11:11:55 +02002449 s.src = g_new0(BlockBackend *, s.src_num);
2450 s.src_sectors = g_new(int64_t, s.src_num);
Peter Lievenaf8d43d2020-09-01 14:51:29 +02002451 s.src_alignment = g_new(int, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002452
Peter Lieven9fd77f92017-04-21 11:11:55 +02002453 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
Peter Lievenaf8d43d2020-09-01 14:51:29 +02002454 BlockDriverState *src_bs;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002455 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Max Reitz3d96cb92019-05-07 22:35:02 +02002456 fmt, src_flags, src_writethrough, s.quiet,
Fam Zheng335e9932017-05-03 00:35:39 +08002457 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002458 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002459 ret = -1;
2460 goto out;
2461 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002462 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2463 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002464 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002465 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002466 ret = -1;
2467 goto out;
2468 }
Peter Lievenaf8d43d2020-09-01 14:51:29 +02002469 src_bs = blk_bs(s.src[bs_i]);
2470 s.src_alignment[bs_i] = DIV_ROUND_UP(src_bs->bl.request_alignment,
2471 BDRV_SECTOR_SIZE);
2472 if (!bdrv_get_info(src_bs, &bdi)) {
2473 s.src_alignment[bs_i] = MAX(s.src_alignment[bs_i],
2474 bdi.cluster_size / BDRV_SECTOR_SIZE);
2475 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002476 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002477 }
bellardea2384d2004-08-01 21:59:26 +00002478
Wenchao Xiaef806542013-12-04 17:10:57 +08002479 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002480 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002481 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2482 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2483 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002484 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002485 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002486 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002487 ret = -1;
2488 goto out;
2489 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002490
Peter Lieven9fd77f92017-04-21 11:11:55 +02002491 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2492 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002493 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002494 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002495 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002496 ret = -1;
2497 goto out;
edison51ef6722010-09-21 19:58:41 -07002498 }
2499
Max Reitz2e024cd2015-02-11 09:58:46 -05002500 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002501 /* Find driver and parse its options */
2502 drv = bdrv_find_format(out_fmt);
2503 if (!drv) {
2504 error_report("Unknown file format '%s'", out_fmt);
2505 ret = -1;
2506 goto out;
2507 }
2508
2509 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2510 if (!proto_drv) {
2511 error_report_err(local_err);
2512 ret = -1;
2513 goto out;
2514 }
2515
Max Reitz2e024cd2015-02-11 09:58:46 -05002516 if (!drv->create_opts) {
2517 error_report("Format driver '%s' does not support image creation",
2518 drv->format_name);
2519 ret = -1;
2520 goto out;
2521 }
Max Reitzf75613c2014-12-02 18:32:46 +01002522
Max Reitz2e024cd2015-02-11 09:58:46 -05002523 if (!proto_drv->create_opts) {
2524 error_report("Protocol driver '%s' does not support image creation",
2525 proto_drv->format_name);
2526 ret = -1;
2527 goto out;
2528 }
Max Reitzf75613c2014-12-02 18:32:46 +01002529
Max Reitz2e024cd2015-02-11 09:58:46 -05002530 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2531 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002532
Max Reitz2e024cd2015-02-11 09:58:46 -05002533 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002534 if (options) {
Markus Armbruster235e59c2020-07-07 18:05:42 +02002535 if (!qemu_opts_do_parse(opts, options, NULL, &local_err)) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002536 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002537 ret = -1;
2538 goto out;
2539 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002540 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002541
Yi Lic075c422020-08-19 09:36:07 +08002542 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
2543 s.total_sectors * BDRV_SECTOR_SIZE, &error_abort);
Eric Blake1899bf42021-09-13 08:17:35 -05002544 ret = add_old_style_options(out_fmt, opts, out_baseimg, backing_fmt);
Max Reitz2e024cd2015-02-11 09:58:46 -05002545 if (ret < 0) {
2546 goto out;
2547 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002548 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002549
Kevin Wolfa18953f2010-10-14 15:46:04 +02002550 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002551 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002552 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002553 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002554 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002555 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002556
David Edmondson168468f2020-02-05 11:02:48 +00002557 if (s.has_zero_init && s.target_has_backing) {
2558 error_report("Cannot use --target-is-zero when the destination "
2559 "image has a backing file");
2560 goto out;
2561 }
2562
Max Reitz48758a82017-04-26 15:46:48 +02002563 if (s.src_num > 1 && out_baseimg) {
2564 error_report("Having a backing file for the target makes no sense when "
2565 "concatenating multiple input images");
2566 ret = -1;
2567 goto out;
2568 }
2569
Eric Blaked9f059a2020-07-06 15:39:54 -05002570 if (out_baseimg_param) {
2571 if (!qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT)) {
Eric Blake497a30d2021-05-03 14:36:00 -07002572 error_report("Use of backing file requires explicit "
2573 "backing format");
2574 ret = -1;
2575 goto out;
Eric Blaked9f059a2020-07-06 15:39:54 -05002576 }
2577 }
2578
Kevin Wolfefa84d42009-05-18 16:42:12 +02002579 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002580 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002581 bool encryption =
2582 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002583 const char *encryptfmt =
2584 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
Chunyan Liu83d05212014-06-05 17:20:51 +08002585 const char *preallocation =
2586 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002587
Vladimir Sementsov-Ogievskiyac850bf2019-06-04 19:15:06 +03002588 if (drv && !block_driver_can_compress(drv)) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002589 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002590 ret = -1;
2591 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002592 }
2593
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002594 if (encryption || encryptfmt) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002595 error_report("Compression and encryption not supported at "
2596 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002597 ret = -1;
2598 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002599 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002600
Chunyan Liu83d05212014-06-05 17:20:51 +08002601 if (preallocation
2602 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002603 {
2604 error_report("Compression and preallocation not supported at "
2605 "the same time");
2606 ret = -1;
2607 goto out;
2608 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002609 }
2610
Eric Blake15e39ad2020-05-21 14:21:36 -05002611 /* Determine if bitmaps need copying */
2612 if (bitmaps) {
2613 if (s.src_num > 1) {
2614 error_report("Copying bitmaps only possible with single source");
2615 ret = -1;
2616 goto out;
2617 }
Eric Blake955171e2021-07-21 10:53:48 -05002618 ret = convert_check_bitmaps(blk_bs(s.src[0]), skip_broken);
Eric Blake74a43202021-07-09 10:39:50 -05002619 if (ret < 0) {
Eric Blake15e39ad2020-05-21 14:21:36 -05002620 goto out;
2621 }
2622 }
2623
Daniel P. Berrangé8d65a3c2018-08-14 13:39:47 +01002624 /*
2625 * The later open call will need any decryption secrets, and
2626 * bdrv_create() will purge "opts", so extract them now before
2627 * they are lost.
2628 */
2629 if (!skip_create) {
2630 open_opts = qdict_new();
2631 qemu_opt_foreach(opts, img_add_key_secrets, open_opts, &error_abort);
Daniel P. Berrangé8d65a3c2018-08-14 13:39:47 +01002632
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002633 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002634 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002635 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002636 error_reportf_err(local_err, "%s: error while converting %s: ",
2637 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002638 goto out;
bellardea2384d2004-08-01 21:59:26 +00002639 }
2640 }
ths3b46e622007-09-17 08:09:54 +00002641
Max Reitz4d7c4872019-07-24 19:12:29 +02002642 s.target_is_new = !skip_create;
2643
Peter Lieven9fd77f92017-04-21 11:11:55 +02002644 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002645 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002646 if (ret < 0) {
2647 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002648 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002649 }
2650
Hanna Reitza1c62432021-08-19 12:12:00 +02002651 if (flags & BDRV_O_NOCACHE) {
2652 /*
2653 * If we open the target with O_DIRECT, it may be necessary to
2654 * extend its size to align to the physical sector size.
2655 */
2656 flags |= BDRV_O_RESIZE;
2657 }
2658
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002659 if (skip_create) {
2660 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
Max Reitz3d96cb92019-05-07 22:35:02 +02002661 flags, writethrough, s.quiet, false);
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002662 } else {
2663 /* TODO ultimately we should allow --target-image-opts
2664 * to be used even when -n is not given.
2665 * That has to wait for bdrv_create to be improved
2666 * to allow filenames in option syntax
2667 */
Daniel P. Berrangé8d65a3c2018-08-14 13:39:47 +01002668 s.target = img_open_file(out_filename, open_opts, out_fmt,
Max Reitz3d96cb92019-05-07 22:35:02 +02002669 flags, writethrough, s.quiet, false);
Daniel P. Berrangé8d65a3c2018-08-14 13:39:47 +01002670 open_opts = NULL; /* blk_new_open will have freed it */
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002671 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002672 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002673 ret = -1;
2674 goto out;
2675 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002676 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002677
Eric Blake15e39ad2020-05-21 14:21:36 -05002678 if (bitmaps && !bdrv_supports_persistent_dirty_bitmap(out_bs)) {
2679 error_report("Format driver '%s' does not support bitmaps",
2680 out_bs->drv->format_name);
2681 ret = -1;
2682 goto out;
2683 }
2684
Vladimir Sementsov-Ogievskiyac850bf2019-06-04 19:15:06 +03002685 if (s.compressed && !block_driver_can_compress(out_bs->drv)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002686 error_report("Compression not supported for this file format");
2687 ret = -1;
2688 goto out;
2689 }
2690
Eric Blake5def6b82016-06-23 16:37:19 -06002691 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lieven6360ab22018-07-13 09:15:39 +02002692 * or discard_alignment of the out_bs is greater. Limit to
2693 * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */
2694 s.buf_sectors = MIN(MAX_BUF_SECTORS,
Peter Lieven9fd77f92017-04-21 11:11:55 +02002695 MAX(s.buf_sectors,
2696 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2697 out_bs->bl.pdiscard_alignment >>
2698 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002699
Peter Lieven8dcd3c92018-07-12 15:00:10 +02002700 /* try to align the write requests to the destination to avoid unnecessary
2701 * RMW cycles. */
2702 s.alignment = MAX(pow2floor(s.min_sparse),
2703 DIV_ROUND_UP(out_bs->bl.request_alignment,
2704 BDRV_SECTOR_SIZE));
2705 assert(is_power_of_2(s.alignment));
2706
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002707 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002708 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002709 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002710 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002711 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002712 ret = -1;
2713 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002714 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002715 error_report("output file is smaller than input file");
2716 ret = -1;
2717 goto out;
2718 }
2719 }
2720
Max Reitzc69291e2020-01-21 16:59:14 +01002721 if (s.target_has_backing && s.target_is_new) {
Max Reitz351c8ef2018-05-01 18:57:49 +02002722 /* Errors are treated as "backing length unknown" (which means
2723 * s.target_backing_sectors has to be negative, which it will
2724 * be automatically). The backing file length is used only
2725 * for optimizations, so such a case is not fatal. */
Max Reitz4a2061e2019-06-12 19:00:30 +02002726 s.target_backing_sectors =
2727 bdrv_nb_sectors(bdrv_backing_chain_next(out_bs));
Max Reitz351c8ef2018-05-01 18:57:49 +02002728 } else {
2729 s.target_backing_sectors = -1;
2730 }
2731
Peter Lieven24f833c2013-11-27 11:07:07 +01002732 ret = bdrv_get_info(out_bs, &bdi);
2733 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002734 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002735 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002736 goto out;
2737 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002738 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002739 s.compressed = s.compressed || bdi.needs_compressed_writes;
2740 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Peter Lieven24f833c2013-11-27 11:07:07 +01002741 }
2742
Zhengui0c8c4892020-10-20 14:47:44 +00002743 if (rate_limit) {
2744 set_rate_limit(s.target, rate_limit);
2745 }
2746
Peter Lieven9fd77f92017-04-21 11:11:55 +02002747 ret = convert_do_copy(&s);
Eric Blake15e39ad2020-05-21 14:21:36 -05002748
2749 /* Now copy the bitmaps */
2750 if (bitmaps && ret == 0) {
Eric Blake955171e2021-07-21 10:53:48 -05002751 ret = convert_copy_bitmaps(blk_bs(s.src[0]), out_bs, skip_broken);
Eric Blake15e39ad2020-05-21 14:21:36 -05002752 }
2753
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002754out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002755 if (!ret) {
2756 qemu_progress_print(100, 0);
2757 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002758 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002759 qemu_opts_del(opts);
2760 qemu_opts_free(create_opts);
Daniel P. Berrangé8d65a3c2018-08-14 13:39:47 +01002761 qobject_unref(open_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002762 blk_unref(s.target);
2763 if (s.src) {
2764 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2765 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002766 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002767 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002768 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002769 g_free(s.src_sectors);
Peter Lievenaf8d43d2020-09-01 14:51:29 +02002770 g_free(s.src_alignment);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002771fail_getopt:
Tuguoyi6aec8302020-11-02 09:04:57 +00002772 qemu_opts_del(sn_opts);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002773 g_free(options);
2774
Peter Lieven9fd77f92017-04-21 11:11:55 +02002775 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002776}
2777
bellard57d1a2b2004-08-03 21:15:11 +00002778
bellardfaea38e2006-08-05 21:31:00 +00002779static void dump_snapshots(BlockDriverState *bs)
2780{
2781 QEMUSnapshotInfo *sn_tab, *sn;
2782 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002783
2784 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2785 if (nb_sns <= 0)
2786 return;
2787 printf("Snapshot list:\n");
Markus Armbrustere1ce7d72019-04-17 21:17:55 +02002788 bdrv_snapshot_dump(NULL);
Wenchao Xia5b917042013-05-25 11:09:45 +08002789 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002790 for(i = 0; i < nb_sns; i++) {
2791 sn = &sn_tab[i];
Markus Armbrustere1ce7d72019-04-17 21:17:55 +02002792 bdrv_snapshot_dump(sn);
Wenchao Xia5b917042013-05-25 11:09:45 +08002793 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002794 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002795 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002796}
2797
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002798static void dump_json_image_info_list(ImageInfoList *list)
2799{
Markus Armbrustereab3a462020-12-11 18:11:37 +01002800 GString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002801 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002802 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002803
2804 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2805 visit_complete(v, &obj);
Markus Armbruster6589f452020-12-11 18:11:35 +01002806 str = qobject_to_json_pretty(obj, true);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002807 assert(str != NULL);
Markus Armbrustereab3a462020-12-11 18:11:37 +01002808 printf("%s\n", str->str);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002809 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002810 visit_free(v);
Markus Armbrustereab3a462020-12-11 18:11:37 +01002811 g_string_free(str, true);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002812}
2813
Benoît Canetc054b3f2012-09-05 13:09:02 +02002814static void dump_json_image_info(ImageInfo *info)
2815{
Markus Armbrustereab3a462020-12-11 18:11:37 +01002816 GString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002817 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002818 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002819
2820 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2821 visit_complete(v, &obj);
Markus Armbruster6589f452020-12-11 18:11:35 +01002822 str = qobject_to_json_pretty(obj, true);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002823 assert(str != NULL);
Markus Armbrustereab3a462020-12-11 18:11:37 +01002824 printf("%s\n", str->str);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002825 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002826 visit_free(v);
Markus Armbrustereab3a462020-12-11 18:11:37 +01002827 g_string_free(str, true);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002828}
2829
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002830static void dump_human_image_info_list(ImageInfoList *list)
2831{
2832 ImageInfoList *elem;
2833 bool delim = false;
2834
2835 for (elem = list; elem; elem = elem->next) {
2836 if (delim) {
2837 printf("\n");
2838 }
2839 delim = true;
2840
Markus Armbrustere1ce7d72019-04-17 21:17:55 +02002841 bdrv_image_info_dump(elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002842 }
2843}
2844
2845static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2846{
2847 return strcmp(a, b) == 0;
2848}
2849
2850/**
2851 * Open an image file chain and return an ImageInfoList
2852 *
2853 * @filename: topmost image filename
2854 * @fmt: topmost image format (may be NULL to autodetect)
2855 * @chain: true - enumerate entire backing file chain
2856 * false - only topmost image file
2857 *
2858 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2859 * image file. If there was an error a message will have been printed to
2860 * stderr.
2861 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002862static ImageInfoList *collect_image_info_list(bool image_opts,
2863 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002864 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002865 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002866{
2867 ImageInfoList *head = NULL;
Eric Blakec3033fd2021-01-13 16:10:12 -06002868 ImageInfoList **tail = &head;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002869 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002870 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002871
2872 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2873
2874 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002875 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002876 BlockDriverState *bs;
2877 ImageInfo *info;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002878
2879 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2880 error_report("Backing file '%s' creates an infinite loop.",
2881 filename);
2882 goto err;
2883 }
2884 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2885
Max Reitzefaa7c42016-03-16 19:54:38 +01002886 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002887 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2888 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002889 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002890 goto err;
2891 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002892 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002893
Wenchao Xia43526ec2013-06-06 12:27:58 +08002894 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002895 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002896 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002897 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002898 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002899 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002900
Eric Blakec3033fd2021-01-13 16:10:12 -06002901 QAPI_LIST_APPEND(tail, info);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002902
Markus Armbruster26f54e92014-10-07 13:59:04 +02002903 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002904
Stefan Hajnoczi0da7d132019-12-05 13:46:46 +00002905 /* Clear parameters that only apply to the topmost image */
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002906 filename = fmt = NULL;
Stefan Hajnoczi0da7d132019-12-05 13:46:46 +00002907 image_opts = false;
2908
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002909 if (chain) {
2910 if (info->has_full_backing_filename) {
2911 filename = info->full_backing_filename;
2912 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002913 error_report("Could not determine absolute backing filename,"
2914 " but backing filename '%s' present",
2915 info->backing_filename);
2916 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002917 }
2918 if (info->has_backing_filename_format) {
2919 fmt = info->backing_filename_format;
2920 }
2921 }
2922 }
2923 g_hash_table_destroy(filenames);
2924 return head;
2925
2926err:
2927 qapi_free_ImageInfoList(head);
2928 g_hash_table_destroy(filenames);
2929 return NULL;
2930}
2931
Benoît Canetc054b3f2012-09-05 13:09:02 +02002932static int img_info(int argc, char **argv)
2933{
2934 int c;
2935 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002936 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002937 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002938 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002939 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002940 bool force_share = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002941
bellardea2384d2004-08-01 21:59:26 +00002942 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002943 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002944 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002945 int option_index = 0;
2946 static const struct option long_options[] = {
2947 {"help", no_argument, 0, 'h'},
2948 {"format", required_argument, 0, 'f'},
2949 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002950 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002951 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002952 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002953 {"force-share", no_argument, 0, 'U'},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002954 {0, 0, 0, 0}
2955 };
Fam Zheng335e9932017-05-03 00:35:39 +08002956 c = getopt_long(argc, argv, ":f:hU",
Benoît Canetc054b3f2012-09-05 13:09:02 +02002957 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002958 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002959 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002960 }
bellardea2384d2004-08-01 21:59:26 +00002961 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002962 case ':':
2963 missing_argument(argv[optind - 1]);
2964 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002965 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002966 unrecognized_option(argv[optind - 1]);
2967 break;
bellardea2384d2004-08-01 21:59:26 +00002968 case 'h':
2969 help();
2970 break;
2971 case 'f':
2972 fmt = optarg;
2973 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002974 case 'U':
2975 force_share = true;
2976 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002977 case OPTION_OUTPUT:
2978 output = optarg;
2979 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002980 case OPTION_BACKING_CHAIN:
2981 chain = true;
2982 break;
Kevin Wolf99b1e642021-02-17 12:56:45 +01002983 case OPTION_OBJECT:
2984 user_creatable_process_cmdline(optarg);
2985 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002986 case OPTION_IMAGE_OPTS:
2987 image_opts = true;
2988 break;
bellardea2384d2004-08-01 21:59:26 +00002989 }
2990 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002991 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002992 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002993 }
bellardea2384d2004-08-01 21:59:26 +00002994 filename = argv[optind++];
2995
Benoît Canetc054b3f2012-09-05 13:09:02 +02002996 if (output && !strcmp(output, "json")) {
2997 output_format = OFORMAT_JSON;
2998 } else if (output && !strcmp(output, "human")) {
2999 output_format = OFORMAT_HUMAN;
3000 } else if (output) {
3001 error_report("--output must be used with human or json as argument.");
3002 return 1;
3003 }
3004
Fam Zheng335e9932017-05-03 00:35:39 +08003005 list = collect_image_info_list(image_opts, filename, fmt, chain,
3006 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02003007 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003008 return 1;
3009 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02003010
Benoît Canetc054b3f2012-09-05 13:09:02 +02003011 switch (output_format) {
3012 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02003013 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02003014 break;
3015 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02003016 if (chain) {
3017 dump_json_image_info_list(list);
3018 } else {
3019 dump_json_image_info(list->value);
3020 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02003021 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003022 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02003023
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02003024 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00003025 return 0;
3026}
3027
Eric Blake30065d12019-03-26 13:40:43 -05003028static int dump_map_entry(OutputFormat output_format, MapEntry *e,
3029 MapEntry *next)
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003030{
3031 switch (output_format) {
3032 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08003033 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003034 error_report("File contains external, encrypted or compressed clusters.");
Eric Blake30065d12019-03-26 13:40:43 -05003035 return -1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003036 }
Fam Zheng16b0d552016-01-26 11:59:02 +08003037 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003038 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08003039 e->start, e->length,
3040 e->has_offset ? e->offset : 0,
3041 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003042 }
3043 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
3044 * Modify the flags here to allow more coalescing.
3045 */
Fam Zheng16b0d552016-01-26 11:59:02 +08003046 if (next && (!next->data || next->zero)) {
3047 next->data = false;
3048 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003049 }
3050 break;
3051 case OFORMAT_JSON:
Eyal Moscovicie46c0b12020-05-13 16:36:28 +03003052 printf("{ \"start\": %"PRId64", \"length\": %"PRId64","
Eric Blake8417e132021-07-01 14:06:55 -05003053 " \"depth\": %"PRId64", \"present\": %s, \"zero\": %s,"
3054 " \"data\": %s", e->start, e->length, e->depth,
3055 e->present ? "true" : "false",
Fam Zheng16b0d552016-01-26 11:59:02 +08003056 e->zero ? "true" : "false",
3057 e->data ? "true" : "false");
3058 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02003059 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003060 }
3061 putchar('}');
3062
Eyal Moscovicie46c0b12020-05-13 16:36:28 +03003063 if (next) {
3064 puts(",");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003065 }
3066 break;
3067 }
Eric Blake30065d12019-03-26 13:40:43 -05003068 return 0;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003069}
3070
Eric Blake5e344dd2017-10-11 22:47:02 -05003071static int get_block_status(BlockDriverState *bs, int64_t offset,
3072 int64_t bytes, MapEntry *e)
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003073{
Eric Blake237d78f2017-10-11 22:47:03 -05003074 int ret;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003075 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08003076 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05003077 bool has_offset;
Eric Blake237d78f2017-10-11 22:47:03 -05003078 int64_t map;
Max Reitzf30c66b2019-02-01 20:29:05 +01003079 char *filename = NULL;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003080
3081 /* As an optimization, we could cache the current range of unallocated
3082 * clusters in each file of the chain, and avoid querying the same
3083 * range repeatedly.
3084 */
3085
3086 depth = 0;
3087 for (;;) {
Max Reitz4a2061e2019-06-12 19:00:30 +02003088 bs = bdrv_skip_filters(bs);
Eric Blake237d78f2017-10-11 22:47:03 -05003089 ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003090 if (ret < 0) {
3091 return ret;
3092 }
Eric Blake237d78f2017-10-11 22:47:03 -05003093 assert(bytes);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003094 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
3095 break;
3096 }
Max Reitz4a2061e2019-06-12 19:00:30 +02003097 bs = bdrv_cow_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003098 if (bs == NULL) {
3099 ret = 0;
3100 break;
3101 }
3102
3103 depth++;
3104 }
3105
John Snow28756452016-02-05 13:12:33 -05003106 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
3107
Max Reitzf30c66b2019-02-01 20:29:05 +01003108 if (file && has_offset) {
3109 bdrv_refresh_filename(file);
3110 filename = file->filename;
3111 }
3112
John Snow28756452016-02-05 13:12:33 -05003113 *e = (MapEntry) {
Eric Blake5e344dd2017-10-11 22:47:02 -05003114 .start = offset,
Eric Blake237d78f2017-10-11 22:47:03 -05003115 .length = bytes,
John Snow28756452016-02-05 13:12:33 -05003116 .data = !!(ret & BDRV_BLOCK_DATA),
3117 .zero = !!(ret & BDRV_BLOCK_ZERO),
Eric Blake237d78f2017-10-11 22:47:03 -05003118 .offset = map,
John Snow28756452016-02-05 13:12:33 -05003119 .has_offset = has_offset,
3120 .depth = depth,
Eric Blake8417e132021-07-01 14:06:55 -05003121 .present = !!(ret & BDRV_BLOCK_ALLOCATED),
Max Reitzf30c66b2019-02-01 20:29:05 +01003122 .has_filename = filename,
3123 .filename = filename,
John Snow28756452016-02-05 13:12:33 -05003124 };
3125
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003126 return 0;
3127}
3128
Fam Zheng16b0d552016-01-26 11:59:02 +08003129static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
3130{
3131 if (curr->length == 0) {
3132 return false;
3133 }
3134 if (curr->zero != next->zero ||
3135 curr->data != next->data ||
3136 curr->depth != next->depth ||
Eric Blake8417e132021-07-01 14:06:55 -05003137 curr->present != next->present ||
Fam Zheng16b0d552016-01-26 11:59:02 +08003138 curr->has_filename != next->has_filename ||
3139 curr->has_offset != next->has_offset) {
3140 return false;
3141 }
3142 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
3143 return false;
3144 }
3145 if (curr->has_offset && curr->offset + curr->length != next->offset) {
3146 return false;
3147 }
3148 return true;
3149}
3150
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003151static int img_map(int argc, char **argv)
3152{
3153 int c;
3154 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003155 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003156 BlockDriverState *bs;
3157 const char *filename, *fmt, *output;
3158 int64_t length;
3159 MapEntry curr = { .length = 0 }, next;
3160 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003161 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08003162 bool force_share = false;
Eyal Moscovicic0469492020-05-13 16:36:29 +03003163 int64_t start_offset = 0;
3164 int64_t max_length = -1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003165
3166 fmt = NULL;
3167 output = NULL;
3168 for (;;) {
3169 int option_index = 0;
3170 static const struct option long_options[] = {
3171 {"help", no_argument, 0, 'h'},
3172 {"format", required_argument, 0, 'f'},
3173 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003174 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003175 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003176 {"force-share", no_argument, 0, 'U'},
Eyal Moscovicic0469492020-05-13 16:36:29 +03003177 {"start-offset", required_argument, 0, 's'},
3178 {"max-length", required_argument, 0, 'l'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003179 {0, 0, 0, 0}
3180 };
Eyal Moscovicic0469492020-05-13 16:36:29 +03003181 c = getopt_long(argc, argv, ":f:s:l:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003182 long_options, &option_index);
3183 if (c == -1) {
3184 break;
3185 }
3186 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003187 case ':':
3188 missing_argument(argv[optind - 1]);
3189 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003190 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003191 unrecognized_option(argv[optind - 1]);
3192 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003193 case 'h':
3194 help();
3195 break;
3196 case 'f':
3197 fmt = optarg;
3198 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003199 case 'U':
3200 force_share = true;
3201 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003202 case OPTION_OUTPUT:
3203 output = optarg;
3204 break;
Eyal Moscovicic0469492020-05-13 16:36:29 +03003205 case 's':
3206 start_offset = cvtnum("start offset", optarg);
3207 if (start_offset < 0) {
3208 return 1;
3209 }
3210 break;
3211 case 'l':
3212 max_length = cvtnum("max length", optarg);
3213 if (max_length < 0) {
3214 return 1;
3215 }
3216 break;
Kevin Wolf99b1e642021-02-17 12:56:45 +01003217 case OPTION_OBJECT:
3218 user_creatable_process_cmdline(optarg);
3219 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003220 case OPTION_IMAGE_OPTS:
3221 image_opts = true;
3222 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003223 }
3224 }
Fam Zhengac1307a2014-04-22 13:36:11 +08003225 if (optind != argc - 1) {
3226 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003227 }
Fam Zhengac1307a2014-04-22 13:36:11 +08003228 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003229
3230 if (output && !strcmp(output, "json")) {
3231 output_format = OFORMAT_JSON;
3232 } else if (output && !strcmp(output, "human")) {
3233 output_format = OFORMAT_HUMAN;
3234 } else if (output) {
3235 error_report("--output must be used with human or json as argument.");
3236 return 1;
3237 }
3238
Fam Zheng335e9932017-05-03 00:35:39 +08003239 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003240 if (!blk) {
3241 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003242 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003243 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003244
3245 if (output_format == OFORMAT_HUMAN) {
3246 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
Eyal Moscovicie46c0b12020-05-13 16:36:28 +03003247 } else if (output_format == OFORMAT_JSON) {
3248 putchar('[');
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003249 }
3250
Max Reitzf1d3cd72015-02-05 13:58:18 -05003251 length = blk_getlength(blk);
Eyal Moscovici8f282e82020-05-13 16:36:27 +03003252 if (length < 0) {
3253 error_report("Failed to get size for '%s'", filename);
3254 return 1;
3255 }
Eyal Moscovicic0469492020-05-13 16:36:29 +03003256 if (max_length != -1) {
3257 length = MIN(start_offset + max_length, length);
3258 }
Eyal Moscovici8f282e82020-05-13 16:36:27 +03003259
Eyal Moscovicic0469492020-05-13 16:36:29 +03003260 curr.start = start_offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003261 while (curr.start + curr.length < length) {
Eric Blake5e344dd2017-10-11 22:47:02 -05003262 int64_t offset = curr.start + curr.length;
Kevin Wolfd0ceea82020-07-07 16:46:29 +02003263 int64_t n = length - offset;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003264
Eric Blake5e344dd2017-10-11 22:47:02 -05003265 ret = get_block_status(bs, offset, n, &next);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003266 if (ret < 0) {
3267 error_report("Could not read file metadata: %s", strerror(-ret));
3268 goto out;
3269 }
3270
Fam Zheng16b0d552016-01-26 11:59:02 +08003271 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003272 curr.length += next.length;
3273 continue;
3274 }
3275
3276 if (curr.length > 0) {
Eric Blake30065d12019-03-26 13:40:43 -05003277 ret = dump_map_entry(output_format, &curr, &next);
3278 if (ret < 0) {
3279 goto out;
3280 }
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003281 }
3282 curr = next;
3283 }
3284
Eric Blake30065d12019-03-26 13:40:43 -05003285 ret = dump_map_entry(output_format, &curr, NULL);
Eyal Moscovicie46c0b12020-05-13 16:36:28 +03003286 if (output_format == OFORMAT_JSON) {
3287 puts("]");
3288 }
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003289
3290out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003291 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02003292 return ret < 0;
3293}
3294
aliguorif7b4a942009-01-07 17:40:15 +00003295#define SNAPSHOT_LIST 1
3296#define SNAPSHOT_CREATE 2
3297#define SNAPSHOT_APPLY 3
3298#define SNAPSHOT_DELETE 4
3299
Stuart Brady153859b2009-06-07 00:42:17 +01003300static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00003301{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003302 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00003303 BlockDriverState *bs;
3304 QEMUSnapshotInfo sn;
3305 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003306 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00003307 int action = 0;
3308 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003309 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003310 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003311 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08003312 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00003313
Kevin Wolfce099542016-03-15 13:01:04 +01003314 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00003315 /* Parse commandline parameters */
3316 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003317 static const struct option long_options[] = {
3318 {"help", no_argument, 0, 'h'},
3319 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003320 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003321 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003322 {0, 0, 0, 0}
3323 };
Fam Zheng335e9932017-05-03 00:35:39 +08003324 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003325 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003326 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00003327 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003328 }
aliguorif7b4a942009-01-07 17:40:15 +00003329 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003330 case ':':
3331 missing_argument(argv[optind - 1]);
3332 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003333 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003334 unrecognized_option(argv[optind - 1]);
3335 break;
aliguorif7b4a942009-01-07 17:40:15 +00003336 case 'h':
3337 help();
Stuart Brady153859b2009-06-07 00:42:17 +01003338 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003339 case 'l':
3340 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003341 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01003342 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003343 }
3344 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02003345 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00003346 break;
3347 case 'a':
3348 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003349 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01003350 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003351 }
3352 action = SNAPSHOT_APPLY;
3353 snapshot_name = optarg;
3354 break;
3355 case 'c':
3356 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003357 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01003358 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003359 }
3360 action = SNAPSHOT_CREATE;
3361 snapshot_name = optarg;
3362 break;
3363 case 'd':
3364 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003365 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01003366 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003367 }
3368 action = SNAPSHOT_DELETE;
3369 snapshot_name = optarg;
3370 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003371 case 'q':
3372 quiet = true;
3373 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003374 case 'U':
3375 force_share = true;
3376 break;
Kevin Wolf99b1e642021-02-17 12:56:45 +01003377 case OPTION_OBJECT:
3378 user_creatable_process_cmdline(optarg);
3379 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003380 case OPTION_IMAGE_OPTS:
3381 image_opts = true;
3382 break;
aliguorif7b4a942009-01-07 17:40:15 +00003383 }
3384 }
3385
Kevin Wolffc11eb22013-08-05 10:53:04 +02003386 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003387 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003388 }
aliguorif7b4a942009-01-07 17:40:15 +00003389 filename = argv[optind++];
3390
3391 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08003392 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
3393 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003394 if (!blk) {
3395 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003396 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003397 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00003398
3399 /* Perform the requested action */
3400 switch(action) {
3401 case SNAPSHOT_LIST:
3402 dump_snapshots(bs);
3403 break;
3404
3405 case SNAPSHOT_CREATE:
3406 memset(&sn, 0, sizeof(sn));
3407 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
3408
3409 qemu_gettimeofday(&tv);
3410 sn.date_sec = tv.tv_sec;
3411 sn.date_nsec = tv.tv_usec * 1000;
3412
3413 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003414 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003415 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003416 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003417 }
aliguorif7b4a942009-01-07 17:40:15 +00003418 break;
3419
3420 case SNAPSHOT_APPLY:
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01003421 ret = bdrv_snapshot_goto(bs, snapshot_name, &err);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003422 if (ret) {
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01003423 error_reportf_err(err, "Could not apply snapshot '%s': ",
3424 snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003425 }
aliguorif7b4a942009-01-07 17:40:15 +00003426 break;
3427
3428 case SNAPSHOT_DELETE:
Daniel Henrique Barboza8c040932018-11-07 11:09:59 -02003429 ret = bdrv_snapshot_find(bs, &sn, snapshot_name);
3430 if (ret < 0) {
3431 error_report("Could not delete snapshot '%s': snapshot not "
3432 "found", snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003433 ret = 1;
Daniel Henrique Barboza8c040932018-11-07 11:09:59 -02003434 } else {
3435 ret = bdrv_snapshot_delete(bs, sn.id_str, sn.name, &err);
3436 if (ret < 0) {
3437 error_reportf_err(err, "Could not delete snapshot '%s': ",
3438 snapshot_name);
3439 ret = 1;
3440 }
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003441 }
aliguorif7b4a942009-01-07 17:40:15 +00003442 break;
3443 }
3444
3445 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003446 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003447 if (ret) {
3448 return 1;
3449 }
Stuart Brady153859b2009-06-07 00:42:17 +01003450 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003451}
3452
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003453static int img_rebase(int argc, char **argv)
3454{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003455 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003456 uint8_t *buf_old = NULL;
3457 uint8_t *buf_new = NULL;
Sam Eiderman863cc782019-05-23 19:33:36 +03003458 BlockDriverState *bs = NULL, *prefix_chain_bs = NULL;
Max Reitz4a2061e2019-06-12 19:00:30 +02003459 BlockDriverState *unfiltered_bs;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003460 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003461 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3462 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003463 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003464 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003465 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003466 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003467 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003468 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003469 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003470
3471 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003472 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003473 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003474 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003475 out_baseimg = NULL;
3476 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003477 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003478 static const struct option long_options[] = {
3479 {"help", no_argument, 0, 'h'},
3480 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003481 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003482 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003483 {0, 0, 0, 0}
3484 };
Fam Zheng335e9932017-05-03 00:35:39 +08003485 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003486 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003487 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003488 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003489 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003490 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003491 case ':':
3492 missing_argument(argv[optind - 1]);
3493 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003494 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003495 unrecognized_option(argv[optind - 1]);
3496 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003497 case 'h':
3498 help();
3499 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003500 case 'f':
3501 fmt = optarg;
3502 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003503 case 'F':
3504 out_basefmt = optarg;
3505 break;
3506 case 'b':
3507 out_baseimg = optarg;
3508 break;
3509 case 'u':
3510 unsafe = 1;
3511 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003512 case 'p':
3513 progress = 1;
3514 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003515 case 't':
3516 cache = optarg;
3517 break;
Max Reitz40055952014-07-22 22:58:42 +02003518 case 'T':
3519 src_cache = optarg;
3520 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003521 case 'q':
3522 quiet = true;
3523 break;
Kevin Wolf99b1e642021-02-17 12:56:45 +01003524 case OPTION_OBJECT:
3525 user_creatable_process_cmdline(optarg);
3526 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003527 case OPTION_IMAGE_OPTS:
3528 image_opts = true;
3529 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003530 case 'U':
3531 force_share = true;
3532 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003533 }
3534 }
3535
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003536 if (quiet) {
3537 progress = 0;
3538 }
3539
Fam Zhengac1307a2014-04-22 13:36:11 +08003540 if (optind != argc - 1) {
3541 error_exit("Expecting one image file name");
3542 }
3543 if (!unsafe && !out_baseimg) {
3544 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003545 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003546 filename = argv[optind++];
3547
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003548 qemu_progress_init(progress, 2.0);
3549 qemu_progress_print(0, 100);
3550
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003551 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003552 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003553 if (ret < 0) {
3554 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003555 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003556 }
3557
Kevin Wolfce099542016-03-15 13:01:04 +01003558 src_flags = 0;
3559 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003560 if (ret < 0) {
3561 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003562 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003563 }
3564
Kevin Wolfce099542016-03-15 13:01:04 +01003565 /* The source files are opened read-only, don't care about WCE */
3566 assert((src_flags & BDRV_O_RDWR) == 0);
3567 (void) src_writethrough;
3568
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003569 /*
3570 * Open the images.
3571 *
3572 * Ignore the old backing file for unsafe rebase in case we want to correct
3573 * the reference to a renamed or moved backing file.
3574 */
Fam Zheng335e9932017-05-03 00:35:39 +08003575 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3576 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003577 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003578 ret = -1;
3579 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003580 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003581 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003582
Max Reitz4a2061e2019-06-12 19:00:30 +02003583 unfiltered_bs = bdrv_skip_filters(bs);
3584
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003585 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003586 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003587 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003588 ret = -1;
3589 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003590 }
3591 }
3592
3593 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003594 if (!unsafe) {
Max Reitz644483d2015-02-05 13:58:17 -05003595 QDict *options = NULL;
Max Reitz4a2061e2019-06-12 19:00:30 +02003596 BlockDriverState *base_bs = bdrv_cow_bs(unfiltered_bs);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003597
Sam Eiderman4ebe0612019-05-23 19:33:35 +03003598 if (base_bs) {
Kevin Wolfd861ab32019-04-25 14:25:10 +02003599 blk_old_backing = blk_new(qemu_get_aio_context(),
3600 BLK_PERM_CONSISTENT_READ,
Sam Eiderman4ebe0612019-05-23 19:33:35 +03003601 BLK_PERM_ALL);
3602 ret = blk_insert_bs(blk_old_backing, base_bs,
3603 &local_err);
3604 if (ret < 0) {
Max Reitz35ddd932019-05-09 19:52:35 +02003605 error_reportf_err(local_err,
Sam Eiderman4ebe0612019-05-23 19:33:35 +03003606 "Could not reuse old backing file '%s': ",
3607 base_bs->filename);
Max Reitz35ddd932019-05-09 19:52:35 +02003608 goto out;
3609 }
3610 } else {
3611 blk_old_backing = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003612 }
Max Reitz644483d2015-02-05 13:58:17 -05003613
Alex Bligha6166732012-10-16 13:46:18 +01003614 if (out_baseimg[0]) {
Max Reitzd16699b2018-05-09 20:20:01 +02003615 const char *overlay_filename;
3616 char *out_real_path;
3617
Fam Zheng335e9932017-05-03 00:35:39 +08003618 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003619 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003620 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003621 }
3622 if (force_share) {
3623 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003624 }
3625
Max Reitzf30c66b2019-02-01 20:29:05 +01003626 bdrv_refresh_filename(bs);
Max Reitzd16699b2018-05-09 20:20:01 +02003627 overlay_filename = bs->exact_filename[0] ? bs->exact_filename
3628 : bs->filename;
Max Reitz645ae7d2019-02-01 20:29:14 +01003629 out_real_path =
3630 bdrv_get_full_backing_filename_from_filename(overlay_filename,
3631 out_baseimg,
3632 &local_err);
Max Reitzd16699b2018-05-09 20:20:01 +02003633 if (local_err) {
Max Reitzf22356d2019-05-28 21:53:38 +02003634 qobject_unref(options);
Max Reitzd16699b2018-05-09 20:20:01 +02003635 error_reportf_err(local_err,
3636 "Could not resolve backing filename: ");
3637 ret = -1;
Max Reitzd16699b2018-05-09 20:20:01 +02003638 goto out;
3639 }
3640
Sam Eiderman863cc782019-05-23 19:33:36 +03003641 /*
3642 * Find out whether we rebase an image on top of a previous image
3643 * in its chain.
3644 */
3645 prefix_chain_bs = bdrv_find_backing_image(bs, out_real_path);
Sam Eiderman330c7292019-05-23 19:33:37 +03003646 if (prefix_chain_bs) {
Max Reitzf22356d2019-05-28 21:53:38 +02003647 qobject_unref(options);
Sam Eiderman330c7292019-05-23 19:33:37 +03003648 g_free(out_real_path);
Max Reitzf22356d2019-05-28 21:53:38 +02003649
Kevin Wolfd861ab32019-04-25 14:25:10 +02003650 blk_new_backing = blk_new(qemu_get_aio_context(),
3651 BLK_PERM_CONSISTENT_READ,
Sam Eiderman330c7292019-05-23 19:33:37 +03003652 BLK_PERM_ALL);
3653 ret = blk_insert_bs(blk_new_backing, prefix_chain_bs,
3654 &local_err);
3655 if (ret < 0) {
3656 error_reportf_err(local_err,
3657 "Could not reuse backing file '%s': ",
3658 out_baseimg);
3659 goto out;
3660 }
3661 } else {
3662 blk_new_backing = blk_new_open(out_real_path, NULL,
3663 options, src_flags, &local_err);
3664 g_free(out_real_path);
3665 if (!blk_new_backing) {
3666 error_reportf_err(local_err,
3667 "Could not open new backing file '%s': ",
3668 out_baseimg);
3669 ret = -1;
3670 goto out;
3671 }
Alex Bligha6166732012-10-16 13:46:18 +01003672 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003673 }
3674 }
3675
3676 /*
3677 * Check each unallocated cluster in the COW file. If it is unallocated,
3678 * accesses go to the backing file. We must therefore compare this cluster
3679 * in the old and new backing file, and if they differ we need to copy it
3680 * from the old backing file into the COW file.
3681 *
3682 * If qemu-img crashes during this step, no harm is done. The content of
3683 * the image is the same as the original one at any time.
3684 */
3685 if (!unsafe) {
Eric Blake41536282017-10-11 22:47:15 -05003686 int64_t size;
Max Reitz35ddd932019-05-09 19:52:35 +02003687 int64_t old_backing_size = 0;
Eric Blake41536282017-10-11 22:47:15 -05003688 int64_t new_backing_size = 0;
3689 uint64_t offset;
3690 int64_t n;
Kevin Wolf1f710492012-10-12 14:29:18 +02003691 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003692
Max Reitzf1d3cd72015-02-05 13:58:18 -05003693 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3694 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003695
Eric Blake41536282017-10-11 22:47:15 -05003696 size = blk_getlength(blk);
3697 if (size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003698 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003699 filename, strerror(-size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003700 ret = -1;
3701 goto out;
3702 }
Max Reitz35ddd932019-05-09 19:52:35 +02003703 if (blk_old_backing) {
3704 old_backing_size = blk_getlength(blk_old_backing);
3705 if (old_backing_size < 0) {
3706 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003707
Max Reitz35ddd932019-05-09 19:52:35 +02003708 bdrv_get_backing_filename(bs, backing_name,
3709 sizeof(backing_name));
3710 error_report("Could not get size of '%s': %s",
3711 backing_name, strerror(-old_backing_size));
3712 ret = -1;
3713 goto out;
3714 }
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003715 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003716 if (blk_new_backing) {
Eric Blake41536282017-10-11 22:47:15 -05003717 new_backing_size = blk_getlength(blk_new_backing);
3718 if (new_backing_size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003719 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003720 out_baseimg, strerror(-new_backing_size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003721 ret = -1;
3722 goto out;
3723 }
Alex Bligha6166732012-10-16 13:46:18 +01003724 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003725
Eric Blake41536282017-10-11 22:47:15 -05003726 if (size != 0) {
3727 local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE));
Kevin Wolf1f710492012-10-12 14:29:18 +02003728 }
3729
Eric Blake41536282017-10-11 22:47:15 -05003730 for (offset = 0; offset < size; offset += n) {
Max Reitz1c6e8772019-05-09 19:52:36 +02003731 bool buf_old_is_zero = false;
3732
Eric Blake41536282017-10-11 22:47:15 -05003733 /* How many bytes can we handle with the next read? */
3734 n = MIN(IO_BUF_SIZE, size - offset);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003735
3736 /* If the cluster is allocated, we don't need to take action */
Max Reitz4a2061e2019-06-12 19:00:30 +02003737 ret = bdrv_is_allocated(unfiltered_bs, offset, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003738 if (ret < 0) {
3739 error_report("error while reading image metadata: %s",
3740 strerror(-ret));
3741 goto out;
3742 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003743 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003744 continue;
3745 }
3746
Sam Eiderman863cc782019-05-23 19:33:36 +03003747 if (prefix_chain_bs) {
3748 /*
3749 * If cluster wasn't changed since prefix_chain, we don't need
3750 * to take action
3751 */
Max Reitz4a2061e2019-06-12 19:00:30 +02003752 ret = bdrv_is_allocated_above(bdrv_cow_bs(unfiltered_bs),
3753 prefix_chain_bs, false,
3754 offset, n, &n);
Sam Eiderman863cc782019-05-23 19:33:36 +03003755 if (ret < 0) {
3756 error_report("error while reading image metadata: %s",
3757 strerror(-ret));
3758 goto out;
3759 }
3760 if (!ret) {
3761 continue;
3762 }
3763 }
3764
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003765 /*
3766 * Read old and new backing file and take into consideration that
3767 * backing files may be smaller than the COW image.
3768 */
Eric Blake41536282017-10-11 22:47:15 -05003769 if (offset >= old_backing_size) {
3770 memset(buf_old, 0, n);
Max Reitz1c6e8772019-05-09 19:52:36 +02003771 buf_old_is_zero = true;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003772 } else {
Eric Blake41536282017-10-11 22:47:15 -05003773 if (offset + n > old_backing_size) {
3774 n = old_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003775 }
3776
Eric Blake41536282017-10-11 22:47:15 -05003777 ret = blk_pread(blk_old_backing, offset, buf_old, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003778 if (ret < 0) {
3779 error_report("error while reading from old backing file");
3780 goto out;
3781 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003782 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003783
Eric Blake41536282017-10-11 22:47:15 -05003784 if (offset >= new_backing_size || !blk_new_backing) {
3785 memset(buf_new, 0, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003786 } else {
Eric Blake41536282017-10-11 22:47:15 -05003787 if (offset + n > new_backing_size) {
3788 n = new_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003789 }
3790
Eric Blake41536282017-10-11 22:47:15 -05003791 ret = blk_pread(blk_new_backing, offset, buf_new, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003792 if (ret < 0) {
3793 error_report("error while reading from new backing file");
3794 goto out;
3795 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003796 }
3797
3798 /* If they differ, we need to write to the COW file */
3799 uint64_t written = 0;
3800
Eric Blake41536282017-10-11 22:47:15 -05003801 while (written < n) {
Eric Blakedc61cd32017-10-11 22:47:14 -05003802 int64_t pnum;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003803
Eric Blake41536282017-10-11 22:47:15 -05003804 if (compare_buffers(buf_old + written, buf_new + written,
3805 n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003806 {
Max Reitz1c6e8772019-05-09 19:52:36 +02003807 if (buf_old_is_zero) {
3808 ret = blk_pwrite_zeroes(blk, offset + written, pnum, 0);
3809 } else {
3810 ret = blk_pwrite(blk, offset + written,
3811 buf_old + written, pnum, 0);
3812 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003813 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003814 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003815 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003816 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003817 }
3818 }
3819
3820 written += pnum;
3821 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003822 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003823 }
3824 }
3825
3826 /*
3827 * Change the backing file. All clusters that are different from the old
3828 * backing file are overwritten in the COW file now, so the visible content
3829 * doesn't change when we switch the backing file.
3830 */
Alex Bligha6166732012-10-16 13:46:18 +01003831 if (out_baseimg && *out_baseimg) {
Max Reitz4a2061e2019-06-12 19:00:30 +02003832 ret = bdrv_change_backing_file(unfiltered_bs, out_baseimg, out_basefmt,
3833 true);
Alex Bligha6166732012-10-16 13:46:18 +01003834 } else {
Max Reitz4a2061e2019-06-12 19:00:30 +02003835 ret = bdrv_change_backing_file(unfiltered_bs, NULL, NULL, false);
Alex Bligha6166732012-10-16 13:46:18 +01003836 }
3837
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003838 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003839 error_report("Could not change the backing file to '%s': No "
3840 "space left in the file header", out_baseimg);
Eric Blakea7cd44b2021-07-08 10:52:28 -05003841 } else if (ret == -EINVAL && out_baseimg && !out_basefmt) {
3842 error_report("Could not change the backing file to '%s': backing "
3843 "format must be specified", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003844 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003845 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003846 out_baseimg, strerror(-ret));
3847 }
3848
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003849 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003850 /*
3851 * TODO At this point it is possible to check if any clusters that are
3852 * allocated in the COW file are the same in the backing file. If so, they
3853 * could be dropped from the COW file. Don't do this before switching the
3854 * backing file, in case of a crash this would lead to corruption.
3855 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003856out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003857 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003858 /* Cleanup */
3859 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003860 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003861 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003862 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003863 qemu_vfree(buf_old);
3864 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003865
Markus Armbruster26f54e92014-10-07 13:59:04 +02003866 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003867 if (ret) {
3868 return 1;
3869 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003870 return 0;
3871}
3872
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003873static int img_resize(int argc, char **argv)
3874{
Markus Armbruster6750e792015-02-12 17:43:08 +01003875 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003876 int c, ret, relative;
3877 const char *filename, *fmt, *size;
Max Reitz09c5c6d2019-09-18 11:51:44 +02003878 int64_t n, total_size, current_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003879 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003880 BlockBackend *blk = NULL;
Max Reitzdc5f6902017-06-13 22:20:55 +02003881 PreallocMode prealloc = PREALLOC_MODE_OFF;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003882 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003883
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003884 static QemuOptsList resize_options = {
3885 .name = "resize_options",
3886 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3887 .desc = {
3888 {
3889 .name = BLOCK_OPT_SIZE,
3890 .type = QEMU_OPT_SIZE,
3891 .help = "Virtual disk size"
3892 }, {
3893 /* end of list */
3894 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003895 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003896 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003897 bool image_opts = false;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003898 bool shrink = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003899
Kevin Wolfe80fec72011-04-29 10:58:12 +02003900 /* Remove size from argv manually so that negative numbers are not treated
3901 * as options by getopt. */
3902 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003903 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003904 return 1;
3905 }
3906
3907 size = argv[--argc];
3908
3909 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003910 fmt = NULL;
3911 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003912 static const struct option long_options[] = {
3913 {"help", no_argument, 0, 'h'},
3914 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003915 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Max Reitzdc5f6902017-06-13 22:20:55 +02003916 {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003917 {"shrink", no_argument, 0, OPTION_SHRINK},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003918 {0, 0, 0, 0}
3919 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003920 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003921 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003922 if (c == -1) {
3923 break;
3924 }
3925 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003926 case ':':
3927 missing_argument(argv[optind - 1]);
3928 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003929 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003930 unrecognized_option(argv[optind - 1]);
3931 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003932 case 'h':
3933 help();
3934 break;
3935 case 'f':
3936 fmt = optarg;
3937 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003938 case 'q':
3939 quiet = true;
3940 break;
Kevin Wolf99b1e642021-02-17 12:56:45 +01003941 case OPTION_OBJECT:
3942 user_creatable_process_cmdline(optarg);
3943 break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003944 case OPTION_IMAGE_OPTS:
3945 image_opts = true;
3946 break;
Max Reitzdc5f6902017-06-13 22:20:55 +02003947 case OPTION_PREALLOCATION:
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02003948 prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
Markus Armbruster06c60b62017-08-24 10:45:57 +02003949 PREALLOC_MODE__MAX, NULL);
Max Reitzdc5f6902017-06-13 22:20:55 +02003950 if (prealloc == PREALLOC_MODE__MAX) {
3951 error_report("Invalid preallocation mode '%s'", optarg);
3952 return 1;
3953 }
3954 break;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003955 case OPTION_SHRINK:
3956 shrink = true;
3957 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003958 }
3959 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003960 if (optind != argc - 1) {
Max Reitzbe8fbd42018-02-05 17:27:45 +01003961 error_exit("Expecting image file name and size");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003962 }
3963 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003964
3965 /* Choose grow, shrink, or absolute resize mode */
3966 switch (size[0]) {
3967 case '+':
3968 relative = 1;
3969 size++;
3970 break;
3971 case '-':
3972 relative = -1;
3973 size++;
3974 break;
3975 default:
3976 relative = 0;
3977 break;
3978 }
3979
3980 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003981 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbruster235e59c2020-07-07 18:05:42 +02003982 if (!qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err)) {
Markus Armbruster6750e792015-02-12 17:43:08 +01003983 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003984 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003985 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003986 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003987 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003988 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3989 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003990
Max Reitzefaa7c42016-03-16 19:54:38 +01003991 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003992 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3993 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003994 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003995 ret = -1;
3996 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003997 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003998
Max Reitzdc5f6902017-06-13 22:20:55 +02003999 current_size = blk_getlength(blk);
4000 if (current_size < 0) {
4001 error_report("Failed to inquire current image length: %s",
4002 strerror(-current_size));
4003 ret = -1;
4004 goto out;
4005 }
4006
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01004007 if (relative) {
Max Reitzdc5f6902017-06-13 22:20:55 +02004008 total_size = current_size + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01004009 } else {
4010 total_size = n;
4011 }
4012 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01004013 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09004014 ret = -1;
4015 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01004016 }
4017
Max Reitzdc5f6902017-06-13 22:20:55 +02004018 if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
4019 error_report("Preallocation can only be used for growing images");
4020 ret = -1;
4021 goto out;
4022 }
4023
Pavel Butsykin4ffca892017-09-18 15:42:27 +03004024 if (total_size < current_size && !shrink) {
Kevin Wolf1c404d72020-07-10 14:17:17 +02004025 error_report("Use the --shrink option to perform a shrink operation.");
Pavel Butsykin4ffca892017-09-18 15:42:27 +03004026 warn_report("Shrinking an image will delete all data beyond the "
4027 "shrunken image's end. Before performing such an "
4028 "operation, make sure there is no important data there.");
Kevin Wolf1c404d72020-07-10 14:17:17 +02004029 ret = -1;
4030 goto out;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03004031 }
4032
Max Reitze8d04f92019-09-18 11:51:43 +02004033 /*
4034 * The user expects the image to have the desired size after
4035 * resizing, so pass @exact=true. It is of no use to report
4036 * success when the image has not actually been resized.
4037 */
Kevin Wolf8c6242b2020-04-24 14:54:41 +02004038 ret = blk_truncate(blk, total_size, true, prealloc, 0, &err);
Max Reitz09c5c6d2019-09-18 11:51:44 +02004039 if (!ret) {
4040 qprintf(quiet, "Image resized.\n");
4041 } else {
Max Reitzed3d2ec2017-03-28 22:51:27 +02004042 error_report_err(err);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01004043 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09004044out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02004045 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09004046 if (ret) {
4047 return 1;
4048 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01004049 return 0;
4050}
4051
Max Reitz76a3a342014-10-27 11:12:51 +01004052static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02004053 int64_t offset, int64_t total_work_size,
4054 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01004055{
4056 qemu_progress_print(100.f * offset / total_work_size, 0);
4057}
4058
Max Reitz51641352018-05-09 23:00:20 +02004059static int print_amend_option_help(const char *format)
4060{
4061 BlockDriver *drv;
4062
4063 /* Find driver and parse its options */
4064 drv = bdrv_find_format(format);
4065 if (!drv) {
4066 error_report("Unknown file format '%s'", format);
4067 return 1;
4068 }
4069
4070 if (!drv->bdrv_amend_options) {
4071 error_report("Format driver '%s' does not support option amendment",
4072 format);
4073 return 1;
4074 }
4075
Maxim Levitskydf373fb2020-06-25 14:55:39 +02004076 /* Every driver supporting amendment must have amend_opts */
4077 assert(drv->amend_opts);
Max Reitz51641352018-05-09 23:00:20 +02004078
Maxim Levitsky0b6786a2020-06-25 14:55:40 +02004079 printf("Amend options for '%s':\n", format);
Maxim Levitskydf373fb2020-06-25 14:55:39 +02004080 qemu_opts_print_help(drv->amend_opts, false);
Max Reitz51641352018-05-09 23:00:20 +02004081 return 0;
4082}
4083
Max Reitz6f176b42013-09-03 10:09:50 +02004084static int img_amend(int argc, char **argv)
4085{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01004086 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02004087 int c, ret = 0;
4088 char *options = NULL;
Maxim Levitskydf373fb2020-06-25 14:55:39 +02004089 QemuOptsList *amend_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08004090 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02004091 const char *fmt = NULL, *filename, *cache;
4092 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01004093 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01004094 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02004095 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02004096 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004097 bool image_opts = false;
Maxim Levitskya3579bf2020-06-25 14:55:38 +02004098 bool force = false;
Max Reitz6f176b42013-09-03 10:09:50 +02004099
Max Reitzbd39e6e2014-07-22 22:58:43 +02004100 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02004101 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004102 static const struct option long_options[] = {
4103 {"help", no_argument, 0, 'h'},
4104 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004105 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Maxim Levitskya3579bf2020-06-25 14:55:38 +02004106 {"force", no_argument, 0, OPTION_FORCE},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004107 {0, 0, 0, 0}
4108 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004109 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004110 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02004111 if (c == -1) {
4112 break;
4113 }
4114
4115 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004116 case ':':
4117 missing_argument(argv[optind - 1]);
4118 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08004119 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004120 unrecognized_option(argv[optind - 1]);
4121 break;
4122 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08004123 help();
4124 break;
4125 case 'o':
Markus Armbruster6d2b5cb2020-04-15 09:49:25 +02004126 if (accumulate_options(&options, optarg) < 0) {
Stefan Hajnoczif7077622017-03-17 18:45:40 +08004127 ret = -1;
4128 goto out_no_progress;
4129 }
Stefan Hajnoczif7077622017-03-17 18:45:40 +08004130 break;
4131 case 'f':
4132 fmt = optarg;
4133 break;
4134 case 't':
4135 cache = optarg;
4136 break;
4137 case 'p':
4138 progress = true;
4139 break;
4140 case 'q':
4141 quiet = true;
4142 break;
4143 case OPTION_OBJECT:
Kevin Wolf99b1e642021-02-17 12:56:45 +01004144 user_creatable_process_cmdline(optarg);
Stefan Hajnoczif7077622017-03-17 18:45:40 +08004145 break;
4146 case OPTION_IMAGE_OPTS:
4147 image_opts = true;
4148 break;
Maxim Levitskya3579bf2020-06-25 14:55:38 +02004149 case OPTION_FORCE:
4150 force = true;
4151 break;
Max Reitz6f176b42013-09-03 10:09:50 +02004152 }
4153 }
4154
Max Reitz6f176b42013-09-03 10:09:50 +02004155 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08004156 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02004157 }
4158
Max Reitz76a3a342014-10-27 11:12:51 +01004159 if (quiet) {
4160 progress = false;
4161 }
4162 qemu_progress_init(progress, 1.0);
4163
Kevin Wolfa283cb62014-02-21 16:24:07 +01004164 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
4165 if (fmt && has_help_option(options)) {
4166 /* If a format is explicitly specified (and possibly no filename is
4167 * given), print option help here */
Max Reitz51641352018-05-09 23:00:20 +02004168 ret = print_amend_option_help(fmt);
Kevin Wolfa283cb62014-02-21 16:24:07 +01004169 goto out;
4170 }
4171
4172 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01004173 error_report("Expecting one image file name");
4174 ret = -1;
4175 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01004176 }
Max Reitz6f176b42013-09-03 10:09:50 +02004177
Kevin Wolfce099542016-03-15 13:01:04 +01004178 flags = BDRV_O_RDWR;
4179 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02004180 if (ret < 0) {
4181 error_report("Invalid cache option: %s", cache);
4182 goto out;
4183 }
4184
Fam Zheng335e9932017-05-03 00:35:39 +08004185 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4186 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02004187 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02004188 ret = -1;
4189 goto out;
4190 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02004191 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02004192
4193 fmt = bs->drv->format_name;
4194
Kevin Wolf626f84f2014-02-21 16:24:06 +01004195 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01004196 /* If the format was auto-detected, print option help here */
Max Reitz51641352018-05-09 23:00:20 +02004197 ret = print_amend_option_help(fmt);
Max Reitz6f176b42013-09-03 10:09:50 +02004198 goto out;
4199 }
4200
Max Reitz1f996682018-05-09 23:00:17 +02004201 if (!bs->drv->bdrv_amend_options) {
4202 error_report("Format driver '%s' does not support option amendment",
Max Reitzb2439d22014-12-02 18:32:47 +01004203 fmt);
4204 ret = -1;
4205 goto out;
4206 }
4207
Maxim Levitskydf373fb2020-06-25 14:55:39 +02004208 /* Every driver supporting amendment must have amend_opts */
4209 assert(bs->drv->amend_opts);
Max Reitz1f996682018-05-09 23:00:17 +02004210
Maxim Levitskydf373fb2020-06-25 14:55:39 +02004211 amend_opts = qemu_opts_append(amend_opts, bs->drv->amend_opts);
4212 opts = qemu_opts_create(amend_opts, NULL, 0, &error_abort);
Markus Armbruster235e59c2020-07-07 18:05:42 +02004213 if (!qemu_opts_do_parse(opts, options, NULL, &err)) {
Maxim Levitsky0b6786a2020-06-25 14:55:40 +02004214 /* Try to parse options using the create options */
Maxim Levitsky0b6786a2020-06-25 14:55:40 +02004215 amend_opts = qemu_opts_append(amend_opts, bs->drv->create_opts);
4216 qemu_opts_del(opts);
4217 opts = qemu_opts_create(amend_opts, NULL, 0, &error_abort);
Markus Armbruster9e194e02020-07-07 18:06:11 +02004218 if (qemu_opts_do_parse(opts, options, NULL, NULL)) {
Maxim Levitsky0b6786a2020-06-25 14:55:40 +02004219 error_append_hint(&err,
4220 "This option is only supported for image creation\n");
Maxim Levitsky0b6786a2020-06-25 14:55:40 +02004221 }
4222
Paolo Bonziniece90862017-01-04 15:56:24 +01004223 error_report_err(err);
4224 ret = -1;
4225 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02004226 }
4227
Max Reitz76a3a342014-10-27 11:12:51 +01004228 /* In case the driver does not call amend_status_cb() */
4229 qemu_progress_print(0.f, 0);
Maxim Levitskya3579bf2020-06-25 14:55:38 +02004230 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, force, &err);
Max Reitz76a3a342014-10-27 11:12:51 +01004231 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02004232 if (ret < 0) {
Max Reitzd1402b52018-05-09 23:00:18 +02004233 error_report_err(err);
Max Reitz6f176b42013-09-03 10:09:50 +02004234 goto out;
4235 }
4236
4237out:
Max Reitz76a3a342014-10-27 11:12:51 +01004238 qemu_progress_end();
4239
Max Reitze814dff2015-08-20 16:00:38 -07004240out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02004241 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08004242 qemu_opts_del(opts);
Maxim Levitskydf373fb2020-06-25 14:55:39 +02004243 qemu_opts_free(amend_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01004244 g_free(options);
4245
Max Reitz6f176b42013-09-03 10:09:50 +02004246 if (ret) {
4247 return 1;
4248 }
4249 return 0;
4250}
4251
Kevin Wolfb6133b82014-08-05 14:17:13 +02004252typedef struct BenchData {
4253 BlockBackend *blk;
4254 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004255 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004256 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02004257 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004258 int nrreq;
4259 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004260 int flush_interval;
4261 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004262 uint8_t *buf;
4263 QEMUIOVector *qiov;
4264
4265 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004266 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004267 uint64_t offset;
4268} BenchData;
4269
Kevin Wolf55d539c2016-06-03 13:59:41 +02004270static void bench_undrained_flush_cb(void *opaque, int ret)
4271{
4272 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02004273 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02004274 exit(EXIT_FAILURE);
4275 }
4276}
4277
Kevin Wolfb6133b82014-08-05 14:17:13 +02004278static void bench_cb(void *opaque, int ret)
4279{
4280 BenchData *b = opaque;
4281 BlockAIOCB *acb;
4282
4283 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02004284 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02004285 exit(EXIT_FAILURE);
4286 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02004287
4288 if (b->in_flush) {
4289 /* Just finished a flush with drained queue: Start next requests */
4290 assert(b->in_flight == 0);
4291 b->in_flush = false;
4292 } else if (b->in_flight > 0) {
4293 int remaining = b->n - b->in_flight;
4294
Kevin Wolfb6133b82014-08-05 14:17:13 +02004295 b->n--;
4296 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004297
4298 /* Time for flush? Drain queue if requested, then flush */
4299 if (b->flush_interval && remaining % b->flush_interval == 0) {
4300 if (!b->in_flight || !b->drain_on_flush) {
4301 BlockCompletionFunc *cb;
4302
4303 if (b->drain_on_flush) {
4304 b->in_flush = true;
4305 cb = bench_cb;
4306 } else {
4307 cb = bench_undrained_flush_cb;
4308 }
4309
4310 acb = blk_aio_flush(b->blk, cb, b);
4311 if (!acb) {
4312 error_report("Failed to issue flush request");
4313 exit(EXIT_FAILURE);
4314 }
4315 }
4316 if (b->drain_on_flush) {
4317 return;
4318 }
4319 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004320 }
4321
4322 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01004323 int64_t offset = b->offset;
4324 /* blk_aio_* might look for completed I/Os and kick bench_cb
4325 * again, so make sure this operation is counted by in_flight
4326 * and b->offset is ready for the next submission.
4327 */
4328 b->in_flight++;
4329 b->offset += b->step;
4330 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004331 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01004332 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004333 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01004334 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004335 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004336 if (!acb) {
4337 error_report("Failed to issue request");
4338 exit(EXIT_FAILURE);
4339 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004340 }
4341}
4342
4343static int img_bench(int argc, char **argv)
4344{
4345 int c, ret = 0;
4346 const char *fmt = NULL, *filename;
4347 bool quiet = false;
4348 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004349 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004350 int count = 75000;
4351 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02004352 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004353 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004354 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02004355 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004356 int flush_interval = 0;
4357 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004358 int64_t image_size;
4359 BlockBackend *blk = NULL;
4360 BenchData data = {};
4361 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02004362 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004363 struct timeval t1, t2;
4364 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08004365 bool force_share = false;
Fam Zheng79d46582018-01-16 14:08:58 +08004366 size_t buf_size;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004367
4368 for (;;) {
4369 static const struct option long_options[] = {
4370 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02004371 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02004372 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004373 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02004374 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08004375 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02004376 {0, 0, 0, 0}
4377 };
Aarushi Mehtacdd26772020-01-20 14:18:55 +00004378 c = getopt_long(argc, argv, ":hc:d:f:ni:o:qs:S:t:wU", long_options,
4379 NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004380 if (c == -1) {
4381 break;
4382 }
4383
4384 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004385 case ':':
4386 missing_argument(argv[optind - 1]);
4387 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004388 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004389 unrecognized_option(argv[optind - 1]);
4390 break;
4391 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02004392 help();
4393 break;
4394 case 'c':
4395 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004396 unsigned long res;
4397
4398 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02004399 error_report("Invalid request count specified");
4400 return 1;
4401 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004402 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004403 break;
4404 }
4405 case 'd':
4406 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004407 unsigned long res;
4408
4409 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02004410 error_report("Invalid queue depth specified");
4411 return 1;
4412 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004413 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004414 break;
4415 }
4416 case 'f':
4417 fmt = optarg;
4418 break;
4419 case 'n':
4420 flags |= BDRV_O_NATIVE_AIO;
4421 break;
Aarushi Mehtacdd26772020-01-20 14:18:55 +00004422 case 'i':
4423 ret = bdrv_parse_aio(optarg, &flags);
4424 if (ret < 0) {
4425 error_report("Invalid aio option: %s", optarg);
4426 ret = -1;
4427 goto out;
4428 }
4429 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02004430 case 'o':
4431 {
Eyal Moscovici43d589b2020-05-13 16:36:26 +03004432 offset = cvtnum("offset", optarg);
Markus Armbruster606caa02017-02-21 21:14:04 +01004433 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02004434 return 1;
4435 }
4436 break;
4437 }
4438 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004439 case 'q':
4440 quiet = true;
4441 break;
4442 case 's':
4443 {
4444 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004445
Eyal Moscovici43d589b2020-05-13 16:36:26 +03004446 sval = cvtnum_full("buffer size", optarg, 0, INT_MAX);
4447 if (sval < 0) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02004448 return 1;
4449 }
4450
4451 bufsize = sval;
4452 break;
4453 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02004454 case 'S':
4455 {
4456 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02004457
Eyal Moscovici43d589b2020-05-13 16:36:26 +03004458 sval = cvtnum_full("step_size", optarg, 0, INT_MAX);
4459 if (sval < 0) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02004460 return 1;
4461 }
4462
4463 step = sval;
4464 break;
4465 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004466 case 't':
4467 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
4468 if (ret < 0) {
4469 error_report("Invalid cache mode");
4470 ret = -1;
4471 goto out;
4472 }
4473 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004474 case 'w':
4475 flags |= BDRV_O_RDWR;
4476 is_write = true;
4477 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004478 case 'U':
4479 force_share = true;
4480 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004481 case OPTION_PATTERN:
4482 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004483 unsigned long res;
4484
4485 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004486 error_report("Invalid pattern byte specified");
4487 return 1;
4488 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004489 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004490 break;
4491 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02004492 case OPTION_FLUSH_INTERVAL:
4493 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004494 unsigned long res;
4495
4496 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004497 error_report("Invalid flush interval specified");
4498 return 1;
4499 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004500 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004501 break;
4502 }
4503 case OPTION_NO_DRAIN:
4504 drain_on_flush = false;
4505 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004506 case OPTION_IMAGE_OPTS:
4507 image_opts = true;
4508 break;
4509 }
4510 }
4511
4512 if (optind != argc - 1) {
4513 error_exit("Expecting one image file name");
4514 }
4515 filename = argv[argc - 1];
4516
Kevin Wolf55d539c2016-06-03 13:59:41 +02004517 if (!is_write && flush_interval) {
4518 error_report("--flush-interval is only available in write tests");
4519 ret = -1;
4520 goto out;
4521 }
4522 if (flush_interval && flush_interval < depth) {
4523 error_report("Flush interval can't be smaller than depth");
4524 ret = -1;
4525 goto out;
4526 }
4527
Fam Zheng335e9932017-05-03 00:35:39 +08004528 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4529 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004530 if (!blk) {
4531 ret = -1;
4532 goto out;
4533 }
4534
4535 image_size = blk_getlength(blk);
4536 if (image_size < 0) {
4537 ret = image_size;
4538 goto out;
4539 }
4540
4541 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004542 .blk = blk,
4543 .image_size = image_size,
4544 .bufsize = bufsize,
4545 .step = step ?: bufsize,
4546 .nrreq = depth,
4547 .n = count,
4548 .offset = offset,
4549 .write = is_write,
4550 .flush_interval = flush_interval,
4551 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004552 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004553 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004554 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004555 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004556 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004557 if (flush_interval) {
4558 printf("Sending flush every %d requests\n", flush_interval);
4559 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004560
Fam Zheng79d46582018-01-16 14:08:58 +08004561 buf_size = data.nrreq * data.bufsize;
4562 data.buf = blk_blockalign(blk, buf_size);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004563 memset(data.buf, pattern, data.nrreq * data.bufsize);
4564
Fam Zheng79d46582018-01-16 14:08:58 +08004565 blk_register_buf(blk, data.buf, buf_size);
4566
Kevin Wolfb6133b82014-08-05 14:17:13 +02004567 data.qiov = g_new(QEMUIOVector, data.nrreq);
4568 for (i = 0; i < data.nrreq; i++) {
4569 qemu_iovec_init(&data.qiov[i], 1);
4570 qemu_iovec_add(&data.qiov[i],
4571 data.buf + i * data.bufsize, data.bufsize);
4572 }
4573
4574 gettimeofday(&t1, NULL);
4575 bench_cb(&data, 0);
4576
4577 while (data.n > 0) {
4578 main_loop_wait(false);
4579 }
4580 gettimeofday(&t2, NULL);
4581
4582 printf("Run completed in %3.3f seconds.\n",
4583 (t2.tv_sec - t1.tv_sec)
4584 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4585
4586out:
Fam Zheng79d46582018-01-16 14:08:58 +08004587 if (data.buf) {
4588 blk_unregister_buf(blk, data.buf);
4589 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004590 qemu_vfree(data.buf);
4591 blk_unref(blk);
4592
4593 if (ret) {
4594 return 1;
4595 }
4596 return 0;
4597}
4598
Eric Blake3b51ab42020-05-12 20:16:45 -05004599enum ImgBitmapAct {
4600 BITMAP_ADD,
4601 BITMAP_REMOVE,
4602 BITMAP_CLEAR,
4603 BITMAP_ENABLE,
4604 BITMAP_DISABLE,
4605 BITMAP_MERGE,
4606};
4607typedef struct ImgBitmapAction {
4608 enum ImgBitmapAct act;
4609 const char *src; /* only used for merge */
4610 QSIMPLEQ_ENTRY(ImgBitmapAction) next;
4611} ImgBitmapAction;
4612
4613static int img_bitmap(int argc, char **argv)
4614{
4615 Error *err = NULL;
4616 int c, ret = 1;
4617 QemuOpts *opts = NULL;
4618 const char *fmt = NULL, *src_fmt = NULL, *src_filename = NULL;
4619 const char *filename, *bitmap;
4620 BlockBackend *blk = NULL, *src = NULL;
4621 BlockDriverState *bs = NULL, *src_bs = NULL;
4622 bool image_opts = false;
4623 int64_t granularity = 0;
4624 bool add = false, merge = false;
4625 QSIMPLEQ_HEAD(, ImgBitmapAction) actions;
4626 ImgBitmapAction *act, *act_next;
4627 const char *op;
4628
4629 QSIMPLEQ_INIT(&actions);
4630
4631 for (;;) {
4632 static const struct option long_options[] = {
4633 {"help", no_argument, 0, 'h'},
4634 {"object", required_argument, 0, OPTION_OBJECT},
4635 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4636 {"add", no_argument, 0, OPTION_ADD},
4637 {"remove", no_argument, 0, OPTION_REMOVE},
4638 {"clear", no_argument, 0, OPTION_CLEAR},
4639 {"enable", no_argument, 0, OPTION_ENABLE},
4640 {"disable", no_argument, 0, OPTION_DISABLE},
4641 {"merge", required_argument, 0, OPTION_MERGE},
4642 {"granularity", required_argument, 0, 'g'},
4643 {"source-file", required_argument, 0, 'b'},
4644 {"source-format", required_argument, 0, 'F'},
4645 {0, 0, 0, 0}
4646 };
4647 c = getopt_long(argc, argv, ":b:f:F:g:h", long_options, NULL);
4648 if (c == -1) {
4649 break;
4650 }
4651
4652 switch (c) {
4653 case ':':
4654 missing_argument(argv[optind - 1]);
4655 break;
4656 case '?':
4657 unrecognized_option(argv[optind - 1]);
4658 break;
4659 case 'h':
4660 help();
4661 break;
4662 case 'b':
4663 src_filename = optarg;
4664 break;
4665 case 'f':
4666 fmt = optarg;
4667 break;
4668 case 'F':
4669 src_fmt = optarg;
4670 break;
4671 case 'g':
4672 granularity = cvtnum("granularity", optarg);
4673 if (granularity < 0) {
4674 return 1;
4675 }
4676 break;
4677 case OPTION_ADD:
4678 act = g_new0(ImgBitmapAction, 1);
4679 act->act = BITMAP_ADD;
4680 QSIMPLEQ_INSERT_TAIL(&actions, act, next);
4681 add = true;
4682 break;
4683 case OPTION_REMOVE:
4684 act = g_new0(ImgBitmapAction, 1);
4685 act->act = BITMAP_REMOVE;
4686 QSIMPLEQ_INSERT_TAIL(&actions, act, next);
4687 break;
4688 case OPTION_CLEAR:
4689 act = g_new0(ImgBitmapAction, 1);
4690 act->act = BITMAP_CLEAR;
4691 QSIMPLEQ_INSERT_TAIL(&actions, act, next);
4692 break;
4693 case OPTION_ENABLE:
4694 act = g_new0(ImgBitmapAction, 1);
4695 act->act = BITMAP_ENABLE;
4696 QSIMPLEQ_INSERT_TAIL(&actions, act, next);
4697 break;
4698 case OPTION_DISABLE:
4699 act = g_new0(ImgBitmapAction, 1);
4700 act->act = BITMAP_DISABLE;
4701 QSIMPLEQ_INSERT_TAIL(&actions, act, next);
4702 break;
4703 case OPTION_MERGE:
4704 act = g_new0(ImgBitmapAction, 1);
4705 act->act = BITMAP_MERGE;
4706 act->src = optarg;
4707 QSIMPLEQ_INSERT_TAIL(&actions, act, next);
4708 merge = true;
4709 break;
4710 case OPTION_OBJECT:
Kevin Wolf99b1e642021-02-17 12:56:45 +01004711 user_creatable_process_cmdline(optarg);
Eric Blake3b51ab42020-05-12 20:16:45 -05004712 break;
4713 case OPTION_IMAGE_OPTS:
4714 image_opts = true;
4715 break;
4716 }
4717 }
4718
Eric Blake3b51ab42020-05-12 20:16:45 -05004719 if (QSIMPLEQ_EMPTY(&actions)) {
4720 error_report("Need at least one of --add, --remove, --clear, "
4721 "--enable, --disable, or --merge");
4722 goto out;
4723 }
4724
4725 if (granularity && !add) {
4726 error_report("granularity only supported with --add");
4727 goto out;
4728 }
4729 if (src_fmt && !src_filename) {
4730 error_report("-F only supported with -b");
4731 goto out;
4732 }
4733 if (src_filename && !merge) {
4734 error_report("Merge bitmap source file only supported with "
4735 "--merge");
4736 goto out;
4737 }
4738
4739 if (optind != argc - 2) {
4740 error_report("Expecting filename and bitmap name");
4741 goto out;
4742 }
4743
4744 filename = argv[optind];
4745 bitmap = argv[optind + 1];
4746
Eric Blake14f16bf2020-09-14 14:10:09 -05004747 /*
4748 * No need to open backing chains; we will be manipulating bitmaps
4749 * directly in this image without reference to image contents.
4750 */
4751 blk = img_open(image_opts, filename, fmt, BDRV_O_RDWR | BDRV_O_NO_BACKING,
4752 false, false, false);
Eric Blake3b51ab42020-05-12 20:16:45 -05004753 if (!blk) {
4754 goto out;
4755 }
4756 bs = blk_bs(blk);
4757 if (src_filename) {
Eric Blake14f16bf2020-09-14 14:10:09 -05004758 src = img_open(false, src_filename, src_fmt, BDRV_O_NO_BACKING,
4759 false, false, false);
Eric Blake3b51ab42020-05-12 20:16:45 -05004760 if (!src) {
4761 goto out;
4762 }
4763 src_bs = blk_bs(src);
4764 } else {
4765 src_bs = bs;
4766 }
4767
4768 QSIMPLEQ_FOREACH_SAFE(act, &actions, next, act_next) {
4769 switch (act->act) {
4770 case BITMAP_ADD:
4771 qmp_block_dirty_bitmap_add(bs->node_name, bitmap,
4772 !!granularity, granularity, true, true,
4773 false, false, &err);
4774 op = "add";
4775 break;
4776 case BITMAP_REMOVE:
4777 qmp_block_dirty_bitmap_remove(bs->node_name, bitmap, &err);
4778 op = "remove";
4779 break;
4780 case BITMAP_CLEAR:
4781 qmp_block_dirty_bitmap_clear(bs->node_name, bitmap, &err);
4782 op = "clear";
4783 break;
4784 case BITMAP_ENABLE:
4785 qmp_block_dirty_bitmap_enable(bs->node_name, bitmap, &err);
4786 op = "enable";
4787 break;
4788 case BITMAP_DISABLE:
4789 qmp_block_dirty_bitmap_disable(bs->node_name, bitmap, &err);
4790 op = "disable";
4791 break;
Eric Blake6c729dd2020-05-21 14:21:35 -05004792 case BITMAP_MERGE:
4793 do_dirty_bitmap_merge(bs->node_name, bitmap, src_bs->node_name,
4794 act->src, &err);
Eric Blake3b51ab42020-05-12 20:16:45 -05004795 op = "merge";
4796 break;
Eric Blake3b51ab42020-05-12 20:16:45 -05004797 default:
4798 g_assert_not_reached();
4799 }
4800
4801 if (err) {
4802 error_reportf_err(err, "Operation %s on bitmap %s failed: ",
4803 op, bitmap);
4804 goto out;
4805 }
4806 g_free(act);
4807 }
4808
4809 ret = 0;
4810
4811 out:
4812 blk_unref(src);
4813 blk_unref(blk);
4814 qemu_opts_del(opts);
4815 return ret;
4816}
4817
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004818#define C_BS 01
4819#define C_COUNT 02
4820#define C_IF 04
4821#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004822#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004823
4824struct DdInfo {
4825 unsigned int flags;
4826 int64_t count;
4827};
4828
4829struct DdIo {
4830 int bsz; /* Block size */
4831 char *filename;
4832 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004833 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004834};
4835
4836struct DdOpts {
4837 const char *name;
4838 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4839 unsigned int flag;
4840};
4841
4842static int img_dd_bs(const char *arg,
4843 struct DdIo *in, struct DdIo *out,
4844 struct DdInfo *dd)
4845{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004846 int64_t res;
4847
Eyal Moscovici43d589b2020-05-13 16:36:26 +03004848 res = cvtnum_full("bs", arg, 1, INT_MAX);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004849
Eyal Moscovici43d589b2020-05-13 16:36:26 +03004850 if (res < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004851 return 1;
4852 }
4853 in->bsz = out->bsz = res;
4854
4855 return 0;
4856}
4857
4858static int img_dd_count(const char *arg,
4859 struct DdIo *in, struct DdIo *out,
4860 struct DdInfo *dd)
4861{
Eyal Moscovici43d589b2020-05-13 16:36:26 +03004862 dd->count = cvtnum("count", arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004863
Markus Armbruster606caa02017-02-21 21:14:04 +01004864 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004865 return 1;
4866 }
4867
4868 return 0;
4869}
4870
4871static int img_dd_if(const char *arg,
4872 struct DdIo *in, struct DdIo *out,
4873 struct DdInfo *dd)
4874{
4875 in->filename = g_strdup(arg);
4876
4877 return 0;
4878}
4879
4880static int img_dd_of(const char *arg,
4881 struct DdIo *in, struct DdIo *out,
4882 struct DdInfo *dd)
4883{
4884 out->filename = g_strdup(arg);
4885
4886 return 0;
4887}
4888
Reda Sallahif7c15532016-08-10 16:16:09 +02004889static int img_dd_skip(const char *arg,
4890 struct DdIo *in, struct DdIo *out,
4891 struct DdInfo *dd)
4892{
Eyal Moscovici43d589b2020-05-13 16:36:26 +03004893 in->offset = cvtnum("skip", arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004894
Markus Armbruster606caa02017-02-21 21:14:04 +01004895 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004896 return 1;
4897 }
4898
4899 return 0;
4900}
4901
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004902static int img_dd(int argc, char **argv)
4903{
4904 int ret = 0;
4905 char *arg = NULL;
4906 char *tmp;
4907 BlockDriver *drv = NULL, *proto_drv = NULL;
4908 BlockBackend *blk1 = NULL, *blk2 = NULL;
4909 QemuOpts *opts = NULL;
4910 QemuOptsList *create_opts = NULL;
4911 Error *local_err = NULL;
4912 bool image_opts = false;
4913 int c, i;
4914 const char *out_fmt = "raw";
4915 const char *fmt = NULL;
4916 int64_t size = 0;
4917 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004918 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004919 struct DdInfo dd = {
4920 .flags = 0,
4921 .count = 0,
4922 };
4923 struct DdIo in = {
4924 .bsz = 512, /* Block size is by default 512 bytes */
4925 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004926 .buf = NULL,
4927 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004928 };
4929 struct DdIo out = {
4930 .bsz = 512,
4931 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004932 .buf = NULL,
4933 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004934 };
4935
4936 const struct DdOpts options[] = {
4937 { "bs", img_dd_bs, C_BS },
4938 { "count", img_dd_count, C_COUNT },
4939 { "if", img_dd_if, C_IF },
4940 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004941 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004942 { NULL, NULL, 0 }
4943 };
4944 const struct option long_options[] = {
4945 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004946 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004947 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004948 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004949 { 0, 0, 0, 0 }
4950 };
4951
Fam Zheng335e9932017-05-03 00:35:39 +08004952 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004953 if (c == EOF) {
4954 break;
4955 }
4956 switch (c) {
4957 case 'O':
4958 out_fmt = optarg;
4959 break;
4960 case 'f':
4961 fmt = optarg;
4962 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004963 case ':':
4964 missing_argument(argv[optind - 1]);
4965 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004966 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004967 unrecognized_option(argv[optind - 1]);
4968 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004969 case 'h':
4970 help();
4971 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004972 case 'U':
4973 force_share = true;
4974 break;
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004975 case OPTION_OBJECT:
Kevin Wolf99b1e642021-02-17 12:56:45 +01004976 user_creatable_process_cmdline(optarg);
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004977 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004978 case OPTION_IMAGE_OPTS:
4979 image_opts = true;
4980 break;
4981 }
4982 }
4983
4984 for (i = optind; i < argc; i++) {
4985 int j;
4986 arg = g_strdup(argv[i]);
4987
4988 tmp = strchr(arg, '=');
4989 if (tmp == NULL) {
4990 error_report("unrecognized operand %s", arg);
4991 ret = -1;
4992 goto out;
4993 }
4994
4995 *tmp++ = '\0';
4996
4997 for (j = 0; options[j].name != NULL; j++) {
4998 if (!strcmp(arg, options[j].name)) {
4999 break;
5000 }
5001 }
5002 if (options[j].name == NULL) {
5003 error_report("unrecognized operand %s", arg);
5004 ret = -1;
5005 goto out;
5006 }
5007
5008 if (options[j].f(tmp, &in, &out, &dd) != 0) {
5009 ret = -1;
5010 goto out;
5011 }
5012 dd.flags |= options[j].flag;
5013 g_free(arg);
5014 arg = NULL;
5015 }
5016
5017 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
5018 error_report("Must specify both input and output files");
5019 ret = -1;
5020 goto out;
5021 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01005022
Fam Zheng335e9932017-05-03 00:35:39 +08005023 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
5024 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02005025
5026 if (!blk1) {
5027 ret = -1;
5028 goto out;
5029 }
5030
5031 drv = bdrv_find_format(out_fmt);
5032 if (!drv) {
5033 error_report("Unknown file format");
5034 ret = -1;
5035 goto out;
5036 }
5037 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
5038
5039 if (!proto_drv) {
5040 error_report_err(local_err);
5041 ret = -1;
5042 goto out;
5043 }
5044 if (!drv->create_opts) {
5045 error_report("Format driver '%s' does not support image creation",
5046 drv->format_name);
5047 ret = -1;
5048 goto out;
5049 }
5050 if (!proto_drv->create_opts) {
5051 error_report("Protocol driver '%s' does not support image creation",
5052 proto_drv->format_name);
5053 ret = -1;
5054 goto out;
5055 }
5056 create_opts = qemu_opts_append(create_opts, drv->create_opts);
5057 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
5058
5059 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
5060
5061 size = blk_getlength(blk1);
5062 if (size < 0) {
5063 error_report("Failed to get size for '%s'", in.filename);
5064 ret = -1;
5065 goto out;
5066 }
5067
5068 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
5069 dd.count * in.bsz < size) {
5070 size = dd.count * in.bsz;
5071 }
5072
Reda Sallahif7c15532016-08-10 16:16:09 +02005073 /* Overflow means the specified offset is beyond input image's size */
5074 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
5075 size < in.bsz * in.offset)) {
5076 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
5077 } else {
5078 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
5079 size - in.bsz * in.offset, &error_abort);
5080 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02005081
5082 ret = bdrv_create(drv, out.filename, opts, &local_err);
5083 if (ret < 0) {
5084 error_reportf_err(local_err,
5085 "%s: error while creating output image: ",
5086 out.filename);
5087 ret = -1;
5088 goto out;
5089 }
5090
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01005091 /* TODO, we can't honour --image-opts for the target,
5092 * since it needs to be given in a format compatible
5093 * with the bdrv_create() call above which does not
5094 * support image-opts style.
5095 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01005096 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01005097 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02005098
5099 if (!blk2) {
5100 ret = -1;
5101 goto out;
5102 }
5103
Reda Sallahif7c15532016-08-10 16:16:09 +02005104 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
5105 size < in.offset * in.bsz)) {
5106 /* We give a warning if the skip option is bigger than the input
5107 * size and create an empty output disk image (i.e. like dd(1)).
5108 */
5109 error_report("%s: cannot skip to specified offset", in.filename);
5110 in_pos = size;
5111 } else {
5112 in_pos = in.offset * in.bsz;
5113 }
5114
Reda Sallahi86ce1f62016-08-10 04:43:12 +02005115 in.buf = g_new(uint8_t, in.bsz);
5116
Reda Sallahif7c15532016-08-10 16:16:09 +02005117 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02005118 int in_ret, out_ret;
5119
5120 if (in_pos + in.bsz > size) {
5121 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
5122 } else {
5123 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
5124 }
5125 if (in_ret < 0) {
5126 error_report("error while reading from input image file: %s",
5127 strerror(-in_ret));
5128 ret = -1;
5129 goto out;
5130 }
5131 in_pos += in_ret;
5132
5133 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
5134
5135 if (out_ret < 0) {
5136 error_report("error while writing to output image file: %s",
5137 strerror(-out_ret));
5138 ret = -1;
5139 goto out;
5140 }
5141 out_pos += out_ret;
5142 }
5143
5144out:
5145 g_free(arg);
5146 qemu_opts_del(opts);
5147 qemu_opts_free(create_opts);
5148 blk_unref(blk1);
5149 blk_unref(blk2);
5150 g_free(in.filename);
5151 g_free(out.filename);
5152 g_free(in.buf);
5153 g_free(out.buf);
5154
5155 if (ret) {
5156 return 1;
5157 }
5158 return 0;
5159}
5160
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01005161static void dump_json_block_measure_info(BlockMeasureInfo *info)
5162{
Markus Armbrustereab3a462020-12-11 18:11:37 +01005163 GString *str;
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01005164 QObject *obj;
5165 Visitor *v = qobject_output_visitor_new(&obj);
5166
5167 visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
5168 visit_complete(v, &obj);
Markus Armbruster6589f452020-12-11 18:11:35 +01005169 str = qobject_to_json_pretty(obj, true);
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01005170 assert(str != NULL);
Markus Armbrustereab3a462020-12-11 18:11:37 +01005171 printf("%s\n", str->str);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02005172 qobject_unref(obj);
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01005173 visit_free(v);
Markus Armbrustereab3a462020-12-11 18:11:37 +01005174 g_string_free(str, true);
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01005175}
5176
5177static int img_measure(int argc, char **argv)
5178{
5179 static const struct option long_options[] = {
5180 {"help", no_argument, 0, 'h'},
5181 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
5182 {"object", required_argument, 0, OPTION_OBJECT},
5183 {"output", required_argument, 0, OPTION_OUTPUT},
5184 {"size", required_argument, 0, OPTION_SIZE},
5185 {"force-share", no_argument, 0, 'U'},
5186 {0, 0, 0, 0}
5187 };
5188 OutputFormat output_format = OFORMAT_HUMAN;
5189 BlockBackend *in_blk = NULL;
5190 BlockDriver *drv;
5191 const char *filename = NULL;
5192 const char *fmt = NULL;
5193 const char *out_fmt = "raw";
5194 char *options = NULL;
5195 char *snapshot_name = NULL;
5196 bool force_share = false;
5197 QemuOpts *opts = NULL;
5198 QemuOpts *object_opts = NULL;
5199 QemuOpts *sn_opts = NULL;
5200 QemuOptsList *create_opts = NULL;
5201 bool image_opts = false;
5202 uint64_t img_size = UINT64_MAX;
5203 BlockMeasureInfo *info = NULL;
5204 Error *local_err = NULL;
5205 int ret = 1;
5206 int c;
5207
5208 while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
5209 long_options, NULL)) != -1) {
5210 switch (c) {
5211 case '?':
5212 case 'h':
5213 help();
5214 break;
5215 case 'f':
5216 fmt = optarg;
5217 break;
5218 case 'O':
5219 out_fmt = optarg;
5220 break;
5221 case 'o':
Markus Armbruster6d2b5cb2020-04-15 09:49:25 +02005222 if (accumulate_options(&options, optarg) < 0) {
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01005223 goto out;
5224 }
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01005225 break;
5226 case 'l':
5227 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
5228 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
5229 optarg, false);
5230 if (!sn_opts) {
5231 error_report("Failed in parsing snapshot param '%s'",
5232 optarg);
5233 goto out;
5234 }
5235 } else {
5236 snapshot_name = optarg;
5237 }
5238 break;
5239 case 'U':
5240 force_share = true;
5241 break;
5242 case OPTION_OBJECT:
Kevin Wolf99b1e642021-02-17 12:56:45 +01005243 user_creatable_process_cmdline(optarg);
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01005244 break;
5245 case OPTION_IMAGE_OPTS:
5246 image_opts = true;
5247 break;
5248 case OPTION_OUTPUT:
5249 if (!strcmp(optarg, "json")) {
5250 output_format = OFORMAT_JSON;
5251 } else if (!strcmp(optarg, "human")) {
5252 output_format = OFORMAT_HUMAN;
5253 } else {
5254 error_report("--output must be used with human or json "
5255 "as argument.");
5256 goto out;
5257 }
5258 break;
5259 case OPTION_SIZE:
5260 {
5261 int64_t sval;
5262
Eyal Moscovici43d589b2020-05-13 16:36:26 +03005263 sval = cvtnum("image size", optarg);
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01005264 if (sval < 0) {
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01005265 goto out;
5266 }
5267 img_size = (uint64_t)sval;
5268 }
5269 break;
5270 }
5271 }
5272
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01005273 if (argc - optind > 1) {
5274 error_report("At most one filename argument is allowed.");
5275 goto out;
5276 } else if (argc - optind == 1) {
5277 filename = argv[optind];
5278 }
5279
Stefan Hajnoczic3673dc2020-02-21 11:25:21 +00005280 if (!filename && (image_opts || fmt || snapshot_name || sn_opts)) {
5281 error_report("--image-opts, -f, and -l require a filename argument.");
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01005282 goto out;
5283 }
5284 if (filename && img_size != UINT64_MAX) {
5285 error_report("--size N cannot be used together with a filename.");
5286 goto out;
5287 }
5288 if (!filename && img_size == UINT64_MAX) {
5289 error_report("Either --size N or one filename must be specified.");
5290 goto out;
5291 }
5292
5293 if (filename) {
5294 in_blk = img_open(image_opts, filename, fmt, 0,
5295 false, false, force_share);
5296 if (!in_blk) {
5297 goto out;
5298 }
5299
5300 if (sn_opts) {
5301 bdrv_snapshot_load_tmp(blk_bs(in_blk),
5302 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
5303 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
5304 &local_err);
5305 } else if (snapshot_name != NULL) {
5306 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
5307 snapshot_name, &local_err);
5308 }
5309 if (local_err) {
5310 error_reportf_err(local_err, "Failed to load snapshot: ");
5311 goto out;
5312 }
5313 }
5314
5315 drv = bdrv_find_format(out_fmt);
5316 if (!drv) {
5317 error_report("Unknown file format '%s'", out_fmt);
5318 goto out;
5319 }
5320 if (!drv->create_opts) {
5321 error_report("Format driver '%s' does not support image creation",
5322 drv->format_name);
5323 goto out;
5324 }
5325
5326 create_opts = qemu_opts_append(create_opts, drv->create_opts);
5327 create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
5328 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
5329 if (options) {
Markus Armbruster235e59c2020-07-07 18:05:42 +02005330 if (!qemu_opts_do_parse(opts, options, NULL, &local_err)) {
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01005331 error_report_err(local_err);
5332 error_report("Invalid options for file format '%s'", out_fmt);
5333 goto out;
5334 }
5335 }
5336 if (img_size != UINT64_MAX) {
5337 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
5338 }
5339
5340 info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
5341 if (local_err) {
5342 error_report_err(local_err);
5343 goto out;
5344 }
5345
5346 if (output_format == OFORMAT_HUMAN) {
5347 printf("required size: %" PRIu64 "\n", info->required);
5348 printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
Eric Blake5d72c682020-05-21 14:21:34 -05005349 if (info->has_bitmaps) {
5350 printf("bitmaps size: %" PRIu64 "\n", info->bitmaps);
5351 }
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01005352 } else {
5353 dump_json_block_measure_info(info);
5354 }
5355
5356 ret = 0;
5357
5358out:
5359 qapi_free_BlockMeasureInfo(info);
5360 qemu_opts_del(object_opts);
5361 qemu_opts_del(opts);
5362 qemu_opts_del(sn_opts);
5363 qemu_opts_free(create_opts);
5364 g_free(options);
5365 blk_unref(in_blk);
5366 return ret;
5367}
Kevin Wolfb6133b82014-08-05 14:17:13 +02005368
Anthony Liguoric227f092009-10-01 16:12:16 -05005369static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01005370#define DEF(option, callback, arg_string) \
5371 { option, callback },
5372#include "qemu-img-cmds.h"
5373#undef DEF
Stuart Brady153859b2009-06-07 00:42:17 +01005374 { NULL, NULL, },
5375};
5376
bellardea2384d2004-08-01 21:59:26 +00005377int main(int argc, char **argv)
5378{
Anthony Liguoric227f092009-10-01 16:12:16 -05005379 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01005380 const char *cmdname;
Jeff Cody7db16892014-04-25 17:02:32 -04005381 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04005382 static const struct option long_options[] = {
5383 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03005384 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03005385 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04005386 {0, 0, 0, 0}
5387 };
bellardea2384d2004-08-01 21:59:26 +00005388
MORITA Kazutaka526eda12013-07-23 17:30:11 +09005389#ifdef CONFIG_POSIX
5390 signal(SIGPIPE, SIG_IGN);
5391#endif
5392
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +01005393 socket_init();
Christophe Fergeauf5852ef2019-01-31 17:46:14 +01005394 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01005395 module_call_init(MODULE_INIT_TRACE);
Fam Zheng10f5bff2014-02-10 14:48:51 +08005396 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01005397
Markus Armbrusterf9734d52021-07-20 14:53:53 +02005398 qemu_init_main_loop(&error_fatal);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03005399
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03005400 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01005401
Daniel P. Berrange064097d2016-02-10 18:41:01 +00005402 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00005403 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08005404 if (argc < 2) {
5405 error_exit("Not enough arguments");
5406 }
Stuart Brady153859b2009-06-07 00:42:17 +01005407
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00005408 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03005409 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00005410
Stefan Hajnoczic9192972017-03-17 18:45:41 +08005411 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03005412 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08005413 case ':':
5414 missing_argument(argv[optind - 1]);
5415 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08005416 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08005417 unrecognized_option(argv[optind - 1]);
5418 return 0;
5419 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03005420 help();
5421 return 0;
5422 case 'V':
5423 printf(QEMU_IMG_VERSION);
5424 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03005425 case 'T':
Paolo Bonzini92eecff2020-11-02 06:58:41 -05005426 trace_opt_parse(optarg);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03005427 break;
Stuart Brady153859b2009-06-07 00:42:17 +01005428 }
bellardea2384d2004-08-01 21:59:26 +00005429 }
Stuart Brady153859b2009-06-07 00:42:17 +01005430
Denis V. Lunev10985132016-06-17 17:44:13 +03005431 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04005432
Denis V. Lunev10985132016-06-17 17:44:13 +03005433 /* reset getopt_long scanning */
5434 argc -= optind;
5435 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04005436 return 0;
5437 }
Denis V. Lunev10985132016-06-17 17:44:13 +03005438 argv += optind;
Richard W.M. Jonesd339d762019-01-18 10:11:14 +00005439 qemu_reset_optind();
Denis V. Lunev10985132016-06-17 17:44:13 +03005440
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03005441 if (!trace_init_backends()) {
5442 exit(1);
5443 }
Paolo Bonzini92eecff2020-11-02 06:58:41 -05005444 trace_init_file();
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03005445 qemu_set_log(LOG_TRACE);
5446
Denis V. Lunev10985132016-06-17 17:44:13 +03005447 /* find the command */
5448 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
5449 if (!strcmp(cmdname, cmd->name)) {
5450 return cmd->handler(argc, argv);
5451 }
5452 }
Jeff Cody7db16892014-04-25 17:02:32 -04005453
Stuart Brady153859b2009-06-07 00:42:17 +01005454 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08005455 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00005456}