blob: f4074ebf75290eabb07f49c6c9ac70779f18b6ab [file] [log] [blame]
bellardea2384d2004-08-01 21:59:26 +00001/*
bellardfb43f4d2006-08-07 21:34:46 +00002 * QEMU disk image utility
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardea2384d2004-08-01 21:59:26 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Markus Armbruster452fcdb2018-02-01 12:18:39 +010024
Peter Maydell80c71a22016-01-18 18:01:42 +000025#include "qemu/osdep.h"
Eric Blakec2a3d7d2017-07-21 08:50:47 -050026#include <getopt.h>
27
Fam Zheng67a1de02016-06-01 17:44:21 +080028#include "qemu-version.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010029#include "qapi/error.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010030#include "qapi/qapi-visit-block-core.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010031#include "qapi/qobject-output-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010032#include "qapi/qmp/qjson.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010033#include "qapi/qmp/qdict.h"
Markus Armbrusterfc81fa12018-02-01 12:18:40 +010034#include "qapi/qmp/qstring.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020035#include "qemu/cutils.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000036#include "qemu/config-file.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010037#include "qemu/option.h"
38#include "qemu/error-report.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030039#include "qemu/log.h"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000040#include "qom/object_interfaces.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010041#include "sysemu/sysemu.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020042#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010043#include "block/block_int.h"
Max Reitzd4a32382014-10-24 15:57:37 +020044#include "block/blockjob.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080045#include "block/qapi.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010046#include "crypto/init.h"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +030047#include "trace/control.h"
bellarde8445332006-06-14 15:32:10 +000048
Thomas Huth7e563bf2018-02-15 12:06:47 +010049#define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \
Thomas Huth0781dd62016-10-05 11:54:44 +020050 "\n" QEMU_COPYRIGHT "\n"
Jeff Cody5f6979c2014-04-28 14:37:18 -040051
Anthony Liguoric227f092009-10-01 16:12:16 -050052typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010053 const char *name;
54 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050055} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010056
Federico Simoncelli8599ea42013-01-28 06:59:47 -050057enum {
58 OPTION_OUTPUT = 256,
59 OPTION_BACKING_CHAIN = 257,
Daniel P. Berrange3babeb12016-02-17 10:10:17 +000060 OPTION_OBJECT = 258,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +000061 OPTION_IMAGE_OPTS = 259,
Kevin Wolfb6495fa2015-07-10 18:09:18 +020062 OPTION_PATTERN = 260,
Kevin Wolf55d539c2016-06-03 13:59:41 +020063 OPTION_FLUSH_INTERVAL = 261,
64 OPTION_NO_DRAIN = 262,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +010065 OPTION_TARGET_IMAGE_OPTS = 263,
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +010066 OPTION_SIZE = 264,
Max Reitzdc5f6902017-06-13 22:20:55 +020067 OPTION_PREALLOCATION = 265,
Pavel Butsykin4ffca892017-09-18 15:42:27 +030068 OPTION_SHRINK = 266,
Federico Simoncelli8599ea42013-01-28 06:59:47 -050069};
70
71typedef enum OutputFormat {
72 OFORMAT_JSON,
73 OFORMAT_HUMAN,
74} OutputFormat;
75
Kevin Wolfe6996142016-03-15 13:03:11 +010076/* Default to cache=writeback as data integrity is not important for qemu-img */
Federico Simoncelli661a0f72011-06-20 12:48:19 -040077#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000078
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010079static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000080{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010081 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000082}
83
Fam Zhengac1307a2014-04-22 13:36:11 +080084static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
85{
86 va_list ap;
87
88 error_printf("qemu-img: ");
89
90 va_start(ap, fmt);
91 error_vprintf(fmt, ap);
92 va_end(ap);
93
94 error_printf("\nTry 'qemu-img --help' for more information\n");
95 exit(EXIT_FAILURE);
96}
97
Stefan Hajnoczic9192972017-03-17 18:45:41 +080098static void QEMU_NORETURN missing_argument(const char *option)
99{
100 error_exit("missing argument for option '%s'", option);
101}
102
103static void QEMU_NORETURN unrecognized_option(const char *option)
104{
105 error_exit("unrecognized option '%s'", option);
106}
107
blueswir1d2c639d2009-01-24 18:19:25 +0000108/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +0800109static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +0000110{
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100111 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -0400112 QEMU_IMG_VERSION
Denis V. Lunev10985132016-06-17 17:44:13 +0300113 "usage: qemu-img [standard options] command [command options]\n"
malc3f020d72010-02-08 12:04:56 +0300114 "QEMU disk image utility\n"
115 "\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300116 " '-h', '--help' display this help and exit\n"
117 " '-V', '--version' output version information and exit\n"
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +0300118 " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
119 " specify tracing options\n"
Denis V. Lunev10985132016-06-17 17:44:13 +0300120 "\n"
malc3f020d72010-02-08 12:04:56 +0300121 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100122#define DEF(option, callback, arg_string) \
123 " " arg_string "\n"
124#include "qemu-img-cmds.h"
125#undef DEF
malc3f020d72010-02-08 12:04:56 +0300126 "\n"
127 "Command parameters:\n"
128 " 'filename' is a disk image filename\n"
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000129 " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
130 " manual page for a description of the object properties. The most common\n"
131 " object type is a 'secret', which is used to supply passwords and/or\n"
132 " encryption keys.\n"
malc3f020d72010-02-08 12:04:56 +0300133 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400134 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800135 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
136 " 'directsync' and 'unsafe' (default for convert)\n"
Stefan Hajnoczibb87fdf2014-09-02 11:01:02 +0100137 " 'src_cache' is the cache mode used to read input disk images, the valid\n"
138 " options are the same as for the 'cache' option\n"
malc3f020d72010-02-08 12:04:56 +0300139 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200140 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
141 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
142 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300143 " 'output_filename' is the destination disk image filename\n"
144 " 'output_fmt' is the destination format\n"
145 " 'options' is a comma separated list of format specific options in a\n"
146 " name=value format. Use -o ? for an overview of the options supported by the\n"
147 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800148 " 'snapshot_param' is param used for internal snapshot, format\n"
149 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
150 " '[ID_OR_NAME]'\n"
malc3f020d72010-02-08 12:04:56 +0300151 " '-c' indicates that target image must be compressed (qcow format only)\n"
John Snow6e6e55f2017-07-17 20:34:22 -0400152 " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
153 " new backing file match exactly. The image doesn't need a working\n"
154 " backing file before rebasing in this case (useful for renaming the\n"
155 " backing file). For image creation, allow creating without attempting\n"
156 " to open the backing file.\n"
malc3f020d72010-02-08 12:04:56 +0300157 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200158 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100159 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200160 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
161 " contain only zeros for qemu-img to create a sparse image during\n"
162 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
163 " unallocated or zero sectors, and the destination image will always be\n"
164 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200165 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100166 " '-n' skips the target volume creation (useful if the volume is created\n"
167 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300168 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200169 "Parameters to check subcommand:\n"
170 " '-r' tries to repair any inconsistencies that are found during the check.\n"
171 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
172 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200173 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200174 "\n"
Peter Lieven2d9187b2017-02-28 13:40:07 +0100175 "Parameters to convert subcommand:\n"
176 " '-m' specifies how many coroutines work in parallel during the convert\n"
177 " process (defaults to 8)\n"
178 " '-W' allow to write to the target out of order rather than sequential\n"
179 "\n"
malc3f020d72010-02-08 12:04:56 +0300180 "Parameters to snapshot subcommand:\n"
181 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
182 " '-a' applies a snapshot (revert disk to saved state)\n"
183 " '-c' creates a snapshot\n"
184 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100185 " '-l' lists all snapshots in the given image\n"
186 "\n"
187 "Parameters to compare subcommand:\n"
188 " '-f' first image format\n"
189 " '-F' second image format\n"
Reda Sallahi86ce1f62016-08-10 04:43:12 +0200190 " '-s' run in Strict mode - fail on different image size or sector allocation\n"
191 "\n"
192 "Parameters to dd subcommand:\n"
193 " 'bs=BYTES' read and write up to BYTES bytes at a time "
194 "(default: 512)\n"
195 " 'count=N' copy only N input blocks\n"
196 " 'if=FILE' read from FILE\n"
Reda Sallahif7c15532016-08-10 16:16:09 +0200197 " 'of=FILE' write to FILE\n"
198 " 'skip=N' skip N bs-sized blocks at the start of input\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100199
200 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100201 bdrv_iterate_format(format_print, NULL);
Eric Blakef5048cb2017-08-03 11:33:53 -0500202 printf("\n\n" QEMU_HELP_BOTTOM "\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800203 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000204}
205
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000206static QemuOptsList qemu_object_opts = {
207 .name = "object",
208 .implied_opt_name = "qom-type",
209 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
210 .desc = {
211 { }
212 },
213};
214
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000215static QemuOptsList qemu_source_opts = {
216 .name = "source",
217 .implied_opt_name = "file",
218 .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
219 .desc = {
220 { }
221 },
222};
223
Stefan Weil7c30f652013-06-16 17:01:05 +0200224static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100225{
226 int ret = 0;
227 if (!quiet) {
228 va_list args;
229 va_start(args, fmt);
230 ret = vprintf(fmt, args);
231 va_end(args);
232 }
233 return ret;
234}
235
bellardea2384d2004-08-01 21:59:26 +0000236
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100237static int print_block_option_help(const char *filename, const char *fmt)
238{
239 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800240 QemuOptsList *create_opts = NULL;
Max Reitzb65a5e12015-02-05 13:58:12 -0500241 Error *local_err = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100242
243 /* Find driver and parse its options */
244 drv = bdrv_find_format(fmt);
245 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100246 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100247 return 1;
248 }
249
Max Reitzd402b6a2018-05-09 23:00:21 +0200250 if (!drv->create_opts) {
251 error_report("Format driver '%s' does not support image creation", fmt);
252 return 1;
253 }
254
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800255 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100256 if (filename) {
Max Reitzb65a5e12015-02-05 13:58:12 -0500257 proto_drv = bdrv_find_protocol(filename, true, &local_err);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100258 if (!proto_drv) {
Markus Armbruster2867ce42015-03-12 16:08:02 +0100259 error_report_err(local_err);
Chunyan Liu83d05212014-06-05 17:20:51 +0800260 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100261 return 1;
262 }
Max Reitzd402b6a2018-05-09 23:00:21 +0200263 if (!proto_drv->create_opts) {
264 error_report("Protocal driver '%s' does not support image creation",
265 proto_drv->format_name);
266 return 1;
267 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800268 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100269 }
270
Max Reitz7f3fb002018-05-09 23:00:19 +0200271 printf("Supported options:\n");
Chunyan Liu83d05212014-06-05 17:20:51 +0800272 qemu_opts_print_help(create_opts);
273 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100274 return 0;
275}
276
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000277
Max Reitzefaa7c42016-03-16 19:54:38 +0100278static BlockBackend *img_open_opts(const char *optstr,
Kevin Wolfce099542016-03-15 13:01:04 +0100279 QemuOpts *opts, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800280 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000281{
282 QDict *options;
283 Error *local_err = NULL;
284 BlockBackend *blk;
285 options = qemu_opts_to_qdict(opts, NULL);
Fam Zheng335e9932017-05-03 00:35:39 +0800286 if (force_share) {
287 if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
Max Reitz4615f872018-05-02 22:20:50 +0200288 && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) {
Fam Zheng335e9932017-05-03 00:35:39 +0800289 error_report("--force-share/-U conflicts with image options");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200290 qobject_unref(options);
Fam Zheng335e9932017-05-03 00:35:39 +0800291 return NULL;
292 }
Max Reitz4615f872018-05-02 22:20:50 +0200293 qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on");
Fam Zheng335e9932017-05-03 00:35:39 +0800294 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100295 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000296 if (!blk) {
Daniel P. Berrange143605a2016-04-06 10:16:18 +0100297 error_reportf_err(local_err, "Could not open '%s': ", optstr);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000298 return NULL;
299 }
Kevin Wolfce099542016-03-15 13:01:04 +0100300 blk_set_enable_write_cache(blk, !writethrough);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000301
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000302 return blk;
303}
304
Max Reitzefaa7c42016-03-16 19:54:38 +0100305static BlockBackend *img_open_file(const char *filename,
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100306 QDict *options,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000307 const char *fmt, int flags,
Fam Zheng335e9932017-05-03 00:35:39 +0800308 bool writethrough, bool quiet,
309 bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000310{
311 BlockBackend *blk;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200312 Error *local_err = NULL;
Kevin Wolfad717132010-12-16 15:37:41 +0100313
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100314 if (!options) {
315 options = qdict_new();
316 }
bellard75c23802004-08-27 21:28:58 +0000317 if (fmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500318 qdict_put_str(options, "driver", fmt);
bellard75c23802004-08-27 21:28:58 +0000319 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100320
Fam Zheng335e9932017-05-03 00:35:39 +0800321 if (force_share) {
Eric Blake579cf1d2017-05-15 14:54:39 -0500322 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +0800323 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100324 blk = blk_new_open(filename, NULL, options, flags, &local_err);
Max Reitz5bd31322015-02-05 13:58:16 -0500325 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100326 error_reportf_err(local_err, "Could not open '%s': ", filename);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000327 return NULL;
bellard75c23802004-08-27 21:28:58 +0000328 }
Kevin Wolfce099542016-03-15 13:01:04 +0100329 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100330
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200331 return blk;
bellard75c23802004-08-27 21:28:58 +0000332}
333
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000334
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100335static int img_add_key_secrets(void *opaque,
336 const char *name, const char *value,
337 Error **errp)
338{
339 QDict *options = opaque;
340
341 if (g_str_has_suffix(name, "key-secret")) {
Eric Blake187f47e2017-06-24 12:10:07 -0600342 qdict_put_str(options, name, value);
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100343 }
344
345 return 0;
346}
347
348static BlockBackend *img_open_new_file(const char *filename,
349 QemuOpts *create_opts,
350 const char *fmt, int flags,
351 bool writethrough, bool quiet,
352 bool force_share)
353{
354 QDict *options = NULL;
355
356 options = qdict_new();
357 qemu_opt_foreach(create_opts, img_add_key_secrets, options, &error_abort);
358
359 return img_open_file(filename, options, fmt, flags, writethrough, quiet,
360 force_share);
361}
362
363
Max Reitzefaa7c42016-03-16 19:54:38 +0100364static BlockBackend *img_open(bool image_opts,
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000365 const char *filename,
Kevin Wolfce099542016-03-15 13:01:04 +0100366 const char *fmt, int flags, bool writethrough,
Fam Zheng335e9932017-05-03 00:35:39 +0800367 bool quiet, bool force_share)
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000368{
369 BlockBackend *blk;
370 if (image_opts) {
371 QemuOpts *opts;
372 if (fmt) {
373 error_report("--image-opts and --format are mutually exclusive");
374 return NULL;
375 }
376 opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
377 filename, true);
378 if (!opts) {
379 return NULL;
380 }
Fam Zheng335e9932017-05-03 00:35:39 +0800381 blk = img_open_opts(filename, opts, flags, writethrough, quiet,
382 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000383 } else {
Daniel P. Berrange29cf9332017-05-15 17:47:12 +0100384 blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
Fam Zheng335e9932017-05-03 00:35:39 +0800385 force_share);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000386 }
387 return blk;
388}
389
390
Chunyan Liu83d05212014-06-05 17:20:51 +0800391static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100392 const char *base_filename,
393 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200394{
Markus Armbruster6750e792015-02-12 17:43:08 +0100395 Error *err = NULL;
396
Kevin Wolfefa84d42009-05-18 16:42:12 +0200397 if (base_filename) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100398 qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100399 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100400 error_report("Backing file not supported for file format '%s'",
401 fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100402 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900403 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200404 }
405 }
406 if (base_fmt) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100407 qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +0100408 if (err) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100409 error_report("Backing file format not supported for file "
410 "format '%s'", fmt);
Markus Armbruster6750e792015-02-12 17:43:08 +0100411 error_free(err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900412 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200413 }
414 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900415 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200416}
417
Markus Armbruster606caa02017-02-21 21:14:04 +0100418static int64_t cvtnum(const char *s)
419{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100420 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100421 uint64_t value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100422
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100423 err = qemu_strtosz(s, NULL, &value);
424 if (err < 0) {
425 return err;
426 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100427 if (value > INT64_MAX) {
428 return -ERANGE;
429 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100430 return value;
Markus Armbruster606caa02017-02-21 21:14:04 +0100431}
432
bellardea2384d2004-08-01 21:59:26 +0000433static int img_create(int argc, char **argv)
434{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200435 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100436 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000437 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000438 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000439 const char *filename;
440 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200441 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200442 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100443 bool quiet = false;
John Snow6e6e55f2017-07-17 20:34:22 -0400444 int flags = 0;
ths3b46e622007-09-17 08:09:54 +0000445
bellardea2384d2004-08-01 21:59:26 +0000446 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000447 static const struct option long_options[] = {
448 {"help", no_argument, 0, 'h'},
449 {"object", required_argument, 0, OPTION_OBJECT},
450 {0, 0, 0, 0}
451 };
John Snow6e6e55f2017-07-17 20:34:22 -0400452 c = getopt_long(argc, argv, ":F:b:f:ho:qu",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000453 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100454 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000455 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100456 }
bellardea2384d2004-08-01 21:59:26 +0000457 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800458 case ':':
459 missing_argument(argv[optind - 1]);
460 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100461 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800462 unrecognized_option(argv[optind - 1]);
463 break;
bellardea2384d2004-08-01 21:59:26 +0000464 case 'h':
465 help();
466 break;
aliguori9230eaf2009-03-28 17:55:19 +0000467 case 'F':
468 base_fmt = optarg;
469 break;
bellardea2384d2004-08-01 21:59:26 +0000470 case 'b':
471 base_filename = optarg;
472 break;
473 case 'f':
474 fmt = optarg;
475 break;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200476 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100477 if (!is_valid_option_list(optarg)) {
478 error_report("Invalid option list: %s", optarg);
479 goto fail;
480 }
481 if (!options) {
482 options = g_strdup(optarg);
483 } else {
484 char *old_options = options;
485 options = g_strdup_printf("%s,%s", options, optarg);
486 g_free(old_options);
487 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200488 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100489 case 'q':
490 quiet = true;
491 break;
John Snow6e6e55f2017-07-17 20:34:22 -0400492 case 'u':
493 flags |= BDRV_O_NO_BACKING;
494 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000495 case OPTION_OBJECT: {
496 QemuOpts *opts;
497 opts = qemu_opts_parse_noisily(&qemu_object_opts,
498 optarg, true);
499 if (!opts) {
500 goto fail;
501 }
502 } break;
bellardea2384d2004-08-01 21:59:26 +0000503 }
504 }
aliguori9230eaf2009-03-28 17:55:19 +0000505
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900506 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100507 filename = (optind < argc) ? argv[optind] : NULL;
508 if (options && has_help_option(options)) {
509 g_free(options);
510 return print_block_option_help(filename, fmt);
511 }
512
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100513 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800514 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100515 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100516 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900517
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000518 if (qemu_opts_foreach(&qemu_object_opts,
519 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200520 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000521 goto fail;
522 }
523
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100524 /* Get image size, if specified */
525 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100526 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +0100527
528 sval = cvtnum(argv[optind++]);
529 if (sval < 0) {
liguang79443392012-12-17 09:49:23 +0800530 if (sval == -ERANGE) {
531 error_report("Image size must be less than 8 EiB!");
532 } else {
533 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200534 "G, T, P or E suffixes for ");
535 error_report("kilobytes, megabytes, gigabytes, terabytes, "
536 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800537 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100538 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100539 }
540 img_size = (uint64_t)sval;
541 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200542 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800543 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200544 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100545
Luiz Capitulino9b375252012-11-30 10:52:05 -0200546 bdrv_img_create(filename, fmt, base_filename, base_fmt,
John Snow6e6e55f2017-07-17 20:34:22 -0400547 options, img_size, flags, quiet, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100548 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100549 error_reportf_err(local_err, "%s: ", filename);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100550 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900551 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200552
Kevin Wolf77386bf2014-02-21 16:24:04 +0100553 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000554 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100555
556fail:
557 g_free(options);
558 return 1;
bellardea2384d2004-08-01 21:59:26 +0000559}
560
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100561static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500562{
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500563 QString *str;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500564 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100565 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600566
567 visit_type_ImageCheck(v, NULL, &check, &error_abort);
568 visit_complete(v, &obj);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500569 str = qobject_to_json_pretty(obj);
570 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100571 qprintf(quiet, "%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200572 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600573 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200574 qobject_unref(str);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500575}
576
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100577static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500578{
579 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100580 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500581 } else {
582 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100583 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
584 "Data may be corrupted, or further writes to the image "
585 "may corrupt it.\n",
586 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500587 }
588
589 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100590 qprintf(quiet,
591 "\n%" PRId64 " leaked clusters were found on the image.\n"
592 "This means waste of disk space, but no harm to data.\n",
593 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500594 }
595
596 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100597 qprintf(quiet,
598 "\n%" PRId64
599 " internal errors have occurred during the check.\n",
600 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500601 }
602 }
603
604 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100605 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
606 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
607 check->allocated_clusters, check->total_clusters,
608 check->allocated_clusters * 100.0 / check->total_clusters,
609 check->fragmented_clusters * 100.0 / check->allocated_clusters,
610 check->compressed_clusters * 100.0 /
611 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500612 }
613
614 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100615 qprintf(quiet,
616 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500617 }
618}
619
620static int collect_image_check(BlockDriverState *bs,
621 ImageCheck *check,
622 const char *filename,
623 const char *fmt,
624 int fix)
625{
626 int ret;
627 BdrvCheckResult result;
628
629 ret = bdrv_check(bs, &result, fix);
630 if (ret < 0) {
631 return ret;
632 }
633
634 check->filename = g_strdup(filename);
635 check->format = g_strdup(bdrv_get_format_name(bs));
636 check->check_errors = result.check_errors;
637 check->corruptions = result.corruptions;
638 check->has_corruptions = result.corruptions != 0;
639 check->leaks = result.leaks;
640 check->has_leaks = result.leaks != 0;
641 check->corruptions_fixed = result.corruptions_fixed;
642 check->has_corruptions_fixed = result.corruptions != 0;
643 check->leaks_fixed = result.leaks_fixed;
644 check->has_leaks_fixed = result.leaks != 0;
645 check->image_end_offset = result.image_end_offset;
646 check->has_image_end_offset = result.image_end_offset != 0;
647 check->total_clusters = result.bfi.total_clusters;
648 check->has_total_clusters = result.bfi.total_clusters != 0;
649 check->allocated_clusters = result.bfi.allocated_clusters;
650 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
651 check->fragmented_clusters = result.bfi.fragmented_clusters;
652 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100653 check->compressed_clusters = result.bfi.compressed_clusters;
654 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500655
656 return 0;
657}
658
Kevin Wolfe076f332010-06-29 11:43:13 +0200659/*
660 * Checks an image for consistency. Exit codes:
661 *
Max Reitzd6635c42014-06-02 22:15:21 +0200662 * 0 - Check completed, image is good
663 * 1 - Check not completed because of internal errors
664 * 2 - Check completed, image is corrupted
665 * 3 - Check completed, image has leaked clusters, but is good otherwise
666 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200667 */
aliguori15859692009-04-21 23:11:53 +0000668static int img_check(int argc, char **argv)
669{
670 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500671 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200672 const char *filename, *fmt, *output, *cache;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200673 BlockBackend *blk;
aliguori15859692009-04-21 23:11:53 +0000674 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200675 int fix = 0;
Kevin Wolfce099542016-03-15 13:01:04 +0100676 int flags = BDRV_O_CHECK;
677 bool writethrough;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200678 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100679 bool quiet = false;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000680 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +0800681 bool force_share = false;
aliguori15859692009-04-21 23:11:53 +0000682
683 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500684 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200685 cache = BDRV_DEFAULT_CACHE;
Kevin Wolfce099542016-03-15 13:01:04 +0100686
aliguori15859692009-04-21 23:11:53 +0000687 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500688 int option_index = 0;
689 static const struct option long_options[] = {
690 {"help", no_argument, 0, 'h'},
691 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530692 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500693 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000694 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000695 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +0800696 {"force-share", no_argument, 0, 'U'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500697 {0, 0, 0, 0}
698 };
Fam Zheng335e9932017-05-03 00:35:39 +0800699 c = getopt_long(argc, argv, ":hf:r:T:qU",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500700 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100701 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000702 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100703 }
aliguori15859692009-04-21 23:11:53 +0000704 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800705 case ':':
706 missing_argument(argv[optind - 1]);
707 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100708 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800709 unrecognized_option(argv[optind - 1]);
710 break;
aliguori15859692009-04-21 23:11:53 +0000711 case 'h':
712 help();
713 break;
714 case 'f':
715 fmt = optarg;
716 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200717 case 'r':
718 flags |= BDRV_O_RDWR;
719
720 if (!strcmp(optarg, "leaks")) {
721 fix = BDRV_FIX_LEAKS;
722 } else if (!strcmp(optarg, "all")) {
723 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
724 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800725 error_exit("Unknown option value for -r "
726 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200727 }
728 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500729 case OPTION_OUTPUT:
730 output = optarg;
731 break;
Max Reitz40055952014-07-22 22:58:42 +0200732 case 'T':
733 cache = optarg;
734 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100735 case 'q':
736 quiet = true;
737 break;
Fam Zheng335e9932017-05-03 00:35:39 +0800738 case 'U':
739 force_share = true;
740 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000741 case OPTION_OBJECT: {
742 QemuOpts *opts;
743 opts = qemu_opts_parse_noisily(&qemu_object_opts,
744 optarg, true);
745 if (!opts) {
746 return 1;
747 }
748 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000749 case OPTION_IMAGE_OPTS:
750 image_opts = true;
751 break;
aliguori15859692009-04-21 23:11:53 +0000752 }
753 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200754 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800755 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100756 }
aliguori15859692009-04-21 23:11:53 +0000757 filename = argv[optind++];
758
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500759 if (output && !strcmp(output, "json")) {
760 output_format = OFORMAT_JSON;
761 } else if (output && !strcmp(output, "human")) {
762 output_format = OFORMAT_HUMAN;
763 } else if (output) {
764 error_report("--output must be used with human or json as argument.");
765 return 1;
766 }
767
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000768 if (qemu_opts_foreach(&qemu_object_opts,
769 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200770 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000771 return 1;
772 }
773
Kevin Wolfce099542016-03-15 13:01:04 +0100774 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +0200775 if (ret < 0) {
776 error_report("Invalid source cache option: %s", cache);
777 return 1;
778 }
779
Fam Zheng335e9932017-05-03 00:35:39 +0800780 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
781 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200782 if (!blk) {
783 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900784 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200785 bs = blk_bs(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500786
787 check = g_new0(ImageCheck, 1);
788 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200789
790 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200791 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200792 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500793 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200794 }
795
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500796 if (check->corruptions_fixed || check->leaks_fixed) {
797 int corruptions_fixed, leaks_fixed;
798
799 leaks_fixed = check->leaks_fixed;
800 corruptions_fixed = check->corruptions_fixed;
801
802 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100803 qprintf(quiet,
804 "The following inconsistencies were found and repaired:\n\n"
805 " %" PRId64 " leaked clusters\n"
806 " %" PRId64 " corruptions\n\n"
807 "Double checking the fixed image now...\n",
808 check->leaks_fixed,
809 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500810 }
811
812 ret = collect_image_check(bs, check, filename, fmt, 0);
813
814 check->leaks_fixed = leaks_fixed;
815 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200816 }
817
Max Reitz832390a2014-10-23 15:29:12 +0200818 if (!ret) {
819 switch (output_format) {
820 case OFORMAT_HUMAN:
821 dump_human_image_check(check, quiet);
822 break;
823 case OFORMAT_JSON:
824 dump_json_image_check(check, quiet);
825 break;
826 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500827 }
828
829 if (ret || check->check_errors) {
Max Reitz832390a2014-10-23 15:29:12 +0200830 if (ret) {
831 error_report("Check failed: %s", strerror(-ret));
832 } else {
833 error_report("Check failed");
834 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500835 ret = 1;
836 goto fail;
837 }
838
839 if (check->corruptions) {
840 ret = 2;
841 } else if (check->leaks) {
842 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200843 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500844 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000845 }
846
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500847fail:
848 qapi_free_ImageCheck(check);
Markus Armbruster26f54e92014-10-07 13:59:04 +0200849 blk_unref(blk);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500850 return ret;
aliguori15859692009-04-21 23:11:53 +0000851}
852
Max Reitzd4a32382014-10-24 15:57:37 +0200853typedef struct CommonBlockJobCBInfo {
854 BlockDriverState *bs;
855 Error **errp;
856} CommonBlockJobCBInfo;
857
858static void common_block_job_cb(void *opaque, int ret)
859{
860 CommonBlockJobCBInfo *cbi = opaque;
861
862 if (ret < 0) {
863 error_setg_errno(cbi->errp, -ret, "Block job failed");
864 }
Max Reitzd4a32382014-10-24 15:57:37 +0200865}
866
867static void run_block_job(BlockJob *job, Error **errp)
868{
Kevin Wolfb75536c2016-04-18 17:30:17 +0200869 AioContext *aio_context = blk_get_aio_context(job->blk);
sochin.jiang4172a002017-06-15 14:47:33 +0800870 int ret = 0;
Max Reitzd4a32382014-10-24 15:57:37 +0200871
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200872 aio_context_acquire(aio_context);
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200873 job_ref(&job->job);
Max Reitzd4a32382014-10-24 15:57:37 +0200874 do {
Kevin Wolf30a5c882018-05-04 12:17:20 +0200875 float progress = 0.0f;
Max Reitzd4a32382014-10-24 15:57:37 +0200876 aio_poll(aio_context, true);
Kevin Wolf30a5c882018-05-04 12:17:20 +0200877 if (job->job.progress_total) {
878 progress = (float)job->job.progress_current /
879 job->job.progress_total * 100.f;
880 }
881 qemu_progress_print(progress, 0);
Kevin Wolfdf956ae2018-04-25 15:09:58 +0200882 } while (!job_is_ready(&job->job) && !job_is_completed(&job->job));
Max Reitzd4a32382014-10-24 15:57:37 +0200883
Kevin Wolfdbe5e6c2018-04-19 13:04:01 +0200884 if (!job_is_completed(&job->job)) {
Kevin Wolf3d70ff52018-04-24 16:13:52 +0200885 ret = job_complete_sync(&job->job, errp);
sochin.jiang4172a002017-06-15 14:47:33 +0800886 } else {
Kevin Wolf4ad35182018-04-19 17:30:16 +0200887 ret = job->job.ret;
sochin.jiang4172a002017-06-15 14:47:33 +0800888 }
Kevin Wolf80fa2c72018-04-13 18:50:05 +0200889 job_unref(&job->job);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200890 aio_context_release(aio_context);
Max Reitz687fa1d2014-10-24 15:57:39 +0200891
sochin.jiang4172a002017-06-15 14:47:33 +0800892 /* publish completion progress only when success */
893 if (!ret) {
894 qemu_progress_print(100.f, 0);
895 }
Max Reitzd4a32382014-10-24 15:57:37 +0200896}
897
bellardea2384d2004-08-01 21:59:26 +0000898static int img_commit(int argc, char **argv)
899{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400900 int c, ret, flags;
Max Reitz1b22bff2014-10-24 15:57:40 +0200901 const char *filename, *fmt, *cache, *base;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200902 BlockBackend *blk;
Max Reitzd4a32382014-10-24 15:57:37 +0200903 BlockDriverState *bs, *base_bs;
Kevin Wolf4ef85a92017-01-25 19:16:34 +0100904 BlockJob *job;
Max Reitz687fa1d2014-10-24 15:57:39 +0200905 bool progress = false, quiet = false, drop = false;
Kevin Wolfce099542016-03-15 13:01:04 +0100906 bool writethrough;
Max Reitzd4a32382014-10-24 15:57:37 +0200907 Error *local_err = NULL;
908 CommonBlockJobCBInfo cbi;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000909 bool image_opts = false;
Paolo Bonzini9e944cb2016-10-27 12:49:04 +0200910 AioContext *aio_context;
bellardea2384d2004-08-01 21:59:26 +0000911
912 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400913 cache = BDRV_DEFAULT_CACHE;
Max Reitz1b22bff2014-10-24 15:57:40 +0200914 base = NULL;
bellardea2384d2004-08-01 21:59:26 +0000915 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000916 static const struct option long_options[] = {
917 {"help", no_argument, 0, 'h'},
918 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000919 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000920 {0, 0, 0, 0}
921 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800922 c = getopt_long(argc, argv, ":f:ht:b:dpq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000923 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100924 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000925 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100926 }
bellardea2384d2004-08-01 21:59:26 +0000927 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800928 case ':':
929 missing_argument(argv[optind - 1]);
930 break;
Jes Sorensenef873942010-12-06 15:25:40 +0100931 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +0800932 unrecognized_option(argv[optind - 1]);
933 break;
bellardea2384d2004-08-01 21:59:26 +0000934 case 'h':
935 help();
936 break;
937 case 'f':
938 fmt = optarg;
939 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400940 case 't':
941 cache = optarg;
942 break;
Max Reitz1b22bff2014-10-24 15:57:40 +0200943 case 'b':
944 base = optarg;
945 /* -b implies -d */
946 drop = true;
947 break;
Max Reitz9a86fe42014-10-24 15:57:38 +0200948 case 'd':
949 drop = true;
950 break;
Max Reitz687fa1d2014-10-24 15:57:39 +0200951 case 'p':
952 progress = true;
953 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100954 case 'q':
955 quiet = true;
956 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000957 case OPTION_OBJECT: {
958 QemuOpts *opts;
959 opts = qemu_opts_parse_noisily(&qemu_object_opts,
960 optarg, true);
961 if (!opts) {
962 return 1;
963 }
964 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +0000965 case OPTION_IMAGE_OPTS:
966 image_opts = true;
967 break;
bellardea2384d2004-08-01 21:59:26 +0000968 }
969 }
Max Reitz687fa1d2014-10-24 15:57:39 +0200970
971 /* Progress is not shown in Quiet mode */
972 if (quiet) {
973 progress = false;
974 }
975
Kevin Wolffc11eb22013-08-05 10:53:04 +0200976 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800977 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100978 }
bellardea2384d2004-08-01 21:59:26 +0000979 filename = argv[optind++];
980
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000981 if (qemu_opts_foreach(&qemu_object_opts,
982 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200983 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +0000984 return 1;
985 }
986
Max Reitz9a86fe42014-10-24 15:57:38 +0200987 flags = BDRV_O_RDWR | BDRV_O_UNMAP;
Kevin Wolfce099542016-03-15 13:01:04 +0100988 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400989 if (ret < 0) {
990 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100991 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400992 }
993
Fam Zheng335e9932017-05-03 00:35:39 +0800994 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
995 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200996 if (!blk) {
997 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900998 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +0200999 bs = blk_bs(blk);
1000
Max Reitz687fa1d2014-10-24 15:57:39 +02001001 qemu_progress_init(progress, 1.f);
1002 qemu_progress_print(0.f, 100);
1003
Max Reitz1b22bff2014-10-24 15:57:40 +02001004 if (base) {
1005 base_bs = bdrv_find_backing_image(bs, base);
1006 if (!base_bs) {
Max Reitz6b33f3a2016-12-01 03:05:08 +01001007 error_setg(&local_err,
1008 "Did not find '%s' in the backing chain of '%s'",
1009 base, filename);
Max Reitz1b22bff2014-10-24 15:57:40 +02001010 goto done;
1011 }
1012 } else {
1013 /* This is different from QMP, which by default uses the deepest file in
1014 * the backing chain (i.e., the very base); however, the traditional
1015 * behavior of qemu-img commit is using the immediate backing file. */
Kevin Wolf760e0062015-06-17 14:55:21 +02001016 base_bs = backing_bs(bs);
Max Reitz1b22bff2014-10-24 15:57:40 +02001017 if (!base_bs) {
1018 error_setg(&local_err, "Image does not have a backing file");
1019 goto done;
1020 }
bellardea2384d2004-08-01 21:59:26 +00001021 }
1022
Max Reitzd4a32382014-10-24 15:57:37 +02001023 cbi = (CommonBlockJobCBInfo){
1024 .errp = &local_err,
1025 .bs = bs,
1026 };
1027
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001028 aio_context = bdrv_get_aio_context(bs);
1029 aio_context_acquire(aio_context);
Kevin Wolfbb02b652018-04-19 17:54:56 +02001030 commit_active_start("commit", bs, base_bs, JOB_DEFAULT, 0,
Kevin Wolf0db832f2017-02-20 18:10:05 +01001031 BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
Fam Zheng78bbd912017-04-21 20:27:04 +08001032 &cbi, false, &local_err);
Paolo Bonzini9e944cb2016-10-27 12:49:04 +02001033 aio_context_release(aio_context);
Max Reitzd4a32382014-10-24 15:57:37 +02001034 if (local_err) {
1035 goto done;
1036 }
1037
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001038 /* When the block job completes, the BlockBackend reference will point to
1039 * the old backing file. In order to avoid that the top image is already
1040 * deleted, so we can still empty it afterwards, increment the reference
1041 * counter here preemptively. */
Max Reitz9a86fe42014-10-24 15:57:38 +02001042 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001043 bdrv_ref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001044 }
1045
Kevin Wolf4ef85a92017-01-25 19:16:34 +01001046 job = block_job_get("commit");
1047 run_block_job(job, &local_err);
Max Reitz9a86fe42014-10-24 15:57:38 +02001048 if (local_err) {
1049 goto unref_backing;
1050 }
1051
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001052 if (!drop && bs->drv->bdrv_make_empty) {
1053 ret = bs->drv->bdrv_make_empty(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001054 if (ret) {
1055 error_setg_errno(&local_err, -ret, "Could not empty %s",
1056 filename);
1057 goto unref_backing;
1058 }
1059 }
1060
1061unref_backing:
1062 if (!drop) {
Kevin Wolf3f09bfb2015-09-15 11:58:23 +02001063 bdrv_unref(bs);
Max Reitz9a86fe42014-10-24 15:57:38 +02001064 }
Max Reitzd4a32382014-10-24 15:57:37 +02001065
1066done:
Max Reitz687fa1d2014-10-24 15:57:39 +02001067 qemu_progress_end();
1068
Markus Armbruster26f54e92014-10-07 13:59:04 +02001069 blk_unref(blk);
Max Reitzd4a32382014-10-24 15:57:37 +02001070
1071 if (local_err) {
Markus Armbruster6936f292015-02-10 15:14:02 +01001072 error_report_err(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001073 return 1;
1074 }
Max Reitzd4a32382014-10-24 15:57:37 +02001075
1076 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +00001077 return 0;
1078}
1079
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +04001080/*
Eric Blakedebb38a2017-10-11 22:47:11 -05001081 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1082 * of the first sector boundary within buf where the sector contains a
1083 * non-zero byte. This function is robust to a buffer that is not
1084 * sector-aligned.
1085 */
1086static int64_t find_nonzero(const uint8_t *buf, int64_t n)
1087{
1088 int64_t i;
1089 int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE);
1090
1091 for (i = 0; i < end; i += BDRV_SECTOR_SIZE) {
1092 if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) {
1093 return i;
1094 }
1095 }
1096 if (i < n && !buffer_is_zero(buf + i, n - end)) {
1097 return i;
1098 }
1099 return -1;
1100}
1101
1102/*
thsf58c7b32008-06-05 21:53:49 +00001103 * Returns true iff the first sector pointed to by 'buf' contains at least
1104 * a non-NUL byte.
1105 *
1106 * 'pnum' is set to the number of sectors (including and immediately following
1107 * the first one) that are known to be in the same allocated/unallocated state.
1108 */
bellardea2384d2004-08-01 21:59:26 +00001109static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1110{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001111 bool is_zero;
1112 int i;
bellardea2384d2004-08-01 21:59:26 +00001113
1114 if (n <= 0) {
1115 *pnum = 0;
1116 return 0;
1117 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001118 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001119 for(i = 1; i < n; i++) {
1120 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001121 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +00001122 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001123 }
bellardea2384d2004-08-01 21:59:26 +00001124 }
1125 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001126 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +00001127}
1128
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001129/*
Kevin Wolfa22f1232011-08-26 15:27:13 +02001130 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1131 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1132 * breaking up write requests for only small sparse areas.
1133 */
1134static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1135 int min)
1136{
1137 int ret;
1138 int num_checked, num_used;
1139
1140 if (n < min) {
1141 min = n;
1142 }
1143
1144 ret = is_allocated_sectors(buf, n, pnum);
1145 if (!ret) {
1146 return ret;
1147 }
1148
1149 num_used = *pnum;
1150 buf += BDRV_SECTOR_SIZE * *pnum;
1151 n -= *pnum;
1152 num_checked = num_used;
1153
1154 while (n > 0) {
1155 ret = is_allocated_sectors(buf, n, pnum);
1156
1157 buf += BDRV_SECTOR_SIZE * *pnum;
1158 n -= *pnum;
1159 num_checked += *pnum;
1160 if (ret) {
1161 num_used = num_checked;
1162 } else if (*pnum >= min) {
1163 break;
1164 }
1165 }
1166
1167 *pnum = num_used;
1168 return 1;
1169}
1170
1171/*
Eric Blakedc61cd32017-10-11 22:47:14 -05001172 * Compares two buffers sector by sector. Returns 0 if the first
1173 * sector of each buffer matches, non-zero otherwise.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001174 *
Eric Blakedc61cd32017-10-11 22:47:14 -05001175 * pnum is set to the sector-aligned size of the buffer prefix that
1176 * has the same matching status as the first sector.
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001177 */
Eric Blakedc61cd32017-10-11 22:47:14 -05001178static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2,
1179 int64_t bytes, int64_t *pnum)
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001180{
Radim Krčmář8c1ac472015-02-20 17:06:15 +01001181 bool res;
Eric Blakedc61cd32017-10-11 22:47:14 -05001182 int64_t i = MIN(bytes, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001183
Eric Blakedc61cd32017-10-11 22:47:14 -05001184 assert(bytes > 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001185
Eric Blakedc61cd32017-10-11 22:47:14 -05001186 res = !!memcmp(buf1, buf2, i);
1187 while (i < bytes) {
1188 int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001189
Eric Blakedc61cd32017-10-11 22:47:14 -05001190 if (!!memcmp(buf1 + i, buf2 + i, len) != res) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001191 break;
1192 }
Eric Blakedc61cd32017-10-11 22:47:14 -05001193 i += len;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001194 }
1195
1196 *pnum = i;
1197 return res;
1198}
1199
Kevin Wolf80ee15a2009-09-15 12:30:43 +02001200#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +00001201
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001202/*
1203 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1204 *
Eric Blake0608e402017-10-11 22:47:12 -05001205 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1206 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1207 * failure), and 4 on error (the exit status for read errors), after emitting
1208 * an error message.
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001209 *
Max Reitzf1d3cd72015-02-05 13:58:18 -05001210 * @param blk: BlockBackend for the image
Eric Blakec41508e2017-10-11 22:47:13 -05001211 * @param offset: Starting offset to check
1212 * @param bytes: Number of bytes to check
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001213 * @param filename: Name of disk file we are checking (logging purpose)
1214 * @param buffer: Allocated buffer for storing read data
1215 * @param quiet: Flag for quiet mode
1216 */
Eric Blakec41508e2017-10-11 22:47:13 -05001217static int check_empty_sectors(BlockBackend *blk, int64_t offset,
1218 int64_t bytes, const char *filename,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001219 uint8_t *buffer, bool quiet)
1220{
Eric Blakedebb38a2017-10-11 22:47:11 -05001221 int ret = 0;
1222 int64_t idx;
1223
Eric Blakec41508e2017-10-11 22:47:13 -05001224 ret = blk_pread(blk, offset, buffer, bytes);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001225 if (ret < 0) {
1226 error_report("Error while reading offset %" PRId64 " of %s: %s",
Eric Blakec41508e2017-10-11 22:47:13 -05001227 offset, filename, strerror(-ret));
Eric Blake0608e402017-10-11 22:47:12 -05001228 return 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001229 }
Eric Blakec41508e2017-10-11 22:47:13 -05001230 idx = find_nonzero(buffer, bytes);
Eric Blakedebb38a2017-10-11 22:47:11 -05001231 if (idx >= 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001232 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blakec41508e2017-10-11 22:47:13 -05001233 offset + idx);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001234 return 1;
1235 }
1236
1237 return 0;
1238}
1239
1240/*
1241 * Compares two images. Exit codes:
1242 *
1243 * 0 - Images are identical
1244 * 1 - Images differ
1245 * >1 - Error occurred
1246 */
1247static int img_compare(int argc, char **argv)
1248{
Max Reitz40055952014-07-22 22:58:42 +02001249 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001250 BlockBackend *blk1, *blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001251 BlockDriverState *bs1, *bs2;
Eric Blake033d9fc2017-10-11 22:47:16 -05001252 int64_t total_size1, total_size2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001253 uint8_t *buf1 = NULL, *buf2 = NULL;
Eric Blake31826642017-10-11 22:47:08 -05001254 int64_t pnum1, pnum2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001255 int allocated1, allocated2;
1256 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1257 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +02001258 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01001259 bool writethrough;
Eric Blake033d9fc2017-10-11 22:47:16 -05001260 int64_t total_size;
1261 int64_t offset = 0;
1262 int64_t chunk;
Eric Blakedc61cd32017-10-11 22:47:14 -05001263 int c;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001264 uint64_t progress_base;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001265 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08001266 bool force_share = false;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001267
Max Reitz40055952014-07-22 22:58:42 +02001268 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001269 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001270 static const struct option long_options[] = {
1271 {"help", no_argument, 0, 'h'},
1272 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001273 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08001274 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001275 {0, 0, 0, 0}
1276 };
Fam Zheng335e9932017-05-03 00:35:39 +08001277 c = getopt_long(argc, argv, ":hf:F:T:pqsU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001278 long_options, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001279 if (c == -1) {
1280 break;
1281 }
1282 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001283 case ':':
1284 missing_argument(argv[optind - 1]);
1285 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001286 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08001287 unrecognized_option(argv[optind - 1]);
1288 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001289 case 'h':
1290 help();
1291 break;
1292 case 'f':
1293 fmt1 = optarg;
1294 break;
1295 case 'F':
1296 fmt2 = optarg;
1297 break;
Max Reitz40055952014-07-22 22:58:42 +02001298 case 'T':
1299 cache = optarg;
1300 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001301 case 'p':
1302 progress = true;
1303 break;
1304 case 'q':
1305 quiet = true;
1306 break;
1307 case 's':
1308 strict = true;
1309 break;
Fam Zheng335e9932017-05-03 00:35:39 +08001310 case 'U':
1311 force_share = true;
1312 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001313 case OPTION_OBJECT: {
1314 QemuOpts *opts;
1315 opts = qemu_opts_parse_noisily(&qemu_object_opts,
1316 optarg, true);
1317 if (!opts) {
1318 ret = 2;
1319 goto out4;
1320 }
1321 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001322 case OPTION_IMAGE_OPTS:
1323 image_opts = true;
1324 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001325 }
1326 }
1327
1328 /* Progress is not shown in Quiet mode */
1329 if (quiet) {
1330 progress = false;
1331 }
1332
1333
Kevin Wolffc11eb22013-08-05 10:53:04 +02001334 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001335 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001336 }
1337 filename1 = argv[optind++];
1338 filename2 = argv[optind++];
1339
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001340 if (qemu_opts_foreach(&qemu_object_opts,
1341 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02001342 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001343 ret = 2;
1344 goto out4;
1345 }
1346
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001347 /* Initialize before goto out */
1348 qemu_progress_init(progress, 2.0);
1349
Kevin Wolfce099542016-03-15 13:01:04 +01001350 flags = 0;
1351 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitz40055952014-07-22 22:58:42 +02001352 if (ret < 0) {
1353 error_report("Invalid source cache option: %s", cache);
1354 ret = 2;
1355 goto out3;
1356 }
1357
Fam Zheng335e9932017-05-03 00:35:39 +08001358 blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1359 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001360 if (!blk1) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001361 ret = 2;
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001362 goto out3;
1363 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001364
Fam Zheng335e9932017-05-03 00:35:39 +08001365 blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1366 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001367 if (!blk2) {
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001368 ret = 2;
Markus Armbruster26f54e92014-10-07 13:59:04 +02001369 goto out2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001370 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00001371 bs1 = blk_bs(blk1);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02001372 bs2 = blk_bs(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001373
Max Reitzf1d3cd72015-02-05 13:58:18 -05001374 buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1375 buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
Eric Blake033d9fc2017-10-11 22:47:16 -05001376 total_size1 = blk_getlength(blk1);
1377 if (total_size1 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001378 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001379 filename1, strerror(-total_size1));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001380 ret = 4;
1381 goto out;
1382 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001383 total_size2 = blk_getlength(blk2);
1384 if (total_size2 < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001385 error_report("Can't get size of %s: %s",
Eric Blake033d9fc2017-10-11 22:47:16 -05001386 filename2, strerror(-total_size2));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001387 ret = 4;
1388 goto out;
1389 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001390 total_size = MIN(total_size1, total_size2);
1391 progress_base = MAX(total_size1, total_size2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001392
1393 qemu_progress_print(0, 100);
1394
Eric Blake033d9fc2017-10-11 22:47:16 -05001395 if (strict && total_size1 != total_size2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001396 ret = 1;
1397 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1398 goto out;
1399 }
1400
Eric Blake033d9fc2017-10-11 22:47:16 -05001401 while (offset < total_size) {
Eric Blake31826642017-10-11 22:47:08 -05001402 int status1, status2;
Fam Zheng67a0fd22016-01-26 11:58:48 +08001403
Eric Blake033d9fc2017-10-11 22:47:16 -05001404 status1 = bdrv_block_status_above(bs1, NULL, offset,
1405 total_size1 - offset, &pnum1, NULL,
1406 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001407 if (status1 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001408 ret = 3;
1409 error_report("Sector allocation test failed for %s", filename1);
1410 goto out;
1411 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001412 allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001413
Eric Blake033d9fc2017-10-11 22:47:16 -05001414 status2 = bdrv_block_status_above(bs2, NULL, offset,
1415 total_size2 - offset, &pnum2, NULL,
1416 NULL);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001417 if (status2 < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001418 ret = 3;
1419 error_report("Sector allocation test failed for %s", filename2);
1420 goto out;
1421 }
Fam Zheng25ad8e62016-01-13 16:37:41 +08001422 allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
Eric Blake7daddc62017-10-11 22:47:09 -05001423
1424 assert(pnum1 && pnum2);
Eric Blake033d9fc2017-10-11 22:47:16 -05001425 chunk = MIN(pnum1, pnum2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001426
Fam Zheng25ad8e62016-01-13 16:37:41 +08001427 if (strict) {
Eric Blake31826642017-10-11 22:47:08 -05001428 if (status1 != status2) {
Fam Zheng25ad8e62016-01-13 16:37:41 +08001429 ret = 1;
1430 qprintf(quiet, "Strict mode: Offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001431 " block status mismatch!\n", offset);
Fam Zheng25ad8e62016-01-13 16:37:41 +08001432 goto out;
1433 }
1434 }
1435 if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
Eric Blake7daddc62017-10-11 22:47:09 -05001436 /* nothing to do */
Fam Zheng25ad8e62016-01-13 16:37:41 +08001437 } else if (allocated1 == allocated2) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001438 if (allocated1) {
Eric Blakedc61cd32017-10-11 22:47:14 -05001439 int64_t pnum;
1440
Eric Blake033d9fc2017-10-11 22:47:16 -05001441 chunk = MIN(chunk, IO_BUF_SIZE);
1442 ret = blk_pread(blk1, offset, buf1, chunk);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001443 if (ret < 0) {
1444 error_report("Error while reading offset %" PRId64
Eric Blake033d9fc2017-10-11 22:47:16 -05001445 " of %s: %s",
1446 offset, filename1, strerror(-ret));
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001447 ret = 4;
1448 goto out;
1449 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001450 ret = blk_pread(blk2, offset, buf2, chunk);
1451 if (ret < 0) {
1452 error_report("Error while reading offset %" PRId64
1453 " of %s: %s",
1454 offset, filename2, strerror(-ret));
1455 ret = 4;
1456 goto out;
1457 }
1458 ret = compare_buffers(buf1, buf2, chunk, &pnum);
1459 if (ret || pnum != chunk) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001460 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
Eric Blake033d9fc2017-10-11 22:47:16 -05001461 offset + (ret ? 0 : pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001462 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001463 goto out;
1464 }
1465 }
1466 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001467 chunk = MIN(chunk, IO_BUF_SIZE);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001468 if (allocated1) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001469 ret = check_empty_sectors(blk1, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001470 filename1, buf1, quiet);
1471 } else {
Eric Blake033d9fc2017-10-11 22:47:16 -05001472 ret = check_empty_sectors(blk2, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001473 filename2, buf1, quiet);
1474 }
1475 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001476 goto out;
1477 }
1478 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001479 offset += chunk;
1480 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001481 }
1482
Eric Blake033d9fc2017-10-11 22:47:16 -05001483 if (total_size1 != total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001484 BlockBackend *blk_over;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001485 const char *filename_over;
1486
1487 qprintf(quiet, "Warning: Image size mismatch!\n");
Eric Blake033d9fc2017-10-11 22:47:16 -05001488 if (total_size1 > total_size2) {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001489 blk_over = blk1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001490 filename_over = filename1;
1491 } else {
Max Reitzf1d3cd72015-02-05 13:58:18 -05001492 blk_over = blk2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001493 filename_over = filename2;
1494 }
1495
Eric Blake033d9fc2017-10-11 22:47:16 -05001496 while (offset < progress_base) {
1497 ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset,
1498 progress_base - offset, &chunk,
1499 NULL, NULL);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001500 if (ret < 0) {
1501 ret = 3;
1502 error_report("Sector allocation test failed for %s",
1503 filename_over);
1504 goto out;
1505
1506 }
Eric Blake391cb1a2017-10-11 22:47:10 -05001507 if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) {
Eric Blake033d9fc2017-10-11 22:47:16 -05001508 chunk = MIN(chunk, IO_BUF_SIZE);
1509 ret = check_empty_sectors(blk_over, offset, chunk,
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001510 filename_over, buf1, quiet);
1511 if (ret) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001512 goto out;
1513 }
1514 }
Eric Blake033d9fc2017-10-11 22:47:16 -05001515 offset += chunk;
1516 qemu_progress_print(((float) chunk / progress_base) * 100, 100);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001517 }
1518 }
1519
1520 qprintf(quiet, "Images are identical.\n");
1521 ret = 0;
1522
1523out:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001524 qemu_vfree(buf1);
1525 qemu_vfree(buf2);
Markus Armbruster26f54e92014-10-07 13:59:04 +02001526 blk_unref(blk2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001527out2:
Markus Armbruster26f54e92014-10-07 13:59:04 +02001528 blk_unref(blk1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001529out3:
1530 qemu_progress_end();
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00001531out4:
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001532 return ret;
1533}
1534
Kevin Wolf690c7302015-03-19 13:33:32 +01001535enum ImgConvertBlockStatus {
1536 BLK_DATA,
1537 BLK_ZERO,
1538 BLK_BACKING_FILE,
1539};
1540
Peter Lieven2d9187b2017-02-28 13:40:07 +01001541#define MAX_COROUTINES 16
1542
Kevin Wolf690c7302015-03-19 13:33:32 +01001543typedef struct ImgConvertState {
1544 BlockBackend **src;
1545 int64_t *src_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001546 int src_num;
Kevin Wolf690c7302015-03-19 13:33:32 +01001547 int64_t total_sectors;
1548 int64_t allocated_sectors;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001549 int64_t allocated_done;
1550 int64_t sector_num;
1551 int64_t wr_offs;
Kevin Wolf690c7302015-03-19 13:33:32 +01001552 enum ImgConvertBlockStatus status;
1553 int64_t sector_next_status;
1554 BlockBackend *target;
1555 bool has_zero_init;
1556 bool compressed;
Max Reitz351c8ef2018-05-01 18:57:49 +02001557 bool unallocated_blocks_are_zero;
Kevin Wolf690c7302015-03-19 13:33:32 +01001558 bool target_has_backing;
Max Reitz351c8ef2018-05-01 18:57:49 +02001559 int64_t target_backing_sectors; /* negative if unknown */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001560 bool wr_in_order;
Fam Zhengee5306d2018-06-01 17:26:48 +08001561 bool copy_range;
Kevin Wolf690c7302015-03-19 13:33:32 +01001562 int min_sparse;
1563 size_t cluster_sectors;
1564 size_t buf_sectors;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001565 long num_coroutines;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001566 int running_coroutines;
1567 Coroutine *co[MAX_COROUTINES];
1568 int64_t wait_sector_num[MAX_COROUTINES];
1569 CoMutex lock;
1570 int ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001571} ImgConvertState;
1572
Peter Lieven2d9187b2017-02-28 13:40:07 +01001573static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1574 int *src_cur, int64_t *src_cur_offset)
Kevin Wolf690c7302015-03-19 13:33:32 +01001575{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001576 *src_cur = 0;
1577 *src_cur_offset = 0;
1578 while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1579 *src_cur_offset += s->src_sectors[*src_cur];
1580 (*src_cur)++;
1581 assert(*src_cur < s->src_num);
Kevin Wolf690c7302015-03-19 13:33:32 +01001582 }
1583}
1584
1585static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1586{
Eric Blake31826642017-10-11 22:47:08 -05001587 int64_t src_cur_offset;
1588 int ret, n, src_cur;
Max Reitz351c8ef2018-05-01 18:57:49 +02001589 bool post_backing_zero = false;
Kevin Wolf690c7302015-03-19 13:33:32 +01001590
Peter Lieven2d9187b2017-02-28 13:40:07 +01001591 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
Kevin Wolf690c7302015-03-19 13:33:32 +01001592
1593 assert(s->total_sectors > sector_num);
1594 n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1595
Max Reitz351c8ef2018-05-01 18:57:49 +02001596 if (s->target_backing_sectors >= 0) {
1597 if (sector_num >= s->target_backing_sectors) {
1598 post_backing_zero = s->unallocated_blocks_are_zero;
1599 } else if (sector_num + n > s->target_backing_sectors) {
1600 /* Split requests around target_backing_sectors (because
1601 * starting from there, zeros are handled differently) */
1602 n = s->target_backing_sectors - sector_num;
1603 }
1604 }
1605
Kevin Wolf690c7302015-03-19 13:33:32 +01001606 if (s->sector_next_status <= sector_num) {
Eric Blake31826642017-10-11 22:47:08 -05001607 int64_t count = n * BDRV_SECTOR_SIZE;
1608
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001609 if (s->target_has_backing) {
Eric Blake237d78f2017-10-11 22:47:03 -05001610
1611 ret = bdrv_block_status(blk_bs(s->src[src_cur]),
1612 (sector_num - src_cur_offset) *
1613 BDRV_SECTOR_SIZE,
1614 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001615 } else {
Eric Blake31826642017-10-11 22:47:08 -05001616 ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL,
1617 (sector_num - src_cur_offset) *
1618 BDRV_SECTOR_SIZE,
1619 count, &count, NULL, NULL);
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001620 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001621 if (ret < 0) {
1622 return ret;
1623 }
Eric Blake31826642017-10-11 22:47:08 -05001624 n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
Kevin Wolf690c7302015-03-19 13:33:32 +01001625
1626 if (ret & BDRV_BLOCK_ZERO) {
Max Reitz351c8ef2018-05-01 18:57:49 +02001627 s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO;
Kevin Wolf690c7302015-03-19 13:33:32 +01001628 } else if (ret & BDRV_BLOCK_DATA) {
1629 s->status = BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001630 } else {
Vladimir Sementsov-Ogievskiy9f1b92a2017-04-07 14:34:04 +03001631 s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
Kevin Wolf690c7302015-03-19 13:33:32 +01001632 }
1633
1634 s->sector_next_status = sector_num + n;
1635 }
1636
1637 n = MIN(n, s->sector_next_status - sector_num);
1638 if (s->status == BLK_DATA) {
1639 n = MIN(n, s->buf_sectors);
1640 }
1641
1642 /* We need to write complete clusters for compressed images, so if an
1643 * unallocated area is shorter than that, we must consider the whole
1644 * cluster allocated. */
1645 if (s->compressed) {
1646 if (n < s->cluster_sectors) {
1647 n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1648 s->status = BLK_DATA;
1649 } else {
1650 n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1651 }
1652 }
1653
1654 return n;
1655}
1656
Peter Lieven2d9187b2017-02-28 13:40:07 +01001657static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1658 int nb_sectors, uint8_t *buf)
Kevin Wolf690c7302015-03-19 13:33:32 +01001659{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001660 int n, ret;
1661 QEMUIOVector qiov;
1662 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001663
Kevin Wolf690c7302015-03-19 13:33:32 +01001664 assert(nb_sectors <= s->buf_sectors);
1665 while (nb_sectors > 0) {
1666 BlockBackend *blk;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001667 int src_cur;
1668 int64_t bs_sectors, src_cur_offset;
Kevin Wolf690c7302015-03-19 13:33:32 +01001669
1670 /* In the case of compression with multiple source files, we can get a
1671 * nb_sectors that spreads into the next part. So we must be able to
1672 * read across multiple BDSes for one convert_read() call. */
Peter Lieven2d9187b2017-02-28 13:40:07 +01001673 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1674 blk = s->src[src_cur];
1675 bs_sectors = s->src_sectors[src_cur];
Kevin Wolf690c7302015-03-19 13:33:32 +01001676
Peter Lieven2d9187b2017-02-28 13:40:07 +01001677 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1678 iov.iov_base = buf;
1679 iov.iov_len = n << BDRV_SECTOR_BITS;
1680 qemu_iovec_init_external(&qiov, &iov, 1);
1681
1682 ret = blk_co_preadv(
1683 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1684 n << BDRV_SECTOR_BITS, &qiov, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001685 if (ret < 0) {
1686 return ret;
1687 }
1688
1689 sector_num += n;
1690 nb_sectors -= n;
1691 buf += n * BDRV_SECTOR_SIZE;
1692 }
1693
1694 return 0;
1695}
1696
Peter Lieven2d9187b2017-02-28 13:40:07 +01001697
1698static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1699 int nb_sectors, uint8_t *buf,
1700 enum ImgConvertBlockStatus status)
Kevin Wolf690c7302015-03-19 13:33:32 +01001701{
1702 int ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001703 QEMUIOVector qiov;
1704 struct iovec iov;
Kevin Wolf690c7302015-03-19 13:33:32 +01001705
1706 while (nb_sectors > 0) {
1707 int n = nb_sectors;
Lidong Chendb933fb2017-04-27 10:58:27 +08001708 BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1709
Peter Lieven2d9187b2017-02-28 13:40:07 +01001710 switch (status) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001711 case BLK_BACKING_FILE:
1712 /* If we have a backing file, leave clusters unallocated that are
1713 * unallocated in the source image, so that the backing file is
1714 * visible at the respective offset. */
1715 assert(s->target_has_backing);
1716 break;
1717
1718 case BLK_DATA:
Lidong Chendb933fb2017-04-27 10:58:27 +08001719 /* If we're told to keep the target fully allocated (-S 0) or there
1720 * is real non-zero data, we must write it. Otherwise we can treat
1721 * it as zero sectors.
1722 * Compressed clusters need to be written as a whole, so in that
1723 * case we can only save the write if the buffer is completely
1724 * zeroed. */
Kevin Wolf690c7302015-03-19 13:33:32 +01001725 if (!s->min_sparse ||
Lidong Chendb933fb2017-04-27 10:58:27 +08001726 (!s->compressed &&
1727 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1728 (s->compressed &&
1729 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
Kevin Wolf690c7302015-03-19 13:33:32 +01001730 {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001731 iov.iov_base = buf;
1732 iov.iov_len = n << BDRV_SECTOR_BITS;
1733 qemu_iovec_init_external(&qiov, &iov, 1);
1734
1735 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
Lidong Chendb933fb2017-04-27 10:58:27 +08001736 n << BDRV_SECTOR_BITS, &qiov, flags);
Kevin Wolf690c7302015-03-19 13:33:32 +01001737 if (ret < 0) {
1738 return ret;
1739 }
1740 break;
1741 }
1742 /* fall-through */
1743
1744 case BLK_ZERO:
1745 if (s->has_zero_init) {
Lidong Chendb933fb2017-04-27 10:58:27 +08001746 assert(!s->target_has_backing);
Kevin Wolf690c7302015-03-19 13:33:32 +01001747 break;
1748 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001749 ret = blk_co_pwrite_zeroes(s->target,
1750 sector_num << BDRV_SECTOR_BITS,
1751 n << BDRV_SECTOR_BITS, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001752 if (ret < 0) {
1753 return ret;
1754 }
1755 break;
1756 }
1757
1758 sector_num += n;
1759 nb_sectors -= n;
1760 buf += n * BDRV_SECTOR_SIZE;
1761 }
1762
1763 return 0;
1764}
1765
Fam Zhengee5306d2018-06-01 17:26:48 +08001766static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num,
1767 int nb_sectors)
1768{
1769 int n, ret;
1770
1771 while (nb_sectors > 0) {
1772 BlockBackend *blk;
1773 int src_cur;
1774 int64_t bs_sectors, src_cur_offset;
1775 int64_t offset;
1776
1777 convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1778 offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS;
1779 blk = s->src[src_cur];
1780 bs_sectors = s->src_sectors[src_cur];
1781
1782 n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1783
1784 ret = blk_co_copy_range(blk, offset, s->target,
1785 sector_num << BDRV_SECTOR_BITS,
Vladimir Sementsov-Ogievskiy67b51fb2018-07-09 19:37:17 +03001786 n << BDRV_SECTOR_BITS, 0, 0);
Fam Zhengee5306d2018-06-01 17:26:48 +08001787 if (ret < 0) {
1788 return ret;
1789 }
1790
1791 sector_num += n;
1792 nb_sectors -= n;
1793 }
1794 return 0;
1795}
1796
Peter Lieven2d9187b2017-02-28 13:40:07 +01001797static void coroutine_fn convert_co_do_copy(void *opaque)
1798{
1799 ImgConvertState *s = opaque;
1800 uint8_t *buf = NULL;
1801 int ret, i;
1802 int index = -1;
1803
1804 for (i = 0; i < s->num_coroutines; i++) {
1805 if (s->co[i] == qemu_coroutine_self()) {
1806 index = i;
1807 break;
1808 }
1809 }
1810 assert(index >= 0);
1811
1812 s->running_coroutines++;
1813 buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1814
1815 while (1) {
1816 int n;
1817 int64_t sector_num;
1818 enum ImgConvertBlockStatus status;
Fam Zhengee5306d2018-06-01 17:26:48 +08001819 bool copy_range;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001820
1821 qemu_co_mutex_lock(&s->lock);
1822 if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1823 qemu_co_mutex_unlock(&s->lock);
Anton Nefedovb91127e2017-04-26 11:33:15 +03001824 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001825 }
1826 n = convert_iteration_sectors(s, s->sector_num);
1827 if (n < 0) {
1828 qemu_co_mutex_unlock(&s->lock);
1829 s->ret = n;
Anton Nefedovb91127e2017-04-26 11:33:15 +03001830 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001831 }
1832 /* save current sector and allocation status to local variables */
1833 sector_num = s->sector_num;
1834 status = s->status;
1835 if (!s->min_sparse && s->status == BLK_ZERO) {
1836 n = MIN(n, s->buf_sectors);
1837 }
1838 /* increment global sector counter so that other coroutines can
1839 * already continue reading beyond this request */
1840 s->sector_num += n;
1841 qemu_co_mutex_unlock(&s->lock);
1842
1843 if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1844 s->allocated_done += n;
1845 qemu_progress_print(100.0 * s->allocated_done /
1846 s->allocated_sectors, 0);
1847 }
1848
Fam Zhengee5306d2018-06-01 17:26:48 +08001849retry:
1850 copy_range = s->copy_range && s->status == BLK_DATA;
1851 if (status == BLK_DATA && !copy_range) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001852 ret = convert_co_read(s, sector_num, n, buf);
1853 if (ret < 0) {
1854 error_report("error while reading sector %" PRId64
1855 ": %s", sector_num, strerror(-ret));
1856 s->ret = ret;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001857 }
1858 } else if (!s->min_sparse && status == BLK_ZERO) {
1859 status = BLK_DATA;
1860 memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1861 }
1862
1863 if (s->wr_in_order) {
1864 /* keep writes in order */
Anton Nefedovb91127e2017-04-26 11:33:15 +03001865 while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001866 s->wait_sector_num[index] = sector_num;
1867 qemu_coroutine_yield();
1868 }
1869 s->wait_sector_num[index] = -1;
1870 }
1871
Anton Nefedovb91127e2017-04-26 11:33:15 +03001872 if (s->ret == -EINPROGRESS) {
Fam Zhengee5306d2018-06-01 17:26:48 +08001873 if (copy_range) {
1874 ret = convert_co_copy_range(s, sector_num, n);
1875 if (ret) {
1876 s->copy_range = false;
1877 goto retry;
1878 }
1879 } else {
1880 ret = convert_co_write(s, sector_num, n, buf, status);
1881 }
Anton Nefedovb91127e2017-04-26 11:33:15 +03001882 if (ret < 0) {
1883 error_report("error while writing sector %" PRId64
1884 ": %s", sector_num, strerror(-ret));
1885 s->ret = ret;
1886 }
Peter Lieven2d9187b2017-02-28 13:40:07 +01001887 }
1888
1889 if (s->wr_in_order) {
1890 /* reenter the coroutine that might have waited
1891 * for this write to complete */
1892 s->wr_offs = sector_num + n;
1893 for (i = 0; i < s->num_coroutines; i++) {
1894 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1895 /*
1896 * A -> B -> A cannot occur because A has
1897 * s->wait_sector_num[i] == -1 during A -> B. Therefore
1898 * B will never enter A during this time window.
1899 */
1900 qemu_coroutine_enter(s->co[i]);
1901 break;
1902 }
1903 }
1904 }
1905 }
1906
Peter Lieven2d9187b2017-02-28 13:40:07 +01001907 qemu_vfree(buf);
1908 s->co[index] = NULL;
1909 s->running_coroutines--;
1910 if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1911 /* the convert job finished successfully */
1912 s->ret = 0;
1913 }
1914}
1915
Kevin Wolf690c7302015-03-19 13:33:32 +01001916static int convert_do_copy(ImgConvertState *s)
1917{
Peter Lieven2d9187b2017-02-28 13:40:07 +01001918 int ret, i, n;
1919 int64_t sector_num = 0;
Kevin Wolf690c7302015-03-19 13:33:32 +01001920
1921 /* Check whether we have zero initialisation or can get it efficiently */
1922 s->has_zero_init = s->min_sparse && !s->target_has_backing
1923 ? bdrv_has_zero_init(blk_bs(s->target))
1924 : false;
1925
1926 if (!s->has_zero_init && !s->target_has_backing &&
1927 bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1928 {
Kevin Wolf720ff282016-06-16 15:13:15 +02001929 ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
Kevin Wolf690c7302015-03-19 13:33:32 +01001930 if (ret == 0) {
1931 s->has_zero_init = true;
1932 }
1933 }
1934
1935 /* Allocate buffer for copied data. For compressed images, only one cluster
1936 * can be copied at a time. */
1937 if (s->compressed) {
1938 if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1939 error_report("invalid cluster size");
Peter Lieven2d9187b2017-02-28 13:40:07 +01001940 return -EINVAL;
Kevin Wolf690c7302015-03-19 13:33:32 +01001941 }
1942 s->buf_sectors = s->cluster_sectors;
1943 }
Kevin Wolf690c7302015-03-19 13:33:32 +01001944
Kevin Wolf690c7302015-03-19 13:33:32 +01001945 while (sector_num < s->total_sectors) {
1946 n = convert_iteration_sectors(s, sector_num);
1947 if (n < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001948 return n;
Kevin Wolf690c7302015-03-19 13:33:32 +01001949 }
Max Reitzaad15de2016-03-24 23:33:57 +01001950 if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1951 {
Kevin Wolf690c7302015-03-19 13:33:32 +01001952 s->allocated_sectors += n;
1953 }
1954 sector_num += n;
1955 }
1956
1957 /* Do the copy */
Kevin Wolf690c7302015-03-19 13:33:32 +01001958 s->sector_next_status = 0;
Peter Lieven2d9187b2017-02-28 13:40:07 +01001959 s->ret = -EINPROGRESS;
Kevin Wolf690c7302015-03-19 13:33:32 +01001960
Peter Lieven2d9187b2017-02-28 13:40:07 +01001961 qemu_co_mutex_init(&s->lock);
1962 for (i = 0; i < s->num_coroutines; i++) {
1963 s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1964 s->wait_sector_num[i] = -1;
1965 qemu_coroutine_enter(s->co[i]);
Kevin Wolf690c7302015-03-19 13:33:32 +01001966 }
1967
Anton Nefedovb91127e2017-04-26 11:33:15 +03001968 while (s->running_coroutines) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001969 main_loop_wait(false);
1970 }
1971
1972 if (s->compressed && !s->ret) {
Kevin Wolf690c7302015-03-19 13:33:32 +01001973 /* signal EOF to align */
Pavel Butsykinfe5c1352016-07-22 11:17:40 +03001974 ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
Kevin Wolf690c7302015-03-19 13:33:32 +01001975 if (ret < 0) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01001976 return ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001977 }
1978 }
1979
Peter Lieven2d9187b2017-02-28 13:40:07 +01001980 return s->ret;
Kevin Wolf690c7302015-03-19 13:33:32 +01001981}
1982
bellardea2384d2004-08-01 21:59:26 +00001983static int img_convert(int argc, char **argv)
1984{
Peter Lieven9fd77f92017-04-21 11:11:55 +02001985 int c, bs_i, flags, src_flags = 0;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001986 const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
Peter Lieven9fd77f92017-04-21 11:11:55 +02001987 *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1988 *out_filename, *out_baseimg_param, *snapshot_name = NULL;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001989 BlockDriver *drv = NULL, *proto_drv = NULL;
bellardfaea38e2006-08-05 21:31:00 +00001990 BlockDriverInfo bdi;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001991 BlockDriverState *out_bs;
1992 QemuOpts *opts = NULL, *sn_opts = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08001993 QemuOptsList *create_opts = NULL;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001994 char *options = NULL;
Max Reitzcc84d902013-09-06 17:14:26 +02001995 Error *local_err = NULL;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001996 bool writethrough, src_writethrough, quiet = false, image_opts = false,
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01001997 skip_create = false, progress = false, tgt_image_opts = false;
Peter Lieven9fd77f92017-04-21 11:11:55 +02001998 int64_t ret = -EINVAL;
Fam Zheng335e9932017-05-03 00:35:39 +08001999 bool force_share = false;
bellardea2384d2004-08-01 21:59:26 +00002000
Peter Lieven9fd77f92017-04-21 11:11:55 +02002001 ImgConvertState s = (ImgConvertState) {
2002 /* Need at least 4k of zeros for sparse detection */
2003 .min_sparse = 8,
Fam Zhengee5306d2018-06-01 17:26:48 +08002004 .copy_range = true,
Peter Lieven9fd77f92017-04-21 11:11:55 +02002005 .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
2006 .wr_in_order = true,
2007 .num_coroutines = 8,
2008 };
2009
bellardea2384d2004-08-01 21:59:26 +00002010 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002011 static const struct option long_options[] = {
2012 {"help", no_argument, 0, 'h'},
2013 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002014 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002015 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002016 {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002017 {0, 0, 0, 0}
2018 };
Thomas Huth46e8d272018-06-06 14:35:51 +02002019 c = getopt_long(argc, argv, ":hf:O:B:co:l:S:pt:T:qnm:WU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002020 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002021 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002022 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002023 }
bellardea2384d2004-08-01 21:59:26 +00002024 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002025 case ':':
2026 missing_argument(argv[optind - 1]);
2027 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002028 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002029 unrecognized_option(argv[optind - 1]);
2030 break;
bellardea2384d2004-08-01 21:59:26 +00002031 case 'h':
2032 help();
2033 break;
2034 case 'f':
2035 fmt = optarg;
2036 break;
2037 case 'O':
2038 out_fmt = optarg;
2039 break;
thsf58c7b32008-06-05 21:53:49 +00002040 case 'B':
2041 out_baseimg = optarg;
2042 break;
bellardea2384d2004-08-01 21:59:26 +00002043 case 'c':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002044 s.compressed = true;
Fam Zhengee5306d2018-06-01 17:26:48 +08002045 s.copy_range = false;
bellardea2384d2004-08-01 21:59:26 +00002046 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002047 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01002048 if (!is_valid_option_list(optarg)) {
2049 error_report("Invalid option list: %s", optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002050 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01002051 }
2052 if (!options) {
2053 options = g_strdup(optarg);
2054 } else {
2055 char *old_options = options;
2056 options = g_strdup_printf("%s,%s", options, optarg);
2057 g_free(old_options);
2058 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002059 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08002060 case 'l':
2061 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +01002062 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2063 optarg, false);
Wenchao Xiaef806542013-12-04 17:10:57 +08002064 if (!sn_opts) {
2065 error_report("Failed in parsing snapshot param '%s'",
2066 optarg);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002067 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08002068 }
2069 } else {
2070 snapshot_name = optarg;
2071 }
2072 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002073 case 'S':
2074 {
2075 int64_t sval;
Markus Armbruster606caa02017-02-21 21:14:04 +01002076
2077 sval = cvtnum(optarg);
2078 if (sval < 0) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02002079 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002080 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002081 }
2082
Peter Lieven9fd77f92017-04-21 11:11:55 +02002083 s.min_sparse = sval / BDRV_SECTOR_SIZE;
Fam Zhengee5306d2018-06-01 17:26:48 +08002084 s.copy_range = false;
Kevin Wolfa22f1232011-08-26 15:27:13 +02002085 break;
2086 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002087 case 'p':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002088 progress = true;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002089 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002090 case 't':
2091 cache = optarg;
2092 break;
Max Reitz40055952014-07-22 22:58:42 +02002093 case 'T':
2094 src_cache = optarg;
2095 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002096 case 'q':
2097 quiet = true;
2098 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002099 case 'n':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002100 skip_create = true;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002101 break;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002102 case 'm':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002103 if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2104 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
Peter Lieven2d9187b2017-02-28 13:40:07 +01002105 error_report("Invalid number of coroutines. Allowed number of"
2106 " coroutines is between 1 and %d", MAX_COROUTINES);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002107 goto fail_getopt;
2108 }
2109 break;
2110 case 'W':
Peter Lieven9fd77f92017-04-21 11:11:55 +02002111 s.wr_in_order = false;
Peter Lieven2d9187b2017-02-28 13:40:07 +01002112 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002113 case 'U':
2114 force_share = true;
2115 break;
Max Reitz3258b912017-04-26 15:46:47 +02002116 case OPTION_OBJECT: {
2117 QemuOpts *object_opts;
2118 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2119 optarg, true);
2120 if (!object_opts) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002121 goto fail_getopt;
2122 }
2123 break;
Max Reitz3258b912017-04-26 15:46:47 +02002124 }
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002125 case OPTION_IMAGE_OPTS:
2126 image_opts = true;
2127 break;
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002128 case OPTION_TARGET_IMAGE_OPTS:
2129 tgt_image_opts = true;
2130 break;
bellardea2384d2004-08-01 21:59:26 +00002131 }
2132 }
ths3b46e622007-09-17 08:09:54 +00002133
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002134 if (!out_fmt && !tgt_image_opts) {
2135 out_fmt = "raw";
2136 }
2137
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002138 if (qemu_opts_foreach(&qemu_object_opts,
2139 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002140 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002141 goto fail_getopt;
2142 }
2143
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002144 if (tgt_image_opts && !skip_create) {
2145 error_report("--target-image-opts requires use of -n flag");
2146 goto fail_getopt;
2147 }
2148
Peter Lieven9fd77f92017-04-21 11:11:55 +02002149 s.src_num = argc - optind - 1;
2150 out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2151
2152 if (options && has_help_option(options)) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002153 if (out_fmt) {
2154 ret = print_block_option_help(out_filename, out_fmt);
2155 goto fail_getopt;
2156 } else {
2157 error_report("Option help requires a format be specified");
2158 goto fail_getopt;
2159 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002160 }
2161
2162 if (s.src_num < 1) {
2163 error_report("Must specify image file name");
2164 goto fail_getopt;
2165 }
2166
2167
Peter Lieven9fd77f92017-04-21 11:11:55 +02002168 /* ret is still -EINVAL until here */
2169 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2170 if (ret < 0) {
2171 error_report("Invalid source cache option: %s", src_cache);
Peter Lieven2d9187b2017-02-28 13:40:07 +01002172 goto fail_getopt;
2173 }
2174
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002175 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002176 if (quiet) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002177 progress = false;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002178 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002179 qemu_progress_init(progress, 1.0);
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002180 qemu_progress_print(0, 100);
2181
Peter Lieven9fd77f92017-04-21 11:11:55 +02002182 s.src = g_new0(BlockBackend *, s.src_num);
2183 s.src_sectors = g_new(int64_t, s.src_num);
balrog926c2d22007-10-31 01:11:44 +00002184
Peter Lieven9fd77f92017-04-21 11:11:55 +02002185 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2186 s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
Fam Zheng335e9932017-05-03 00:35:39 +08002187 fmt, src_flags, src_writethrough, quiet,
2188 force_share);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002189 if (!s.src[bs_i]) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002190 ret = -1;
2191 goto out;
2192 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002193 s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2194 if (s.src_sectors[bs_i] < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002195 error_report("Could not get size of %s: %s",
Peter Lieven9fd77f92017-04-21 11:11:55 +02002196 argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002197 ret = -1;
2198 goto out;
2199 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002200 s.total_sectors += s.src_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00002201 }
bellardea2384d2004-08-01 21:59:26 +00002202
Wenchao Xiaef806542013-12-04 17:10:57 +08002203 if (sn_opts) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002204 bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
Peter Maydell10d6eda2017-02-10 16:28:24 +00002205 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2206 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2207 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002208 } else if (snapshot_name != NULL) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002209 if (s.src_num > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02002210 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07002211 ret = -1;
2212 goto out;
2213 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08002214
Peter Lieven9fd77f92017-04-21 11:11:55 +02002215 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2216 &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08002217 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01002218 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002219 error_reportf_err(local_err, "Failed to load snapshot: ");
Wenchao Xiaef806542013-12-04 17:10:57 +08002220 ret = -1;
2221 goto out;
edison51ef6722010-09-21 19:58:41 -07002222 }
2223
Max Reitz2e024cd2015-02-11 09:58:46 -05002224 if (!skip_create) {
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002225 /* Find driver and parse its options */
2226 drv = bdrv_find_format(out_fmt);
2227 if (!drv) {
2228 error_report("Unknown file format '%s'", out_fmt);
2229 ret = -1;
2230 goto out;
2231 }
2232
2233 proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2234 if (!proto_drv) {
2235 error_report_err(local_err);
2236 ret = -1;
2237 goto out;
2238 }
2239
Max Reitz2e024cd2015-02-11 09:58:46 -05002240 if (!drv->create_opts) {
2241 error_report("Format driver '%s' does not support image creation",
2242 drv->format_name);
2243 ret = -1;
2244 goto out;
2245 }
Max Reitzf75613c2014-12-02 18:32:46 +01002246
Max Reitz2e024cd2015-02-11 09:58:46 -05002247 if (!proto_drv->create_opts) {
2248 error_report("Protocol driver '%s' does not support image creation",
2249 proto_drv->format_name);
2250 ret = -1;
2251 goto out;
2252 }
Max Reitzf75613c2014-12-02 18:32:46 +01002253
Max Reitz2e024cd2015-02-11 09:58:46 -05002254 create_opts = qemu_opts_append(create_opts, drv->create_opts);
2255 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02002256
Max Reitz2e024cd2015-02-11 09:58:46 -05002257 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002258 if (options) {
2259 qemu_opts_do_parse(opts, options, NULL, &local_err);
2260 if (local_err) {
Markus Armbruster97a2ca72015-03-14 10:23:15 +01002261 error_report_err(local_err);
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01002262 ret = -1;
2263 goto out;
2264 }
Max Reitz2e024cd2015-02-11 09:58:46 -05002265 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002266
Peter Lieven9fd77f92017-04-21 11:11:55 +02002267 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
Markus Armbruster39101f22015-02-12 16:46:36 +01002268 &error_abort);
Max Reitz2e024cd2015-02-11 09:58:46 -05002269 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2270 if (ret < 0) {
2271 goto out;
2272 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002273 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002274
Kevin Wolfa18953f2010-10-14 15:46:04 +02002275 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08002276 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02002277 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002278 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002279 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002280 s.target_has_backing = (bool) out_baseimg;
Kevin Wolfa18953f2010-10-14 15:46:04 +02002281
Max Reitz48758a82017-04-26 15:46:48 +02002282 if (s.src_num > 1 && out_baseimg) {
2283 error_report("Having a backing file for the target makes no sense when "
2284 "concatenating multiple input images");
2285 ret = -1;
2286 goto out;
2287 }
2288
Kevin Wolfefa84d42009-05-18 16:42:12 +02002289 /* Check if compression is supported */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002290 if (s.compressed) {
Chunyan Liu83d05212014-06-05 17:20:51 +08002291 bool encryption =
2292 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002293 const char *encryptfmt =
2294 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
Chunyan Liu83d05212014-06-05 17:20:51 +08002295 const char *preallocation =
2296 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02002297
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002298 if (drv && !drv->bdrv_co_pwritev_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002299 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002300 ret = -1;
2301 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002302 }
2303
Daniel P. Berrange0cb8d472017-06-23 17:24:06 +01002304 if (encryption || encryptfmt) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002305 error_report("Compression and encryption not supported at "
2306 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002307 ret = -1;
2308 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02002309 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02002310
Chunyan Liu83d05212014-06-05 17:20:51 +08002311 if (preallocation
2312 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02002313 {
2314 error_report("Compression and preallocation not supported at "
2315 "the same time");
2316 ret = -1;
2317 goto out;
2318 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02002319 }
2320
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002321 if (!skip_create) {
2322 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002323 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002324 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01002325 error_reportf_err(local_err, "%s: error while converting %s: ",
2326 out_filename, out_fmt);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002327 goto out;
bellardea2384d2004-08-01 21:59:26 +00002328 }
2329 }
ths3b46e622007-09-17 08:09:54 +00002330
Peter Lieven9fd77f92017-04-21 11:11:55 +02002331 flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Kevin Wolfce099542016-03-15 13:01:04 +01002332 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002333 if (ret < 0) {
2334 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02002335 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002336 }
2337
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002338 if (skip_create) {
2339 s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2340 flags, writethrough, quiet, false);
2341 } else {
2342 /* TODO ultimately we should allow --target-image-opts
2343 * to be used even when -n is not given.
2344 * That has to wait for bdrv_create to be improved
2345 * to allow filenames in option syntax
2346 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01002347 s.target = img_open_new_file(out_filename, opts, out_fmt,
2348 flags, writethrough, quiet, false);
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002349 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002350 if (!s.target) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002351 ret = -1;
2352 goto out;
2353 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002354 out_bs = blk_bs(s.target);
bellardea2384d2004-08-01 21:59:26 +00002355
Daniel P. Berrange305b4c62017-05-15 17:47:11 +01002356 if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2357 error_report("Compression not supported for this file format");
2358 ret = -1;
2359 goto out;
2360 }
2361
Eric Blake5def6b82016-06-23 16:37:19 -06002362 /* increase bufsectors from the default 4096 (2M) if opt_transfer
Peter Lievenf2521c92013-11-27 11:07:06 +01002363 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2364 * as maximum. */
Peter Lieven9fd77f92017-04-21 11:11:55 +02002365 s.buf_sectors = MIN(32768,
2366 MAX(s.buf_sectors,
2367 MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2368 out_bs->bl.pdiscard_alignment >>
2369 BDRV_SECTOR_BITS)));
Peter Lievenf2521c92013-11-27 11:07:06 +01002370
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002371 if (skip_create) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002372 int64_t output_sectors = blk_nb_sectors(s.target);
Markus Armbruster43716fa2014-06-26 13:23:21 +02002373 if (output_sectors < 0) {
Gongleieec5eb42015-02-25 12:22:27 +08002374 error_report("unable to get output image length: %s",
Markus Armbruster43716fa2014-06-26 13:23:21 +02002375 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002376 ret = -1;
2377 goto out;
Peter Lieven9fd77f92017-04-21 11:11:55 +02002378 } else if (output_sectors < s.total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01002379 error_report("output file is smaller than input file");
2380 ret = -1;
2381 goto out;
2382 }
2383 }
2384
Max Reitz351c8ef2018-05-01 18:57:49 +02002385 if (s.target_has_backing) {
2386 /* Errors are treated as "backing length unknown" (which means
2387 * s.target_backing_sectors has to be negative, which it will
2388 * be automatically). The backing file length is used only
2389 * for optimizations, so such a case is not fatal. */
2390 s.target_backing_sectors = bdrv_nb_sectors(out_bs->backing->bs);
2391 } else {
2392 s.target_backing_sectors = -1;
2393 }
2394
Peter Lieven24f833c2013-11-27 11:07:07 +01002395 ret = bdrv_get_info(out_bs, &bdi);
2396 if (ret < 0) {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002397 if (s.compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002398 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002399 goto out;
2400 }
Peter Lieven24f833c2013-11-27 11:07:07 +01002401 } else {
Peter Lieven9fd77f92017-04-21 11:11:55 +02002402 s.compressed = s.compressed || bdi.needs_compressed_writes;
2403 s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
Max Reitz351c8ef2018-05-01 18:57:49 +02002404 s.unallocated_blocks_are_zero = bdi.unallocated_blocks_are_zero;
Peter Lieven24f833c2013-11-27 11:07:07 +01002405 }
2406
Peter Lieven9fd77f92017-04-21 11:11:55 +02002407 ret = convert_do_copy(&s);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002408out:
Peter Lieven13c28af2013-11-27 11:07:01 +01002409 if (!ret) {
2410 qemu_progress_print(100, 0);
2411 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002412 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08002413 qemu_opts_del(opts);
2414 qemu_opts_free(create_opts);
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02002415 qemu_opts_del(sn_opts);
Peter Lieven9fd77f92017-04-21 11:11:55 +02002416 blk_unref(s.target);
2417 if (s.src) {
2418 for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2419 blk_unref(s.src[bs_i]);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002420 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002421 g_free(s.src);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002422 }
Peter Lieven9fd77f92017-04-21 11:11:55 +02002423 g_free(s.src_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01002424fail_getopt:
2425 g_free(options);
2426
Peter Lieven9fd77f92017-04-21 11:11:55 +02002427 return !!ret;
bellardea2384d2004-08-01 21:59:26 +00002428}
2429
bellard57d1a2b2004-08-03 21:15:11 +00002430
bellardfaea38e2006-08-05 21:31:00 +00002431static void dump_snapshots(BlockDriverState *bs)
2432{
2433 QEMUSnapshotInfo *sn_tab, *sn;
2434 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00002435
2436 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2437 if (nb_sns <= 0)
2438 return;
2439 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08002440 bdrv_snapshot_dump(fprintf, stdout, NULL);
2441 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002442 for(i = 0; i < nb_sns; i++) {
2443 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08002444 bdrv_snapshot_dump(fprintf, stdout, sn);
2445 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00002446 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002447 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00002448}
2449
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002450static void dump_json_image_info_list(ImageInfoList *list)
2451{
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002452 QString *str;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002453 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002454 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002455
2456 visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2457 visit_complete(v, &obj);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002458 str = qobject_to_json_pretty(obj);
2459 assert(str != NULL);
2460 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002461 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002462 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002463 qobject_unref(str);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002464}
2465
Benoît Canetc054b3f2012-09-05 13:09:02 +02002466static void dump_json_image_info(ImageInfo *info)
2467{
Benoît Canetc054b3f2012-09-05 13:09:02 +02002468 QString *str;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002469 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01002470 Visitor *v = qobject_output_visitor_new(&obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002471
2472 visit_type_ImageInfo(v, NULL, &info, &error_abort);
2473 visit_complete(v, &obj);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002474 str = qobject_to_json_pretty(obj);
2475 assert(str != NULL);
2476 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002477 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -06002478 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002479 qobject_unref(str);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002480}
2481
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002482static void dump_human_image_info_list(ImageInfoList *list)
2483{
2484 ImageInfoList *elem;
2485 bool delim = false;
2486
2487 for (elem = list; elem; elem = elem->next) {
2488 if (delim) {
2489 printf("\n");
2490 }
2491 delim = true;
2492
Wenchao Xia5b917042013-05-25 11:09:45 +08002493 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002494 }
2495}
2496
2497static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2498{
2499 return strcmp(a, b) == 0;
2500}
2501
2502/**
2503 * Open an image file chain and return an ImageInfoList
2504 *
2505 * @filename: topmost image filename
2506 * @fmt: topmost image format (may be NULL to autodetect)
2507 * @chain: true - enumerate entire backing file chain
2508 * false - only topmost image file
2509 *
2510 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2511 * image file. If there was an error a message will have been printed to
2512 * stderr.
2513 */
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002514static ImageInfoList *collect_image_info_list(bool image_opts,
2515 const char *filename,
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002516 const char *fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002517 bool chain, bool force_share)
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002518{
2519 ImageInfoList *head = NULL;
2520 ImageInfoList **last = &head;
2521 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08002522 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002523
2524 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2525
2526 while (filename) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02002527 BlockBackend *blk;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002528 BlockDriverState *bs;
2529 ImageInfo *info;
2530 ImageInfoList *elem;
2531
2532 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2533 error_report("Backing file '%s' creates an infinite loop.",
2534 filename);
2535 goto err;
2536 }
2537 g_hash_table_insert(filenames, (gpointer)filename, NULL);
2538
Max Reitzefaa7c42016-03-16 19:54:38 +01002539 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08002540 BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2541 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002542 if (!blk) {
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002543 goto err;
2544 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002545 bs = blk_bs(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002546
Wenchao Xia43526ec2013-06-06 12:27:58 +08002547 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002548 if (err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01002549 error_report_err(err);
Markus Armbruster26f54e92014-10-07 13:59:04 +02002550 blk_unref(blk);
Wenchao Xia43526ec2013-06-06 12:27:58 +08002551 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08002552 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002553
2554 elem = g_new0(ImageInfoList, 1);
2555 elem->value = info;
2556 *last = elem;
2557 last = &elem->next;
2558
Markus Armbruster26f54e92014-10-07 13:59:04 +02002559 blk_unref(blk);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002560
2561 filename = fmt = NULL;
2562 if (chain) {
2563 if (info->has_full_backing_filename) {
2564 filename = info->full_backing_filename;
2565 } else if (info->has_backing_filename) {
John Snow92d617a2015-12-14 14:55:15 -05002566 error_report("Could not determine absolute backing filename,"
2567 " but backing filename '%s' present",
2568 info->backing_filename);
2569 goto err;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002570 }
2571 if (info->has_backing_filename_format) {
2572 fmt = info->backing_filename_format;
2573 }
2574 }
2575 }
2576 g_hash_table_destroy(filenames);
2577 return head;
2578
2579err:
2580 qapi_free_ImageInfoList(head);
2581 g_hash_table_destroy(filenames);
2582 return NULL;
2583}
2584
Benoît Canetc054b3f2012-09-05 13:09:02 +02002585static int img_info(int argc, char **argv)
2586{
2587 int c;
2588 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002589 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002590 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002591 ImageInfoList *list;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002592 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002593 bool force_share = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002594
bellardea2384d2004-08-01 21:59:26 +00002595 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002596 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00002597 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02002598 int option_index = 0;
2599 static const struct option long_options[] = {
2600 {"help", no_argument, 0, 'h'},
2601 {"format", required_argument, 0, 'f'},
2602 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002603 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002604 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002605 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002606 {"force-share", no_argument, 0, 'U'},
Benoît Canetc054b3f2012-09-05 13:09:02 +02002607 {0, 0, 0, 0}
2608 };
Fam Zheng335e9932017-05-03 00:35:39 +08002609 c = getopt_long(argc, argv, ":f:hU",
Benoît Canetc054b3f2012-09-05 13:09:02 +02002610 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002611 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00002612 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002613 }
bellardea2384d2004-08-01 21:59:26 +00002614 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002615 case ':':
2616 missing_argument(argv[optind - 1]);
2617 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002618 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002619 unrecognized_option(argv[optind - 1]);
2620 break;
bellardea2384d2004-08-01 21:59:26 +00002621 case 'h':
2622 help();
2623 break;
2624 case 'f':
2625 fmt = optarg;
2626 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002627 case 'U':
2628 force_share = true;
2629 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02002630 case OPTION_OUTPUT:
2631 output = optarg;
2632 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002633 case OPTION_BACKING_CHAIN:
2634 chain = true;
2635 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002636 case OPTION_OBJECT: {
2637 QemuOpts *opts;
2638 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2639 optarg, true);
2640 if (!opts) {
2641 return 1;
2642 }
2643 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002644 case OPTION_IMAGE_OPTS:
2645 image_opts = true;
2646 break;
bellardea2384d2004-08-01 21:59:26 +00002647 }
2648 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002649 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002650 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002651 }
bellardea2384d2004-08-01 21:59:26 +00002652 filename = argv[optind++];
2653
Benoît Canetc054b3f2012-09-05 13:09:02 +02002654 if (output && !strcmp(output, "json")) {
2655 output_format = OFORMAT_JSON;
2656 } else if (output && !strcmp(output, "human")) {
2657 output_format = OFORMAT_HUMAN;
2658 } else if (output) {
2659 error_report("--output must be used with human or json as argument.");
2660 return 1;
2661 }
2662
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002663 if (qemu_opts_foreach(&qemu_object_opts,
2664 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002665 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002666 return 1;
2667 }
2668
Fam Zheng335e9932017-05-03 00:35:39 +08002669 list = collect_image_info_list(image_opts, filename, fmt, chain,
2670 force_share);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002671 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002672 return 1;
2673 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002674
Benoît Canetc054b3f2012-09-05 13:09:02 +02002675 switch (output_format) {
2676 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002677 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02002678 break;
2679 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002680 if (chain) {
2681 dump_json_image_info_list(list);
2682 } else {
2683 dump_json_image_info(list->value);
2684 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002685 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002686 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02002687
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02002688 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00002689 return 0;
2690}
2691
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002692static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2693 MapEntry *next)
2694{
2695 switch (output_format) {
2696 case OFORMAT_HUMAN:
Fam Zheng16b0d552016-01-26 11:59:02 +08002697 if (e->data && !e->has_offset) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002698 error_report("File contains external, encrypted or compressed clusters.");
2699 exit(1);
2700 }
Fam Zheng16b0d552016-01-26 11:59:02 +08002701 if (e->data && !e->zero) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002702 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
Fam Zheng16b0d552016-01-26 11:59:02 +08002703 e->start, e->length,
2704 e->has_offset ? e->offset : 0,
2705 e->has_filename ? e->filename : "");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002706 }
2707 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2708 * Modify the flags here to allow more coalescing.
2709 */
Fam Zheng16b0d552016-01-26 11:59:02 +08002710 if (next && (!next->data || next->zero)) {
2711 next->data = false;
2712 next->zero = true;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002713 }
2714 break;
2715 case OFORMAT_JSON:
Fam Zheng16b0d552016-01-26 11:59:02 +08002716 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2717 " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002718 (e->start == 0 ? "[" : ",\n"),
2719 e->start, e->length, e->depth,
Fam Zheng16b0d552016-01-26 11:59:02 +08002720 e->zero ? "true" : "false",
2721 e->data ? "true" : "false");
2722 if (e->has_offset) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002723 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002724 }
2725 putchar('}');
2726
2727 if (!next) {
2728 printf("]\n");
2729 }
2730 break;
2731 }
2732}
2733
Eric Blake5e344dd2017-10-11 22:47:02 -05002734static int get_block_status(BlockDriverState *bs, int64_t offset,
2735 int64_t bytes, MapEntry *e)
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002736{
Eric Blake237d78f2017-10-11 22:47:03 -05002737 int ret;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002738 int depth;
Fam Zheng67a0fd22016-01-26 11:58:48 +08002739 BlockDriverState *file;
John Snow28756452016-02-05 13:12:33 -05002740 bool has_offset;
Eric Blake237d78f2017-10-11 22:47:03 -05002741 int64_t map;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002742
2743 /* As an optimization, we could cache the current range of unallocated
2744 * clusters in each file of the chain, and avoid querying the same
2745 * range repeatedly.
2746 */
2747
2748 depth = 0;
2749 for (;;) {
Eric Blake237d78f2017-10-11 22:47:03 -05002750 ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002751 if (ret < 0) {
2752 return ret;
2753 }
Eric Blake237d78f2017-10-11 22:47:03 -05002754 assert(bytes);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002755 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2756 break;
2757 }
Kevin Wolf760e0062015-06-17 14:55:21 +02002758 bs = backing_bs(bs);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002759 if (bs == NULL) {
2760 ret = 0;
2761 break;
2762 }
2763
2764 depth++;
2765 }
2766
John Snow28756452016-02-05 13:12:33 -05002767 has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2768
2769 *e = (MapEntry) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002770 .start = offset,
Eric Blake237d78f2017-10-11 22:47:03 -05002771 .length = bytes,
John Snow28756452016-02-05 13:12:33 -05002772 .data = !!(ret & BDRV_BLOCK_DATA),
2773 .zero = !!(ret & BDRV_BLOCK_ZERO),
Eric Blake237d78f2017-10-11 22:47:03 -05002774 .offset = map,
John Snow28756452016-02-05 13:12:33 -05002775 .has_offset = has_offset,
2776 .depth = depth,
2777 .has_filename = file && has_offset,
2778 .filename = file && has_offset ? file->filename : NULL,
2779 };
2780
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002781 return 0;
2782}
2783
Fam Zheng16b0d552016-01-26 11:59:02 +08002784static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2785{
2786 if (curr->length == 0) {
2787 return false;
2788 }
2789 if (curr->zero != next->zero ||
2790 curr->data != next->data ||
2791 curr->depth != next->depth ||
2792 curr->has_filename != next->has_filename ||
2793 curr->has_offset != next->has_offset) {
2794 return false;
2795 }
2796 if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2797 return false;
2798 }
2799 if (curr->has_offset && curr->offset + curr->length != next->offset) {
2800 return false;
2801 }
2802 return true;
2803}
2804
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002805static int img_map(int argc, char **argv)
2806{
2807 int c;
2808 OutputFormat output_format = OFORMAT_HUMAN;
Markus Armbruster26f54e92014-10-07 13:59:04 +02002809 BlockBackend *blk;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002810 BlockDriverState *bs;
2811 const char *filename, *fmt, *output;
2812 int64_t length;
2813 MapEntry curr = { .length = 0 }, next;
2814 int ret = 0;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002815 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002816 bool force_share = false;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002817
2818 fmt = NULL;
2819 output = NULL;
2820 for (;;) {
2821 int option_index = 0;
2822 static const struct option long_options[] = {
2823 {"help", no_argument, 0, 'h'},
2824 {"format", required_argument, 0, 'f'},
2825 {"output", required_argument, 0, OPTION_OUTPUT},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002826 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002827 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002828 {"force-share", no_argument, 0, 'U'},
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002829 {0, 0, 0, 0}
2830 };
Fam Zheng335e9932017-05-03 00:35:39 +08002831 c = getopt_long(argc, argv, ":f:hU",
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002832 long_options, &option_index);
2833 if (c == -1) {
2834 break;
2835 }
2836 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002837 case ':':
2838 missing_argument(argv[optind - 1]);
2839 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002840 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002841 unrecognized_option(argv[optind - 1]);
2842 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002843 case 'h':
2844 help();
2845 break;
2846 case 'f':
2847 fmt = optarg;
2848 break;
Fam Zheng335e9932017-05-03 00:35:39 +08002849 case 'U':
2850 force_share = true;
2851 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002852 case OPTION_OUTPUT:
2853 output = optarg;
2854 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002855 case OPTION_OBJECT: {
2856 QemuOpts *opts;
2857 opts = qemu_opts_parse_noisily(&qemu_object_opts,
2858 optarg, true);
2859 if (!opts) {
2860 return 1;
2861 }
2862 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002863 case OPTION_IMAGE_OPTS:
2864 image_opts = true;
2865 break;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002866 }
2867 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002868 if (optind != argc - 1) {
2869 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002870 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002871 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002872
2873 if (output && !strcmp(output, "json")) {
2874 output_format = OFORMAT_JSON;
2875 } else if (output && !strcmp(output, "human")) {
2876 output_format = OFORMAT_HUMAN;
2877 } else if (output) {
2878 error_report("--output must be used with human or json as argument.");
2879 return 1;
2880 }
2881
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002882 if (qemu_opts_foreach(&qemu_object_opts,
2883 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02002884 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002885 return 1;
2886 }
2887
Fam Zheng335e9932017-05-03 00:35:39 +08002888 blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002889 if (!blk) {
2890 return 1;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002891 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002892 bs = blk_bs(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002893
2894 if (output_format == OFORMAT_HUMAN) {
2895 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2896 }
2897
Max Reitzf1d3cd72015-02-05 13:58:18 -05002898 length = blk_getlength(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002899 while (curr.start + curr.length < length) {
Eric Blake5e344dd2017-10-11 22:47:02 -05002900 int64_t offset = curr.start + curr.length;
2901 int64_t n;
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002902
2903 /* Probe up to 1 GiB at a time. */
Eric Blakee0b371e2018-06-11 16:39:26 -05002904 n = MIN(1 << 30, length - offset);
Eric Blake5e344dd2017-10-11 22:47:02 -05002905 ret = get_block_status(bs, offset, n, &next);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002906
2907 if (ret < 0) {
2908 error_report("Could not read file metadata: %s", strerror(-ret));
2909 goto out;
2910 }
2911
Fam Zheng16b0d552016-01-26 11:59:02 +08002912 if (entry_mergeable(&curr, &next)) {
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002913 curr.length += next.length;
2914 continue;
2915 }
2916
2917 if (curr.length > 0) {
2918 dump_map_entry(output_format, &curr, &next);
2919 }
2920 curr = next;
2921 }
2922
2923 dump_map_entry(output_format, &curr, NULL);
2924
2925out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02002926 blk_unref(blk);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002927 return ret < 0;
2928}
2929
aliguorif7b4a942009-01-07 17:40:15 +00002930#define SNAPSHOT_LIST 1
2931#define SNAPSHOT_CREATE 2
2932#define SNAPSHOT_APPLY 3
2933#define SNAPSHOT_DELETE 4
2934
Stuart Brady153859b2009-06-07 00:42:17 +01002935static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002936{
Markus Armbruster26f54e92014-10-07 13:59:04 +02002937 BlockBackend *blk;
aliguorif7b4a942009-01-07 17:40:15 +00002938 BlockDriverState *bs;
2939 QEMUSnapshotInfo sn;
2940 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002941 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002942 int action = 0;
2943 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002944 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002945 Error *err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002946 bool image_opts = false;
Fam Zheng335e9932017-05-03 00:35:39 +08002947 bool force_share = false;
aliguorif7b4a942009-01-07 17:40:15 +00002948
Kevin Wolfce099542016-03-15 13:01:04 +01002949 bdrv_oflags = BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002950 /* Parse commandline parameters */
2951 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002952 static const struct option long_options[] = {
2953 {"help", no_argument, 0, 'h'},
2954 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00002955 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08002956 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002957 {0, 0, 0, 0}
2958 };
Fam Zheng335e9932017-05-03 00:35:39 +08002959 c = getopt_long(argc, argv, ":la:c:d:hqU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00002960 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002961 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002962 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002963 }
aliguorif7b4a942009-01-07 17:40:15 +00002964 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002965 case ':':
2966 missing_argument(argv[optind - 1]);
2967 break;
Jes Sorensenef873942010-12-06 15:25:40 +01002968 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08002969 unrecognized_option(argv[optind - 1]);
2970 break;
aliguorif7b4a942009-01-07 17:40:15 +00002971 case 'h':
2972 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002973 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002974 case 'l':
2975 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002976 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002977 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002978 }
2979 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002980 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002981 break;
2982 case 'a':
2983 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002984 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002985 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002986 }
2987 action = SNAPSHOT_APPLY;
2988 snapshot_name = optarg;
2989 break;
2990 case 'c':
2991 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002992 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002993 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002994 }
2995 action = SNAPSHOT_CREATE;
2996 snapshot_name = optarg;
2997 break;
2998 case 'd':
2999 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003000 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01003001 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003002 }
3003 action = SNAPSHOT_DELETE;
3004 snapshot_name = optarg;
3005 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003006 case 'q':
3007 quiet = true;
3008 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003009 case 'U':
3010 force_share = true;
3011 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003012 case OPTION_OBJECT: {
3013 QemuOpts *opts;
3014 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3015 optarg, true);
3016 if (!opts) {
3017 return 1;
3018 }
3019 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003020 case OPTION_IMAGE_OPTS:
3021 image_opts = true;
3022 break;
aliguorif7b4a942009-01-07 17:40:15 +00003023 }
3024 }
3025
Kevin Wolffc11eb22013-08-05 10:53:04 +02003026 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003027 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003028 }
aliguorif7b4a942009-01-07 17:40:15 +00003029 filename = argv[optind++];
3030
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003031 if (qemu_opts_foreach(&qemu_object_opts,
3032 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003033 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003034 return 1;
3035 }
3036
aliguorif7b4a942009-01-07 17:40:15 +00003037 /* Open the image */
Fam Zheng335e9932017-05-03 00:35:39 +08003038 blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
3039 force_share);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003040 if (!blk) {
3041 return 1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003042 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003043 bs = blk_bs(blk);
aliguorif7b4a942009-01-07 17:40:15 +00003044
3045 /* Perform the requested action */
3046 switch(action) {
3047 case SNAPSHOT_LIST:
3048 dump_snapshots(bs);
3049 break;
3050
3051 case SNAPSHOT_CREATE:
3052 memset(&sn, 0, sizeof(sn));
3053 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
3054
3055 qemu_gettimeofday(&tv);
3056 sn.date_sec = tv.tv_sec;
3057 sn.date_nsec = tv.tv_usec * 1000;
3058
3059 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003060 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003061 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00003062 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003063 }
aliguorif7b4a942009-01-07 17:40:15 +00003064 break;
3065
3066 case SNAPSHOT_APPLY:
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01003067 ret = bdrv_snapshot_goto(bs, snapshot_name, &err);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003068 if (ret) {
Kevin Wolf0b62bcb2017-11-20 15:28:41 +01003069 error_reportf_err(err, "Could not apply snapshot '%s': ",
3070 snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003071 }
aliguorif7b4a942009-01-07 17:40:15 +00003072 break;
3073
3074 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003075 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003076 if (err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003077 error_reportf_err(err, "Could not delete snapshot '%s': ",
3078 snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08003079 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003080 }
aliguorif7b4a942009-01-07 17:40:15 +00003081 break;
3082 }
3083
3084 /* Cleanup */
Markus Armbruster26f54e92014-10-07 13:59:04 +02003085 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003086 if (ret) {
3087 return 1;
3088 }
Stuart Brady153859b2009-06-07 00:42:17 +01003089 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00003090}
3091
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003092static int img_rebase(int argc, char **argv)
3093{
Markus Armbruster26f54e92014-10-07 13:59:04 +02003094 BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
Paolo Bonzini396374c2016-02-25 23:53:54 +01003095 uint8_t *buf_old = NULL;
3096 uint8_t *buf_new = NULL;
Max Reitzf1d3cd72015-02-05 13:58:18 -05003097 BlockDriverState *bs = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003098 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02003099 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3100 int c, flags, src_flags, ret;
Kevin Wolfce099542016-03-15 13:01:04 +01003101 bool writethrough, src_writethrough;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003102 int unsafe = 0;
Fam Zheng335e9932017-05-03 00:35:39 +08003103 bool force_share = false;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003104 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003105 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02003106 Error *local_err = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003107 bool image_opts = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003108
3109 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003110 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003111 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02003112 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003113 out_baseimg = NULL;
3114 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003115 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003116 static const struct option long_options[] = {
3117 {"help", no_argument, 0, 'h'},
3118 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003119 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08003120 {"force-share", no_argument, 0, 'U'},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003121 {0, 0, 0, 0}
3122 };
Fam Zheng335e9932017-05-03 00:35:39 +08003123 c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003124 long_options, NULL);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003125 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003126 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003127 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003128 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003129 case ':':
3130 missing_argument(argv[optind - 1]);
3131 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003132 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003133 unrecognized_option(argv[optind - 1]);
3134 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003135 case 'h':
3136 help();
3137 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01003138 case 'f':
3139 fmt = optarg;
3140 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003141 case 'F':
3142 out_basefmt = optarg;
3143 break;
3144 case 'b':
3145 out_baseimg = optarg;
3146 break;
3147 case 'u':
3148 unsafe = 1;
3149 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003150 case 'p':
3151 progress = 1;
3152 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003153 case 't':
3154 cache = optarg;
3155 break;
Max Reitz40055952014-07-22 22:58:42 +02003156 case 'T':
3157 src_cache = optarg;
3158 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003159 case 'q':
3160 quiet = true;
3161 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003162 case OPTION_OBJECT: {
3163 QemuOpts *opts;
3164 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3165 optarg, true);
3166 if (!opts) {
3167 return 1;
3168 }
3169 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003170 case OPTION_IMAGE_OPTS:
3171 image_opts = true;
3172 break;
Fam Zheng335e9932017-05-03 00:35:39 +08003173 case 'U':
3174 force_share = true;
3175 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003176 }
3177 }
3178
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003179 if (quiet) {
3180 progress = 0;
3181 }
3182
Fam Zhengac1307a2014-04-22 13:36:11 +08003183 if (optind != argc - 1) {
3184 error_exit("Expecting one image file name");
3185 }
3186 if (!unsafe && !out_baseimg) {
3187 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01003188 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003189 filename = argv[optind++];
3190
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003191 if (qemu_opts_foreach(&qemu_object_opts,
3192 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003193 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003194 return 1;
3195 }
3196
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003197 qemu_progress_init(progress, 2.0);
3198 qemu_progress_print(0, 100);
3199
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003200 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Kevin Wolfce099542016-03-15 13:01:04 +01003201 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003202 if (ret < 0) {
3203 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003204 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04003205 }
3206
Kevin Wolfce099542016-03-15 13:01:04 +01003207 src_flags = 0;
3208 ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
Max Reitz40055952014-07-22 22:58:42 +02003209 if (ret < 0) {
3210 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003211 goto out;
Max Reitz40055952014-07-22 22:58:42 +02003212 }
3213
Kevin Wolfce099542016-03-15 13:01:04 +01003214 /* The source files are opened read-only, don't care about WCE */
3215 assert((src_flags & BDRV_O_RDWR) == 0);
3216 (void) src_writethrough;
3217
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003218 /*
3219 * Open the images.
3220 *
3221 * Ignore the old backing file for unsafe rebase in case we want to correct
3222 * the reference to a renamed or moved backing file.
3223 */
Fam Zheng335e9932017-05-03 00:35:39 +08003224 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3225 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003226 if (!blk) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003227 ret = -1;
3228 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003229 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003230 bs = blk_bs(blk);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003231
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003232 if (out_basefmt != NULL) {
Max Reitz644483d2015-02-05 13:58:17 -05003233 if (bdrv_find_format(out_basefmt) == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003234 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003235 ret = -1;
3236 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003237 }
3238 }
3239
3240 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01003241 if (!unsafe) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003242 char backing_name[PATH_MAX];
Max Reitz644483d2015-02-05 13:58:17 -05003243 QDict *options = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003244
Max Reitz644483d2015-02-05 13:58:17 -05003245 if (bs->backing_format[0] != '\0') {
3246 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05003247 qdict_put_str(options, "driver", bs->backing_format);
Max Reitz644483d2015-02-05 13:58:17 -05003248 }
3249
Fam Zheng335e9932017-05-03 00:35:39 +08003250 if (force_share) {
3251 if (!options) {
3252 options = qdict_new();
3253 }
Eric Blake579cf1d2017-05-15 14:54:39 -05003254 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Fam Zheng335e9932017-05-03 00:35:39 +08003255 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003256 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzefaa7c42016-03-16 19:54:38 +01003257 blk_old_backing = blk_new_open(backing_name, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003258 options, src_flags, &local_err);
3259 if (!blk_old_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003260 error_reportf_err(local_err,
3261 "Could not open old backing file '%s': ",
3262 backing_name);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003263 ret = -1;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003264 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003265 }
Max Reitz644483d2015-02-05 13:58:17 -05003266
Alex Bligha6166732012-10-16 13:46:18 +01003267 if (out_baseimg[0]) {
Max Reitzd16699b2018-05-09 20:20:01 +02003268 const char *overlay_filename;
3269 char *out_real_path;
3270
Fam Zheng335e9932017-05-03 00:35:39 +08003271 options = qdict_new();
Max Reitz644483d2015-02-05 13:58:17 -05003272 if (out_basefmt) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003273 qdict_put_str(options, "driver", out_basefmt);
Fam Zheng335e9932017-05-03 00:35:39 +08003274 }
3275 if (force_share) {
3276 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
Max Reitz644483d2015-02-05 13:58:17 -05003277 }
3278
Max Reitzd16699b2018-05-09 20:20:01 +02003279 overlay_filename = bs->exact_filename[0] ? bs->exact_filename
3280 : bs->filename;
3281 out_real_path = g_malloc(PATH_MAX);
3282
3283 bdrv_get_full_backing_filename_from_filename(overlay_filename,
3284 out_baseimg,
3285 out_real_path,
3286 PATH_MAX,
3287 &local_err);
3288 if (local_err) {
3289 error_reportf_err(local_err,
3290 "Could not resolve backing filename: ");
3291 ret = -1;
3292 g_free(out_real_path);
3293 goto out;
3294 }
3295
3296 blk_new_backing = blk_new_open(out_real_path, NULL,
Max Reitz644483d2015-02-05 13:58:17 -05003297 options, src_flags, &local_err);
Max Reitzd16699b2018-05-09 20:20:01 +02003298 g_free(out_real_path);
Max Reitz644483d2015-02-05 13:58:17 -05003299 if (!blk_new_backing) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01003300 error_reportf_err(local_err,
3301 "Could not open new backing file '%s': ",
3302 out_baseimg);
Xu Tiane84a0dd2016-10-09 17:17:27 +08003303 ret = -1;
Alex Bligha6166732012-10-16 13:46:18 +01003304 goto out;
3305 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003306 }
3307 }
3308
3309 /*
3310 * Check each unallocated cluster in the COW file. If it is unallocated,
3311 * accesses go to the backing file. We must therefore compare this cluster
3312 * in the old and new backing file, and if they differ we need to copy it
3313 * from the old backing file into the COW file.
3314 *
3315 * If qemu-img crashes during this step, no harm is done. The content of
3316 * the image is the same as the original one at any time.
3317 */
3318 if (!unsafe) {
Eric Blake41536282017-10-11 22:47:15 -05003319 int64_t size;
3320 int64_t old_backing_size;
3321 int64_t new_backing_size = 0;
3322 uint64_t offset;
3323 int64_t n;
Kevin Wolf1f710492012-10-12 14:29:18 +02003324 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08003325
Max Reitzf1d3cd72015-02-05 13:58:18 -05003326 buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3327 buf_new = blk_blockalign(blk, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003328
Eric Blake41536282017-10-11 22:47:15 -05003329 size = blk_getlength(blk);
3330 if (size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003331 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003332 filename, strerror(-size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003333 ret = -1;
3334 goto out;
3335 }
Eric Blake41536282017-10-11 22:47:15 -05003336 old_backing_size = blk_getlength(blk_old_backing);
3337 if (old_backing_size < 0) {
Jeff Cody9a29e182015-01-22 08:03:30 -05003338 char backing_name[PATH_MAX];
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003339
3340 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3341 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003342 backing_name, strerror(-old_backing_size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003343 ret = -1;
3344 goto out;
3345 }
Max Reitzf1d3cd72015-02-05 13:58:18 -05003346 if (blk_new_backing) {
Eric Blake41536282017-10-11 22:47:15 -05003347 new_backing_size = blk_getlength(blk_new_backing);
3348 if (new_backing_size < 0) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003349 error_report("Could not get size of '%s': %s",
Eric Blake41536282017-10-11 22:47:15 -05003350 out_baseimg, strerror(-new_backing_size));
Markus Armbruster52bf1e72014-06-26 13:23:25 +02003351 ret = -1;
3352 goto out;
3353 }
Alex Bligha6166732012-10-16 13:46:18 +01003354 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003355
Eric Blake41536282017-10-11 22:47:15 -05003356 if (size != 0) {
3357 local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE));
Kevin Wolf1f710492012-10-12 14:29:18 +02003358 }
3359
Eric Blake41536282017-10-11 22:47:15 -05003360 for (offset = 0; offset < size; offset += n) {
3361 /* How many bytes can we handle with the next read? */
3362 n = MIN(IO_BUF_SIZE, size - offset);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003363
3364 /* If the cluster is allocated, we don't need to take action */
Eric Blake41536282017-10-11 22:47:15 -05003365 ret = bdrv_is_allocated(bs, offset, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02003366 if (ret < 0) {
3367 error_report("error while reading image metadata: %s",
3368 strerror(-ret));
3369 goto out;
3370 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02003371 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003372 continue;
3373 }
3374
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003375 /*
3376 * Read old and new backing file and take into consideration that
3377 * backing files may be smaller than the COW image.
3378 */
Eric Blake41536282017-10-11 22:47:15 -05003379 if (offset >= old_backing_size) {
3380 memset(buf_old, 0, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003381 } else {
Eric Blake41536282017-10-11 22:47:15 -05003382 if (offset + n > old_backing_size) {
3383 n = old_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003384 }
3385
Eric Blake41536282017-10-11 22:47:15 -05003386 ret = blk_pread(blk_old_backing, offset, buf_old, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003387 if (ret < 0) {
3388 error_report("error while reading from old backing file");
3389 goto out;
3390 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003391 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003392
Eric Blake41536282017-10-11 22:47:15 -05003393 if (offset >= new_backing_size || !blk_new_backing) {
3394 memset(buf_new, 0, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003395 } else {
Eric Blake41536282017-10-11 22:47:15 -05003396 if (offset + n > new_backing_size) {
3397 n = new_backing_size - offset;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003398 }
3399
Eric Blake41536282017-10-11 22:47:15 -05003400 ret = blk_pread(blk_new_backing, offset, buf_new, n);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01003401 if (ret < 0) {
3402 error_report("error while reading from new backing file");
3403 goto out;
3404 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003405 }
3406
3407 /* If they differ, we need to write to the COW file */
3408 uint64_t written = 0;
3409
Eric Blake41536282017-10-11 22:47:15 -05003410 while (written < n) {
Eric Blakedc61cd32017-10-11 22:47:14 -05003411 int64_t pnum;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003412
Eric Blake41536282017-10-11 22:47:15 -05003413 if (compare_buffers(buf_old + written, buf_new + written,
3414 n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003415 {
Eric Blake41536282017-10-11 22:47:15 -05003416 ret = blk_pwrite(blk, offset + written,
Eric Blakedc61cd32017-10-11 22:47:14 -05003417 buf_old + written, pnum, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003418 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003419 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003420 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003421 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003422 }
3423 }
3424
3425 written += pnum;
3426 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003427 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003428 }
3429 }
3430
3431 /*
3432 * Change the backing file. All clusters that are different from the old
3433 * backing file are overwritten in the COW file now, so the visible content
3434 * doesn't change when we switch the backing file.
3435 */
Alex Bligha6166732012-10-16 13:46:18 +01003436 if (out_baseimg && *out_baseimg) {
3437 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3438 } else {
3439 ret = bdrv_change_backing_file(bs, NULL, NULL);
3440 }
3441
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003442 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003443 error_report("Could not change the backing file to '%s': No "
3444 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003445 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003446 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003447 out_baseimg, strerror(-ret));
3448 }
3449
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003450 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003451 /*
3452 * TODO At this point it is possible to check if any clusters that are
3453 * allocated in the COW file are the same in the backing file. If so, they
3454 * could be dropped from the COW file. Don't do this before switching the
3455 * backing file, in case of a crash this would lead to corruption.
3456 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003457out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02003458 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003459 /* Cleanup */
3460 if (!unsafe) {
Markus Armbruster26f54e92014-10-07 13:59:04 +02003461 blk_unref(blk_old_backing);
Markus Armbruster26f54e92014-10-07 13:59:04 +02003462 blk_unref(blk_new_backing);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003463 }
Paolo Bonzini396374c2016-02-25 23:53:54 +01003464 qemu_vfree(buf_old);
3465 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003466
Markus Armbruster26f54e92014-10-07 13:59:04 +02003467 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003468 if (ret) {
3469 return 1;
3470 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01003471 return 0;
3472}
3473
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003474static int img_resize(int argc, char **argv)
3475{
Markus Armbruster6750e792015-02-12 17:43:08 +01003476 Error *err = NULL;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003477 int c, ret, relative;
3478 const char *filename, *fmt, *size;
Max Reitz5279b302018-04-21 18:39:57 +02003479 int64_t n, total_size, current_size, new_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003480 bool quiet = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003481 BlockBackend *blk = NULL;
Max Reitzdc5f6902017-06-13 22:20:55 +02003482 PreallocMode prealloc = PREALLOC_MODE_OFF;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003483 QemuOpts *param;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003484
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003485 static QemuOptsList resize_options = {
3486 .name = "resize_options",
3487 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3488 .desc = {
3489 {
3490 .name = BLOCK_OPT_SIZE,
3491 .type = QEMU_OPT_SIZE,
3492 .help = "Virtual disk size"
3493 }, {
3494 /* end of list */
3495 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003496 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003497 };
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003498 bool image_opts = false;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003499 bool shrink = false;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003500
Kevin Wolfe80fec72011-04-29 10:58:12 +02003501 /* Remove size from argv manually so that negative numbers are not treated
3502 * as options by getopt. */
3503 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003504 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02003505 return 1;
3506 }
3507
3508 size = argv[--argc];
3509
3510 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003511 fmt = NULL;
3512 for(;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003513 static const struct option long_options[] = {
3514 {"help", no_argument, 0, 'h'},
3515 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003516 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Max Reitzdc5f6902017-06-13 22:20:55 +02003517 {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003518 {"shrink", no_argument, 0, OPTION_SHRINK},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003519 {0, 0, 0, 0}
3520 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003521 c = getopt_long(argc, argv, ":f:hq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003522 long_options, NULL);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003523 if (c == -1) {
3524 break;
3525 }
3526 switch(c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003527 case ':':
3528 missing_argument(argv[optind - 1]);
3529 break;
Jes Sorensenef873942010-12-06 15:25:40 +01003530 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003531 unrecognized_option(argv[optind - 1]);
3532 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003533 case 'h':
3534 help();
3535 break;
3536 case 'f':
3537 fmt = optarg;
3538 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01003539 case 'q':
3540 quiet = true;
3541 break;
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003542 case OPTION_OBJECT: {
3543 QemuOpts *opts;
3544 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3545 optarg, true);
3546 if (!opts) {
3547 return 1;
3548 }
3549 } break;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003550 case OPTION_IMAGE_OPTS:
3551 image_opts = true;
3552 break;
Max Reitzdc5f6902017-06-13 22:20:55 +02003553 case OPTION_PREALLOCATION:
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02003554 prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
Markus Armbruster06c60b62017-08-24 10:45:57 +02003555 PREALLOC_MODE__MAX, NULL);
Max Reitzdc5f6902017-06-13 22:20:55 +02003556 if (prealloc == PREALLOC_MODE__MAX) {
3557 error_report("Invalid preallocation mode '%s'", optarg);
3558 return 1;
3559 }
3560 break;
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003561 case OPTION_SHRINK:
3562 shrink = true;
3563 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003564 }
3565 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02003566 if (optind != argc - 1) {
Max Reitzbe8fbd42018-02-05 17:27:45 +01003567 error_exit("Expecting image file name and size");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003568 }
3569 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003570
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003571 if (qemu_opts_foreach(&qemu_object_opts,
3572 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003573 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003574 return 1;
3575 }
3576
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003577 /* Choose grow, shrink, or absolute resize mode */
3578 switch (size[0]) {
3579 case '+':
3580 relative = 1;
3581 size++;
3582 break;
3583 case '-':
3584 relative = -1;
3585 size++;
3586 break;
3587 default:
3588 relative = 0;
3589 break;
3590 }
3591
3592 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08003593 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01003594 qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
Markus Armbruster6750e792015-02-12 17:43:08 +01003595 if (err) {
3596 error_report_err(err);
Jes Sorensen2a819982010-12-06 17:08:31 +01003597 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003598 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01003599 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003600 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08003601 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3602 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003603
Max Reitzefaa7c42016-03-16 19:54:38 +01003604 blk = img_open(image_opts, filename, fmt,
Fam Zheng335e9932017-05-03 00:35:39 +08003605 BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3606 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003607 if (!blk) {
Jes Sorensen2a819982010-12-06 17:08:31 +01003608 ret = -1;
3609 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003610 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003611
Max Reitzdc5f6902017-06-13 22:20:55 +02003612 current_size = blk_getlength(blk);
3613 if (current_size < 0) {
3614 error_report("Failed to inquire current image length: %s",
3615 strerror(-current_size));
3616 ret = -1;
3617 goto out;
3618 }
3619
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003620 if (relative) {
Max Reitzdc5f6902017-06-13 22:20:55 +02003621 total_size = current_size + n * relative;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003622 } else {
3623 total_size = n;
3624 }
3625 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01003626 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003627 ret = -1;
3628 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003629 }
3630
Max Reitzdc5f6902017-06-13 22:20:55 +02003631 if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
3632 error_report("Preallocation can only be used for growing images");
3633 ret = -1;
3634 goto out;
3635 }
3636
Pavel Butsykin4ffca892017-09-18 15:42:27 +03003637 if (total_size < current_size && !shrink) {
3638 warn_report("Shrinking an image will delete all data beyond the "
3639 "shrunken image's end. Before performing such an "
3640 "operation, make sure there is no important data there.");
3641
3642 if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) {
3643 error_report(
3644 "Use the --shrink option to perform a shrink operation.");
3645 ret = -1;
3646 goto out;
3647 } else {
3648 warn_report("Using the --shrink option will suppress this message. "
3649 "Note that future versions of qemu-img may refuse to "
3650 "shrink images without this option.");
3651 }
3652 }
3653
Max Reitzdc5f6902017-06-13 22:20:55 +02003654 ret = blk_truncate(blk, total_size, prealloc, &err);
Max Reitz5279b302018-04-21 18:39:57 +02003655 if (ret < 0) {
Max Reitzed3d2ec2017-03-28 22:51:27 +02003656 error_report_err(err);
Max Reitz5279b302018-04-21 18:39:57 +02003657 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003658 }
Max Reitz5279b302018-04-21 18:39:57 +02003659
3660 new_size = blk_getlength(blk);
3661 if (new_size < 0) {
3662 error_report("Failed to verify truncated image length: %s",
3663 strerror(-new_size));
3664 ret = -1;
3665 goto out;
3666 }
3667
3668 /* Some block drivers implement a truncation method, but only so
3669 * the user can cause qemu to refresh the image's size from disk.
3670 * The idea is that the user resizes the image outside of qemu and
3671 * then invokes block_resize to inform qemu about it.
3672 * (This includes iscsi and file-posix for device files.)
3673 * Of course, that is not the behavior someone invoking
3674 * qemu-img resize would find useful, so we catch that behavior
3675 * here and tell the user. */
3676 if (new_size != total_size && new_size == current_size) {
3677 error_report("Image was not resized; resizing may not be supported "
3678 "for this image");
3679 ret = -1;
3680 goto out;
3681 }
3682
3683 if (new_size != total_size) {
3684 warn_report("Image should have been resized to %" PRIi64
3685 " bytes, but was resized to %" PRIi64 " bytes",
3686 total_size, new_size);
3687 }
3688
3689 qprintf(quiet, "Image resized.\n");
3690
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003691out:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003692 blk_unref(blk);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09003693 if (ret) {
3694 return 1;
3695 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01003696 return 0;
3697}
3698
Max Reitz76a3a342014-10-27 11:12:51 +01003699static void amend_status_cb(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02003700 int64_t offset, int64_t total_work_size,
3701 void *opaque)
Max Reitz76a3a342014-10-27 11:12:51 +01003702{
3703 qemu_progress_print(100.f * offset / total_work_size, 0);
3704}
3705
Max Reitz51641352018-05-09 23:00:20 +02003706static int print_amend_option_help(const char *format)
3707{
3708 BlockDriver *drv;
3709
3710 /* Find driver and parse its options */
3711 drv = bdrv_find_format(format);
3712 if (!drv) {
3713 error_report("Unknown file format '%s'", format);
3714 return 1;
3715 }
3716
3717 if (!drv->bdrv_amend_options) {
3718 error_report("Format driver '%s' does not support option amendment",
3719 format);
3720 return 1;
3721 }
3722
3723 /* Every driver supporting amendment must have create_opts */
3724 assert(drv->create_opts);
3725
3726 printf("Creation options for '%s':\n", format);
3727 qemu_opts_print_help(drv->create_opts);
3728 printf("\nNote that not all of these options may be amendable.\n");
3729 return 0;
3730}
3731
Max Reitz6f176b42013-09-03 10:09:50 +02003732static int img_amend(int argc, char **argv)
3733{
Markus Armbrusterdc523cd342015-02-12 18:37:11 +01003734 Error *err = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003735 int c, ret = 0;
3736 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08003737 QemuOptsList *create_opts = NULL;
3738 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02003739 const char *fmt = NULL, *filename, *cache;
3740 int flags;
Kevin Wolfce099542016-03-15 13:01:04 +01003741 bool writethrough;
Max Reitz76a3a342014-10-27 11:12:51 +01003742 bool quiet = false, progress = false;
Markus Armbruster26f54e92014-10-07 13:59:04 +02003743 BlockBackend *blk = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02003744 BlockDriverState *bs = NULL;
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003745 bool image_opts = false;
Max Reitz6f176b42013-09-03 10:09:50 +02003746
Max Reitzbd39e6e2014-07-22 22:58:43 +02003747 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02003748 for (;;) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003749 static const struct option long_options[] = {
3750 {"help", no_argument, 0, 'h'},
3751 {"object", required_argument, 0, OPTION_OBJECT},
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00003752 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003753 {0, 0, 0, 0}
3754 };
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003755 c = getopt_long(argc, argv, ":ho:f:t:pq",
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003756 long_options, NULL);
Max Reitz6f176b42013-09-03 10:09:50 +02003757 if (c == -1) {
3758 break;
3759 }
3760
3761 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003762 case ':':
3763 missing_argument(argv[optind - 1]);
3764 break;
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003765 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08003766 unrecognized_option(argv[optind - 1]);
3767 break;
3768 case 'h':
Stefan Hajnoczif7077622017-03-17 18:45:40 +08003769 help();
3770 break;
3771 case 'o':
3772 if (!is_valid_option_list(optarg)) {
3773 error_report("Invalid option list: %s", optarg);
3774 ret = -1;
3775 goto out_no_progress;
3776 }
3777 if (!options) {
3778 options = g_strdup(optarg);
3779 } else {
3780 char *old_options = options;
3781 options = g_strdup_printf("%s,%s", options, optarg);
3782 g_free(old_options);
3783 }
3784 break;
3785 case 'f':
3786 fmt = optarg;
3787 break;
3788 case 't':
3789 cache = optarg;
3790 break;
3791 case 'p':
3792 progress = true;
3793 break;
3794 case 'q':
3795 quiet = true;
3796 break;
3797 case OPTION_OBJECT:
3798 opts = qemu_opts_parse_noisily(&qemu_object_opts,
3799 optarg, true);
3800 if (!opts) {
3801 ret = -1;
3802 goto out_no_progress;
3803 }
3804 break;
3805 case OPTION_IMAGE_OPTS:
3806 image_opts = true;
3807 break;
Max Reitz6f176b42013-09-03 10:09:50 +02003808 }
3809 }
3810
Max Reitz6f176b42013-09-03 10:09:50 +02003811 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08003812 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02003813 }
3814
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003815 if (qemu_opts_foreach(&qemu_object_opts,
3816 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +02003817 NULL, NULL)) {
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00003818 ret = -1;
3819 goto out_no_progress;
3820 }
3821
Max Reitz76a3a342014-10-27 11:12:51 +01003822 if (quiet) {
3823 progress = false;
3824 }
3825 qemu_progress_init(progress, 1.0);
3826
Kevin Wolfa283cb62014-02-21 16:24:07 +01003827 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3828 if (fmt && has_help_option(options)) {
3829 /* If a format is explicitly specified (and possibly no filename is
3830 * given), print option help here */
Max Reitz51641352018-05-09 23:00:20 +02003831 ret = print_amend_option_help(fmt);
Kevin Wolfa283cb62014-02-21 16:24:07 +01003832 goto out;
3833 }
3834
3835 if (optind != argc - 1) {
Max Reitzb2f27e42014-10-27 11:12:52 +01003836 error_report("Expecting one image file name");
3837 ret = -1;
3838 goto out;
Kevin Wolfa283cb62014-02-21 16:24:07 +01003839 }
Max Reitz6f176b42013-09-03 10:09:50 +02003840
Kevin Wolfce099542016-03-15 13:01:04 +01003841 flags = BDRV_O_RDWR;
3842 ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
Max Reitzbd39e6e2014-07-22 22:58:43 +02003843 if (ret < 0) {
3844 error_report("Invalid cache option: %s", cache);
3845 goto out;
3846 }
3847
Fam Zheng335e9932017-05-03 00:35:39 +08003848 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3849 false);
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003850 if (!blk) {
Max Reitz6f176b42013-09-03 10:09:50 +02003851 ret = -1;
3852 goto out;
3853 }
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02003854 bs = blk_bs(blk);
Max Reitz6f176b42013-09-03 10:09:50 +02003855
3856 fmt = bs->drv->format_name;
3857
Kevin Wolf626f84f2014-02-21 16:24:06 +01003858 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01003859 /* If the format was auto-detected, print option help here */
Max Reitz51641352018-05-09 23:00:20 +02003860 ret = print_amend_option_help(fmt);
Max Reitz6f176b42013-09-03 10:09:50 +02003861 goto out;
3862 }
3863
Max Reitz1f996682018-05-09 23:00:17 +02003864 if (!bs->drv->bdrv_amend_options) {
3865 error_report("Format driver '%s' does not support option amendment",
Max Reitzb2439d22014-12-02 18:32:47 +01003866 fmt);
3867 ret = -1;
3868 goto out;
3869 }
3870
Max Reitz1f996682018-05-09 23:00:17 +02003871 /* Every driver supporting amendment must have create_opts */
3872 assert(bs->drv->create_opts);
3873
Chunyan Liuc282e1f2014-06-05 17:21:11 +08003874 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08003875 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
Paolo Bonziniece90862017-01-04 15:56:24 +01003876 qemu_opts_do_parse(opts, options, NULL, &err);
3877 if (err) {
3878 error_report_err(err);
3879 ret = -1;
3880 goto out;
Max Reitz6f176b42013-09-03 10:09:50 +02003881 }
3882
Max Reitz76a3a342014-10-27 11:12:51 +01003883 /* In case the driver does not call amend_status_cb() */
3884 qemu_progress_print(0.f, 0);
Max Reitzd1402b52018-05-09 23:00:18 +02003885 ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, &err);
Max Reitz76a3a342014-10-27 11:12:51 +01003886 qemu_progress_print(100.f, 0);
Max Reitz6f176b42013-09-03 10:09:50 +02003887 if (ret < 0) {
Max Reitzd1402b52018-05-09 23:00:18 +02003888 error_report_err(err);
Max Reitz6f176b42013-09-03 10:09:50 +02003889 goto out;
3890 }
3891
3892out:
Max Reitz76a3a342014-10-27 11:12:51 +01003893 qemu_progress_end();
3894
Max Reitze814dff2015-08-20 16:00:38 -07003895out_no_progress:
Markus Armbruster26f54e92014-10-07 13:59:04 +02003896 blk_unref(blk);
Chunyan Liu83d05212014-06-05 17:20:51 +08003897 qemu_opts_del(opts);
3898 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01003899 g_free(options);
3900
Max Reitz6f176b42013-09-03 10:09:50 +02003901 if (ret) {
3902 return 1;
3903 }
3904 return 0;
3905}
3906
Kevin Wolfb6133b82014-08-05 14:17:13 +02003907typedef struct BenchData {
3908 BlockBackend *blk;
3909 uint64_t image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003910 bool write;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003911 int bufsize;
Kevin Wolf83de9be2015-07-13 13:13:17 +02003912 int step;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003913 int nrreq;
3914 int n;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003915 int flush_interval;
3916 bool drain_on_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003917 uint8_t *buf;
3918 QEMUIOVector *qiov;
3919
3920 int in_flight;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003921 bool in_flush;
Kevin Wolfb6133b82014-08-05 14:17:13 +02003922 uint64_t offset;
3923} BenchData;
3924
Kevin Wolf55d539c2016-06-03 13:59:41 +02003925static void bench_undrained_flush_cb(void *opaque, int ret)
3926{
3927 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003928 error_report("Failed flush request: %s", strerror(-ret));
Kevin Wolf55d539c2016-06-03 13:59:41 +02003929 exit(EXIT_FAILURE);
3930 }
3931}
3932
Kevin Wolfb6133b82014-08-05 14:17:13 +02003933static void bench_cb(void *opaque, int ret)
3934{
3935 BenchData *b = opaque;
3936 BlockAIOCB *acb;
3937
3938 if (ret < 0) {
Markus Armbrusterdf3c2862016-08-03 13:37:51 +02003939 error_report("Failed request: %s", strerror(-ret));
Kevin Wolfb6133b82014-08-05 14:17:13 +02003940 exit(EXIT_FAILURE);
3941 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02003942
3943 if (b->in_flush) {
3944 /* Just finished a flush with drained queue: Start next requests */
3945 assert(b->in_flight == 0);
3946 b->in_flush = false;
3947 } else if (b->in_flight > 0) {
3948 int remaining = b->n - b->in_flight;
3949
Kevin Wolfb6133b82014-08-05 14:17:13 +02003950 b->n--;
3951 b->in_flight--;
Kevin Wolf55d539c2016-06-03 13:59:41 +02003952
3953 /* Time for flush? Drain queue if requested, then flush */
3954 if (b->flush_interval && remaining % b->flush_interval == 0) {
3955 if (!b->in_flight || !b->drain_on_flush) {
3956 BlockCompletionFunc *cb;
3957
3958 if (b->drain_on_flush) {
3959 b->in_flush = true;
3960 cb = bench_cb;
3961 } else {
3962 cb = bench_undrained_flush_cb;
3963 }
3964
3965 acb = blk_aio_flush(b->blk, cb, b);
3966 if (!acb) {
3967 error_report("Failed to issue flush request");
3968 exit(EXIT_FAILURE);
3969 }
3970 }
3971 if (b->drain_on_flush) {
3972 return;
3973 }
3974 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003975 }
3976
3977 while (b->n > b->in_flight && b->in_flight < b->nrreq) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003978 int64_t offset = b->offset;
3979 /* blk_aio_* might look for completed I/Os and kick bench_cb
3980 * again, so make sure this operation is counted by in_flight
3981 * and b->offset is ready for the next submission.
3982 */
3983 b->in_flight++;
3984 b->offset += b->step;
3985 b->offset %= b->image_size;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003986 if (b->write) {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003987 acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003988 } else {
Paolo Bonzini4baaa8c2016-12-07 16:08:27 +01003989 acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02003990 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003991 if (!acb) {
3992 error_report("Failed to issue request");
3993 exit(EXIT_FAILURE);
3994 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02003995 }
3996}
3997
3998static int img_bench(int argc, char **argv)
3999{
4000 int c, ret = 0;
4001 const char *fmt = NULL, *filename;
4002 bool quiet = false;
4003 bool image_opts = false;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004004 bool is_write = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004005 int count = 75000;
4006 int depth = 64;
Kevin Wolfd3199a32015-07-10 18:09:18 +02004007 int64_t offset = 0;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004008 size_t bufsize = 4096;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004009 int pattern = 0;
Kevin Wolf83de9be2015-07-13 13:13:17 +02004010 size_t step = 0;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004011 int flush_interval = 0;
4012 bool drain_on_flush = true;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004013 int64_t image_size;
4014 BlockBackend *blk = NULL;
4015 BenchData data = {};
4016 int flags = 0;
Kevin Wolf604e8612016-06-14 11:29:32 +02004017 bool writethrough = false;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004018 struct timeval t1, t2;
4019 int i;
Fam Zheng335e9932017-05-03 00:35:39 +08004020 bool force_share = false;
Fam Zheng79d46582018-01-16 14:08:58 +08004021 size_t buf_size;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004022
4023 for (;;) {
4024 static const struct option long_options[] = {
4025 {"help", no_argument, 0, 'h'},
Kevin Wolf55d539c2016-06-03 13:59:41 +02004026 {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
Kevin Wolfb6133b82014-08-05 14:17:13 +02004027 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004028 {"pattern", required_argument, 0, OPTION_PATTERN},
Kevin Wolf55d539c2016-06-03 13:59:41 +02004029 {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
Fam Zheng335e9932017-05-03 00:35:39 +08004030 {"force-share", no_argument, 0, 'U'},
Kevin Wolfb6133b82014-08-05 14:17:13 +02004031 {0, 0, 0, 0}
4032 };
Fam Zheng335e9932017-05-03 00:35:39 +08004033 c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004034 if (c == -1) {
4035 break;
4036 }
4037
4038 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004039 case ':':
4040 missing_argument(argv[optind - 1]);
4041 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004042 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004043 unrecognized_option(argv[optind - 1]);
4044 break;
4045 case 'h':
Kevin Wolfb6133b82014-08-05 14:17:13 +02004046 help();
4047 break;
4048 case 'c':
4049 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004050 unsigned long res;
4051
4052 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02004053 error_report("Invalid request count specified");
4054 return 1;
4055 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004056 count = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004057 break;
4058 }
4059 case 'd':
4060 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004061 unsigned long res;
4062
4063 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02004064 error_report("Invalid queue depth specified");
4065 return 1;
4066 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004067 depth = res;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004068 break;
4069 }
4070 case 'f':
4071 fmt = optarg;
4072 break;
4073 case 'n':
4074 flags |= BDRV_O_NATIVE_AIO;
4075 break;
Kevin Wolfd3199a32015-07-10 18:09:18 +02004076 case 'o':
4077 {
Markus Armbruster606caa02017-02-21 21:14:04 +01004078 offset = cvtnum(optarg);
4079 if (offset < 0) {
Kevin Wolfd3199a32015-07-10 18:09:18 +02004080 error_report("Invalid offset specified");
4081 return 1;
4082 }
4083 break;
4084 }
4085 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004086 case 'q':
4087 quiet = true;
4088 break;
4089 case 's':
4090 {
4091 int64_t sval;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004092
Markus Armbruster606caa02017-02-21 21:14:04 +01004093 sval = cvtnum(optarg);
4094 if (sval < 0 || sval > INT_MAX) {
Kevin Wolfb6133b82014-08-05 14:17:13 +02004095 error_report("Invalid buffer size specified");
4096 return 1;
4097 }
4098
4099 bufsize = sval;
4100 break;
4101 }
Kevin Wolf83de9be2015-07-13 13:13:17 +02004102 case 'S':
4103 {
4104 int64_t sval;
Kevin Wolf83de9be2015-07-13 13:13:17 +02004105
Markus Armbruster606caa02017-02-21 21:14:04 +01004106 sval = cvtnum(optarg);
4107 if (sval < 0 || sval > INT_MAX) {
Kevin Wolf83de9be2015-07-13 13:13:17 +02004108 error_report("Invalid step size specified");
4109 return 1;
4110 }
4111
4112 step = sval;
4113 break;
4114 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004115 case 't':
4116 ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
4117 if (ret < 0) {
4118 error_report("Invalid cache mode");
4119 ret = -1;
4120 goto out;
4121 }
4122 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004123 case 'w':
4124 flags |= BDRV_O_RDWR;
4125 is_write = true;
4126 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004127 case 'U':
4128 force_share = true;
4129 break;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004130 case OPTION_PATTERN:
4131 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004132 unsigned long res;
4133
4134 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004135 error_report("Invalid pattern byte specified");
4136 return 1;
4137 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004138 pattern = res;
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004139 break;
4140 }
Kevin Wolf55d539c2016-06-03 13:59:41 +02004141 case OPTION_FLUSH_INTERVAL:
4142 {
Peter Maydell8b3c6792017-02-10 16:28:23 +00004143 unsigned long res;
4144
4145 if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004146 error_report("Invalid flush interval specified");
4147 return 1;
4148 }
Peter Maydell8b3c6792017-02-10 16:28:23 +00004149 flush_interval = res;
Kevin Wolf55d539c2016-06-03 13:59:41 +02004150 break;
4151 }
4152 case OPTION_NO_DRAIN:
4153 drain_on_flush = false;
4154 break;
Kevin Wolfb6133b82014-08-05 14:17:13 +02004155 case OPTION_IMAGE_OPTS:
4156 image_opts = true;
4157 break;
4158 }
4159 }
4160
4161 if (optind != argc - 1) {
4162 error_exit("Expecting one image file name");
4163 }
4164 filename = argv[argc - 1];
4165
Kevin Wolf55d539c2016-06-03 13:59:41 +02004166 if (!is_write && flush_interval) {
4167 error_report("--flush-interval is only available in write tests");
4168 ret = -1;
4169 goto out;
4170 }
4171 if (flush_interval && flush_interval < depth) {
4172 error_report("Flush interval can't be smaller than depth");
4173 ret = -1;
4174 goto out;
4175 }
4176
Fam Zheng335e9932017-05-03 00:35:39 +08004177 blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4178 force_share);
Kevin Wolfb6133b82014-08-05 14:17:13 +02004179 if (!blk) {
4180 ret = -1;
4181 goto out;
4182 }
4183
4184 image_size = blk_getlength(blk);
4185 if (image_size < 0) {
4186 ret = image_size;
4187 goto out;
4188 }
4189
4190 data = (BenchData) {
Kevin Wolf55d539c2016-06-03 13:59:41 +02004191 .blk = blk,
4192 .image_size = image_size,
4193 .bufsize = bufsize,
4194 .step = step ?: bufsize,
4195 .nrreq = depth,
4196 .n = count,
4197 .offset = offset,
4198 .write = is_write,
4199 .flush_interval = flush_interval,
4200 .drain_on_flush = drain_on_flush,
Kevin Wolfb6133b82014-08-05 14:17:13 +02004201 };
Kevin Wolfd3199a32015-07-10 18:09:18 +02004202 printf("Sending %d %s requests, %d bytes each, %d in parallel "
Kevin Wolf83de9be2015-07-13 13:13:17 +02004203 "(starting at offset %" PRId64 ", step size %d)\n",
Kevin Wolfd3199a32015-07-10 18:09:18 +02004204 data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
Kevin Wolf83de9be2015-07-13 13:13:17 +02004205 data.offset, data.step);
Kevin Wolf55d539c2016-06-03 13:59:41 +02004206 if (flush_interval) {
4207 printf("Sending flush every %d requests\n", flush_interval);
4208 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004209
Fam Zheng79d46582018-01-16 14:08:58 +08004210 buf_size = data.nrreq * data.bufsize;
4211 data.buf = blk_blockalign(blk, buf_size);
Kevin Wolfb6495fa2015-07-10 18:09:18 +02004212 memset(data.buf, pattern, data.nrreq * data.bufsize);
4213
Fam Zheng79d46582018-01-16 14:08:58 +08004214 blk_register_buf(blk, data.buf, buf_size);
4215
Kevin Wolfb6133b82014-08-05 14:17:13 +02004216 data.qiov = g_new(QEMUIOVector, data.nrreq);
4217 for (i = 0; i < data.nrreq; i++) {
4218 qemu_iovec_init(&data.qiov[i], 1);
4219 qemu_iovec_add(&data.qiov[i],
4220 data.buf + i * data.bufsize, data.bufsize);
4221 }
4222
4223 gettimeofday(&t1, NULL);
4224 bench_cb(&data, 0);
4225
4226 while (data.n > 0) {
4227 main_loop_wait(false);
4228 }
4229 gettimeofday(&t2, NULL);
4230
4231 printf("Run completed in %3.3f seconds.\n",
4232 (t2.tv_sec - t1.tv_sec)
4233 + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4234
4235out:
Fam Zheng79d46582018-01-16 14:08:58 +08004236 if (data.buf) {
4237 blk_unregister_buf(blk, data.buf);
4238 }
Kevin Wolfb6133b82014-08-05 14:17:13 +02004239 qemu_vfree(data.buf);
4240 blk_unref(blk);
4241
4242 if (ret) {
4243 return 1;
4244 }
4245 return 0;
4246}
4247
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004248#define C_BS 01
4249#define C_COUNT 02
4250#define C_IF 04
4251#define C_OF 010
Reda Sallahif7c15532016-08-10 16:16:09 +02004252#define C_SKIP 020
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004253
4254struct DdInfo {
4255 unsigned int flags;
4256 int64_t count;
4257};
4258
4259struct DdIo {
4260 int bsz; /* Block size */
4261 char *filename;
4262 uint8_t *buf;
Reda Sallahif7c15532016-08-10 16:16:09 +02004263 int64_t offset;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004264};
4265
4266struct DdOpts {
4267 const char *name;
4268 int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4269 unsigned int flag;
4270};
4271
4272static int img_dd_bs(const char *arg,
4273 struct DdIo *in, struct DdIo *out,
4274 struct DdInfo *dd)
4275{
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004276 int64_t res;
4277
Markus Armbruster606caa02017-02-21 21:14:04 +01004278 res = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004279
Markus Armbruster606caa02017-02-21 21:14:04 +01004280 if (res <= 0 || res > INT_MAX) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004281 error_report("invalid number: '%s'", arg);
4282 return 1;
4283 }
4284 in->bsz = out->bsz = res;
4285
4286 return 0;
4287}
4288
4289static int img_dd_count(const char *arg,
4290 struct DdIo *in, struct DdIo *out,
4291 struct DdInfo *dd)
4292{
Markus Armbruster606caa02017-02-21 21:14:04 +01004293 dd->count = cvtnum(arg);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004294
Markus Armbruster606caa02017-02-21 21:14:04 +01004295 if (dd->count < 0) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004296 error_report("invalid number: '%s'", arg);
4297 return 1;
4298 }
4299
4300 return 0;
4301}
4302
4303static int img_dd_if(const char *arg,
4304 struct DdIo *in, struct DdIo *out,
4305 struct DdInfo *dd)
4306{
4307 in->filename = g_strdup(arg);
4308
4309 return 0;
4310}
4311
4312static int img_dd_of(const char *arg,
4313 struct DdIo *in, struct DdIo *out,
4314 struct DdInfo *dd)
4315{
4316 out->filename = g_strdup(arg);
4317
4318 return 0;
4319}
4320
Reda Sallahif7c15532016-08-10 16:16:09 +02004321static int img_dd_skip(const char *arg,
4322 struct DdIo *in, struct DdIo *out,
4323 struct DdInfo *dd)
4324{
Markus Armbruster606caa02017-02-21 21:14:04 +01004325 in->offset = cvtnum(arg);
Reda Sallahif7c15532016-08-10 16:16:09 +02004326
Markus Armbruster606caa02017-02-21 21:14:04 +01004327 if (in->offset < 0) {
Reda Sallahif7c15532016-08-10 16:16:09 +02004328 error_report("invalid number: '%s'", arg);
4329 return 1;
4330 }
4331
4332 return 0;
4333}
4334
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004335static int img_dd(int argc, char **argv)
4336{
4337 int ret = 0;
4338 char *arg = NULL;
4339 char *tmp;
4340 BlockDriver *drv = NULL, *proto_drv = NULL;
4341 BlockBackend *blk1 = NULL, *blk2 = NULL;
4342 QemuOpts *opts = NULL;
4343 QemuOptsList *create_opts = NULL;
4344 Error *local_err = NULL;
4345 bool image_opts = false;
4346 int c, i;
4347 const char *out_fmt = "raw";
4348 const char *fmt = NULL;
4349 int64_t size = 0;
4350 int64_t block_count = 0, out_pos, in_pos;
Fam Zheng335e9932017-05-03 00:35:39 +08004351 bool force_share = false;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004352 struct DdInfo dd = {
4353 .flags = 0,
4354 .count = 0,
4355 };
4356 struct DdIo in = {
4357 .bsz = 512, /* Block size is by default 512 bytes */
4358 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004359 .buf = NULL,
4360 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004361 };
4362 struct DdIo out = {
4363 .bsz = 512,
4364 .filename = NULL,
Reda Sallahif7c15532016-08-10 16:16:09 +02004365 .buf = NULL,
4366 .offset = 0
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004367 };
4368
4369 const struct DdOpts options[] = {
4370 { "bs", img_dd_bs, C_BS },
4371 { "count", img_dd_count, C_COUNT },
4372 { "if", img_dd_if, C_IF },
4373 { "of", img_dd_of, C_OF },
Reda Sallahif7c15532016-08-10 16:16:09 +02004374 { "skip", img_dd_skip, C_SKIP },
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004375 { NULL, NULL, 0 }
4376 };
4377 const struct option long_options[] = {
4378 { "help", no_argument, 0, 'h'},
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004379 { "object", required_argument, 0, OPTION_OBJECT},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004380 { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
Fam Zheng335e9932017-05-03 00:35:39 +08004381 { "force-share", no_argument, 0, 'U'},
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004382 { 0, 0, 0, 0 }
4383 };
4384
Fam Zheng335e9932017-05-03 00:35:39 +08004385 while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004386 if (c == EOF) {
4387 break;
4388 }
4389 switch (c) {
4390 case 'O':
4391 out_fmt = optarg;
4392 break;
4393 case 'f':
4394 fmt = optarg;
4395 break;
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004396 case ':':
4397 missing_argument(argv[optind - 1]);
4398 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004399 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004400 unrecognized_option(argv[optind - 1]);
4401 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004402 case 'h':
4403 help();
4404 break;
Fam Zheng335e9932017-05-03 00:35:39 +08004405 case 'U':
4406 force_share = true;
4407 break;
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004408 case OPTION_OBJECT:
4409 if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004410 ret = -1;
4411 goto out;
4412 }
Stefan Hajnoczi2a245702017-06-19 16:00:02 +01004413 break;
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004414 case OPTION_IMAGE_OPTS:
4415 image_opts = true;
4416 break;
4417 }
4418 }
4419
4420 for (i = optind; i < argc; i++) {
4421 int j;
4422 arg = g_strdup(argv[i]);
4423
4424 tmp = strchr(arg, '=');
4425 if (tmp == NULL) {
4426 error_report("unrecognized operand %s", arg);
4427 ret = -1;
4428 goto out;
4429 }
4430
4431 *tmp++ = '\0';
4432
4433 for (j = 0; options[j].name != NULL; j++) {
4434 if (!strcmp(arg, options[j].name)) {
4435 break;
4436 }
4437 }
4438 if (options[j].name == NULL) {
4439 error_report("unrecognized operand %s", arg);
4440 ret = -1;
4441 goto out;
4442 }
4443
4444 if (options[j].f(tmp, &in, &out, &dd) != 0) {
4445 ret = -1;
4446 goto out;
4447 }
4448 dd.flags |= options[j].flag;
4449 g_free(arg);
4450 arg = NULL;
4451 }
4452
4453 if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4454 error_report("Must specify both input and output files");
4455 ret = -1;
4456 goto out;
4457 }
Daniel P. Berrange83d4bf92017-05-15 17:47:09 +01004458
4459 if (qemu_opts_foreach(&qemu_object_opts,
4460 user_creatable_add_opts_foreach,
4461 NULL, NULL)) {
4462 ret = -1;
4463 goto out;
4464 }
4465
Fam Zheng335e9932017-05-03 00:35:39 +08004466 blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4467 force_share);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004468
4469 if (!blk1) {
4470 ret = -1;
4471 goto out;
4472 }
4473
4474 drv = bdrv_find_format(out_fmt);
4475 if (!drv) {
4476 error_report("Unknown file format");
4477 ret = -1;
4478 goto out;
4479 }
4480 proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4481
4482 if (!proto_drv) {
4483 error_report_err(local_err);
4484 ret = -1;
4485 goto out;
4486 }
4487 if (!drv->create_opts) {
4488 error_report("Format driver '%s' does not support image creation",
4489 drv->format_name);
4490 ret = -1;
4491 goto out;
4492 }
4493 if (!proto_drv->create_opts) {
4494 error_report("Protocol driver '%s' does not support image creation",
4495 proto_drv->format_name);
4496 ret = -1;
4497 goto out;
4498 }
4499 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4500 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4501
4502 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4503
4504 size = blk_getlength(blk1);
4505 if (size < 0) {
4506 error_report("Failed to get size for '%s'", in.filename);
4507 ret = -1;
4508 goto out;
4509 }
4510
4511 if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4512 dd.count * in.bsz < size) {
4513 size = dd.count * in.bsz;
4514 }
4515
Reda Sallahif7c15532016-08-10 16:16:09 +02004516 /* Overflow means the specified offset is beyond input image's size */
4517 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4518 size < in.bsz * in.offset)) {
4519 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4520 } else {
4521 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4522 size - in.bsz * in.offset, &error_abort);
4523 }
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004524
4525 ret = bdrv_create(drv, out.filename, opts, &local_err);
4526 if (ret < 0) {
4527 error_reportf_err(local_err,
4528 "%s: error while creating output image: ",
4529 out.filename);
4530 ret = -1;
4531 goto out;
4532 }
4533
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004534 /* TODO, we can't honour --image-opts for the target,
4535 * since it needs to be given in a format compatible
4536 * with the bdrv_create() call above which does not
4537 * support image-opts style.
4538 */
Daniel P. Berrange29cf9332017-05-15 17:47:12 +01004539 blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
Daniel P. Berrangeea204dd2017-05-15 17:47:10 +01004540 false, false, false);
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004541
4542 if (!blk2) {
4543 ret = -1;
4544 goto out;
4545 }
4546
Reda Sallahif7c15532016-08-10 16:16:09 +02004547 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4548 size < in.offset * in.bsz)) {
4549 /* We give a warning if the skip option is bigger than the input
4550 * size and create an empty output disk image (i.e. like dd(1)).
4551 */
4552 error_report("%s: cannot skip to specified offset", in.filename);
4553 in_pos = size;
4554 } else {
4555 in_pos = in.offset * in.bsz;
4556 }
4557
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004558 in.buf = g_new(uint8_t, in.bsz);
4559
Reda Sallahif7c15532016-08-10 16:16:09 +02004560 for (out_pos = 0; in_pos < size; block_count++) {
Reda Sallahi86ce1f62016-08-10 04:43:12 +02004561 int in_ret, out_ret;
4562
4563 if (in_pos + in.bsz > size) {
4564 in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4565 } else {
4566 in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4567 }
4568 if (in_ret < 0) {
4569 error_report("error while reading from input image file: %s",
4570 strerror(-in_ret));
4571 ret = -1;
4572 goto out;
4573 }
4574 in_pos += in_ret;
4575
4576 out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4577
4578 if (out_ret < 0) {
4579 error_report("error while writing to output image file: %s",
4580 strerror(-out_ret));
4581 ret = -1;
4582 goto out;
4583 }
4584 out_pos += out_ret;
4585 }
4586
4587out:
4588 g_free(arg);
4589 qemu_opts_del(opts);
4590 qemu_opts_free(create_opts);
4591 blk_unref(blk1);
4592 blk_unref(blk2);
4593 g_free(in.filename);
4594 g_free(out.filename);
4595 g_free(in.buf);
4596 g_free(out.buf);
4597
4598 if (ret) {
4599 return 1;
4600 }
4601 return 0;
4602}
4603
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004604static void dump_json_block_measure_info(BlockMeasureInfo *info)
4605{
4606 QString *str;
4607 QObject *obj;
4608 Visitor *v = qobject_output_visitor_new(&obj);
4609
4610 visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
4611 visit_complete(v, &obj);
4612 str = qobject_to_json_pretty(obj);
4613 assert(str != NULL);
4614 printf("%s\n", qstring_get_str(str));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004615 qobject_unref(obj);
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004616 visit_free(v);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02004617 qobject_unref(str);
Stefan Hajnoczifd03c2b2017-07-05 13:57:36 +01004618}
4619
4620static int img_measure(int argc, char **argv)
4621{
4622 static const struct option long_options[] = {
4623 {"help", no_argument, 0, 'h'},
4624 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4625 {"object", required_argument, 0, OPTION_OBJECT},
4626 {"output", required_argument, 0, OPTION_OUTPUT},
4627 {"size", required_argument, 0, OPTION_SIZE},
4628 {"force-share", no_argument, 0, 'U'},
4629 {0, 0, 0, 0}
4630 };
4631 OutputFormat output_format = OFORMAT_HUMAN;
4632 BlockBackend *in_blk = NULL;
4633 BlockDriver *drv;
4634 const char *filename = NULL;
4635 const char *fmt = NULL;
4636 const char *out_fmt = "raw";
4637 char *options = NULL;
4638 char *snapshot_name = NULL;
4639 bool force_share = false;
4640 QemuOpts *opts = NULL;
4641 QemuOpts *object_opts = NULL;
4642 QemuOpts *sn_opts = NULL;
4643 QemuOptsList *create_opts = NULL;
4644 bool image_opts = false;
4645 uint64_t img_size = UINT64_MAX;
4646 BlockMeasureInfo *info = NULL;
4647 Error *local_err = NULL;
4648 int ret = 1;
4649 int c;
4650
4651 while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
4652 long_options, NULL)) != -1) {
4653 switch (c) {
4654 case '?':
4655 case 'h':
4656 help();
4657 break;
4658 case 'f':
4659 fmt = optarg;
4660 break;
4661 case 'O':
4662 out_fmt = optarg;
4663 break;
4664 case 'o':
4665 if (!is_valid_option_list(optarg)) {
4666 error_report("Invalid option list: %s", optarg);
4667 goto out;
4668 }
4669 if (!options) {
4670 options = g_strdup(optarg);
4671 } else {
4672 char *old_options = options;
4673 options = g_strdup_printf("%s,%s", options, optarg);
4674 g_free(old_options);
4675 }
4676 break;
4677 case 'l':
4678 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
4679 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
4680 optarg, false);
4681 if (!sn_opts) {
4682 error_report("Failed in parsing snapshot param '%s'",
4683 optarg);
4684 goto out;
4685 }
4686 } else {
4687 snapshot_name = optarg;
4688 }
4689 break;
4690 case 'U':
4691 force_share = true;
4692 break;
4693 case OPTION_OBJECT:
4694 object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
4695 optarg, true);
4696 if (!object_opts) {
4697 goto out;
4698 }
4699 break;
4700 case OPTION_IMAGE_OPTS:
4701 image_opts = true;
4702 break;
4703 case OPTION_OUTPUT:
4704 if (!strcmp(optarg, "json")) {
4705 output_format = OFORMAT_JSON;
4706 } else if (!strcmp(optarg, "human")) {
4707 output_format = OFORMAT_HUMAN;
4708 } else {
4709 error_report("--output must be used with human or json "
4710 "as argument.");
4711 goto out;
4712 }
4713 break;
4714 case OPTION_SIZE:
4715 {
4716 int64_t sval;
4717
4718 sval = cvtnum(optarg);
4719 if (sval < 0) {
4720 if (sval == -ERANGE) {
4721 error_report("Image size must be less than 8 EiB!");
4722 } else {
4723 error_report("Invalid image size specified! You may use "
4724 "k, M, G, T, P or E suffixes for ");
4725 error_report("kilobytes, megabytes, gigabytes, terabytes, "
4726 "petabytes and exabytes.");
4727 }
4728 goto out;
4729 }
4730 img_size = (uint64_t)sval;
4731 }
4732 break;
4733 }
4734 }
4735
4736 if (qemu_opts_foreach(&qemu_object_opts,
4737 user_creatable_add_opts_foreach,
4738 NULL, NULL)) {
4739 goto out;
4740 }
4741
4742 if (argc - optind > 1) {
4743 error_report("At most one filename argument is allowed.");
4744 goto out;
4745 } else if (argc - optind == 1) {
4746 filename = argv[optind];
4747 }
4748
4749 if (!filename &&
4750 (object_opts || image_opts || fmt || snapshot_name || sn_opts)) {
4751 error_report("--object, --image-opts, -f, and -l "
4752 "require a filename argument.");
4753 goto out;
4754 }
4755 if (filename && img_size != UINT64_MAX) {
4756 error_report("--size N cannot be used together with a filename.");
4757 goto out;
4758 }
4759 if (!filename && img_size == UINT64_MAX) {
4760 error_report("Either --size N or one filename must be specified.");
4761 goto out;
4762 }
4763
4764 if (filename) {
4765 in_blk = img_open(image_opts, filename, fmt, 0,
4766 false, false, force_share);
4767 if (!in_blk) {
4768 goto out;
4769 }
4770
4771 if (sn_opts) {
4772 bdrv_snapshot_load_tmp(blk_bs(in_blk),
4773 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
4774 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
4775 &local_err);
4776 } else if (snapshot_name != NULL) {
4777 bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
4778 snapshot_name, &local_err);
4779 }
4780 if (local_err) {
4781 error_reportf_err(local_err, "Failed to load snapshot: ");
4782 goto out;
4783 }
4784 }
4785
4786 drv = bdrv_find_format(out_fmt);
4787 if (!drv) {
4788 error_report("Unknown file format '%s'", out_fmt);
4789 goto out;
4790 }
4791 if (!drv->create_opts) {
4792 error_report("Format driver '%s' does not support image creation",
4793 drv->format_name);
4794 goto out;
4795 }
4796
4797 create_opts = qemu_opts_append(create_opts, drv->create_opts);
4798 create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
4799 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4800 if (options) {
4801 qemu_opts_do_parse(opts, options, NULL, &local_err);
4802 if (local_err) {
4803 error_report_err(local_err);
4804 error_report("Invalid options for file format '%s'", out_fmt);
4805 goto out;
4806 }
4807 }
4808 if (img_size != UINT64_MAX) {
4809 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
4810 }
4811
4812 info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
4813 if (local_err) {
4814 error_report_err(local_err);
4815 goto out;
4816 }
4817
4818 if (output_format == OFORMAT_HUMAN) {
4819 printf("required size: %" PRIu64 "\n", info->required);
4820 printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
4821 } else {
4822 dump_json_block_measure_info(info);
4823 }
4824
4825 ret = 0;
4826
4827out:
4828 qapi_free_BlockMeasureInfo(info);
4829 qemu_opts_del(object_opts);
4830 qemu_opts_del(opts);
4831 qemu_opts_del(sn_opts);
4832 qemu_opts_free(create_opts);
4833 g_free(options);
4834 blk_unref(in_blk);
4835 return ret;
4836}
Kevin Wolfb6133b82014-08-05 14:17:13 +02004837
Anthony Liguoric227f092009-10-01 16:12:16 -05004838static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01004839#define DEF(option, callback, arg_string) \
4840 { option, callback },
4841#include "qemu-img-cmds.h"
4842#undef DEF
Stuart Brady153859b2009-06-07 00:42:17 +01004843 { NULL, NULL, },
4844};
4845
bellardea2384d2004-08-01 21:59:26 +00004846int main(int argc, char **argv)
4847{
Anthony Liguoric227f092009-10-01 16:12:16 -05004848 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01004849 const char *cmdname;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004850 Error *local_error = NULL;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004851 char *trace_file = NULL;
Jeff Cody7db16892014-04-25 17:02:32 -04004852 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04004853 static const struct option long_options[] = {
4854 {"help", no_argument, 0, 'h'},
Denis V. Lunev10985132016-06-17 17:44:13 +03004855 {"version", no_argument, 0, 'V'},
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004856 {"trace", required_argument, NULL, 'T'},
Jeff Cody7db16892014-04-25 17:02:32 -04004857 {0, 0, 0, 0}
4858 };
bellardea2384d2004-08-01 21:59:26 +00004859
MORITA Kazutaka526eda12013-07-23 17:30:11 +09004860#ifdef CONFIG_POSIX
4861 signal(SIGPIPE, SIG_IGN);
4862#endif
4863
Daniel P. Berrangefe4db842016-10-04 14:35:52 +01004864 module_call_init(MODULE_INIT_TRACE);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004865 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08004866 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01004867
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004868 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01004869 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03004870 exit(EXIT_FAILURE);
4871 }
4872
Eduardo Habkoste8f2d272016-05-12 11:10:04 -03004873 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +01004874
Daniel P. Berrange064097d2016-02-10 18:41:01 +00004875 module_call_init(MODULE_INIT_QOM);
bellardea2384d2004-08-01 21:59:26 +00004876 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08004877 if (argc < 2) {
4878 error_exit("Not enough arguments");
4879 }
Stuart Brady153859b2009-06-07 00:42:17 +01004880
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004881 qemu_add_opts(&qemu_object_opts);
Daniel P. Berrangeeb769f72016-02-17 10:10:20 +00004882 qemu_add_opts(&qemu_source_opts);
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004883 qemu_add_opts(&qemu_trace_opts);
Daniel P. Berrange3babeb12016-02-17 10:10:17 +00004884
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004885 while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
Denis V. Lunev10985132016-06-17 17:44:13 +03004886 switch (c) {
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004887 case ':':
4888 missing_argument(argv[optind - 1]);
4889 return 0;
Stefan Hajnoczi4581c162017-03-17 18:45:39 +08004890 case '?':
Stefan Hajnoczic9192972017-03-17 18:45:41 +08004891 unrecognized_option(argv[optind - 1]);
4892 return 0;
4893 case 'h':
Denis V. Lunev10985132016-06-17 17:44:13 +03004894 help();
4895 return 0;
4896 case 'V':
4897 printf(QEMU_IMG_VERSION);
4898 return 0;
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004899 case 'T':
4900 g_free(trace_file);
4901 trace_file = trace_opt_parse(optarg);
4902 break;
Stuart Brady153859b2009-06-07 00:42:17 +01004903 }
bellardea2384d2004-08-01 21:59:26 +00004904 }
Stuart Brady153859b2009-06-07 00:42:17 +01004905
Denis V. Lunev10985132016-06-17 17:44:13 +03004906 cmdname = argv[optind];
Jeff Cody7db16892014-04-25 17:02:32 -04004907
Denis V. Lunev10985132016-06-17 17:44:13 +03004908 /* reset getopt_long scanning */
4909 argc -= optind;
4910 if (argc < 1) {
Jeff Cody5f6979c2014-04-28 14:37:18 -04004911 return 0;
4912 }
Denis V. Lunev10985132016-06-17 17:44:13 +03004913 argv += optind;
Denis V. Lunevcfef6a42016-07-04 16:16:48 +03004914 optind = 0;
Denis V. Lunev10985132016-06-17 17:44:13 +03004915
Denis V. Lunev06a1e0c2016-06-17 17:44:14 +03004916 if (!trace_init_backends()) {
4917 exit(1);
4918 }
4919 trace_init_file(trace_file);
4920 qemu_set_log(LOG_TRACE);
4921
Denis V. Lunev10985132016-06-17 17:44:13 +03004922 /* find the command */
4923 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4924 if (!strcmp(cmdname, cmd->name)) {
4925 return cmd->handler(argc, argv);
4926 }
4927 }
Jeff Cody7db16892014-04-25 17:02:32 -04004928
Stuart Brady153859b2009-06-07 00:42:17 +01004929 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08004930 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00004931}